Spaces:
Runtime error
Runtime error
thoristhor
commited on
Commit
•
1f95db9
1
Parent(s):
12b142f
Update app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import pinecone
|
4 |
+
from gpt_index import GPTIndexMemory, GPTPineconeIndex
|
5 |
+
from langchain.agents import Tool
|
6 |
+
from langchain.chains.conversation.memory import ConversationBufferMemory
|
7 |
+
from langchain import OpenAI
|
8 |
+
from langchain.agents import initialize_agent
|
9 |
+
|
10 |
+
OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
|
11 |
+
PINECONE_API_KEY=os.environ["PINECONE_API_KEY"]
|
12 |
+
|
13 |
+
pinecone.init(api_key=PINECONE_API_KEY, environment="us-east1-gcp")
|
14 |
+
|
15 |
+
pindex=pinecone.Index("sejarah")
|
16 |
+
indexed_pinecone=GPTPineconeIndex([], pinecone_index=pindex)
|
17 |
+
|
18 |
+
tools = [
|
19 |
+
Tool(
|
20 |
+
name = "GPT Index",
|
21 |
+
func=lambda q: str(indexed_pinecone.query(q)),
|
22 |
+
description="useful for when you want to answer questions about the author. The input to this tool should be a complete english sentence.",
|
23 |
+
return_direct=True
|
24 |
+
)
|
25 |
+
]
|
26 |
+
memory = GPTIndexMemory(index=indexed_pinecone, memory_key="chat_history", query_kwargs={"response_mode": "compact"})
|
27 |
+
llm=LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo"))
|
28 |
+
agent_chain = initialize_agent(tools, llm, agent="conversational-react-description", memory=memory, verbose=True)
|
29 |
+
|
30 |
+
def predict(input, history=[]):
|
31 |
+
response = agent_chain.run(input)
|
32 |
+
history = history + [(input, response)]
|
33 |
+
response = history
|
34 |
+
# response = [response]
|
35 |
+
# return response, response
|
36 |
+
return response, response
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
chatbot = gr.Chatbot()
|
40 |
+
state = gr.State([])
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
44 |
+
|
45 |
+
txt.submit(predict, [txt, state], [chatbot, state])
|
46 |
+
# txt.submit(agent_executor.run, [txt, state], [chatbot, state])
|
47 |
+
|
48 |
+
demo.launch()
|