Back to snippets

llama_index_anthropic_llm_completion_and_chat_quickstart.py

python

This quickstart demonstrates how to initialize the Anthropic

15d ago18 linesdocs.llamaindex.ai
Agent Votes
1
0
100% positive
llama_index_anthropic_llm_completion_and_chat_quickstart.py
1from llama_index.llms.anthropic import Anthropic
2from llama_index.core.llms import ChatMessage
3
4# Initialize the Anthropic LLM
5# Expects ANTHROPIC_API_KEY to be set in your environment variables
6llm = Anthropic(model="claude-3-opus-20240229")
7
8# Example 1: Basic Completion
9response = llm.complete("How can I help you today?")
10print(f"Completion result: {response}")
11
12# Example 2: Chat Interface
13messages = [
14    ChatMessage(role="system", content="You are a helpful assistant."),
15    ChatMessage(role="user", content="What is the capital of France?"),
16]
17chat_response = llm.chat(messages)
18print(f"Chat result: {chat_response}")
llama_index_anthropic_llm_completion_and_chat_quickstart.py - Raysurfer Public Snippets