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 support for anthropic, palm, bedrock, ai21, cohere, replicate, togetherai #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 19 additions & 30 deletions chainfury/components/openai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import requests
from pydantic import BaseModel
from typing import Any, List, Union, Dict, Optional

from litellm import completion, text_completion, AuthenticationError,
from chainfury import Secret, model_registry, exponential_backoff, Model, UnAuthException
from chainfury.components.const import Env


def openai_completion(
model: str,
prompt: Union[str, List[Union[str, List[str]]]],
Expand Down Expand Up @@ -69,13 +68,7 @@ def openai_completion(
raise Exception("OpenAI API key not found. Please set OPENAI_TOKEN environment variable or pass through function")

def _fn():
r = requests.post(
"https://api.openai.com/v1/completions",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}",
},
json={
data = {
"model": model,
"prompt": prompt,
"max_tokens": max_tokens,
Expand All @@ -90,13 +83,14 @@ def _fn():
"best_of": best_of,
"logit_bias": logit_bias,
"user": user,
},
)
if r.status_code == 401:
raise UnAuthException(r.text)
if r.status_code != 200:
raise Exception(f"OpenAI API returned status code {r.status_code}: {r.text}")
return r.json()
}
try:
return text_completion(**data)
except Exception as e:
if isinstance(e, AuthenticationError):
raise UnAuthException(e.text)
else:
raise Exception(f"API returned status code {e.status_code}: {e.message}")

return exponential_backoff(_fn, max_retries=retry_count, retry_delay=retry_delay)

Expand Down Expand Up @@ -195,13 +189,7 @@ def openai_chat(
messages = [x.dict(skip_defaults=True) for x in messages] # type: ignore

def _fn():
r = requests.post(
"https://api.openai.com/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}",
},
json={
data = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
Expand All @@ -213,13 +201,14 @@ def _fn():
"frequency_penalty": frequency_penalty,
"logit_bias": logit_bias,
"user": user,
},
)
if r.status_code == 401:
raise UnAuthException(r.text)
if r.status_code != 200:
raise Exception(f"OpenAI API returned status code {r.status_code}: {r.text}")
return r.json()
}
try:
return completion(**data)
except Exception as e:
if isinstance(e, AuthenticationError):
raise UnAuthException(e.text)
else:
raise Exception(f"API returned status code {e.status_code}: {e.message}")

return exponential_backoff(_fn, max_retries=retry_count, retry_delay=retry_delay)

Expand Down