-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
216 lines (179 loc) · 7.49 KB
/
Copy pathchatbot.py
File metadata and controls
216 lines (179 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from typing import Any, Dict
from langchain.agents import Tool, AgentExecutor, create_react_agent
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import os
import json
import asyncio
import logging
from dotenv import load_dotenv
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class APIAgentManager:
def __init__(self, openai_api_key: str, model: str = "gpt-3.5-turbo"):
self.openai_api_key = openai_api_key
self.model = model
self.tools = None
self.llm = None
self.agent = None
self.agent_executor = None
def load_apis(self):
with open('db.json', 'r') as f:
self.apis = json.load(f)['apis']
def evaluate_response(self, query: str, response: Dict[str, Any]) -> float:
"""Evaluate API response relevance"""
evaluation_prompt = f"""
Given the query: {query}
And the API response: {response}
Rate the relevance from 0 to 1.
Return only the number.
"""
try:
score = float(self.llm.predict(evaluation_prompt))
return min(max(score, 0), 1) # Ensure between 0 and 1
except:
return 0
def check_stored_responses(self, query: str) -> Dict[str, Any]:
try:
best_response = None
best_score = 0
for filename in os.listdir('responses'):
with open(os.path.join('responses', filename), 'r') as f:
stored_response = json.load(f)
score = self.evaluate_response(query, stored_response)
if score > best_score:
best_score = score
best_response = stored_response
return best_response if best_score > 0.7 else None
except Exception as e:
logger.error(f"Error checking stored responses: {e}")
return None
def create_tool_for_api(self, stored_response: Dict[str, Any]) -> Tool:
return Tool(
name=f"api_{stored_response['api_name']}",
func=lambda x: stored_response,
description=f"Use this tool for queries about {stored_response['api_name']}"
)
async def initialize(self):
try:
self.llm = ChatOpenAI(
temperature=0,
model=self.model,
api_key=self.openai_api_key
)
def dummy_tool(x):
return "This is a dummy tool response"
if self.tools is None or len(self.tools) == 0:
self.tools = [Tool(
name="dummy_tool",
func=dummy_tool,
description="A dummy tool for initialization"
)]
else:
# Check if tool with same name already exists
existing_tool_names = [tool.name for tool in self.tools]
if "dummy_tool" not in existing_tool_names:
self.tools.append(
Tool(
name="dummy_tool",
func=dummy_tool,
description="A dummy tool for initialization"
)
)
prompt = PromptTemplate.from_template(
"""You are a helpful assistant that answers questions about users and continents.
Use the available tools to find information.
You have access to the following tools: {tools}
The available tools are: {tool_names}
To use a tool, you MUST use the following format:
Thought: I need to use X tool because...
Action: the_tool_name
Action Input: the input to the tool
Observation: the result of the action
When you have a final response:
Thought: I have all the information I need...
Final Answer: your response
Current conversation:
{chat_history}
Human: {input}
Assistant: Let me help you with that.
{agent_scratchpad}"""
)
self.agent = create_react_agent(
llm=self.llm,
tools=self.tools,
prompt=prompt
)
self.agent_executor = AgentExecutor(
agent=self.agent,
tools=self.tools,
verbose=True,
max_iterations=3,
handle_parsing_errors=True
)
except Exception as e:
logger.error(f"Error in initialization: {e}")
raise
async def update_tools(self, new_tool: Tool):
try:
if self.tools is None:
self.tools = []
self.tools.append(new_tool)
await self.initialize()
except Exception as e:
logger.error(f"Error updating tools: {e}")
raise
class Chatbot:
def __init__(self, openai_api_key: str):
self.agent_manager = APIAgentManager(openai_api_key)
self.initialized = False
async def ensure_initialized(self):
if not self.initialized:
await self.agent_manager.initialize()
self.initialized = True
async def process_query(self, query: str) -> str:
try:
await self.ensure_initialized()
stored_response = self.agent_manager.check_stored_responses(query)
if stored_response:
tool = self.agent_manager.create_tool_for_api(stored_response)
await self.agent_manager.update_tools(tool)
chat_history = []
response = await self.agent_manager.agent_executor.ainvoke({
"input": query,
"chat_history": chat_history
})
return response["output"] if "output" in response else str(response)
return "No suitable response found in stored responses."
except Exception as e:
logger.error(f"Error processing query: {e}")
return f"An error occurred: {str(e)}"
async def async_main():
env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env')
logger.debug(f"Looking for .env file at: {env_path}")
if os.path.exists(env_path):
logger.debug(".env file found")
load_dotenv(env_path)
else:
logger.warning(".env file not found!")
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
print("Error: OPENAI_API_KEY environment variable not set")
return
chatbot = Chatbot(openai_api_key)
print("Chatbot initialized. Type 'quit' to exit.")
try:
while True:
query = input("You: ").strip()
if query.lower() in ['quit', 'exit', 'bye']:
break
response = await chatbot.process_query(query)
print(f"Bot: {response}")
except KeyboardInterrupt:
print("\nShutting down...")
except Exception as e:
print(f"\nError: {e}")
def main():
asyncio.run(async_main())
if __name__ == "__main__":
main()