Create Ava.py
Browse files- Providers/Ava.py +61 -0
Providers/Ava.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from ...typing import sha256, Dict, get_type_hints
|
5 |
+
|
6 |
+
url = "https://ava-alpha-api.codelink.io/api/chat"
|
7 |
+
model = ["gpt-4"]
|
8 |
+
supports_stream = True
|
9 |
+
needs_auth = False
|
10 |
+
|
11 |
+
class Model:
|
12 |
+
def __init__(self):
|
13 |
+
self.url = "https://ava-alpha-api.codelink.io/api/chat"
|
14 |
+
self.headers = {
|
15 |
+
"content-type": "application/json"
|
16 |
+
}
|
17 |
+
self.payload = {
|
18 |
+
"model": "gpt-4",
|
19 |
+
"temperature": 0.6,
|
20 |
+
"stream": True
|
21 |
+
}
|
22 |
+
self.accumulated_content = ""
|
23 |
+
|
24 |
+
def _process_line(self, line):
|
25 |
+
line_text = line.decode("utf-8").strip()
|
26 |
+
if line_text.startswith("data:"):
|
27 |
+
data = line_text[len("data:"):]
|
28 |
+
try:
|
29 |
+
data_json = json.loads(data)
|
30 |
+
if "choices" in data_json:
|
31 |
+
choices = data_json["choices"]
|
32 |
+
for choice in choices:
|
33 |
+
if "finish_reason" in choice and choice["finish_reason"] == "stop":
|
34 |
+
break
|
35 |
+
if "delta" in choice and "content" in choice["delta"]:
|
36 |
+
content = choice["delta"]["content"]
|
37 |
+
self.accumulated_content += content
|
38 |
+
except json.JSONDecodeError as e:
|
39 |
+
return
|
40 |
+
|
41 |
+
def ChatCompletion(self, messages):
|
42 |
+
self.payload["messages"] = messages
|
43 |
+
|
44 |
+
with requests.post(self.url, headers=self.headers, data=json.dumps(self.payload), stream=True) as response:
|
45 |
+
for line in response.iter_lines():
|
46 |
+
self._process_line(line)
|
47 |
+
|
48 |
+
accumulated_content = self.accumulated_content
|
49 |
+
self.accumulated_content = ""
|
50 |
+
|
51 |
+
return accumulated_content
|
52 |
+
|
53 |
+
def _create_completion(model: str, messages: list, stream: bool, **kwargs):
|
54 |
+
model = Model()
|
55 |
+
|
56 |
+
# Call the chat completion method
|
57 |
+
response = model.ChatCompletion(messages)
|
58 |
+
return response
|
59 |
+
|
60 |
+
params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
|
61 |
+
'(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
|