From 68cc2397562fb7311c12cdbd56902b1753c1b5ff Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:40:00 +0200 Subject: [PATCH 1/8] Feat: GPT4All Model Integration --- .../persona/prompt_template/gpt_structure.py | 128 +++++++----------- 1 file changed, 50 insertions(+), 78 deletions(-) diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index f9c4718949..98da31e7f0 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -1,17 +1,20 @@ """ -Author: Joon Sung Park (joonspk@stanford.edu) +Author: +Cassini Saturn (0xcassini@gmail.com) +Joon Sung Park (joonspk@stanford.edu) File: gpt_structure.py -Description: Wrapper functions for calling OpenAI APIs. +Description: Wrapper functions for calling GPT4All APIs. """ import json -import random -import openai import time from utils import * -openai.api_key = openai_api_key +from gpt4all import GPT4All, Embed4All + + +model = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin") def temp_sleep(seconds=0.1): time.sleep(seconds) @@ -19,38 +22,29 @@ def temp_sleep(seconds=0.1): def ChatGPT_single_request(prompt): temp_sleep() - completion = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=[{"role": "user", "content": prompt}] - ) - return completion["choices"][0]["message"]["content"] - + output = model.generate(prompt, max_tokens=30) + return output # ============================================================================ -# #####################[SECTION 1: CHATGPT-3 STRUCTURE] ###################### +# #####################[SECTION 1: CHATGPT4All STRUCTURE] ###################### # ============================================================================ -def GPT4_request(prompt): +def GPT4All_request(prompt): """ - Given a prompt and a dictionary of GPT parameters, make a request to OpenAI - server and returns the response. + Given a prompt, make a request to GPT4All model and returns the response. ARGS: prompt: a str prompt gpt_parameter: a python dictionary with the keys indicating the names of the parameter and the values indicating the parameter values. RETURNS: - a str of GPT-3's response. + a str of GPT4All's response. """ temp_sleep() try: - completion = openai.ChatCompletion.create( - model="gpt-4", - messages=[{"role": "user", "content": prompt}] - ) - return completion["choices"][0]["message"]["content"] - + output = model.generate(prompt, max_tokens=30) + return output except: print ("ChatGPT ERROR") return "ChatGPT ERROR" @@ -58,24 +52,20 @@ def GPT4_request(prompt): def ChatGPT_request(prompt): """ - Given a prompt and a dictionary of GPT parameters, make a request to OpenAI - server and returns the response. + Given a prompt, make a request to GPT4All model + ARGS: prompt: a str prompt gpt_parameter: a python dictionary with the keys indicating the names of the parameter and the values indicating the parameter values. RETURNS: - a str of GPT-3's response. + a str of GPT4All's response. """ # temp_sleep() try: - completion = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=[{"role": "user", "content": prompt}] - ) - return completion["choices"][0]["message"]["content"] - + output = model.generate(prompt, max_tokens=3) + return output except: print ("ChatGPT ERROR") return "ChatGPT ERROR" @@ -89,19 +79,19 @@ def GPT4_safe_generate_response(prompt, func_validate=None, func_clean_up=None, verbose=False): - prompt = 'GPT-3 Prompt:\n"""\n' + prompt + '\n"""\n' + prompt = 'GPT4All Prompt:\n"""\n' + prompt + '\n"""\n' prompt += f"Output the response to the prompt above in json. {special_instruction}\n" prompt += "Example output json:\n" prompt += '{"output": "' + str(example_output) + '"}' if verbose: - print ("CHAT GPT PROMPT") + print ("GPT4All PROMPT") print (prompt) for i in range(repeat): try: - curr_gpt_response = GPT4_request(prompt).strip() + curr_gpt_response = GPT4All_request(prompt).strip() end_index = curr_gpt_response.rfind('}') + 1 curr_gpt_response = curr_gpt_response[:end_index] curr_gpt_response = json.loads(curr_gpt_response)["output"] @@ -128,14 +118,14 @@ def ChatGPT_safe_generate_response(prompt, func_validate=None, func_clean_up=None, verbose=False): - # prompt = 'GPT-3 Prompt:\n"""\n' + prompt + '\n"""\n' + # prompt = 'GPT4All Prompt:\n"""\n' + prompt + '\n"""\n' prompt = '"""\n' + prompt + '\n"""\n' prompt += f"Output the response to the prompt above in json. {special_instruction}\n" prompt += "Example output json:\n" prompt += '{"output": "' + str(example_output) + '"}' if verbose: - print ("CHAT GPT PROMPT") + print ("GPT4All PROMPT") print (prompt) for i in range(repeat): @@ -171,7 +161,7 @@ def ChatGPT_safe_generate_response_OLD(prompt, func_clean_up=None, verbose=False): if verbose: - print ("CHAT GPT PROMPT") + print ("GPT4All PROMPT") print (prompt) for i in range(repeat): @@ -191,12 +181,12 @@ def ChatGPT_safe_generate_response_OLD(prompt, # ============================================================================ -# ###################[SECTION 2: ORIGINAL GPT-3 STRUCTURE] ################### +# ###################[SECTION 2: ORIGINAL GPT4All STRUCTURE] ################### # ============================================================================ def GPT_request(prompt, gpt_parameter): """ - Given a prompt and a dictionary of GPT parameters, make a request to OpenAI + Given a prompt and a dictionary of GPT parameters, make a request to GPT4All server and returns the response. ARGS: prompt: a str prompt @@ -204,21 +194,21 @@ def GPT_request(prompt, gpt_parameter): the parameter and the values indicating the parameter values. RETURNS: - a str of GPT-3's response. + a str of GPT4All's response. """ temp_sleep() try: - response = openai.Completion.create( - model=gpt_parameter["engine"], - prompt=prompt, - temperature=gpt_parameter["temperature"], - max_tokens=gpt_parameter["max_tokens"], - top_p=gpt_parameter["top_p"], - frequency_penalty=gpt_parameter["frequency_penalty"], - presence_penalty=gpt_parameter["presence_penalty"], - stream=gpt_parameter["stream"], - stop=gpt_parameter["stop"],) - return response.choices[0].text + + + output = model.generate( + prompt, + max_tokens=gpt_parameter["max_tokens"], + temp=gpt_parameter["temperature"], + top_p=gpt_parameter["top_p"], + + + ) + return output except: print ("TOKEN LIMIT EXCEEDED") return "TOKEN LIMIT EXCEEDED" @@ -236,7 +226,7 @@ def generate_prompt(curr_input, prompt_lib_file): INPUT, THIS CAN BE A LIST.) prompt_lib_file: the path to the promopt file. RETURNS: - a str prompt that will be sent to OpenAI's GPT server. + a str prompt that will be sent to GPT4All's model. """ if type(curr_input) == type("string"): curr_input = [curr_input] @@ -273,12 +263,14 @@ def safe_generate_response(prompt, return fail_safe_response -def get_embedding(text, model="text-embedding-ada-002"): - text = text.replace("\n", " ") - if not text: - text = "this is blank" - return openai.Embedding.create( - input=[text], model=model)['data'][0]['embedding'] +def get_embedding(text): + text = text.replace("\n", " ") + if not text: + text = "this is blank" + embedder = Embed4All() + embedding = embedder.embed(text) + return embedding + if __name__ == '__main__': @@ -309,23 +301,3 @@ def __func_clean_up(gpt_response): True) print (output) - - - - - - - - - - - - - - - - - - - - From 240bd2cbe28d0a94fb22b7da40d8a95b20ad65d1 Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:44:54 +0200 Subject: [PATCH 2/8] Update README.md --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fb44f00c64..5ee97ba136 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,6 @@ To set up your environment, you will need to generate a `utils.py` file that con ### Step 1. Generate Utils File In the `reverie/backend_server` folder (where `reverie.py` is located), create a new file titled `utils.py` and copy and paste the content below into the file: ``` -# Copy and paste your OpenAI API Key -openai_api_key = "" # Put your name key_owner = "" @@ -31,7 +29,7 @@ collision_block_id = "32125" # Verbose debug = True ``` -Replace `` with your OpenAI API key, and `` with your name. +Replace `` with your name. ### Step 2. Install requirements.txt Install everything listed in the `requirements.txt` file (I strongly recommend first setting up a virtualenv as usual). A note on Python version: we tested our environment on Python 3.9.12. @@ -112,12 +110,12 @@ For a more involved customization, you will need to author your own base simulat ## Generative Eddy Authors and Citation -**Authors:** Joon Sung Park, Joseph C. O'Brien, Carrie J. Cai, Meredith Ringel Morris, Percy Liang, Michael S. Bernstein +**Authors:** Cassini Saturn, Joon Sung Park, Joseph C. O'Brien, Carrie J. Cai, Meredith Ringel Morris, Percy Liang, Michael S. Bernstein Please cite our paper if you use the code or data in this repository. ``` @inproceedings{Park2023GenerativeAgents, -author = {Park, Joon Sung and O'Brien, Joseph C. and Cai, Carrie J. and Morris, Meredith Ringel and Liang, Percy and Bernstein, Michael S.}, +author = {Cassini, Saturn and Park, Joon Sung and O'Brien, Joseph C. and Cai, Carrie J. and Morris, Meredith Ringel and Liang, Percy and Bernstein, Michael S.}, title = {Generative Agents: Interactive Simulacra of Human Behavior}, year = {2023}, publisher = {Association for Computing Machinery}, From 668342802c2f3dd7073bfa01d061b8985f68bd07 Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Thu, 10 Aug 2023 15:49:46 +0200 Subject: [PATCH 3/8] Update requirements.txt to include gpt4all and remove openai --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 106c82f63e..9bb842bd8c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,7 +28,7 @@ matplotlib==3.7.2 multidict==6.0.4 nltk==3.6.5 numpy==1.25.2 -openai==0.27.0 +gpt4all==1.0.5 outcome==1.2.0 packaging==23.0 pandas==2.0.3 From e6cddd10411419cf6fbb4b525f88bdc4575988d1 Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:26:42 +0200 Subject: [PATCH 4/8] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 5ee97ba136..09e91ee7cb 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ To set up your environment, you will need to generate a `utils.py` file that con ### Step 1. Generate Utils File In the `reverie/backend_server` folder (where `reverie.py` is located), create a new file titled `utils.py` and copy and paste the content below into the file: ``` +# Select the GPT4All Model you'll use for the simulation. See: https://observablehq.com/@simonw/gpt4all-models +gpt4all_model="orca-mini-3b.ggmlv3.q4_0.bin" + # Put your name key_owner = "" From 4063127010235f3e6a07b76fb2cd534f6dd991a8 Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:27:34 +0200 Subject: [PATCH 5/8] Update gpt_structure.py to include the gpt4all_model --- .../backend_server/persona/prompt_template/gpt_structure.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index 98da31e7f0..b0f0ce1924 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -13,8 +13,7 @@ from gpt4all import GPT4All, Embed4All - -model = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin") +model = GPT4All(gpt4all_model) def temp_sleep(seconds=0.1): time.sleep(seconds) From 3d1b9d110cf02508bc120e48925d1f2bf1983e2c Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:55:24 +0200 Subject: [PATCH 6/8] Update gpt_structure.py to include temp and max tokens params --- .../backend_server/persona/prompt_template/gpt_structure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index b0f0ce1924..a5be9c7496 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -273,8 +273,8 @@ def get_embedding(text): if __name__ == '__main__': - gpt_parameter = {"engine": "text-davinci-003", "max_tokens": 50, - "temperature": 0, "top_p": 1, "stream": False, + gpt_parameter = {"max_tokens": max_tokens, + "temperature": temperature, "top_p": 1, "stream": False, "frequency_penalty": 0, "presence_penalty": 0, "stop": ['"']} curr_input = ["driving to a friend's house"] From 582881cde377d8c6bb0e8ddbe823e3ec977fae49 Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:56:22 +0200 Subject: [PATCH 7/8] Update README.md with new utils params --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 09e91ee7cb..dbf6010214 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ In the `reverie/backend_server` folder (where `reverie.py` is located), create a ``` # Select the GPT4All Model you'll use for the simulation. See: https://observablehq.com/@simonw/gpt4all-models gpt4all_model="orca-mini-3b.ggmlv3.q4_0.bin" +max_tokens = 30 +temperature = 0.5 # Put your name key_owner = "" From bfa7fff8d49924305a9eb856bffdf52afaae2c5e Mon Sep 17 00:00:00 2001 From: SaturnCassini <114288685+SaturnCassini@users.noreply.github.com> Date: Sun, 13 Aug 2023 11:59:55 +0200 Subject: [PATCH 8/8] max_tokens passed to all functions --- .../backend_server/persona/prompt_template/gpt_structure.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reverie/backend_server/persona/prompt_template/gpt_structure.py b/reverie/backend_server/persona/prompt_template/gpt_structure.py index a5be9c7496..f4d7f6fd65 100644 --- a/reverie/backend_server/persona/prompt_template/gpt_structure.py +++ b/reverie/backend_server/persona/prompt_template/gpt_structure.py @@ -21,7 +21,7 @@ def temp_sleep(seconds=0.1): def ChatGPT_single_request(prompt): temp_sleep() - output = model.generate(prompt, max_tokens=30) + output = model.generate(prompt, max_tokens=max_tokens) return output # ============================================================================ @@ -42,7 +42,7 @@ def GPT4All_request(prompt): temp_sleep() try: - output = model.generate(prompt, max_tokens=30) + output = model.generate(prompt, max_tokens=max_tokens) return output except: print ("ChatGPT ERROR") @@ -63,7 +63,7 @@ def ChatGPT_request(prompt): """ # temp_sleep() try: - output = model.generate(prompt, max_tokens=3) + output = model.generate(prompt, max_tokens=max_tokens) return output except: print ("ChatGPT ERROR")