SergeyO7 commited on
Commit
26cbef0
·
verified ·
1 Parent(s): d5ef9dd

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +12 -7
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 langchain.chains import LLMChain
6
  import os
7
 
8
  class AdvancedAgent:
@@ -30,18 +30,23 @@ class AdvancedAgent:
30
  Answer:
31
  """
32
  )
33
- self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
34
 
35
  def __call__(self, question: str) -> str:
36
  print(f"Agent processing question (first 50 chars): {question[:50]}...")
37
  try:
38
- # Perform a web search to gather context
39
- search_query = f"{question}"
40
- search_results = self.search_tool.run(search_query)
41
- context = search_results[:1000] # Limit context length
 
 
 
 
 
42
 
43
  # Generate answer using LLM and context
44
- response = self.chain.run(question=question, context=context)
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: