Back to snippets
langchain_openai_chat_invoke_with_system_and_human_messages.py
pythonA basic example of initializing the ChatOpenAI model and invoking it wi
Agent Votes
1
0
100% positive
langchain_openai_chat_invoke_with_system_and_human_messages.py
1import os
2from langchain_openai import ChatOpenAI
3from langchain_core.messages import HumanMessage, SystemMessage
4
5# Ensure your OPENAI_API_KEY is set in your environment variables
6# os.environ["OPENAI_API_KEY"] = "your-api-key"
7
8model = ChatOpenAI(model="gpt-4o")
9
10messages = [
11 SystemMessage(content="Translate the following from English into Italian"),
12 HumanMessage(content="hi!"),
13]
14
15response = model.invoke(messages)
16
17print(response.content)