Skip to content

Commit 453f572

Browse files
authored
Initial OpenAI /v1/chat/completions API compatibility (ollama#2376)
1 parent c9dfa6e commit 453f572

File tree

3 files changed

+466
-0
lines changed

3 files changed

+466
-0
lines changed

docs/openai.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# OpenAI compatibility
2+
3+
Ollama provides experimental compatibility with parts of the [OpenAI API](https://platform.openai.com/docs/api-reference) to help connect existing applications to Ollama.
4+
5+
> **Note:** OpenAI compatibility is experimental and is subject to major adjustments including breaking changes. For fully-featured access to the Ollama API, see the Ollama [Python library](https://github.com/ollama/ollama-python), [JavaScript library](https://github.com/ollama/ollama-js) and [REST API](https://github.com/jmorganca/ollama/blob/main/docs/api.md).
6+
7+
## Usage
8+
9+
### OpenAI Python library
10+
11+
```python
12+
from openai import OpenAI
13+
14+
client = OpenAI(
15+
base_url='http://localhost:11434/v1/',
16+
17+
# required but ignored
18+
api_key='ollama',
19+
)
20+
21+
chat_completion = client.chat.completions.create(
22+
messages=[
23+
{
24+
'role': 'user',
25+
'content': 'Say this is a test',
26+
}
27+
],
28+
model='llama2',
29+
)
30+
```
31+
32+
### OpenAI JavaScript library
33+
34+
```javascript
35+
import OpenAI from 'openai'
36+
37+
const openai = new OpenAI({
38+
baseURL: 'http://localhost:11434/v1/',
39+
40+
// required but ignored
41+
apiKey: 'ollama',
42+
})
43+
44+
const chatCompletion = await openai.chat.completions.create({
45+
messages: [{ role: 'user', content: 'Say this is a test' }],
46+
model: 'llama2',
47+
})
48+
```
49+
50+
### `curl`
51+
52+
```
53+
curl http://localhost:11434/v1/chat/completions \
54+
-H "Content-Type: application/json" \
55+
-d '{
56+
"model": "llama2",
57+
"messages": [
58+
{
59+
"role": "system",
60+
"content": "You are a helpful assistant."
61+
},
62+
{
63+
"role": "user",
64+
"content": "Hello!"
65+
}
66+
]
67+
}'
68+
```
69+
70+
## Endpoints
71+
72+
### `/v1/chat/completions`
73+
74+
#### Supported features
75+
76+
- [x] Chat completions
77+
- [x] Streaming
78+
- [x] JSON mode
79+
- [x] Reproducible outputs
80+
- [ ] Vision
81+
- [ ] Function calling
82+
- [ ] Logprobs
83+
84+
#### Supported request fields
85+
86+
- [x] `model`
87+
- [x] `messages`
88+
- [x] Text `content`
89+
- [ ] Array of `content` parts
90+
- [x] `frequency_penalty`
91+
- [x] `presence_penalty`
92+
- [x] `response_format`
93+
- [x] `seed`
94+
- [x] `stop`
95+
- [x] `stream`
96+
- [x] `temperature`
97+
- [x] `top_p`
98+
- [x] `max_tokens`
99+
- [ ] `logit_bias`
100+
- [ ] `tools`
101+
- [ ] `tool_choice`
102+
- [ ] `user`
103+
104+
#### Notes
105+
106+
- Setting `seed` will always set `temperature` to `0`
107+
- `finish_reason` will always be `stop`
108+
- `usage.prompt_tokens` will be 0 for completions where prompt evaluation is cached
109+
110+
## Models
111+
112+
Before using a model, pull it locally `ollama pull`:
113+
114+
```shell
115+
ollama pull llama2
116+
```
117+
118+
### Default model names
119+
120+
For tooling that relies on default OpenAI model names such as `gpt-3.5-turbo`, use `ollama cp` to copy an existing model name to a temporary name:
121+
122+
```
123+
ollama cp llama2 gpt-3.5-turbo
124+
```
125+
126+
Afterwards, this new model name can be specified the `model` field:
127+
128+
```shell
129+
curl http://localhost:11434/v1/chat/completions \
130+
-H "Content-Type: application/json" \
131+
-d '{
132+
"model": "gpt-3.5-turbo",
133+
"messages": [
134+
{
135+
"role": "user",
136+
"content": "Hello!"
137+
}
138+
]
139+
}'
140+
```

0 commit comments

Comments
 (0)