Spaces:
Sleeping
Sleeping
Merge branch 'feat/agent-improvement'
Browse files- ShrewdAgent.py +18 -13
- app.py +2 -0
ShrewdAgent.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
from typing import TypedDict, Annotated, Optional, Any, Callable, Sequence, Union
|
3 |
|
4 |
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
|
@@ -29,18 +30,21 @@ class AgentState(TypedDict):
|
|
29 |
|
30 |
|
31 |
class ShrewdAgent:
|
32 |
-
message_system = """
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
def __init__(self):
|
46 |
self.tools = [
|
@@ -56,7 +60,7 @@ class ShrewdAgent:
|
|
56 |
transcribe_audio,
|
57 |
]
|
58 |
self.llm = ChatOpenAI(
|
59 |
-
model="gpt-
|
60 |
temperature=0,
|
61 |
api_key=SecretStr(os.environ['OPENAI_API_KEY'])
|
62 |
).bind_tools(self.tools)
|
@@ -67,7 +71,8 @@ class ShrewdAgent:
|
|
67 |
}
|
68 |
|
69 |
self.agent = _build_state_graph(AgentState, assistant_node, self.tools)
|
70 |
-
logger.info("
|
|
|
71 |
|
72 |
def __call__(self, question: str) -> str:
|
73 |
logger.info(f"Agent received question:\n{question}")
|
|
|
1 |
import os
|
2 |
+
from textwrap import dedent
|
3 |
from typing import TypedDict, Annotated, Optional, Any, Callable, Sequence, Union
|
4 |
|
5 |
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
|
|
|
30 |
|
31 |
|
32 |
class ShrewdAgent:
|
33 |
+
message_system = dedent("""
|
34 |
+
You are a general AI assistant equipped with a suite of external tools. Your task is to
|
35 |
+
answer the following question as accurately and helpfully as possible by using the tools
|
36 |
+
provided. Do not write or execute code yourself. For any operation requiring computation,
|
37 |
+
data retrieval, or external access, explicitly invoke the appropriate tool.
|
38 |
|
39 |
+
Follow these guidelines:
|
40 |
+
- Clearly explain your reasoning step by step.
|
41 |
+
- Justify your choice of tool(s) at each step.
|
42 |
+
- If multiple interpretations are possible, outline them and explain your reasoning for selecting one.
|
43 |
+
- If the answer requires external data or inference, retrieve or deduce it via the available tools.
|
44 |
+
|
45 |
+
Important: Your final output MUST be only a number or a word with no additional text or explanation,
|
46 |
+
unless the response format is explicitly specified in the question. Do not include reasoning,
|
47 |
+
commentary, or any other content beyond the requested answer.""")
|
48 |
|
49 |
def __init__(self):
|
50 |
self.tools = [
|
|
|
60 |
transcribe_audio,
|
61 |
]
|
62 |
self.llm = ChatOpenAI(
|
63 |
+
model="gpt-4.1",
|
64 |
temperature=0,
|
65 |
api_key=SecretStr(os.environ['OPENAI_API_KEY'])
|
66 |
).bind_tools(self.tools)
|
|
|
71 |
}
|
72 |
|
73 |
self.agent = _build_state_graph(AgentState, assistant_node, self.tools)
|
74 |
+
logger.info(f"Agent initialized with tools: {[tool.name for tool in self.tools]}")
|
75 |
+
logger.debug(f"system message:\n{self.message_system}")
|
76 |
|
77 |
def __call__(self, question: str) -> str:
|
78 |
logger.info(f"Agent received question:\n{question}")
|
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
@@ -87,6 +88,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
87 |
submitted_answer = agent(question_with_attachment)
|
88 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
89 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
90 |
except Exception as e:
|
91 |
print(f"Error running agent on task {task_id}: {e}")
|
92 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
|
|
88 |
submitted_answer = agent(question_with_attachment)
|
89 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
90 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
91 |
+
time.sleep(70) # wait for reducing rate limit errors
|
92 |
except Exception as e:
|
93 |
print(f"Error running agent on task {task_id}: {e}")
|
94 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|