Welcome back, the core element of any LLM application is the AI model.
Langchain doesn t have its own LLMs, but it provides a standard interface for
interacting with various LLMs, including OpenAI s GPT models, Google s Gemini,
Metas, LAMA and more.
In Langchain terminology, these are
called LLM providers.
Now, let s dive into how to invoke OpenAI
s GPT models like GPT -3 .5 Turbo, GPT -4 or GPT -4 Turbo within Langchain.
These models expose an interface where chat messages or conversations serve as
inputs and outputs.
From Langchain OpenAI, I am importing
chatOpenA. Now, let s create an LLM object, LLM equals chatOpenA.
Next, I ll use LLM .invoke to send a request to the OpenAI GPT model.
This method takes the prompt as an argument.
For example, explain quantum mechanics in
one sentence.
The invoke method returns an output
object, and the content attribute of the output object contains the text response
from the OpenAI GPT model.
I am running the code very well.
Notice that I called LLM .invoke without any arguments, and you might be wondering
which model and parameters it used.
To view the constructor s arguments along
with their defaults, let s check the built -in help of the class.
Help, chatOpenA.
As you can see, there are several
available arguments of particular interest to us are model and temperature.
Note that the default model is GPT 3 .5 turbo and the default value for the
temperature is 0 .7.
I ve delved into OpenAI s chat
completions parameters during the OpenAI API with Python course.
If you prefer to use GPT 4 or GPT 4 turbo instead of GPT 3 .5 turbo, simply change
the model name.
I am adding a new argument model equals
GPT 4 turbo preview.
This is the name for GPT 4 turbo, and I m
running the code.
Note that at this moment, GPT 4 turbo is
available only to paid accounts.
For this course, we ll stick with GPT 3
.5 turbo because it s both cost -effective and powerful.
Let s take a look at the OpenAI chat completions API.
The OpenAI API for GPT 3 .5 turbo, GPT 4, GPT 4 turbo and so on uses a list of
dictionaries called messages.
These messages define three roles System,
User and Assistant.
The System role helps set the behavior of
the Assistant, the User message serves as the prompt or the question we ask the
Assistant, and the Assistant messages store prior responses.
To use these messages when calling the LLM from Langchain, we need to import a
schema for the messages.
From Langchain Schema Import, and I will
import three classes for the three message types,
In the OpenAI chat completions API, a message is equivalent to the Assistant
message and a human message represents the user s message or prompt.
Now, let s create a list called messages.
The list will contain a System message
and a human message which is the prompt.
System message of content equals you are
a physicist and respond only in German and the human message content equals and
the same task explain quantum mechanics in one sentence.
Finally, let s make the API call to OpenAI output equals LLM .invoke of
messages and print output .content.
I am running the code very well, the LLM
followed my instructions and responded in one sentence in German.