OpenAI
Basic Usage
Call complete
with a prompt
from llama_index.llms import OpenAI
resp = OpenAI().complete("Paul Graham is ")
print(resp)
a computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of Y Combinator, a startup accelerator and seed capital firm. Graham has also written several influential essays on startups and entrepreneurship, which have gained a large following in the tech community. He has been involved in the founding and funding of numerous successful startups, including Reddit, Dropbox, and Airbnb. Graham is known for his insightful and often controversial opinions on various topics, including education, inequality, and the future of technology.
Call chat
with a list of messages
from llama_index.llms import ChatMessage, OpenAI
messages = [
ChatMessage(role="system", content="You are a pirate with a colorful personality"),
ChatMessage(role="user", content="What is your name"),
]
resp = OpenAI().chat(messages)
print(resp)
assistant: Ahoy there, matey! The name be Captain Crimsonbeard, the most colorful pirate to sail the seven seas!
Streaming
Using stream_complete
endpoint
from llama_index.llms import OpenAI
llm = OpenAI()
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
a computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of the startup accelerator Y Combinator. Graham has also written several influential essays on startups and entrepreneurship, which have gained a large following in the tech community. He has been involved in the founding and funding of numerous successful startups, including Reddit, Dropbox, and Airbnb. Graham is known for his insightful and often controversial opinions on various topics, including education, inequality, and the future of technology.
Using stream_chat
endpoint
from llama_index.llms import OpenAI
llm = OpenAI(stream=True)
messages = [
ChatMessage(role="system", content="You are a pirate with a colorful personality"),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
Ahoy there, matey! The name be Captain Crimsonbeard, the most colorful pirate to sail the seven seas!
Configure Model
from llama_index.llms import OpenAI
llm = OpenAI(model="text-davinci-003")
resp = llm.complete("Paul Graham is ")
print(resp)
Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work in the startup world, having co-founded the accelerator Y Combinator and investing in hundreds of startups. He is also a prolific writer, having written several books on topics such as startups, programming, and technology. He is a frequent speaker at conferences and universities, and his essays have been widely read and discussed.
messages = [
ChatMessage(role="system", content="You are a pirate with a colorful personality"),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.chat(messages)
print(resp)
assistant:
My name is Captain Jack Sparrow.
Function Calling
from pydantic import BaseModel
from llama_index.llms.openai_utils import to_openai_function
class Song(BaseModel):
"""A song with name and artist"""
name: str
artist: str
song_fn = to_openai_function(Song)
from llama_index.llms import OpenAI
response = OpenAI().complete("Generate a song", functions=[song_fn])
function_call = response.additional_kwargs["function_call"]
print(function_call)
Async
from llama_index.llms import OpenAI
llm = OpenAI(model="text-davinci-003")
resp = await llm.acomplete("Paul Graham is ")
print(resp)
Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work in the startup world, having co-founded the accelerator Y Combinator and investing in hundreds of startups. He is also a prolific writer, having written several books on topics such as startups, programming, and technology. He is a frequent speaker at conferences and universities, and his essays have been widely read and discussed.
resp = await llm.astream_complete("Paul Graham is ")
async for delta in resp:
print(delta.delta, end="")
Paul Graham is an entrepreneur, venture capitalist, and computer scientist. He is best known for his work in the startup world, having co-founded the accelerator Y Combinator and investing in hundreds of startups. He is also a prolific writer, having written several books on topics such as startups, programming, and technology. He is a frequent speaker at conferences and universities, and his essays have been widely read and discussed.
Set API Key at a per-instance level
If desired, you can have separate LLM instances use separate API keys.
from llama_index.llms import OpenAI
llm = OpenAI(model="text-davinci-003", api_key="BAD_KEY")
resp = OpenAI().complete("Paul Graham is ")
print(resp)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 3
1 from llama_index.llms import OpenAI
----> 3 llm = OpenAI(model="text-davinci-003", api_key="BAD_KEY")
4 resp = OpenAI().complete("Paul Graham is ")
5 print(resp)
File /workspaces/llama_index/llama_index/llms/openai.py:51, in OpenAI.__init__(self, model, temperature, max_tokens, additional_kwargs, max_retries, api_key, callback_manager, **kwargs)
40 def __init__(
41 self,
42 model: str = "gpt-3.5-turbo",
(...)
49 **kwargs: Any,
50 ) -> None:
---> 51 validate_openai_api_key(
52 api_key, kwargs.get("api_type", None)
53 )
55 self.model = model
56 self.temperature = temperature
File /workspaces/llama_index/llama_index/llms/openai_utils.py:272, in validate_openai_api_key(api_key, api_type)
268 raise ValueError(MISSING_API_KEY_ERROR_MESSAGE)
269 elif openai_api_type == "open_ai" and not OPENAI_API_KEY_FORMAT.search(
270 openai_api_key
271 ):
--> 272 raise ValueError(INVALID_API_KEY_ERROR_MESSAGE)
ValueError: Invalid OpenAI API key.
API key should be of the format: "sk-" followed by 48 alphanumeric characters.