-
-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ecosia Provider, Add OpenaiAccount alias
Use AsyncClient in API, add web_search parameter in API Improve error messages in Openai
- Loading branch information
Showing
12 changed files
with
125 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import asyncio | ||
import g4f | ||
from g4f.client import AsyncClient | ||
|
||
async def main(): | ||
client = AsyncClient( | ||
provider=g4f.Provider.Ecosia, | ||
) | ||
async for chunk in client.chat.completions.create( | ||
[{"role": "user", "content": "happy dogs on work. write some lines"}], | ||
g4f.models.default, | ||
stream=True, | ||
green=True, | ||
): | ||
print(chunk.choices[0].delta.content or "", end="") | ||
print(f"\nwith {chunk.model}") | ||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
from __future__ import annotations | ||
|
||
import base64 | ||
import json | ||
from aiohttp import ClientSession, BaseConnector | ||
|
||
from ..typing import AsyncResult, Messages | ||
from ..requests.raise_for_status import raise_for_status | ||
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin | ||
from .helper import get_connector | ||
|
||
class Ecosia(AsyncGeneratorProvider, ProviderModelMixin): | ||
url = "https://www.ecosia.org" | ||
working = True | ||
supports_gpt_35_turbo = True | ||
default_model = "gpt-3.5-turbo-0125" | ||
model_aliases = {"gpt-3.5-turbo": "gpt-3.5-turbo-0125"} | ||
|
||
@classmethod | ||
async def create_async_generator( | ||
cls, | ||
model: str, | ||
messages: Messages, | ||
connector: BaseConnector = None, | ||
green: bool = False, | ||
proxy: str = None, | ||
**kwargs | ||
) -> AsyncResult: | ||
cls.get_model(model) | ||
headers = { | ||
"authority": "api.ecosia.org", | ||
"accept": "*/*", | ||
"origin": cls.url, | ||
"referer": f"{cls.url}/", | ||
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36", | ||
} | ||
async with ClientSession(headers=headers, connector=get_connector(connector, proxy)) as session: | ||
data = { | ||
"messages": base64.b64encode(json.dumps(messages).encode()).decode() | ||
} | ||
api_url = f"https://api.ecosia.org/v2/chat/?sp={'eco' if green else 'productivity'}" | ||
async with session.post(api_url, json=data) as response: | ||
await raise_for_status(response) | ||
async for chunk in response.content.iter_any(): | ||
if chunk: | ||
yield chunk.decode(errors="ignore") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from .OpenaiChat import OpenaiChat | ||
|
||
class OpenaiAccount(OpenaiChat): | ||
label = "OpenAI ChatGPT with Account" | ||
needs_auth = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters