from langchain_cohere import ChatCoherefrom langchain.messages import ( HumanMessage, ToolMessage,)from langchain.tools import tool@tooldef magic_function(number: int) -> int: """Applies a magic operation to an integer Args: number: Number to have magic operation performed on """ return number + 10def invoke_tools(tool_calls, messages): for tool_call in tool_calls: selected_tool = {"magic_function":magic_function}[ tool_call["name"].lower() ] tool_output = selected_tool.invoke(tool_call["args"]) messages.append(ToolMessage(tool_output, tool_call_id=tool_call["id"])) return messagestools = [magic_function]llm = ChatCohere()llm_with_tools = llm.bind_tools(tools=tools)messages = [ HumanMessage( content="What is the value of magic_function(2)?" )]res = llm_with_tools.invoke(messages)while res.tool_calls: messages.append(res) messages = invoke_tools(res.tool_calls, messages) res = llm_with_tools.invoke(messages)print(res.content)
from langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_cohere import ChatCohere, create_cohere_react_agentfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_classic.agents import AgentExecutorllm = ChatCohere()internet_search = TavilySearchResults(max_results=4)internet_search.name = "internet_search"internet_search.description = "Route a user query to the internet"prompt = ChatPromptTemplate.from_template("{input}")agent = create_cohere_react_agent( llm, [internet_search], prompt)agent_executor = AgentExecutor(agent=agent, tools=[internet_search], verbose=True)agent_executor.invoke({ "input": "In what year was the company that was founded as Sound of Music added to the S&P 500?",})
from langchain_cohere import CohereEmbeddingsembeddings = CohereEmbeddings(model="embed-english-light-v3.0")print(embeddings.embed_documents(["This is a test document."]))