Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Baidu Provider Support #124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions aisuite/providers/baidu_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import qianfan
from aisuite.provider import Provider
from aisuite.framework import ChatCompletionResponse


class BaiduProvider(Provider):
"""BaiduProvider is a class that provides an interface to the Baidu's model."""

def __init__(self, **config):
os.environ["QIANFAN_ACCESS_KEY"] = config.get("access_key") or os.getenv(
"QIANFAN_ACCESS_KEY"
)
os.environ["QIANFAN_SECRET_KEY"] = config.get("secret_key") or os.getenv(
"QIANFAN_SECRET_KEY"
)

if not os.getenv("QIANFAN_ACCESS_KEY"):
raise EnvironmentError(
"Qanfan access key is missing. Please provide it in the config or set the QIANFAN_ACCESS_KEY environment variable."
)
if not os.getenv("QIANFAN_SECRET_KEY"):
raise EnvironmentError(
"Qanfan secret key is missing. Please provide it in the config or set the QIANFAN_SECRET_KEY environment variable."
)

self.client = qianfan.ChatCompletion()

def chat_completions_create(self, model, messages, **kwargs):
"""Send a chat completion request to the Baidu's model."""

response = self.client.do(model=model, messages=messages, **kwargs)
return self.normalize_response(response)

def normalize_response(self, response):
"""Normalize the response from Qianfan to match OpenAI's response format."""

openai_response = ChatCompletionResponse()
openai_response.choices[0].message.content = response["body"]["result"]
return openai_response
52 changes: 52 additions & 0 deletions guides/baidu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Baidu

To use Baidu's model, you must log in to the [Baidu AI Cloud Qianfan Large Model Service and Development Platform](https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html) to obtain an Access Key and Secret Key. After obtaining these keys, configure them as environment variables using the following approach:

```shell
export QIANFAN_ACCESS_KEY="your-access-key"

export QIANFAN_SECRET_KEY="your-secret-key"
```

## Create a Chat Completion

### Example Using pip:

```shell
pip install aisuite[all]
```

### Example with poetry:

```shell
poetry add aisuite
```

### In Your Code:

```python
import aisuite as ai

# Initialize the client
client = ai.Client()

# Set provider and model ID
provider = "baidu"
model_id = "ERNIE-3.5-8K"

# Prepare messages
messages = [
{"role": "user", "content": "Hello!"},
]

# Make a chat completion request
response = client.chat.completions.create(
model=f"{provider}:{model_id}",
messages=messages,
)

# Output the response
print(response.choices[0].message.content)
```

Happy coding! If you would like to contribute, please read our [Contributing Guide](../CONTRIBUTING.md).
Loading