mirror of https://github.com/xtekky/gpt4free
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
1 year ago
|
from __future__ import annotations
|
||
1 year ago
|
|
||
1 year ago
|
import json
|
||
|
|
||
|
import requests
|
||
|
|
||
12 months ago
|
from ...typing import Any, CreateResult
|
||
|
from ..base_provider import BaseProvider
|
||
1 year ago
|
|
||
|
|
||
|
class Lockchat(BaseProvider):
|
||
1 year ago
|
url: str = "http://supertest.lockchat.app"
|
||
|
supports_stream = True
|
||
1 year ago
|
supports_gpt_35_turbo = True
|
||
1 year ago
|
supports_gpt_4 = True
|
||
1 year ago
|
|
||
|
@staticmethod
|
||
|
def create_completion(
|
||
|
model: str,
|
||
|
messages: list[dict[str, str]],
|
||
1 year ago
|
stream: bool, **kwargs: Any) -> CreateResult:
|
||
|
|
||
1 year ago
|
temperature = float(kwargs.get("temperature", 0.7))
|
||
|
payload = {
|
||
|
"temperature": temperature,
|
||
1 year ago
|
"messages" : messages,
|
||
|
"model" : model,
|
||
|
"stream" : True,
|
||
1 year ago
|
}
|
||
|
|
||
|
headers = {
|
||
|
"user-agent": "ChatX/39 CFNetwork/1408.0.4 Darwin/22.5.0",
|
||
|
}
|
||
1 year ago
|
response = requests.post("http://supertest.lockchat.app/v1/chat/completions",
|
||
|
json=payload, headers=headers, stream=True)
|
||
|
|
||
1 year ago
|
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(
|
||
1 year ago
|
model = model,
|
||
|
messages = messages,
|
||
|
stream = stream,
|
||
|
temperature = temperature,
|
||
|
**kwargs)
|
||
|
|
||
1 year ago
|
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])
|
||
1 year ago
|
return f"g4f.provider.{cls.__name__} supports: ({param})"
|