Name | llama-index-llms-openai JSON |
Version |
0.3.1
JSON |
| download |
home_page | None |
Summary | llama-index llms openai integration |
upload_time | 2024-11-20 20:56:38 |
maintainer | None |
docs_url | None |
author | llama-index |
requires_python | <4.0,>=3.9 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# LlamaIndex Llms Integration: Openai
## Installation
To install the required package, run:
```bash
%pip install llama-index-llms-openai
```
## Setup
1. Set your OpenAI API key as an environment variable. You can replace `"sk-..."` with your actual API key:
```python
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
```
## Basic Usage
### Generate Completions
To generate a completion for a prompt, use the `complete` method:
```python
from llama_index.llms.openai import OpenAI
resp = OpenAI().complete("Paul Graham is ")
print(resp)
```
### Chat Responses
To send a chat message and receive a response, create a list of `ChatMessage` instances and use the `chat` method:
```python
from llama_index.core.llms import ChatMessage
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)
```
## Streaming Responses
### Stream Complete
To stream responses for a prompt, use the `stream_complete` method:
```python
from llama_index.llms.openai import OpenAI
llm = OpenAI()
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
```
### Stream Chat
To stream chat responses, use the `stream_chat` method:
```python
from llama_index.llms.openai import OpenAI
from llama_index.core.llms import ChatMessage
llm = OpenAI()
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="")
```
## Configure Model
You can specify a particular model when creating the `OpenAI` instance:
```python
llm = OpenAI(model="gpt-3.5-turbo")
resp = llm.complete("Paul Graham is ")
print(resp)
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)
```
## Asynchronous Usage
You can also use asynchronous methods for completion:
```python
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo")
resp = await llm.acomplete("Paul Graham is ")
print(resp)
```
## Set API Key at a Per-Instance Level
If desired, you can have separate LLM instances use different API keys:
```python
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo", api_key="BAD_KEY")
resp = OpenAI().complete("Paul Graham is ")
print(resp)
```
### LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/openai/
Raw data
{
"_id": null,
"home_page": null,
"name": "llama-index-llms-openai",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": "llama-index",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/6d/7b/222b6f9c2cd42d29c410d9485d202317f1cd442c504d3064376954c773bc/llama_index_llms_openai-0.3.1.tar.gz",
"platform": null,
"description": "# LlamaIndex Llms Integration: Openai\n\n## Installation\n\nTo install the required package, run:\n\n```bash\n%pip install llama-index-llms-openai\n```\n\n## Setup\n\n1. Set your OpenAI API key as an environment variable. You can replace `\"sk-...\"` with your actual API key:\n\n```python\nimport os\n\nos.environ[\"OPENAI_API_KEY\"] = \"sk-...\"\n```\n\n## Basic Usage\n\n### Generate Completions\n\nTo generate a completion for a prompt, use the `complete` method:\n\n```python\nfrom llama_index.llms.openai import OpenAI\n\nresp = OpenAI().complete(\"Paul Graham is \")\nprint(resp)\n```\n\n### Chat Responses\n\nTo send a chat message and receive a response, create a list of `ChatMessage` instances and use the `chat` method:\n\n```python\nfrom llama_index.core.llms import ChatMessage\n\nmessages = [\n ChatMessage(\n role=\"system\", content=\"You are a pirate with a colorful personality.\"\n ),\n ChatMessage(role=\"user\", content=\"What is your name?\"),\n]\nresp = OpenAI().chat(messages)\nprint(resp)\n```\n\n## Streaming Responses\n\n### Stream Complete\n\nTo stream responses for a prompt, use the `stream_complete` method:\n\n```python\nfrom llama_index.llms.openai import OpenAI\n\nllm = OpenAI()\nresp = llm.stream_complete(\"Paul Graham is \")\nfor r in resp:\n print(r.delta, end=\"\")\n```\n\n### Stream Chat\n\nTo stream chat responses, use the `stream_chat` method:\n\n```python\nfrom llama_index.llms.openai import OpenAI\nfrom llama_index.core.llms import ChatMessage\n\nllm = OpenAI()\nmessages = [\n ChatMessage(\n role=\"system\", content=\"You are a pirate with a colorful personality.\"\n ),\n ChatMessage(role=\"user\", content=\"What is your name?\"),\n]\nresp = llm.stream_chat(messages)\nfor r in resp:\n print(r.delta, end=\"\")\n```\n\n## Configure Model\n\nYou can specify a particular model when creating the `OpenAI` instance:\n\n```python\nllm = OpenAI(model=\"gpt-3.5-turbo\")\nresp = llm.complete(\"Paul Graham is \")\nprint(resp)\n\nmessages = [\n ChatMessage(\n role=\"system\", content=\"You are a pirate with a colorful personality.\"\n ),\n ChatMessage(role=\"user\", content=\"What is your name?\"),\n]\nresp = llm.chat(messages)\nprint(resp)\n```\n\n## Asynchronous Usage\n\nYou can also use asynchronous methods for completion:\n\n```python\nfrom llama_index.llms.openai import OpenAI\n\nllm = OpenAI(model=\"gpt-3.5-turbo\")\nresp = await llm.acomplete(\"Paul Graham is \")\nprint(resp)\n```\n\n## Set API Key at a Per-Instance Level\n\nIf desired, you can have separate LLM instances use different API keys:\n\n```python\nfrom llama_index.llms.openai import OpenAI\n\nllm = OpenAI(model=\"gpt-3.5-turbo\", api_key=\"BAD_KEY\")\nresp = OpenAI().complete(\"Paul Graham is \")\nprint(resp)\n```\n\n### LLM Implementation example\n\nhttps://docs.llamaindex.ai/en/stable/examples/llm/openai/\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "llama-index llms openai integration",
"version": "0.3.1",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5dcd5badc63469392df245677287a480e850f8433781a0d7482fc85e742c6bf0",
"md5": "26498d07434eac1226c724e23b6109e6",
"sha256": "45218f445b21484334ba3af55c11aaba6bff0a6952abbef483ab710a50167b57"
},
"downloads": -1,
"filename": "llama_index_llms_openai-0.3.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "26498d07434eac1226c724e23b6109e6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 13618,
"upload_time": "2024-11-20T20:56:37",
"upload_time_iso_8601": "2024-11-20T20:56:37.269755Z",
"url": "https://files.pythonhosted.org/packages/5d/cd/5badc63469392df245677287a480e850f8433781a0d7482fc85e742c6bf0/llama_index_llms_openai-0.3.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6d7b222b6f9c2cd42d29c410d9485d202317f1cd442c504d3064376954c773bc",
"md5": "67b26c761af97097a7d495fa37d79baa",
"sha256": "51abfdc0df6c052dda53814fb33eb7af245c81982c02972ce27e292d3d42783d"
},
"downloads": -1,
"filename": "llama_index_llms_openai-0.3.1.tar.gz",
"has_sig": false,
"md5_digest": "67b26c761af97097a7d495fa37d79baa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 13437,
"upload_time": "2024-11-20T20:56:38",
"upload_time_iso_8601": "2024-11-20T20:56:38.105706Z",
"url": "https://files.pythonhosted.org/packages/6d/7b/222b6f9c2cd42d29c410d9485d202317f1cd442c504d3064376954c773bc/llama_index_llms_openai-0.3.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-20 20:56:38",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "llama-index-llms-openai"
}