mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-08-01 12:40:23 +08:00
first commit
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
# Chat
|
||||
(*chat*)
|
||||
|
||||
## Overview
|
||||
|
||||
Chat completion operations
|
||||
|
||||
### Available Operations
|
||||
|
||||
* [complete](#complete) - Create a chat completion
|
||||
* [complete_stream](#complete_stream) - Create a chat completion
|
||||
|
||||
## complete
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
|
||||
### Example Usage
|
||||
|
||||
<!-- UsageSnippet language="python" operationID="createChatCompletion" method="post" path="/chat/completions" -->
|
||||
```python
|
||||
from openrouter import OpenRouter, models
|
||||
import os
|
||||
|
||||
|
||||
with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
|
||||
) as open_router:
|
||||
|
||||
res = open_router.chat.complete(messages=[
|
||||
{
|
||||
"role": models.ChatCompletionUserMessageParamRole.USER,
|
||||
"content": "Hello, how are you?",
|
||||
},
|
||||
], stream=False, temperature=1, top_p=1)
|
||||
|
||||
# Handle response
|
||||
print(res)
|
||||
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
| Parameter | Type | Required | Description | Example |
|
||||
| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `messages` | List[[models.ChatCompletionMessageParam](../../models/chatcompletionmessageparam.md)] | :heavy_check_mark: | List of messages for the conversation | [<br/>{<br/>"role": "user",<br/>"content": "Hello, how are you?"<br/>}<br/>] |
|
||||
| `model` | *Optional[str]* | :heavy_minus_sign: | Model to use for completion | |
|
||||
| `frequency_penalty` | *OptionalNullable[float]* | :heavy_minus_sign: | Frequency penalty (-2.0 to 2.0) | |
|
||||
| `logit_bias` | Dict[str, *float*] | :heavy_minus_sign: | Token logit bias adjustments | |
|
||||
| `logprobs` | *OptionalNullable[bool]* | :heavy_minus_sign: | Return log probabilities | |
|
||||
| `top_logprobs` | *OptionalNullable[float]* | :heavy_minus_sign: | Number of top log probabilities to return (0-20) | |
|
||||
| `max_completion_tokens` | *OptionalNullable[float]* | :heavy_minus_sign: | Maximum tokens in completion | |
|
||||
| `max_tokens` | *OptionalNullable[float]* | :heavy_minus_sign: | Maximum tokens (deprecated, use max_completion_tokens) | |
|
||||
| `metadata` | Dict[str, *str*] | :heavy_minus_sign: | Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values) | |
|
||||
| `presence_penalty` | *OptionalNullable[float]* | :heavy_minus_sign: | Presence penalty (-2.0 to 2.0) | |
|
||||
| `reasoning` | [OptionalNullable[models.ChatCompletionCreateParamsReasoning]](../../models/chatcompletioncreateparamsreasoning.md) | :heavy_minus_sign: | Reasoning configuration | |
|
||||
| `response_format` | [Optional[models.ChatCompletionCreateParamsResponseFormatUnion]](../../models/chatcompletioncreateparamsresponseformatunion.md) | :heavy_minus_sign: | Response format configuration | |
|
||||
| `seed` | *OptionalNullable[int]* | :heavy_minus_sign: | Random seed for deterministic outputs | |
|
||||
| `stop` | [OptionalNullable[models.ChatCompletionCreateParamsStop]](../../models/chatcompletioncreateparamsstop.md) | :heavy_minus_sign: | Stop sequences (up to 4) | |
|
||||
| `stream` | *OptionalNullable[bool]* | :heavy_minus_sign: | Enable streaming response | |
|
||||
| `stream_options` | [OptionalNullable[models.ChatCompletionCreateParamsStreamOptions]](../../models/chatcompletioncreateparamsstreamoptions.md) | :heavy_minus_sign: | N/A | |
|
||||
| `temperature` | *OptionalNullable[float]* | :heavy_minus_sign: | Sampling temperature (0-2) | |
|
||||
| `tool_choice` | [Optional[models.ChatCompletionToolChoiceOption]](../../models/chatcompletiontoolchoiceoption.md) | :heavy_minus_sign: | Tool choice configuration | |
|
||||
| `tools` | List[[models.ChatCompletionTool](../../models/chatcompletiontool.md)] | :heavy_minus_sign: | Available tools for function calling | |
|
||||
| `top_p` | *OptionalNullable[float]* | :heavy_minus_sign: | Nucleus sampling parameter (0-1) | |
|
||||
| `user` | *Optional[str]* | :heavy_minus_sign: | Unique user identifier | |
|
||||
| `models_llm` | List[*str*] | :heavy_minus_sign: | Order of models to fallback to for this request | |
|
||||
| `reasoning_effort` | [OptionalNullable[models.ChatCompletionCreateParamsReasoningEffort]](../../models/chatcompletioncreateparamsreasoningeffort.md) | :heavy_minus_sign: | Reasoning effort | |
|
||||
| `provider` | [OptionalNullable[models.ChatCompletionCreateParamsProvider]](../../models/chatcompletioncreateparamsprovider.md) | :heavy_minus_sign: | When multiple model providers are available, optionally indicate your routing preference. | |
|
||||
| `plugins` | List[[models.ChatCompletionCreateParamsPluginUnion](../../models/chatcompletioncreateparamspluginunion.md)] | :heavy_minus_sign: | Plugins you want to enable for this request, including their settings. | |
|
||||
| `retries` | [Optional[utils.RetryConfig]](../../models/utils/retryconfig.md) | :heavy_minus_sign: | Configuration to override the default retry behavior of the client. | |
|
||||
|
||||
### Response
|
||||
|
||||
**[models.ChatCompletion](../../models/chatcompletion.md)**
|
||||
|
||||
### Errors
|
||||
|
||||
| Error Type | Status Code | Content Type |
|
||||
| ----------------------------- | ----------------------------- | ----------------------------- |
|
||||
| errors.ChatCompletionError | 400, 401, 429 | application/json |
|
||||
| errors.ChatCompletionError | 500 | application/json |
|
||||
| errors.OpenRouterDefaultError | 4XX, 5XX | \*/\* |
|
||||
|
||||
## complete_stream
|
||||
|
||||
Creates a model response for the given chat conversation. Supports both streaming and non-streaming modes.
|
||||
|
||||
### Example Usage
|
||||
|
||||
<!-- UsageSnippet language="python" operationID="streamChatCompletion" method="post" path="/chat/completions#stream" -->
|
||||
```python
|
||||
from openrouter import OpenRouter, models
|
||||
import os
|
||||
|
||||
|
||||
with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
|
||||
) as open_router:
|
||||
|
||||
res = open_router.chat.complete_stream(messages=[
|
||||
{
|
||||
"role": models.ChatCompletionUserMessageParamRole.USER,
|
||||
"content": "Hello, how are you?",
|
||||
},
|
||||
], stream=True, temperature=1, top_p=1)
|
||||
|
||||
with res as event_stream:
|
||||
for event in event_stream:
|
||||
# handle event
|
||||
print(event, flush=True)
|
||||
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
| Parameter | Type | Required | Description | Example |
|
||||
| ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `messages` | List[[models.ChatCompletionMessageParam](../../models/chatcompletionmessageparam.md)] | :heavy_check_mark: | List of messages for the conversation | [<br/>{<br/>"role": "user",<br/>"content": "Hello, how are you?"<br/>}<br/>] |
|
||||
| `model` | *Optional[str]* | :heavy_minus_sign: | Model to use for completion | |
|
||||
| `frequency_penalty` | *OptionalNullable[float]* | :heavy_minus_sign: | Frequency penalty (-2.0 to 2.0) | |
|
||||
| `logit_bias` | Dict[str, *float*] | :heavy_minus_sign: | Token logit bias adjustments | |
|
||||
| `logprobs` | *OptionalNullable[bool]* | :heavy_minus_sign: | Return log probabilities | |
|
||||
| `top_logprobs` | *OptionalNullable[float]* | :heavy_minus_sign: | Number of top log probabilities to return (0-20) | |
|
||||
| `max_completion_tokens` | *OptionalNullable[float]* | :heavy_minus_sign: | Maximum tokens in completion | |
|
||||
| `max_tokens` | *OptionalNullable[float]* | :heavy_minus_sign: | Maximum tokens (deprecated, use max_completion_tokens) | |
|
||||
| `metadata` | Dict[str, *str*] | :heavy_minus_sign: | Key-value pairs for additional object information (max 16 pairs, 64 char keys, 512 char values) | |
|
||||
| `presence_penalty` | *OptionalNullable[float]* | :heavy_minus_sign: | Presence penalty (-2.0 to 2.0) | |
|
||||
| `reasoning` | [OptionalNullable[models.ChatStreamCompletionCreateParamsReasoning]](../../models/chatstreamcompletioncreateparamsreasoning.md) | :heavy_minus_sign: | Reasoning configuration | |
|
||||
| `response_format` | [Optional[models.ChatStreamCompletionCreateParamsResponseFormatUnion]](../../models/chatstreamcompletioncreateparamsresponseformatunion.md) | :heavy_minus_sign: | Response format configuration | |
|
||||
| `seed` | *OptionalNullable[int]* | :heavy_minus_sign: | Random seed for deterministic outputs | |
|
||||
| `stop` | [OptionalNullable[models.ChatStreamCompletionCreateParamsStop]](../../models/chatstreamcompletioncreateparamsstop.md) | :heavy_minus_sign: | Stop sequences (up to 4) | |
|
||||
| `stream` | *Optional[bool]* | :heavy_minus_sign: | Enable streaming response | |
|
||||
| `stream_options` | [OptionalNullable[models.ChatStreamCompletionCreateParamsStreamOptions]](../../models/chatstreamcompletioncreateparamsstreamoptions.md) | :heavy_minus_sign: | N/A | |
|
||||
| `temperature` | *OptionalNullable[float]* | :heavy_minus_sign: | Sampling temperature (0-2) | |
|
||||
| `tool_choice` | [Optional[models.ChatCompletionToolChoiceOption]](../../models/chatcompletiontoolchoiceoption.md) | :heavy_minus_sign: | Tool choice configuration | |
|
||||
| `tools` | List[[models.ChatCompletionTool](../../models/chatcompletiontool.md)] | :heavy_minus_sign: | Available tools for function calling | |
|
||||
| `top_p` | *OptionalNullable[float]* | :heavy_minus_sign: | Nucleus sampling parameter (0-1) | |
|
||||
| `user` | *Optional[str]* | :heavy_minus_sign: | Unique user identifier | |
|
||||
| `models_llm` | List[*str*] | :heavy_minus_sign: | Order of models to fallback to for this request | |
|
||||
| `reasoning_effort` | [OptionalNullable[models.ChatStreamCompletionCreateParamsReasoningEffort]](../../models/chatstreamcompletioncreateparamsreasoningeffort.md) | :heavy_minus_sign: | Reasoning effort | |
|
||||
| `provider` | [OptionalNullable[models.ChatStreamCompletionCreateParamsProvider]](../../models/chatstreamcompletioncreateparamsprovider.md) | :heavy_minus_sign: | When multiple model providers are available, optionally indicate your routing preference. | |
|
||||
| `plugins` | List[[models.ChatStreamCompletionCreateParamsPluginUnion](../../models/chatstreamcompletioncreateparamspluginunion.md)] | :heavy_minus_sign: | Plugins you want to enable for this request, including their settings. | |
|
||||
| `retries` | [Optional[utils.RetryConfig]](../../models/utils/retryconfig.md) | :heavy_minus_sign: | Configuration to override the default retry behavior of the client. | |
|
||||
|
||||
### Response
|
||||
|
||||
**[Union[eventstreaming.EventStream[models.StreamChatCompletionResponseBody], eventstreaming.EventStreamAsync[models.StreamChatCompletionResponseBody]]](../../models/.md)**
|
||||
|
||||
### Errors
|
||||
|
||||
| Error Type | Status Code | Content Type |
|
||||
| ----------------------------- | ----------------------------- | ----------------------------- |
|
||||
| errors.ChatCompletionError | 400, 401, 429 | application/json |
|
||||
| errors.ChatCompletionError | 500 | application/json |
|
||||
| errors.OpenRouterDefaultError | 4XX, 5XX | \*/\* |
|
||||
@@ -0,0 +1,10 @@
|
||||
# OpenRouter SDK
|
||||
|
||||
## Overview
|
||||
|
||||
OpenRouter Chat Completions API: OpenAI-compatible Chat Completions API with additional OpenRouter features
|
||||
|
||||
OpenRouter Documentation
|
||||
<https://openrouter.ai/docs>
|
||||
|
||||
### Available Operations
|
||||
Reference in New Issue
Block a user