Formatting & Warning Removal

Formatted all Models

Added optional line to remove warning in AUTOgpt
This commit is contained in:
Jaydin_MacBook
2023-05-11 10:43:27 -04:00
parent 544886dc74
commit ce810d3e53
5 changed files with 337 additions and 199 deletions
+83 -42
View File
@@ -1,7 +1,7 @@
# !pip install bs4
# !pip install nest_asyncio
# General
# General
import os
import json
import pandas as pd
@@ -9,10 +9,10 @@ from dotenv import load_dotenv
from pathlib import Path
from json import JSONDecodeError
from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent
from langchain.docstore.document import Document
import asyncio
@@ -21,69 +21,86 @@ import nest_asyncio
# Needed synce jupyter runs an async eventloop
nest_asyncio.apply()
# [Optional] Set the environment variable Tokenizers_PARALLELISM to false to get rid of the warning
# os.environ["TOKENIZERS_PARALLELISM"] = "false"
load_dotenv()
select_model = input("Select the model you want to use (1, 2, 3 or 4) \n \
select_model = input(
"Select the model you want to use (1, 2, 3 or 4) \n \
1) ChatGPT \n \
2) HuggingChat \n \
3) BingChat \n \
4) Google Bard \n \
>>> ")
>>> "
)
if select_model == "1":
CG_TOKEN = os.getenv("CHATGPT_TOKEN", "your-chatgpt-token")
if (CG_TOKEN != "your-chatgpt-token"):
if CG_TOKEN != "your-chatgpt-token":
os.environ["CHATGPT_TOKEN"] = CG_TOKEN
else:
raise ValueError("ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token")
raise ValueError(
"ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token"
)
start_chat = os.getenv("USE_EXISTING_CHAT", False)
if os.getenv("USE_GPT4") == "True":
model = "gpt4"
else:
model = "default"
if start_chat:
chat_id = os.getenv("CHAT_ID")
if chat_id == None:
raise ValueError("You have to set up your chat-id in the .env file")
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], conversation=chat_id , model=model)
llm = ChatGPTAPI.ChatGPT(
token=os.environ["CHATGPT_TOKEN"], conversation=chat_id, model=model
)
else:
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
llm = ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
elif select_model == "2":
llm=HuggingChatAPI.HuggingChat()
llm = HuggingChatAPI.HuggingChat()
elif select_model == "3":
if not os.path.exists("cookiesBing.json"):
raise ValueError("File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format.")
raise ValueError(
"File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format."
)
cookie_path = Path() / "cookiesBing.json"
with open("cookiesBing.json", 'r') as file:
with open("cookiesBing.json", "r") as file:
try:
file_json = json.loads(file.read())
except JSONDecodeError:
raise ValueError("You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required.")
llm=BingChatAPI.BingChat(cookiepath=str(cookie_path), conversation_style="creative")
raise ValueError(
"You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required."
)
llm = BingChatAPI.BingChat(
cookiepath=str(cookie_path), conversation_style="creative"
)
elif select_model == "4":
GB_TOKEN = os.getenv("BARDCHAT_TOKEN", "your-googlebard-token")
if GB_TOKEN != "your-googlebard-token":
os.environ["BARDCHAT_TOKEN"] = GB_TOKEN
else:
raise ValueError("GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token")
raise ValueError(
"GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token"
)
cookie_path = os.environ["BARDCHAT_TOKEN"]
llm=BardChatAPI.BardChat(cookie=cookie_path)
llm = BardChatAPI.BardChat(cookie=cookie_path)
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your-huggingface-token")
if (HF_TOKEN != "your-huggingface-token"):
if HF_TOKEN != "your-huggingface-token":
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
else:
raise ValueError("HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token")
raise ValueError(
"HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token"
)
# Tools
import os
@@ -95,6 +112,8 @@ from langchain.tools.file_management.write import WriteFileTool
from tempfile import TemporaryDirectory
ROOT_DIR = TemporaryDirectory()
@contextmanager
def pushd(new_dir):
"""Context manager for changing the current working directory."""
@@ -105,6 +124,7 @@ def pushd(new_dir):
finally:
os.chdir(prev_dir)
@tool
def process_csv(
csv_file_path: str, instructions: str, output_path: Optional[str] = None
@@ -126,17 +146,20 @@ def process_csv(
return result
except Exception as e:
return f"Error: {e}"
# !pip install playwright
# !playwright install
async def async_load_playwright(url: str) -> str:
"""Load the specified URLs using Playwright and parse using BeautifulSoup."""
from bs4 import BeautifulSoup
from playwright.async_api import async_playwright
try:
print(">>> WARNING <<<")
print("If you are running this for the first time, you nedd to install playwright")
print(
"If you are running this for the first time, you nedd to install playwright"
)
print(">>> AUTO INSTALLING PLAYWRIGHT <<<")
os.system("playwright install")
print(">>> PLAYWRIGHT INSTALLED <<<")
@@ -165,10 +188,12 @@ async def async_load_playwright(url: str) -> str:
await browser.close()
return results
def run_async(coro):
event_loop = asyncio.get_event_loop()
return event_loop.run_until_complete(coro)
@tool
def browse_web_page(url: str) -> str:
"""Verbose way to scrape a whole webpage. Likely to cause issues parsing."""
@@ -179,23 +204,31 @@ from langchain.tools import BaseTool, DuckDuckGoSearchRun
from langchain.text_splitter import RecursiveCharacterTextSplitter
from pydantic import Field
from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain
from langchain.chains.qa_with_sources.loading import (
load_qa_with_sources_chain,
BaseCombineDocumentsChain,
)
def _get_text_splitter():
return RecursiveCharacterTextSplitter(
# Set a really small chunk size, just to show.
chunk_size = 500,
chunk_overlap = 20,
length_function = len,
chunk_size=500,
chunk_overlap=20,
length_function=len,
)
class WebpageQATool(BaseTool):
name = "query_webpage"
description = "Browse a webpage and retrieve the information relevant to the question."
text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=_get_text_splitter)
description = (
"Browse a webpage and retrieve the information relevant to the question."
)
text_splitter: RecursiveCharacterTextSplitter = Field(
default_factory=_get_text_splitter
)
qa_chain: BaseCombineDocumentsChain
def _run(self, url: str, question: str) -> str:
"""Useful for browsing websites and scraping the text information."""
result = browse_web_page.run(url)
@@ -204,15 +237,23 @@ class WebpageQATool(BaseTool):
results = []
# TODO: Handle this with a MapReduceChain
for i in range(0, len(web_docs), 4):
input_docs = web_docs[i:i+4]
window_result = self.qa_chain({"input_documents": input_docs, "question": question}, return_only_outputs=True)
input_docs = web_docs[i : i + 4]
window_result = self.qa_chain(
{"input_documents": input_docs, "question": question},
return_only_outputs=True,
)
results.append(f"Response from window {i} - {window_result}")
results_docs = [Document(page_content="\n".join(results), metadata={"source": url})]
return self.qa_chain({"input_documents": results_docs, "question": question}, return_only_outputs=True)
results_docs = [
Document(page_content="\n".join(results), metadata={"source": url})
]
return self.qa_chain(
{"input_documents": results_docs, "question": question},
return_only_outputs=True,
)
async def _arun(self, url: str, question: str) -> str:
raise NotImplementedError
query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm))
@@ -221,13 +262,13 @@ query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm))
import faiss
from langchain.vectorstores import FAISS
from langchain.docstore import InMemoryDocstore
from Embedding import HuggingFaceEmbedding # EMBEDDING FUNCTION
from Embedding import HuggingFaceEmbedding # EMBEDDING FUNCTION
from langchain.tools.human.tool import HumanInputRun
# Define your embedding model
embeddings_model = HuggingFaceEmbedding.newEmbeddingFunction
embedding_size = 1536 # if you change this you need to change also in Embedding/HuggingFaceEmbedding.py
embedding_size = 1536 # if you change this you need to change also in Embedding/HuggingFaceEmbedding.py
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model, index, InMemoryDocstore({}), {})
+52 -31
View File
@@ -10,10 +10,10 @@ from langchain import HuggingFaceHub
from langchain.docstore import InMemoryDocstore
from langchain import LLMChain, PromptTemplate
from langchain.llms import BaseLLM
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from langchain.vectorstores.base import VectorStore
from pydantic import BaseModel, Field
from langchain.chains.base import Chain
@@ -24,71 +24,85 @@ import faiss
load_dotenv()
select_model = input("Select the model you want to use (1, 2, 3 or 4) \n \
select_model = input(
"Select the model you want to use (1, 2, 3 or 4) \n \
1) ChatGPT \n \
2) HuggingChat \n \
3) BingChat (NOT GOOD RESULT)\n \
4) BardChat \n \
>>> ")
>>> "
)
if select_model == "1":
CG_TOKEN = os.getenv("CHATGPT_TOKEN", "your-chatgpt-token")
if (CG_TOKEN != "your-chatgpt-token"):
if CG_TOKEN != "your-chatgpt-token":
os.environ["CHATGPT_TOKEN"] = CG_TOKEN
else:
raise ValueError("ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token")
raise ValueError(
"ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token"
)
start_chat = os.getenv("USE_EXISTING_CHAT", False)
if os.getenv("USE_GPT4") == "True":
model = "gpt4"
else:
model = "default"
if start_chat:
chat_id = os.getenv("CHAT_ID")
if chat_id == None:
raise ValueError("You have to set up your chat-id in the .env file")
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], conversation=chat_id , model=model)
llm = ChatGPTAPI.ChatGPT(
token=os.environ["CHATGPT_TOKEN"], conversation=chat_id, model=model
)
else:
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
llm = ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
elif select_model == "2":
llm=HuggingChatAPI.HuggingChat()
llm = HuggingChatAPI.HuggingChat()
elif select_model == "3":
if not os.path.exists("cookiesBing.json"):
raise ValueError("File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format.")
raise ValueError(
"File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format."
)
cookie_path = Path() / "cookiesBing.json"
with open("cookiesBing.json", 'r') as file:
with open("cookiesBing.json", "r") as file:
try:
file_json = json.loads(file.read())
except JSONDecodeError:
raise ValueError("You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required.")
llm=BingChatAPI.BingChat(cookiepath=str(cookie_path), conversation_style="creative")
raise ValueError(
"You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required."
)
llm = BingChatAPI.BingChat(
cookiepath=str(cookie_path), conversation_style="creative"
)
elif select_model == "4":
GB_TOKEN = os.getenv("BARDCHAT_TOKEN", "your-googlebard-token")
if GB_TOKEN != "your-googlebard-token":
os.environ["BARDCHAT_TOKEN"] = GB_TOKEN
else:
raise ValueError("GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token")
raise ValueError(
"GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token"
)
cookie_path = os.environ["BARDCHAT_TOKEN"]
llm=BardChatAPI.BardChat(cookie=cookie_path)
llm = BardChatAPI.BardChat(cookie=cookie_path)
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your-huggingface-token")
if (HF_TOKEN != "your-huggingface-token"):
if HF_TOKEN != "your-huggingface-token":
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
else:
raise ValueError("HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token")
raise ValueError(
"HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token"
)
from Embedding import HuggingFaceEmbedding # EMBEDDING FUNCTION
from Embedding import HuggingFaceEmbedding # EMBEDDING FUNCTION
# Define your embedding model
embeddings_model = HuggingFaceEmbedding.newEmbeddingFunction
@@ -99,7 +113,7 @@ vectorstore = FAISS(embeddings_model, index, InMemoryDocstore({}), {})
print(vectorstore)
#DEFINE TOOL
# DEFINE TOOL
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import OpenAI, LLMChain
from langchain.tools import BaseTool, DuckDuckGoSearchRun
@@ -146,19 +160,26 @@ agent_executor = AgentExecutor.from_agent_and_tools(
# Logging of LLMChains
verbose = False
int_max_iterations = input("Enter the maximum number of iterations: (Suggest from 3 and 5) ")
int_max_iterations = input(
"Enter the maximum number of iterations: (Suggest from 3 and 5) "
)
max_iterations = int(int_max_iterations)
if input("Do you want to store the results? (y/n) ") == "y" :
if input("Do you want to store the results? (y/n) ") == "y":
store_results = True
else:
store_results = False
# If None, will keep on going forever
max_iterations: Optional[int] = max_iterations
max_iterations: Optional[int] = max_iterations
baby_agi = BabyAGIMod.BabyAGI.from_llm(
llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=verbose, max_iterations=max_iterations, store= store_results
llm=llm,
vectorstore=vectorstore,
task_execution_chain=agent_executor,
verbose=verbose,
max_iterations=max_iterations,
store=store_results,
)
+96 -54
View File
@@ -11,9 +11,9 @@ from langchain.schema import (
from langchain.llms.base import LLM
from typing import Optional, List, Mapping, Any
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
import streamlit as st
from streamlit_chat_media import message
import os
@@ -23,19 +23,19 @@ st.set_page_config(
page_icon="🚀",
layout="wide",
menu_items={
'Get help': 'https://www.intelligenzaartificialeitalia.net/',
'Report a bug': "mailto:servizi@intelligenzaartificialeitalia.net",
'About': "# *🚀 FREE AUTOGPT 🚀* "
}
"Get help": "https://www.intelligenzaartificialeitalia.net/",
"Report a bug": "mailto:servizi@intelligenzaartificialeitalia.net",
"About": "# *🚀 FREE AUTOGPT 🚀* ",
},
)
st.markdown("<style> iframe > div { text-align: left;} </style>", unsafe_allow_html=True)
st.markdown(
"<style> iframe > div { text-align: left;} </style>", unsafe_allow_html=True
)
class CAMELAgent:
def __init__(
self,
system_message: SystemMessage,
@@ -51,7 +51,7 @@ class CAMELAgent:
def init_messages(self) -> None:
self.stored_messages = [self.system_message]
def update_messages(self, message: BaseMessage) -> List[BaseMessage]:
self.stored_messages.append(message)
return self.stored_messages
@@ -66,8 +66,6 @@ class CAMELAgent:
print(f"AI Assistant:\n\n{output_message}\n\n")
return output_message
col1, col2 = st.columns(2)
assistant_role_name = col1.text_input("Assistant Role Name", "Python Programmer")
@@ -78,26 +76,35 @@ word_limit = st.number_input("Word Limit", 10, 1500, 50)
if st.button("Start Autonomus AI AGENT"):
task_specifier_sys_msg = SystemMessage(content="You can make a task more specific.")
task_specifier_prompt = (
"""Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.
task_specifier_prompt = """Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.
Please make it more specific. Be creative and imaginative.
Please reply with the specified task in {word_limit} words or less. Do not add anything else."""
task_specifier_template = HumanMessagePromptTemplate.from_template(
template=task_specifier_prompt
)
task_specifier_template = HumanMessagePromptTemplate.from_template(template=task_specifier_prompt)
task_specify_agent = CAMELAgent(task_specifier_sys_msg, HuggingChatAPI.HuggingChat())
task_specifier_msg = task_specifier_template.format_messages(assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task, word_limit=word_limit)[0]
task_specify_agent = CAMELAgent(
task_specifier_sys_msg, HuggingChatAPI.HuggingChat()
)
task_specifier_msg = task_specifier_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
word_limit=word_limit,
)[0]
specified_task_msg = task_specify_agent.step(task_specifier_msg)
print(f"Specified task: {specified_task_msg}")
message(f"Specified task: {specified_task_msg}", allow_html=True, key="specified_task" , avatar_style="adventurer")
message(
f"Specified task: {specified_task_msg}",
allow_html=True,
key="specified_task",
avatar_style="adventurer",
)
specified_task = specified_task_msg
# messages.py
from langchain.prompts.chat import (
SystemMessagePromptTemplate,
@@ -151,39 +158,60 @@ if st.button("Start Autonomus AI AGENT"):
Never say <CAMEL_TASK_DONE> unless my responses have solved your task!
It's Important wich when the task . "{task}" is completed, you must only reply with a single word <CAMEL_TASK_DONE>"""
def get_sys_msgs(assistant_role_name: str, user_role_name: str, task: str):
assistant_sys_template = SystemMessagePromptTemplate.from_template(template=assistant_inception_prompt)
assistant_sys_msg = assistant_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0]
user_sys_template = SystemMessagePromptTemplate.from_template(template=user_inception_prompt)
user_sys_msg = user_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0]
return assistant_sys_msg, user_sys_msg
#define the role system messages
assistant_sys_msg, user_sys_msg = get_sys_msgs(assistant_role_name, user_role_name, specified_task)
assistant_sys_template = SystemMessagePromptTemplate.from_template(
template=assistant_inception_prompt
)
assistant_sys_msg = assistant_sys_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
)[0]
#AI ASSISTANT setup |-> add the agent LLM MODEL HERE <-|
user_sys_template = SystemMessagePromptTemplate.from_template(
template=user_inception_prompt
)
user_sys_msg = user_sys_template.format_messages(
assistant_role_name=assistant_role_name,
user_role_name=user_role_name,
task=task,
)[0]
return assistant_sys_msg, user_sys_msg
# define the role system messages
assistant_sys_msg, user_sys_msg = get_sys_msgs(
assistant_role_name, user_role_name, specified_task
)
# AI ASSISTANT setup |-> add the agent LLM MODEL HERE <-|
assistant_agent = CAMELAgent(assistant_sys_msg, HuggingChatAPI.HuggingChat())
#AI USER setup |-> add the agent LLM MODEL HERE <-|
# AI USER setup |-> add the agent LLM MODEL HERE <-|
user_agent = CAMELAgent(user_sys_msg, HuggingChatAPI.HuggingChat())
# Reset agents
assistant_agent.reset()
user_agent.reset()
# Initialize chats
# Initialize chats
assistant_msg = HumanMessage(
content=(f"{user_sys_msg}. "
"Now start to give me introductions one by one. "
"Only reply with Instruction and Input."))
content=(
f"{user_sys_msg}. "
"Now start to give me introductions one by one. "
"Only reply with Instruction and Input."
)
)
user_msg = HumanMessage(content=f"{assistant_sys_msg.content}")
user_msg = assistant_agent.step(user_msg)
message(f"AI Assistant ({assistant_role_name}):\n\n{user_msg}\n\n", is_user=False, allow_html=True, key="0_assistant" , avatar_style="pixel-art")
message(
f"AI Assistant ({assistant_role_name}):\n\n{user_msg}\n\n",
is_user=False,
allow_html=True,
key="0_assistant",
avatar_style="pixel-art",
)
print(f"Original task prompt:\n{task}\n")
print(f"Specified task prompt:\n{specified_task}\n")
@@ -192,16 +220,30 @@ if st.button("Start Autonomus AI AGENT"):
n += 1
user_ai_msg = user_agent.step(assistant_msg)
user_msg = HumanMessage(content=user_ai_msg)
#print(f"AI User ({user_role_name}):\n\n{user_msg}\n\n")
message(f"AI User ({user_role_name}):\n\n{user_msg.content}\n\n", is_user=True, allow_html=True, key=str(n)+"_user")
# print(f"AI User ({user_role_name}):\n\n{user_msg}\n\n")
message(
f"AI User ({user_role_name}):\n\n{user_msg.content}\n\n",
is_user=True,
allow_html=True,
key=str(n) + "_user",
)
assistant_ai_msg = assistant_agent.step(user_msg)
assistant_msg = HumanMessage(content=assistant_ai_msg)
#print(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg}\n\n")
message(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n", is_user=False, allow_html=True, key=str(n)+"_assistant" , avatar_style="pixel-art")
if "<CAMEL_TASK_DONE>" in user_msg.content or 'task completed' in user_msg.content:
# print(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg}\n\n")
message(
f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n",
is_user=False,
allow_html=True,
key=str(n) + "_assistant",
avatar_style="pixel-art",
)
if (
"<CAMEL_TASK_DONE>" in user_msg.content
or "task completed" in user_msg.content
):
message("Task completed!", allow_html=True, key="task_done")
break
if 'Error' in user_msg.content:
if "Error" in user_msg.content:
message("Task failed!", allow_html=True, key="task_failed")
break
+66 -51
View File
@@ -3,10 +3,10 @@ from dotenv import load_dotenv
from pathlib import Path
from json import JSONDecodeError
from langchain import LLMChain, PromptTemplate
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from FreeLLM import ChatGPTAPI # FREE CHATGPT API
from FreeLLM import HuggingChatAPI # FREE HUGGINGCHAT API
from FreeLLM import BingChatAPI # FREE BINGCHAT API
from FreeLLM import BardChatAPI # FREE GOOGLE BARD API
from langchain.memory import ConversationBufferWindowMemory
import os
@@ -14,59 +14,73 @@ import os
load_dotenv()
#### LOG IN FOR CHATGPT FREE LLM
select_model = input("Select the model you want to use (1, 2, 3 or 4) \n \
select_model = input(
"Select the model you want to use (1, 2, 3 or 4) \n \
1) ChatGPT \n \
2) HuggingChat \n \
3) BingChat \n \
4) Google Bard \n \
>>> ")
>>> "
)
if select_model == "1":
CG_TOKEN = os.getenv("CHATGPT_TOKEN", "your-chatgpt-token")
if (CG_TOKEN != "your-chatgpt-token"):
if CG_TOKEN != "your-chatgpt-token":
os.environ["CHATGPT_TOKEN"] = CG_TOKEN
else:
raise ValueError("ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token")
raise ValueError(
"ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token"
)
start_chat = os.getenv("USE_EXISTING_CHAT", False)
if os.getenv("USE_GPT4") == "True":
model = "gpt4"
else:
model = "default"
if start_chat:
chat_id = os.getenv("CHAT_ID")
if chat_id == None:
raise ValueError("You have to set up your chat-id in the .env file")
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], conversation=chat_id , model=model)
llm = ChatGPTAPI.ChatGPT(
token=os.environ["CHATGPT_TOKEN"], conversation=chat_id, model=model
)
else:
llm= ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
llm = ChatGPTAPI.ChatGPT(token=os.environ["CHATGPT_TOKEN"], model=model)
elif select_model == "2":
llm=HuggingChatAPI.HuggingChat()
llm = HuggingChatAPI.HuggingChat()
elif select_model == "3":
if not os.path.exists("cookiesBing.json"):
raise ValueError("File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format.")
raise ValueError(
"File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format."
)
cookie_path = Path() / "cookiesBing.json"
with open("cookiesBing.json", 'r') as file:
with open("cookiesBing.json", "r") as file:
try:
file_json = json.loads(file.read())
except JSONDecodeError:
raise ValueError("You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required.")
llm=BingChatAPI.BingChat(cookiepath=str(cookie_path), conversation_style="creative")
raise ValueError(
"You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required."
)
llm = BingChatAPI.BingChat(
cookiepath=str(cookie_path), conversation_style="creative"
)
elif select_model == "4":
GB_TOKEN = os.getenv("BARDCHAT_TOKEN", "your-googlebard-token")
if GB_TOKEN != "your-googlebard-token":
os.environ["BARDCHAT_TOKEN"] = GB_TOKEN
else:
raise ValueError("GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token")
raise ValueError(
"GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token"
)
cookie_path = os.environ["BARDCHAT_TOKEN"]
llm=BardChatAPI.BardChat(cookie=cookie_path)
llm = BardChatAPI.BardChat(cookie=cookie_path)
####
@@ -83,20 +97,20 @@ def initialize_chain(instructions, memory=None):
Assistant:"""
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template
input_variables=["history", "human_input"], template=template
)
chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
llm=llm,
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(),
)
return chain
def initialize_meta_chain():
meta_template="""
meta_template = """
Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future.
####
@@ -113,57 +127,58 @@ def initialize_meta_chain():
"""
meta_prompt = PromptTemplate(
input_variables=["chat_history"],
template=meta_template
input_variables=["chat_history"], template=meta_template
)
meta_chain = LLMChain(
llm=llm,
prompt=meta_prompt,
verbose=True,
llm=llm,
prompt=meta_prompt,
verbose=True,
)
return meta_chain
def get_chat_history(chain_memory):
memory_key = chain_memory.memory_key
chat_history = chain_memory.load_memory_variables(memory_key)[memory_key]
return chat_history
def get_new_instructions(meta_output):
delimiter = 'Instructions: '
new_instructions = meta_output[meta_output.find(delimiter)+len(delimiter):]
delimiter = "Instructions: "
new_instructions = meta_output[meta_output.find(delimiter) + len(delimiter) :]
return new_instructions
def main(task, max_iters=3, max_meta_iters=5):
failed_phrase = 'task failed'
success_phrase = 'task succeeded'
failed_phrase = "task failed"
success_phrase = "task succeeded"
key_phrases = [success_phrase, failed_phrase]
instructions = 'None'
instructions = "None"
for i in range(max_meta_iters):
print(f'[Episode {i+1}/{max_meta_iters}]')
print(f"[Episode {i+1}/{max_meta_iters}]")
chain = initialize_chain(instructions, memory=None)
output = chain.predict(human_input=task)
for j in range(max_iters):
print(f'(Step {j+1}/{max_iters})')
print(f'Assistant: {output}')
print(f'Human: ')
print(f"(Step {j+1}/{max_iters})")
print(f"Assistant: {output}")
print(f"Human: ")
human_input = input()
if any(phrase in human_input.lower() for phrase in key_phrases):
break
output = chain.predict(human_input=human_input)
if success_phrase in human_input.lower():
print(f'You succeeded! Thanks for playing!')
print(f"You succeeded! Thanks for playing!")
return
meta_chain = initialize_meta_chain()
meta_output = meta_chain.predict(chat_history=get_chat_history(chain.memory))
print(f'Feedback: {meta_output}')
print(f"Feedback: {meta_output}")
instructions = get_new_instructions(meta_output)
print(f'New Instructions: {instructions}')
print('\n'+'#'*80+'\n')
print(f'You failed! Thanks for playing!')
print(f"New Instructions: {instructions}")
print("\n" + "#" * 80 + "\n")
print(f"You failed! Thanks for playing!")
task = input("Enter the objective of the AI system: (Be realistic!) ")
max_iters = int(input("Enter the maximum number of interactions per episode: "))
+40 -21
View File
@@ -11,86 +11,105 @@ import huggingface_hub
load_dotenv()
select_model = input("Select the model you want to use (1, 2, 3, 4, 5, 6) \n \
select_model = input(
"Select the model you want to use (1, 2, 3, 4, 5, 6) \n \
1) ChatGPT \n \
2) HuggingChat (NOT GOOD RESULT)\n \
3) BingChat (NOT GOOD RESULT)\n \
4) BardChat \n \
5) StarCoder\n \
6) OpenAssistant\n \
>>> ")
>>> "
)
if select_model == "1":
CG_TOKEN = os.getenv("CHATGPT_TOKEN", "your-chatgpt-token")
if (CG_TOKEN != "your-chatgpt-token"):
if CG_TOKEN != "your-chatgpt-token":
os.environ["CHATGPT_TOKEN"] = CG_TOKEN
else:
raise ValueError("ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token")
raise ValueError(
"ChatGPT Token EMPTY. Edit the .env file and put your ChatGPT token"
)
start_chat = os.getenv("USE_EXISTING_CHAT", False)
if os.getenv("USE_GPT4") == "True":
model = "gpt4"
else:
model = "default"
if start_chat:
chat_id = os.getenv("CHAT_ID")
if chat_id == None:
raise ValueError("You have to set up your chat-id in the .env file")
agent = agents.ChatGPTAgent(token=os.environ["CHATGPT_TOKEN"], conversation=chat_id , model=model)
agent = agents.ChatGPTAgent(
token=os.environ["CHATGPT_TOKEN"], conversation=chat_id, model=model
)
else:
agent = agents.ChatGPTAgent(token=os.environ["CHATGPT_TOKEN"], model=model)
elif select_model == "2":
agent = agents.HuggingChatAgent()
elif select_model == "3":
if not os.path.exists("cookiesBing.json"):
raise ValueError("File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format.")
raise ValueError(
"File 'cookiesBing.json' not found! Create it and put your cookies in there in the JSON format."
)
cookie_path = "cookiesBing.json"
with open("cookiesBing.json", 'r') as file:
with open("cookiesBing.json", "r") as file:
try:
file_json = json.loads(file.read())
except JSONDecodeError:
raise ValueError("You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required.")
agent = agents.BingChatAgent(cookiepath=cookie_path , conversation="balanced")
raise ValueError(
"You did not put your cookies inside 'cookiesBing.json'! You can find the simple guide to get the cookie file here: https://github.com/acheong08/EdgeGPT/tree/master#getting-authentication-required."
)
agent = agents.BingChatAgent(cookiepath=cookie_path, conversation="balanced")
elif select_model == "4":
GB_TOKEN = os.getenv("BARDCHAT_TOKEN", "your-googlebard-token")
if GB_TOKEN != "your-googlebard-token":
os.environ["BARDCHAT_TOKEN"] = GB_TOKEN
else:
raise ValueError("GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token")
raise ValueError(
"GoogleBard Token EMPTY. Edit the .env file and put your GoogleBard token"
)
cookie = os.environ["BARDCHAT_TOKEN"]
agent = agents.BardChatAgent(token=cookie)
elif select_model == "5":
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your-huggingface-token")
if (HF_TOKEN != "your-huggingface-token"):
if HF_TOKEN != "your-huggingface-token":
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
huggingface_hub.login(token=HF_TOKEN)
else:
raise ValueError("HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token")
raise ValueError(
"HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token"
)
from transformers.tools import HfAgent
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
elif select_model == "6":
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "your-huggingface-token")
if (HF_TOKEN != "your-huggingface-token"):
if HF_TOKEN != "your-huggingface-token":
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
huggingface_hub.login(token=HF_TOKEN)
else:
raise ValueError("HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token")
raise ValueError(
"HuggingFace Token EMPTY. Edit the .env file and put your HuggingFace token"
)
from transformers.tools import HfAgent
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
from transformers.tools import HfAgent
agent = HfAgent(url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5")
agent = HfAgent(
url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"
)
prompt = input(">>> Input prompt:\n>")