kostasang commited on
Commit
891f6e3
·
verified ·
1 Parent(s): bc6ca3b

Delete agent.py

Browse files
Files changed (1) hide show
  1. agent.py +0 -134
agent.py DELETED
@@ -1,134 +0,0 @@
1
- import json
2
-
3
- from langchain_core.messages import SystemMessage, HumanMessage
4
- from langchain_openai.chat_models import ChatOpenAI
5
- from langfuse import Langfuse, get_client
6
- from langfuse.langchain import CallbackHandler
7
- from langgraph.graph import START, StateGraph, MessagesState
8
- from langgraph.prebuilt import tools_condition
9
- from langgraph.prebuilt import ToolNode
10
-
11
-
12
- class Agent:
13
- """
14
- Class representing a basic agent that can answer questions.
15
- """
16
-
17
- def __init__(
18
- self,
19
- model: str,
20
- tools: list,
21
- system_prompt_path: str,
22
- openai_api_key: str = None,
23
- langfuse_callback_handler: CallbackHandler = None
24
- ):
25
- """
26
- Initialize the agent object.
27
- :param model: The OpenAI model to use.
28
- :param tools: List of tools the agent can use.
29
- :param system_prompt_path: Path to the system prompt file.
30
- :param openai_api_key: OpenAI API key for authentication.
31
- :param langfuse_callback_handler: Langfuse callback handler for
32
- tracking and logging interactions.
33
- """
34
- self.chat_model = ChatOpenAI(
35
- model=model,
36
- api_key=openai_api_key,
37
- )
38
- with open(system_prompt_path, "r") as file:
39
- self.system_prompt = file.read()
40
- self.tools = tools
41
- if langfuse_callback_handler is not None:
42
- self.chat_model.callbacks = [langfuse_callback_handler]
43
- self.chat_model_with_tools = self.chat_model.bind_tools(
44
- tools=tools,
45
- parallel_tool_calls=False
46
- )
47
- self.graph = self.__build_graph()
48
-
49
- def __call__(self, question: str) -> str:
50
- """
51
- Reply to a question using the agent and return the agents full reply
52
- with reasoning included.
53
- :param question: The question to ask the agent.
54
- :return: The agent's response.
55
- """
56
- final_state = self.graph.invoke(
57
- input={
58
- "messages": [
59
- SystemMessage(content=self.system_prompt),
60
- HumanMessage(content=question)
61
- ]
62
- },
63
- config={
64
- "callbacks": self.chat_model.callbacks
65
- }
66
- )
67
- return final_state["messages"][-1].content
68
-
69
- def __build_graph(self):
70
- """
71
- Build the graph for the agent.
72
- """
73
- builder = StateGraph(MessagesState)
74
-
75
- # Define nodes: these do the work
76
- builder.add_node("assistant", self.__assistant)
77
- builder.add_node("tools", ToolNode(self.tools))
78
-
79
- # Define edges: these determine how the control flow moves
80
- builder.add_edge(START, "assistant")
81
- builder.add_conditional_edges(
82
- "assistant",
83
- tools_condition,
84
- )
85
- builder.add_edge("tools", "assistant")
86
- return builder.compile()
87
-
88
- def __assistant(self, state: MessagesState) -> MessagesState:
89
- """
90
- The assistant function that processes the state and returns a response.
91
- :param state: The current state of the agent.
92
- :return: Updated state with the assistant's response.
93
- """
94
- response = self.chat_model_with_tools.invoke(state["messages"])
95
- return {"messages": [response]}
96
-
97
-
98
- if __name__ == "__main__":
99
-
100
- import os
101
- from langchain_community.tools import DuckDuckGoSearchResults
102
-
103
- from tools import multiply, add, subtract, divide, modulus
104
-
105
- # Initialize Langfuse client with constructor arguments
106
- Langfuse(
107
- public_key=os.environ.get("LANGFUSE_PUBLIC_KEY"),
108
- secret_key=os.environ.get("LANGFUSE_SECRET_KEY"),
109
- host='https://cloud.langfuse.com'
110
- )
111
-
112
- # Get the configured client instance
113
- langfuse = get_client()
114
-
115
- # Initialize the Langfuse handler
116
- langfuse_handler = CallbackHandler()
117
-
118
- tools = [multiply, add, subtract, divide, modulus]
119
- tools.append(
120
- DuckDuckGoSearchResults()
121
- )
122
- agent = Agent(
123
- model="gpt-4o",
124
- tools=tools,
125
- system_prompt_path="prompts/system_prompt.txt",
126
- openai_api_key=os.environ.get("OPENAI_API_KEY"),
127
- langfuse_callback_handler=langfuse_handler
128
- )
129
- response = agent(
130
- question="""
131
- Search for Tom Cruise and summarize the results for me.
132
- """
133
- )
134
- print(response)