dlaima commited on
Commit
f158561
·
verified ·
1 Parent(s): 5e1d037

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.agents import initialize_agent, AgentType
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.memory import ConversationBufferMemory
5
+ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
6
+ from langchain.agents import AgentExecutor
7
+ from langchain.tools import Tool
8
+
9
+ # Define the tool
10
+ def create_your_own(query: str) -> str:
11
+ """This function can do whatever you would like once you fill it in"""
12
+ return query[::-1]
13
+
14
+ # Define other tools (example placeholders for context)
15
+ def get_current_temperature(query: str) -> str:
16
+ return "It's sunny and 75°F."
17
+
18
+ def search_wikipedia(query: str) -> str:
19
+ return "Wikipedia search results for: " + query
20
+
21
+ # Add the new tool to the list of available tools
22
+ tools = [Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
23
+ Tool(name="Search Wikipedia", func=search_wikipedia, description="Search Wikipedia"),
24
+ Tool(name="Create Your Own", func=create_your_own, description="Custom tool for processing input")]
25
+
26
+ # Define the cbfs class for handling the agent
27
+ class cbfs:
28
+
29
+ def __init__(self, tools):
30
+ self.panels = []
31
+ self.functions = [tool.func for tool in tools]
32
+ self.model = ChatOpenAI(temperature=0)
33
+ self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
34
+ self.prompt = ChatPromptTemplate.from_messages([
35
+ ("system", "You are helpful but sassy assistant"),
36
+ MessagesPlaceholder(variable_name="chat_history"),
37
+ ("user", "{input}"),
38
+ MessagesPlaceholder(variable_name="agent_scratchpad")
39
+ ])
40
+ self.chain = self.model.bind_tools(tools) # Connecting the tools to the model
41
+ self.qa = AgentExecutor(agent=self.chain, tools=tools, verbose=False, memory=self.memory)
42
+
43
+ def convchain(self, query):
44
+ if not query:
45
+ return "Please enter a query."
46
+ result = self.qa.invoke({"input": query})
47
+ self.answer = result['output']
48
+ return self.answer
49
+
50
+ # Create an instance of the agent
51
+ cb = cbfs(tools)
52
+
53
+ # Create the Gradio interface
54
+ def process_query(query):
55
+ return cb.convchain(query)
56
+
57
+ # Set up the Gradio interface
58
+ with gr.Blocks() as demo:
59
+ with gr.Row():
60
+ inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
61
+ output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
62
+ inp.submit(process_query, inputs=inp, outputs=output)
63
+
64
+ demo.launch(share=True)