Skip to content

Commit

Permalink
Re-added http based Fireworks & Together; Minor fixes. (#38)
Browse files Browse the repository at this point in the history
Support for Fireworks provider using http.
Fixed few things based on the doc.
Support for Together using http requests.
Removing interface.py files
---------

Co-authored-by: Kevin Solorio <[email protected]>
  • Loading branch information
rohitprasad15 and ksolo authored Oct 2, 2024
1 parent afe2af1 commit ad46773
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 256 deletions.
10 changes: 10 additions & 0 deletions aisuite/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class ProviderNames(str, Enum):
ANTHROPIC = "anthropic"
AWS = "aws"
AZURE = "azure"
FIREWORKS = "fireworks"
GROQ = "groq"
GOOGLE = "google"
HUGGINGFACE = "huggingface"
MISTRAL = "mistral"
OLLAMA = "ollama"
OPENAI = "openai"
TOGETHER = "together"


class ProviderFactory:
Expand Down Expand Up @@ -54,6 +56,14 @@ class ProviderFactory:
),
ProviderNames.OLLAMA: ("aisuite.providers.ollama_provider", "OllamaProvider"),
ProviderNames.OPENAI: ("aisuite.providers.openai_provider", "OpenAIProvider"),
ProviderNames.FIREWORKS: (
"aisuite.providers.fireworks_provider",
"FireworksProvider",
),
ProviderNames.TOGETHER: (
"aisuite.providers.together_provider",
"TogetherProvider",
),
}

@classmethod
Expand Down
6 changes: 0 additions & 6 deletions aisuite/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
"""Provides the individual provider interfaces for each FM provider."""

from .fireworks_interface import FireworksInterface
from .octo_interface import OctoInterface
from .replicate_interface import ReplicateInterface
from .together_interface import TogetherInterface
35 changes: 0 additions & 35 deletions aisuite/providers/fireworks_interface.py

This file was deleted.

65 changes: 65 additions & 0 deletions aisuite/providers/fireworks_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import httpx
from aisuite.provider import Provider, LLMError
from aisuite.framework import ChatCompletionResponse


class FireworksProvider(Provider):
"""
Fireworks AI Provider using httpx for direct API calls.
"""

BASE_URL = "https://api.fireworks.ai/inference/v1/chat/completions"

def __init__(self, **config):
"""
Initialize the Fireworks provider with the given configuration.
The API key is fetched from the config or environment variables.
"""
self.api_key = config.get("api_key", os.getenv("FIREWORKS_API_KEY"))
if not self.api_key:
raise ValueError(
"Fireworks API key is missing. Please provide it in the config or set the FIREWORKS_API_KEY environment variable."
)

# Optionally set a custom timeout (default to 30s)
self.timeout = config.get("timeout", 30)

def chat_completions_create(self, model, messages, **kwargs):
"""
Makes a request to the Fireworks AI chat completions endpoint using httpx.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}

data = {
"model": model,
"messages": messages,
**kwargs, # Pass any additional arguments to the API
}

try:
# Make the request to Fireworks AI endpoint.
response = httpx.post(
self.BASE_URL, json=data, headers=headers, timeout=self.timeout
)
response.raise_for_status()
except httpx.HTTPStatusError as http_err:
raise LLMError(f"Fireworks AI request failed: {http_err}")
except Exception as e:
raise LLMError(f"An error occurred: {e}")

# Return the normalized response
return self._normalize_response(response.json())

def _normalize_response(self, response_data):
"""
Normalize the response to a common format (ChatCompletionResponse).
"""
normalized_response = ChatCompletionResponse()
normalized_response.choices[0].message.content = response_data["choices"][0][
"message"
]["content"]
return normalized_response
40 changes: 0 additions & 40 deletions aisuite/providers/octo_interface.py

This file was deleted.

40 changes: 0 additions & 40 deletions aisuite/providers/replicate_interface.py

This file was deleted.

40 changes: 0 additions & 40 deletions aisuite/providers/together_interface.py

This file was deleted.

65 changes: 65 additions & 0 deletions aisuite/providers/together_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import httpx
from aisuite.provider import Provider, LLMError
from aisuite.framework import ChatCompletionResponse


class TogetherProvider(Provider):
"""
Together AI Provider using httpx for direct API calls.
"""

BASE_URL = "https://api.together.xyz/v1/chat/completions"

def __init__(self, **config):
"""
Initialize the Fireworks provider with the given configuration.
The API key is fetched from the config or environment variables.
"""
self.api_key = config.get("api_key", os.getenv("TOGETHER_API_KEY"))
if not self.api_key:
raise ValueError(
"Together API key is missing. Please provide it in the config or set the TOGETHER_API_KEY environment variable."
)

# Optionally set a custom timeout (default to 30s)
self.timeout = config.get("timeout", 30)

def chat_completions_create(self, model, messages, **kwargs):
"""
Makes a request to the Fireworks AI chat completions endpoint using httpx.
"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
}

data = {
"model": model,
"messages": messages,
**kwargs, # Pass any additional arguments to the API
}

try:
# Make the request to Fireworks AI endpoint.
response = httpx.post(
self.BASE_URL, json=data, headers=headers, timeout=self.timeout
)
response.raise_for_status()
except httpx.HTTPStatusError as http_err:
raise LLMError(f"Together AI request failed: {http_err}")
except Exception as e:
raise LLMError(f"An error occurred: {e}")

# Return the normalized response
return self._normalize_response(response.json())

def _normalize_response(self, response_data):
"""
Normalize the response to a common format (ChatCompletionResponse).
"""
normalized_response = ChatCompletionResponse()
normalized_response.choices[0].message.content = response_data["choices"][0][
"message"
]["content"]
return normalized_response
Loading

0 comments on commit ad46773

Please sign in to comment.