-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhangshulin
committed
Jan 28, 2025
1 parent
cdf9ff4
commit 4f8ed4b
Showing
5 changed files
with
47 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,6 @@ XAI_API_KEY= | |
|
||
# Sambanova | ||
SAMBANOVA_API_KEY= | ||
|
||
# TONGYI | ||
TONGYI_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
import os | ||
import dashscope | ||
from aisuite.provider import Provider | ||
from aisuite.framework import ChatCompletionResponse | ||
import openai | ||
|
||
from aisuite.provider import Provider, LLMError | ||
|
||
class TongyiProvider(Provider): | ||
"""TongyiProvider is a class that provides an interface to the Tongyi's model.""" | ||
|
||
class TongyiProvider(Provider): | ||
def __init__(self, **config): | ||
self.api_key = config.get("api_key") or os.getenv("DASHSCOPE_API_KEY") | ||
|
||
if not self.api_key: | ||
raise EnvironmentError( | ||
"Dashscope API key is missing. Please provide it in the config or set the DASHSCOPE_API_KEY environment variable." | ||
""" | ||
Initialize the Tongyi provider with the given configuration. | ||
Pass the entire configuration dictionary to the Tongyi client constructor. | ||
""" | ||
# Ensure API key is provided either in config or via environment variable | ||
config.setdefault("api_key", os.getenv("TONGYI_API_KEY")) | ||
config["base_url"] = "https://dashscope.aliyuncs.com/compatible-mode/v1" | ||
|
||
if not config["api_key"]: | ||
raise ValueError( | ||
"Tongyi API key is missing. Please provide it in the config or set the TONGYI_API_KEY environment variable." | ||
) | ||
|
||
def chat_completions_create(self, model, messages, **kwargs): | ||
"""Send a chat completion request to the Tongyi's model.""" | ||
|
||
response = dashscope.Generation.call( | ||
api_key=self.api_key, | ||
model=model, | ||
messages=messages, | ||
result_format="message", | ||
**kwargs | ||
) | ||
return self.normalize_response(response) | ||
|
||
def normalize_response(self, response): | ||
"""Normalize the response from Dashscope to match OpenAI's response format.""" | ||
self.client = openai.OpenAI(**config) | ||
|
||
openai_response = ChatCompletionResponse() | ||
openai_response.choices[0].message.content = response["output"]["choices"][0][ | ||
"message" | ||
].get("content") | ||
return openai_response | ||
def chat_completions_create(self, model, messages, **kwargs): | ||
try: | ||
response = self.client.chat.completions.create( | ||
model=model, | ||
messages=messages, | ||
**kwargs, # Pass any additional arguments to the Tongyi API | ||
) | ||
return response | ||
except Exception as e: | ||
raise LLMError(f"An error occurred: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters