mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-10-05 16:26:57 +08:00
Fix OpenaiChat provider, Fix issue with curl_cffi
This commit is contained in:
15
README.md
15
README.md
@@ -358,6 +358,21 @@ response = g4f.ChatCompletion.create(
|
|||||||
|
|
||||||
# Displaying the response
|
# Displaying the response
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
from g4f.image import ImageResponse
|
||||||
|
|
||||||
|
# Get image links from response
|
||||||
|
for chunk in g4f.ChatCompletion.create(
|
||||||
|
model=g4f.models.default, # Using the default model
|
||||||
|
provider=g4f.Provider.OpenaiChat, # Specifying the provider as OpenaiChat
|
||||||
|
messages=[{"role": "user", "content": "Create images with dogs"}],
|
||||||
|
access_token="...", # Need a access token from a plus user
|
||||||
|
stream=True,
|
||||||
|
ignore_stream=True
|
||||||
|
):
|
||||||
|
if isinstance(chunk, ImageResponse):
|
||||||
|
print(chunk.images) # Print generated image links
|
||||||
|
print(chunk.alt) # Print used prompt for image generation
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Using Browser
|
##### Using Browser
|
||||||
|
@@ -342,26 +342,30 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
raise MissingAuthError(f'Missing "access_token"')
|
raise MissingAuthError(f'Missing "access_token"')
|
||||||
cls._cookies = cookies
|
cls._cookies = cookies
|
||||||
|
|
||||||
headers = {"Authorization": f"Bearer {access_token}"}
|
auth_headers = {"Authorization": f"Bearer {access_token}"}
|
||||||
async with StreamSession(
|
async with StreamSession(
|
||||||
proxies={"https": proxy},
|
proxies={"https": proxy},
|
||||||
impersonate="chrome110",
|
impersonate="chrome110",
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
cookies=dict([(name, value) for name, value in cookies.items() if name == "_puid"])
|
headers={"Cookie": "; ".join(f"{k}={v}" for k, v in cookies.items())}
|
||||||
) as session:
|
) as session:
|
||||||
try:
|
try:
|
||||||
image_response = None
|
image_response = None
|
||||||
if image:
|
if image:
|
||||||
image_response = await cls.upload_image(session, headers, image, kwargs.get("image_name"))
|
image_response = await cls.upload_image(session, auth_headers, image, kwargs.get("image_name"))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
yield e
|
yield e
|
||||||
end_turn = EndTurn()
|
end_turn = EndTurn()
|
||||||
model = cls.get_model(model or await cls.get_default_model(session, headers))
|
model = cls.get_model(model or await cls.get_default_model(session, auth_headers))
|
||||||
model = "text-davinci-002-render-sha" if model == "gpt-3.5-turbo" else model
|
model = "text-davinci-002-render-sha" if model == "gpt-3.5-turbo" else model
|
||||||
while not end_turn.is_end:
|
while not end_turn.is_end:
|
||||||
|
arkose_token = await cls.get_arkose_token(session)
|
||||||
data = {
|
data = {
|
||||||
"action": action,
|
"action": action,
|
||||||
"arkose_token": await cls.get_arkose_token(session),
|
"arkose_token": arkose_token,
|
||||||
|
"conversation_mode": {"kind": "primary_assistant"},
|
||||||
|
"force_paragen": False,
|
||||||
|
"force_rate_limit": False,
|
||||||
"conversation_id": conversation_id,
|
"conversation_id": conversation_id,
|
||||||
"parent_message_id": parent_id,
|
"parent_message_id": parent_id,
|
||||||
"model": model,
|
"model": model,
|
||||||
@@ -373,7 +377,11 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
async with session.post(
|
async with session.post(
|
||||||
f"{cls.url}/backend-api/conversation",
|
f"{cls.url}/backend-api/conversation",
|
||||||
json=data,
|
json=data,
|
||||||
headers={"Accept": "text/event-stream", **headers}
|
headers={
|
||||||
|
"Accept": "text/event-stream",
|
||||||
|
"OpenAI-Sentinel-Arkose-Token": arkose_token,
|
||||||
|
**auth_headers
|
||||||
|
}
|
||||||
) as response:
|
) as response:
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
raise RuntimeError(f"Response {response.status_code}: {await response.text()}")
|
raise RuntimeError(f"Response {response.status_code}: {await response.text()}")
|
||||||
@@ -439,7 +447,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
Returns:
|
Returns:
|
||||||
tuple[str, dict]: A tuple containing the access token and cookies.
|
tuple[str, dict]: A tuple containing the access token and cookies.
|
||||||
"""
|
"""
|
||||||
with get_browser(proxy=proxy) as driver:
|
driver = get_browser(proxy=proxy)
|
||||||
|
try:
|
||||||
driver.get(f"{cls.url}/")
|
driver.get(f"{cls.url}/")
|
||||||
WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, "prompt-textarea")))
|
WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, "prompt-textarea")))
|
||||||
access_token = driver.execute_script(
|
access_token = driver.execute_script(
|
||||||
@@ -451,6 +460,8 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
"return accessToken;"
|
"return accessToken;"
|
||||||
)
|
)
|
||||||
return access_token, get_driver_cookies(driver)
|
return access_token, get_driver_cookies(driver)
|
||||||
|
finally:
|
||||||
|
driver.close()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get_arkose_token(cls, session: StreamSession) -> str:
|
async def get_arkose_token(cls, session: StreamSession) -> str:
|
||||||
|
Reference in New Issue
Block a user