Simple ChatBot using Open AI and LangChain, LangSmith

This post is about simple chatbot one liner for understanding the flow. How the API coommunicate, how to structure the code, and what to call. 

Open AI api and LangChain and LangSmith have been used here. Please note, you need to install LangChain, Open AI related chat model interfaces - pip install -qU "langchain[openai]"

Next step is to create one API key in LangSmith, and put your API keys in the below code.

Replace your keys in the respective position: (YOUR SANGSMITH KEY GOES HERE) and (YOUR OPENAI KEY GOES HERE).


This is simple chat application which read your name and understand your first name. The reading and understanding is made through LangChain schema.

Here the code goes...

import getpass
import os
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage
from langchain_core.messages import AIMessage

os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = "YOUR SANGSMITH KEY GOES HERE"

if not os.environ.get("OPENAI_API_KEY"):
  os.environ["OPENAI_API_KEY"] = getpass.getpass("YOUR OPENAI KEY GOES HERE")


chat = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
response = chat.invoke(
  [
    HumanMessage(content="Hi! I'm Himadrish"),
    AIMessage(content="Hello Himadrish! How can I help you today?"),
    HumanMessage(content="What is my first name?"),
   ]
 
  )
print(response.content)

Comments

Popular posts from this blog

SharePoint Framework Reference Guide

SharePoint Admin Center

How to fix Azure DevOps error MSB4126