My first OpenAI with Langchain v 0.1.0
Github Link:
To understand the working of langchain and how it interacts with llms, let us start with a simple sample program that sends prompt to llm and gets response.
Step 1 : Install the required packages.
!pip install langchain-openai langchain
Step 2 : Interact with OpenAI
from langchain_openai import ChatOpenAI
chat = ChatOpenAI(
temperature=0,
openai_api_key=openai_api_key
)
Step 3 : Structure prompt as a list of messages.
from langchain_core.messages import SystemMessage, HumanMessage
messages = [
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(
content="Translate this sentence from English to French. Hello"
),
]
Step 4 : Pass the message to the default gpt model(gpt-3.5-turbo) and get response.
response = chat.invoke(messages)
Step 5 : Print the response and check the output.
Comments
Post a Comment