gpt4free/g4f/Provider/Lockchat.py

65 lines
2.0 KiB
Python
Raw Normal View History

from __future__ import annotations
2023-07-28 13:07:17 +03:00
import json
import requests
from ..typing import Any, CreateResult
2023-07-28 13:07:17 +03:00
from .base_provider import BaseProvider
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-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...")
Lockchat.create_completion(
2023-08-27 18:37:44 +03:00
model = model,
messages = messages,
stream = stream,
temperature = temperature,
**kwargs)
2023-07-28 13:07:17 +03:00
if b"content" in token:
token = json.loads(token.decode("utf-8").split("data: ")[1])
token = token["choices"][0]["delta"].get("content")
if token:
yield (token)
@classmethod
@property
def params(cls):
params = [
("model", "str"),
("messages", "list[dict[str, str]]"),
("stream", "bool"),
("temperature", "float"),
]
param = ", ".join([": ".join(p) for p in params])
2023-09-18 00:23:54 +03:00
return f"g4f.provider.{cls.__name__} supports: ({param})"