Skip to content

Commit

Permalink
Merge pull request #1901 from hlohaus/arka
Browse files Browse the repository at this point in the history
Update reka vision
  • Loading branch information
hlohaus authored Apr 27, 2024
2 parents 41c9c9e + 0cc5b07 commit 5575031
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| 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) |
| 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) |
| 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

<table>
Expand Down
28 changes: 14 additions & 14 deletions g4f/Provider/Reka.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from ..typing import CreateResult, Messages, ImageType
from .base_provider import AbstractProvider
from ..cookies import get_cookies
from ..image import to_bytes

class Reka(AbstractProvider):
url = "https://chat.reka.ai/"
working = True
supports_stream = True
default_vision_model = "reka"
cookies = {}

@classmethod
Expand All @@ -19,21 +21,19 @@ def create_completion(
stream: bool,
proxy: str = None,
timeout: int = 180,
bearer_auth: str = None,
image: ImageType = None, **kwargs) -> CreateResult:

api_key: str = None,
image: ImageType = None,
**kwargs
) -> CreateResult:
cls.proxy = proxy

if not bearer_auth:
cls.cookies = get_cookies("chat.reka.ai")

if not api_key:
cls.cookies = get_cookies("chat.reka.ai")
if not cls.cookies:
raise ValueError("No cookies found for chat.reka.ai")

elif "appSession" not in cls.cookies:
raise ValueError("No appSession found in cookies for chat.reka.ai, log in or provide bearer_auth")

bearer_auth = cls.get_access_token(cls)
api_key = cls.get_access_token(cls)

conversation = []
for message in messages:
Expand All @@ -43,14 +43,14 @@ def create_completion(
})

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]["media_type"] = "image"

headers = {
'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',
'authorization': f'Bearer {bearer_auth}',
'authorization': f'Bearer {api_key}',
'cache-control': 'no-cache',
'content-type': 'application/json',
'origin': 'https://chat.reka.ai',
Expand Down Expand Up @@ -109,15 +109,15 @@ def upload_image(cls, access_token, image: ImageType) -> str:
'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}'
data = f'--{boundary}\r\nContent-Disposition: form-data; name="image"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n'
data += image_data.decode('latin-1')
data += f'\r\n--{boundary}--\r\n'

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']

Expand All @@ -140,7 +140,7 @@ def get_access_token(cls):

try:
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']

Expand Down
3 changes: 2 additions & 1 deletion g4f/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
".meta.ai",
".google.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":
Expand Down

0 comments on commit 5575031

Please sign in to comment.