Your first chat with an LLM
Code from slides:
OpenAI
from pprint import pprint
from dotenv import load_dotenv
from openai import OpenAI
# Loads OPENAI_API_KEY from the .env file
load_dotenv()
# Creates an OpenAI client, which can be used to access any OpenAI service
# (including Whisper and DALL-E, not just chat models). It's totally stateless.
= OpenAI()
client
# The initial set of messages we'll start the conversation with: a system
# prompt and a user prompt.
= [
messages "role": "system", "content": "You are a terse assistant."},
{"role": "user", "content": "What is the capital of the moon?"},
{
]
# Call out to the OpenAI API to generate a response. (This is a blocking call,
# but there are ways to do async, streaming, and async streaming as well.)
= client.chat.completions.create(
response ="gpt-4.1",
model=messages,
messages
)
# Print the response we just received.
print(response.choices[0].message.content)
# If you want to inspect the full response, you can do so by uncommenting the
# following line. The .dict() is helpful in getting more readable output.
# pprint(response.dict())
# The client.chat.completions.create() call is stateless. In order to carry on a
# multi-turn conversation, we need to keep track of the messages we've sent and
# received.
0].message)
messages.append(response.choices[
# Ask a followup question.
"role": "user", "content": "Are you sure?"})
messages.append({= client.chat.completions.create(
response2 ="gpt-4.1",
model=messages,
messages=True,
stream
)
for chunk in response2:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
GitHub OpenAI
import os
from pprint import pprint
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
# arguments passed switch to using GitHub models
= OpenAI(
client =os.environ["GITHUB_TOKEN"],
api_key="https://models.inference.ai.azure.com"
base_url
)
= [
messages "role": "system", "content": "You are a terse assistant."},
{"role": "user", "content": "What is the capital of the moon?"},
{
]
= client.chat.completions.create(
response ="gpt-4.1",
model=messages,
messages
)print(response.choices[0].message.content)
0].message)
messages.append(response.choices[
"role": "user", "content": "Are you sure?"})
messages.append({= client.chat.completions.create(
response2 ="gpt-4.1",
model=messages,
messages=True,
stream
)
for chunk in response2:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
Langchain
from dotenv import load_dotenv
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
# Loads OPENAI_API_KEY from the .env file
load_dotenv()
# Create an OpenAI chat model, with conversation history.
# See https://python.langchain.com/docs/tutorials/chatbot/ for more information.
# The underlying chat model. It doesn't manage any state, so we need to wrap it.
= ChatOpenAI(model="gpt-4.1")
model
# This is how you provide a system message in Langchain. Surprisingly
# complicated, isn't it?
= ChatPromptTemplate.from_messages(
prompt
["You are a terse assistant."),
SystemMessage(="messages"),
MessagesPlaceholder(variable_name
]
)
# Wrap the model and prompt up with some history.
= InMemoryChatMessageHistory()
history = RunnableWithMessageHistory(prompt | model, lambda: history)
client
# We're ready to chat with the model now. For this example we'll make a blocking
# call, but there are ways to do async, streaming, and async streaming as well.
= client.invoke("What is the capital of the moon?")
response print(response.content)
# The input of invoke() can be a message object as well, or a list of messages.
= client.invoke(HumanMessage("Are you sure?"))
response2 print(response2.content)
Python Chatlas
from chatlas import ChatOpenAI
from dotenv import load_dotenv
# Loads OPENAI_API_KEY from the .env file
load_dotenv()
= ChatOpenAI(model="gpt-4.1", system_prompt="You are a terse assistant.")
chat
"What is the capital of the moon?")
chat.chat(
"Are you sure?") chat.chat(
R Ellmer
library(dotenv) # Will read OPENAI_API_KEY from .env file
library(ellmer)
<- chat_openai(
chat model = "gpt-4.1",
system_prompt = "You are a terse assistant.",
)$chat("What is the capital of the moon?")
chat
# The `chat` object is stateful, so this continues the existing conversation
$chat("Are you sure about that?") chat