Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
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
|
@@ -15,7 +16,13 @@ def get_current_temperature(query: str) -> str:
|
|
15 |
return "It's sunny and 75°F."
|
16 |
|
17 |
def search_wikipedia(query: str) -> str:
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
tools = [
|
21 |
Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
|
@@ -27,9 +34,9 @@ tools = [
|
|
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")
|
31 |
self.prompt = ChatPromptTemplate.from_messages([
|
32 |
-
("system", "You are a helpful but sassy assistant"),
|
33 |
MessagesPlaceholder(variable_name="chat_history"),
|
34 |
("user", "{input}"),
|
35 |
MessagesPlaceholder(variable_name="agent_scratchpad")
|
@@ -46,10 +53,11 @@ class cbfs:
|
|
46 |
if not query:
|
47 |
return "Please enter a query."
|
48 |
try:
|
49 |
-
result = self.chain.invoke({"input": query
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
except Exception as e:
|
54 |
print("Execution Error:", str(e))
|
55 |
return f"Error: {str(e)}"
|
@@ -71,3 +79,4 @@ demo.launch(share=True)
|
|
71 |
|
72 |
|
73 |
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import wikipedia
|
4 |
from langchain_community.chat_models import ChatOpenAI
|
5 |
from langchain.memory import ConversationBufferMemory
|
6 |
from langchain.agents import AgentExecutor, initialize_agent
|
|
|
16 |
return "It's sunny and 75°F."
|
17 |
|
18 |
def search_wikipedia(query: str) -> str:
|
19 |
+
try:
|
20 |
+
summary = wikipedia.summary(query, sentences=2)
|
21 |
+
return summary
|
22 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
23 |
+
return f"Multiple results found: {', '.join(e.options[:5])}"
|
24 |
+
except wikipedia.exceptions.PageError:
|
25 |
+
return "No relevant Wikipedia page found."
|
26 |
|
27 |
tools = [
|
28 |
Tool(name="Temperature", func=get_current_temperature, description="Get current temperature"),
|
|
|
34 |
class cbfs:
|
35 |
def __init__(self, tools):
|
36 |
self.model = ChatOpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
|
37 |
+
self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history", ai_prefix="Assistant")
|
38 |
self.prompt = ChatPromptTemplate.from_messages([
|
39 |
+
("system", "You are a helpful but sassy assistant. Remember what the user tells you in the conversation."),
|
40 |
MessagesPlaceholder(variable_name="chat_history"),
|
41 |
("user", "{input}"),
|
42 |
MessagesPlaceholder(variable_name="agent_scratchpad")
|
|
|
53 |
if not query:
|
54 |
return "Please enter a query."
|
55 |
try:
|
56 |
+
result = self.chain.invoke({"input": query})
|
57 |
+
response = result.get("output", "No response generated.")
|
58 |
+
self.memory.save_context({"input": query}, {"output": response})
|
59 |
+
print("Agent Execution Result:", response) # Debugging output
|
60 |
+
return response
|
61 |
except Exception as e:
|
62 |
print("Execution Error:", str(e))
|
63 |
return f"Error: {str(e)}"
|
|
|
79 |
|
80 |
|
81 |
|
82 |
+
|