langchain agent hello world

We can start building LLM agents using Langchain. Similiar to MCP, you would require a LLM engine such as Gemini, Chatgpt or Claude. 

Please note this is only a agent - not a tool.

To get started, you can register one for Gemini for free here https://aistudio.google.com/ and lets have it tested with the code sample. 

In this example, we going to use gemini as the LLM and then ask it to do some search via Tavily tool. 

So you get your LLM, get your agent tool in this case it is Tavily and then you bring it together. 

from langchain_community.tools.tavily_search import TavilySearchResults

search = TavilySearchResults(max_results=2)
search_results = search.invoke("what is the weather in SF")
print(search_results)
# If we want, we can create other tools.
# Once we have all the tools we want, we can put them in a list that we will reference later.
tools = [search]

Setting up your weapon of choice - LLM


import getpass
import os

if not os.environ.get("GOOGLE_API_KEY"):
  os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter API key for Google Gemini: ")

from langchain.chat_models import init_chat_model

model = init_chat_model("gemini-2.0-flash", model_provider="google_genai")


from langchain_core.messages import HumanMessage

response = model.invoke([HumanMessage(content="hi!")])
response.content


model_with_tools = model.bind_tools(tools)

response = model_with_tools.invoke([HumanMessage(content="Hi!")])

print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")

To run it:

response = model_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])

print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")

So Tavily search supports agent tool integration, you can find additional details here

https://pypi.org/project/langchain-tavily/


The full code can be access here:-

https://github.com/mitzenjeremywoo/langchain-agent-hello/blob/main/agent.ipynb

Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type