From a16a96bfb85a0c9e372abe40a6a9bf842d4e419b Mon Sep 17 00:00:00 2001 From: Zack Date: Mon, 23 Oct 2023 11:37:24 -0500 Subject: [PATCH] feat: Add disord_bing example --- .env.example | 1 + .gitignore | 4 +++- apps/discord.py | 11 +++++------ playground/agents/bingchat.py | 32 ++++++++++++++++++++++++++++++ playground/apps/bing_discord.py | 14 +++++++++++++ playground/models/bingchat.py | 35 +++++++++++---------------------- swarms/models/bing_chat.py | 10 ++++++---- 7 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 playground/agents/bingchat.py create mode 100644 playground/apps/bing_discord.py diff --git a/.env.example b/.env.example index c0023751..8c73ae02 100644 --- a/.env.example +++ b/.env.example @@ -35,6 +35,7 @@ REDIS_PORT= #dbs PINECONE_API_KEY="" BING_COOKIE="" +BING_AUTH="" # RevGpt Configuration ACCESS_TOKEN="your_access_token_here" diff --git a/.gitignore b/.gitignore index 09ebd159..92dd6c81 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,8 @@ share/python-wheels/ .installed.cfg *.egg MANIFEST +output/* +cookes.json # PyInstaller # Usually these files are written by a python script from a template @@ -179,4 +181,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ diff --git a/apps/discord.py b/apps/discord.py index a03d0835..aa07f4e5 100644 --- a/apps/discord.py +++ b/apps/discord.py @@ -86,14 +86,13 @@ class Bot: # image_generator.py @self.bot.command() - async def generate_image(ctx, *, prompt: str): + async def generate_image(ctx, *, prompt: str = None, imggen: str = None): """generates images based on the provided prompt""" await ctx.send(f"generating images for prompt: `{prompt}`...") loop = asyncio.get_event_loop() # initialize a future object for the dalle instance - model_instance = dalle3() - future = loop.run_in_executor(Executor, model_instance.run, prompt) + future = loop.run_in_executor(Executor, imggen, prompt) try: # wait for the dalle request to complete, with a timeout of 60 seconds @@ -111,8 +110,8 @@ class Bot: print(f"sending {len(latest_files)} images to discord...") # send all the latest images in a single message - storage_service = os.environ("STORAGE_SERVICE") # "https://storage.googleapis.com/your-bucket-name/ - await ctx.send(files=[storage_service.upload(filepath) for filepath in latest_files]) + # storage_service = os.environ("STORAGE_SERVICE") # "https://storage.googleapis.com/your-bucket-name/ + # await ctx.send(files=[storage_service.upload(filepath) for filepath in latest_files]) except asyncio.timeouterror: await ctx.send("the request took too long! it might have been censored or you're out of boosts. please try entering the prompt again.") @@ -125,7 +124,7 @@ class Bot: if use_agent: response = self.agent.run(text) else: - response = self.llm.run(text) + response = self.llm.__call__(text) await ctx.send(response) def add_command(self, name, func): diff --git a/playground/agents/bingchat.py b/playground/agents/bingchat.py new file mode 100644 index 00000000..bf06ecc6 --- /dev/null +++ b/playground/agents/bingchat.py @@ -0,0 +1,32 @@ +from swarms.models.bing_chat import BingChat +from swarms.workers.worker import Worker +from swarms.tools.autogpt import EdgeGPTTool, tool +from swarms.models import OpenAIChat +import os + +api_key = os.getenv("OPENAI_API_KEY") + +# Initialize the EdgeGPTModel +edgegpt = BingChat(cookies_path="./cookies.txt") + + +@tool +def edgegpt(task: str = None): + """A tool to run infrence on the EdgeGPT Model""" + return EdgeGPTTool.run(task) + + +# Initialize the language model, +# This model can be swapped out with Anthropic, ETC, Huggingface Models like Mistral, ETC +llm = OpenAIChat( + openai_api_key=api_key, + temperature=0.5, +) + +# Initialize the Worker with the custom tool +worker = Worker(llm=llm, ai_name="EdgeGPT Worker", external_tools=[edgegpt]) + +# Use the worker to process a task +task = "Hello, my name is ChatGPT" +response = worker.run(task) +print(response) diff --git a/playground/apps/bing_discord.py b/playground/apps/bing_discord.py new file mode 100644 index 00000000..fc179d1c --- /dev/null +++ b/playground/apps/bing_discord.py @@ -0,0 +1,14 @@ +import os +from swarms.models.bing_chat import BingChat +from apps.discord import Bot +from dotenv import load_dotenv + + +# Initialize the EdgeGPTModel +cookie = os.environ.get("BING_COOKIE") +auth = os.environ.get("AUTH_COOKIE") +bing = BingChat(cookies_path="./cookies.txt", bing_cookie=cookie, auth_cookie=auth) + +bot = Bot(llm=bing, cookie=cookie, auth=auth) +bot.generate_image(imggen=bing.create_img()) +bot.send_text(use_agent=False) diff --git a/playground/models/bingchat.py b/playground/models/bingchat.py index bf06ecc6..6e44cbb5 100644 --- a/playground/models/bingchat.py +++ b/playground/models/bingchat.py @@ -1,32 +1,19 @@ -from swarms.models.bing_chat import BingChat -from swarms.workers.worker import Worker -from swarms.tools.autogpt import EdgeGPTTool, tool -from swarms.models import OpenAIChat import os +from swarms.models.bing_chat import BingChat +from dotenv import load_dotenv -api_key = os.getenv("OPENAI_API_KEY") +load_dotenv() # Initialize the EdgeGPTModel -edgegpt = BingChat(cookies_path="./cookies.txt") - - -@tool -def edgegpt(task: str = None): - """A tool to run infrence on the EdgeGPT Model""" - return EdgeGPTTool.run(task) +edgegpt = BingChat(cookies_path="./cookies.json") +cookie = os.environ.get("BING_COOKIE") +auth = os.environ.get("AUTH_COOKIE") +# Use the worker to process a task +task = "hi" +# img_task = "Sunset over mountains" -# Initialize the language model, -# This model can be swapped out with Anthropic, ETC, Huggingface Models like Mistral, ETC -llm = OpenAIChat( - openai_api_key=api_key, - temperature=0.5, -) - -# Initialize the Worker with the custom tool -worker = Worker(llm=llm, ai_name="EdgeGPT Worker", external_tools=[edgegpt]) +response = edgegpt(task) +# response = edgegpt.create_img(auth_cookie=cookie,auth_cookie_SRCHHPGUSR=auth,prompt=img_task) -# Use the worker to process a task -task = "Hello, my name is ChatGPT" -response = worker.run(task) print(response) diff --git a/swarms/models/bing_chat.py b/swarms/models/bing_chat.py index 1d2eb503..cb90f97e 100644 --- a/swarms/models/bing_chat.py +++ b/swarms/models/bing_chat.py @@ -1,4 +1,4 @@ -"""EdgeGPT model by OpenAI""" +"""Bing-Chat model by Micorsoft""" import asyncio import json from pathlib import Path @@ -25,9 +25,11 @@ class BingChat: """ - def __init__(self, cookies_path: str): + def __init__(self, cookies_path: str, bing_cookie: str = None, auth_cookie: str = None): self.cookies = json.loads(open(cookies_path, encoding="utf-8").read()) self.bot = asyncio.run(Chatbot.create(cookies=self.cookies)) + self.auth_cookie = auth_cookie + self.auth_cookie_SRCHHPGUSR = bing_cookie def __call__( self, prompt: str, style: ConversationStyle = ConversationStyle.creative @@ -43,7 +45,7 @@ class BingChat: return response["text"] def create_img( - self, prompt: str, output_dir: str = "./output", auth_cookie: str = None + self, prompt: str, output_dir: str = "./output", auth_cookie: str = None, auth_cookie_SRCHHPGUSR: str = None ) -> str: """ Generate an image based on the provided prompt and save it in the given output directory. @@ -52,7 +54,7 @@ class BingChat: if not auth_cookie: raise ValueError("Auth cookie is required for image generation.") - image_generator = ImageGen(auth_cookie, quiet=True) + image_generator = ImageGen(auth_cookie, auth_cookie_SRCHHPGUSR, quiet=True, ) images = image_generator.get_images(prompt) image_generator.save_images(images, output_dir=output_dir)