This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from BIGmama-technology/8-add-openai-apis
8 add openai apis
- Loading branch information
Showing
27 changed files
with
362 additions
and
72 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
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,9 +1,5 @@ | ||
from hyko_sdk.definitions import ToolkitAPI | ||
"""register all apis""" | ||
|
||
from .openai.text_completion.metadata import func as text_completion | ||
|
||
all = {text_completion.name: text_completion} | ||
|
||
|
||
def api_handler(name: str) -> ToolkitAPI: | ||
return all[name] | ||
### Openai API | ||
from .openai.text_completion.metadata import func as func | ||
from .openai.text_embedding.metadata import func as func # noqa: F811 |
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,22 @@ | ||
from hyko_sdk.definitions import ToolkitAPI as _ToolkitAPI | ||
|
||
|
||
class APIRegistry: | ||
_registry: dict[str, _ToolkitAPI] = {} | ||
|
||
@classmethod | ||
def register(cls, name: str, api: _ToolkitAPI): | ||
cls._registry[name] = api | ||
|
||
@classmethod | ||
def get_handler(cls, name: str) -> _ToolkitAPI: | ||
if name not in cls._registry: | ||
raise ValueError(f"API handler '{name}' not found") | ||
return cls._registry[name] | ||
|
||
|
||
class ToolkitAPI(_ToolkitAPI): | ||
def __init__(self, name: str, task: str, description: str): | ||
# Automatically register the instance upon creation | ||
super().__init__(name=name, task=task, description=description) | ||
APIRegistry.register(name, self) |
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,79 @@ | ||
from enum import Enum | ||
|
||
import httpx | ||
from hyko_sdk.models import CoreModel, Method | ||
from pydantic import Field | ||
|
||
from hyko_toolkit.apis.api_registry import ToolkitAPI | ||
from hyko_toolkit.exceptions import APICallError | ||
|
||
func = ToolkitAPI( | ||
name="openai_text_embedding", | ||
task="openai", | ||
description="Use openai api for text embedding.", | ||
) | ||
|
||
|
||
class Model(str, Enum): | ||
ada_002 = "text-embedding-ada-002" | ||
large_3 = "text-embedding-3-large" | ||
small_3 = "text-embedding-3-small" | ||
|
||
|
||
@func.set_input | ||
class Inputs(CoreModel): | ||
text: str = Field(..., description="Text to embed.") | ||
|
||
|
||
@func.set_param | ||
class Params(CoreModel): | ||
api_key: str = Field(description="API key") | ||
model: Model = Field( | ||
default=Model.ada_002, | ||
description="Openai model to use.", | ||
) | ||
|
||
|
||
@func.set_output | ||
class Outputs(CoreModel): | ||
embedding: list[float] = Field(..., description="text embedding.") | ||
|
||
|
||
class Embedding(CoreModel): | ||
embedding: list[float] | ||
|
||
|
||
class Usage(CoreModel): | ||
prompt_tokens: int | ||
total_tokens: int | ||
|
||
|
||
class Response(CoreModel): | ||
data: list[Embedding] | ||
model: str | ||
usage: Usage | ||
|
||
|
||
@func.on_call | ||
async def call(inputs: Inputs, params: Params): | ||
async with httpx.AsyncClient() as client: | ||
res = await client.request( | ||
method=Method.post, | ||
url="https://api.openai.com/v1/embeddings", | ||
headers={ | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {params.api_key}", | ||
}, | ||
json={ | ||
"input": inputs.text, | ||
"model": params.model, | ||
"encoding_format": "float", | ||
}, | ||
timeout=60 * 5, | ||
) | ||
if res.is_success: | ||
response = Response(**res.json()) | ||
else: | ||
raise APICallError(status=res.status_code, detail=res.text) | ||
|
||
return Outputs(embedding=response.data[0].embedding) |
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/huggingface/mask_generation/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/image_classification/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/image_instance_segmentation/metadata.py
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
5 changes: 2 additions & 3 deletions
5
hyko_toolkit/models/computer_vision/ultralytics/image_obb_object_detection/main.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/image_obb_object_detection/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/image_object_detection/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/image_pose_estimation/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/video_instance_segmentation/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/video_obb_object_detection/metadata.py
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
3 changes: 1 addition & 2 deletions
3
hyko_toolkit/models/computer_vision/ultralytics/video_object_detection/metadata.py
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
Oops, something went wrong.