Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -2,7 +2,7 @@ from langchain_core.messages import HumanMessage, AIMessage
|
|
2 |
from langchain_community.tools import DuckDuckGoSearchRun
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
-
from
|
6 |
import os
|
7 |
|
8 |
class AdvancedAgent:
|
@@ -30,18 +30,23 @@ class AdvancedAgent:
|
|
30 |
Answer:
|
31 |
"""
|
32 |
)
|
33 |
-
self.chain =
|
34 |
|
35 |
def __call__(self, question: str) -> str:
|
36 |
print(f"Agent processing question (first 50 chars): {question[:50]}...")
|
37 |
try:
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Generate answer using LLM and context
|
44 |
-
response = self.chain.
|
45 |
print(f"Agent generated answer: {response[:50]}...")
|
46 |
return response.strip()
|
47 |
except Exception as e:
|
|
|
2 |
from langchain_community.tools import DuckDuckGoSearchRun
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain_openai import ChatOpenAI
|
5 |
+
from langchain_core.runnables import RunnableSequence
|
6 |
import os
|
7 |
|
8 |
class AdvancedAgent:
|
|
|
30 |
Answer:
|
31 |
"""
|
32 |
)
|
33 |
+
self.chain = self.prompt_template | self.llm
|
34 |
|
35 |
def __call__(self, question: str) -> str:
|
36 |
print(f"Agent processing question (first 50 chars): {question[:50]}...")
|
37 |
try:
|
38 |
+
# Avoid rate limiting with a delay
|
39 |
+
time.sleep(1) # Adjust delay based on testing
|
40 |
+
|
41 |
+
# Perform web search
|
42 |
+
search_results = self.search_tool.run(question)
|
43 |
+
if not search_results:
|
44 |
+
context = "No search results found."
|
45 |
+
else:
|
46 |
+
context = search_results[:1000] # Limit context length
|
47 |
|
48 |
# Generate answer using LLM and context
|
49 |
+
response = self.chain.invoke({"question": question, "context": context})
|
50 |
print(f"Agent generated answer: {response[:50]}...")
|
51 |
return response.strip()
|
52 |
except Exception as e:
|