These API docs are condensed to have the essentials, to reduce token usage when giving GPT the API docs.
### GPT-4 API docs
```
My API key is in .env as OPENAI_API_KEY
GPT-4 API docs
"""
'''python
import openai
# Make sure to set your API key before running this example
# openai.api_key = "your_api_key_here"
response = openai.ChatCompletion.create(
model="gpt-4-0314",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What was the first question I asked you?"},
{"role": "assistant", "content": "The first question you asked was 'What is the capital of France?'"}
]
)
# Parse the response
assistant_reply = response['choices'][0]['message']['content']
print(assistant_reply)
'''
Chat: POST https://api.openai.com/v1/chat/completions, model, messages, optional: temperature (0-2, default=1), top_p (0-1, default=1), n (default=1), stream (default=false), stop, max_tokens, presence_penalty (default=0, -2 to 2), frequency_penalty (default=0, -2 to 2), logit_bias, user. Example: curl -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello!"}]}'
Chat models: input messages (array of role+content), output message. Conversations may start with a system message, then alternate user and assistant messages. Total tokens <= model's max limit (4096 for gpt-3.5-turbo). Assistant's reply: response['choices'][0]['message']['content']. Finish reasons: stop, length, content_filter, null.
Tokens affect API cost, time, and success. Both input and output tokens count. To count tokens without API call, use tiktoken.
Instruct chat models by iterating, making instructions explicit, specifying answer format, or asking step by step. For gpt-3.5-turbo, important instructions in user message.
Model endpoint compatibility: /v1/chat/completions compatible with gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301. Use gpt-4-0314 if not otherwise specified.
"""
Python example:
"""
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(completion.choices[0].message)
"""
```