2023-09-03 11:26:26 +03:00
|
|
|
from __future__ import annotations
|
2023-07-28 13:07:17 +03:00
|
|
|
|
2023-09-03 11:26:26 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2023-10-04 08:20:51 +03:00
|
|
|
from ...typing import Any, CreateResult
|
|
|
|
from ..base_provider import BaseProvider
|
2023-07-28 13:07:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Lockchat(BaseProvider):
|
2023-08-27 18:37:44 +03:00
|
|
|
url: str = "http://supertest.lockchat.app"
|
|
|
|
supports_stream = True
|
2023-07-28 13:07:17 +03:00
|
|
|
supports_gpt_35_turbo = True
|
2023-08-27 18:37:44 +03:00
|
|
|
supports_gpt_4 = True
|
2023-07-28 13:07:17 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_completion(
|
|
|
|
model: str,
|
|
|
|
messages: list[dict[str, str]],
|
2023-08-27 18:37:44 +03:00
|
|
|
stream: bool, **kwargs: Any) -> CreateResult:
|
|
|
|
|
2023-07-28 13:07:17 +03:00
|
|
|
temperature = float(kwargs.get("temperature", 0.7))
|
|
|
|
payload = {
|
|
|
|
"temperature": temperature,
|
2023-08-27 18:37:44 +03:00
|
|
|
"messages" : messages,
|
|
|
|
"model" : model,
|
|
|
|
"stream" : True,
|
2023-07-28 13:07:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"user-agent": "ChatX/39 CFNetwork/1408.0.4 Darwin/22.5.0",
|
|
|
|
}
|
2023-08-27 18:37:44 +03:00
|
|
|
response = requests.post("http://supertest.lockchat.app/v1/chat/completions",
|
|
|
|
json=payload, headers=headers, stream=True)
|
2023-10-23 10:46:25 +03:00
|
|
|
|
2023-07-28 13:07:17 +03:00
|
|
|
response.raise_for_status()
|
|
|
|
for token in response.iter_lines():
|
|
|
|
if b"The model: `gpt-4` does not exist" in token:
|
|
|
|
print("error, retrying...")
|
2023-11-20 21:40:55 +03:00
|
|
|
|
2023-07-28 13:07:17 +03:00
|
|
|
Lockchat.create_completion(
|
2023-08-27 18:37:44 +03:00
|
|
|
model = model,
|
|
|
|
messages = messages,
|
|
|
|
stream = stream,
|
|
|
|
temperature = temperature,
|
|
|
|
**kwargs)
|
2023-10-23 10:46:25 +03:00
|
|
|
|
2023-07-28 13:07:17 +03:00
|
|
|
if b"content" in token:
|
|
|
|
token = json.loads(token.decode("utf-8").split("data: ")[1])
|
2023-11-20 21:40:55 +03:00
|
|
|
token = token["choices"][0]["delta"].get("content")
|
|
|
|
|
|
|
|
if token:
|
2023-11-20 15:59:14 +03:00
|
|
|
yield (token)
|