Skip to content

Commit

Permalink
feat: support new api, json format (#4971)
Browse files Browse the repository at this point in the history
* feat: support new api, json format

* fix: change openai version

* add: notebook

* fix: embedding call api

* fix: poetry lock

GitOrigin-RevId: 9a594f9dc21775042dd2464eec591d616c5be800
  • Loading branch information
berkecanrizai authored and Manul from Pathway committed Nov 20, 2023
1 parent 69709a2 commit ef81b23
Show file tree
Hide file tree
Showing 4 changed files with 521 additions and 285 deletions.
7 changes: 3 additions & 4 deletions llm_app/model_wrappers/api_clients/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def __init__(
):
import openai

openai.api_requestor.TIMEOUT_SECS = 90

openai.api_key = api_key
if api_type:
openai.api_type = api_type
Expand All @@ -34,20 +32,21 @@ def __init__(
openai.api_version = api_version

self.api = openai
self.client = openai.OpenAI(api_key=api_key)


class OpenAIChatCompletionClient(OpenAIClient):
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def make_request(self, **kwargs):
logfun("Calling OpenAI chat completion service %s", str(kwargs)[:100])
return self.api.ChatCompletion.create(**kwargs)
return self.client.chat.completions.create(**kwargs)


class OpenAIEmbeddingClient(OpenAIClient):
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def make_request(self, **kwargs):
logfun("Calling OpenAI embedding service %s", str(kwargs)[:100])
return self.api.Embedding.create(**kwargs)
return self.client.embeddings.create(**kwargs)


class HuggingFaceClient(APIClient):
Expand Down
4 changes: 3 additions & 1 deletion llm_app/model_wrappers/openai_wrapper/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __call__(self, text: str, locator="gpt-3.5-turbo", **kwargs) -> str:
kwargs["model"] = locator

messages = MessagePreparer.prepare_chat_messages(text)
if "response_format" in kwargs:
kwargs["response_format"] = kwargs["response_format"].value

logfun(f"Calling OpenAI API with: {messages}\n")
response = self.api_client.make_request(messages=messages, **kwargs)
Expand Down Expand Up @@ -126,7 +128,7 @@ def __call__(self, text: str, locator="text-embedding-ada-002", **kwargs):
kwargs["model"] = locator

response = self.api_client.make_request(input=[text], **kwargs)
return response["data"][0]["embedding"]
return response.data[0].embedding

def apply(
self,
Expand Down
Loading

0 comments on commit ef81b23

Please sign in to comment.