anandshende-videocx commited on
Commit
e21909c
·
verified ·
1 Parent(s): ac8beb6

Create my_agent.py

Browse files
Files changed (1) hide show
  1. my_agent.py +86 -0
my_agent.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from langgraph.prebuilt import ToolNode, tools_condition
4
+ from langgraph.graph import StateGraph, START, MessagesState
5
+ from langchain.agents import create_agent
6
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
7
+ from langchain_community.tools import DuckDuckGoSearchRun
8
+ from langchain_ollama import ChatOllama
9
+ from langchain.agents.middleware.types import AgentState
10
+ from langchain.messages import HumanMessage, AIMessage, SystemMessage
11
+ from langfuse.langchain import CallbackHandler
12
+
13
+ import add_telemetry # noqa: F401 to initialize Langfuse telemetry
14
+
15
+ hf_token = os.getenv("HF_TOKEN")
16
+ langfuse_handler = CallbackHandler()
17
+
18
+
19
+ class AgentResponseState(AgentState):
20
+ response: str
21
+ messages: list[HumanMessage | AIMessage]
22
+
23
+
24
+ # --- Basic Agent Definition ---
25
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
26
+ class BasicAgent:
27
+ def __init__(self):
28
+ model = HuggingFaceEndpoint(
29
+ repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
30
+ task="text-generation",
31
+ max_new_tokens=512,
32
+ do_sample=False,
33
+ repetition_penalty=1.03,
34
+ )
35
+ llm = ChatHuggingFace(llm=model, verbose=True)
36
+ # llm = ChatOllama(
37
+ # model="qwen3:0.6b",
38
+ # api_base="http://localhost:11434", # replace with
39
+ # # debug=True,
40
+ # )
41
+ tools = [
42
+ DuckDuckGoSearchRun(),
43
+ ]
44
+
45
+ builder = StateGraph(MessagesState)
46
+
47
+ model = create_agent(llm, tools)
48
+ builder.add_node("assistant", model)
49
+ builder.add_node("tools", ToolNode(tools))
50
+
51
+ # Define edges: these determine how the control flow moves
52
+ builder.add_edge(START, "assistant")
53
+ builder.add_conditional_edges(
54
+ "assistant",
55
+ # If the latest message requires a tool, route to tools
56
+ # Otherwise, provide a direct response
57
+ tools_condition,
58
+ )
59
+ builder.add_edge("tools", "assistant")
60
+ self.agent = builder.compile()
61
+
62
+ print("BasicAgent initialized.")
63
+
64
+ def __call__(self, question: str) -> str:
65
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
66
+
67
+ fixed_answer = self.generate_answer(question)
68
+
69
+ print(f"Agent returning fixed answer: {fixed_answer}")
70
+ return fixed_answer
71
+
72
+ def generate_answer(self, question: str) -> str:
73
+ response = self.agent.invoke(
74
+ {
75
+ "messages": [
76
+ {
77
+ "role": "user",
78
+ "content": question,
79
+ }
80
+ ]
81
+ }
82
+ )
83
+ print(f"Agent raw response: {response}")
84
+ print(f"response.content => {response['messages']}")
85
+ print(f"AI response => {response['messages'][-1].content}")
86
+ return response['messages'][-1].content