Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # want to see if formatting the tools is what will make the tool call work.
- from pprint import pprint
- from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
- from dotenv import load_dotenv
- from langchain_groq import ChatGroq
- from IPython.display import Image, display
- from langgraph.graph import StateGraph, START, END
- from langgraph.graph import MessagesState
- from langgraph.prebuilt import ToolNode, tools_condition
- import json
- load_dotenv()
- MODEL = "llama3-8b-8192"
- # MODEL = "gemma2-9b-it"
- llm = ChatGroq(model=MODEL, temperature=0)
- def multiply(a: int, b: int) -> int:
- """Multiply two integers.
- Args:
- a: an integer
- b: an integer
- Returns:
- the product of a and b
- """
- return a * b
- def add(a: int, b: int) -> int:
- """Adds a and b.
- Args:
- a: first int
- b: second int
- """
- return a + b
- def subtract(a: int, b: int) -> int:
- """Subtracts a and b.
- Args:
- a: first int
- b: second int
- """
- return a - b
- tools = [multiply, add, subtract]
- llm_with_tools = llm.bind_tools(tools)
- # Node
- def tool_calling_llm(state: MessagesState):
- return {"messages": [llm_with_tools.invoke(state["messages"])]}
- # Build graph's nodes
- builder = StateGraph(MessagesState)
- builder.add_node("tool_calling_llm", tool_calling_llm)
- builder.add_node("tools", ToolNode(tools)) # for the tools
- # Build graph's edges
- builder.add_edge(START, "tool_calling_llm")
- builder.add_conditional_edges(
- "tool_calling_llm",
- tools_condition,
- )
- builder.add_edge("tools", "tool_calling_llm")
- graph = builder.compile()
- messages = graph.invoke({"messages": [SystemMessage(content="You are a calculator assistant. Use the multiply function to perform mathematical operations and provide the results."), HumanMessage(content="What is 4 * 4?, now add 3, and then subtract 2 from it.")]})
- for m in messages['messages']:
- m.pretty_print()
- # THIS PROGRAM SEEMS TO WORK ONLY WITH THE GEMMA MODEL, NOT WITH THE LLAMA ONE. I THINK I HAVE TO PASS TOOLS AS DICTIONARY TO THE TOOLS NODE. WILL TRY.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement