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