RajeshKothiya commited on
Commit
cffde6b
·
verified ·
1 Parent(s): 81917a3

[ADD] Agent: Adding initial agent code

Browse files
Files changed (1) hide show
  1. agent.py +76 -0
agent.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """LangGraph Agent"""
2
+ import os
3
+ from dotenv import load_dotenv
4
+ from langchain_groq import ChatGroq
5
+ from langchain_core.tools import tool
6
+ from langgraph.prebuilt import ToolNode
7
+ from langgraph.prebuilt import tools_condition
8
+ from langgraph.graph import START, StateGraph, MessagesState
9
+ from langchain_core.messages import SystemMessage, HumanMessage
10
+ from langchain_community.tools import DuckDuckGoSearchResults
11
+
12
+ load_dotenv()
13
+
14
+ @tool
15
+ def web_search(query: str) -> str:
16
+ """
17
+ Search DuckDuckGo for a query.
18
+
19
+ Args:
20
+ query: The search query.
21
+
22
+ Returns:
23
+ A dict with key ``"web_results"`` containing a
24
+ markdown-formatted string of the retrieved documents.
25
+ """
26
+ # Instantiate the DuckDuckGo search utility
27
+ search = DuckDuckGoSearchResults(max_results=3)
28
+
29
+ # Perform the search
30
+ search_result = search.invoke(query)
31
+
32
+ return {"web_results": search_result}
33
+
34
+ tools = [
35
+ web_search,
36
+ ]
37
+
38
+ llm = ChatGroq(model="meta-llama/llama-4-maverick-17b-128e-instruct", temperature=0)
39
+ llm_with_tools = llm.bind_tools(tools)
40
+
41
+ llm_with_tools = llm.bind_tools(tools)
42
+
43
+ # Node
44
+ def start_preprocess(state: MessagesState):
45
+ # System message
46
+ system_prompt = "You are a general AI assistant. I will ask you a question. Your answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
47
+ sys_msg = SystemMessage(content=system_prompt)
48
+ return {"messages": [sys_msg] + state["messages"]}
49
+
50
+ # Node
51
+ def assistant(state: MessagesState):
52
+ """Assistant node"""
53
+ return {"messages": [llm_with_tools.invoke(state["messages"])]}
54
+
55
+ builder = StateGraph(MessagesState)
56
+ builder.add_node("start_preprocess", start_preprocess)
57
+ builder.add_node("assistant", assistant)
58
+ builder.add_node("tools", ToolNode(tools))
59
+ builder.add_edge(START, "start_preprocess")
60
+ builder.add_edge("start_preprocess", "assistant")
61
+ builder.add_conditional_edges(
62
+ "assistant",
63
+ tools_condition,
64
+ )
65
+ builder.add_edge("tools", "assistant")
66
+
67
+ # Compile graph
68
+ graph = builder.compile()
69
+
70
+ if __name__ == "__main__":
71
+ question = "NAMO age ?"
72
+ # Run the graph
73
+ messages = [HumanMessage(content=question)]
74
+ messages = graph.invoke({"messages": messages})
75
+ for m in messages["messages"]:
76
+ m.pretty_print()