diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-04-17 10:43:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-17 10:43:11 +0200 |
commit | 6afc6722a05f6d4498e6dcf19a7689ad58ffd444 (patch) | |
tree | 7b8ab362992408b78838df82a5172be15272a485 /g4f/Provider/DuckDuckGo.py | |
parent | Merge pull request #1833 from hlohaus/curl (diff) | |
parent | Fix style.css (diff) | |
download | gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar.gz gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar.bz2 gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar.lz gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar.xz gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.tar.zst gpt4free-6afc6722a05f6d4498e6dcf19a7689ad58ffd444.zip |
Diffstat (limited to 'g4f/Provider/DuckDuckGo.py')
-rw-r--r-- | g4f/Provider/DuckDuckGo.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/g4f/Provider/DuckDuckGo.py b/g4f/Provider/DuckDuckGo.py index 5269ced3..2fa0612a 100644 --- a/g4f/Provider/DuckDuckGo.py +++ b/g4f/Provider/DuckDuckGo.py @@ -4,8 +4,10 @@ import json import aiohttp from .base_provider import AsyncGeneratorProvider, ProviderModelMixin +from .helper import get_connector from ..typing import AsyncResult, Messages from ..requests.raise_for_status import raise_for_status +from ..providers.conversation import BaseConversation class DuckDuckGo(AsyncGeneratorProvider, ProviderModelMixin): url = "https://duckduckgo.com/duckchat" @@ -42,23 +44,39 @@ class DuckDuckGo(AsyncGeneratorProvider, ProviderModelMixin): cls, model: str, messages: Messages, + proxy: str = None, + connector: aiohttp.BaseConnector = None, + conversation: Conversation = None, + return_conversation: bool = False, **kwargs ) -> AsyncResult: - async with aiohttp.ClientSession(headers=cls.headers) as session: - async with session.get(cls.status_url, headers={"x-vqd-accept": "1"}) as response: - await raise_for_status(response) - vqd_4 = response.headers.get("x-vqd-4") + async with aiohttp.ClientSession(headers=cls.headers, connector=get_connector(connector, proxy)) as session: + if conversation is not None and len(messages) > 1: + vqd_4 = conversation.vqd_4 + messages = [*conversation.messages, messages[-2], messages[-1]] + else: + async with session.get(cls.status_url, headers={"x-vqd-accept": "1"}) as response: + await raise_for_status(response) + vqd_4 = response.headers.get("x-vqd-4") + messages = [messages[-1]] payload = { 'model': cls.get_model(model), 'messages': messages } async with session.post(cls.chat_url, json=payload, headers={"x-vqd-4": vqd_4}) as response: await raise_for_status(response) + if return_conversation: + yield Conversation(response.headers.get("x-vqd-4"), messages) async for line in response.content: if line.startswith(b"data: "): chunk = line[6:] if chunk.startswith(b"[DONE]"): break data = json.loads(chunk) - if "message" in data: - yield data["message"]
\ No newline at end of file + if "message" in data and data["message"]: + yield data["message"] + +class Conversation(BaseConversation): + def __init__(self, vqd_4: str, messages: Messages) -> None: + self.vqd_4 = vqd_4 + self.messages = messages
\ No newline at end of file |