Merge pull request #1901 from hlohaus/arka

Update reka vision
This commit is contained in:
H Lohaus 2024-04-27 15:45:57 +02:00 committed by GitHub
commit 5575031f09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 15 deletions

View File

@ -413,9 +413,26 @@ While we wait for gpt-5, here is a list of new models that are at least better t
| Gemini API | `g4f.Provider.GeminiPro` | ❌ | gemini-1.5-pro | [ai.google.dev](https://ai.google.dev) | | Gemini API | `g4f.Provider.GeminiPro` | ❌ | gemini-1.5-pro | [ai.google.dev](https://ai.google.dev) |
| Meta AI | `g4f.Provider.MetaAI` | ✔️ | ❌ | [meta.ai](https://www.meta.ai) | | Meta AI | `g4f.Provider.MetaAI` | ✔️ | ❌ | [meta.ai](https://www.meta.ai) |
| OpenAI ChatGPT | `g4f.Provider.OpenaiChat` | dall-e-3 | gpt-4-vision | [chat.openai.com](https://chat.openai.com) | | OpenAI ChatGPT | `g4f.Provider.OpenaiChat` | dall-e-3 | gpt-4-vision | [chat.openai.com](https://chat.openai.com) |
| Reka | `g4f.Provider.Reka` | ❌ | ✔️ | [chat.reka.ai](https://chat.reka.ai/) |
| Replicate | `g4f.Provider.Replicate` | stability-ai/sdxl| llava-v1.6-34b | [replicate.com](https://replicate.com) | | Replicate | `g4f.Provider.Replicate` | stability-ai/sdxl| llava-v1.6-34b | [replicate.com](https://replicate.com) |
| You.com | `g4f.Provider.You` | dall-e-3| ✔️ | [you.com](https://you.com) | | You.com | `g4f.Provider.You` | dall-e-3| ✔️ | [you.com](https://you.com) |
```python
from g4f.client import Client
from g4f.Provider.GeminiPro import GeminiPro
client = Client(
api_key="...",
provider=GeminiPro
)
response = client.chat.completions.create(
model="gemini-pro-vision",
messages=[{"role": "user", "content": "What are on this image?"}],
image=open("docs/waterfall.jpeg", "rb")
)
print(response.choices[0].message.content)
```
## 🔗 Powered by gpt4free ## 🔗 Powered by gpt4free
<table> <table>

View File

@ -4,11 +4,13 @@ import os, requests, time, json
from ..typing import CreateResult, Messages, ImageType from ..typing import CreateResult, Messages, ImageType
from .base_provider import AbstractProvider from .base_provider import AbstractProvider
from ..cookies import get_cookies from ..cookies import get_cookies
from ..image import to_bytes
class Reka(AbstractProvider): class Reka(AbstractProvider):
url = "https://chat.reka.ai/" url = "https://chat.reka.ai/"
working = True working = True
supports_stream = True supports_stream = True
default_vision_model = "reka"
cookies = {} cookies = {}
@classmethod @classmethod
@ -19,21 +21,19 @@ class Reka(AbstractProvider):
stream: bool, stream: bool,
proxy: str = None, proxy: str = None,
timeout: int = 180, timeout: int = 180,
bearer_auth: str = None, api_key: str = None,
image: ImageType = None, **kwargs) -> CreateResult: image: ImageType = None,
**kwargs
) -> CreateResult:
cls.proxy = proxy cls.proxy = proxy
if not bearer_auth: if not api_key:
cls.cookies = get_cookies("chat.reka.ai") cls.cookies = get_cookies("chat.reka.ai")
if not cls.cookies: if not cls.cookies:
raise ValueError("No cookies found for chat.reka.ai") raise ValueError("No cookies found for chat.reka.ai")
elif "appSession" not in cls.cookies: elif "appSession" not in cls.cookies:
raise ValueError("No appSession found in cookies for chat.reka.ai, log in or provide bearer_auth") raise ValueError("No appSession found in cookies for chat.reka.ai, log in or provide bearer_auth")
api_key = cls.get_access_token(cls)
bearer_auth = cls.get_access_token(cls)
conversation = [] conversation = []
for message in messages: for message in messages:
@ -43,14 +43,14 @@ class Reka(AbstractProvider):
}) })
if image: if image:
image_url = cls.upload_image(cls, bearer_auth, image) image_url = cls.upload_image(cls, api_key, image)
conversation[-1]["image_url"] = image_url conversation[-1]["image_url"] = image_url
conversation[-1]["media_type"] = "image" conversation[-1]["media_type"] = "image"
headers = { headers = {
'accept': '*/*', 'accept': '*/*',
'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', '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',
'authorization': f'Bearer {bearer_auth}', 'authorization': f'Bearer {api_key}',
'cache-control': 'no-cache', 'cache-control': 'no-cache',
'content-type': 'application/json', 'content-type': 'application/json',
'origin': 'https://chat.reka.ai', 'origin': 'https://chat.reka.ai',
@ -109,7 +109,7 @@ class Reka(AbstractProvider):
'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', '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',
} }
image_data = image.read() image_data = to_bytes(image)
boundary = f'----WebKitFormBoundary{boundary_token}' boundary = f'----WebKitFormBoundary{boundary_token}'
data = f'--{boundary}\r\nContent-Disposition: form-data; name="image"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n' data = f'--{boundary}\r\nContent-Disposition: form-data; name="image"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n'
@ -117,7 +117,7 @@ class Reka(AbstractProvider):
data += f'\r\n--{boundary}--\r\n' data += f'\r\n--{boundary}--\r\n'
response = requests.post('https://chat.reka.ai/api/upload-image', response = requests.post('https://chat.reka.ai/api/upload-image',
cookies=Reka.cookies, headers=headers, proxies=cls.proxy, data=data.encode('latin-1')) cookies=cls.cookies, headers=headers, proxies=cls.proxy, data=data.encode('latin-1'))
return response.json()['media_url'] return response.json()['media_url']
@ -140,7 +140,7 @@ class Reka(AbstractProvider):
try: try:
response = requests.get('https://chat.reka.ai/bff/auth/access_token', response = requests.get('https://chat.reka.ai/bff/auth/access_token',
cookies=cls.cookies, headers=headers) cookies=cls.cookies, headers=headers, proxies=cls.proxy)
return response.json()['accessToken'] return response.json()['accessToken']

View File

@ -31,7 +31,8 @@ DOMAINS = [
".meta.ai", ".meta.ai",
".google.com", ".google.com",
"www.whiterabbitneo.com", "www.whiterabbitneo.com",
"huggingface.co" "huggingface.co",
"chat.reka.ai",
] ]
if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null": if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":