dlaima commited on
Commit
9d05c23
·
verified ·
1 Parent(s): c528499

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -31
app.py CHANGED
@@ -1,35 +1,30 @@
1
- import os
2
  import gradio as gr
3
- from langchain.agents import initialize_agent
4
- #from langchain.chat_models import ChatOpenAI
5
  from langchain_community.chat_models import ChatOpenAI
6
  from langchain.memory import ConversationBufferMemory
 
7
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
8
  from langchain.tools import Tool
9
 
10
- # Define the tool
11
-
12
  def create_your_own(query: str) -> str:
13
  """This function can do whatever you would like once you fill it in"""
14
  return query[::-1]
15
 
16
- # Define other tools (example placeholders for context)
17
  def get_current_temperature(query: str) -> str:
18
  return "It's sunny and 75°F."
19
 
20
  def search_wikipedia(query: str) -> str:
21
  return "Wikipedia search results for: " + query
22
 
23
- # Add the new tool to the list of available tools
24
  tools = [
25
  Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
26
  Tool(name="Search Wikipedia", func=search_wikipedia, description="Search Wikipedia"),
27
  Tool(name="Create Your Own", func=create_your_own, description="Custom tool for processing input")
28
  ]
29
 
30
- # Define the cbfs class for handling the agent
31
  class cbfs:
32
-
33
  def __init__(self, tools):
34
  self.model = ChatOpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
35
  self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
@@ -39,47 +34,39 @@ class cbfs:
39
  ("user", "{input}"),
40
  MessagesPlaceholder(variable_name="agent_scratchpad")
41
  ])
42
-
43
  self.chain = initialize_agent(
44
- tools=tools,
45
- llm=self.model,
46
- #agent="chat-conversational-react-description",
47
  agent="zero-shot-react-description",
48
- memory=self.memory,
49
- verbose=True
50
  )
51
 
52
- #def convchain(self, query):
53
- #if not query:
54
- #return "Please enter a query."
55
- #result = self.chain.invoke({"input": query})
56
- #return result.get('output_text', "No response generated.")
57
-
58
  def convchain(self, query):
59
  if not query:
60
  return "Please enter a query."
61
- try:
62
- result = self.chain.invoke({"input": query})
63
- print("Agent Result:", result) # Debugging line
64
- return result.get('output_text', "No response generated.")
65
- except Exception as e:
66
- return f"Error: {str(e)}"
 
67
 
68
- # Create an instance of the agent
69
  cb = cbfs(tools)
70
 
71
- # Create the Gradio interface
72
  def process_query(query):
73
  return cb.convchain(query)
74
 
75
- # Set up the Gradio interface
76
  with gr.Blocks() as demo:
77
  with gr.Row():
78
  inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
79
  output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
80
-
81
  inp.submit(process_query, inputs=inp, outputs=output)
82
 
83
  demo.launch(share=True)
84
 
85
 
 
 
 
1
  import gradio as gr
2
+ import os
 
3
  from langchain_community.chat_models import ChatOpenAI
4
  from langchain.memory import ConversationBufferMemory
5
+ from langchain.agents import AgentExecutor, initialize_agent
6
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
7
  from langchain.tools import Tool
8
 
9
+ # Define tools
 
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
  def get_current_temperature(query: str) -> str:
15
  return "It's sunny and 75°F."
16
 
17
  def search_wikipedia(query: str) -> str:
18
  return "Wikipedia search results for: " + query
19
 
 
20
  tools = [
21
  Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
22
  Tool(name="Search Wikipedia", func=search_wikipedia, description="Search Wikipedia"),
23
  Tool(name="Create Your Own", func=create_your_own, description="Custom tool for processing input")
24
  ]
25
 
26
+ # Define chatbot class
27
  class cbfs:
 
28
  def __init__(self, tools):
29
  self.model = ChatOpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
30
  self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
 
34
  ("user", "{input}"),
35
  MessagesPlaceholder(variable_name="agent_scratchpad")
36
  ])
 
37
  self.chain = initialize_agent(
38
+ tools=tools,
39
+ llm=self.model,
 
40
  agent="zero-shot-react-description",
41
+ verbose=True,
42
+ memory=self.memory
43
  )
44
 
 
 
 
 
 
 
45
  def convchain(self, query):
46
  if not query:
47
  return "Please enter a query."
48
+ try:
49
+ result = self.chain.invoke({"input": query})
50
+ print("Agent Execution Result:", result) # Debugging output
51
+ return result.get("output", "No response generated.")
52
+ except Exception as e:
53
+ print("Execution Error:", str(e))
54
+ return f"Error: {str(e)}"
55
 
56
+ # Create chatbot instance
57
  cb = cbfs(tools)
58
 
 
59
  def process_query(query):
60
  return cb.convchain(query)
61
 
62
+ # Define Gradio UI
63
  with gr.Blocks() as demo:
64
  with gr.Row():
65
  inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
66
  output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
 
67
  inp.submit(process_query, inputs=inp, outputs=output)
68
 
69
  demo.launch(share=True)
70
 
71
 
72
+