-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:edenai/edenai-apis
- Loading branch information
Showing
26 changed files
with
1,270 additions
and
50 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 |
---|---|---|
|
@@ -24,3 +24,5 @@ media | |
.idea | ||
|
||
pyrightconfig.json | ||
|
||
*.env* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"user_id": "", | ||
"app_id": "", | ||
"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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"api_key": "", | ||
"email": "", | ||
"password": "", | ||
"comment": "You can either set your API Key, or your Login Credentials (Email and Password) for Authentication" | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .mistral_api import MistralApi |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from edenai_apis.utils.exception import ( | ||
ProviderErrorLists, | ||
ProviderInternalServerError, | ||
ProviderTimeoutError, | ||
) | ||
|
||
# NOTE: error messages should be regex patterns | ||
ERRORS: ProviderErrorLists = { | ||
ProviderInternalServerError: [ | ||
r"Error calling Clarifai", | ||
r"Failure", | ||
], | ||
ProviderTimeoutError: [ | ||
r"<[^<>]+debug_error_string = 'UNKNOWN:Error received from peer ipv4:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+ {created_time:'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+[\+\-]\d{2}:\d{2}', grpc_status:14, grpc_message:'GOAWAY received'}'>", | ||
r"Model is deploying" | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"text": { | ||
"generation": { | ||
"constraints": { | ||
"models":["mistral-7B-Instruct", "mistral-7B-OpenOrca","openHermes-2-mistral-7B"], | ||
"default_model": "mistral-7B-Instruct" | ||
}, | ||
"version": "v1" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import Dict | ||
from google.protobuf.json_format import MessageToDict | ||
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc | ||
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel | ||
from clarifai_grpc.grpc.api.status import status_code_pb2 | ||
|
||
from edenai_apis.features import ProviderInterface, TextInterface | ||
from edenai_apis.features.text.generation.generation_dataclass import ( | ||
GenerationDataClass, | ||
) | ||
from edenai_apis.loaders.data_loader import ProviderDataEnum | ||
from edenai_apis.loaders.loaders import load_provider | ||
from edenai_apis.utils.exception import ProviderException | ||
from edenai_apis.utils.types import ResponseType | ||
|
||
|
||
|
||
class MistralApi(ProviderInterface, TextInterface): | ||
provider_name = "mistral" | ||
|
||
def __init__(self, api_keys: Dict = {}) -> None: | ||
self.api_settings = load_provider( | ||
ProviderDataEnum.KEY, self.provider_name, api_keys=api_keys | ||
) | ||
self.user_id = self.api_settings["user_id"] | ||
self.app_id = self.api_settings["app_id"] | ||
self.key = self.api_settings["key"] | ||
|
||
def __chat_markup_tokens(self, model): | ||
if model == "mistral-7B-Instruct": | ||
return "[INST]", "[/INST]" | ||
else: | ||
return "<|im_start|>", "<|im_end|>" | ||
|
||
def text__generation( | ||
self, text: str, temperature: float, max_tokens: int, model: str | ||
) -> ResponseType[GenerationDataClass]: | ||
start, end = self.__chat_markup_tokens(model) | ||
|
||
text = f"{start} {text} {end}" | ||
|
||
channel = ClarifaiChannel.get_grpc_channel() | ||
stub = service_pb2_grpc.V2Stub(channel) | ||
|
||
metadata = (("authorization", self.key),) | ||
user_data_object = resources_pb2.UserAppIDSet( | ||
user_id="mistralai", app_id="completion" | ||
) | ||
|
||
post_model_outputs_response = stub.PostModelOutputs( | ||
service_pb2.PostModelOutputsRequest( | ||
user_app_id=user_data_object, | ||
model_id=model, | ||
inputs=[ | ||
resources_pb2.Input( | ||
data=resources_pb2.Data(text=resources_pb2.Text(raw=text)) | ||
) | ||
], | ||
), | ||
metadata=metadata, | ||
) | ||
|
||
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS: | ||
raise ProviderException( | ||
post_model_outputs_response.status.description, | ||
code=post_model_outputs_response.status.code, | ||
) | ||
|
||
response = MessageToDict( | ||
post_model_outputs_response, preserving_proto_field_name=True | ||
) | ||
|
||
output = response.get("outputs", []) | ||
if len(output) == 0: | ||
raise ProviderException( | ||
"Mistral returned an empty response!", | ||
code=post_model_outputs_response.status.code, | ||
) | ||
|
||
original_response = output[0].get("data", {}) or {} | ||
|
||
return ResponseType[GenerationDataClass]( | ||
original_response=original_response, | ||
standardized_response=GenerationDataClass( | ||
generated_text=(original_response.get("text", {}) or {}).get("raw", "") | ||
), | ||
) |
13 changes: 13 additions & 0 deletions
13
edenai_apis/apis/mistral/outputs/text/generation_output.json
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"original_response": { | ||
"text": { | ||
"raw": "party\n\nAI assistant: Hi there! I'm an AI language model, designed to assist and engage in conversations. My name is not \"who are you?\" but I'm here to help you with any questions or tasks you may have. How can I assist you today?", | ||
"text_info": { | ||
"encoding": "UnknownTextEnc" | ||
} | ||
} | ||
}, | ||
"standardized_response": { | ||
"generated_text": "party\n\nAI assistant: Hi there! I'm an AI language model, designed to assist and engage in conversations. My name is not \"who are you?\" but I'm here to help you with any questions or tasks you may have. How can I assist you today?" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .senseloaf_api import SenseloafApi |
Oops, something went wrong.