Mahmoud-abbasi-m commited on
Commit
9ac9c41
1 Parent(s): 7c8a55f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_experimental.llms.ollama_functions import OllamaFunctions
2
+ from langchain_core.messages import HumanMessage, ToolMessage
3
+
4
+ model = OllamaFunctions(
5
+ model="llama3",
6
+ format="json"
7
+ )
8
+
9
+
10
+ @tool
11
+ def add(a: int, b: int) -> int:
12
+ """Adds a and b. Args: a: first int b: second int """
13
+ return a + b
14
+
15
+
16
+ @tool
17
+ def multiply(a: int, b: int) -> int:
18
+ """Multiplies a and b. Args: a: first int b: second int"""
19
+ return a * b
20
+
21
+
22
+ tools = [add, multiply]
23
+ llm_with_tools = model.bind_tools(tools)
24
+
25
+ messages = [HumanMessage(query)]
26
+ ai_msg = llm_with_tools.invoke(messages)
27
+ messages.append(ai_msg)
28
+
29
+ for tool_call in ai_msg.tool_calls:
30
+ selected_tool = {"add": add, "multiply": multiply}[tool_call["name"].lower()]
31
+ tool_output = selected_tool.invoke(tool_call["args"])
32
+ messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"]))
33
+
34
+ messages