diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-06 01:05:00 +0200 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-06 01:05:00 +0200 |
commit | d44b39b31c83c6a4bc636bea931275702c700feb (patch) | |
tree | 8967aa34d2e2f9fa7aa1d86131f524ddd3925ad8 /g4f/Provider | |
parent | Add authless OpenaiChat (diff) | |
download | gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar.gz gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar.bz2 gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar.lz gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar.xz gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.tar.zst gpt4free-d44b39b31c83c6a4bc636bea931275702c700feb.zip |
Diffstat (limited to 'g4f/Provider')
-rw-r--r-- | g4f/Provider/base_provider.py | 1 | ||||
-rw-r--r-- | g4f/Provider/needs_auth/Groq.py | 23 | ||||
-rw-r--r-- | g4f/Provider/needs_auth/Openai.py | 74 | ||||
-rw-r--r-- | g4f/Provider/needs_auth/__init__.py | 4 |
4 files changed, 101 insertions, 1 deletions
diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py index 8e761dba..4c0157f3 100644 --- a/g4f/Provider/base_provider.py +++ b/g4f/Provider/base_provider.py @@ -1,2 +1,3 @@ from ..providers.base_provider import * +from ..providers.types import FinishReason from .helper import get_cookies, format_prompt
\ No newline at end of file diff --git a/g4f/Provider/needs_auth/Groq.py b/g4f/Provider/needs_auth/Groq.py new file mode 100644 index 00000000..87e87e60 --- /dev/null +++ b/g4f/Provider/needs_auth/Groq.py @@ -0,0 +1,23 @@ +from __future__ import annotations + +from .Openai import Openai +from ...typing import AsyncResult, Messages + +class Groq(Openai): + url = "https://console.groq.com/playground" + working = True + default_model = "mixtral-8x7b-32768" + models = ["mixtral-8x7b-32768", "llama2-70b-4096", "gemma-7b-it"] + model_aliases = {"mixtral-8x7b": "mixtral-8x7b-32768", "llama2-70b": "llama2-70b-4096"} + + @classmethod + def create_async_generator( + cls, + model: str, + messages: Messages, + api_base: str = "https://api.groq.com/openai/v1", + **kwargs + ) -> AsyncResult: + return super().create_async_generator( + model, messages, api_base=api_base, **kwargs + )
\ No newline at end of file diff --git a/g4f/Provider/needs_auth/Openai.py b/g4f/Provider/needs_auth/Openai.py new file mode 100644 index 00000000..b876cd0b --- /dev/null +++ b/g4f/Provider/needs_auth/Openai.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +import json + +from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin, FinishReason +from ...typing import AsyncResult, Messages +from ...requests.raise_for_status import raise_for_status +from ...requests import StreamSession +from ...errors import MissingAuthError + +class Openai(AsyncGeneratorProvider, ProviderModelMixin): + url = "https://openai.com" + working = True + needs_auth = True + supports_message_history = True + supports_system_message = True + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + timeout: int = 120, + api_key: str = None, + api_base: str = "https://api.openai.com/v1", + temperature: float = None, + max_tokens: int = None, + top_p: float = None, + stop: str = None, + stream: bool = False, + **kwargs + ) -> AsyncResult: + if api_key is None: + raise MissingAuthError('Add a "api_key"') + async with StreamSession( + proxies={"all": proxy}, + headers=cls.get_headers(api_key), + timeout=timeout + ) as session: + data = { + "messages": messages, + "model": cls.get_model(model), + "temperature": temperature, + "max_tokens": max_tokens, + "top_p": top_p, + "stop": stop, + "stream": stream, + } + async with session.post(f"{api_base.rstrip('/')}/chat/completions", json=data) as response: + await raise_for_status(response) + async for line in response.iter_lines(): + if line.startswith(b"data: ") or not stream: + async for chunk in cls.read_line(line[6:] if stream else line, stream): + yield chunk + + @staticmethod + async def read_line(line: str, stream: bool): + if line == b"[DONE]": + return + choice = json.loads(line)["choices"][0] + if stream and "content" in choice["delta"] and choice["delta"]["content"]: + yield choice["delta"]["content"] + elif not stream and "content" in choice["message"]: + yield choice["message"]["content"] + if "finish_reason" in choice and choice["finish_reason"] is not None: + yield FinishReason(choice["finish_reason"]) + + @staticmethod + def get_headers(api_key: str) -> dict: + return { + "Authorization": f"Bearer {api_key}", + "Content-Type": "application/json", + }
\ No newline at end of file diff --git a/g4f/Provider/needs_auth/__init__.py b/g4f/Provider/needs_auth/__init__.py index 5eb1b2eb..92fa165b 100644 --- a/g4f/Provider/needs_auth/__init__.py +++ b/g4f/Provider/needs_auth/__init__.py @@ -4,4 +4,6 @@ from .Theb import Theb from .ThebApi import ThebApi from .OpenaiChat import OpenaiChat from .OpenAssistant import OpenAssistant -from .Poe import Poe
\ No newline at end of file +from .Poe import Poe +from .Openai import Openai +from .Groq import Groq
\ No newline at end of file |