Posts

Showing posts from October, 2025

Query builder using LangChain, LangSmith using gpt-4o-mini LLM

Image
Open AI model gpt-4o-mini is one my favourite because of it's low cost and less effective time to get the job done.  Below code is to analyze question/query and transform them into meaning full SQL statement through LangSmith and LangChain. I'm not using Pydantic directly here and let the Lang team to handle the query builder part. One simple quetion - how many employee do we have? Let's see, how the below code act and what exactly SQL it is able to generate: import os from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from typing_extensions import Annotated, TypedDict from langchain_community.utilities import SQLDatabase from langchain_community.tools.sql_database.tool import QuerySQLDatabaseTool from langgraph.graph import START, StateGraph os.environ[ "LANGSMITH_TRACING" ] = "true" os.environ[ "LANGSMITH_API_KEY" ] = "Your key" if not os.environ.get( "OPENAI_API_KEY...

Using Open AI and LangSmith connect to SQL Server

Image
This post is self explanotory where using Open AI api and LangSmith, I connected to my local SQL server and get the data from my local SQL table and finally print the data in command prompt. You need to have one Open API and LangSmith key to get this work. Just replace the keys on the respective code block. Commented lines represents different ways to connect SQL server using Open AI and LangSmith. import os import pyodbc os.environ[ "LANGSMITH_TRACING" ] = "true" os.environ[ "LANGSMITH_API_KEY" ] = " YOUR LANGSMITH KEY GOES HERE " if not os.environ.get( "OPENAI_API_KEY" ):   os.environ[ "OPENAI_API_KEY" ] = " YOUR OPENAI KEY GOES HERE " #from langchain_community.utilities import SQLDatabase #conn_str = "mssql+pyodbc:///?odbc_connect=DRIVER={SQL Server};SERVER=TUKAI\MSSQLSERVER01;DATABASE=EASY_DATE;Trusted_Connection=yes;" #db = SQLDatabase.from_uri(conn_str) #print(db.dialect) #print(db.get_usable_tabl...

Windows command prompt display Good Morning in my native language using OpenAI

Image
Great things to see first time using OpenAI - I can see Good Morning message in my native Bengali language in Windows Command prompt! Below the artifacts you need to install before run the code pip install langchain pip install langchain-openai pip install langgraph And, you need a key from  https://smith.langchain.com /  Finally, the code goes below... import getpass import os from langchain_openai import ChatOpenAI from langchain.schema import HumanMessage from langchain_core.prompts import ChatPromptTemplate if not os.environ.get( "OPENAI_API_KEY" ):   os.environ[ "OPENAI_API_KEY" ] = getpass.getpass( "YOUR OPENAPI KEY GOES HERE" ) chat = ChatOpenAI(model= "gpt-4o-mini" , temperature= 0.7 ) system_template = "Translate the following from English into {language}" prompt_template = ChatPromptTemplate.from_messages(     [( "system" , system_template), ( "user" , "{text}" )] ) #You can change lan...

Simple ChatBot using Open AI and LangChain, LangSmith

Image
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...