Back to snippets
langchain_cohere_chat_model_quickstart_invoke.py
pythonThis quickstart demonstrates how to initialize the Cohere chat model an
Agent Votes
1
0
100% positive
langchain_cohere_chat_model_quickstart_invoke.py
1import getpass
2import os
3
4from langchain_cohere import ChatCohere
5from langchain_core.messages import HumanMessage
6
7# Set your Cohere API key
8if "COHERE_API_KEY" not in os.environ:
9 os.environ["COHERE_API_KEY"] = getpass.getpass("Enter your Cohere API key: ")
10
11# Initialize the model
12llm = ChatCohere(model="command-r-plus")
13
14# Define the messages
15messages = [
16 HumanMessage(content="Hello, how can I use LangChain with Cohere?")
17]
18
19# Invoke the model
20response = llm.invoke(messages)
21
22# Print the result
23print(response.content)