Spaces:
Sleeping
Sleeping
test2
Browse files
app.py
CHANGED
@@ -1,29 +1,62 @@
|
|
1 |
from openai import OpenAI
|
2 |
import os
|
|
|
|
|
|
|
3 |
|
4 |
-
class
|
5 |
def __init__(self):
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
if not api_key:
|
8 |
-
raise ValueError("OPENAI_API_KEY
|
9 |
self.client = OpenAI(api_key=api_key)
|
10 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def __call__(self, question: str) -> str:
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
response = self.client.chat.completions.create(
|
16 |
-
model="gpt-4",
|
17 |
messages=[
|
18 |
-
{"role": "system", "content": "You are a
|
19 |
-
{"role": "user", "content":
|
20 |
],
|
21 |
-
temperature=0.
|
22 |
-
max_tokens=
|
23 |
)
|
24 |
answer = response.choices[0].message.content.strip()
|
25 |
-
print(f"
|
26 |
return answer
|
27 |
except Exception as e:
|
28 |
-
print(f"Error
|
29 |
-
return f"[
|
|
|
1 |
from openai import OpenAI
|
2 |
import os
|
3 |
+
import math
|
4 |
+
import datetime
|
5 |
+
import requests
|
6 |
|
7 |
+
class ToolEnhancedAgent:
|
8 |
def __init__(self):
|
9 |
api_key = os.getenv("OPENAI_API_KEY")
|
10 |
if not api_key:
|
11 |
+
raise ValueError("OPENAI_API_KEY is not set.")
|
12 |
self.client = OpenAI(api_key=api_key)
|
13 |
+
print("ToolEnhancedAgent initialized with GPT + CoT + Tools.")
|
14 |
+
|
15 |
+
def use_tool(self, tool_name: str, input_text: str) -> str:
|
16 |
+
try:
|
17 |
+
if tool_name == "calculator":
|
18 |
+
return str(eval(input_text))
|
19 |
+
elif tool_name == "date":
|
20 |
+
return str(datetime.datetime.now().date())
|
21 |
+
elif tool_name == "wikipedia":
|
22 |
+
summary = self.search_wikipedia(input_text)
|
23 |
+
return summary
|
24 |
+
else:
|
25 |
+
return "[Tool Error: Unknown Tool]"
|
26 |
+
except Exception as e:
|
27 |
+
return f"[Tool Error: {e}]"
|
28 |
+
|
29 |
+
def search_wikipedia(self, query):
|
30 |
+
try:
|
31 |
+
res = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}")
|
32 |
+
if res.status_code == 200:
|
33 |
+
return res.json().get("extract", "No summary found.")
|
34 |
+
else:
|
35 |
+
return f"No Wikipedia summary for {query}."
|
36 |
+
except Exception as e:
|
37 |
+
return f"Wiki error: {e}"
|
38 |
|
39 |
def __call__(self, question: str) -> str:
|
40 |
+
prompt = (
|
41 |
+
"You are a helpful AI assistant. Use tools when necessary. "
|
42 |
+
"Think step-by-step before answering. Respond clearly.\n\n"
|
43 |
+
f"Question: {question}\n"
|
44 |
+
"Answer (show thinking steps):"
|
45 |
+
)
|
46 |
+
|
47 |
try:
|
48 |
response = self.client.chat.completions.create(
|
49 |
+
model="gpt-4",
|
50 |
messages=[
|
51 |
+
{"role": "system", "content": "You are a smart assistant that can use tools and think step-by-step."},
|
52 |
+
{"role": "user", "content": prompt}
|
53 |
],
|
54 |
+
temperature=0.3,
|
55 |
+
max_tokens=700,
|
56 |
)
|
57 |
answer = response.choices[0].message.content.strip()
|
58 |
+
print(f"Answer generated: {answer[:100]}...")
|
59 |
return answer
|
60 |
except Exception as e:
|
61 |
+
print(f"[Agent Error]: {e}")
|
62 |
+
return f"[Agent Error: {e}]"
|