-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from RockChinQ/feat/more-adapters
Feat: add more adapters
- Loading branch information
Showing
13 changed files
with
699 additions
and
38 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
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,94 @@ | ||
import typing | ||
import traceback | ||
import uuid | ||
import random | ||
|
||
import bardapi as bard | ||
|
||
from free_one_api.entities import request, response | ||
|
||
from ...models import adapter | ||
from ...models.adapter import llm | ||
from ...entities import request, response, exceptions | ||
|
||
|
||
@adapter.llm_adapter | ||
class BardAdapter(llm.LLMLibAdapter): | ||
|
||
@classmethod | ||
def name(cls) -> str: | ||
return "dsdanielpark/Bard-API" | ||
|
||
@classmethod | ||
def description(self) -> str: | ||
return "Use dsdanielpark/Bard-API to access Google Bard web edition." | ||
|
||
def supported_models(self) -> list[str]: | ||
return [ | ||
"gpt-3.5-turbo", | ||
"gpt-4" | ||
] | ||
|
||
def function_call_supported(self) -> bool: | ||
return False | ||
|
||
def stream_mode_supported(self) -> bool: | ||
return False | ||
|
||
def multi_round_supported(self) -> bool: | ||
return True | ||
|
||
@classmethod | ||
def config_comment(cls) -> str: | ||
return \ | ||
"""Currently supports non stream mode only. | ||
You should provide __Secure-1PSID as token extracted from cookies of Bard site. | ||
{ | ||
"token": "bQhxxxxxxxxxxx" | ||
} | ||
Method of getting __Secure-1PSID string, please refer to https://github.com/dsdanielpark/Bard-API | ||
""" | ||
|
||
@classmethod | ||
def supported_path(cls) -> str: | ||
return "/v1/chat/completions" | ||
|
||
_chatbot: bard.Bard = None | ||
|
||
@property | ||
def chatbot(self) -> bard.Bard: | ||
if self._chatbot == None: | ||
self._chatbot = bard.Bard(token=self.config['token']) | ||
return self._chatbot | ||
|
||
def __init__(self, config: dict): | ||
self.config = config | ||
|
||
async def test(self) -> (bool, str): | ||
try: | ||
self.chatbot.get_answer("hello, please reply 'hi' only.") | ||
return True, "" | ||
except Exception as e: | ||
traceback.print_exc() | ||
return False, str(e) | ||
|
||
async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]: | ||
prompt = "" | ||
|
||
for msg in req.messages: | ||
prompt += f"{msg['role']}: {msg['content']}\n" | ||
|
||
prompt += "assistant: " | ||
|
||
random_int = random.randint(0, 1000000000) | ||
|
||
resp_text = self.chatbot.get_answer(prompt)['content'] | ||
|
||
yield response.Response( | ||
id=random_int, | ||
finish_reason=response.FinishReason.STOP, | ||
normal_message=resp_text, | ||
function_call=None | ||
) |
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,92 @@ | ||
import typing | ||
import traceback | ||
import uuid | ||
import random | ||
|
||
import claude_api as claude | ||
|
||
from free_one_api.entities import request, response | ||
|
||
from ...models import adapter | ||
from ...models.adapter import llm | ||
from ...entities import request, response, exceptions | ||
|
||
|
||
@adapter.llm_adapter | ||
class ClaudeAdapter(llm.LLMLibAdapter): | ||
|
||
@classmethod | ||
def name(cls) -> str: | ||
return "KoushikNavuluri/Claude-API" | ||
|
||
@classmethod | ||
def description(self) -> str: | ||
return "Use KoushikNavuluri/Claude-API to access Claude web edition." | ||
|
||
def supported_models(self) -> list[str]: | ||
return [ | ||
"gpt-3.5-turbo", | ||
"gpt-4" | ||
] | ||
|
||
def function_call_supported(self) -> bool: | ||
return False | ||
|
||
def stream_mode_supported(self) -> bool: | ||
return False | ||
|
||
def multi_round_supported(self) -> bool: | ||
return True | ||
|
||
@classmethod | ||
def config_comment(cls) -> str: | ||
return \ | ||
"""Currently supports non stream mode only. | ||
You should provide cookie string as `cookie` in config: | ||
{ | ||
"cookie": "your cookie string" | ||
} | ||
Method of getting cookie string, please refer to https://github.com/KoushikNavuluri/Claude-API | ||
""" | ||
|
||
@classmethod | ||
def supported_path(cls) -> str: | ||
return "/v1/chat/completions" | ||
|
||
chatbot: claude.Client | ||
|
||
def __init__(self, config: dict): | ||
self.config = config | ||
self.chatbot = claude.Client(self.config["cookie"]) | ||
|
||
async def test(self) -> (bool, str): | ||
try: | ||
conversation_id = self.chatbot.create_new_chat()['uuid'] | ||
response = self.chatbot.send_message("Hello, Claude!", conversation_id) | ||
return True, "" | ||
except Exception as e: | ||
traceback.print_exc() | ||
return False, str(e) | ||
|
||
async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]: | ||
prompt = "" | ||
|
||
for msg in req.messages: | ||
prompt += f"{msg['role']}: {msg['content']}\n" | ||
|
||
prompt += "assistant: " | ||
|
||
random_int = random.randint(0, 1000000000) | ||
|
||
conversation_id = self.chatbot.create_new_chat()['uuid'] | ||
resp_text = self.chatbot.send_message(prompt, conversation_id) | ||
|
||
self.chatbot.delete_conversation(conversation_id) | ||
|
||
yield response.Response( | ||
id=random_int, | ||
finish_reason=response.FinishReason.STOP, | ||
normal_message=resp_text, | ||
function_call=None | ||
) |
Oops, something went wrong.