Skip to content

Instantly share code, notes, and snippets.

@cboettig
Created October 26, 2024 00:34
Show Gist options
  • Select an option

  • Save cboettig/6359fc23a637d328e5fa25a40499abb2 to your computer and use it in GitHub Desktop.

Select an option

Save cboettig/6359fc23a637d328e5fa25a40499abb2 to your computer and use it in GitHub Desktop.
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import ConfigurableField
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
@tool
def multiply(x: float, y: float) -> float:
"""Multiply 'x' times 'y'."""
return x * y
@tool
def exponentiate(x: float, y: float) -> float:
"""Raise 'x' to the 'y'."""
return x**y
@tool
def add(x: float, y: float) -> float:
"""Add 'x' and 'y'."""
return x + y
prompt = ChatPromptTemplate.from_messages([
("system", "you're a helpful assistant"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
tools = [multiply, exponentiate, add]
os.environ["OPENAI_API_KEY"] = st.secrets['LITELLM_KEY']
llm = ChatOpenAI(model = "gorilla", temperature=0, base_url="https://llm.nrp-nautilus.io/")
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what's 3 plus the quantity 5 raised to the power of 2. also what's 17.24 - 918.1241", })
@cboettig
Copy link
Author

Example query response:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment