XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

add reka core model (vision)

2b271013
abc <98614666+xtekky@users.noreply.github.com>
提交于

代码差异

5 个文件 +197 -2
Modified README.md +5 -2
@@ -304,8 +304,11 @@ While we wait for gpt-5, here is a list of new models that are at least better t
304 304 | ------ | ------- | ------ | ------ |
305 305 | [mixtral-8x22b](https://huggingface.co/mistral-community/Mixtral-8x22B-v0.1) | `g4f.Provider.DeepInfra` | 176B / 44b active | gpt-3.5-turbo |
306 306 | [dbrx-instruct](https://www.databricks.com/blog/introducing-dbrx-new-state-art-open-llm) | `g4f.Provider.DeepInfra` | 132B / 36B active| gpt-3.5-turbo |
307 | [command-r+](https://txt.cohere.com/command-r-plus-microsoft-azure/) | `g4f.Provider.HuggingChat` | 104B | gpt-4-0613 |
308
307 | [command-r+](https://txt.cohere.com/command-r-plus-microsoft-azure/) | `g4f.Provider.HuggingChat` | 104B | gpt-4-0314 |
308 | [reka-core](https://chat.reka.ai/) | `g4f.Provider.Reka` | 104B | gpt-4-vision |
309 | [claude-3-opus](https://anthropic.com/) | `g4f.Provider.You` | ?B | gpt-4-0125-preview |
310 | [claude-3-sonnet](https://anthropic.com/) | `g4f.Provider.You` | ?B | gpt-4-0314 |
311 | [llama-3-70b](https://meta.ai/) | `g4f.Provider.Llama` or `DeepInfra` | ?B | gpt-4-0314 |
309 312
310 313 ### GPT-3.5
311 314
Added etc/examples/image_chat_reka.py +27 -0
@@ -0,0 +1,27 @@
1 # Image Chat with Reca
2 # !! YOU NEED COOKIES / BE LOGGED IN TO chat.reka.ai
3 # download an image and save it as test.png in the same folder
4
5 from g4f.client import Client
6 from g4f.Provider import Reka
7
8 client = Client(
9 provider = Reka # Optional if you set model name to reka-core
10 )
11
12 completion = client.chat.completions.create(
13 model = "reka-core",
14 messages = [
15 {
16 "role": "user",
17 "content": "What can you see in the image ?"
18 }
19 ],
20 stream = True,
21 image = open("test.png", "rb") # open("path", "rb"), do not use .read(), etc. it must be a file object
22 )
23
24 for message in completion:
25 print(message.choices[0].delta.content or "")
26
27 # >>> In the image there is ...
Added g4f/Provider/Reka.py +148 -0
@@ -0,0 +1,148 @@
1 from __future__ import annotations
2
3 import os, requests, time, json
4 from ..typing import CreateResult, Messages, ImageType
5 from .base_provider import AbstractProvider
6 from ..cookies import get_cookies
7
8 class Reka(AbstractProvider):
9 url = "https://chat.reka.ai/"
10 working = True
11 supports_stream = True
12 cookies = {}
13
14 @classmethod
15 def create_completion(
16 cls,
17 model: str,
18 messages: Messages,
19 stream: bool,
20 proxy: str = None,
21 timeout: int = 180,
22 bearer_auth: str = None,
23 image: ImageType = None, **kwargs) -> CreateResult:
24
25 cls.proxy = proxy
26
27 if not bearer_auth:
28 cls.cookies = get_cookies("chat.reka.ai")
29
30 if not cls.cookies:
31 raise ValueError("No cookies found for chat.reka.ai")
32
33 elif "appSession" not in cls.cookies:
34 raise ValueError("No appSession found in cookies for chat.reka.ai, log in or provide bearer_auth")
35
36 bearer_auth = cls.get_access_token(cls)
37
38 conversation = []
39 for message in messages:
40 conversation.append({
41 "type": "human",
42 "text": message["content"],
43 })
44
45 if image:
46 image_url = cls.upload_image(cls, bearer_auth, image)
47 conversation[-1]["image_url"] = image_url
48 conversation[-1]["media_type"] = "image"
49
50 headers = {
51 'accept': '*/*',
52 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
53 'authorization': f'Bearer {bearer_auth}',
54 'cache-control': 'no-cache',
55 'content-type': 'application/json',
56 'origin': 'https://chat.reka.ai',
57 'pragma': 'no-cache',
58 'priority': 'u=1, i',
59 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
60 'sec-ch-ua-mobile': '?0',
61 'sec-ch-ua-platform': '"macOS"',
62 'sec-fetch-dest': 'empty',
63 'sec-fetch-mode': 'cors',
64 'sec-fetch-site': 'same-origin',
65 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
66 }
67
68 json_data = {
69 'conversation_history': conversation,
70 'stream': True,
71 'use_search_engine': False,
72 'use_code_interpreter': False,
73 'model_name': 'reka-core',
74 'random_seed': int(time.time() * 1000),
75 }
76
77 tokens = ''
78
79 response = requests.post('https://chat.reka.ai/api/chat',
80 cookies=cls.cookies, headers=headers, json=json_data, proxies=cls.proxy, stream=True)
81
82 for completion in response.iter_lines():
83 if b'data' in completion:
84 token_data = json.loads(completion.decode('utf-8')[5:])['text']
85
86 yield (token_data.replace(tokens, ''))
87
88 tokens = token_data
89
90 def upload_image(cls, access_token, image: ImageType) -> str:
91 boundary_token = os.urandom(8).hex()
92
93 headers = {
94 'accept': '*/*',
95 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
96 'cache-control': 'no-cache',
97 'authorization': f'Bearer {access_token}',
98 'content-type': f'multipart/form-data; boundary=----WebKitFormBoundary{boundary_token}',
99 'origin': 'https://chat.reka.ai',
100 'pragma': 'no-cache',
101 'priority': 'u=1, i',
102 'referer': 'https://chat.reka.ai/chat/hPReZExtDOPvUfF8vCPC',
103 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
104 'sec-ch-ua-mobile': '?0',
105 'sec-ch-ua-platform': '"macOS"',
106 'sec-fetch-dest': 'empty',
107 'sec-fetch-mode': 'cors',
108 'sec-fetch-site': 'same-origin',
109 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
110 }
111
112 image_data = image.read()
113
114 boundary = f'----WebKitFormBoundary{boundary_token}'
115 data = f'--{boundary}\r\nContent-Disposition: form-data; name="image"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n'
116 data += image_data.decode('latin-1')
117 data += f'\r\n--{boundary}--\r\n'
118
119 response = requests.post('https://chat.reka.ai/api/upload-image',
120 cookies=Reka.cookies, headers=headers, proxies=cls.proxy, data=data.encode('latin-1'))
121
122 return response.json()['media_url']
123
124 def get_access_token(cls):
125 headers = {
126 'accept': '*/*',
127 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
128 'cache-control': 'no-cache',
129 'pragma': 'no-cache',
130 'priority': 'u=1, i',
131 'referer': 'https://chat.reka.ai/chat',
132 'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
133 'sec-ch-ua-mobile': '?0',
134 'sec-ch-ua-platform': '"macOS"',
135 'sec-fetch-dest': 'empty',
136 'sec-fetch-mode': 'cors',
137 'sec-fetch-site': 'same-origin',
138 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
139 }
140
141 try:
142 response = requests.get('https://chat.reka.ai/bff/auth/access_token',
143 cookies=cls.cookies, headers=headers)
144
145 return response.json()['accessToken']
146
147 except Exception as e:
148 raise ValueError(f"Failed to get access token: {e}, refresh your cookies / log in into chat.reka.ai")
Modified g4f/Provider/__init__.py +1 -0
@@ -50,6 +50,7 @@ from .ReplicateImage import ReplicateImage
50 50 from .Vercel import Vercel
51 51 from .WhiteRabbitNeo import WhiteRabbitNeo
52 52 from .You import You
53 from .Reka import Reka
53 54
54 55 import sys
55 56
Modified g4f/models.py +16 -0
@@ -29,6 +29,7 @@ from .Provider import (
29 29 Pi,
30 30 Vercel,
31 31 You,
32 Reka
32 33 )
33 34
34 35
@@ -306,6 +307,12 @@ blackbox = Model(
306 307 best_provider = Blackbox
307 308 )
308 309
310 reka_core = Model(
311 name = 'reka-core',
312 base_provider = 'Reka AI',
313 best_provider = Reka
314 )
315
309 316 class ModelUtils:
310 317 """
311 318 Utility class for mapping string identifiers to Model instances.
@@ -333,8 +340,12 @@ class ModelUtils:
333 340 'llama2-7b' : llama2_7b,
334 341 'llama2-13b': llama2_13b,
335 342 'llama2-70b': llama2_70b,
343
344 'llama3-8b' : llama3_8b_instruct, # alias
345 'llama3-70b': llama3_70b_instruct, # alias
336 346 'llama3-8b-instruct' : llama3_8b_instruct,
337 347 'llama3-70b-instruct': llama3_70b_instruct,
348
338 349 'codellama-34b-instruct': codellama_34b_instruct,
339 350 'codellama-70b-instruct': codellama_70b_instruct,
340 351
@@ -359,6 +370,11 @@ class ModelUtils:
359 370 'claude-3-opus': claude_3_opus,
360 371 'claude-3-sonnet': claude_3_sonnet,
361 372
373 # reka core
374 'reka-core': reka_core,
375 'reka': reka_core,
376 'Reka Core': reka_core,
377
362 378 # other
363 379 'blackbox': blackbox,
364 380 'command-r+': command_r_plus,