Update agent.py
Browse files
agent.py
CHANGED
|
@@ -7,6 +7,7 @@ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
| 7 |
from tools import ExtractTextFromImage, DescribeImage, read_excel, read_python, wiki_search, web_search, arxiv_search
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
from typing import TypedDict, Annotated, Optional
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class AgentState(TypedDict):
|
|
@@ -21,6 +22,7 @@ class BasicAgent():
|
|
| 21 |
self.tools = [extract_text_from_image.__call__, describe_image.__call__, read_excel, read_python, wiki_search, web_search, arxiv_search]
|
| 22 |
self.chat_with_tools = chat.bind_tools(self.tools)
|
| 23 |
self._initialize_graph()
|
|
|
|
| 24 |
print("BasicAgent initialized.")
|
| 25 |
|
| 26 |
|
|
@@ -39,6 +41,17 @@ class BasicAgent():
|
|
| 39 |
# Compile the graph
|
| 40 |
self.agent = builder.compile()
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def __call__(self, question: str, file_name : str) -> str:
|
| 44 |
print(f"Agent received question: {question}. /nProvided file: {file_name}.")
|
|
@@ -46,7 +59,7 @@ class BasicAgent():
|
|
| 46 |
messages=[HumanMessage(content=f"{question}. The filename you have access to is {file_name}.")]
|
| 47 |
else:
|
| 48 |
messages=[HumanMessage(content=question)]
|
| 49 |
-
response = self.agent.invoke({"messages":messages})
|
| 50 |
answer = response['messages'][-1].content
|
| 51 |
print(f"Agent returning answer: {answer}")
|
| 52 |
return answer
|
|
|
|
| 7 |
from tools import ExtractTextFromImage, DescribeImage, read_excel, read_python, wiki_search, web_search, arxiv_search
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
from typing import TypedDict, Annotated, Optional
|
| 10 |
+
from langfuse.callback import CallbackHandler
|
| 11 |
|
| 12 |
|
| 13 |
class AgentState(TypedDict):
|
|
|
|
| 22 |
self.tools = [extract_text_from_image.__call__, describe_image.__call__, read_excel, read_python, wiki_search, web_search, arxiv_search]
|
| 23 |
self.chat_with_tools = chat.bind_tools(self.tools)
|
| 24 |
self._initialize_graph()
|
| 25 |
+
self._initialize_telemetry()
|
| 26 |
print("BasicAgent initialized.")
|
| 27 |
|
| 28 |
|
|
|
|
| 41 |
# Compile the graph
|
| 42 |
self.agent = builder.compile()
|
| 43 |
|
| 44 |
+
def _initialize_telemetry(self):
|
| 45 |
+
LANGFUSE_PUBLIC_KEY = os.getenv("LANGFUSE_PUBLIC_KEY")
|
| 46 |
+
LANGFUSE_SECRET_KEY = os.getenv("LANGFUSE_SECRET_KEY")
|
| 47 |
+
LANGFUSE_HOST = "https://cloud.langfuse.com"
|
| 48 |
+
|
| 49 |
+
self.langfuse_handler = CallbackHandler(
|
| 50 |
+
public_key=LANGFUSE_PUBLIC_KEY,
|
| 51 |
+
secret_key=LANGFUSE_SECRET_KEY,
|
| 52 |
+
host=LANGFUSE_HOST
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
|
| 56 |
def __call__(self, question: str, file_name : str) -> str:
|
| 57 |
print(f"Agent received question: {question}. /nProvided file: {file_name}.")
|
|
|
|
| 59 |
messages=[HumanMessage(content=f"{question}. The filename you have access to is {file_name}.")]
|
| 60 |
else:
|
| 61 |
messages=[HumanMessage(content=question)]
|
| 62 |
+
response = self.agent.invoke({"messages":messages}, config={"callbacks": [self.langfuse_handler]})
|
| 63 |
answer = response['messages'][-1].content
|
| 64 |
print(f"Agent returning answer: {answer}")
|
| 65 |
return answer
|