Skip to content

Commit

Permalink
[0.6.0] Turbo with distributed_chat
Browse files Browse the repository at this point in the history
  • Loading branch information
yashbonde committed Dec 14, 2024
1 parent ffcbf3b commit 091a33d
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ minor versions.

All relevant steps to be taken will be mentioned here.

0.6.0
-----

- ``distributed_chat`` functionality in ``tuneapi.apis.turbo`` support. In all APIs search for ``model.distributed_chat()``
method. This enables **fault tolerant LLM API calls**.

0.5.13
-----

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tuneapi"
version = "0.5.13"
version = "0.6.0"
description = "Tune AI APIs."
authors = ["Frello Technology Private Limited <[email protected]>"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions tuneapi/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from tuneapi.apis.model_groq import Groq
from tuneapi.apis.model_mistral import Mistral
from tuneapi.apis.model_gemini import Gemini
from tuneapi.apis.turbo import distributed_chat
18 changes: 18 additions & 0 deletions tuneapi/apis/model_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.apis.turbo import distributed_chat


class Anthropic(tt.ModelInterface):
Expand Down Expand Up @@ -236,6 +237,23 @@ def stream_chat(
break
return

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)


# helper methods

Expand Down
20 changes: 19 additions & 1 deletion tuneapi/apis/model_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import json
import requests
from typing import Optional, Any, Dict
from typing import Optional, Any, Dict, List

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.apis.turbo import distributed_chat


class Gemini(tt.ModelInterface):
Expand Down Expand Up @@ -276,3 +277,20 @@ def stream_chat(
fn_call["arguments"] = fn_call.pop("args")
yield fn_call
block_lines = ""

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)
18 changes: 18 additions & 0 deletions tuneapi/apis/model_groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.apis.turbo import distributed_chat


class Groq(tt.ModelInterface):
Expand Down Expand Up @@ -190,3 +191,20 @@ def stream_chat(
fn_call["arguments"] = tu.from_json(fn_call["arguments"])
yield fn_call
return

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)
30 changes: 23 additions & 7 deletions tuneapi/apis/model_mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.utils import ENV, SimplerTimes as stime, from_json, to_json
from tuneapi.types import Thread, human, Message
from tuneapi.apis.turbo import distributed_chat


class Mistral(tt.ModelInterface):
Expand All @@ -23,7 +22,7 @@ def __init__(
):
self.model_id = id
self.base_url = base_url
self.api_token = ENV.MISTRAL_TOKEN("")
self.api_token = tu.ENV.MISTRAL_TOKEN("")
self.extra_headers = extra_headers

def set_api_token(self, token: str) -> None:
Expand Down Expand Up @@ -95,7 +94,7 @@ def _process_input(self, chats, token: Optional[str] = None):

def chat(
self,
chats: Thread | str,
chats: tt.Thread | str,
model: Optional[str] = None,
max_tokens: int = 1024,
temperature: float = 1,
Expand Down Expand Up @@ -124,7 +123,7 @@ def chat(

def stream_chat(
self,
chats: Thread | str,
chats: tt.Thread | str,
model: Optional[str] = None,
max_tokens: int = 1024,
temperature: float = 1,
Expand All @@ -135,7 +134,7 @@ def stream_chat(
extra_headers: Optional[Dict[str, str]] = None,
):
tools = []
if isinstance(chats, Thread):
if isinstance(chats, tt.Thread):
tools = [{"type": "function", "function": x.to_dict()} for x in chats.tools]
headers, messages = self._process_input(chats, token)
extra_headers = extra_headers or self.extra_headers
Expand Down Expand Up @@ -191,6 +190,23 @@ def stream_chat(
except:
break
if fn_call:
fn_call["arguments"] = from_json(fn_call["arguments"])
fn_call["arguments"] = tu.from_json(fn_call["arguments"])
yield fn_call
return

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)
18 changes: 18 additions & 0 deletions tuneapi/apis/model_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.apis.turbo import distributed_chat


class Openai(tt.ModelInterface):
Expand Down Expand Up @@ -190,6 +191,23 @@ def stream_chat(
yield fn_call
return

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)

def embedding(
self,
chats: tt.Thread | List[str] | str,
Expand Down
18 changes: 18 additions & 0 deletions tuneapi/apis/model_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import tuneapi.utils as tu
import tuneapi.types as tt
from tuneapi.apis.turbo import distributed_chat


class TuneModel(tt.ModelInterface):
Expand Down Expand Up @@ -217,3 +218,20 @@ def stream_chat(
fn_call["arguments"] = tu.from_json(fn_call["arguments"])
yield fn_call
return

def distributed_chat(
self,
prompts: List[tt.Thread],
post_logic: Optional[callable] = None,
max_threads: int = 10,
retry: int = 3,
pbar=True,
):
return distributed_chat(
self,
prompts=prompts,
post_logic=post_logic,
max_threads=max_threads,
retry=retry,
pbar=pbar,
)
Loading

0 comments on commit 091a33d

Please sign in to comment.