From 8fa4fb389b5ffc9ee9f820ba3ddc5656e2b212f8 Mon Sep 17 00:00:00 2001 From: cycneuramus <56681631+cycneuramus@users.noreply.github.com> Date: Sat, 20 May 2023 11:18:12 +0200 Subject: [PATCH 1/5] ci: build on release --- .github/workflows/build.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b371b81..b004ed7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,6 @@ on: - push: - tags: - - "v*.*.*" + release: + types: [published] jobs: build: From ca1f028409f71c30669f7d60932a0a7d64af8adb Mon Sep 17 00:00:00 2001 From: cycneuramus <56681631+cycneuramus@users.noreply.github.com> Date: Mon, 22 May 2023 11:08:10 +0200 Subject: [PATCH 2/5] feat: use Bing Chat without authentication --- .env | 2 +- .github/README.md | 2 ++ ai.py | 17 ++++++++++------- config/bing.json | 1 - 4 files changed, 13 insertions(+), 9 deletions(-) delete mode 100644 config/bing.json diff --git a/.env b/.env index 9183f5d..f4a7056 100644 --- a/.env +++ b/.env @@ -20,4 +20,4 @@ MODEL=/models/ DEFAULT_MODEL= # Space-separated list of disabled models -DISABLED_MODELS=bard bing hugchat # these require cookie authentication (see readme) +DISABLED_MODELS=bard hugchat # these require cookie authentication (see readme) diff --git a/.github/README.md b/.github/README.md index d9c39a1..a614ced 100644 --- a/.github/README.md +++ b/.github/README.md @@ -70,6 +70,8 @@ Go to https://bard.google.com/ ### 3. Bing Chat +__SETUP NO LONGER REQUIRED. THIS STEP CAN BE SKIPPED.__ + See the [EdgeGPT](https://github.com/acheong08/EdgeGPT) repository. TL;DR:
diff --git a/ai.py b/ai.py index 717b06f..085efe0 100644 --- a/ai.py +++ b/ai.py @@ -34,7 +34,6 @@ def __init__(self, model): def get_api(self): if self.model == "bing": return BingAPI( - cookie_path="./config/bing.json", conversation_style=ConversationStyle.balanced, ) @@ -67,9 +66,9 @@ async def send(self, text): class BingAPI: - def __init__(self, cookie_path, conversation_style): + def __init__(self, conversation_style): self.conversation_style = conversation_style - self.chat = Bing(cookie_path=cookie_path) + self.chat = Bing() def _cleanup_footnote_marks(self, response): return re.sub(r"\[\^(\d+)\^\]", r"[\1]", response) @@ -78,17 +77,21 @@ def _parse_sources(self, sources_raw): name = "providerDisplayName" url = "seeMoreUrl" + i = 1 sources = "" - for i, source in enumerate(sources_raw, start=1): + for source in sources_raw: if name in source.keys() and url in source.keys(): sources += f"[{i}]: {source[name]}: {source[url]}\n" + i += 1 else: continue return sources async def send(self, text): - data = await self.chat.ask(prompt=text) + data = await self.chat.ask( + prompt=text, conversation_style=self.conversation_style + ) sources_raw = data["item"]["messages"][1]["sourceAttributions"] if sources_raw: sources = self._parse_sources(sources_raw) @@ -119,8 +122,8 @@ def __init__( self.model = model self.history = ChatHistory(max_history) self.max_tokens = max_tokens - self.api_key = api_key - self.api_base = api_base + self.api_key = api_key + self.api_base = api_base async def send(self, text): openai.api_key = self.api_key diff --git a/config/bing.json b/config/bing.json deleted file mode 100644 index c940ecd..0000000 --- a/config/bing.json +++ /dev/null @@ -1 +0,0 @@ -BING COOKIES GOES HERE From 230b6e04557af8048c317fdb9debf638b71eedc5 Mon Sep 17 00:00:00 2001 From: cycneuramus <56681631+cycneuramus@users.noreply.github.com> Date: Fri, 26 May 2023 17:46:05 +0200 Subject: [PATCH 3/5] fix: restore optional Bing cookie auth --- ai.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ai.py b/ai.py index 085efe0..950a994 100644 --- a/ai.py +++ b/ai.py @@ -1,3 +1,4 @@ +import json import os import re from collections import deque @@ -33,8 +34,11 @@ def __init__(self, model): def get_api(self): if self.model == "bing": + cookie_path = "./config/bing.json" + if not os.path.exists(cookie_path): + cookie_path = None return BingAPI( - conversation_style=ConversationStyle.balanced, + conversation_style=ConversationStyle.creative, cookie_path=cookie_path ) if self.model == "bard": @@ -49,7 +53,8 @@ def get_api(self): return OpenAIAPI(api_key=openai_api_key, api_base=openai_api_base) if self.model == "hugchat": - return HugchatAPI(cookie_path="./config/hugchat.json") + cookie_path = "./config/hugchat.json" + return HugchatAPI(cookie_path=cookie_path) if self.model == "llama": llama_api_key = "this_can_be_anything" @@ -66,9 +71,17 @@ async def send(self, text): class BingAPI: - def __init__(self, conversation_style): + def __init__(self, conversation_style, cookie_path): self.conversation_style = conversation_style - self.chat = Bing() + self.cookie_path = cookie_path + self.chat = self._create_chat + + async def _create_chat(self, cookie_path): + if cookie_path is None: + return await Bing.create() + else: + cookies = json.loads(open(cookie_path, encoding="utf-8").read()) + return await Bing.create(cookies=cookies) def _cleanup_footnote_marks(self, response): return re.sub(r"\[\^(\d+)\^\]", r"[\1]", response) @@ -77,8 +90,7 @@ def _parse_sources(self, sources_raw): name = "providerDisplayName" url = "seeMoreUrl" - i = 1 - sources = "" + i, sources = 1, "" for source in sources_raw: if name in source.keys() and url in source.keys(): sources += f"[{i}]: {source[name]}: {source[url]}\n" From b23b324cd6b2df85089cf520ad0cbb0dabd8bc07 Mon Sep 17 00:00:00 2001 From: cycneuramus <56681631+cycneuramus@users.noreply.github.com> Date: Sat, 27 May 2023 11:53:38 +0200 Subject: [PATCH 4/5] docs: optional auth for Bing Chat --- .github/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/README.md b/.github/README.md index a614ced..06ca1cb 100644 --- a/.github/README.md +++ b/.github/README.md @@ -70,7 +70,7 @@ Go to https://bard.google.com/ ### 3. Bing Chat -__SETUP NO LONGER REQUIRED. THIS STEP CAN BE SKIPPED.__ +_The setup steps below may not be required but is still supported in case unauthenticated access to Bing Chat does not work in your region._ See the [EdgeGPT](https://github.com/acheong08/EdgeGPT) repository. TL;DR: From 156f686cec274f46e8270c250ca3f7b6bb723736 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 7 Jul 2023 15:15:48 +0000 Subject: [PATCH 5/5] feat: allow specifying GPT model --- .env | 1 + ai.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.env b/.env index f4a7056..67bf973 100644 --- a/.env +++ b/.env @@ -5,6 +5,7 @@ SIGNAL_PHONE_NUMBER= # ChatGPT OPENAI_API_BASE= OPENAI_API_KEY= +OPENAI_MODEL=gpt-3.5-turbo # Bard BARD_TOKEN= diff --git a/ai.py b/ai.py index 950a994..071431b 100644 --- a/ai.py +++ b/ai.py @@ -50,7 +50,10 @@ def get_api(self): openai_api_base = ( os.getenv("OPENAI_API_BASE") or "https://api.openai.com/v1" ) - return OpenAIAPI(api_key=openai_api_key, api_base=openai_api_base) + openai_model = os.getenv("OPENAI_MODEL") or "gpt-3.5-turbo" + return OpenAIAPI( + api_key=openai_api_key, api_base=openai_api_base, model=openai_model + ) if self.model == "hugchat": cookie_path = "./config/hugchat.json"