date_collected
stringclasses
1 value
repo_name
stringlengths
6
116
file_name
stringlengths
2
220
file_contents
stringlengths
13
357k
prompts
sequence
2024-01-10
icoxfog417/baby-steps-of-rl-ja
FN~a2c_agent.py
import argparse from collections import deque import numpy as np from sklearn.preprocessing import StandardScaler import tensorflow as tf from tensorflow.python import keras as K from PIL import Image import gym import gym_ple from fn_framework import FNAgent, Trainer, Observer tf.compat.v1.disable_eager_execution() class ActorCriticAgent(FNAgent): def __init__(self, actions): # ActorCriticAgent uses self policy (doesn't use epsilon). super().__init__(epsilon=0.0, actions=actions) self._updater = None @classmethod def load(cls, env, model_path): actions = list(range(env.action_space.n)) agent = cls(actions) agent.model = K.models.load_model(model_path, custom_objects={ "SampleLayer": SampleLayer}) agent.initialized = True return agent def initialize(self, experiences, optimizer): feature_shape = experiences[0].s.shape self.make_model(feature_shape) self.set_updater(optimizer) self.initialized = True print("Done initialization. From now, begin training!") def make_model(self, feature_shape): normal = K.initializers.glorot_normal() model = K.Sequential() model.add(K.layers.Conv2D( 32, kernel_size=8, strides=4, padding="same", input_shape=feature_shape, kernel_initializer=normal, activation="relu")) model.add(K.layers.Conv2D( 64, kernel_size=4, strides=2, padding="same", kernel_initializer=normal, activation="relu")) model.add(K.layers.Conv2D( 64, kernel_size=3, strides=1, padding="same", kernel_initializer=normal, activation="relu")) model.add(K.layers.Flatten()) model.add(K.layers.Dense(256, kernel_initializer=normal, activation="relu")) actor_layer = K.layers.Dense(len(self.actions), kernel_initializer=normal) action_evals = actor_layer(model.output) actions = SampleLayer()(action_evals) critic_layer = K.layers.Dense(1, kernel_initializer=normal) values = critic_layer(model.output) self.model = K.Model(inputs=model.input, outputs=[actions, action_evals, values]) def set_updater(self, optimizer, value_loss_weight=1.0, entropy_weight=0.1): actions = tf.compat.v1.placeholder(shape=(None), dtype="int32") values = tf.compat.v1.placeholder(shape=(None), dtype="float32") _, action_evals, estimateds = self.model.output neg_logs = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=action_evals, labels=actions) # tf.stop_gradient: Prevent policy_loss influences critic_layer. advantages = values - tf.stop_gradient(estimateds) policy_loss = tf.reduce_mean(neg_logs * advantages) value_loss = tf.keras.losses.MeanSquaredError()(values, estimateds) action_entropy = tf.reduce_mean(self.categorical_entropy(action_evals)) loss = policy_loss + value_loss_weight * value_loss loss -= entropy_weight * action_entropy updates = optimizer.get_updates(loss=loss, params=self.model.trainable_weights) self._updater = K.backend.function( inputs=[self.model.input, actions, values], outputs=[loss, policy_loss, value_loss, tf.reduce_mean(neg_logs), tf.reduce_mean(advantages), action_entropy], updates=updates) def categorical_entropy(self, logits): """ From OpenAI baseline implementation. https://github.com/openai/baselines/blob/master/baselines/common/distributions.py#L192 """ a0 = logits - tf.reduce_max(logits, axis=-1, keepdims=True) ea0 = tf.exp(a0) z0 = tf.reduce_sum(ea0, axis=-1, keepdims=True) p0 = ea0 / z0 return tf.reduce_sum(p0 * (tf.math.log(z0) - a0), axis=-1) def policy(self, s): if not self.initialized: return np.random.randint(len(self.actions)) else: action, action_evals, values = self.model.predict(np.array([s])) return action[0] def estimate(self, s): action, action_evals, values = self.model.predict(np.array([s])) return values[0][0] def update(self, states, actions, rewards): return self._updater([states, actions, rewards]) class SampleLayer(K.layers.Layer): def __init__(self, **kwargs): self.output_dim = 1 # sample one action from evaluations super(SampleLayer, self).__init__(**kwargs) def build(self, input_shape): super(SampleLayer, self).build(input_shape) def call(self, x): noise = tf.random.uniform(tf.shape(x)) return tf.argmax(x - tf.math.log(-tf.math.log(noise)), axis=1) def compute_output_shape(self, input_shape): return (input_shape[0], self.output_dim) class ActorCriticAgentTest(ActorCriticAgent): def make_model(self, feature_shape): normal = K.initializers.glorot_normal() model = K.Sequential() model.add(K.layers.Dense(10, input_shape=feature_shape, kernel_initializer=normal, activation="relu")) model.add(K.layers.Dense(10, kernel_initializer=normal, activation="relu")) actor_layer = K.layers.Dense(len(self.actions), kernel_initializer=normal) action_evals = actor_layer(model.output) actions = SampleLayer()(action_evals) critic_layer = K.layers.Dense(1, kernel_initializer=normal) values = critic_layer(model.output) self.model = K.Model(inputs=model.input, outputs=[actions, action_evals, values]) class CatcherObserver(Observer): def __init__(self, env, width, height, frame_count): super().__init__(env) self.width = width self.height = height self.frame_count = frame_count self._frames = deque(maxlen=frame_count) def transform(self, state): grayed = Image.fromarray(state).convert("L") resized = grayed.resize((self.width, self.height)) resized = np.array(resized).astype("float") normalized = resized / 255.0 # scale to 0~1 if len(self._frames) == 0: for i in range(self.frame_count): self._frames.append(normalized) else: self._frames.append(normalized) feature = np.array(self._frames) # Convert the feature shape (f, w, h) => (h, w, f). feature = np.transpose(feature, (1, 2, 0)) return feature class ActorCriticTrainer(Trainer): def __init__(self, buffer_size=256, batch_size=32, gamma=0.99, learning_rate=1e-3, report_interval=10, log_dir="", file_name=""): super().__init__(buffer_size, batch_size, gamma, report_interval, log_dir) self.file_name = file_name if file_name else "a2c_agent.h5" self.learning_rate = learning_rate self.losses = {} self.rewards = [] self._max_reward = -10 def train(self, env, episode_count=900, initial_count=10, test_mode=False, render=False, observe_interval=100): actions = list(range(env.action_space.n)) if not test_mode: agent = ActorCriticAgent(actions) else: agent = ActorCriticAgentTest(actions) observe_interval = 0 self.training_episode = episode_count self.train_loop(env, agent, episode_count, initial_count, render, observe_interval) return agent def episode_begin(self, episode, agent): self.rewards = [] def step(self, episode, step_count, agent, experience): self.rewards.append(experience.r) if not agent.initialized: if len(self.experiences) < self.buffer_size: # Store experience until buffer_size (enough to initialize). return False optimizer = K.optimizers.Adam(lr=self.learning_rate, clipnorm=5.0) agent.initialize(self.experiences, optimizer) self.logger.set_model(agent.model) self.training = True self.experiences.clear() else: if len(self.experiences) < self.batch_size: # Store experience until batch_size (enough to update). return False batch = self.make_batch(agent) loss, lp, lv, p_ng, p_ad, p_en = agent.update(*batch) # Record latest metrics. self.losses["loss/total"] = loss self.losses["loss/policy"] = lp self.losses["loss/value"] = lv self.losses["policy/neg_logs"] = p_ng self.losses["policy/advantage"] = p_ad self.losses["policy/entropy"] = p_en self.experiences.clear() def make_batch(self, agent): states = [] actions = [] values = [] experiences = list(self.experiences) states = np.array([e.s for e in experiences]) actions = np.array([e.a for e in experiences]) # Calculate values. # If the last experience isn't terminal (done) then estimates value. last = experiences[-1] future = last.r if last.d else agent.estimate(last.n_s) for e in reversed(experiences): value = e.r if not e.d: value += self.gamma * future values.append(value) future = value values = np.array(list(reversed(values))) scaler = StandardScaler() values = scaler.fit_transform(values.reshape((-1, 1))).flatten() return states, actions, values def episode_end(self, episode, step_count, agent): reward = sum(self.rewards) self.reward_log.append(reward) if agent.initialized: self.logger.write(self.training_count, "reward", reward) self.logger.write(self.training_count, "reward_max", max(self.rewards)) for k in self.losses: self.logger.write(self.training_count, k, self.losses[k]) if reward > self._max_reward: agent.save(self.logger.path_of(self.file_name)) self._max_reward = reward if self.is_event(episode, self.report_interval): recent_rewards = self.reward_log[-self.report_interval:] self.logger.describe("reward", recent_rewards, episode=episode) def main(play, is_test): file_name = "a2c_agent.h5" if not is_test else "a2c_agent_test.h5" trainer = ActorCriticTrainer(file_name=file_name) path = trainer.logger.path_of(trainer.file_name) agent_class = ActorCriticAgent if is_test: print("Train on test mode") obs = gym.make("CartPole-v0") agent_class = ActorCriticAgentTest else: env = gym.make("Catcher-v0") obs = CatcherObserver(env, 80, 80, 4) trainer.learning_rate = 7e-5 if play: agent = agent_class.load(obs, path) agent.play(obs, episode_count=10, render=True) else: trainer.train(obs, test_mode=is_test) if __name__ == "__main__": parser = argparse.ArgumentParser(description="A2C Agent") parser.add_argument("--play", action="store_true", help="play with trained model") parser.add_argument("--test", action="store_true", help="train by test mode") args = parser.parse_args() main(args.play, args.test)
[]
2024-01-10
obata-k/michat
michat~lib~speak~speak.py
import logging import os from argparse import ArgumentParser from pathlib import Path from enum import Enum import json import openai from dotenv import load_dotenv from playsound import playsound from voicevox_core import AccelerationMode, VoicevoxCore open_jtalk_dict_dir = "./open_jtalk_dic_utf_8-1.11" acceleration_mode = AccelerationMode.AUTO system_root = Path("system") class ChatGPTFeature(Enum): ZUNDAMON = "ずんだもん" ENE = "エネ" MIKU = "初音ミク" @classmethod def get_names(cls) -> list: return [i.name for i in cls] @classmethod def get_values(cls) -> list: return [i.value for i in cls] @classmethod def index(cls, value) -> int: return cls.get_values().index(value) @classmethod def value_of(cls, target_value): for e in ChatGPTFeature: if e.value == target_value: return e raise ValueError("{} is not a valid feature".format(target_value)) class ChatGPT: def __init__(self, max_token_size): self.__max_token_size = max_token_size dotenv_path = Path(os.path.join(os.getcwd(), ".env")) if dotenv_path.exists(): load_dotenv(dotenv_path) openai.api_key = os.environ.get("OPENAI_API_KEY") @property def max_token_size(self): return self.__max_token_size @max_token_size.setter def max_token_size(self, n): self.__max_token_size = n # history は(DBやファイルなど)外部で保持している def generate(self, system_text, user_text, history=None): messages = [] if history is None: history = [] for h in history: messages.append(h) messages.extend( [ { "role": "system", "content": system_text, }, { "role": "user", "content": user_text, }, ] ) # GPT-3でテキストを生成する response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, max_tokens=int(self.max_token_size), n=1, stop=None, temperature=0.5, ) # GPT-3の生成したテキストを取得する text = response.choices[0].message.content.strip() history = history + [ { "role": "user", "content": user_text, }, {"role": "assistant", "content": text}, ] return (text, history) class ChatGPTWithEmotion(ChatGPT): def __init__(self, max_token_size): super().__init__(max_token_size) self.system_emotion = system_root / Path("system-emotion.txt") def trim_and_parse(self, text): lines = [] payload = None for line in text.splitlines(): try: payload = json.loads(line) continue except ValueError: pass if "感情パラメータ" not in line and line != "": lines.append(line) return "\n".join(lines), payload def generate(self, system_text, user_text, history=None): with open(self.system_emotion, "r") as f: system_text += f.read() generated, new_history = super().generate(system_text, user_text, history) response, params = self.trim_and_parse(generated) return (response, new_history, params) class Audio: def __init__(self, speaker_id): self.speaker_id = speaker_id # voicevoxでテキストを音声に変換する def transform(self, text): self.core = VoicevoxCore( acceleration_mode=acceleration_mode, open_jtalk_dict_dir=open_jtalk_dict_dir ) self.core.load_model(self.speaker_id) self.audio_query = self.core.audio_query(text, self.speaker_id) # 音声をファイルに保存する def save_wav(self, out): out.write_bytes(self.wav) def get_wav(self): self.wav = self.core.synthesis(self.audio_query, self.speaker_id) return self.wav # 音声を再生する def play(self, file): playsound(file) def setup_log(log_file, log_level): FORMAT = "%(asctime)s: [%(levelname)s] %(message)s" logging.basicConfig(format=FORMAT) level_num = getattr(logging, log_level.upper(), None) if not isinstance(level_num, int): raise ValueError("Invalid log level: %s" % log_level) logger = logging.getLogger(__name__) logger.setLevel(level_num) if log_file == "stdout": stdout_handler = logging.StreamHandler() logger.addHandler(stdout_handler) else: file_handler = logging.FileHandler(filename=log_file) logger.addHandler(file_handler) logger.propagate = False return logger def system_text(feature=ChatGPTFeature.ZUNDAMON): if feature is ChatGPTFeature.ZUNDAMON: system = Path("system-zundamon.txt") elif feature is ChatGPTFeature.ENE: system = Path("system-ene.txt") elif feature is ChatGPTFeature.MIKU: system = Path("system-miku.txt") else: raise ValueError("invalid ChatGPT feature was set") with open(system_root / system, "r") as f: text = f.read() return text
[]
2024-01-10
glencode/babyagi_git
classic~babyfoxagi~tasks~task_registry.py
import openai import json import threading import os import numpy as np from datetime import datetime from collections import defaultdict class TaskRegistry: def __init__(self): self.tasks = [] # Initialize the lock self.lock = threading.Lock() objectives_file_path = "tasks/example_objectives" self.example_loader = ExampleObjectivesLoader(objectives_file_path) def load_example_objectives(self, user_objective): return self.example_loader.load_example_objectives(user_objective) def create_tasklist(self, objective, skill_descriptions): #reflect on objective notes = self.reflect_on_objective(objective,skill_descriptions) #load most relevant object and tasklist from objectives_examples.json example_objective, example_tasklist, example_reflection = self.load_example_objectives(objective) prompt = ( f"You are an expert task list creation AI tasked with creating a list of tasks as a JSON array, considering the ultimate objective of your team: {objective}. " f"Create a very short task list based on the objective, the final output of the last task will be provided back to the user. Limit tasks types to those that can be completed with the available skills listed below. Task description should be detailed.###" f"AVAILABLE SKILLS: {skill_descriptions}.###" f"RULES:" f"Do not use skills that are not listed." f"Always provide an ID to each task." f"Always include one skill." f"The final task should always output the final result of the overall objective." f"dependent_task_ids should always be an empty array, or an array of numbers representing the task ID it should pull results from." f"Make sure all task IDs are in chronological order.###\n" f"Helpful Notes as guidance:{notes}###\n" f"EXAMPLE OBJECTIVE={json.dumps(example_objective)}" f"TASK LIST={json.dumps(example_tasklist)}" f"OBJECTIVE={objective}" f"TASK LIST=" ) #print(prompt) print("\033[90m\033[3m" + "\nInitializing...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": "You are a task creation AI." }, { "role": "user", "content": prompt } ], temperature=0, max_tokens=2500, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] try: task_list = json.loads(result) #print(task_list) self.tasks = task_list except Exception as error: print(error) def reflect_on_objective(self, objective, skill_descriptions): #load most relevant object and tasklist from objectives_examples.json example_objective, example_tasklist, example_reflection = self.load_example_objectives(objective) prompt = ( f"You are an Ai specializing in generating helpful thoughts and ideas on tackling an objective, and your task is to think about how to tackle this objective: {objective}. " f"These are the skills available to you: {skill_descriptions}.###" f"Think about what tools and information you need to handle this objective, and which of the available skills would be most helpful to you and writea descriptive note to pass onto a task creation AI." f"Consider the following example objective, tasklist, and reflection as a sample." f"###EXAMPLE OBJECTIVE:{example_objective}." f"###EXAMPLE TASKLIST:{example_tasklist}." f"###REFLECTION FROM EXAMPLE:{example_reflection}." f"###THE AI AGENT'S OBJECTIVE:{example_reflection}." f"###INSTRUCTION: please provide helpful notes for the task creation agent specific to this objective." ) #print(prompt) print("\033[90m\033[3m" + "\nInitializing...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": f"You are an Ai specializing in generating helpful thoughts and ideas on tackling an objective, and your task is to think about how to tackle this objective: {objective}. " }, { "role": "user", "content": prompt } ], temperature=0, max_tokens=250, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] print(result) return result def execute_task(self, i, task, skill_registry, task_outputs, objective): p_nexttask="\033[92m\033[1m"+"\n*****NEXT TASK ID:"+str(task['id'])+"*****\n"+"\033[0m\033[0m" p_nexttask += f"\033[ EExecuting task {task.get('id')}: {task.get('task')}) [{task.get('skill')}]\033[)" print(p_nexttask) # Retrieve the skill from the registry skill = skill_registry.get_skill(task['skill']) # Get the outputs of the dependent tasks dependent_task_outputs = {dep: task_outputs[dep]["output"] for dep in task['dependent_task_ids']} if 'dependent_task_ids' in task else {} # Execute the skill # print("execute:"+str([task['task'], dependent_task_outputs, objective])) task_output = skill.execute(task['task'], dependent_task_outputs, objective) print("\033[93m\033[1m"+"\nTask Output (ID:"+str(task['id'])+"):"+"\033[0m\033[0m") print("TASK: "+str(task["task"])) print("OUTPUT: "+str(task_output)) return i, task_output def reorder_tasks(self): self.tasks= sorted(self.tasks, key=lambda task: task['id']) def add_task(self, task, after_task_id): # Get the task ids task_ids = [t["id"] for t in self.tasks] # Get the index of the task id to add the new task after insert_index = task_ids.index(after_task_id) + 1 if after_task_id in task_ids else len(task_ids) # Insert the new task self.tasks.insert(insert_index, task) self.reorder_tasks() def get_tasks(self): return self.tasks def update_tasks(self, task_update): for task in self.tasks: if task['id'] == task_update['id']: task.update(task_update) self.reorder_tasks() def reflect_on_output(self, task_output, skill_descriptions): with self.lock: example = [ [ {"id": 3, "task": "New task 1 description", "skill": "text_completion_skill", "dependent_task_ids": [], "status": "complete"}, {"id": 4, "task": "New task 2 description", "skill": "text_completion_skill", "dependent_task_ids": [], "status": "incomplete"} ], [2, 3], {"id": 5, "task": "Complete the objective and provide a final report", "skill": "text_completion_skill", "dependent_task_ids": [1, 2, 3, 4], "status": "incomplete"} ] prompt = ( f"You are an expert task manager, review the task output to decide whether any new tasks need to be added, or whether any tasks need to be updated." f"As you add a new task, see if there are any tasks that need to be updated (such as updating dependencies)." f"Use the current task list as reference." f"Do not add duplicate tasks to those in the current task list." f"Only provide JSON as your response without further comments." f"Every new and updated task must include all variables, even they are empty array." f"Dependent IDs must be smaller than the ID of the task." f"New tasks IDs should be no larger than the last task ID." f"Always select at least one skill." f"Task IDs should be unique and in chronological order." f"Do not change the status of complete tasks." f"Only add skills from the AVAILABLE SKILLS, using the exact same spelling." f"Provide your array as a JSON array with double quotes. The first object is new tasks to add as a JSON array, the second array lists the ID numbers where the new tasks should be added after (number of ID numbers matches array), and the third object provides the tasks that need to be updated." f"Make sure to keep dependent_task_ids key, even if an empty array." f"AVAILABLE SKILLS: {skill_descriptions}.###" f"\n###Here is the last task output: {task_output}" f"\n###Here is the current task list: {self.tasks}" f"\n###EXAMPLE OUTPUT FORMAT = {json.dumps(example)}" f"\n###OUTPUT = " ) print("\033[90m\033[3m" + "\nReflecting on task output to generate new tasks if necessary...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k-0613", messages=[ { "role": "system", "content": "You are a task creation AI." }, { "role": "user", "content": prompt } ], temperature=0.7, max_tokens=1500, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] print("\n#" + str(result)) # Check if the returned result has the expected structure if isinstance(result, str): try: task_list = json.loads(result) print("####task_list in function") print(task_list) print("####task_list split in function") print(task_list[0], task_list[1], task_list[2]) return task_list[0], task_list[1], task_list[2] except Exception as error: print(error) else: raise ValueError("Invalid task list structure in the output") def get_tasks(self): """ Returns the current list of tasks. Returns: list: the list of tasks. """ return self.tasks def get_task(self, task_id): """ Returns a task given its task_id. Parameters: task_id : int The unique ID of the task. Returns: dict The task that matches the task_id. """ matching_tasks = [task for task in self.tasks if task["id"] == task_id] if matching_tasks: return matching_tasks[0] else: print(f"No task found with id {task_id}") return None def print_tasklist(self, tasks): p_tasklist="\033[95m\033[1m" + "\n*****TASK LIST*****\n" + "\033[0m" for t in tasks: dependent_task_ids = t.get('dependent_task_ids', []) dependent_task = "" if dependent_task_ids: dependent_task = f"\033[31m<dependencies: {', '.join([f'#{dep_id}' for dep_id in dependent_task_ids])}>\033[0m" status_color = "\033[32m" if t.get('status') == "completed" else "\033[31m" p_tasklist+= f"\033[1m{t.get('id')}\033[0m: {t.get('task')} {status_color}[{t.get('status')}]\033[0m \033[93m[{t.get('skill')}] {dependent_task}\033[0m\n" print(p_tasklist) def reflect_tasklist(self, objective, task_list, task_outputs, skill_descriptions): prompt = ( f"You are an expert task manager. Reflect on the objective, entire task list, and the corresponding outputs to generate a better task list for the objective." f"Do not included 'results', and change every status to 'incomplete'." f"Only provide JSON as your response without further comments. " f"Use the current task list as reference. " f"Always make at least one change to the current task list " f"OBJECTIVE: {objective}." f"AVAILABLE SKILLS: {skill_descriptions}." f"\n###Here is the current task list: {json.dumps(task_list)}" f"\n###Here is the task outputs: {json.dumps(task_outputs)}" f"\n###IMPROVED TASKLIST = " ) print("\033[90m\033[3m" + "\nReflecting on entire task list...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": "You are an AI specializing in reflecting on task lists and improving them. You will never simply return the provided task list, but always improve on it." }, { "role": "user", "content": prompt } ], temperature=0, max_tokens=4000, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] try: improved_task_list = json.loads(result) # Formatting improved_task_list to your desired format formatted_improved_task_list = [{ "objective": objective, "examples": improved_task_list, "date": datetime.now().strftime("%Y-%m-%d") }] with open(f'tasks/example_objectives/improved_{datetime.now().strftime("%Y%m%d%H%M%S")}.json', 'w') as f: json.dump(formatted_improved_task_list, f) print(f"IMPROVED TASK LIST:{formatted_improved_task_list}") except Exception as error: print(error) def reflect_on_result(self, objective, task_list, task_outputs, skill_descriptions): prompt = ( f"You are an expert AI specializing in analyzing yourself, an autonomous agent that combines multiple LLM calls. Reflect on the objective, entire task list, and the corresponding outputs and provide an analysis of the performance of yourself and how you could have performed better." f"\n###OBJECTIVE: {objective}." f"\n###AVAILABLE SKILLS: {skill_descriptions}." f"\n###TASK LIST: {json.dumps(task_list)}" f"\n###TASK OUTPUTS: {json.dumps(task_outputs)}" f"\n###ANALYSIS:" ) print("\033[90m\033[3m" + "\nReflecting on result...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": "You are an expert AI specializing in analyzing yourself, an autonomous agent that combines multiple LLM calls. Reflect on the objective, entire task list, and the corresponding outputs and provide an analysis of the performance of yourself and how you could have performed better." }, { "role": "user", "content": prompt } ], temperature=0, max_tokens=2000, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] try: print(result) return result except Exception as error: print(error) def reflect_on_final(self, objective, task_list, task_outputs, skill_descriptions): print("here!") system_content_result = "You are an expert AI specializing in analyzing yourself, an autonomous agent that combines multiple LLM calls. Reflect on the objective, entire task list, and the corresponding outputs and provide an analysis of the performance of yourself and how you could have performed better." role_content_result = ( f"You are an expert AI specializing in analyzing yourself, an autonomous agent that combines multiple LLM calls. Reflect on the objective, entire task list, and the corresponding outputs and provide an analysis of the performance of yourself and how you could have performed better." f"\n###OBJECTIVE: {objective}." f"\n###AVAILABLE SKILLS: {skill_descriptions}." f"\n###TASK LIST: {json.dumps(task_list)}" f"\n###TASK OUTPUTS: {json.dumps(task_outputs)}" f"\n###ANALYSIS:" ) print("\033[90m\033[3m" + "\nReflecting on result...\n" + "\033[0m") response = self.chatcompletion(role_content_result, system_content_result,500) # Extract the content of the assistant's response and parse it as JSON simple_reflection = response["choices"][0]["message"]["content"] try: print(simple_reflection) except Exception as error: print(error) system_content_task = "You are an AI specializing in reflecting on task lists and improving them. You will never simply return the provided task list, but always improve on it." role_content_task = ( f"You are an expert task manager. Reflect on the objective, entire task list, and the corresponding outputs to generate a better task list for the objective." f"Do not included 'results', and change every status to 'incomplete'." f"Only provide JSON as your response without further comments. " f"Use the current task list as reference. " f"Always make at least one change to the current task list " f"OBJECTIVE: {objective}." f"AVAILABLE SKILLS: {skill_descriptions}." f"SIMPLE REFLECTION: {simple_reflection}." f"\n###Here is the current task list: {json.dumps(task_list)}" f"\n###Here is the task outputs: {json.dumps(task_outputs)}" f"\n###IMPROVED TASKLIST = " ) print("\033[90m\033[3m" + "\nReflecting on entire task list...\n" + "\033[0m") response = self.chatcompletion(role_content_task, system_content_task,4000) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] print(result) try: improved_task_list = json.loads(result) # Formatting improved_task_list to your desired format formatted_improved_task_list = [{ "objective": objective, "examples": improved_task_list, "date": datetime.now().strftime("%Y-%m-%d"), "reflection":simple_reflection }] with open(f'tasks/example_objectives/improved_{datetime.now().strftime("%Y%m%d%H%M%S")}.json', 'w') as f: json.dump(formatted_improved_task_list, f) print(f"IMPROVED TASK LIST:{formatted_improved_task_list}") except Exception as error: print(error) def chatcompletion(self, role_content, system_content, max_tokens): return openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": system_content }, { "role": "user", "content": role_content } ], temperature=0, max_tokens=max_tokens, top_p=1, frequency_penalty=0, presence_penalty=0 ) from datetime import datetime class ExampleObjectivesLoader: def __init__(self, objectives_folder_path, decay_factor=0.01): self.objectives_folder_path = objectives_folder_path self.decay_factor = decay_factor self.objectives_examples = [] # Initialize as an empty list def load_objectives_examples(self): objectives_dict = defaultdict(dict) for filename in os.listdir(self.objectives_folder_path): file_path = os.path.join(self.objectives_folder_path, filename) with open(file_path, 'r') as file: objectives = json.load(file) for objective in objectives: key = objective['objective'] date = objective.get('date', None) if date is not None: date = datetime.strptime(date, '%Y-%m-%d') if key not in objectives_dict or (date and datetime.strptime(objectives_dict[key]['date'], "%Y-%m-%d") < date): objectives_dict[key] = objective self.objectives_examples = list(objectives_dict.values()) def find_most_relevant_objective(self, user_input): user_input_embedding = self.get_embedding(user_input, model='text-embedding-ada-002') most_relevant_objective = max( self.objectives_examples, key=lambda pair: self.cosine_similarity(pair['objective'], user_input_embedding) * self.get_decay(pair) ) return most_relevant_objective['objective'], most_relevant_objective['examples'], most_relevant_objective.get('reflection', '') def get_decay(self, objective): date = objective.get('date', None) if date is not None: date = datetime.strptime(date, '%Y-%m-%d') days_passed = (datetime.now() - date).days else: # if there's no date, assume a large number of days passed days_passed = 365 * 10 # 10 years decay = np.exp(-self.decay_factor * days_passed) return decay def get_embedding(self, text, model='text-embedding-ada-002'): response = openai.Embedding.create(input=[text], model=model) embedding = response['data'][0]['embedding'] return embedding def cosine_similarity(self, objective, embedding): max_similarity = float('-inf') objective_embedding = self.get_embedding(objective, model='text-embedding-ada-002') similarity = self.calculate_similarity(objective_embedding, embedding) max_similarity = max(max_similarity, similarity) return max_similarity def calculate_similarity(self, embedding1, embedding2): embedding1 = np.array(embedding1, dtype=np.float32) embedding2 = np.array(embedding2, dtype=np.float32) similarity = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2)) return similarity def load_example_objectives(self, user_objective): self.load_objectives_examples() most_relevant_objective, most_relevant_tasklist, most_relevant_reflection = self.find_most_relevant_objective(user_objective) example_objective = most_relevant_objective example_tasklist = most_relevant_tasklist example_reflection = most_relevant_reflection return example_objective, example_tasklist, example_reflection
[ "TASK LIST=", "Always select at least one skill.", "Provide your array as a JSON array with double quotes. The first object is new tasks to add as a JSON array, the second array lists the ID numbers where the new tasks should be added after (number of ID numbers matches array), and the third object provides the tasks that need to be updated.", "Use the current task list as reference. ", "\n###OUTPUT = ", "Use the current task list as reference.", "Dependent IDs must be smaller than the ID of the task.", "Make sure all task IDs are in chronological order.###\n", "You are an expert task manager. Reflect on the objective, entire task list, and the corresponding outputs to generate a better task list for the objective.", "AVAILABLE SKILLS: PLACEHOLDER.###", "OBJECTIVE: PLACEHOLDER.", "You are an expert task manager, review the task output to decide whether any new tasks need to be added, or whether any tasks need to be updated.", "Only add skills from the AVAILABLE SKILLS, using the exact same spelling.", "\n###IMPROVED TASKLIST = ", "Make sure to keep dependent_task_ids key, even if an empty array.", "AVAILABLE SKILLS: PLACEHOLDER.", "As you add a new task, see if there are any tasks that need to be updated (such as updating dependencies).", "Create a very short task list based on the objective, the final output of the last task will be provided back to the user. Limit tasks types to those that can be completed with the available skills listed below. Task description should be detailed.###", "Helpful Notes as guidance:PLACEHOLDER###\n", "Do not change the status of complete tasks.", "\n###ANALYSIS:", "Do not add duplicate tasks to those in the current task list.", "New tasks IDs should be no larger than the last task ID.", "Always include one skill.", "Always make at least one change to the current task list ", "Only provide JSON as your response without further comments. ", "You are an expert task list creation AI tasked with creating a list of tasks as a JSON array, considering the ultimate objective of your team: PLACEHOLDER. ", "Task IDs should be unique and in chronological order.", "Always provide an ID to each task.", "You are an Ai specializing in generating helpful thoughts and ideas on tackling an objective, and your task is to think about how to tackle this objective: PLACEHOLDER. ", "OBJECTIVE=PLACEHOLDER", "\n###AVAILABLE SKILLS: PLACEHOLDER.", "\n###Here is the last task output: PLACEHOLDER", "Do not use skills that are not listed.", "Do not included 'results', and change every status to 'incomplete'.", "The final task should always output the final result of the overall objective.", "You are a task creation AI.", "You are an expert AI specializing in analyzing yourself, an autonomous agent that combines multiple LLM calls. Reflect on the objective, entire task list, and the corresponding outputs and provide an analysis of the performance of yourself and how you could have performed better.", "Every new and updated task must include all variables, even they are empty array.", "\n###OBJECTIVE: PLACEHOLDER.", "dependent_task_ids should always be an empty array, or an array of numbers representing the task ID it should pull results from.", "You are an Ai specializing in generating helpful thoughts and ideas on tackling an objective, and your task is to think about how to tackle this objective: PLACEHOLDER. These are the skills available to you: PLACEHOLDER.###Think about what tools and information you need to handle this objective, and which of the available skills would be most helpful to you and writea descriptive note to pass onto a task creation AI.Consider the following example objective, tasklist, and reflection as a sample.###EXAMPLE OBJECTIVE:PLACEHOLDER.###EXAMPLE TASKLIST:PLACEHOLDER.###REFLECTION FROM EXAMPLE:PLACEHOLDER.###THE AI AGENT'S OBJECTIVE:PLACEHOLDER.###INSTRUCTION: please provide helpful notes for the task creation agent specific to this objective.", "Only provide JSON as your response without further comments.", "You are an AI specializing in reflecting on task lists and improving them. You will never simply return the provided task list, but always improve on it." ]
2024-01-10
glencode/babyagi_git
classic~BabyElfAGI~tasks~task_registry.py
import openai import json import threading import os import numpy as np class TaskRegistry: def __init__(self): self.tasks = [] # Initialize the lock self.lock = threading.Lock() objectives_file_path = "tasks/example_objectives" self.example_loader = ExampleObjectivesLoader(objectives_file_path) def load_example_objectives(self, user_objective): return self.example_loader.load_example_objectives(user_objective) def create_tasklist(self, objective, skill_descriptions): #load most relevant object and tasklist from objectives_examples.json example_objective, example_tasklist = self.load_example_objectives(objective) prompt = ( f"You are an expert task list creation AI tasked with creating a list of tasks as a JSON array, considering the ultimate objective of your team: {objective}. " f"Create a very short task list based on the objective, the final output of the last task will be provided back to the user. Limit tasks types to those that can be completed with the available skills listed below. Task description should be detailed.###" f"AVAILABLE SKILLS: {skill_descriptions}.###" f"RULES:" f"Do not use skills that are not listed." f"Always include one skill." f"dependent_task_ids should always be an empty array, or an array of numbers representing the task ID it should pull results from." f"Make sure all task IDs are in chronological order.###\n" f"EXAMPLE OBJECTIVE={json.dumps(example_objective)}" f"TASK LIST={json.dumps(example_tasklist)}" f"OBJECTIVE={objective}" f"TASK LIST=" ) print("\033[90m\033[3m" + "\nInitializing...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=[ { "role": "system", "content": "You are a task creation AI." }, { "role": "user", "content": prompt } ], temperature=0, max_tokens=1500, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] try: task_list = json.loads(result) self.tasks = task_list except Exception as error: print(error) def execute_task(self, i, task, skill_registry, task_outputs, objective): p_nexttask="\033[92m\033[1m"+"\n*****NEXT TASK ID:"+str(task['id'])+"*****\n"+"\033[0m\033[0m" p_nexttask += f"\033[ EExecuting task {task.get('id')}: {task.get('task')}) [{task.get('skill')}]\033[)" print(p_nexttask) # Retrieve the skill from the registry skill = skill_registry.get_skill(task['skill']) # Get the outputs of the dependent tasks dependent_task_outputs = {dep: task_outputs[dep]["output"] for dep in task['dependent_task_ids']} if 'dependent_task_ids' in task else {} # Execute the skill # print("execute:"+str([task['task'], dependent_task_outputs, objective])) task_output = skill.execute(task['task'], dependent_task_outputs, objective) print("\033[93m\033[1m"+"\nTask Output (ID:"+str(task['id'])+"):"+"\033[0m\033[0m") print("TASK: "+str(task["task"])) print("OUTPUT: "+str(task_output)) return i, task_output def reorder_tasks(self): self.tasks = sorted(self.tasks, key=lambda task: task['id']) def add_task(self, task, after_task_id): # Get the task ids task_ids = [t["id"] for t in self.tasks] # Get the index of the task id to add the new task after insert_index = task_ids.index(after_task_id) + 1 if after_task_id in task_ids else len(task_ids) # Insert the new task self.tasks.insert(insert_index, task) self.reorder_tasks() def update_tasks(self, task_update): for task in self.tasks: if task['id'] == task_update['id']: # This merges the original task dictionary with the update, overwriting only the fields present in the update. task.update(task_update) self.reorder_tasks() def reflect_on_output(self, task_output, skill_descriptions): with self.lock: example = [ [ {"id": 3, "task": "New task 1 description", "skill": "text_completion_skill", "dependent_task_ids": [], "status": "complete"}, {"id": 4, "task": "New task 2 description", "skill": "text_completion_skill", "dependent_task_ids": [], "status": "incomplete"} ], [2, 3], {"id": 5, "task": "Complete the objective and provide a final report", "skill": "text_completion_skill", "dependent_task_ids": [1, 2, 3, 4], "status": "incomplete"} ] prompt = ( f"You are an expert task manager, review the task output to decide at least one new task to add." f"As you add a new task, see if there are any tasks that need to be updated (such as updating dependencies)." f"Use the current task list as reference." f"Do not add duplicate tasks to those in the current task list." f"Only provide JSON as your response without further comments." f"Every new and updated task must include all variables, even they are empty array." f"Dependent IDs must be smaller than the ID of the task." f"New tasks IDs should be no larger than the last task ID." f"Always select at least one skill." f"Task IDs should be unique and in chronological order." f"Do not change the status of complete tasks." f"Only add skills from the AVAILABLE SKILLS, using the exact same spelling." f"Provide your array as a JSON array with double quotes. The first object is new tasks to add as a JSON array, the second array lists the ID numbers where the new tasks should be added after (number of ID numbers matches array), and the third object provides the tasks that need to be updated." f"Make sure to keep dependent_task_ids key, even if an empty array." f"AVAILABLE SKILLS: {skill_descriptions}.###" f"\n###Here is the last task output: {task_output}" f"\n###Here is the current task list: {self.tasks}" f"\n###EXAMPLE OUTPUT FORMAT = {json.dumps(example)}" f"\n###OUTPUT = " ) print("\033[90m\033[3m" + "\nReflecting on task output to generate new tasks if necessary...\n" + "\033[0m") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k-0613", messages=[ { "role": "system", "content": "You are a task creation AI." }, { "role": "user", "content": prompt } ], temperature=0.7, max_tokens=1500, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract the content of the assistant's response and parse it as JSON result = response["choices"][0]["message"]["content"] print("\n#" + str(result)) # Check if the returned result has the expected structure if isinstance(result, str): try: task_list = json.loads(result) # print("RESULT:") print(task_list) # return [],[],[] return task_list[0], task_list[1], task_list[2] except Exception as error: print(error) else: raise ValueError("Invalid task list structure in the output") def get_tasks(self): """ Returns the current list of tasks. Returns: list: the list of tasks. """ return self.tasks def get_task(self, task_id): """ Returns a task given its task_id. Parameters: task_id : int The unique ID of the task. Returns: dict The task that matches the task_id. """ matching_tasks = [task for task in self.tasks if task["id"] == task_id] if matching_tasks: return matching_tasks[0] else: print(f"No task found with id {task_id}") return None def print_tasklist(self, task_list): p_tasklist="\033[95m\033[1m" + "\n*****TASK LIST*****\n" + "\033[0m" for t in task_list: dependent_task_ids = t.get('dependent_task_ids', []) dependent_task = "" if dependent_task_ids: dependent_task = f"\033[31m<dependencies: {', '.join([f'#{dep_id}' for dep_id in dependent_task_ids])}>\033[0m" status_color = "\033[32m" if t.get('status') == "completed" else "\033[31m" p_tasklist+= f"\033[1m{t.get('id')}\033[0m: {t.get('task')} {status_color}[{t.get('status')}]\033[0m \033[93m[{t.get('skill')}] {dependent_task}\033[0m\n" print(p_tasklist) class ExampleObjectivesLoader: def __init__(self, objectives_folder_path): self.objectives_folder_path = objectives_folder_path self.objectives_examples = [] # Initialize as an empty list def load_objectives_examples(self): self.objectives_examples = [] for filename in os.listdir(self.objectives_folder_path): file_path = os.path.join(self.objectives_folder_path, filename) with open(file_path, 'r') as file: objectives = json.load(file) self.objectives_examples.extend(objectives) def find_most_relevant_objective(self, user_input): user_input_embedding = self.get_embedding(user_input, model='text-embedding-ada-002') most_relevant_objective = max( self.objectives_examples, key=lambda pair: self.cosine_similarity(pair['objective'], user_input_embedding) ) return most_relevant_objective['objective'], most_relevant_objective['examples'] def get_embedding(self, text, model='text-embedding-ada-002'): response = openai.Embedding.create(input=[text], model=model) embedding = response['data'][0]['embedding'] return embedding def cosine_similarity(self, objective, embedding): max_similarity = float('-inf') objective_embedding = self.get_embedding(objective, model='text-embedding-ada-002') similarity = self.calculate_similarity(objective_embedding, embedding) max_similarity = max(max_similarity, similarity) return max_similarity def calculate_similarity(self, embedding1, embedding2): embedding1 = np.array(embedding1, dtype=np.float32) embedding2 = np.array(embedding2, dtype=np.float32) similarity = np.dot(embedding1, embedding2) / (np.linalg.norm(embedding1) * np.linalg.norm(embedding2)) return similarity def load_example_objectives(self, user_objective): self.load_objectives_examples() most_relevant_objective, most_relevant_tasklist = self.find_most_relevant_objective(user_objective) example_objective = most_relevant_objective example_tasklist = most_relevant_tasklist return example_objective, example_tasklist
[ "Always select at least one skill.", "TASK LIST=", "Provide your array as a JSON array with double quotes. The first object is new tasks to add as a JSON array, the second array lists the ID numbers where the new tasks should be added after (number of ID numbers matches array), and the third object provides the tasks that need to be updated.", "\n###OUTPUT = ", "Use the current task list as reference.", "Dependent IDs must be smaller than the ID of the task.", "Make sure all task IDs are in chronological order.###\n", "AVAILABLE SKILLS: PLACEHOLDER.###", "Only add skills from the AVAILABLE SKILLS, using the exact same spelling.", "Make sure to keep dependent_task_ids key, even if an empty array.", "As you add a new task, see if there are any tasks that need to be updated (such as updating dependencies).", "Create a very short task list based on the objective, the final output of the last task will be provided back to the user. Limit tasks types to those that can be completed with the available skills listed below. Task description should be detailed.###", "Do not change the status of complete tasks.", "Do not add duplicate tasks to those in the current task list.", "New tasks IDs should be no larger than the last task ID.", "You are an expert task manager, review the task output to decide at least one new task to add.", "Always include one skill.", "Task IDs should be unique and in chronological order.", "You are an expert task list creation AI tasked with creating a list of tasks as a JSON array, considering the ultimate objective of your team: PLACEHOLDER. ", "OBJECTIVE=PLACEHOLDER", "\n###Here is the last task output: PLACEHOLDER", "Do not use skills that are not listed.", "You are a task creation AI.", "Every new and updated task must include all variables, even they are empty array.", "dependent_task_ids should always be an empty array, or an array of numbers representing the task ID it should pull results from.", "Only provide JSON as your response without further comments." ]
2024-01-10
glencode/babyagi_git
babyagi.py
#!/usr/bin/env python3 from dotenv import load_dotenv # Load default environment variables (.env) load_dotenv() import os import time import logging from collections import deque from typing import Dict, List import importlib import openai import chromadb import tiktoken as tiktoken from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction from chromadb.api.types import Documents, EmbeddingFunction, Embeddings import re # default opt out of chromadb telemetry. from chromadb.config import Settings client = chromadb.Client(Settings(anonymized_telemetry=False)) # Engine configuration # Model: GPT, LLAMA, HUMAN, etc. LLM_MODEL = os.getenv("LLM_MODEL", os.getenv("OPENAI_API_MODEL", "gpt-3.5-turbo")).lower() # API Keys OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "") if not (LLM_MODEL.startswith("llama") or LLM_MODEL.startswith("human")): assert OPENAI_API_KEY, "\033[91m\033[1m" + "OPENAI_API_KEY environment variable is missing from .env" + "\033[0m\033[0m" # Table config RESULTS_STORE_NAME = os.getenv("RESULTS_STORE_NAME", os.getenv("TABLE_NAME", "")) assert RESULTS_STORE_NAME, "\033[91m\033[1m" + "RESULTS_STORE_NAME environment variable is missing from .env" + "\033[0m\033[0m" # Run configuration INSTANCE_NAME = os.getenv("INSTANCE_NAME", os.getenv("BABY_NAME", "BabyAGI")) COOPERATIVE_MODE = "none" JOIN_EXISTING_OBJECTIVE = False # Goal configuration OBJECTIVE = os.getenv("OBJECTIVE", "") INITIAL_TASK = os.getenv("INITIAL_TASK", os.getenv("FIRST_TASK", "")) # Model configuration OPENAI_TEMPERATURE = float(os.getenv("OPENAI_TEMPERATURE", 0.0)) # Extensions support begin def can_import(module_name): try: importlib.import_module(module_name) return True except ImportError: return False DOTENV_EXTENSIONS = os.getenv("DOTENV_EXTENSIONS", "").split(" ") # Command line arguments extension # Can override any of the above environment variables ENABLE_COMMAND_LINE_ARGS = ( os.getenv("ENABLE_COMMAND_LINE_ARGS", "false").lower() == "true" ) if ENABLE_COMMAND_LINE_ARGS: if can_import("extensions.argparseext"): from extensions.argparseext import parse_arguments OBJECTIVE, INITIAL_TASK, LLM_MODEL, DOTENV_EXTENSIONS, INSTANCE_NAME, COOPERATIVE_MODE, JOIN_EXISTING_OBJECTIVE = parse_arguments() # Human mode extension # Gives human input to babyagi if LLM_MODEL.startswith("human"): if can_import("extensions.human_mode"): from extensions.human_mode import user_input_await # Load additional environment variables for enabled extensions # TODO: This might override the following command line arguments as well: # OBJECTIVE, INITIAL_TASK, LLM_MODEL, INSTANCE_NAME, COOPERATIVE_MODE, JOIN_EXISTING_OBJECTIVE if DOTENV_EXTENSIONS: if can_import("extensions.dotenvext"): from extensions.dotenvext import load_dotenv_extensions load_dotenv_extensions(DOTENV_EXTENSIONS) # TODO: There's still work to be done here to enable people to get # defaults from dotenv extensions, but also provide command line # arguments to override them # Extensions support end print("\033[95m\033[1m" + "\n*****CONFIGURATION*****\n" + "\033[0m\033[0m") print(f"Name : {INSTANCE_NAME}") print(f"Mode : {'alone' if COOPERATIVE_MODE in ['n', 'none'] else 'local' if COOPERATIVE_MODE in ['l', 'local'] else 'distributed' if COOPERATIVE_MODE in ['d', 'distributed'] else 'undefined'}") print(f"LLM : {LLM_MODEL}") # Check if we know what we are doing assert OBJECTIVE, "\033[91m\033[1m" + "OBJECTIVE environment variable is missing from .env" + "\033[0m\033[0m" assert INITIAL_TASK, "\033[91m\033[1m" + "INITIAL_TASK environment variable is missing from .env" + "\033[0m\033[0m" LLAMA_MODEL_PATH = os.getenv("LLAMA_MODEL_PATH", "models/llama-2-7b.Q3_K_S.gguf") if LLM_MODEL.startswith("llama"): if can_import("llama_cpp"): from llama_cpp import Llama print(f"LLAMA : {LLAMA_MODEL_PATH}" + "\n") assert os.path.exists(LLAMA_MODEL_PATH), "\033[91m\033[1m" + f"Model can't be found." + "\033[0m\033[0m" CTX_MAX = 1024 LLAMA_THREADS_NUM = int(os.getenv("LLAMA_THREADS_NUM", 8)) print('Initialize model for evaluation') llm = Llama( model_path=LLAMA_MODEL_PATH, n_ctx=CTX_MAX, n_threads=LLAMA_THREADS_NUM, n_batch=512, use_mlock=False, ) print('\nInitialize model for embedding') llm_embed = Llama( model_path=LLAMA_MODEL_PATH, n_ctx=CTX_MAX, n_threads=LLAMA_THREADS_NUM, n_batch=512, embedding=True, use_mlock=False, ) print( "\033[91m\033[1m" + "\n*****USING LLAMA.CPP. POTENTIALLY SLOW.*****" + "\033[0m\033[0m" ) else: print( "\033[91m\033[1m" + "\nLlama LLM requires package llama-cpp. Falling back to GPT-3.5-turbo." + "\033[0m\033[0m" ) LLM_MODEL = "gpt-3.5-turbo" if LLM_MODEL.startswith("gpt-4"): print( "\033[91m\033[1m" + "\n*****USING GPT-4. POTENTIALLY EXPENSIVE. MONITOR YOUR COSTS*****" + "\033[0m\033[0m" ) if LLM_MODEL.startswith("human"): print( "\033[91m\033[1m" + "\n*****USING HUMAN INPUT*****" + "\033[0m\033[0m" ) print("\033[94m\033[1m" + "\n*****OBJECTIVE*****\n" + "\033[0m\033[0m") print(f"{OBJECTIVE}") if not JOIN_EXISTING_OBJECTIVE: print("\033[93m\033[1m" + "\nInitial task:" + "\033[0m\033[0m" + f" {INITIAL_TASK}") else: print("\033[93m\033[1m" + f"\nJoining to help the objective" + "\033[0m\033[0m") # Configure OpenAI openai.api_key = OPENAI_API_KEY # Llama embedding function class LlamaEmbeddingFunction(EmbeddingFunction): def __init__(self): return def __call__(self, texts: Documents) -> Embeddings: embeddings = [] for t in texts: e = llm_embed.embed(t) embeddings.append(e) return embeddings # Results storage using local ChromaDB class DefaultResultsStorage: def __init__(self): logging.getLogger('chromadb').setLevel(logging.ERROR) # Create Chroma collection chroma_persist_dir = "chroma" chroma_client = chromadb.PersistentClient( settings=chromadb.config.Settings( persist_directory=chroma_persist_dir, ) ) metric = "cosine" if LLM_MODEL.startswith("llama"): embedding_function = LlamaEmbeddingFunction() else: embedding_function = OpenAIEmbeddingFunction(api_key=OPENAI_API_KEY) self.collection = chroma_client.get_or_create_collection( name=RESULTS_STORE_NAME, metadata={"hnsw:space": metric}, embedding_function=embedding_function, ) def add(self, task: Dict, result: str, result_id: str): # Break the function if LLM_MODEL starts with "human" (case-insensitive) if LLM_MODEL.startswith("human"): return # Continue with the rest of the function embeddings = llm_embed.embed(result) if LLM_MODEL.startswith("llama") else None if ( len(self.collection.get(ids=[result_id], include=[])["ids"]) > 0 ): # Check if the result already exists self.collection.update( ids=result_id, embeddings=embeddings, documents=result, metadatas={"task": task["task_name"], "result": result}, ) else: self.collection.add( ids=result_id, embeddings=embeddings, documents=result, metadatas={"task": task["task_name"], "result": result}, ) def query(self, query: str, top_results_num: int) -> List[dict]: count: int = self.collection.count() if count == 0: return [] results = self.collection.query( query_texts=query, n_results=min(top_results_num, count), include=["metadatas"] ) return [item["task"] for item in results["metadatas"][0]] # Initialize results storage def try_weaviate(): WEAVIATE_URL = os.getenv("WEAVIATE_URL", "") WEAVIATE_USE_EMBEDDED = os.getenv("WEAVIATE_USE_EMBEDDED", "False").lower() == "true" if (WEAVIATE_URL or WEAVIATE_USE_EMBEDDED) and can_import("extensions.weaviate_storage"): WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY", "") from extensions.weaviate_storage import WeaviateResultsStorage print("\nUsing results storage: " + "\033[93m\033[1m" + "Weaviate" + "\033[0m\033[0m") return WeaviateResultsStorage(OPENAI_API_KEY, WEAVIATE_URL, WEAVIATE_API_KEY, WEAVIATE_USE_EMBEDDED, LLM_MODEL, LLAMA_MODEL_PATH, RESULTS_STORE_NAME, OBJECTIVE) return None def try_pinecone(): PINECONE_API_KEY = os.getenv("PINECONE_API_KEY", "") if PINECONE_API_KEY and can_import("extensions.pinecone_storage"): PINECONE_ENVIRONMENT = os.getenv("PINECONE_ENVIRONMENT", "") assert ( PINECONE_ENVIRONMENT ), "\033[91m\033[1m" + "PINECONE_ENVIRONMENT environment variable is missing from .env" + "\033[0m\033[0m" from extensions.pinecone_storage import PineconeResultsStorage print("\nUsing results storage: " + "\033[93m\033[1m" + "Pinecone" + "\033[0m\033[0m") return PineconeResultsStorage(OPENAI_API_KEY, PINECONE_API_KEY, PINECONE_ENVIRONMENT, LLM_MODEL, LLAMA_MODEL_PATH, RESULTS_STORE_NAME, OBJECTIVE) return None def use_chroma(): print("\nUsing results storage: " + "\033[93m\033[1m" + "Chroma (Default)" + "\033[0m\033[0m") return DefaultResultsStorage() results_storage = try_weaviate() or try_pinecone() or use_chroma() # Task storage supporting only a single instance of BabyAGI class SingleTaskListStorage: def __init__(self): self.tasks = deque([]) self.task_id_counter = 0 def append(self, task: Dict): self.tasks.append(task) def replace(self, tasks: List[Dict]): self.tasks = deque(tasks) def popleft(self): return self.tasks.popleft() def is_empty(self): return False if self.tasks else True def next_task_id(self): self.task_id_counter += 1 return self.task_id_counter def get_task_names(self): return [t["task_name"] for t in self.tasks] # Initialize tasks storage tasks_storage = SingleTaskListStorage() if COOPERATIVE_MODE in ['l', 'local']: if can_import("extensions.ray_tasks"): import sys from pathlib import Path sys.path.append(str(Path(__file__).resolve().parent)) from extensions.ray_tasks import CooperativeTaskListStorage tasks_storage = CooperativeTaskListStorage(OBJECTIVE) print("\nReplacing tasks storage: " + "\033[93m\033[1m" + "Ray" + "\033[0m\033[0m") elif COOPERATIVE_MODE in ['d', 'distributed']: pass def limit_tokens_from_string(string: str, model: str, limit: int) -> str: """Limits the string to a number of tokens (estimated).""" try: encoding = tiktoken.encoding_for_model(model) except: encoding = tiktoken.encoding_for_model('gpt2') # Fallback for others. encoded = encoding.encode(string) return encoding.decode(encoded[:limit]) def openai_call( prompt: str, model: str = LLM_MODEL, temperature: float = OPENAI_TEMPERATURE, max_tokens: int = 100, ): while True: try: if model.lower().startswith("llama"): result = llm(prompt[:CTX_MAX], stop=["### Human"], echo=False, temperature=0.2, top_k=40, top_p=0.95, repeat_penalty=1.05, max_tokens=200) # print('\n*****RESULT JSON DUMP*****\n') # print(json.dumps(result)) # print('\n') return result['choices'][0]['text'].strip() elif model.lower().startswith("human"): return user_input_await(prompt) elif not model.lower().startswith("gpt-"): # Use completion API response = openai.Completion.create( engine=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=1, frequency_penalty=0, presence_penalty=0, ) return response.choices[0].text.strip() else: # Use 4000 instead of the real limit (4097) to give a bit of wiggle room for the encoding of roles. # TODO: different limits for different models. trimmed_prompt = limit_tokens_from_string(prompt, model, 4000 - max_tokens) # Use chat completion API messages = [{"role": "system", "content": trimmed_prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, n=1, stop=None, ) return response.choices[0].message.content.strip() except openai.error.RateLimitError: print( " *** The OpenAI API rate limit has been exceeded. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again except openai.error.Timeout: print( " *** OpenAI API timeout occurred. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again except openai.error.APIError: print( " *** OpenAI API error occurred. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again except openai.error.APIConnectionError: print( " *** OpenAI API connection error occurred. Check your network settings, proxy configuration, SSL certificates, or firewall rules. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again except openai.error.InvalidRequestError: print( " *** OpenAI API invalid request. Check the documentation for the specific API method you are calling and make sure you are sending valid and complete parameters. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again except openai.error.ServiceUnavailableError: print( " *** OpenAI API service unavailable. Waiting 10 seconds and trying again. ***" ) time.sleep(10) # Wait 10 seconds and try again else: break def task_creation_agent( objective: str, result: Dict, task_description: str, task_list: List[str] ): prompt = f""" You are to use the result from an execution agent to create new tasks with the following objective: {objective}. The last completed task has the result: \n{result["data"]} This result was based on this task description: {task_description}.\n""" if task_list: prompt += f"These are incomplete tasks: {', '.join(task_list)}\n" prompt += "Based on the result, return a list of tasks to be completed in order to meet the objective. " if task_list: prompt += "These new tasks must not overlap with incomplete tasks. " prompt += """ Return one task per line in your response. The result must be a numbered list in the format: #. First task #. Second task The number of each entry must be followed by a period. If your list is empty, write "There are no tasks to add at this time." Unless your list is empty, do not include any headers before your numbered list or follow your numbered list with any other output.""" print(f'\n*****TASK CREATION AGENT PROMPT****\n{prompt}\n') response = openai_call(prompt, max_tokens=2000) print(f'\n****TASK CREATION AGENT RESPONSE****\n{response}\n') new_tasks = response.split('\n') new_tasks_list = [] for task_string in new_tasks: task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = ''.join(s for s in task_parts[0] if s.isnumeric()) task_name = re.sub(r'[^\w\s_]+', '', task_parts[1]).strip() if task_name.strip() and task_id.isnumeric(): new_tasks_list.append(task_name) # print('New task created: ' + task_name) out = [{"task_name": task_name} for task_name in new_tasks_list] return out def prioritization_agent(): task_names = tasks_storage.get_task_names() bullet_string = '\n' prompt = f""" You are tasked with prioritizing the following tasks: {bullet_string + bullet_string.join(task_names)} Consider the ultimate objective of your team: {OBJECTIVE}. Tasks should be sorted from highest to lowest priority, where higher-priority tasks are those that act as pre-requisites or are more essential for meeting the objective. Do not remove any tasks. Return the ranked tasks as a numbered list in the format: #. First task #. Second task The entries must be consecutively numbered, starting with 1. The number of each entry must be followed by a period. Do not include any headers before your ranked list or follow your list with any other output.""" print(f'\n****TASK PRIORITIZATION AGENT PROMPT****\n{prompt}\n') response = openai_call(prompt, max_tokens=2000) print(f'\n****TASK PRIORITIZATION AGENT RESPONSE****\n{response}\n') if not response: print('Received empty response from priotritization agent. Keeping task list unchanged.') return new_tasks = response.split("\n") if "\n" in response else [response] new_tasks_list = [] for task_string in new_tasks: task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = ''.join(s for s in task_parts[0] if s.isnumeric()) task_name = re.sub(r'[^\w\s_]+', '', task_parts[1]).strip() if task_name.strip(): new_tasks_list.append({"task_id": task_id, "task_name": task_name}) return new_tasks_list # Execute a task based on the objective and five previous tasks def execution_agent(objective: str, task: str) -> str: """ Executes a task based on the given objective and previous context. Args: objective (str): The objective or goal for the AI to perform the task. task (str): The task to be executed by the AI. Returns: str: The response generated by the AI for the given task. """ context = context_agent(query=objective, top_results_num=5) # print("\n****RELEVANT CONTEXT****\n") # print(context) # print('') prompt = f'Perform one task based on the following objective: {objective}.\n' if context: prompt += 'Take into account these previously completed tasks:' + '\n'.join(context) prompt += f'\nYour task: {task}\nResponse:' return openai_call(prompt, max_tokens=2000) # Get the top n completed tasks for the objective def context_agent(query: str, top_results_num: int): """ Retrieves context for a given query from an index of tasks. Args: query (str): The query or objective for retrieving context. top_results_num (int): The number of top results to retrieve. Returns: list: A list of tasks as context for the given query, sorted by relevance. """ results = results_storage.query(query=query, top_results_num=top_results_num) # print("****RESULTS****") # print(results) return results # Add the initial task if starting new objective if not JOIN_EXISTING_OBJECTIVE: initial_task = { "task_id": tasks_storage.next_task_id(), "task_name": INITIAL_TASK } tasks_storage.append(initial_task) def main(): loop = True while loop: # As long as there are tasks in the storage... if not tasks_storage.is_empty(): # Print the task list print("\033[95m\033[1m" + "\n*****TASK LIST*****\n" + "\033[0m\033[0m") for t in tasks_storage.get_task_names(): print(" • " + str(t)) # Step 1: Pull the first incomplete task task = tasks_storage.popleft() print("\033[92m\033[1m" + "\n*****NEXT TASK*****\n" + "\033[0m\033[0m") print(str(task["task_name"])) # Send to execution function to complete the task based on the context result = execution_agent(OBJECTIVE, str(task["task_name"])) print("\033[93m\033[1m" + "\n*****TASK RESULT*****\n" + "\033[0m\033[0m") print(result) # Step 2: Enrich result and store in the results storage # This is where you should enrich the result if needed enriched_result = { "data": result } # extract the actual result from the dictionary # since we don't do enrichment currently # vector = enriched_result["data"] result_id = f"result_{task['task_id']}" results_storage.add(task, result, result_id) # Step 3: Create new tasks and re-prioritize task list # only the main instance in cooperative mode does that new_tasks = task_creation_agent( OBJECTIVE, enriched_result, task["task_name"], tasks_storage.get_task_names(), ) print('Adding new tasks to task_storage') for new_task in new_tasks: new_task.update({"task_id": tasks_storage.next_task_id()}) print(str(new_task)) tasks_storage.append(new_task) if not JOIN_EXISTING_OBJECTIVE: prioritized_tasks = prioritization_agent() if prioritized_tasks: tasks_storage.replace(prioritized_tasks) # Sleep a bit before checking the task list again time.sleep(5) else: print('Done.') loop = False if __name__ == "__main__": main()
[ "\n", "Take into account these previously completed tasks:", "\nYour task: PLACEHOLDER\nResponse:", "Perform one task based on the following objective: PLACEHOLDER.\n", "Based on the result, return a list of tasks to be completed in order to meet the objective. ", "\nYou are to use the result from an execution agent to create new tasks with the following objective: PLACEHOLDER.\nThe last completed task has the result: \nPLACEHOLDER\nThis result was based on this task description: PLACEHOLDER.\n", ", ", "These new tasks must not overlap with incomplete tasks. ", "\nReturn one task per line in your response. The result must be a numbered list in the format:\n\n#. First task\n#. Second task\n\nThe number of each entry must be followed by a period. If your list is empty, write \"There are no tasks to add at this time.\"\nUnless your list is empty, do not include any headers before your numbered list or follow your numbered list with any other output." ]
2024-01-10
sv2441/LLM-Risk-Assessment-Engine
pages~1_UseCaseCollection.py
import streamlit as st import pandas as pd import base64 import csv import math import docx import os from langchain.output_parsers import OutputFixingParser from langchain.llms import OpenAI from langchain.chains import LLMChain, SimpleSequentialChain from langchain.prompts import PromptTemplate, ChatPromptTemplate from langchain.chat_models import ChatOpenAI from langchain.output_parsers import ResponseSchema, StructuredOutputParser from dotenv import load_dotenv from langchain.callbacks import get_openai_callback from langchain.llms import OpenAI load_dotenv() os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"] # os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY') chat_llm = ChatOpenAI(temperature=0.0) # # Determine the execution environment (development or production) is_dev = os.getenv("IS_DEV", "false").lower() == "true" data_path = "data" if is_dev else "/data" doc = docx.Document() def read_docx(file_path): """ Read the content of a docx file and return it as a text string. """ doc = docx.Document(file_path) full_text = [] for para in doc.paragraphs: full_text.append(para.text) return '\n'.join(full_text) file_path = r'data/Questions to ask summary.docx' document_text = read_docx(file_path) questions_to_ask_summary= document_text def generate_questions(use_case,questions_to_ask_summary): title_template = """ Ask about the following in questions form questions based on the "{usecase}" provided. Each Queston should have its in deatil description related to the "{usecase}"Given . Use "{questions_to_ask_summary}" document as a knowledge base for Generation of Question and in detail description related to "{usecase}".  1) Nature of Use Case: 2) Number of User Interactions: 3) Purpose of Use Case: 1) Intended User Group: 2) Sensitivity of Use Case and Data: 1) Nature of LLMs Used: 2) Embedding Approach: 3) Vector Stores: 4) Prompting Approach: 5) Fine-Tuning Approach: 6) Type of Evaluations: 7) Guardrails: 8) Monitoring Approach: 9) Deployment Model: 10) Humans in the Loop: 11) Logging and Feedback Mechanism: """ prompt = ChatPromptTemplate.from_template(template=title_template) messages = prompt.format_messages(usecase=use_case,questions_to_ask_summary=questions_to_ask_summary) response = chat_llm(messages) doc.add_paragraph("Questions:") doc.add_paragraph(response.content) questions=response.content st.write(questions) answers = st.text_input('Write your use case based on question asked') if st.button('Save'): doc.add_paragraph("Answers:") doc.add_paragraph(answers) st.write("Go to Next page For generation of Risk and Actionables") return doc.save('questions_and_answers.docx') # Streamlit UI layout st.title('LLM Risk Assessment Engine :S1') st.subheader('Specializes in providing preliminary risk indicators for any use case') # Text input for use case use_case = st.text_input('Write your use case') # Submit button for the use case if st.button('Generate Questions'): st.write("Generating Questions") questions=generate_questions(use_case,questions_to_ask_summary) st.write(questions)
[ "\n Ask about the following in questions form questions based on the \"{usecase}\" provided.\n Each Queston should have its in deatil description related to the \"{usecase}\"Given . \n Use \"{questions_to_ask_summary}\" document as a knowledge base for Generation of Question and in detail description related to \"{usecase}\". \n \n 1) Nature of Use Case: \n 2) Number of User Interactions:\n 3) Purpose of Use Case: \n 1) Intended User Group:\n 2) Sensitivity of Use Case and Data: \n 1) Nature of LLMs Used:\n 2) Embedding Approach: \n 3) Vector Stores:\n 4) Prompting Approach:\n 5) Fine-Tuning Approach:\n 6) Type of Evaluations: \n 7) Guardrails:\n 8) Monitoring Approach:\n 9) Deployment Model:\n 10) Humans in the Loop:\n 11) Logging and Feedback Mechanism:\n ", "{questions_to_ask_summary}" ]
2024-01-10
sv2441/LLM-Risk-Assessment-Engine
pages~2_Document_Generater.py
import streamlit as st import pandas as pd import base64 from datetime import datetime import csv import math import docx import os from langchain.output_parsers import OutputFixingParser from langchain.llms import OpenAI from langchain.chains import LLMChain, SimpleSequentialChain from langchain.prompts import PromptTemplate, ChatPromptTemplate from langchain.chat_models import ChatOpenAI from langchain.output_parsers import ResponseSchema, StructuredOutputParser from dotenv import load_dotenv from langchain.callbacks import get_openai_callback from langchain.llms import OpenAI load_dotenv() os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"] # os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY') chat_llm = ChatOpenAI(temperature=0.0) def read_docx(file_path): """ Read the content of a docx file and return it as a text string. """ doc = docx.Document(file_path) full_text = [] for para in doc.paragraphs: full_text.append(para.text) return '\n'.join(full_text) def save_string_to_docx(text, filename): doc = docx.Document() doc.add_paragraph(text) try: doc.save(filename) print(f'Saved to {filename}') except Exception as e: print(f'Error: {str(e)}') def generate_risk_rank(): doc = docx.Document() file_path = r'questions_and_answers.docx' questions_and_answers = read_docx(file_path) doc.add_paragraph("Answers:") doc.add_paragraph(questions_and_answers) file_path = r'data/Documented nature of risks.docx' Documented_nature_of_risks= read_docx(file_path) title_template = """ Identify the risks that apply to the "{questions_and_answers}". Use the information in "{Documented_nature_of_risks}" document to identify the applicable risks. Provide atleast 1 or 2 examples of each risk using the use case brief and the user responses to the questions in "{questions_and_answers}" """ prompt = ChatPromptTemplate.from_template(template=title_template) messages = prompt.format_messages(questions_and_answers=questions_and_answers,Documented_nature_of_risks=Documented_nature_of_risks) response2 = chat_llm(messages) risk_information=response2.content doc.add_paragraph("Key Risks:") doc.add_paragraph(risk_information) title_template = """ Rank the "{risk_information}" in terms of priority and provide a criticality score as high/ medium/ low given for "{questions_and_answers}". It should have Criticality Score and Reason for the above "{risk_information}". """ prompt = ChatPromptTemplate.from_template(template=title_template) messages = prompt.format_messages(risk_information=risk_information,questions_and_answers=questions_and_answers) response3 = chat_llm(messages) risk_ranking=response3.content doc.add_paragraph("Risk Ranking:") doc.add_paragraph(risk_ranking) title_template = """Provide Actionable steps for governance to address each identified risk for "{risk_ranking}". For each risk compile a set of actionables to address the "{risk_ranking}". These actionables shall be governance actionables. """ prompt = ChatPromptTemplate.from_template(template=title_template) messages = prompt.format_messages(risk_ranking=risk_ranking) response4 = chat_llm(messages) Actionables=response4.content doc.add_paragraph("Actionables:") doc.add_paragraph(Actionables) doc.add_paragraph(Actionables) summary = "" for paragraph in doc.paragraphs: # Here you define the logic to decide which paragraphs to include in the summary summary += paragraph.text + "\n" title_template = """Compile All information in "{summary}" . and Sturcture in below Format The document shall contain the following information: Section A: Brief about the use case . Section B: List of high-level risks associated with the use case. Section C: Table containing key risks with their risk ranking along with the reasons for the risk ranking. Section D: List of actionables for each risk listed in Section C. """ prompt = ChatPromptTemplate.from_template(template=title_template) messages = prompt.format_messages(summary=summary) response2 = chat_llm(messages) final_document=response2.content st.write(final_document) filename = "Final_document.docx" result=save_string_to_docx(final_document, filename) with open('Final_document.docx', 'rb') as f: doc_data = f.read() b64 = base64.b64encode(doc_data).decode() href = f'<a href="data:application/octet-stream;base64,{b64}" download="Ai_Risk_Document.docx">Download Result</a>' st.markdown(href, unsafe_allow_html=True) # Streamlit UI layout st.title('LLM Risk Assessment Engine :S2') st.subheader('Specializes in providing preliminary risk indicators for any use case') # Submit button for the use case if st.button('Generate Document'): st.write("Generating Key Risk and Actionables.....") generate_risk_rank() st.write("Download Final Document.....") # generate_document()
[ "Compile All information in \"{summary}\" . and Sturcture in below Format\n The document shall contain the following information: \n Section A: Brief about the use case . \n Section B: List of high-level risks associated with the use case.\n Section C: Table containing key risks with their risk ranking along with the reasons for the risk ranking.\n Section D: List of actionables for each risk listed in Section C.\n ", "\n Identify the risks that apply to the \"{questions_and_answers}\". Use the information in \"{Documented_nature_of_risks}\" document to identify the applicable risks. \n Provide atleast 1 or 2 examples of each risk using the use case brief and the user responses to the questions in \"{questions_and_answers}\"\n ", "Provide Actionable steps for governance to address each identified risk for \"{risk_ranking}\".\n For each risk compile a set of actionables to address the \"{risk_ranking}\". These actionables shall be governance actionables.\n ", "\n Rank the \"{risk_information}\" in terms of priority and provide a criticality score as high/ medium/ low given for \"{questions_and_answers}\".\n It should have Criticality Score and Reason for the above \"{risk_information}\".\n \n " ]
2024-01-10
dmohle/tPythonAIbotWithMemory01
flashWebpageDemo02.py
from flask import Flask, request, render_template import openai import constants text_test = "This is a test" num_words = len(text_test.split(" ")) print(f"Num of words from the test is: {num_words}") # persistent chats with davinci openai.api_key = constants.api_key app = Flask(__name__) # Store the conversation history conversation_history = {} def count_tokens(text): num_words = len(text.split(" ")) add_fifty_percent = len(text.split(" ")) / 2 num_of_tokens = num_words + add_fifty_percent return num_of_tokens @app.route("/", methods=["GET", "POST"]) def start_here(): if request.method == "POST": user_id = request.remote_addr # Simplified user identification (use a better method in production) text_question = request.form.get("question") # Retrieve the existing conversation history, if it exists chat_history = conversation_history.get(user_id, "") # Append the new question to the history chat_history += f"\nHuman: {text_question}\nAI:" request_text = chat_history print(f"Chat history is: {request_text}") # count the tokens # request_tokens = count_tokens(request_text) # Make a request to the OpenAI API using the text-davinci-003 model try: response = openai.Completion.create( engine="text-davinci-003", prompt=chat_history, max_tokens=1500, temperature=0.7, # get three responses. n = 3, stop="\nHuman:" ) response_text = response # Extract one AI response and update the chat history ai_response = response.choices[0].text.strip() chat_history += ai_response ai_response02 = response.choices[1].text.strip() ai_response03 = response.choices[2].text.strip() conversation_history[user_id] = chat_history # response_tokens = count_tokens(response_text) total_tokens = 999 # request_tokens + response_tokens # Send the responses back to the user return render_template("index.html", textQuestion = "", textAnswer = ai_response, textAnswer02 = ai_response02, textAnswer03 = ai_response03, tokenUsage = total_tokens, queryUsage = chat_history ) except openai.error.OpenAIError as e: print(f"OpenAIError occurred: {e.__class__.__name__} - {e}") return render_template("index.html", textQuestion=text_question, textAnswer=f"OpenAIError occurred: {e.__class__.__name__} - {e}") except Exception as e: print(f"Unexpected error: {e.__class__.__name__} - {e}") return render_template("index.html", textQuestion=text_question, textAnswer=f"Unexpected error: {e.__class__.__name__} - {e}") print(f"Request Tokens: {request_tokens}, Response Tokens: {response_tokens}, Total Tokens: {total_tokens}") return render_template("index.html", textQuestion="", textAnswer="") if __name__ == "__main__": app.run(debug=True)
[]
2024-01-10
hate5six/neural-el
models~figer_model~coldStart.py
import os import sys import tensorflow as tf import numpy as np import readers.utils as utils from evaluation import evaluate from evaluation import evaluate_el from evaluation import evaluate_types from models.base import Model from models.figer_model.context_encoder import ContextEncoderModel from models.figer_model.coherence_model import CoherenceModel from models.figer_model.wiki_desc import WikiDescModel from models.figer_model.joint_context import JointContextModel from models.figer_model.labeling_model import LabelingModel from models.figer_model.entity_posterior import EntityPosterior from models.figer_model.loss_optim import LossOptim class ColdStart(object): def __init__(self, figermodel): print("###### ENTERED THE COLD WORLD OF THE UNKNOWN ##############") # Object of the WikiELModel Class self.fm = figermodel self.coldDir = self.fm.reader.coldDir coldWid2DescVecs_pkl = os.path.join(self.coldDir, "coldwid2descvecs.pkl") self.coldWid2DescVecs = utils.load(coldWid2DescVecs_pkl) self.num_cold_entities = self.fm.reader.num_cold_entities self.batch_size = self.fm.batch_size (self.coldwid2idx, self.idx2coldwid) = (self.fm.reader.coldwid2idx, self.fm.reader.idx2coldwid) def _makeDescLossGraph(self): with tf.variable_scope("cold") as s: with tf.device(self.fm.device_placements['gpu']) as d: tf.set_random_seed(1) self.coldEnEmbsToAssign = tf.placeholder( tf.float32, [self.num_cold_entities, 200], name="coldEmbsAssignment") self.coldEnEmbs = tf.get_variable( name="cold_entity_embeddings", shape=[self.num_cold_entities, 200], initializer=tf.random_normal_initializer(mean=-0.25, stddev=1.0/(100.0))) self.assignColdEmbs = self.coldEnEmbs.assign(self.coldEnEmbsToAssign) self.trueColdEnIds = tf.placeholder( tf.int32, [self.batch_size], name="true_entities_idxs") # Should be a list of zeros self.softTrueIdxs = tf.placeholder( tf.int32, [self.batch_size], name="softmaxTrueEnsIdxs") # [B, D] self.trueColdEmb = tf.nn.embedding_lookup( self.coldEnEmbs, self.trueColdEnIds) # [B, 1, D] self.trueColdEmb_exp = tf.expand_dims( input=self.trueColdEmb, dim=1) self.label_scores = tf.matmul(self.trueColdEmb, self.fm.labeling_model.label_weights) self.labeling_losses = tf.nn.sigmoid_cross_entropy_with_logits( logits=self.label_scores, targets=self.fm.labels_batch, name="labeling_loss") self.labelingLoss = tf.reduce_sum( self.labeling_losses) / tf.to_float(self.batch_size) # [B, D] self.descEncoded = self.fm.wikidescmodel.desc_encoded ## Maximize sigmoid of dot-prod between true emb. and desc encoding descLosses = -tf.sigmoid(tf.reduce_sum(tf.mul(self.trueColdEmb, self.descEncoded), 1)) self.descLoss = tf.reduce_sum(descLosses)/tf.to_float(self.batch_size) # L-2 Norm Loss self.trueEmbNormLoss = tf.reduce_sum( tf.square(self.trueColdEmb))/(tf.to_float(self.batch_size)) ''' Concat trueColdEmb_exp to negKnownEmbs so that 0 is the true entity. Dotprod this emb matrix with descEncoded to get scores and apply softmax ''' self.trcoldvars = self.fm.scope_vars_list(scope_name="cold", var_list=tf.trainable_variables()) print("Vars in Training") for var in self.trcoldvars: print(var.name) self.optimizer = tf.train.AdamOptimizer( learning_rate=self.fm.learning_rate, name='AdamCold_') self.total_labeling_loss = self.labelingLoss + self.trueEmbNormLoss self.label_gvs = self.optimizer.compute_gradients( loss=self.total_labeling_loss, var_list=self.trcoldvars) self.labeling_optim_op = self.optimizer.apply_gradients(self.label_gvs) self.total_loss = self.labelingLoss + 100*self.descLoss + self.trueEmbNormLoss self.comb_gvs = self.optimizer.compute_gradients( loss=self.total_loss, var_list=self.trcoldvars) self.combined_optim_op = self.optimizer.apply_gradients(self.comb_gvs) self.allcoldvars = self.fm.scope_vars_list(scope_name="cold", var_list=tf.all_variables()) print("All Vars in Cold") for var in self.allcoldvars: print(var.name) print("Loaded and graph made") ### GRAPH COMPLETE ### ############################################################################# def _trainColdEmbFromTypes(self, epochsToTrain=5): print("Training Cold Entity Embeddings from Typing Info") epochsDone = self.fm.reader.val_epochs while self.fm.reader.val_epochs < epochsToTrain: (left_batch, left_lengths, right_batch, right_lengths, wids_batch, labels_batch, coherence_batch, wid_idxs_batch, wid_cprobs_batch) = self.fm.reader._next_padded_batch(data_type=1) trueColdWidIdxsBatch = [] trueColdWidDescWordVecBatch = [] for wid in wids_batch: trueColdWidIdxsBatch.append(self.coldwid2idx[wid]) trueColdWidDescWordVecBatch.append(self.coldWid2DescVecs[wid]) feed_dict = {self.trueColdEnIds: trueColdWidIdxsBatch, self.fm.labels_batch: labels_batch} fetch_tensor = [self.labelingLoss, self.trueEmbNormLoss] (fetches, _) = self.fm.sess.run([fetch_tensor, self.labeling_optim_op], feed_dict=feed_dict) labelingLoss = fetches[0] trueEmbNormLoss = fetches[1] print("LL : {} NormLoss : {}".format(labelingLoss, trueEmbNormLoss)) newedone = self.fm.reader.val_epochs if newedone > epochsDone: print("Epochs : {}".format(newedone)) epochsDone = newedone ############################################################################# def _trainColdEmbFromTypesAndDesc(self, epochsToTrain=5): print("Training Cold Entity Embeddings from Typing Info") epochsDone = self.fm.reader.val_epochs while self.fm.reader.val_epochs < epochsToTrain: (left_batch, left_lengths, right_batch, right_lengths, wids_batch, labels_batch, coherence_batch, wid_idxs_batch, wid_cprobs_batch) = self.fm.reader._next_padded_batch(data_type=1) trueColdWidIdxsBatch = [] trueColdWidDescWordVecBatch = [] for wid in wids_batch: trueColdWidIdxsBatch.append(self.coldwid2idx[wid]) trueColdWidDescWordVecBatch.append(self.coldWid2DescVecs[wid]) feed_dict = {self.fm.wikidesc_batch: trueColdWidDescWordVecBatch, self.trueColdEnIds: trueColdWidIdxsBatch, self.fm.labels_batch: labels_batch} fetch_tensor = [self.labelingLoss, self.descLoss, self.trueEmbNormLoss] (fetches,_) = self.fm.sess.run([fetch_tensor, self.combined_optim_op], feed_dict=feed_dict) labelingLoss = fetches[0] descLoss = fetches[1] normLoss = fetches[2] print("L : {} D : {} NormLoss : {}".format(labelingLoss, descLoss, normLoss)) newedone = self.fm.reader.val_epochs if newedone > epochsDone: print("Epochs : {}".format(newedone)) epochsDone = newedone ############################################################################# def runEval(self): print("Running Evaluations") self.fm.reader.reset_validation() correct = 0 total = 0 totnew = 0 correctnew = 0 while self.fm.reader.val_epochs < 1: (left_batch, left_lengths, right_batch, right_lengths, wids_batch, labels_batch, coherence_batch, wid_idxs_batch, wid_cprobs_batch) = self.fm.reader._next_padded_batch(data_type=1) trueColdWidIdxsBatch = [] for wid in wids_batch: trueColdWidIdxsBatch.append(self.coldwid2idx[wid]) feed_dict = {self.fm.sampled_entity_ids: wid_idxs_batch, self.fm.left_context_embeddings: left_batch, self.fm.right_context_embeddings: right_batch, self.fm.left_lengths: left_lengths, self.fm.right_lengths: right_lengths, self.fm.coherence_indices: coherence_batch[0], self.fm.coherence_values: coherence_batch[1], self.fm.coherence_matshape: coherence_batch[2], self.trueColdEnIds: trueColdWidIdxsBatch} fetch_tensor = [self.trueColdEmb, self.fm.joint_context_encoded, self.fm.posterior_model.sampled_entity_embeddings, self.fm.posterior_model.entity_scores] fetched_vals = self.fm.sess.run(fetch_tensor, feed_dict=feed_dict) [trueColdEmbs, # [B, D] context_encoded, # [B, D] neg_entity_embeddings, # [B, N, D] neg_entity_scores] = fetched_vals # [B, N] # [B] trueColdWidScores = np.sum(trueColdEmbs*context_encoded, axis=1) entity_scores = neg_entity_scores entity_scores[:,0] = trueColdWidScores context_entity_scores = np.exp(entity_scores)/np.sum(np.exp(entity_scores)) maxIdxs = np.argmax(context_entity_scores, axis=1) for i in range(0, self.batch_size): total += 1 if maxIdxs[i] == 0: correct += 1 scores_withpriors = context_entity_scores + wid_cprobs_batch maxIdxs = np.argmax(scores_withpriors, axis=1) for i in range(0, self.batch_size): totnew += 1 if maxIdxs[i] == 0: correctnew += 1 print("Context T : {} C : {}".format(total, correct)) print("WPriors T : {} C : {}".format(totnew, correctnew)) ############################################################################## def typeBasedColdEmbExp(self, ckptName="FigerModel-20001"): ''' Train cold embeddings using wiki desc loss ''' saver = tf.train.Saver(var_list=tf.all_variables()) print("Loading Model ... ") if ckptName == None: print("Given CKPT Name") sys.exit() else: load_status = self.fm.loadSpecificCKPT( saver=saver, checkpoint_dir=self.fm.checkpoint_dir, ckptName=ckptName, attrs=self.fm._attrs) if not load_status: print("No model to load. Exiting") sys.exit(0) self._makeDescLossGraph() self.fm.sess.run(tf.initialize_variables(self.allcoldvars)) self._trainColdEmbFromTypes(epochsToTrain=5) self.runEval() ############################################################################## def typeAndWikiDescBasedColdEmbExp(self, ckptName="FigerModel-20001"): ''' Train cold embeddings using wiki desc loss ''' saver = tf.train.Saver(var_list=tf.all_variables()) print("Loading Model ... ") if ckptName == None: print("Given CKPT Name") sys.exit() else: load_status = self.fm.loadSpecificCKPT( saver=saver, checkpoint_dir=self.fm.checkpoint_dir, ckptName=ckptName, attrs=self.fm._attrs) if not load_status: print("No model to load. Exiting") sys.exit(0) self._makeDescLossGraph() self.fm.sess.run(tf.initialize_variables(self.allcoldvars)) self._trainColdEmbFromTypesAndDesc(epochsToTrain=5) self.runEval() # EVALUATION FOR COLD START WHEN INITIALIZING COLD EMB FROM WIKI DESC ENCODING def wikiDescColdEmbExp(self, ckptName="FigerModel-20001"): ''' Assign cold entity embeddings as wiki desc encoding ''' assert self.batch_size == 1 print("Loaded Cold Start Class. ") print("Size of cold entities : {}".format(len(self.coldWid2DescVecs))) saver = tf.train.Saver(var_list=tf.all_variables(), max_to_keep=5) print("Loading Model ... ") if ckptName == None: print("Given CKPT Name") sys.exit() else: load_status = self.fm.loadSpecificCKPT( saver=saver, checkpoint_dir=self.fm.checkpoint_dir, ckptName=ckptName, attrs=self.fm._attrs) if not load_status: print("No model to load. Exiting") sys.exit(0) iter_done = self.fm.global_step.eval() print("[#] Model loaded with iterations done: %d" % iter_done) self._makeDescLossGraph() self.fm.sess.run(tf.initialize_variables(self.allcoldvars)) # Fill with encoded desc. in order of idx2coldwid print("Getting Encoded Description Vectors") descEncodedMatrix = [] for idx in range(0, len(self.idx2coldwid)): wid = self.idx2coldwid[idx] desc_vec = self.coldWid2DescVecs[wid] feed_dict = {self.fm.wikidesc_batch: [desc_vec]} desc_encoded = self.fm.sess.run(self.fm.wikidescmodel.desc_encoded, feed_dict=feed_dict) descEncodedMatrix.append(desc_encoded[0]) print("Initialization Experiment") self.runEval() print("Assigning Cold Embeddings from Wiki Desc Encoder ...") self.fm.sess.run(self.assignColdEmbs, feed_dict={self.coldEnEmbsToAssign:descEncodedMatrix}) print("After assigning based on Wiki Encoder") self.runEval() ##############################################################################
[]
2024-01-10
hate5six/neural-el
models~figer_model~el_model.py
import time import tensorflow as tf import numpy as np import random import sys import gc from evaluation import evaluate_inference from evaluation import evaluate_types from models.base import Model from models.figer_model.context_encoder import ContextEncoderModel from models.figer_model.coherence_model import CoherenceModel from models.figer_model.wiki_desc import WikiDescModel from models.figer_model.labeling_model import LabelingModel from models.figer_model.entity_posterior import EntityPosterior from models.figer_model.loss_optim import LossOptim np.set_printoptions(precision=5) class ELModel(Model): """Unsupervised Clustering using Discrete-State VAE""" def __init__(self, sess, reader, dataset, max_steps, pretrain_max_steps, word_embed_dim, context_encoded_dim, context_encoder_lstmsize, context_encoder_num_layers, coherence_numlayers, jointff_numlayers, learning_rate, dropout_keep_prob, reg_constant, checkpoint_dir, optimizer, mode='train', strict=False, pretrain_word_embed=True, typing=True, el=True, coherence=False, textcontext=False, useCNN=False, WDLength=100, Fsize=5, entyping=True): self.optimizer = optimizer self.mode = mode self.sess = sess self.reader = reader # Reader class self.dataset = dataset self.strict = strict self.pretrain_word_embed = pretrain_word_embed assert self.pretrain_word_embed, "Only use pretrained word embeddings" self.typing = typing # Bool - Perform typing self.el = el # Bool - Perform Entity-Linking self.coherence = coherence self.textcontext = textcontext if not (self.coherence or self.textcontext): print("Both textcontext and coherence cannot be False") sys.exit(0) self.useCNN = useCNN self.WDLength = WDLength self.Fsize = Fsize self.entyping = entyping self.max_steps = max_steps # Max num of steps of training to run self.pretrain_max_steps = pretrain_max_steps self.batch_size = reader.batch_size self.reg_constant = reg_constant self.dropout_keep_prob = dropout_keep_prob self.lr = learning_rate # Num of clusters = Number of entities in dataset. self.num_labels = self.reader.num_labels self.num_words = self.reader.num_words self.num_knwn_entities = self.reader.num_knwn_entities self.num_cand_entities = self.reader.num_cands # Size of word embeddings if not self.pretrain_word_embed: self.word_embed_dim = word_embed_dim else: self.word_embed_dim = 300 # Context encoders self.context_encoded_dim = context_encoded_dim self.context_encoder_lstmsize = context_encoder_lstmsize self.context_encoder_num_layers = context_encoder_num_layers # Coherence Encoder self.coherence_numlayers = coherence_numlayers # Joint FeedForward self.jointff_numlayers = jointff_numlayers self.checkpoint_dir = checkpoint_dir self.embeddings_scope = "embeddings" self.word_embed_var_name = "word_embeddings" self.encoder_model_scope = "context_encoder" self.coherence_model_scope = "coherence_encoder" self.wikidesc_model_scope = "wikidesc_encoder" self.joint_context_scope = "joint_context" self.label_model_scope = "labeling_model" self.labeling_loss_scope = "labeling_loss" self.entity_posterior_scope = "en_posterior_model" self.posterior_loss_scope = "en_posterior_loss" self.wikidesc_loss_scope = "wikidesc_loss" self.optim_scope = "labeling_optimization" self._attrs=[ "textcontext", "coherence", "typing", "pretrain_word_embed", "word_embed_dim", "num_words", "num_labels", "num_knwn_entities", "context_encoded_dim", "context_encoder_lstmsize", "context_encoder_num_layers", "coherence_numlayers", "reg_constant", "strict", "lr", "optimizer"] #GPU Allocations self.device_placements = { 'cpu': '/cpu:0', 'gpu': '/gpu:0' } with tf.variable_scope("figer_model") as scope: self.learning_rate = tf.Variable(self.lr, name='learning_rate', trainable=False) self.global_step = tf.Variable(0, name='global_step', trainable=False, dtype=tf.int32) self.increment_global_step_op = self.global_step.assign_add(1) self.build_placeholders() # Encoder Models : Name LSTM, Text FF and Links FF networks with tf.variable_scope(self.encoder_model_scope) as scope: if self.pretrain_word_embed == False: self.left_context_embeddings = tf.nn.embedding_lookup( self.word_embeddings, self.left_batch, name="left_embeddings") self.right_context_embeddings = tf.nn.embedding_lookup( self.word_embeddings, self.right_batch, name="right_embeddings") if self.textcontext: self.context_encoder_model = ContextEncoderModel( num_layers=self.context_encoder_num_layers, batch_size=self.batch_size, lstm_size=self.context_encoder_lstmsize, left_embed_batch=self.left_context_embeddings, left_lengths=self.left_lengths, right_embed_batch=self.right_context_embeddings, right_lengths=self.right_lengths, context_encoded_dim=self.context_encoded_dim, scope_name=self.encoder_model_scope, device=self.device_placements['gpu'], dropout_keep_prob=self.dropout_keep_prob) if self.coherence: self.coherence_model = CoherenceModel( num_layers=self.coherence_numlayers, batch_size=self.batch_size, input_size=self.reader.num_cohstr, coherence_indices=self.coherence_indices, coherence_values=self.coherence_values, coherence_matshape=self.coherence_matshape, context_encoded_dim=self.context_encoded_dim, scope_name=self.coherence_model_scope, device=self.device_placements['gpu'], dropout_keep_prob=self.dropout_keep_prob) if self.coherence and self.textcontext: # [B, 2*context_encoded_dim] joint_context_encoded = tf.concat( 1, [self.context_encoder_model.context_encoded, self.coherence_model.coherence_encoded], name='joint_context_encoded') context_vec_size = 2*self.context_encoded_dim ### WITH FF AFTER CONCAT ########## trans_weights = tf.get_variable( name="joint_trans_weights", shape=[context_vec_size, context_vec_size], initializer=tf.random_normal_initializer( mean=0.0, stddev=1.0/(100.0))) # [B, context_encoded_dim] joint_context_encoded = tf.matmul(joint_context_encoded, trans_weights) self.joint_context_encoded = tf.nn.relu(joint_context_encoded) #################################### elif self.textcontext: self.joint_context_encoded = self.context_encoder_model.context_encoded context_vec_size = self.context_encoded_dim elif self.coherence: self.joint_context_encoded = self.coherence_model.coherence_encoded context_vec_size = self.context_encoded_dim else: print("ERROR:Atleast one of local or " "document context needed.") sys.exit(0) self.posterior_model = EntityPosterior( batch_size=self.batch_size, num_knwn_entities=self.num_knwn_entities, context_encoded_dim=context_vec_size, context_encoded=self.joint_context_encoded, entity_ids=self.sampled_entity_ids, scope_name=self.entity_posterior_scope, device_embeds=self.device_placements['gpu'], device_gpu=self.device_placements['gpu']) self.labeling_model = LabelingModel( batch_size=self.batch_size, num_labels=self.num_labels, context_encoded_dim=context_vec_size, true_entity_embeddings=self.posterior_model.trueentity_embeddings, word_embed_dim=self.word_embed_dim, context_encoded=self.joint_context_encoded, mention_embed=None, scope_name=self.label_model_scope, device=self.device_placements['gpu']) if self.useCNN: self.wikidescmodel = WikiDescModel( desc_batch=self.wikidesc_batch, trueentity_embs=self.posterior_model.trueentity_embeddings, negentity_embs=self.posterior_model.negentity_embeddings, allentity_embs=self.posterior_model.sampled_entity_embeddings, batch_size=self.batch_size, doclength=self.WDLength, wordembeddim=self.word_embed_dim, filtersize=self.Fsize, desc_encoded_dim=context_vec_size, scope_name=self.wikidesc_model_scope, device=self.device_placements['gpu'], dropout_keep_prob=self.dropout_keep_prob) #end - encoder variable scope # Encoder FF Variables + Cluster Embedding self.train_vars = tf.trainable_variables() self.loss_optim = LossOptim(self) ################ end Initialize ############################################# def build_placeholders(self): # Left Context self.left_batch = tf.placeholder( tf.int32, [self.batch_size, None], name="left_batch") self.left_context_embeddings = tf.placeholder( tf.float32, [self.batch_size, None, self.word_embed_dim], name="left_embeddings") self.left_lengths = tf.placeholder( tf.int32, [self.batch_size], name="left_lengths") # Right Context self.right_batch = tf.placeholder( tf.int32, [self.batch_size, None], name="right_batch") self.right_context_embeddings = tf.placeholder( tf.float32, [self.batch_size, None, self.word_embed_dim], name="right_embeddings") self.right_lengths = tf.placeholder( tf.int32, [self.batch_size], name="right_lengths") # Mention Embedding self.mention_embed = tf.placeholder( tf.float32, [self.batch_size, self.word_embed_dim], name="mentions_embed") # Wiki Description Batch self.wikidesc_batch = tf.placeholder( tf.float32, [self.batch_size, self.WDLength, self.word_embed_dim], name="wikidesc_batch") # Labels self.labels_batch = tf.placeholder( tf.float32, [self.batch_size, self.num_labels], name="true_labels") # Candidates, Priors and True Entities Ids self.sampled_entity_ids = tf.placeholder( tf.int32, [self.batch_size, self.num_cand_entities], name="sampled_candidate_entities") self.entity_priors = tf.placeholder( tf.float32, [self.batch_size, self.num_cand_entities], name="entitiy_priors") self.true_entity_ids = tf.placeholder( tf.int32, [self.batch_size], name="true_entities_in_sampled") # Coherence self.coherence_indices = tf.placeholder( tf.int64, [None, 2], name="coherence_indices") self.coherence_values = tf.placeholder( tf.float32, [None], name="coherence_values") self.coherence_matshape = tf.placeholder( tf.int64, [2], name="coherence_matshape") #END-Placeholders if self.pretrain_word_embed == False: with tf.variable_scope(self.embeddings_scope) as s: with tf.device(self.device_placements['cpu']) as d: self.word_embeddings = tf.get_variable( name=self.word_embed_var_name, shape=[self.num_words, self.word_embed_dim], initializer=tf.random_normal_initializer( mean=0.0, stddev=(1.0/100.0))) def training_setup(self): # Make the loss graph print("[#] Making Loss Graph ....") self.loss_optim.make_loss_graph() print("[#] Defining pretraining losses and optimizers ...") self.loss_optim.label_optimization( trainable_vars=self.train_vars, optim_scope=self.optim_scope) print("All Trainable Variables") self.print_variables_in_collection(tf.trainable_variables()) def training(self): self.training_setup() vars_tostore = tf.all_variables() saver = tf.train.Saver(var_list=vars_tostore, max_to_keep=30) # (Try) Load all pretraining model variables # If graph not found - Initialize trainable + optim variables print("Loading pre-saved checkpoint...") load_status = self.load(saver=saver, checkpoint_dir=self.checkpoint_dir, attrs=self._attrs) if not load_status: print("No checkpoint found. Training from scratch") self.sess.run(tf.initialize_variables(vars_tostore)) start_iter = self.global_step.eval() start_time = time.time() print("[#] Pre-Training iterations done: %d" % start_iter) data_loading = 0 tf.get_default_graph().finalize() for iteration in range(start_iter, self.max_steps): dstime = time.time() # GET BATCH (left_batch, left_lengths, right_batch, right_lengths, wikidesc_batch, labels_batch, coherence_batch, wid_idxs_batch, wid_cprobs_batch) = self.reader.next_train_batch() (coh_indices, coh_values, coh_matshape) = coherence_batch dtime = time.time() - dstime data_loading += dtime # FEED DICT feed_dict = {self.wikidesc_batch: wikidesc_batch, self.sampled_entity_ids: wid_idxs_batch, self.true_entity_ids: [0]*self.batch_size, self.entity_priors: wid_cprobs_batch} if self.typing or self.entyping: type_dict = {self.labels_batch: labels_batch} feed_dict.update(type_dict) if self.textcontext: if not self.pretrain_word_embed: context_dict = { self.left_batch: left_batch, self.right_batch: right_batch, self.left_lengths: left_lengths, self.right_lengths: right_lengths} feed_dict.update(context_dict) else: context_dict = { self.left_context_embeddings: left_batch, self.right_context_embeddings: right_batch, self.left_lengths: left_lengths, self.right_lengths: right_lengths} feed_dict.update(context_dict) if self.coherence: coherence_dict = {self.coherence_indices: coherence_batch[0], self.coherence_values: coherence_batch[1], self.coherence_matshape: coherence_batch[2]} feed_dict.update(coherence_dict) # FETCH TENSORS fetch_tensors = [self.loss_optim.labeling_loss, self.labeling_model.label_probs, self.loss_optim.posterior_loss, self.posterior_model.entity_posteriors, self.loss_optim.entity_labeling_loss] if self.useCNN: fetch_tensors.append(self.loss_optim.wikidesc_loss) (fetches_old, _, _) = self.sess.run([fetch_tensors, self.loss_optim.optim_op, self.increment_global_step_op], feed_dict=feed_dict) [old_loss, old_label_sigms, old_post_loss, old_posts, enLabelLoss] = fetches_old[0:5] if self.useCNN: [oldCNNLoss, trueCosDis, negCosDis] = [fetches_old[5], 0.0, 0.0] else: [oldCNNLoss, trueCosDis, negCosDis] = [0.0, 0.0, 0.0] ''' fetches_new = self.sess.run(fetch_tensors, feed_dict=feed_dict) [new_loss, new_label_sigms, new_post_loss, new_posts] = fetches_new ''' if iteration % 100 == 0: # [B, L] old_corr_preds, old_precision = evaluate.strict_pred( labels_batch, old_label_sigms) context_preds = evaluate.correct_context_prediction( old_posts, self.batch_size) print("Iter %2d, Epoch %d, T %4.2f secs, " "Labeling Loss %.3f, EnLabelLoss %.3f" % (iteration, self.reader.tr_epochs, time.time() - start_time, old_loss, enLabelLoss)) print("Old Posterior Loss : {0:.3f}, CNN Loss: {1:.3f} " "TrueCos: {2:.3f} NegCos: {3:.3f}".format( old_post_loss, oldCNNLoss, trueCosDis, negCosDis)) print("[OLD] Num of strict correct predictions : {}, {}".format( old_corr_preds, old_precision)) print("[OLD] Num of correct context predictions : {}".format( context_preds)) print("Time to load data : %4.2f seconds \n" % data_loading) data_loading = 0 if iteration != 0 and iteration % 500 == 0: self.save(saver=saver, checkpoint_dir=self.checkpoint_dir, attrs=self._attrs, global_step=self.global_step) self.validation_performance(data_type=1, verbose=False) self.validation_performance(data_type=2, verbose=False) if iteration % 5000 == 0: print("Collecting garbage.") gc.collect() #end training # ##################### TEST ################################## def inference(self, ckptpath=None): saver = tf.train.Saver(var_list=tf.all_variables()) # (Try) Load all pretraining model variables print("Loading pre-saved model...") load_status = self.loadCKPTPath(saver=saver, ckptPath=ckptpath) if not load_status: print("No model to load. Exiting") sys.exit(0) tf.get_default_graph().finalize() r = self.inference_run() return r def inference_run(self): print("Test accuracy starting ... ") print(self.reader.typeOfReader) assert self.reader.typeOfReader == "inference" # assert self.reader.batch_size == 1 self.reader.reset_test() numInstances = 0 # For types: List contains numpy matrices of row_size = BatchSize predLabelScoresnumpymat_list = [] # For EL : Lists contain one list per mention condProbs_list = [] # Crosswikis conditional priors widIdxs_list = [] # Candidate WID IDXs (First is true) contextProbs_list = [] # Predicted Entity prob using context while self.reader.epochs < 1: (left_batch, left_lengths, right_batch, right_lengths, coherence_batch, wid_idxs_batch, wid_cprobs_batch) = self.reader.next_test_batch() # Candidates for entity linking # feed_dict = {self.sampled_entity_ids: wid_idxs_batch, # self.entity_priors: wid_cprobs_batch} feed_dict = {self.sampled_entity_ids: wid_idxs_batch} # Required Context if self.textcontext: if not self.pretrain_word_embed: context_dict = { self.left_batch: left_batch, self.right_batch: right_batch, self.left_lengths: left_lengths, self.right_lengths: right_lengths} else: context_dict = { self.left_context_embeddings: left_batch, self.right_context_embeddings: right_batch, self.left_lengths: left_lengths, self.right_lengths: right_lengths} feed_dict.update(context_dict) if self.coherence: coherence_dict = {self.coherence_indices: coherence_batch[0], self.coherence_values: coherence_batch[1], self.coherence_matshape: coherence_batch[2]} feed_dict.update(coherence_dict) fetch_tensors = [self.labeling_model.label_probs, self.posterior_model.entity_posteriors] fetches = self.sess.run(fetch_tensors, feed_dict=feed_dict) [label_sigms, context_probs] = fetches predLabelScoresnumpymat_list.append(label_sigms) condProbs_list.extend(wid_cprobs_batch) widIdxs_list.extend(wid_idxs_batch) contextProbs_list.extend(context_probs.tolist()) numInstances += self.reader.batch_size print("Num of instances {}".format(numInstances)) # print("Starting Type and EL Evaluations ... ") pred_TypeSetsList = evaluate_types.evaluate( predLabelScoresnumpymat_list, self.reader.idx2label) (evWTs, sortedContextWTs) = evaluate_inference.evaluateEL( condProbs_list, widIdxs_list, contextProbs_list, self.reader.idx2knwid, self.reader.wid2WikiTitle, verbose=False) return (predLabelScoresnumpymat_list, widIdxs_list, condProbs_list, contextProbs_list, evWTs, pred_TypeSetsList) def softmax(self, scores): expc = np.exp(scores) sumc = np.sum(expc) softmax_out = expc/sumc return softmax_out def print_all_variables(self): print("All Variables in the graph : ") self.print_variables_in_collection(tf.all_variables()) def print_trainable_variables(self): print("All Trainable variables in the graph : ") self.print_variables_in_collection(tf.trainable_variables()) def print_variables_in_collection(self, list_vars): print("Variables in list: ") for var in list_vars: print(" %s" % var.name) def extractEntityEmbeddings(self, ckptPath=None): saver = tf.train.Saver(var_list=tf.all_variables()) print("Loading pre-saved model...") load_status = self.loadCKPTPath(saver=saver, ckptPath=ckptPath) if not load_status: print("No model to load. Exiting") sys.exit(0) tf.get_default_graph().finalize() enembs = self.sess.run(self.posterior_model.knwn_entity_embeddings) return enembs
[]
2024-01-10
janetzki/GUIDE
src~sd_labeling~semantic_domain_labeler.py
import os import numpy as np import openai import pandas as pd from sklearn.metrics import precision_score, recall_score, f1_score, roc_auc_score # Use GPT-3 to label Bible verses (identify semantic domains) by answering pre-generated y/n questions # model_name = "ada:ft-personal:sd-labeler-8-2023-04-17-14-23-46" ### model_name = "davinci:ft-personal:sd-labeler-8-2023-04-17-15-12-52" # model_name = "ada:ft-personal:sd-labeler-9-2023-04-18-16-06-58" ### model_name = "davinci:ft-personal:sd-labeler-9-2023-04-18-14-52-28" # model_name = "ada:ft-personal:sd-labeler-10-2023-04-19-11-55-29" # model_name = "ada:ft-personal:sd-labeler-11-2023-04-19-15-08-24" # model_name = "ada:ft-personal:sd-labeler-11-2023-04-19-16-10-35" # actually the 12th version # model_name = "ada:ft-personal:sd-labeler-13-2023-04-20-21-21-04" # model_name = "ada:ft-personal:sd-labeler-14-2023-04-21-11-25-33" # model_name = "ada:ft-personal:sd-labeler-16-2023-04-24-11-38-16" # model_name = "ada:ft-personal:sd-labeler-15-2023-04-21-13-31-05" # {7: {'accuracy': 0.87, 'F1': 0.8670076726342711}, 8: {'accuracy': 0.84, 'F1': 0.830220713073005}, 9: {'accuracy': 0.79, 'F1': 0.7851662404092072}, 10: {'accuracy': 0.82, 'F1': 0.8125}, 11: {'accuracy': 0.81, 'F1': 0.7973333333333333}, 12: {'accuracy': 0.77, 'F1': 0.7631551848419318}} # model_name = "babbage:ft-personal:sd-labeler-15-2023-04-25-14-17-41" model_name = "curie:ft-personal:sd-labeler-15-2023-04-24-12-46-47" # {7: {'accuracy': 0.91, 'F1': 0.907928388746803}, 8: {'accuracy': 0.86, 'F1': 0.8440285204991087}, 9: {'accuracy': 0.83, 'F1': 0.8236331569664903}, 10: {'accuracy': 0.83, 'F1': 0.8186666666666667}, 11: {'accuracy': 0.84, 'F1': 0.8283998283998284}, 12: {'accuracy': 0.85, 'F1': 0.841621792841305}} ### model_name = "davinci:ft-personal:sd-labeler-15-2023-04-24-12-23-09" # model_name = "babbage:ft-personal:sd-labeler-17-2023-04-27-16-21-59" # model_name = "curie:ft-personal:sd-labeler-17-2023-04-25-12-48-17" # model_name = "babbage:ft-personal:sd-labeler-18-2023-04-27-16-52-12" answered_question_count = None total_question_count = None def answer_question(question): if question[-1] != '?': print(f"Error: question does not end with '?': '{question}'") return f"Error: question does not end with '?': '{question}'" question = question[:-1] + ' ->' # # randomly select questions # if random.randint(0, 19) != 0: # answered_question_count += 1 # return None # question = lowercase_question(question) # for Ada-12/13 openai.api_key = os.environ['OPENAI_API_KEY'] try: # answer = openai.ChatCompletion.create( # model="gpt-3.5-turbo", # temperature=0.0, # request_timeout=5, # messages=[ # # {"role": "system", "content": "Only answer with y or n."}, # {"role": "system", # "content": 'Answer all questions with "ok". Only if they are absurd, answer with "absurd". Remember that all quotes are from the four gospels.'}, # {"role": "user", "content": question}, # ] # )["choices"][0]["message"]["content"] answer = openai.Completion.create( model=model_name, temperature=0, request_timeout=5, max_tokens=1, prompt=f"{question}", )["choices"][0]["text"] if answer in (' 1', ' yes'): answer = 1 elif answer in (' 0', ' no'): answer = 0 else: print(f"Error: answer is not ' yes' or ' no': '{answer}'") except Exception as e: print(f"Error: {e}") answer = str(e) # print progress global answered_question_count answered_question_count += 1 if answered_question_count % 10 == 0: print(f"{answered_question_count}/{total_question_count} questions answered") print(f"{question} {answer}") return answer def answer_questions(selected_df, result_column_name): global total_question_count total_question_count = len(selected_df) selected_df[result_column_name] = selected_df['direct_question'].apply(answer_question) # filter out questions that were already answered (answer is not 0 or 1) # repeat 3 times for questions that were not answered (e.g., because of timeout) df_unanswered = selected_df[selected_df[result_column_name].isin((0, 1, "0", "1")) == False] total_question_count = len(df_unanswered) df_unanswered[result_column_name] = df_unanswered['direct_question'].apply(answer_question) selected_df.update(df_unanswered) df_unanswered = selected_df[selected_df[result_column_name].isin((0, 1, "0", "1")) == False] total_question_count = len(df_unanswered) df_unanswered[result_column_name] = df_unanswered['direct_question'].apply(answer_question) selected_df.update(df_unanswered) df_unanswered = selected_df[selected_df[result_column_name].isin((0, 1, "0", "1")) == False] total_question_count = len(df_unanswered) df_unanswered[result_column_name] = df_unanswered['direct_question'].apply(answer_question) selected_df.update(df_unanswered) return selected_df def label_questions(df, is_evaluatable=False, test_file_num=None): # df['answer'] = df['answer'].astype(int) # df = df[~df['qid'].str.startswith('8')] # # select rows for semantic domain # df = df[df['qid'].str.startswith('3')] # df = df.reset_index(drop=True) # print number of 0 and 1 answers print(df['answer'].value_counts()) # # load data/2_sd_labeling/ada_answers_in.csv into a dataframe # df_answers = pd.read_csv("data/2_sd_labeling/ada_answers_in.csv") # assert (len(df) == len(df_answers)) # df[result_column_name] = df_answers[result_column_name] # # randomly select questions with equal distribution of 0 and 1 answers # seed = int(time.time()) # num_questions = 100 # df = df[df['answer'] == 0]\ # .sample(n=num_questions, random_state=seed)\ # .append(df[df['answer'] == 1].sample(n=num_questions, random_state=seed)) global answered_question_count answered_question_count = 0 result_column_name = 'gpt3_answer' if is_evaluatable: df_answered = df[df['answer'].isna() == False] df_answered = answer_questions(df_answered, result_column_name) df[result_column_name] = np.nan df.update(df_answered) # evaluate accuracy, precision, recall, F1 score, and ROC AUC df[result_column_name] = df[result_column_name].astype(int) df['correct'] = df[result_column_name] == df['answer'] f1 = f1_score(df["answer"], df[result_column_name], average="macro") precision = precision_score(df["answer"], df[result_column_name], average="macro") recall = recall_score(df["answer"], df[result_column_name], average="macro") accuracy = df['correct'].mean() roc_auc = roc_auc_score(df["answer"], df[result_column_name]) print( f'test file: {test_file_num}, F1: {f1:.2f}, precision: {precision:.2f}, recall: {recall:.2f}, accuracy: {accuracy:.2f}, roc_auc: {roc_auc:.2f}') # put question column at the end df = df[['correct', result_column_name, 'answer', 'direct_question']] # # print entire dataframe with incorrect answers # pd.set_option('display.max_rows', None) # pd.set_option('display.max_columns', None) # pd.set_option('display.width', None) # pd.set_option('display.max_colwidth', None) # print(df[df['correct'] == False]) # save results df.to_csv(f"data/2_sd_labeling/test sets/answers_{test_file_num}_F1:_{f1}_{model_name}.csv", index=False) return {'accuracy': accuracy, 'F1': f1} else: df_unanswered = df[df['answer'].isna() == True] df_unanswered = answer_questions(df_unanswered, result_column_name) df[result_column_name] = np.nan df.update(df_unanswered) df.to_csv(f"data/2_sd_labeling/matched_questions_answered_curie_4.csv", index=False) return None # 100 --> 0.79, 0.81 # 200 --> 0.865 # 50x0 + 50x1 --> 0.78 # full file: 200x0 + 200x1 --> 0.8025 # SD 1: 200x0 + 200x1 --> 0.8225, 0.805, 0.825, 0.815, 0.79, 0.785 # SD 1 with rephrased questions: 0.79, 0.7475, 0.81, 0.75, 0.79, 0.85, 0.845, 0.81 # SD 1 with rephrased questions: 0.89, 0.875, 0.87 # SD 1 without rephrased questions: 0.905, 0.9 def test_model(test_file_num): df = pd.read_csv(f"data/2_sd_labeling/test sets/test_set_{test_file_num}.csv", usecols=["direct_question", "answer", "qid"]) return label_questions(df, True, test_file_num) def answer_unanswered_questions(): df = pd.read_excel("data/2_sd_labeling/matched_questions.xlsx", usecols=["direct_question", "answer", "qid"], nrows=156501) df = df.iloc[137199:] return label_questions(df) if __name__ == "__main__": # # test model # test_results = dict() # for test_file_num in [10, 11, 12]: # test_results[test_file_num] = test_model(test_file_num) # print(test_results) # print(f"average F1 score: {np.mean([test_results[test_file_num]['F1'] for test_file_num in test_results]):.2f}") # print( # f"average accuracy: {np.mean([test_results[test_file_num]['accuracy'] for test_file_num in test_results]):.2f}") answer_unanswered_questions()
[ "PLACEHOLDER" ]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~script_agent.py
import requests import threading import logging from openai import OpenAI , AzureOpenAI import openai import sys from pathlib import Path import time import json import datetime from .agent import agent from .summary_agent import summary_agent from .summary import summary class script_agent(agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , save_target_file = None): # basic setting self.name = agent_name self.room = game_room self.logger : logging.Logger = logging.getLogger(f"{__name__}.{self.name}") self.logger_handler = [] self.__logging_setting__() # openai api setting self.api_kwargs = {} try: self.__openai_init__(api_json) except: raise Exception("API Init failed") # game info self.role = None self.player_id = None self.player_name = [] self.game_data : list[dict]= None self.teamate = None self.operation_record = [] self.last_token_used = 0 self.save_target_file = save_target_file self.game_info_name = Path(game_info_path).stem # acc init self.mapping_dict : dict = None self.__init_mapping_dict__() self.acc_record = [] # from file load a game self.__load_game_info__(game_info_path) # for test get info self.is_deleted = False self.update = 0 # start the script game self.__start_script_game__() def __init_mapping_dict__(self): keyword_dict = { "good" : ["好人"], "god" : ["神","神職","神明"], "seer" : ["預言家"], "witch" :["女巫"], "hunter" : ["獵人"], "village" : ["平民" , "民" , "村民"], "werewolf" : ["狼","狼人","壞人"], } self.mapping_dict = {} for label , key_words in keyword_dict.items(): for keyword in key_words: self.mapping_dict[keyword] = label self.partially_correct_check = { "good" : ["seer" , "witch" , "village" , "hunter"], "god" : ["seer" , "witch" , "hunter"] } def __load_game_info__(self , game_info_path , const_player_name = True): with open(game_info_path , "r" , encoding="utf-8") as f: agent_info : dict[str:str] = json.loads(f.readline()) # first line with agent info self.role = list(agent_info.values())[0] self.player_id = list(agent_info.keys())[0] # second line with player info player_info : dict[str: dict[str,str]] = json.loads(f.readline()) self.teamate = [] self.player_role = [] for id , info in player_info.items(): # const with player name or origin name if const_player_name: self.player_name.append(f"Player{id}") else: self.player_name.append(info["user_name"]) # get the teammate info if id != self.player_id and info['user_role'] == "werewolf": self.teamate.append(id) # self.player_role.append(info['user_role']) if self.role == "werewolf": pass self.game_data = [json.loads(info) for info in f.readlines() if "stage" in json.loads(info).keys()] def __start_script_game__(self): self.__start_game_init__(None) for data in self.game_data: self.logger.debug(data) self.__process_data__(data) # logging agent info agent_info = self.get_info() self.last_token_used = int(agent_info['token_used'][0]) del agent_info['memory'] self.logger.debug(agent_info) if agent_info['updated'][0] == "1": self.__cal_quess_role_acc__(agent_info['guess_roles']) for anno in data['announcement']: if anno['operation'] == "game_over" : self.__game_over_process__(anno , 0) break if self.save_target_file != None: self.__save_to_file__() self.__del__() def __cal_quess_role_acc__(self , guess_roles): acc_cnt = 0 result = [] for idx , guess in enumerate(guess_roles): guess = self.mapping_dict[guess] if guess in self.mapping_dict.keys() else None real = self.player_role[idx] if idx == int(self.player_id): result.append("自己") elif guess == None: result.append("全錯") elif guess == real: acc_cnt += 1 result.append("全對") elif guess in self.partially_correct_check.keys() and real in self.partially_correct_check[guess]: acc_cnt += 0.5 result.append("半對") else: result.append("全錯") acc = acc_cnt / (len(self.player_role) -1) self.acc_record.append(acc) self.logger.debug(guess_roles) self.logger.debug(self.player_role) self.logger.info(f"guess roles with {acc}") self.logger.info(result) return acc def __start_game_init__(self , room_data): self.logger.debug(f"game is started , this final room info : {room_data}") def __get_role__(self): pass def __get_all_role__(self): pass def __check_game_state__(self, failure_cnt): pass def __game_over_process__(self, anno, wait_time): self.logger.info(f"Script game is over , {anno['description']}") def __skip_stage__(self): pass def __send_operation__(self, data): self.operation_record.append(data) self.logger.info(f"Agent send operation : {data}") def __save_to_file__(self): result_dic = { "agent_type" : type(self).__name__, "scr_game_info" : self.game_info_name, "all_acc" : self.acc_record, "all_operation" : self.operation_record, "token_used" : self.last_token_used } with open(self.save_target_file , 'a+' , encoding='utf-8') as f : json.dump(result_dic , f , ensure_ascii=False) f.write('\n') def __del__(self): if self.is_deleted: return self.is_deleted = True self.logger.info(f"---------------Script Result---------------") if len(self.acc_record) != 0: self.logger.info(f"Agent guess roles avg acc {(sum(self.acc_record) / len(self.acc_record)):.3f}") self.logger.info(f"{(self.acc_record)}") self.logger.info(f"operation record") for _ in self.operation_record: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------") class summary_script_agent(summary_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , save_target_file = None): # basic setting self.name = agent_name self.room = game_room self.logger : logging.Logger = logging.getLogger(f"{__name__}.{self.name}") self.logger_handler = [] self.__logging_setting__() # openai api setting self.api_kwargs = {} try: self.__openai_init__(api_json) except: raise Exception("API Init failed") # game info self.role = None self.player_id = None self.player_name = [] self.game_data : list[dict]= None self.teamate = None self.operation_record = [] # acc init self.mapping_dict : dict = None self.__init_mapping_dict__() self.acc_record = [] self.last_token_used = 0 self.save_target_file = save_target_file self.game_info_name = Path(game_info_path).stem # for test get info self.update = 0 # summary self.summary_generator = summary(logger= self.logger, api_json = api_json) self.operation_info = {} self.game_info = [] self.is_deleted = False # from file load a game self.__load_game_info__(game_info_path) # start the script game self.__start_script_game__() def __init_mapping_dict__(self): keyword_dict = { "good" : ["好人"], "god" : ["神","神職","神明"], "seer" : ["預言家"], "witch" :["女巫"], "hunter" : ["獵人"], "village" : ["平民" , "民" , "村民"], "werewolf" : ["狼","狼人","壞人"], } self.mapping_dict = {} for label , key_words in keyword_dict.items(): for keyword in key_words: self.mapping_dict[keyword] = label self.partially_correct_check = { "good" : ["seer" , "witch" , "village" , "hunter"], "god" : ["seer" , "witch" , "hunter"] } def __load_game_info__(self , game_info_path , const_player_name = True): with open(game_info_path , "r" , encoding="utf-8") as f: agent_info : dict[str:str] = json.loads(f.readline()) self.game_info.append(agent_info) # first line with agent info self.role = list(agent_info.values())[0] self.player_id = list(agent_info.keys())[0] # second line with player info player_info : dict[str: dict[str,str]] = json.loads(f.readline()) self.game_info.append(player_info) self.teamate = [] self.player_role = [] for id , info in player_info.items(): # const with player name or origin name if const_player_name: self.player_name.append(f"Player{id}") else: self.player_name.append(info["user_name"]) # get the teammate info if id != self.player_id and info['user_role'] == "werewolf": self.teamate.append(id) # self.player_role.append(info['user_role']) if self.role == "werewolf": pass self.game_data = [json.loads(info) for info in f.readlines() if "stage" in json.loads(info).keys()] def __start_script_game__(self): self.__start_game_init__(None) for data in self.game_data: self.__record_agent_game_info__(data) data["guess_summary"] = self.__get_summary(cur_stage= "guess_role") data["stage_summary"] = self.__get_summary(cur_stage= data['stage'].split('-')[-1]) if len(data['information']) != 0 else [None] self.logger.debug(data) self.__process_data__(data) # logging agent info agent_info = self.get_info() self.last_token_used = int(agent_info['token_used'][0]) del agent_info['memory'] self.logger.debug(agent_info) if agent_info['updated'][0] == "1": self.__cal_quess_role_acc__(agent_info['guess_roles']) for anno in data['announcement']: if anno['operation'] == "game_over" : self.__game_over_process__(anno , 0) break if self.save_target_file != None: self.__save_to_file__() self.__del__() def __cal_quess_role_acc__(self , guess_roles): acc_cnt = 0 result = [] for idx , guess in enumerate(guess_roles): guess = self.mapping_dict[guess] if guess in self.mapping_dict.keys() else None real = self.player_role[idx] if idx == int(self.player_id): result.append("自己") elif guess == None: result.append("全錯") elif guess == real: acc_cnt += 1 result.append("全對") elif guess in self.partially_correct_check.keys() and real in self.partially_correct_check[guess]: acc_cnt += 0.5 result.append("半對") else: result.append("全錯") acc = acc_cnt / (len(self.player_role) -1) self.acc_record.append(acc) self.logger.debug(guess_roles) self.logger.debug(self.player_role) self.logger.info(f"guess roles with {acc}") self.logger.info(result) return acc def __get_summary(self, cur_stage): # 狼人發言、一般人發言 if cur_stage in ["dialogue", "werewolf_dialogue"]: stage = "dialogue" # 狼人投票、一般人投票 elif cur_stage in ["werewolf", "vote1", "vote2"] : stage = "vote" # 預言家、女巫、獵人 elif cur_stage in ["seer", "witch", "hunter"]: stage = "operation" elif cur_stage == "guess_role": stage = "guess_role" else: return [None] self.similarly_sentences = self.summary_generator.find_similarly_summary(stage, game_info = self.game_info) return self.similarly_sentences def __start_game_init__(self , room_data): self.logger.debug(f"game is started , this final room info : {room_data}") def __get_role__(self): pass def __get_all_role__(self): pass def __check_game_state__(self, failure_cnt): pass def __game_over_process__(self, anno, wait_time): self.logger.info(f"Script game is over , {anno['description']}") def __skip_stage__(self): pass def __send_operation__(self, data): operation_key = data['operation'] if data['stage_name'].split('-')[-1] != "witch" else f"{data['operation']} {data['chat']}" self.operation_info[operation_key] = data self.operation_record.append(data) self.logger.info(f"Agent send operation : {data}") def __save_to_file__(self): result_dic = { "agent_type" : type(self).__name__, "scr_game_info" : self.game_info_name, "all_acc" : self.acc_record, "all_operation" : self.operation_record, "token_used" : self.last_token_used } with open(self.save_target_file , 'a+' , encoding='utf8') as f : json.dump(result_dic , f , ensure_ascii=False) f.write('\n') def __del__(self): if self.is_deleted: return self.is_deleted = True self.logger.info(f"---------------Script Result---------------") if len(self.acc_record) != 0: self.logger.info(f"Agent guess roles avg acc {(sum(self.acc_record) / len(self.acc_record)):.3f}") self.logger.info(f"{(self.acc_record)}") self.logger.info(f"operation record") for _ in self.operation_record: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------")
[]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~long_memory_stream~memory_stream_utils~long_memory_stream.py
import requests import threading import logging from openai import OpenAI , AzureOpenAI import sys from pathlib import Path import time import json import numpy as np import re import time import math from sentence_transformers import SentenceTransformer, util class long_memeory_stream(): sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') def __init__(self , prompt_dir , logger , client , openai_kwargs , summary=False , log_prompt = False , used_memory=True): self.memory_stream = [] self.openai_kwargs = openai_kwargs self.client : OpenAI | AzureOpenAI = client self.logger : logging.Logger = logger self.log_prompt = log_prompt self.max_fail_cnt = 3 self.token_used = 0 # used for the llm keyword translate self.chinese_to_english = { # importantance "分數" : "score", # reflection question "問題" : "question", # refection "見解" : "opinion", "參考見解" : "reference", # suspect role list "猜測身分" : "role", # vote "投票" : "vote", # dialogue "發言" : "dialogue", # importantance / suspect role list / vote "原因" : "reason", } self.role_to_chinese = { "seer" : "預言家", "witch" : "女巫", "village" : "村民", "werewolf" : "狼人", "hunter" : "獵人" } self.player_num = None self.role = None self.suspect_role_list : dict[int , str] = {} self.know_role_list : dict[int , str] = {} self.remain_player = [] self.prompt_dir = prompt_dir self.prompt_template : dict[str , str] = None self.example : dict[str , str] = None self.day = 0 # sample `update_stage` return type self.ret_format = { "stage_name" : None, "operation": None, "target" : None, "chat" : None } # gues roles updated flag for get info self.guess_roles_updated = 0 # record the reflection self.reflection_list = [] # the use summary or not flag self.summary = summary # the use memory or not flag self.used_memory = used_memory # sumary data , if summary flag = false , set all to empty string self.summary_operation_data : list = ["" for i in range(5)] self.summary_guess_role_data : list = ["" for i in range(5)] # addtion prompt suggest self.dialogue_suggestion = "也請不要完全相信其他玩家的發言,小心掉入狼人陷阱" def update_game_info(self , player_id , player_name , role , roles_setting): """update the player name & init suspect_role_list""" self.player_num = len(player_name) self.player_id = int(player_id) self.player_name = player_name self.role = role self.roles_setting = roles_setting self.__load_prompt_and_example__(self.prompt_dir) # self.push('0' , len(self.memory_stream) , f"您為{self.player_id}號玩家({player_name[self.player_id]})" , default_importantance=10) self.push('0' , len(self.memory_stream) , f"您的身分為{self.role_to_chinese[role]}" , default_importantance=10) # self.push('0' , len(self.memory_stream) , f"{self.player_id}號玩家({player_name[self.player_id]})是{self.role_to_chinese[role]}" , default_importantance=10) self.suspect_role_list = {i:"未知" for i in range(self.player_num) if i != self.player_id} self.logger.debug(self.suspect_role_list) self.know_role_list[int(player_id)] = role # self.remain_player = [i for i in range(self.player_num)] def push(self , day , turn , observation , default_importantance = None): """push the observation in memeory stream""" if default_importantance == None: info = self.__cal_importantance__(observation) else: info = {"score" : default_importantance,"reason" : "default"} full_observation = { "day" : day, "turn" : turn, "last_used" : turn, "observation" : observation , "importantance" : info["score"], "impo_reason" : info['reason'] } self.logger.debug(f"push observation {full_observation}") self.memory_stream.append(full_observation) def update_stage(self , data): # logging for test self.logger.info(f"---{data['stage']} {data['stage_description']}---\n") for i in self.memory_stream[-7:]: self.logger.debug(f" {i['observation']}") self.logger.debug("") # if summary flag = true , save the summary if self.summary: if data['guess_summary'] != [None]: self.summary_guess_role_data = [_.strip('\n') for _ in data['guess_summary']] if data['stage_summary'] != [None]: self.summary_operation_data = [_.strip('\n') for _ in data['stage_summary']] # a new day init # skip check_role stage if '-' in data['stage'] and self.day != data['stage'].split('-')[0]: self.day = data['stage'].split('-')[0] if self.day != "1": self.__day_init__() # if have vote info if any(data["vote_info"].values()) : self.__push_vote_info__(data["vote_info"] , data["stage"]) self.__process_announcement__(data) operations = self.__process_information__(data) for op in operations: op['stage_name'] = data['stage'] return operations def get_long_memory_info(self): combine_guess_roles = {} for i in range(len(self.player_name)): if i in self.know_role_list.keys(): combine_guess_roles[i] = self.know_role_list[i] else: combine_guess_roles[i] = self.suspect_role_list[i] ret = { "memory" : [self.__memory_to_str__(self.memory_stream[-10:])], "guess_roles" :[i for i in combine_guess_roles.values()], "token_used" : [str(self.token_used)], "updated" : [str(self.guess_roles_updated)] } self.guess_roles_updated = 0 return ret def __process_announcement__(self , data): """add announcement to memory stream""" announcement = data['announcement'] chat_flag = False for anno in announcement: observation = "" # player (except this agent) last stage chat if anno["operation"] == "chat" and anno['user'][0] != self.player_id: observation = f"{anno['user'][0]}號玩家({self.player_name[anno['user'][0]]})說「{anno['description']}」" chat_flag = True self.push(self.day , len(self.memory_stream) , observation) # player died elif anno["operation"] == "died": observation = f"{anno['user'][0]}號玩家({self.player_name[anno['user'][0]]})死了" # self.remain_player.remove(int(anno['user'][0])) self.push(self.day , len(self.memory_stream) , observation , default_importantance=5) def __process_information__(self , data) -> list[dict]: informations = data["information"] operation = [] for info in informations: # generate dialouge operation if info['operation'] == "dialogue": self.__reflection__(self.day , len(self.memory_stream)) self.__gen_suspect_role_list__(self.day , len(self.memory_stream)) operation.append(self.__gen_dialogue__(self.day , len(self.memory_stream))) # generate vote operation elif info['operation'] == "vote_or_not" and "vote" in data["stage"]: self.__reflection__(self.day , len(self.memory_stream)) self.__gen_suspect_role_list__(self.day , len(self.memory_stream)) operation.append(self.__gen_vote__(self.day , len(self.memory_stream) , info['target'])) return operation def __retrieval__(self , day , turn , query , pick_num = 5 , threshold = 0.5): """ the retrieval process , will call importantance,recency,relevance func and return the top {pick_num} memory sored by score. """ # not used the memory if self.used_memory == False: return self.memory_stream self.logger.debug(f"start retrieval") self.logger.debug(f" query : {query}") importantance_score = [ob['importantance'] for ob in self.memory_stream] recency_score = self.__cal_recency__(day , turn) ori_relevance_score = self.__cal_relevance__(query) # normalize recency_score /= np.linalg.norm(recency_score) importantance_score /= np.linalg.norm(importantance_score) relevance_score = ori_relevance_score / np.linalg.norm(ori_relevance_score) importantance_factor = 1 relevance_factor = 1 recency_factor = 1 # calulate score score = recency_score * recency_factor + importantance_score * importantance_factor + relevance_score * relevance_factor sorted_memory_streams = self.memory_stream.copy() delete_idx = [] # delete the relevance_score < threshold for idx in range(len(sorted_memory_streams)): sorted_memory_streams[idx]["score"] = score[idx] if ori_relevance_score[idx] < threshold: delete_idx.append(idx) # delete score < threshold memory for idx in reversed(delete_idx): sorted_memory_streams.pop(idx) sorted_memory_streams.sort(key=lambda element: element['score'] , reverse=True) # logger with 1.5 * pick_num memory score self.logger.debug(f" sum | importantance | recency | relevance | Memory | ori_rele") for order_mem in sorted_memory_streams[:int(1.5*pick_num)]: sum_score = order_mem["score"] ori_idx = order_mem["turn"] memory = order_mem['observation'].strip('\n') self.logger.debug(f" {sum_score:.3f} | {importantance_score[ori_idx]:.11f} | {recency_score[ori_idx]:.5f} | {relevance_score[ori_idx]:.7f} | {memory} | {ori_relevance_score[ori_idx]}") # updated last uesd for idx in range(min(pick_num , len(sorted_memory_streams))): self.memory_stream[sorted_memory_streams[idx]['turn']]['last_used'] = turn return sorted_memory_streams[:pick_num] def __reflection__(self , day , turn): """ the relection func , first will gen question from recent observation second , use the question as retrieval query search the memory third , refection by the memory and push the new refection to memory """ if not self.used_memory: return info = self.__reflection_question__(day , turn) question = info['question'].strip('\n') memory = self.__retrieval__(day , turn , question) info = self.__reflection_opinion__(memory) self.push(day , turn , info['opinion']) self.logger.info(f"reflection : {info['opinion']}") self.reflection_list.append(info) def __gen_suspect_role_list__(self , day , turn): """iterate the {suspect_role_list} and gen the new suspect role """ for player , role in self.suspect_role_list.items(): if player in self.know_role_list.keys(): continue if player == self.player_id : continue memory = self.__retrieval__(day , turn , f"{player}號玩家({self.player_name[player]})是什麼身分?") replace_order = { "%m" : self.__memory_to_str__(memory), "%e" : self.example['suspect_role_list'], "%t" : f"{player}號玩家({self.player_name[player]})", "%rs" : self.__roles_setting_to_str__(), "%ar" : f"{self.player_id}號玩家({self.player_name[self.player_id]}),身分為{self.role_to_chinese[self.role]}", "%s" : self.__summary_to_str__(1) } final_prompt = self.prompt_template['suspect_role_list'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) info = { "role" : "村民", "reason" : "test" } info = self.__process_LLM_output__(final_prompt , {"role" : str , "reason" : str} , info , "guess roles") self.suspect_role_list[player] = info["role"] self.logger.info(f"update suspect role list : {self.suspect_role_list}") self.guess_roles_updated = 1 def __gen_vote__(self , day , turn , target): """gen the vote player num & get the reason""" # memory = self.__retrieval__(day , turn , "幾號玩家是大家懷疑對象") # memory_str = self.__memory_to_str__(memory) replace_order = { "%m" : self.__memory_to_str__(self.memory_stream[-10:]), "%l" : self.__role_list_to_str__()[0], "%e" : self.example['vote'], "%t" : "、".join([str(_) for _ in target if _ != self.player_id]), "%ar" : f"{self.player_id}號玩家({self.player_name[self.player_id]}),身分為{self.role_to_chinese[self.role]}", "%rs" : self.__roles_setting_to_str__(), "%s" : self.__summary_to_str__() } final_prompt = self.prompt_template['vote'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) info = { "vote" : 4, "reason" : "test" } info = self.__process_LLM_output__(final_prompt , {"vote" : int , "reason" : str} , info , "vote") ret = self.ret_format.copy() ret['operation'] = "vote_or_not" ret['target'] = info["vote"] ret['chat'] = "" return ret def __gen_dialogue__(self , day ,turn): """gen the dialogue""" query = self.__reflection_question__(day , turn)['question'] memory = self.__retrieval__(day , turn , query) # memory_str = self.__memory_to_str__(self.memory_stream[-5:]) replace_order = { "%m" : self.__memory_to_str__(memory), "%l" : self.__role_list_to_str__()[0], "%e" : self.example['dialogue'], "%rs" : self.__roles_setting_to_str__(), "%su" : self.dialogue_suggestion, "%ar" : f"{self.player_id}號玩家({self.player_name[self.player_id]}),身分為{self.role_to_chinese[self.role]}", "%s" : self.__summary_to_str__() } final_prompt = self.prompt_template['dialogue'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) # final_prompt = self.prompt_template['dialogue'].replace("%m" , memory_str).replace("%e" , self.example['dialogue']).replace("%l" , sus_role_str).replace("%s" , summary) info = { "dialogue" : "test", } info = self.__process_LLM_output__(final_prompt , {"dialogue" : str} , info , "dialogue") ret = self.ret_format.copy() ret['operation'] = "dialogue" ret['target'] = -1 ret['chat'] = info['dialogue'] return ret def __role_list_to_str__(self): """ export the {suspect_role_list} and {know_role_list} to string like 1號玩家(Yui1)可能是女巫 or 1號玩家(Yui1)是女巫 """ sus_role_str = '\n'.join([f"{player}號玩家({self.player_name[player]})可能是{role}。" for player , role in self.suspect_role_list.items()]) know_role_str = '\n'.join([f"{player}號玩家({self.player_name[player]})是{role}。" for player , role in self.know_role_list.items()]) return sus_role_str , know_role_str def __roles_setting_to_str__(self): return ','.join([f"{values}個{self.role_to_chinese[key]}" for key , values in self.roles_setting.items()]) def __summary_to_str__(self , summary_type=0 , pick_num = 1): # summary_type 0 => operation # summary_type 1 => guess roles if self.summary == False: return "" self.prompt_template['summary'] summary_data_str = "" if summary_type == 0: summary_data_str = '\n'.join([f"{idx+1}. {_}" for idx , _ in enumerate(self.summary_operation_data[:pick_num])]) else: summary_data_str = '\n'.join([f"{idx+1}. {_}" for idx , _ in enumerate(self.summary_guess_role_data[:pick_num])]) return f"{self.prompt_template['summary']}\n{summary_data_str}" def __cal_importantance__(self , observation): """cal the importantance score""" replace_order = { "%m" : observation, "%e" : self.example['importantance'], } final_prompt = self.prompt_template['importantance'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) info = { "score" : 0, "reason" : "test" } if self.used_memory: info = self.__process_LLM_output__(final_prompt, {"score" : int, "reason" : str} , info , "importantance") return info def __cal_recency__(self , day, turn) : """cal the recency score""" initial_value = 1.0 decay_factor = 0.90 score = [0 for i in range(len(self.memory_stream))] for idx , observation in enumerate(self.memory_stream): time = (turn-observation['last_used']) score[idx] = initial_value * math.pow(decay_factor, time) score = np.array(score) return score def __cal_relevance__(self , query : str): """cal the relevance score""" query_embedding = self.sentence_model.encode([query] , convert_to_tensor=True) score = [0 for i in range(len(self.memory_stream))] text = [i['observation'] for i in self.memory_stream] embeddings = self.sentence_model.encode(text, convert_to_tensor=True) for idx in range(embeddings.shape[0]): score[idx] = util.pytorch_cos_sim(query_embedding, embeddings[idx]).to("cpu").item() score = np.array(score) return score def __reflection_question__(self , day , turn , pick_num = 8): """one of reflection process , get the question used for reflection.""" self.logger.debug('reflection_question') replace_order = { "%m" : self.__memory_to_str__(self.memory_stream[-pick_num:]), "%l" : self.__role_list_to_str__()[0], "%e" : self.example['reflection_question'], "%ar" : f"{self.player_id}號玩家({self.player_name[self.player_id]}),身分為{self.role_to_chinese[self.role]}", "%rs" : self.__roles_setting_to_str__(), } final_prompt = self.prompt_template['reflection_question'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) info = { "question" : "test", } if self.used_memory: info = self.__process_LLM_output__(final_prompt, {"question" : str} , info , "reflection question") return info def __reflection_opinion__(self , memory): """one of reflection process , get the opinion as new observation.""" self.logger.debug('reflection_opinion') memory_str = self.__memory_to_str__(memory) sus_role_str , know_role_str = self.__role_list_to_str__() replace_order = { "%m" : self.__memory_to_str__(memory), "%l" : self.__role_list_to_str__()[0], "%e" : self.example['reflection'], "%rs" : self.__roles_setting_to_str__(), "%ar" : f"{self.player_id}號玩家({self.player_name[self.player_id]}),身分為{self.role_to_chinese[self.role]}", } final_prompt = self.prompt_template['reflection'] for key , item in replace_order.items() : final_prompt = final_prompt.replace(key , item) # final_prompt = self.prompt_template['reflection'].replace('%m' , memory_str).replace("%e" , self.example['reflection']).replace("%l" , sus_role_str) info = { "opinion" : "test", "reference" : "test", } info = self.__process_LLM_output__(final_prompt, {"opinion" : str , "reference" : str , "reason" : str} , info , "reflection opinion") # process reference to real memory idx try: reference_memory = info["reference"].strip('\n').split('、') real_reference_idx = [memory[int(idx)-1]["turn"] for idx in reference_memory] info["reference"] = real_reference_idx except Exception as e: self.logger.warning(f"__reflection_opinion__ fail with reference , {e}") return info def __push_vote_info__(self , vote_info : dict , stage): prefix = "狼人投票殺人階段:" if stage.split('-')[-1] == "seer" else "玩家票人出去階段:" for player , voted in vote_info.items(): player = int(player) if voted != -1: ob = f"{player}號玩家({self.player_name[player]})投給{voted}號玩家({self.player_name[voted]})" else: ob = f"{player}號玩家({self.player_name[player]})棄票" self.push(self.day , len(self.memory_stream) , ob , default_importantance=5) def __day_init__(self): self.__reflection__(self.day , len(self.memory_stream)) self.__gen_suspect_role_list__(self.day , len(self.memory_stream)) # pass def __process_LLM_output__(self , prompt , keyword_dict : dict, sample_output , task_name): """ communication with LLM , repeat {max_fail_cnt} util find the {keyword_list} in LLM response . return the {keyword_list} dict , if fail get {keyword_list} in LLM response , return {sample_output}. """ success_get_keyword = False fail_idx = 0 self.logger.debug(f"Start Task : {task_name}") self.logger.debug(f" LLM keyword : {keyword_dict}") if self.log_prompt: self.logger.debug(f"{prompt}") info = {} while not success_get_keyword and fail_idx < self.max_fail_cnt: self.logger.debug(f" {fail_idx} response generate...") info = {} result = self.__openai_send__(prompt) # result block by openai if result == None: fail_idx+=1 continue splited_result = result.split('\n') keyword_name = "" for line in splited_result: # get keyword like [XXX] keyword = re.search('\[(.*)\]', line) if keyword != None and keyword.group(1) in self.chinese_to_english.keys(): keyword_name = self.chinese_to_english[keyword.group(1)] info[keyword_name] = "" elif keyword_name != "": info[keyword_name] += line + "\n" # check the keyword is in keyword_list and the type is satisfy require if info.keys() == keyword_dict.keys() and all(_.strip('\n').strip('-').isnumeric() for keyword , _ in info.items() if keyword_dict[keyword] == int): success_get_keyword = True # change data type & remove the '\n' for keyword , _ in info.items() : if keyword_dict[keyword] == int : info[keyword] = int(_.strip('\n')) else: info[keyword] = _.strip('\n') else : fail_idx+=1 self.logger.warning(f" {fail_idx} failed \n{result}") if fail_idx >= self.max_fail_cnt: info = sample_output self.logger.debug(f" failure cnt exceed {self.max_fail_cnt}") self.logger.debug(f"Task output : {info}") return info def __memory_to_str__(self , memory , add_idx=True): """ export the memory dict to str like 1. {observation[1]} 2. {observation[2]} or {observation[1]} {observation[2]} """ if add_idx: return '\n'.join([f"{idx+1}. {i['observation']}" for idx , i in enumerate(memory)]) else: return '\n'.join([f"{i['observation']}" for idx , i in enumerate(memory)]) def __openai_send__(self , prompt): """openai api send prompt , can override this.""" response = self.client.chat.completions.create( **self.openai_kwargs, messages = [ {"role":"system","content":"您為狼人殺遊戲的玩家,請根據狼人殺遊戲相關規則與經驗,回答相關的內容。"}, {"role":"user","content":prompt} ], temperature=0.7, max_tokens=800, top_p=0.95, frequency_penalty=0, presence_penalty=0, stop=None) response = response.model_dump() self.token_used += response["usage"]["total_tokens"] if response['choices'][0]["finish_reason"] == "content_filter": self.logger.debug("Block By Openai") return None return response['choices'][0]['message']['content'] def __len__(self): return len(self.memory_stream) def __load_prompt_and_example__(self , prompt_dir): """load prompt json to dict""" self.logger.debug("load common json") with open(prompt_dir / "common_prompt.json" , encoding="utf-8") as json_file: self.prompt_template = json.load(json_file) with open(prompt_dir / "common_example.json" , encoding="utf-8") as json_file: self.example = json.load(json_file) with open(prompt_dir / "summary_addition_prompt.json" , encoding="utf-8") as json_file: self.summary_template = json.load(json_file) for key , prompt_li in self.prompt_template.items(): self.prompt_template[key] = '\n'.join(prompt_li) for key , prompt_li in self.example.items(): self.example[key] = '\n'.join(prompt_li) for key , prompt_li in self.summary_template.items(): # load the summary addtional prompt only the `summary` flag is true if self.summary: self.prompt_template[key] = '\n'.join(prompt_li) else: self.prompt_template[key] = "" def __register_keywords__(self , keywords:dict[str,str]): self.logger.debug(f"Register new keyword : {keywords}") self.chinese_to_english.update(keywords)
[ "importantance", "reflection", "您為狼人殺遊戲的玩家,請根據狼人殺遊戲相關規則與經驗,回答相關的內容。", "suspect_role_list", "reflection_question" ]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~intelligent_agent~intelligent_agent.py
import requests import openai from pathlib import Path from ..agent import agent from .prompts import prompts from .summary_prompt import summary_prompts from pathlib import Path import logging from datetime import datetime import sys import json from ..summary_agent import summary_agent from ..script_agent import script_agent, summary_script_agent class intelligent_agent(agent): def __init__(self , api_json = "doc/secret/chatgpt.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = Path("prompt/memory_stream/")): # api_json = "doc/secret/chatgpt.key" super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # used for start game for test self.master_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ5dWkiLCJyb29tX25hbWUiOiJURVNUUk9PTSIsImxlYWRlciI6dHJ1ZSwiaWF0IjoxNjkwMzc5NTM0LCJleHAiOjE2OTkwMTk1MzR9.BEmD52DuK657YQezsqNgJAwbPfl54o8Pb--Dh7VQMMA" # init long memory class & models self.prompts : prompts = None # start the game self.day = None self.turn = 0 def get_info(self) -> dict[str,str]: return self.prompts.__get_agent_info__() def __process_data__(self, data): """Process the data got from server""" operations = self.prompts.agent_process(data) # self.logger.debug("Operations "+str(operations)) for i in operations: op_data = { "stage_name" : data['stage'], "operation" : i["operation"], "target" : i["target"], "chat" : i["chat"] } self.__send_operation__(op_data) if data['stage'].split("-")[2] == "dialogue": self.__skip_stage__() def __start_game_init__(self, room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") # self.room_setting = room_data['game_setting'] self.player_name = [name for name in room_data["room_user"]] data = self.__get_role__() self.logger.debug(f'User data: {data}') self.prompts : prompts = prompts(data['player_id'], data['game_info'], room_data['game_setting'], self.logger, self.client, self.api_kwargs) self.__get_all_role__() self.__check_game_state__(0) def __get_guess_role__(self): r = self.prompts.__get_guess_role__() self.logger.debug(f'User data: {r}') return r class intelligent_agent_script(script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream", save=None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room, save_target_file= save) def __process_data__(self, data): """Process the data got from server""" operations = self.prompts.agent_process(data) # self.logger.debug("Operations "+str(operations)) for i in operations: op_data = { "stage_name" : data['stage'], "operation" : i["operation"], "target" : i["target"], "chat" : i["chat"] } self.__send_operation__(op_data) if data['stage'].split("-")[2] == "dialogue": self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" room_data = {"game_setting": { "player_num": 7, "operation_time" : 5, "dialogue_time" : 10, "seer" : 1, "witch" : 1, "village" : 2, "werewolf" : 2, "hunter" : 1 }} self.logger.debug(f"game is started , this final room info : {room_data}") # self.room_setting = room_data['game_setting'] # self.player_name = [name for name in room_data["room_user"]] data = {} data["player_id"] = self.player_id data["game_info"] = {"teamate": self.teamate, "user_role": self.role} if self.role != "werewolf": data["game_info"]["teamate"] = [] self.logger.debug(f'User data: {data}') self.prompts : prompts = prompts(data['player_id'], data['game_info'], room_data['game_setting'], self.logger, self.client, self.api_kwargs) def get_info(self) -> dict[str,str]: return self.prompts.__get_agent_info__() class summary_intelligent_agent(summary_agent): def __init__(self , api_json = "doc/secret/openai.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = Path("prompt/memory_stream/")): super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # used for start game for test self.master_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ5dWkiLCJyb29tX25hbWUiOiJURVNUUk9PTSIsImxlYWRlciI6dHJ1ZSwiaWF0IjoxNjkwMzc5NTM0LCJleHAiOjE2OTkwMTk1MzR9.BEmD52DuK657YQezsqNgJAwbPfl54o8Pb--Dh7VQMMA" # init long memory class & models self.prompts : summary_prompts = None # start the game self.day = None self.turn = 0 def get_info(self) -> dict[str,str]: agent_info = self.prompts.__get_agent_info__() agent_info["token_used"] = [str(int(agent_info["token_used"][0]) + self.summary_generator.token_used)] return agent_info def __process_data__(self, data): """Process the data got from server""" operations = self.prompts.agent_process(data) # self.logger.debug("Operations "+str(operations)) for i in operations: op_data = { "stage_name" : data['stage'], "operation" : i["operation"], "target" : i["target"], "chat" : i["chat"] } self.__send_operation__(op_data) if data['stage'].split("-")[2] == "dialogue": self.__skip_stage__() def __start_game_init__(self, room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.player_name = [name for name in room_data["room_user"]] data = self.__get_role__() self.logger.debug(f'User data: {data}') self.prompts : summary_prompts = summary_prompts(data['player_id'], data['game_info'], room_data['game_setting'], self.logger, self.client, self.api_kwargs) self.__get_all_role__() self.__check_game_state__(0) def __get_guess_role__(self): r = self.prompts.__get_guess_role__() self.logger.debug(f'User data: {r}') return r class summary_intelligent_agent_script(summary_script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream", save=None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room, save_target_file= save) def __process_data__(self, data): """Process the data got from server""" operations = self.prompts.agent_process(data) # self.logger.debug("Operations "+str(operations)) for i in operations: op_data = { "stage_name" : data['stage'], "operation" : i["operation"], "target" : i["target"], "chat" : i["chat"] } self.__send_operation__(op_data) if data['stage'].split("-")[2] == "dialogue": self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" room_data = {"game_setting": { "player_num": 7, "operation_time" : 5, "dialogue_time" : 10, "seer" : 1, "witch" : 1, "village" : 2, "werewolf" : 2, "hunter" : 1 }} self.logger.debug(f"game is started , this final room info : {room_data}") data = {} data["player_id"] = self.player_id data["game_info"] = {"teamate": self.teamate, "user_role": self.role} if self.role != "werewolf": data["game_info"]["teamate"] = [] self.logger.debug(f'User data: {data}') self.prompts : summary_prompts = summary_prompts(data['player_id'], data['game_info'], room_data['game_setting'], self.logger, self.client, self.api_kwargs) def get_info(self) -> dict[str,str]: agent_info = self.prompts.__get_agent_info__() agent_info["token_used"] = [str(int(agent_info["token_used"][0]) + self.summary_generator.token_used)] return agent_info class intelligent_agent_test(agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = Path("prompt/memory_stream/")): self.__reset_server__(server_url) super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # used for start game for test self.master_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ5dWkiLCJyb29tX25hbWUiOiJURVNUUk9PTSIsImxlYWRlciI6dHJ1ZSwiaWF0IjoxNjkwMzc5NTM0LCJleHAiOjE2OTkwMTk1MzR9.BEmD52DuK657YQezsqNgJAwbPfl54o8Pb--Dh7VQMMA" # init long memory class & models self.prompts : summary_prompts = None # start the game self.day = None self.turn = 0 # set the game for test self.room_setting = { "player_num": 7, "operation_time" : 5, "dialogue_time" : 10, "seer" : 1, "witch" : 1, "village" : 2, "werewolf" : 2, "hunter" : 1 } self.__setting_game() # start the game for test self.__start_server__() def __logging_setting__(self): """logging setting , can override this.""" log_format = logging.Formatter('[%(asctime)s] [%(levelname)s] - %(message)s') self.logger.setLevel(logging.DEBUG) current_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') handler = logging.FileHandler(filename=f'logs/{self.name}_{self.room}_{current_time}.log', encoding='utf-8' , mode="w") handler.setLevel(logging.DEBUG) handler.setFormatter(log_format) self.logger.addHandler(handler) handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.DEBUG) handler.setFormatter(log_format) self.logger.addHandler(handler) logging.getLogger("requests").propagate = False def get_info(self) -> dict[str,str]: return self.prompts.__get_agent_info__() def __process_data__(self, data): """Process the data got from server""" operations = self.prompts.agent_process(data) # self.logger.debug("Operations "+str(operations)) for i in operations: op_data = { "stage_name" : data['stage'], "operation" : i["operation"], "target" : i["target"], "chat" : i["chat"] } self.__send_operation__(op_data) def __reset_server__(self , server_url): """for convenient test""" try : r = requests.get(f'{server_url}/api/reset' , timeout=5) # if r.status_code == 200: # self.logger.debug("Reset Room Success") # else: # self.logger.warning(f"Reset Room Error : {r.json()}") except Exception as e : self.logger.warning(f"__reset_server__ Server Error , {e}") def __start_server__(self): """for convenient test""" try : r = requests.get(f'{self.server_url}/api/start_game/{self.room}' , headers= { "Authorization" : f"Bearer {self.master_token}" }) if r.status_code == 200: self.logger.debug("Start Game") else: self.logger.warning(f"Start Game : {r.json()}") except Exception as e : self.logger.warning(f"__start_server__ Server Error , {e}") def __setting_game(self): """for convenient test""" try : r = requests.post(f'{self.server_url}/api/room/{self.room}' , headers= { "Authorization" : f"Bearer {self.master_token}" }, json= self.room_setting) if r.status_code == 200: self.logger.debug("Setting Game Success") else: self.logger.warning(f"Setting Game Error : {r.json()}") except Exception as e : self.logger.warning(f"__setting_game Server Error , {e}") self.__check_game_state__(0) def __start_game_init__(self, room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.room_setting = room_data['game_setting'] self.player_name = [name for name in room_data["room_user"]] data = self.__get_role__() self.logger.debug(f'User data: {data}') self.prompts : prompts = prompts(data['player_id'], data['game_info'], self.room_setting, self.logger, self.client, self.api_kwargs) self.__check_game_state__(0) # def __get_guess_role__(self): # return self.prompts.__get_guess_role__()
[]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~long_memory_stream~simple_agent.py
import requests import threading import logging import openai import sys from pathlib import Path import time import json import numpy as np import re import time import math from sentence_transformers import SentenceTransformer, util from ..agent import agent from ..summary_agent import summary_agent from ..script_agent import script_agent from .memory_stream_utils.role import role , werewolf , seer , witch , hunter class simple_agent(agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = "doc/prompt/memory_stream"): super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # init long memory class & models self.long_memory : role = None # start the game self.day = None self.turn = 0 self.prompt_dir = Path(prompt_dir) def get_info(self) -> dict[str,str]: return self.long_memory.get_long_memory_info() def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.player_name = [name for name in room_data["room_user"]] role_info = self.__get_role__() self.__get_all_role__() role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = {role_name : room_data['game_setting'][role_name] for role_name in role_to_class.keys()} self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger, self.client , self.api_kwargs , used_memory=False) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , role_info['game_info']['teamate']) self.__check_game_state__(0) class summary_simple_agent(summary_agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = "doc/prompt/memory_stream"): super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # init long memory class & models self.long_memory : role = None # start the game self.day = None self.turn = 0 self.prompt_dir = Path(prompt_dir) def get_info(self) -> dict[str,str]: ret = self.long_memory.get_long_memory_info() self.logger.debug(f"Token used agent : {int(ret['token_used'][0])} summary : {self.summary_generator.token_used}") ret['token_used'][0] = str( int(ret['token_used'][0]) + self.summary_generator.token_used ) return ret def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.player_name = [name for name in room_data["room_user"]] role_info = self.__get_role__() self.__get_all_role__() role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = {role_name : room_data['game_setting'][role_name] for role_name in role_to_class.keys()} self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger, self.client , self.api_kwargs , summary=True , used_memory=False) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , role_info['game_info']['teamate']) self.__check_game_state__(0)
[]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~long_memory_stream~test_simple_agent.py
import requests import threading import logging import openai import sys from pathlib import Path import time import json import numpy as np import re import time import math from sentence_transformers import SentenceTransformer, util from ..agent import agent from ..script_agent import script_agent , summary_script_agent from .memory_stream_utils.role import role , werewolf , seer , witch , hunter from .memory_stream_agent import memory_stream_agent ''' This file is for testing the memory_stream_agent / summary_memory_stream_agent contain two type testing method 1. use script agent (recommend) 2. use API Call with server ''' class simple_agent_script(script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream" , save_target_file = None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room , save_target_file) def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def get_info(self) -> dict[str,str]: return self.long_memory.get_long_memory_info() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = { "werewolf" : 2, "seer" : 1, "witch" : 1, "hunter" : 1, "village" : 2, } self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger , self.client , self.api_kwargs , log_prompt=True , used_memory=False) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , self.teamate) def __del__(self): super().__del__() self.logger.info(f"---------------Memory Stream---------------") self.logger.info(f"memory") for _ in self.long_memory.memory_stream: self.logger.info(f" {_}") self.logger.info(f"reflect") for _ in self.long_memory.reflection_list: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------") for handler in self.logger_handler: self.logger.removeHandler(handler) class summary_simple_agent_script(summary_script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream" , save_target_file = None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room , save_target_file) def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def get_info(self) -> dict[str,str]: ret = self.long_memory.get_long_memory_info() self.logger.debug(f"Token used agent : {int(ret['token_used'][0])} summary : {self.summary_generator.token_used}") ret['token_used'][0] = str( int(ret['token_used'][0]) + self.summary_generator.token_used ) return ret def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = { "werewolf" : 2, "seer" : 1, "witch" : 1, "hunter" : 1, "village" : 2, } self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger , self.client , self.api_kwargs , summary=True , log_prompt=True , used_memory=False) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , self.teamate) def __del__(self): super().__del__() self.logger.info(f"---------------Memory Stream---------------") self.logger.info(f"memory") for _ in self.long_memory.memory_stream: self.logger.info(f" {_}") self.logger.info(f"reflect") for _ in self.long_memory.reflection_list: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------") for handler in self.logger_handler: self.logger.removeHandler(handler)
[]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~long_memory_stream~memory_stream_agent.py
import requests import threading import logging import openai import sys from pathlib import Path import time import json import numpy as np import re import time import math from sentence_transformers import SentenceTransformer, util from ..agent import agent from ..summary_agent import summary_agent from ..script_agent import script_agent from .memory_stream_utils.role import role , werewolf , seer , witch , hunter class memory_stream_agent(agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = "doc/prompt/memory_stream"): super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # init long memory class & models self.long_memory : role = None # start the game self.day = None self.turn = 0 self.prompt_dir = Path(prompt_dir) def get_info(self) -> dict[str,str]: return self.long_memory.get_long_memory_info() def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.player_name = [name for name in room_data["room_user"]] role_info = self.__get_role__() self.__get_all_role__() role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = {role_name : room_data['game_setting'][role_name] for role_name in role_to_class.keys()} self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger, self.client , self.api_kwargs) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , role_info['game_info']['teamate']) self.__check_game_state__(0) class summary_memory_stream_agent(summary_agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = "doc/prompt/memory_stream"): super().__init__(api_json = api_json, server_url = server_url , agent_name = agent_name , room_name = room_name , color = color) # init long memory class & models self.long_memory : role = None # start the game self.day = None self.turn = 0 self.prompt_dir = Path(prompt_dir) def get_info(self) -> dict[str,str]: ret = self.long_memory.get_long_memory_info() self.logger.debug(f"Token used agent : {int(ret['token_used'][0])} summary : {self.summary_generator.token_used}") ret['token_used'][0] = str( int(ret['token_used'][0]) + self.summary_generator.token_used ) return ret def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") self.player_name = [name for name in room_data["room_user"]] role_info = self.__get_role__() self.__get_all_role__() role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = {role_name : room_data['game_setting'][role_name] for role_name in role_to_class.keys()} self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger, self.client , self.api_kwargs , summary = True) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , role_info['game_info']['teamate']) self.__check_game_state__(0)
[]
2024-01-10
Yui-Arthur/generative_agent_with_werewolf_kill
agents~long_memory_stream~test_memory_stream_agent.py
import requests import threading import logging import openai import sys from pathlib import Path import time import json import numpy as np import re import time import math from sentence_transformers import SentenceTransformer, util from ..agent import agent from ..script_agent import script_agent , summary_script_agent from .memory_stream_utils.role import role , werewolf , seer , witch , hunter from .memory_stream_agent import memory_stream_agent ''' This file is for testing the memory_stream_agent / summary_memory_stream_agent contain two type testing method 1. use script agent (recommend) 2. use API Call with server ''' class memory_stream_agent_test(memory_stream_agent): def __init__(self , api_json = "doc/secret/yui.key", server_url = "140.127.208.185" , agent_name = "Agent1" , room_name = "TESTROOM" , color = "f9a8d4" , prompt_dir = "doc/prompt/memory_stream"): self.__reset_server__(server_url) super().__init__(api_json = api_json , server_url = server_url , agent_name = agent_name , room_name = room_name , color = color , prompt_dir = prompt_dir) # used for start game for test self.master_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ5dWkiLCJyb29tX25hbWUiOiJURVNUUk9PTSIsImxlYWRlciI6dHJ1ZSwiaWF0IjoxNjkwMzc5NTM0LCJleHAiOjE2OTkwMTk1MzR9.BEmD52DuK657YQezsqNgJAwbPfl54o8Pb--Dh7VQMMA" # set the game for test self.__setting_game() # start the game for test self.__start_server__() def __reset_server__(self , server_url): """for convenient test""" try : r = requests.get(f'{server_url}/api/reset' , timeout=5) # if r.status_code == 200: # self.logger.debug("Reset Room Success") # else: # self.logger.warning(f"Reset Room Error : {r.json()}") except Exception as e : self.logger.warning(f"__reset_server__ Server Error , {e}") def __start_server__(self): """for convenient test""" try : r = requests.get(f'{self.server_url}/api/start_game/{self.room}' , headers= { "Authorization" : f"Bearer {self.master_token}" }) if r.status_code == 200: self.logger.debug("Start Game") else: self.logger.warning(f"Start Game : {r.json()}") except Exception as e : self.logger.warning(f"__start_server__ Server Error , {e}") def __setting_game(self): """for convenient test""" try : r = requests.post(f'{self.server_url}/api/room/{self.room}' , headers= { "Authorization" : f"Bearer {self.master_token}" }, json= { "player_num": 7, "operation_time" : 10, "dialogue_time" : 10, "seer" : 1, "witch" : 1, "village" : 2, "werewolf" : 2, "hunter" : 1 }) if r.status_code == 200: self.logger.debug("Setting Game Success") else: self.logger.warning(f"Setting Game Error : {r.json()}") except Exception as e : self.logger.warning(f"__setting_game Server Error , {e}") class memory_stream_agent_script(script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream" , save_target_file = None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room , save_target_file) def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def get_info(self) -> dict[str,str]: return self.long_memory.get_long_memory_info() def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = { "werewolf" : 2, "seer" : 1, "witch" : 1, "hunter" : 1, "village" : 2, } self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger , self.client , self.api_kwargs , log_prompt = True) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , self.teamate) def __del__(self): super().__del__() self.logger.info(f"---------------Memory Stream---------------") self.logger.info(f"memory") for _ in self.long_memory.memory_stream: self.logger.info(f" {_}") self.logger.info(f"reflect") for _ in self.long_memory.reflection_list: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------") for handler in self.logger_handler: self.logger.removeHandler(handler) class summary_memory_stream_agent_script(summary_script_agent): def __init__(self , api_json = None, game_info_path = None, agent_name = "ScriptGame" , game_room = "ScriptGame" , prompt_dir = "doc/prompt/memory_stream" , save_target_file = None): self.prompt_dir = Path(prompt_dir) super().__init__(api_json, game_info_path, agent_name , game_room , save_target_file) def __process_data__(self, data): """the data process.""" operations = self.long_memory.update_stage(data) skip = False for operation in operations: self.__send_operation__(operation) if operation['operation'] == 'dialogue': skip = True if skip: self.__skip_stage__() def get_info(self) -> dict[str,str]: ret = self.long_memory.get_long_memory_info() self.logger.debug(f"Token used agent : {int(ret['token_used'][0])} summary : {self.summary_generator.token_used}") ret['token_used'][0] = str( int(ret['token_used'][0]) + self.summary_generator.token_used ) return ret def __start_game_init__(self , room_data): """the game started setting , update player name""" self.logger.debug(f"game is started , this final room info : {room_data}") role_to_class = { "werewolf" : werewolf, "seer" : seer, "witch" : witch, "hunter" : hunter, "village" : role, } roles_setting = { "werewolf" : 2, "seer" : 1, "witch" : 1, "hunter" : 1, "village" : 2, } self.long_memory : role = role_to_class[self.role](self.prompt_dir , self.logger , self.client , self.api_kwargs , summary = True , log_prompt = True) if self.role != "werewolf": self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting) else: self.long_memory.update_game_info(self.player_id , self.player_name , self.role , roles_setting , self.teamate) def __del__(self): super().__del__() self.logger.info(f"---------------Memory Stream---------------") self.logger.info(f"memory") for _ in self.long_memory.memory_stream: self.logger.info(f" {_}") self.logger.info(f"reflect") for _ in self.long_memory.reflection_list: self.logger.info(f" {_}") self.logger.info(f"-------------------------------------------") for handler in self.logger_handler: self.logger.removeHandler(handler)
[]
2024-01-10
Hassi34/whatsapp-ai-chatbot
src~custom_toolkit~aws_ec2.py
import boto3 from dotenv import load_dotenv from langchain.tools import BaseTool load_dotenv() ec2_client = boto3.client('ec2') desc = """use this tool when the user asks for information about EC2 AWS resources This tool will act like a cloud architect with for AWS cloud resources. It will return the json formatted list of EC2 AWS resources. """ class awsEC2Tool(BaseTool): name = "AWS EC2 Tool" description = desc def _run(self): ec2_client = boto3.client('ec2') aws_response = ec2_client.describe_instances( # Filters = [ # { # 'Name': 'instance-state-name', # 'Values': ['running'] # } # ] ) instances = [] for reservation in aws_response['Reservations']: for instance in reservation['Instances']: instance_name = instance['Tags'][0]['Value'] instance_id = instance['InstanceId'] instance_type = instance['InstanceType'] launch_time = instance['LaunchTime'].strftime("%Y-%m-%d %H:%M:%S") instances_key_pair_name = instance['KeyName'] instance_monitoring_state = instance['Monitoring'] instance_placement = instance['Placement'] try: private_ip_address = instance['PrivateIpAddress'] except KeyError: private_ip_address = None try: public_ip_address = instance['PublicIpAddress'] except KeyError: public_ip_address = None public_dns_name = instance['PublicDnsName'] instance_current_state = instance['State']['Name'] instances.append( { 'instance_name' : instance_name, 'instance_id': instance_id, 'instance_type': instance_type, 'launch_time' : launch_time, 'instance_key_pair_name' : instances_key_pair_name, 'instance_monitoring_state' : instance_monitoring_state, 'instance_placement' : instance_placement, 'private_ip_address' : private_ip_address, 'public_ip_address' : public_ip_address, 'public_dns_name' : public_dns_name, 'instance_current_state' : instance_current_state } ) if len(instances) > 0: return instances else: return "There are no instances available on your AWS account." def _arun(self, query: str): raise NotImplementedError("This tool does not support async")
[]
2024-01-10
Hassi34/whatsapp-ai-chatbot
src~custom_toolkit~weather_tool.py
from langchain.tools import BaseTool from datetime import datetime import requests import os from dotenv import load_dotenv load_dotenv() OPENWEATHER_API_KEY = os.environ.get("OPENWEATHER_API_KEY") desc = ( "use this tool when you need to get any information about the weather " "It can provide the information about the pressure, temperature, rain/clouds, wind, sunrise, sunset and local time. " "It takes CITY as a parameter and returns the weather data for that specific city/location" "['city]." ) class WeatherTool(BaseTool): name = "Weather Status Tool" description = desc def _run(self, city: str): try: CITY = city['title'] except TypeError: CITY = city if CITY is not None and isinstance(CITY, str): BASE_URL = "http://api.openweathermap.org/data/2.5/weather?" URL = BASE_URL + "appid=" + OPENWEATHER_API_KEY + "&q=" + CITY + "&units=metric" response = requests.get(URL).json() weather_list = [] temp_celsius = str(response['main']['temp']) + " °C" wind_speed = str(response['wind']['speed']) + " km/h" humidity = str(response['main']['humidity']) + " %" air_pressure = str(response['main']['pressure']) + " hPa" clouds_coverage = str(response['clouds']['all']) + " %" description = response['weather'][0]['description'] current_local_time = datetime.utcfromtimestamp(response['dt'] + response['timezone']) sunrise_time = datetime.utcfromtimestamp(response['sys']['sunrise'] + response['timezone']) sunset_time = datetime.utcfromtimestamp(response['sys']['sunrise'] + response['timezone']) weather_list.append({ 'temp celsius' : temp_celsius, 'wind speed' : wind_speed, 'humidity' : humidity, 'air pressure' : air_pressure, 'clouds coverage' : clouds_coverage, 'description' : description, 'local time' : current_local_time, 'sunrise time' : sunrise_time, 'sunset time' : sunset_time }) return weather_list else: return "Please provide a valid name of the city" def _arun(self, query: str): raise NotImplementedError("This tool does not support async")
[]
2024-01-10
Hassi34/whatsapp-ai-chatbot
src~operations.py
from langchain import OpenAI from src.utils.common import read_yaml from langchain import LLMMathChain, SerpAPIWrapper from langchain.agents import AgentType from langchain.agents import initialize_agent #from langchain.tools import Tool from langchain.agents import Tool from src.custom_toolkit import WeatherTool, awsEC2Tool, DummyTool config = read_yaml("src/config.yaml") MODEL_NAME = config['chatbot']['MODEL_NAME'] TEMPERATURE = config['chatbot']['TEMPERATURE'] llm = OpenAI( model_name=MODEL_NAME, temperature=TEMPERATURE ) search = SerpAPIWrapper() llm_math_chain = LLMMathChain(llm=llm, verbose=True) def get_conv_agent(): tools = [awsEC2Tool(), WeatherTool()] tools.append( Tool.from_function( func=search.run, name="Search", description="Only Use this tool only when you need to answer something about the recent or current envent" )) # class CalculatorInput(BaseModel): # question: str = Field(description = "The input string with the number being computed") #tools.append(WeatherTool()) tools.append( Tool.from_function( func=llm_math_chain.run, name="Calculator", description="useful for when you need to answer questions about math", #args_schema=CalculatorInput # coroutine= ... <- you can specify an async method if desired as well )) tools.append(DummyTool()) #tools.append(awsEC2Tool()) conv_agent = initialize_agent( agent= AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, tools=tools, llm=llm, verbose=True, max_iterations=3, early_stopping_method='generate', #memory=ConversationSummaryMemory(llm=llm), handle_parsing_errors="The chain should end with the final result", ) conv_agent.agent.llm_chain.prompt.messages[0].prompt.template = f""" Introduce yourself as an AI Chatbot developed by Hasanain. but don't label it as "System:"\n {str(conv_agent.agent.llm_chain.prompt.messages[0].prompt.template)}\n Don't use the Search tool until there is a need to search something online, something which has happened recently.\n The chain should end with a single string, that means the final answer should be in a string format. Remove the "Thought :" at the end of the chain, only provide the final answer! """ return conv_agent
[]
2024-01-10
Hassi34/whatsapp-ai-chatbot
src~custom_toolkit~dummy.py
import boto3 from dotenv import load_dotenv from langchain.tools import BaseTool load_dotenv() ec2_client = boto3.client('ec2') desc = """Don't use this tool in anycase It doesn't provide any functionality """ class DummyTool(BaseTool): name = "Dummy Tool" description = desc def _run(self):... def _arun(self, query: str): raise NotImplementedError("This tool does not support async")
[]
2024-01-10
JoshuaTrinh5262/aichatbot
server~core_ai~chat_gpt.py
import openai import dotenv import os from llama_index import GPTVectorStoreIndex, LLMPredictor, PromptHelper, SimpleDirectoryReader, VectorStoreIndex from llama_index import StorageContext, load_index_from_storage from langchain.chat_models import ChatOpenAI class ChatGPT: vopenai = openai index = [] # Constructor (initialize instance) def __init__(self): # Instance attributes config = dotenv.dotenv_values("./.env") self.vopenai.api_key = config['OPENAI_API_KEY'] os.environ["OPENAI_API_KEY"] = config['OPENAI_API_KEY'] # self.documentsIndex() self.loadIndexFromStorage() def CustomChatGptByIndex(self, user_input, store_conversation): #chatGPT query_engine = self.index.as_query_engine() store_conversation.append("User: " + user_input) response = query_engine.query(user_input) # #llama index # store_conversation.append("User: " + user_input) # response = self.index.query(user_input) store_conversation.append("Ai: " + response.response.replace("\n", "")) self.saveChatHistory(store_conversation, 'index_chat_history') return response def CustomChatGPT(self, user_input, store_conversation): print(self.vopenai.api_key) store_conversation.append("User: " + user_input) response = self.vopenai.Completion.create( model = "text-davinci-003", prompt = "\n".join(store_conversation), max_tokens = 500, n = 1, stop = None, temperature = 0.5, ) ChatGPT_reply = response.choices[0].text.strip() store_conversation.append(ChatGPT_reply) self.saveChatHistory(store_conversation, 'chat_history') return ChatGPT_reply def saveChatHistory(self, conversation, file_name): file_path = "./server/logs/" + file_name + ".txt" with open(file_path, "w", encoding="utf-8") as file: file.write("\n".join(conversation)) def documentsIndex(self): try: documents = SimpleDirectoryReader('./server/store').load_data() self.index = VectorStoreIndex.from_documents(documents) self.index.storage_context.persist(persist_dir="./server/test_index") return True except Exception as e: print(f"An error occurred: {str(e)}") return False def documentsGptIndex(self): try: documents = SimpleDirectoryReader('./server/store').load_data() llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.1, model="gpt-3.5-turbo")) max_input_size = 2048 num_output = 100 max_chunk_overlap = 20 chunk_overlap_ratio = 0.1 prompt_helper = PromptHelper(max_input_size, num_output, chunk_overlap_ratio, max_chunk_overlap) self.index = GPTVectorStoreIndex.from_documents( documents, llm_predictor = llm_predictor, prompt_helper = prompt_helper ) self.index.storage_context.persist(persist_dir="./server/test_gpt_index") return True except Exception as e: print(f"An error occurred: {str(e)}") return False def loadIndexFromStorage(self): # rebuild storage context storage_context = StorageContext.from_defaults(persist_dir='./server/index') # load index self.index = load_index_from_storage(storage_context)
[ "\n" ]
2024-01-10
JoshuaTrinh5262/aichatbot
crawling~web_crawling.py
################################################################################ ### Step 1 ################################################################################ import requests import re import urllib.request from bs4 import BeautifulSoup from collections import deque from html.parser import HTMLParser from urllib.parse import urlparse import os import pandas as pd import tiktoken import openai import numpy as np from openai.embeddings_utils import distances_from_embeddings, cosine_similarity from lxml import html import csv import dotenv import domain_extraction # Regex pattern to match a URL HTTP_URL_PATTERN = r'^http[s]{0,1}://.+$' config = dotenv.dotenv_values("./.env") # Define root domain to crawl full_url = config['FULL_URL'] domain = domain_extraction.get_domain_from_url(full_url) # Create a class to parse the HTML and get the hyperlinks class HyperlinkParser(HTMLParser): def __init__(self): super().__init__() # Create a list to store the hyperlinks self.hyperlinks = [] # Override the HTMLParser's handle_starttag method to get the hyperlinks def handle_starttag(self, tag, attrs): attrs = dict(attrs) # If the tag is an anchor tag and it has an href attribute, add the href attribute to the list of hyperlinks if tag == "a" and "href" in attrs: self.hyperlinks.append(attrs["href"]) ################################################################################ ### Step 2 ################################################################################ # Function to get the hyperlinks from a URL def get_hyperlinks(url): # Try to open the URL and read the HTML try: # Open the URL and read the HTML with urllib.request.urlopen(url) as response: # If the response is not HTML, return an empty list if not response.info().get('Content-Type').startswith("text/html"): return [] # Decode the HTML html = response.read().decode('utf-8') except Exception as e: print(e) return [] # Create the HTML Parser and then Parse the HTML to get hyperlinks parser = HyperlinkParser() parser.feed(html) return parser.hyperlinks ################################################################################ ### Step 3 ################################################################################ # Function to get the hyperlinks from a URL that are within the same domain def get_domain_hyperlinks(local_domain, url): clean_links = [] for link in set(get_hyperlinks(url)): clean_link = None # If the link is a URL, check if it is within the same domain if re.search(HTTP_URL_PATTERN, link): # Parse the URL and check if the domain is the same url_obj = urlparse(link) if url_obj.netloc == local_domain: clean_link = link # If the link is not a URL, check if it is a relative link else: if link.startswith("/"): link = link[1:] elif ( link.startswith("#") or link.startswith("mailto:") or link.startswith("tel:") ): continue clean_link = "https://" + local_domain + "/" + link if clean_link is not None: if clean_link.endswith("/"): clean_link = clean_link[:-1] clean_links.append(clean_link) # Return the list of hyperlinks that are within the same domain return list(set(clean_links)) ################################################################################ ### Step 4 ################################################################################ def crawl(url): # Parse the URL and get the domain local_domain = urlparse(url).netloc # Create a queue to store the URLs to crawl queue = deque([url]) # Create a set to store the URLs that have already been seen (no duplicates) seen = set([url]) # Create a directory to store the text files if not os.path.exists("text/"): os.mkdir("text/") if not os.path.exists("text/"+local_domain+"/"): os.mkdir("text/" + local_domain + "/") # Create a directory to store the csv files if not os.path.exists("processed"): os.mkdir("processed") # While the queue is not empty, continue crawling while queue: # Get the next URL from the queue url = queue.pop() print(url) # for debugging and to see the progress # Specify the CSV file path csv_file = "text/" + domain + ".csv" # # Write the crawled URLs to the CSV file # with open(csv_file, "w", newline="") as f: # writer = csv.writer(f) # writer.writerow(["Crawled URLs"]) # Write the header # writer.writerows([[url] for url in seen]) # Save text from the url to a <url>.txt file try: with open('text/' + local_domain + '/' + url[8:].replace("/", "_") + ".txt", "w", encoding="UTF-8") as f: # # Get the text from the URL using BeautifulSoup # soup = BeautifulSoup(requests.get(url).text, "html.parser") # # Get the text but remove the tags # text = soup.get_text() response = requests.get(url) tree = html.fromstring(response.content) text = tree.text_content() # If the crawler gets to a page that requires JavaScript, it will stop the crawl if ("You need to enable JavaScript to run this app." in text): print("Unable to parse page " + url + " due to JavaScript being required") # Otherwise, write the text to the file in the text directory f.write(text) except Exception as e: with open('text/' + local_domain + '/error_log.txt', 'a', encoding="UTF-8") as error_file: error_file.write(url + '\n') print("An error occurred:", str(e)) # Get the hyperlinks from the URL and add them to the queue for link in get_domain_hyperlinks(local_domain, url): if link not in seen: queue.append(link) seen.add(link) crawl(full_url) print("Done") ################################################################################ ### Step 5 ################################################################################ def remove_newlines(serie): serie = serie.str.replace('\n', ' ') serie = serie.str.replace('\\n', ' ') serie = serie.str.replace(' ', ' ') serie = serie.str.replace(' ', ' ') return serie ################################################################################ ### Step 6 ################################################################################ # Create a list to store the text files texts=[] # Get all the text files in the text directory for file in os.listdir("text/" + domain + "/"): # Open the file and read the text with open("text/" + domain + "/" + file, "r", encoding="UTF-8") as f: text = f.read() # Omit the first 11 lines and the last 4 lines, then replace -, _, and #update with spaces. texts.append((file[11:-4].replace('-',' ').replace('_', ' ').replace('#update',''), text)) # Create a dataframe from the list of texts df = pd.DataFrame(texts, columns = ['fname', 'text']) # Set the text column to be the raw text with the newlines removed df['text'] = df.fname + ". " + remove_newlines(df.text) df.to_csv('processed/scraped.csv') df.head()
[]
2024-01-10
Wriath18/Saathi_Chatbot
app_t.py
from langchain import PromptTemplate, LLMChain import chainlit as cl from langchain import HuggingFaceHub import PySimpleGUI as sg from deep_translator import GoogleTranslator language_options = { 'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Italian': 'it', 'Portuguese': 'pt', 'Dutch': 'nl', 'Russian': 'ru', 'Japanese': 'ja', 'Korean': 'ko', 'Chinese': 'zh-cn', 'Arabic': 'ar' } a = [] # Language selection list def select_language(window, event, values): a.append(language_options[values['-LANGUAGE-']]) window.close() # GUI for language selection sg.theme('DefaultNoMoreLines') layout = [[sg.Text('Select Language'), sg.Combo(list(language_options.keys()), key='-LANGUAGE-')], [sg.Button('OK', key='-OK-')]] window = sg.Window('Language Selector', layout) while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break elif event == '-OK-': select_language(window, event, values) repo_id = "tiiuae/falcon-7b-instruct" llm = HuggingFaceHub( huggingfacehub_api_token='YOUR HUGGINGFACE API KEY', repo_id=repo_id, model_kwargs={"temperature": 0.3, "max_new_tokens": 1024} ) # Chatbot prompt template template = """ Task: Chat with the user in a highly friendly and conversational manner and responses should be long and fluent. Style: Conversational Tone: Friendly Audience: General public Chatbot's name : Saathi User says: "{user_input}" How should the chatbot respond?""" def translate_text(text, target_language_code): translator = GoogleTranslator(source='auto', target=target_language_code) return translator.translate(text) @cl.on_chat_start def main(): prompt = PromptTemplate(template=template, input_variables=["user_input"]) llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True) cl.user_session.set("llm_chain", llm_chain) @cl.on_message async def main(message: cl.Message): llm_chain = cl.user_session.get("llm_chain") target_language_code = 'en' if a: target_language_code = a[0] res = await llm_chain.acall({"user_input": message.content}, callbacks=[cl.AsyncLangchainCallbackHandler()]) translated_response = translate_text(res['text'], target_language_code) if "Saathi" in translated_response: chatbot_message = translated_response.split("Saathi: ", 1)[-1].replace("User", "") elif "Mini Saathi" in translated_response: chatbot_message = translated_response.split("Mini Saathi: ", 1)[-1].replace("User", "") else: chatbot_message = translated_response.split("Chatbot: ", 1)[-1].replace("User", "") # Send the extracted chatbot's message back to the user await cl.Message(content=chatbot_message).send() if __name__ == "__main__": main()
[ "user_input", " Task: Chat with the user in a highly friendly and conversational manner and responses should be long and fluent.\nStyle: Conversational\nTone: Friendly\nAudience: General public\nChatbot's name : Saathi\nUser says: \"{user_input}\"\nHow should the chatbot respond?", "{user_input}" ]
2024-01-10
Altinn/digdir-slack-bot
docs_qa~chains.py
import box import yaml import os import openai import instructor from pydantic import BaseModel, Field import pprint from utils.openai_utils import json_gpt from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from docs_qa.prompts import qa_template from docs_qa.llm import build_llm from utils.openai_utils import json_gpt # module init with open('docs_qa/config/config.yml', 'r', encoding='utf8') as ymlfile: cfg = box.Box(yaml.safe_load(ymlfile)) instructor.patch() openai.api_key = os.environ['OPENAI_API_KEY_ALTINN3_DEV'] openai.api_base = os.environ['OPENAI_API_URL_ALTINN3_DEV'] pp = pprint.PrettyPrinter(indent=2) async def generate_hypothetical_answer(user_input) -> str: HA_INPUT = f"""Generate a hypothetical answer to the user's question. This answer will be used to rank search results. Pretend you have all the information you need to answer, but don't use any actual facts. Instead, use placeholders like NAME did something, or NAME said something at PLACE. User question: {user_input} Format: {{"hypotheticalAnswer": "hypothetical answer text"}} """ hypothetical_answer = json_gpt(HA_INPUT)["hypotheticalAnswer"] return hypothetical_answer def set_qa_prompt(): """ Prompt template for QA retrieval for each vectorstore """ prompt = PromptTemplate(template=qa_template, input_variables=['context', 'question']) return prompt def build_retrieval_qa(llm, prompt, vectordb): dbqa = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=vectordb.as_retriever(search_kwargs={'k': cfg.VECTOR_COUNT}), return_source_documents=cfg.RETURN_SOURCE_DOCUMENTS, chain_type_kwargs={'prompt': prompt}, verbose=False ) return dbqa def setup_dbqa(): embeddings = OpenAIEmbeddings() vectordb = FAISS.load_local(cfg.DB_FAISS_PATH, embeddings) llm = build_llm() qa_prompt = set_qa_prompt() dbqa = build_retrieval_qa(llm, qa_prompt, vectordb) return dbqa
[ "question", "context" ]
2024-01-10
Altinn/digdir-slack-bot
bots~docs_qa.py
import json import pprint from slack_sdk.errors import SlackApiError import openai.error import utils.slack_utils as slack_utils from bots.structured_log import bot_log, BotLogEntry from docs_qa.rag_manual_stuff import rag_with_typesense from channel_msg_categorize.run_chain import ( run_chain_async as run_channel_msg_categorize, ) pp = pprint.PrettyPrinter(indent=2) chain_name = "[docs]" async def run_bot_async(app, hitl_config, say, msg_body, text): src_evt_context = slack_utils.get_event_context(msg_body) main_channel_id = msg_body.get("event").get("channel") target_channel_id = main_channel_id qa_channel_id = hitl_config.get("qa_channel", "") src_msg_link = "" hitl_enabled = qa_channel_id != "" and hitl_config.get("enabled") # override target_channel_id if hitl enabled if hitl_enabled: target_channel_id = qa_channel_id src_msg_link = slack_utils.get_message_permalink(app, msg_body) print( f"hitl enabled: {hitl_enabled}, main_channel_id: {main_channel_id}, qa_channel_id: {qa_channel_id}" ) # categorize message, respond to messages of type '[Support Request]' categorize_response = await run_channel_msg_categorize(text) message_category = categorize_response["text"] bot_log( BotLogEntry( slack_context=src_evt_context, elapsed_ms=slack_utils.time_s_to_ms(categorize_response["duration"]), step_name="categorize_message", payload={ "user_input": text, "bot_name": "docs", "message_category": message_category, }, ) ) if message_category != "[Support Request]": # we only handle support requests, so done print( f'Assistant does not know what to do with messages of category: "{message_category}"' ) return first_message_text = ( f"<{src_msg_link}|Incoming message> from <#{main_channel_id}>" if hitl_enabled else f"" ) quoted_input = text.replace("\n", "\n>") if hitl_enabled: startMsg = app.client.chat_postMessage( text=first_message_text, blocks=[ { "type": "section", "text": { "type": "mrkdwn", "text": first_message_text, }, } ], channel=target_channel_id, ) thread_ts = startMsg["ts"] else: thread_ts = src_evt_context.ts if hitl_enabled: thread1 = app.client.chat_postMessage( text=f"Running {chain_name} chain...", channel=qa_channel_id, thread_ts=thread_ts, ) else: thread1 = say(text="Reading Altinn Studio docs...", thread_ts=thread_ts) rag_with_typesense_error = None try: rag_response = await rag_with_typesense(text) payload = { "user_input": text, "bot_name": "docs", "input_language": rag_response["input_language"], "search_queries": rag_response["search_queries"], "answer": rag_response["result"], "source_urls": rag_response["source_urls"], "relevant_urls": rag_response["relevant_urls"], "not_loaded_urls": rag_response.get("not_loaded_urls", []), } if rag_response["rag_success"] is not None: payload["rag_success"] = rag_response["rag_success"] except openai.error.ServiceUnavailableError as e: rag_with_typesense_error = f"OpenAI API error: {e}" except Exception as ex: rag_with_typesense_error = f"Error: {ex}" if rag_with_typesense_error: app.client.chat_postMessage( thread_ts=thread_ts, text=rag_with_typesense_error, channel=target_channel_id, ) return answer = rag_response["result"] relevant_sources = rag_response["relevant_urls"] answer_block = ( { "type": "section", "text": {"type": "mrkdwn", "text": answer}, "accessory": { "type": "button", "text": {"type": "plain_text", "text": f"Send"}, "value": f"{src_evt_context.team}|{src_evt_context.channel}|{src_evt_context.ts}", "action_id": "docs|qa|approve_reply", }, } if hitl_enabled else { "type": "section", "text": {"type": "mrkdwn", "text": answer}, } ) blocks = [ answer_block, ] if len(relevant_sources) > 0: links_mrkdwn = "\n".join( f"<{source['url']}|{source['title']}>" for source in relevant_sources ) blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": f"For more information:\n{links_mrkdwn}", }, } ) blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": f"Generated in {round(rag_response['durations']['total'], ndigits=1)} seconds.\n" + f"Please give us your feedback with a :+1: or :-1:", }, } ) reply_text = ( f"Answer:\n{answer}" if hitl_enabled else f'Here is what I found related to your query:\n >"{text}"\n\n_{answer}_' ) try: app.client.chat_update( channel=thread1["channel"], ts=thread1["ts"], text=reply_text, blocks=blocks, as_user=True, ) except SlackApiError as e: print(f"Error attempting to delete temp bot message {e}") bot_log( BotLogEntry( slack_context=slack_utils.get_context_from_thread_response(src_evt_context.ts, thread1), elapsed_ms=slack_utils.time_s_to_ms(rag_response["durations"]["total"]), durations=rag_response["durations"], step_name="rag_with_typesense", payload=payload, ) ) # known_path_segment = "altinn/docs/content" known_path_segment = "https://docs.altinn.studio" source_docs = rag_response["source_documents"] not_loaded_urls = rag_response["not_loaded_urls"] table_blocks = [] fields_list = "*Retrieved articles*\n" not_loaded_list = "" # Data rows for i, doc in enumerate(source_docs): source = doc["metadata"]["source"] path_segment_index = source.index(known_path_segment) if path_segment_index >= 0: slice_start = ( (-1 * len(source)) + path_segment_index + len(known_path_segment) + 1 ) source = "https://docs.altinn.studio/" + source[slice_start:] source = source.rpartition("/")[0] + "/" source_text = source.replace("https://docs.altinn.studio/", "") fields_list += f"#{i+1}: <{source}|{source_text}>\n" for i, url in enumerate(not_loaded_urls): not_loaded_list += ( f"#{i+1}: <{url}|{url.replace('https://docs.altinn.studio/', '')}>\n" ) search_queries_summary = "\n> ".join(rag_response["search_queries"]) table_blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": f"Phrases generated for retrieval:\n> {search_queries_summary}", }, } ) table_blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": fields_list, }, } ) if len(not_loaded_list) > 0: table_blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": f"*Retrieved, but not used:*\n{not_loaded_list}", }, } ) table_blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": f"Processing times (sec):\n```\n{json.dumps(rag_response['durations'], indent=2)}```", }, } ) # TODO: add to channel config db table if True: app.client.chat_postMessage( thread_ts=thread_ts, text="Retrieved documentation:", blocks=table_blocks, channel=target_channel_id, ) # TODO: add to channel config db table if False: say( thread_ts=thread_ts, channel=target_channel_id, blocks=[ { "type": "section", "text": { "type": "mrkdwn", "text": f"Processing times (sec):\n```\n{json.dumps(rag_response['durations'], indent=2)}```", }, } ], text=f"Processing times (sec): {rag_response['durations']['total']}", )
[]
2024-01-10
Altinn/digdir-slack-bot
code_qa~db_build.py
# ========================= # Module: Vector DB Build # ========================= import box import yaml import os from langchain.vectorstores import FAISS from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import DirectoryLoader from langchain.embeddings import OpenAIEmbeddings from langchain.document_loaders import UnstructuredMarkdownLoader # Import config vars with open('code_qa/config/config.yml', 'r', encoding='utf8') as ymlfile: cfg = box.Box(yaml.safe_load(ymlfile)) # Build vector database def run_db_build(): loader = DirectoryLoader(cfg.DATA_PATH, glob='**/*.cs.summary.txt', loader_cls=UnstructuredMarkdownLoader) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=cfg.CHUNK_SIZE, chunk_overlap=cfg.CHUNK_OVERLAP) texts = text_splitter.split_documents(documents) api_key = os.environ['OPENAI_API_KEY_ALTINN3_DEV'] embeddings = OpenAIEmbeddings(openai_api_key=api_key) vectorstore = FAISS.from_documents(texts, embeddings) vectorstore.save_local(cfg.DB_FAISS_PATH) if __name__ == "__main__": run_db_build()
[]
2024-01-10
Altinn/digdir-slack-bot
bots~github_qa.py
import json import pprint from slack_sdk.errors import SlackApiError import openai.error import utils.slack_utils as slack_utils from bots.structured_log import bot_log, BotLogEntry from github_qa.rag_issues import rag_with_typesense from channel_msg_categorize.run_chain import ( run_chain_async as run_channel_msg_categorize, ) pp = pprint.PrettyPrinter(indent=2) chain_name = "[gh-issues]" async def run_bot_async(app, hitl_config, say, msg_body, text): src_evt_context = slack_utils.get_event_context(msg_body) print(f"src_msg_metadata: ") pp.pprint(src_evt_context) main_channel_id = msg_body.get("event").get("channel") target_channel_id = main_channel_id qa_channel_id = hitl_config.get("qa_channel", "") src_msg_link = "" hitl_enabled = qa_channel_id != "" and hitl_config.get("enabled") # override target_channel_id if hitl enabled if hitl_enabled: target_channel_id = qa_channel_id src_msg_link = slack_utils.get_message_permalink(app, msg_body) print( f"hitl enabled: {hitl_enabled}, main_channel_id: {main_channel_id}, qa_channel_id: {qa_channel_id}" ) # categorize message, respond to messages of type '[Support Request]' categorize_response = await run_channel_msg_categorize(text) message_category = categorize_response["text"] print(f"Message category: {message_category}") bot_log( BotLogEntry( slack_context=src_evt_context, elapsed_ms=slack_utils.time_s_to_ms(categorize_response["duration"]), step_name="categorize_message", payload={ "user_input": text, "bot_name": "gh_issues", "message_category": message_category, }, ) ) # if message_category != "[Support Request]": # # we only handle support requests, so done # print( # f'Assistant does not know what to do with messages of category: "{message_category}"' # ) # return first_message_text = ( f"<{src_msg_link}|Incoming message> from <#{main_channel_id}>" if hitl_enabled else f"" ) quoted_input = text.replace("\n", "\n>") if hitl_enabled: startMsg = app.client.chat_postMessage( text=first_message_text, blocks=[ { "type": "section", "text": { "type": "mrkdwn", "text": first_message_text, }, } ], channel=target_channel_id, ) thread_ts = startMsg["ts"] else: thread_ts = src_evt_context.ts if hitl_enabled: thread1 = app.client.chat_postMessage( text=f"Running {chain_name} chain...", channel=qa_channel_id, thread_ts=thread_ts, ) else: thread1 = say(text="Checking Github issues", thread_ts=thread_ts) try: rag_response = await rag_with_typesense(text) payload = { "user_input": text, "bot_name": chain_name, "search_queries": rag_response["search_queries"], "answer": rag_response["result"], "source_urls": rag_response["source_urls"], "relevant_urls": rag_response["relevant_urls"], } if rag_response["rag_success"] is not None: payload["rag_success"] = rag_response["rag_success"] bot_log( BotLogEntry( slack_context=src_evt_context, elapsed_ms=slack_utils.time_s_to_ms(rag_response["durations"]["total"]), durations=rag_response["durations"], step_name="rag_with_typesense", payload=payload, ) ) except openai.error.ServiceUnavailableError as e: print(f"OpenAI API error: {e}") app.client.chat_postMessage( thread_ts=thread_ts, text=f"OpenAI API error: {e}", channel=target_channel_id, ) return except Exception as e: print(f"Error: {e}") app.client.chat_postMessage( thread_ts=thread_ts, text=f"Error: {e}", channel=target_channel_id, ) return answer = rag_response["result"] relevant_sources = rag_response["relevant_urls"] answer_block = ( { "type": "section", "text": {"type": "mrkdwn", "text": answer}, "accessory": { "type": "button", "text": {"type": "plain_text", "text": f"Send"}, "value": f"{src_evt_context.team}|{src_evt_context.channel}|{src_evt_context.ts}", "action_id": "docs|qa|approve_reply", }, } if hitl_enabled else { "type": "section", "text": {"type": "mrkdwn", "text": answer}, } ) blocks = [answer_block] if len(relevant_sources) > 0: links_mrkdwn = "\n".join( f"<{source['url']}|{source['title']}>" for source in relevant_sources ) blocks.append( { "type": "section", "text": {"type": "mrkdwn", "text": f"Relevant links:\n{links_mrkdwn}"}, } ) blocks.append({ "type": "section", "text": {"type": "mrkdwn", "text": f"Generated in {round(rag_response['durations']['total'], ndigits=1)} seconds."}, }) reply_text = ( f"Suggested reply:\n{answer}" if hitl_enabled else f'Here is what I found related to your query:\n >"{text}"\n\n_{answer}_' ) try: app.client.chat_update( channel=thread1["channel"], ts=thread1["ts"], text=reply_text, blocks=blocks, as_user=True, ) except SlackApiError as e: print(f"Error attempting to delete temp bot message {e}") # Process source documents source_blocks = [] source_docs = rag_response["source_documents"] for i, doc in enumerate(source_docs): # print(f"doc {i}:\n{doc}") source = doc["metadata"]["source"] vector_distance = round(doc.get("vector_distance"), 3) source_summary = f"{i+1}: <{source}|{doc.get('title','')}> [{doc.get('state', '')}]" label_list = doc.get('labels', []) if len(label_list) > 0: label_list = [f"`{label}`" for label in label_list] source_summary += f"\n> labels: {' '.join(label_list)}" source_blocks.append( { "type": "section", "text": {"type": "mrkdwn", "text": source_summary}, }) source_blocks.append({ "type": "section", "text": { "type": "mrkdwn", "text": f"Processing times (sec):\n```\n{json.dumps(rag_response['durations'], indent=2)}```", }, }) # TODO: add to channel config if True: app.client.chat_postMessage( thread_ts=thread_ts, text="Retrieved Github issues", blocks=source_blocks, channel=target_channel_id, )
[]
2024-01-10
Altinn/digdir-slack-bot
code_qa~ask_openai.py
# ========================= # Module: Summarize code # ========================= import openai import os import argparse def ask_openai(system: str, user: str) -> str: """ Send a message to OpenAI and get the response. Parameters: - system (str): The system prompt to the assistant. - user (str): The user's prompt to the assistant. Returns: - str: The assistant's reply. """ # Load your OpenAI API key from the environment variable openai.api_key = os.environ.get('OPENAI_API_KEY_ALTINN3_DEV') openai.api_base = os.environ['OPENAI_API_KEY_ALTINN3_DEV'] # Ensure API key is present if not openai.api_key: raise ValueError("Missing value for environment variable 'OPENAI_API_KEY_ALTINN3_DEV'") # Define the message to be sent messages = [{'role': 'system', 'content': system}, {'role': 'user', 'content': user[0:4096]}] # print(f'messages: {messages}') # Send the message to the OpenAI API response = openai.ChatCompletion.create( model="gpt-4", messages=messages, temperature=0.1 ) # Extract and return the assistant's reply return response.choices[0].message['content'] if __name__ == "__main__": parser = argparse.ArgumentParser(description="Ask OpenAI based on a system prompt and user prompt from files.") parser.add_argument("system", help="Name of the file containing the system prompt.") parser.add_argument("user", help="Name of the file containing the user prompt.") parser.add_argument("output", help="Name of the file to save results.") args = parser.parse_args() with open(args.system, 'r') as sysfile: system_input = sysfile.read() with open(args.user, 'r') as userfile: user_input = userfile.read() response = ask_openai(system_input, user_input) with open(args.output, 'wt') as outputfile: outputfile.write(response) print(f"Assistant:\n\n {response}")
[]
2024-01-10
Altinn/digdir-slack-bot
docs_qa~rag_manual_stuff.py
import box import timeit import yaml import pprint from langchain.pydantic_v1 import BaseModel, Field from langchain.prompts import ChatPromptTemplate from langchain.chains.openai_functions import ( create_structured_output_chain ) from docs_qa.chains import build_llm from docs_qa.prompts import qa_template from docs_qa.extract_search_terms import run_query_async import docs_qa.typesense_search as search from typing import Sequence from .config import config pp = pprint.PrettyPrinter(indent=2) cfg = config() # Import config vars with open('docs_qa/config/config.yml', 'r', encoding='utf8') as ymlfile: cfg = box.Box(yaml.safe_load(ymlfile)) class RagContextRefs(BaseModel): # relevant_content: str = Field(..., description="Three or four sentences from the most relevant parts of the context document") source: str = Field(..., description="The metadata.source property") class RagPromptReply(BaseModel): """Relevant context data""" helpful_answer: str = Field(..., description="The helpful answer") i_dont_know: bool = Field(..., description="True when unable to answer based on the given context.") relevant_contexts: Sequence[RagContextRefs] = Field(..., description="List of context documents that were relevant when answering the question.") async def rag_with_typesense(user_input): durations = { 'generate_searches': 0, 'phrase_similarity_search': 0, 'rag_query': 0, 'total': 0 } total_start = start = timeit.default_timer() extract_search_queries = await run_query_async(user_input) durations['generate_searches'] = timeit.default_timer() - start print(f'Query language code: {extract_search_queries.userInputLanguage}') start = timeit.default_timer() search_phrase_hits = await search.lookup_search_phrases_similar(extract_search_queries) durations['phrase_similarity_search'] = timeit.default_timer() - start # print(f'url list:') # pp.pprint(search_phrase_hits) start = timeit.default_timer() search_response = await search.typesense_retrieve_all_by_url(search_phrase_hits) durations['execute_searches'] = timeit.default_timer() - start search_hits = [ { 'id': document['document']['id'], 'url': document['document']['url_without_anchor'], 'lvl0': document['document']['hierarchy.lvl0'], 'content_markdown': document['document'].get('content_markdown', ''), } for result in search_response['results'] for hit in result['grouped_hits'] for document in hit['hits'] ] # print(f'All source document urls:') # pp.pprint(search_hits) start = timeit.default_timer() loaded_docs = [] loaded_urls = [] loaded_search_hits = [] doc_index = 0 docs_length = 0 # need to preserve order in documents list # should only append doc if context is not too big while doc_index < len(search_hits): search_hit = search_hits[doc_index] doc_index += 1 unique_url = search_hit['url'] if unique_url in loaded_urls: continue doc_md = search_hit['content_markdown'] doc_trimmed = doc_md[:cfg.MAX_SOURCE_LENGTH] if (docs_length + len(doc_trimmed)) > cfg.MAX_CONTEXT_LENGTH: doc_trimmed = doc_trimmed[:cfg.MAX_CONTEXT_LENGTH - docs_length - 20] if len(doc_trimmed) == 0: break loaded_doc = { 'page_content': doc_trimmed, 'metadata': { 'source': unique_url, } } print(f'loaded markdown doc, length= {len(doc_trimmed)}, url= {unique_url}') # pp.pprint(loaded_doc) docs_length += len(doc_trimmed) loaded_docs.append(loaded_doc) loaded_urls.append(unique_url) loaded_search_hits.append(search_hit) if docs_length >= cfg.MAX_CONTEXT_LENGTH: print(f'MAX_CONTEXT_LENGTH: {cfg.MAX_CONTEXT_LENGTH} exceeded, loaded {len(loaded_docs)} docs.') break if len(loaded_docs) >= cfg.MAX_CONTEXT_DOC_COUNT: break not_loaded_urls = [] for hit in search_hits: url = hit['url'] if url not in loaded_urls and url not in not_loaded_urls: not_loaded_urls.append(url) durations['download_docs'] = timeit.default_timer() - start print(f'stuffed source document urls:') # pp.pprint(loaded_urls) print(f'Starting RAG structured output chain, llm: {cfg.MODEL_TYPE}') start = timeit.default_timer() llm = build_llm() prompt = ChatPromptTemplate.from_messages( [('system', 'You are a helpful assistant.'), ('human', qa_template)] ) runnable = create_structured_output_chain(RagPromptReply, llm, prompt) result = runnable.invoke({ "context": yaml.dump(loaded_docs), "question": user_input }) durations['rag_query'] = timeit.default_timer() - start durations['total'] = timeit.default_timer() - total_start # print(f"Time to run RAG structured output chain: {chain_end - chain_start} seconds") # print(f'runnable result:') # pp.pprint(result) if result['function'] is not None: relevant_sources = [{ 'url': context.source, 'title': next((hit['lvl0'] for hit in search_hits if hit['url'] == context.source), None), } for context in result['function'].relevant_contexts] rag_success = result['function'].i_dont_know != True else: relevant_sources = [] # rag_success = None response = { 'result': result['function'].helpful_answer, 'input_language': extract_search_queries.userInputLanguage, 'rag_success': rag_success, 'search_queries': extract_search_queries.searchQueries, 'source_urls': loaded_urls, 'source_documents': loaded_docs, 'relevant_urls': relevant_sources, 'not_loaded_urls': not_loaded_urls, 'durations': durations, } # pp.pprint(response) return response
[ "document", "human", "[('system', 'You are a helpful assistant.'), ('human', PLACEHOLDER)]", "You are a helpful assistant.", "content_markdown" ]
2024-01-10
Altinn/digdir-slack-bot
docs_qa~extract_search_terms.py
import os import openai import instructor from pydantic import BaseModel, Field import pprint instructor.patch() openai.api_type = 'azure' openai.api_key = os.environ['OPENAI_API_KEY_ALTINN3_DEV'] openai.api_base = os.environ['OPENAI_API_URL_ALTINN3_DEV'] openai.api_version = os.environ['AZURE_OPENAI_VERSION'] class GeneratedSearchQueries(BaseModel): searchQueries: list[str] = Field(..., description="Array of search queries.") userInputLanguage: str = Field(..., description="ISO 639-1 language code for the original question") pp = pprint.PrettyPrinter(indent=2) async def run_query_async(user_input) -> GeneratedSearchQueries: query_result: GeneratedSearchQueries = openai.ChatCompletion.create( engine=os.environ['AZURE_OPENAI_DEPLOYMENT'], response_model=GeneratedSearchQueries, temperature=0.1, messages=[ {"role": "system", "content": f"""You have access to a search API that returns relevant documentation. 1. If the question is not in English, first translate to English. 2. Generate the shortest set of search terms that sufficiently limits the set of expected query results. 3. Generate an array of up to 7 search queries that are relevant to this question. Use a variation of related keywords and synonyms for the queries, trying to be as general as possible. Include as many queries as you can think of, including and excluding terms. For example, include queries like ['keyword_1 keyword_2', 'keyword_1', 'keyword_2']. Be creative. The more queries you include, the more likely you are to find relevant results."""}, {"role": "user", "content": user_input}, ] ) for i in range(len(query_result.searchQueries)): query_result.searchQueries[i] = query_result.searchQueries[i].replace("GitHub", "").strip() return query_result
[ "You have access to a search API that returns relevant documentation.\n\n1. If the question is not in English, first translate to English. \n\n2. Generate the shortest set of search terms that sufficiently limits the set of expected query results.\n\n3. Generate an array of up to 7 search queries that are relevant to this question. Use a variation of related keywords and synonyms for the queries, trying to be as general as possible.\nInclude as many queries as you can think of, including and excluding terms.\nFor example, include queries like ['keyword_1 keyword_2', 'keyword_1', 'keyword_2'].\nBe creative. The more queries you include, the more likely you are to find relevant results." ]
2024-01-10
Altinn/digdir-slack-bot
channel_msg_categorize~build_chain.py
''' =========================================== Module: Chain functions =========================================== ''' from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores import FAISS from .prompts import categorize_new_message from .llm import build_llm from .config_chain import config cfg = config() def load_prompt(): """ Prompt template for QA retrieval for each vectorstore """ prompt = PromptTemplate(template=categorize_new_message, input_variables=['question']) return prompt def build_retrieval_choose_team(llm, prompt): dbqa = LLMChain(llm=llm, prompt=prompt, verbose=False) return dbqa def setup_dbqa(): llm = build_llm() loaded_prompt = load_prompt() dbqa = build_retrieval_choose_team(llm, loaded_prompt) return dbqa def query(dbqa, user_input): if cfg.MODEL_TYPE.startswith("gpt"): return dbqa(user_input) else: return dbqa({'query': user_input})
[ "question" ]
2024-01-10
Altinn/digdir-slack-bot
team_qa_choose~db_build.py
# ========================= # Module: Vector DB Build # ========================= import box import yaml from langchain.vectorstores import FAISS from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import DirectoryLoader from langchain.embeddings import HuggingFaceEmbeddings from langchain.document_loaders import UnstructuredMarkdownLoader from .config_chain import config cfg = config() # Build vector database def run_db_build(): loader = DirectoryLoader(cfg.DATA_PATH, glob='**/*.en.md', loader_cls=UnstructuredMarkdownLoader) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=cfg.CHUNK_SIZE, chunk_overlap=cfg.CHUNK_OVERLAP) texts = text_splitter.split_documents(documents) embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'}) vectorstore = FAISS.from_documents(texts, embeddings) vectorstore.save_local(cfg.DB_FAISS_PATH) if __name__ == "__main__": run_db_build()
[]
2024-01-10
Altinn/digdir-slack-bot
github_qa~rag_issues.py
import timeit import yaml import pprint from langchain.pydantic_v1 import BaseModel, Field from langchain.prompts import ChatPromptTemplate from langchain.chains.openai_functions import ( create_structured_output_chain ) from docs_qa.chains import build_llm from github_qa.prompts import qa_template from docs_qa.extract_search_terms import run_query_async from github_qa.typesense_search import typesense_search_multiple from typing import Sequence from .config import config pp = pprint.PrettyPrinter(indent=2) cfg = config() class RagContextRefs(BaseModel): # relevant_content: str = Field(..., description="Three or four sentences from the most relevant parts of the context document") source: str = Field(..., description="The metadata.source property") class RagPromptReply(BaseModel): """Relevant context data""" helpful_answer: str = Field(..., description="The helpful answer") i_dont_know: bool = Field(..., description="True when unable to answer based on the given context.") relevant_contexts: Sequence[RagContextRefs] = Field(..., description="List of context documents that were relevant when answering the question.") async def rag_with_typesense(user_input): durations = { 'generate_searches': 0, 'execute_searches': 0, 'rag_query': 0, 'total': 0 } total_start = start = timeit.default_timer() extract_search_queries = await run_query_async(user_input) durations['generate_searches'] = timeit.default_timer() - start # print(f'generated queries:') # pp.pprint(extract_search_queries) start = timeit.default_timer() try: search_response = await typesense_search_multiple(extract_search_queries) except Exception as e: print(f"An error occurred: {e}") pass durations['execute_searches'] = timeit.default_timer() - start # print(f'search response:') # pp.pprint(search_response) search_hits = [ { 'id': document['document']['id'], 'url': document['document'].get('url', ''), 'title': document['document'].get('title'), 'body': document['document'].get('body')[:1000], 'labels': document['document'].get('labels', []), 'state': document['document'].get('state', ''), 'closed_at': document['document'].get('closed_at'), 'vector_distance': document.get('vector_distance', 1.0), } for result in search_response['results'] if 'hits' in result for document in result['hits'] ] if len(search_hits) == 0: raise Exception("NoSearchResults") # print(f'All issues found:') # pp.pprint(search_hits) # need to preserve order in documents list # should only append doc if context is not too big loaded_docs = [] loaded_source_urls = [] doc_index = 0 docs_length = 0 while doc_index < len(search_hits): search_hit = search_hits[doc_index] doc_index += 1 doc_trimmed = search_hit.get('body','')[:cfg.MAX_SOURCE_LENGTH] loaded_doc = { 'title': search_hit.get('title', ''), 'labels': search_hit.get('labels', []), 'state': search_hit.get('state', ''), 'vector_distance': search_hit.get('vector_distance', 1.0), 'page_content': doc_trimmed, 'metadata': { 'source': search_hit.get('url', ''), } } closed_at = search_hit.get('closed_at') if closed_at: loaded_doc['closed_at'] = closed_at # skip hits that are clearly out of range # if search_hit.get('vector_distance', 1.0) > 0.9: # continue if (docs_length + len(doc_trimmed)) > cfg.MAX_CONTEXT_LENGTH: break # limit result set length if len(loaded_docs) > 8: break docs_length += len(doc_trimmed) loaded_docs.append(loaded_doc) loaded_source_urls.append(search_hit.get('url', '')) print(f'Starting RAG structured output chain, llm: {cfg.MODEL_TYPE}') start = timeit.default_timer() llm = build_llm() prompt = ChatPromptTemplate.from_messages( [('system', 'You are a helpful assistant.'), ('human', qa_template)] ) runnable = create_structured_output_chain(RagPromptReply, llm, prompt) result = runnable.invoke({ "context": yaml.dump(loaded_docs), "question": user_input }) durations['rag_query'] = timeit.default_timer() - start durations['total'] = timeit.default_timer() - total_start # print(f"Time to run RAG structured output chain: {chain_end - chain_start} seconds") # print(f'runnable result:') # pp.pprint(result) if result['function'] is not None: relevant_sources = [{ 'url': context.source, 'title': next((hit['title'] for hit in search_hits if hit['url'] == context.source), None), } for context in result['function'].relevant_contexts] rag_success = result['function'].i_dont_know != True else: relevant_sources = [] response = { 'result': result['function'].helpful_answer, 'rag_success': rag_success, 'search_queries': extract_search_queries.searchQueries, 'source_urls': loaded_source_urls, 'source_documents': loaded_docs, 'relevant_urls': relevant_sources, 'durations': durations } # pp.pprint(response) return response
[ "human", "You are a helpful assistant.", "[('system', 'You are a helpful assistant.'), ('human', PLACEHOLDER)]" ]
2024-01-10
Altinn/digdir-slack-bot
docs_qa~db_build.py
# ========================= # Module: Vector DB Build # ========================= import box import yaml import os from langchain.vectorstores import FAISS from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import DirectoryLoader from langchain.embeddings import OpenAIEmbeddings from langchain.document_loaders import UnstructuredMarkdownLoader # Import config vars with open('docs_qa/config/config.yml', 'r', encoding='utf8') as ymlfile: cfg = box.Box(yaml.safe_load(ymlfile)) # Build vector database def run_db_build(): loader = DirectoryLoader(cfg.DATA_PATH, glob='**/*.en.md', loader_cls=UnstructuredMarkdownLoader) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=cfg.CHUNK_SIZE, chunk_overlap=cfg.CHUNK_OVERLAP) texts = text_splitter.split_documents(documents) api_key = os.environ['OPENAI_API_KEY_ALTINN3_DEV'] endpoint = os.environ['OPENAI_API_URL_ALTINN3_DEV'] embeddings = OpenAIEmbeddings(openai_api_key=api_key, openai_api_base=endpoint) vectorstore = FAISS.from_documents(texts, embeddings) vectorstore.save_local(cfg.DB_FAISS_PATH) if __name__ == "__main__": run_db_build()
[]
2024-01-10
Altinn/digdir-slack-bot
team_qa_choose~build_chain.py
''' =========================================== Module: Chain functions =========================================== ''' import box import yaml from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores import FAISS from .prompts import choose_team_template from .llm import build_llm from .config_chain import config cfg = config() def set_choose_team_prompt(): """ Prompt template for QA retrieval for each vectorstore """ prompt = PromptTemplate(template=choose_team_template, input_variables=['question']) return prompt def build_retrieval_choose_team(llm, prompt): dbqa = LLMChain(llm=llm, prompt=prompt, verbose=False) return dbqa def setup_dbqa(): # embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", # model_kwargs={'device': 'cpu'}) # vectordb = FAISS.load_local(cfg.DB_FAISS_PATH, embeddings) llm = build_llm() choose_team_prompt = set_choose_team_prompt() dbqa = build_retrieval_choose_team(llm, choose_team_prompt) return dbqa def query(dbqa, user_input): if cfg.MODEL_TYPE == "gpt-4": return dbqa(user_input) else: return dbqa({'query': user_input})
[ "question" ]
2024-01-10
sashedher/Geonames
ResultOnto.py
# import GeoOnto from rdflib import Graph import geocoder import re from pprint import pprint # import ResultOntoIndiv import openAI print("\n\n-----------------------------India----------------------\n\n") Feature_code={ 'A': ["country","state", "region"], 'H': ["stream","lake"], 'L': ["parks","area"], 'P': ["city","village"], 'R': ["road","railroad"], 'S': ["spot","building","farm"], 'T': ["mountain","hill","rock"], 'U': ["undersea"], 'V': ["forest","eath"] } SubFeature_Code={ "A" : { "ADM1" : "first-order administrative division.A primary administrative division of a country, such as a state in the United States", "ADM1H" : "historicalk first-order administrative division.A former first-order administrative division", "ADM2" : "second-order administrative division.A subdivision of a first-order administrative division", "ADM2H" : "historical second-order administrative division.A former second-order administrative division", "ADM3" : "third-order administrative division.A subdivision of a second-order administrative division", "ADM3H" : "historical third-order administrative division.A former third-order administrative division", "ADM4" : "fourth-order administrative division.A subdivision of a third-order administrative division", "ADM4H" : "historical fourth-order administrative division.A former fourth-order administrative division", "ADM5" : "fifth-order administrative division.A subdivision of a fourth-order administrative division", "ADM5H" : "historical fifth-order administrative division.A former fifth-order administrative division", "ADMD" : "administrative division.An administrative division of a country, undifferentiated as to administrative level", "ADMDH" : "historical administrative division.A former administrative division of a political entity, undifferentiated as to administrative level", "LTER" : "leased area.A tract of land leased to another country, usually for military installations", "PCL" : "political entity ", "PCLD" : "dependent political entity ", "PCLF" : "freely associated state ", "PCLH" : "historical political entity.A former political entity", "PCLI" : "independent political entity ", "PCLIX" : "section of independent political entity ", "PCLS" : "semi-independent political entity ", "PRSH" : "parish.An ecclesiastical district", "TERR" : "territory ", "ZN" : "zone ", "ZNB" : "buffer zone.A zone recognized as a buffer between two nations in which military presence is minimal or absent", }, "H" : { "AIRS" : "seaplane landing area.A place on a waterbody where floatplanes land and take off", "ANCH" : "anchorage.An area where vessels may anchor", "BAY" : "bay.A coastal indentation between two capes or headlands, larger than a cove but smaller than a gulf", "BAYS" : "bays.coastal indentations between two capes or headlands, larger than a cove but smaller than a gulf", "BGHT" : "bight(s).An open body of water forming a slight recession in a coastline", "BNK" : "bank(s) .An elevation, typically located on a shelf, over which the depth of water is relatively shallow but sufficient for most surface navigation", "BNKR" : "stream bank .A sloping margin of a stream channel which normally confines the stream to its channel on land", "BNKX" : "section of bank.", "BOG" : "bog(s).A wetland characterized by peat forming sphagnum moss, sedge, and other acid-water plants", "CAPG" : "icecap.A dome-shaped mass of glacial ice covering an area of mountain summits or other high lands; smaller than an ice sheet", "CHN" : "channel.the deepest part of a stream, bay, lagoon, or strait, through which the main current flows", "CHNL" : "lake channel(s) .that part of a lake having water deep enough for navigation between islands, shoals, etc.", "CHNM" : "marine channel.that part of a body of water deep enough for navigation through an area otherwise not suitable", "CHNN" : "navigation channel.A buoyed channel of sufficient depth for the safe navigation of vessels", "CNFL" : "confluence.A place where two or more streams or intermittent streams flow together", "CNL" : "canal.An artificial watercourse", "CNLA" : "aqueduct.A conduit used to carry water", "CNLB" : "canal bend.A conspicuously curved or bent section of a canal", "CNLD" : "drainage canal.An artificial waterway carrying water away from a wetland or from drainage ditches", "CNLI" : "irrigation canal.A canal which serves as a main conduit for irrigation water", "CNLN" : "navigation canal(s).A watercourse constructed for navigation of vessels", "CNLQ" : "abandoned canal.", "CNLSB" : "underground irrigation canal(s).A gently inclined underground tunnel bringing water for irrigation from aquifers", "CNLX" : "section of canal.", "COVE" : "cove(s).A small coastal indentation, smaller than a bay", "CRKT" : "tidal creek(s).A meandering channel in a coastal wetland subject to bi-directional tidal currents", "CRNT" : "current.A horizontal flow of water in a given direction with uniform velocity", "CUTF" : "cutoff.A channel formed as a result of a stream cutting through a meander neck", "DCK" : "dock(s).A waterway between two piers, or cut into the land for the berthing of ships", "DCKB" : "docking basin.A part of a harbor where ships dock", "DOMG" : "icecap dome.A comparatively elevated area on an icecap", "DPRG" : "icecap depression.A comparatively depressed area on an icecap", "DTCH" : "ditch.A small artificial watercourse dug for draining or irrigating the land", "DTCHD" : "drainage ditch.A ditch which serves to drain the land", "DTCHI" : "irrigation ditch.A ditch which serves to distribute irrigation water", "DTCHM" : "ditch mouth(s).An area where a drainage ditch enters a lagoon, lake or bay", "ESTY" : "estuary.A funnel-shaped stream mouth or embayment where fresh water mixes with sea water under tidal influences", "FISH" : "fishing area.A fishing ground, bank or area where fishermen go to catch fish", "FJD" : "fjord.A long, narrow, steep-walled, deep-water arm of the sea at high latitudes, usually along mountainous coasts", "FJDS" : "fjords.long, narrow, steep-walled, deep-water arms of the sea at high latitudes, usually along mountainous coasts", "FLLS" : "waterfall(s).A perpendicular or very steep descent of the water of a stream", "FLLSX" : "section of waterfall(s).", "FLTM" : "mud flat(s).A relatively level area of mud either between high and low tide lines, or subject to flooding", "FLTT" : "tidal flat(s).A large flat area of mud or sand attached to the shore and alternately covered and uncovered by the tide", "GLCR" : "glacier(s).A mass of ice, usually at high latitudes or high elevations, with sufficient thickness to flow away from the source area in lobes, tongues, or masses", "GULF" : "gulf.A large recess in the coastline, larger than a bay", "GYSR" : "geyser.A type of hot spring with intermittent eruptions of jets of hot water and steam", "HBR" : "harbor(s).A haven or space of deep water so sheltered by the adjacent land as to afford a safe anchorage for ships", "HBRX" : "section of harbor.", "INLT" : "inlet.A narrow waterway extending into the land, or connecting a bay or lagoon with a larger body of water", "INLTQ" : "former inlet.An inlet which has been filled in, or blocked by deposits", "LBED" : "lake bed(s).A dried up or drained area of a former lake", "LGN" : "lagoon.A shallow coastal waterbody, completely or partly separated from a larger body of water by a barrier island, coral reef or other depositional feature", "LGNS" : "lagoons.shallow coastal waterbodies, completely or partly separated from a larger body of water by a barrier island, coral reef or other depositional feature", "LGNX" : "section of lagoon.", "LK" : "lake.A large inland body of standing water", "LKC" : "crater lake.A lake in a crater or caldera", "LKI" : "intermittent lake.", "LKN" : "salt lake.An inland body of salt water with no outlet", "LKNI" : "intermittent salt lake.", "LKO" : "oxbow lake.A crescent-shaped lake commonly found adjacent to meandering streams", "LKOI" : "intermittent oxbow lake.", "LKS" : "lakes.large inland bodies of standing water", "LKSB" : "underground lake.A standing body of water in a cave", "LKSC" : "crater lakes.lakes in a crater or caldera", "LKSI" : "intermittent lakes.", "LKSN" : "salt lakes.inland bodies of salt water with no outlet", "LKSNI" : "intermittent salt lakes.", "LKX" : "section of lake.", "MFGN" : "salt evaporation ponds.diked salt ponds used in the production of solar evaporated salt", "MGV" : "mangrove swamp.A tropical tidal mud flat characterized by mangrove vegetation", "MOOR" : "moor(s).An area of open ground overlaid with wet peaty soils", "MRSH" : "marsh(es).A wetland dominated by grass-like vegetation", "MRSHN" : "salt marsh.A flat area, subject to periodic salt water inundation, dominated by grassy salt-tolerant plants", "NRWS" : "narrows.A navigable narrow part of a bay, strait, river, etc.", "OCN" : "ocean.one of the major divisions of the vast expanse of salt water covering part of the earth", "OVF" : "overfalls.An area of breaking waves caused by the meeting of currents or by waves moving against the current", "PND" : "pond.A small standing waterbody", "PNDI" : "intermittent pond.", "PNDN" : "salt pond.A small standing body of salt water often in a marsh or swamp, usually along a seacoast", "PNDNI" : "intermittent salt pond(s).", "PNDS" : "ponds.small standing waterbodies", "PNDSF" : "fishponds.ponds or enclosures in which fish are kept or raised", "PNDSI" : "intermittent ponds.", "PNDSN" : "salt ponds.small standing bodies of salt water often in a marsh or swamp, usually along a seacoast", "POOL" : "pool(s).A small and comparatively still, deep part of a larger body of water such as a stream or harbor; or a small body of standing water", "POOLI" : "intermittent pool.", "RCH" : "reach.A straight section of a navigable stream or channel between two bends", "RDGG" : "icecap ridge.A linear elevation on an icecap", "RDST" : "roadstead.An open anchorage affording less protection than a harbor", "RF" : "reef(s).A surface-navigation hazard composed of consolidated material", "RFC" : "coral reef(s).A surface-navigation hazard composed of coral", "RFX" : "section of reef.", "RPDS" : "rapids.A turbulent section of a stream associated with a steep, irregular stream bed", "RSV" : "reservoir(s).An artificial pond or lake", "RSVI" : "intermittent reservoir.", "RSVT" : "water tank.A contained pool or tank of water at, below, or above ground level", "RVN" : "ravine(s).A small, narrow, deep, steep-sided stream channel, smaller than a gorge", "SBKH" : "sabkha(s).A salt flat or salt encrusted plain subject to periodic inundation from flooding or high tides", "SD" : "sound.A long arm of the sea forming a channel between the mainland and an island or islands; or connecting two larger bodies of water", "SEA" : "sea.A large body of salt water more or less confined by continuous land or chains of islands forming a subdivision of an ocean", "SHOL" : "shoal(s).A surface-navigation hazard composed of unconsolidated material", "SILL" : "sill.the low part of an underwater gap or saddle separating basins, including a similar feature at the mouth of a fjord", "SPNG" : "spring(s).A place where ground water flows naturally out of the ground", "SPNS" : "sulphur spring(s).A place where sulphur ground water flows naturally out of the ground", "SPNT" : "hot spring(s).A place where hot ground water flows naturally out of the ground", "STM" : "stream.A body of running water moving to a lower level in a channel on land", "STMA" : "anabranch.A diverging branch flowing out of a main stream and rejoining it downstream", "STMB" : "stream bend.A conspicuously curved or bent segment of a stream", "STMC" : "canalized stream.A stream that has been substantially ditched, diked, or straightened", "STMD" : "distributary(-ies).A branch which flows away from the main stream, as in a delta or irrigation canal", "STMH" : "headwaters.the source and upper part of a stream, including the upper drainage basin", "STMI" : "intermittent stream.", "STMIX" : "section of intermittent stream.", "STMM" : "stream mouth(s).A place where a stream discharges into a lagoon, lake, or the sea", "STMQ" : "abandoned watercourse.A former stream or distributary no longer carrying flowing water, but still evident due to lakes, wetland, topographic or vegetation patterns", "STMS" : "streams.bodies of running water moving to a lower level in a channel on land", "STMSB" : "lost river.A surface stream that disappears into an underground channel, or dries up in an arid area", "STMX" : "section of stream.", "STRT" : "strait.A relatively narrow waterway, usually narrower and less extensive than a sound, connecting two larger bodies of water", "SWMP" : "swamp.A wetland dominated by tree vegetation", "SYSI" : "irrigation system.A network of ditches and one or more of the following elements: water supply, reservoir, canal, pump, well, drain, etc.", "TNLC" : "canal tunnel.A tunnel through which a canal passes", "WAD" : "wadi.A valley or ravine, bounded by relatively steep banks, which in the rainy season becomes a watercourse; found primarily in North Africa and the Middle East", "WADB" : "wadi bend.A conspicuously curved or bent segment of a wadi", "WADJ" : "wadi junction.A place where two or more wadies join", "WADM" : "wadi mouth.the lower terminus of a wadi where it widens into an adjoining floodplain, depression, or waterbody", "WADS" : "wadies.valleys or ravines, bounded by relatively steep banks, which in the rainy season become watercourses; found primarily in North Africa and the Middle East", "WADX" : "section of wadi.", "WHRL" : "whirlpool.A turbulent, rotating movement of water in a stream", "WLL" : "well.A cylindrical hole, pit, or tunnel drilled or dug down to a depth from which water, oil, or gas can be pumped or brought to the surface", "WLLQ" : "abandoned well.", "WLLS" : "wells.cylindrical holes, pits, or tunnels drilled or dug down to a depth from which water, oil, or gas can be pumped or brought to the surface", "WTLD" : "wetland.An area subject to inundation, usually characterized by bog, marsh, or swamp vegetation", "WTLDI" : "intermittent wetland.", "WTRC" : "watercourse.A natural, well-defined channel produced by flowing water, or an artificial channel designed to carry flowing water", "WTRH" : "waterhole(s).A natural hole, hollow, or small depression that contains water, used by man and animals, especially in arid areas" }, "L" : { "AGRC" : "agricultural colony.A tract of land set aside for agricultural settlement", "AMUS" : "amusement park.Amusement Park are theme parks, adventure parks offering entertainment, similar to funfairs but with a fix location", "AREA" : "area.A tract of land without homogeneous character or boundaries", "BSND" : "drainage basin.An area drained by a stream", "BSNP" : "petroleum basin.An area underlain by an oil-rich structural basin", "BTL" : "battlefield.A site of a land battle of historical importance", "CLG" : "clearing.An area in a forest with trees removed", "CMN" : "common.A park or pasture for community use", "CNS" : "concession area.A lease of land by a government for economic development, e.g., mining, forestry", "COLF" : "coalfield.A region in which coal deposits of possible economic value occur", "CONT" : "continent.continent: Europe, Africa, Asia, North America, South America, Oceania, Antarctica", "CST" : "coast.A zone of variable width straddling the shoreline", "CTRB" : "business center.A place where a number of businesses are located", "DEVH" : "housing development.A tract of land on which many houses of similar design are built according to a development plan", "FLD" : "field(s).An open as opposed to wooded area", "FLDI" : "irrigated field(s).A tract of level or terraced land which is irrigated", "GASF" : "gasfield.An area containing a subterranean store of natural gas of economic value", "GRAZ" : "grazing area.An area of grasses and shrubs used for grazing", "GVL" : "gravel area.An area covered with gravel", "INDS" : "industrial area.An area characterized by industrial activity", "LAND" : "arctic land.A tract of land in the Arctic", "LCTY" : "locality.A minor area or place of unspecified or mixed character and indefinite boundaries", "MILB" : "military base.A place used by an army or other armed service for storing arms and supplies, and for accommodating and training troops, a base from which operations can be initiated", "MNA" : "mining area.An area of mine sites where minerals and ores are extracted", "MVA" : "maneuver area.A tract of land where military field exercises are carried out", "NVB" : "naval base.An area used to store supplies, provide barracks for troops and naval personnel, a port for naval vessels, and from which operations are initiated", "OAS" : "oasis(-es).An area in a desert made productive by the availability of water", "OILF" : "oilfield.An area containing a subterranean store of petroleum of economic value", "PEAT" : "peat cutting area.An area where peat is harvested", "PRK" : "park.An area, often of forested land, maintained as a place of beauty, or for recreation", "PRT" : "port.A place provided with terminal and transfer facilities for loading and discharging waterborne cargo or passengers, usually located in a harbor", "QCKS" : "quicksand.An area where loose sand with water moving through it may become unstable when heavy objects are placed at the surface, causing them to sink", "RES" : "reserve.A tract of public land reserved for future use or restricted as to use", "RESA" : "agricultural reserve.A tract of land reserved for agricultural reclamation and/or development", "RESF" : "forest reserve.A forested area set aside for preservation or controlled use", "RESH" : "hunting reserve.A tract of land used primarily for hunting", "RESN" : "nature reserve.An area reserved for the maintenance of a natural habitat", "RESP" : "palm tree reserve.An area of palm trees where use is controlled", "RESV" : "reservation.A tract of land set aside for aboriginal, tribal, or native populations", "RESW" : "wildlife reserve.A tract of public land reserved for the preservation of wildlife", "RGN" : "region.An area distinguished by one or more observable physical or cultural characteristics", "RGNE" : "economic region.A region of a country established for economic development or for statistical purposes", "RGNH" : "historical region.A former historic area distinguished by one or more observable physical or cultural characteristics", "RGNL" : "lake region.A tract of land distinguished by numerous lakes", "RNGA" : "artillery range.A tract of land used for artillery firing practice", "SALT" : "salt area.A shallow basin or flat where salt accumulates after periodic inundation", "SNOW" : "snowfield.An area of permanent snow and ice forming the accumulation area of a glacier", "TRB" : "tribal area.A tract of land used by nomadic or other tribes" }, "P" : { "PPL" : "populated place.A city, town, village, or other agglomeration of buildings where people live and work", "PPLA" : "seat of a first-order administrative division.seat of a first-order administrative division (PPLC takes precedence over PPLA)", "PPLA2" : "seat of a second-order administrative division", "PPLA3" : "seat of a third-order administrative division", "PPLA4" : "seat of a fourth-order administrative division", "PPLA5" : "seat of a fifth-order administrative division", "PPLC" : "capital of a political entity", "PPLCH" : "historical capital of a political entity.A former capital of a political entity", "PPLF" : "farm village.A populated place where the population is largely engaged in agricultural activities", "PPLG" : "seat of government of a political entity", "PPLH" : "historical populated place. a populated place that no longer exists", "PPLL" : "populated locality.An area similar to a locality but with a small group of dwellings or other buildings", "PPLQ" : "abandoned populated place", "PPLR" : "religious populated place.A populated place whose population is largely engaged in religious occupations", "PPLS" : "populated places.cities, towns, villages, or other agglomerations of buildings where people live and work", "PPLW" : "destroyed populated place.A village, town or city destroyed by a natural disaster, or by war", "PPLX" : "section of populated place", "STLMT" : "israeli settlement" }, "R" : { "CSWY" : "causeway.A raised roadway across wet ground or shallow water", "OILP" : "oil pipeline.A pipeline used for transporting oil", "PRMN" : "promenade.A place for public walking, usually along a beach front", "PTGE" : "portage.A place where boats, goods, etc., are carried overland between navigable waters", "RD" : "road.An open way with improved surface for transportation of animals, people and vehicles", "RDA" : "ancient road the remains of a road used by ancient cultures", "RDB" : "road bend.A conspicuously curved or bent section of a road", "RDCUT" : "road cut.An excavation cut through a hill or ridge for a road", "RDJCT" : "road .A place where two or more roads join", "RJCT" : "railroad junction.A place where two or more railroad tracks join", "RR" : "railroad.A permanent twin steel-rail track on which freight and passenger cars move long distances", "RRQ" : "abandoned railroad", "RTE" : "caravan route.The route taken by caravans", "RYD" : "railroad yard.A system of tracks used for the making up of trains, and switching and storing freight cars", "ST" : "street.A paved urban thoroughfare", "STKR" : "stock route.A route taken by livestock herds", "TNL" : "tunnel.A subterranean passageway for transportation", "TNLN" : "natural tunnel.A cave that is open at both ends", "TNLRD" : "road tunnel.A tunnel through which a road passes", "TNLRR" : "railroad tunnel.A tunnel through which a railroad passes", "TNLS" : "tunnels subterranean passageways for transportation", "TRL" : "trail.A path, track, or route used by pedestrians, animals, or off-road vehicles" }, "S" : { "ADMF" : "administrative facility.A government building", "AGRF" : "agricultural facility.A building and/or tract of land used for improving agriculture", "AIRB" : "airbase.An area used to store supplies, provide barracks for air force personnel, hangars and runways for aircraft, and from which operations are initiated", "AIRF" : "airfield.A place on land where aircraft land and take off; no facilities provided for the commercial handling of passengers and cargo", "AIRH" : "heliport.A place where helicopters land and take off", "AIRP" : "airport.A place where aircraft regularly land and take off, with runways, navigational aids, and major facilities for the commercial handling of passengers and cargo", "AIRQ" : "abandoned airfield ", "AIRT" : "terminal.Airport facilities for the handling of freight and passengers", "AMTH" : "amphitheater.An oval or circular structure with rising tiers of seats about a stage or open space", "ANS" : "archaeological/prehistoric site.A place where archeological remains, old structures, or cultural artifacts are located", "AQC" : "aquaculture facility facility or area for the cultivation of aquatic animals and plants, especially fish, shellfish, and seaweed, in natural or controlled marine or freshwater environments; underwater agriculture", "ARCH" : "arch.A natural or man-made structure in the form of an arch", "ARCHV" : "archive.A place or institution where documents are preserved", "ART" : "piece of art.A piece of art, like a sculpture, painting. In contrast to monument (MNMT) it is not commemorative.", "ASTR" : "astronomical station.A point on the earth whose position has been determined by observations of celestial bodies", "ASYL" : "asylum.A facility where the insane are cared for and protected", "ATHF" : "athletic field.A tract of land used for playing team sports, and athletic track and field events", "ATM" : "automatic teller machine.An unattended electronic machine in a public place, connected to a data system and related equipment and activated by a bank customer to obtain cash withdrawals and other banking services.", "BANK" : "bank.A business establishment in which money is kept for saving or commercial purposes or is invested, supplied for loans, or exchanged.", "BCN" : "beacon.A fixed artificial navigation mark", "BDG" : "bridge.A structure erected across an obstacle such as a stream, road, etc., in order to carry roads, railroads, and pedestrians across", "BDGQ" : "ruined bridge.A destroyed or decayed bridge which is no longer functional", "BLDA" : "apartment building.A building containing several individual apartments", "BLDG" : "building(s).A structure built for permanent use, as a house, factory, etc.", "BLDO" : "office building commercial building where business and/or services are conducted", "BP" : "boundary marker.A fixture marking a point along a boundary", "BRKS" : "barracks.A building for lodging military personnel", "BRKW" : "breakwater.A structure erected to break the force of waves at the entrance to a harbor or port", "BSTN" : "baling station.A facility for baling agricultural products", "BTYD" : "boatyard.A waterside facility for servicing, repairing, and building small vessels", "BUR" : "burial cave(s).A cave used for human burials", "BUSTN" : "bus station.A facility comprising ticket office, platforms, etc. for loading and unloading passengers", "BUSTP" : "bus stop.A place lacking station facilities", "CARN" : "cairn.A heap of stones erected as a landmark or for other purposes", "CAVE" : "cave(s).An underground passageway or chamber, or cavity on the side of a cliff", "CH" : "church.A building for public Christian worship", "CMP" : "camp(s).A site occupied by tents, huts, or other shelters for temporary use", "CMPL" : "logging camp.A camp used by loggers", "CMPLA" : "labor camp.A camp used by migrant or temporary laborers", "CMPMN" : "mining camp.A camp used by miners", "CMPO" : "oil camp.A camp used by oilfield workers", "CMPQ" : "abandoned camp ", "CMPRF" : "refugee camp.A camp used by refugees", "CMTY" : "cemetery.A burial place or ground", "COMC" : "communication center.A facility, including buildings, antennae, towers and electronic equipment for receiving and transmitting information", "CRRL" : "corral(s).A pen or enclosure for confining or capturing animals", "CSNO" : "casino.A building used for entertainment, especially gambling", "CSTL" : "castle.A large fortified building or set of buildings", "CSTM" : "customs house.A building in a port where customs and duties are paid, and where vessels are entered and cleared", "CTHSE" : "courthouse.A building in which courts of law are held", "CTRA" : "atomic center.A facility where atomic research is carried out", "CTRCM" : "community center.A facility for community recreation and other activities", "CTRF" : "facility center.A place where more than one facility is situated", "CTRM" : "medical center.A complex of health care buildings including two or more of the following: hospital, medical school, clinic, pharmacy, doctor's offices, etc.", "CTRR" : "religious center.A facility where more than one religious activity is carried out, e.g., retreat, school, monastery, worship", "CTRS" : "space center.A facility for launching, tracking, or controlling satellites and space vehicles", "CVNT" : "convent.A building where a community of nuns lives in seclusion", "DAM" : "dam.A barrier constructed across a stream to impound water", "DAMQ" : "ruined dam.A destroyed or decayed dam which is no longer functional", "DAMSB" : "sub-surface dam.A dam put down to bedrock in a sand river", "DARY" : "dairy.A facility for the processing, sale and distribution of milk or milk products", "DCKD" : "dry dock.A dock providing support for a vessel, and means for removing the water so that the bottom of the vessel can be exposed", "DCKY" : "dockyard.A facility for servicing, building, or repairing ships", "DIKE" : "dike.An earth or stone embankment usually constructed for flood or stream control", "DIP" : "diplomatic facility office, residence, or facility of a foreign government, which may include an embassy, consulate, chancery, office of charge d'affaires, or other diplomatic, economic, military, or cultural mission", "DPOF" : "fuel depot.An area where fuel is stored", "EST" : "estate(s).A large commercialized agricultural landholding with associated buildings and other facilities", "ESTO" : "oil palm plantation.An estate specializing in the cultivation of oil palm trees", "ESTR" : "rubber plantation.An estate which specializes in growing and tapping rubber trees", "ESTSG" : "sugar plantation.An estate that specializes in growing sugar cane", "ESTT" : "tea plantation.An estate which specializes in growing tea bushes", "ESTX" : "section of estate ", "FCL" : "facility.A building or buildings housing a center, institute, foundation, hospital, prison, mission, courthouse, etc.", "FNDY" : "foundry.A building or works where metal casting is carried out", "FRM" : "farm.A tract of land with associated buildings devoted to agriculture", "FRMQ" : "abandoned farm ", "FRMS" : "farms tracts of land with associated buildings devoted to agriculture", "FRMT" : "farmstead the buildings and adjacent service areas of a farm", "FT" : "fort.A defensive structure or earthworks", "FY" : "ferry.A boat or other floating conveyance and terminal facilities regularly used to transport people and vehicles across a waterbody", "FYT" : "ferry terminal.A place where ferries pick-up and discharge passengers, vehicles and or cargo", "GATE" : "gate.A controlled access entrance or exit", "GDN" : "garden(s).An enclosure for displaying selected plant or animal life", "GHAT" : "ghat.A set of steps leading to a river, which are of religious significance, and at their base is usually a platform for bathing", "GHSE" : "guest house.A house used to provide lodging for paying guests", "GOSP" : "gas-oil separator plant.A facility for separating gas from oil", "GOVL" : "local government office.A facility housing local governmental offices, usually a city, town, or village hall", "GRVE" : "grave.A burial site", "HERM" : "hermitage.A secluded residence, usually for religious sects", "HLT" : "halting place.A place where caravans stop for rest", "HMSD" : "homestead.A residence, owner's or manager's, on a sheep or cattle station, woolshed, outcamp, or Aboriginal outstation, specific to Australia and New Zealand", "HSE" : "house(s).A building used as a human habitation", "HSEC" : "country house.A large house, mansion, or chateau, on a large estate", "HSP" : "hospital.A building in which sick or injured, especially those confined to bed, are medically treated", "HSPC" : "clinic.A medical facility associated with a hospital for outpatients", "HSPD" : "dispensary.A building where medical or dental aid is dispensed", "HSPL" : "leprosarium.An asylum or hospital for lepers", "HSTS" : "historical site.A place of historical importance", "HTL" : "hotel.A building providing lodging and/or meals for the public", "HUT" : "hut.A small primitive house", "HUTS" : "huts small primitive houses", "INSM" : "military installation.A facility for use of and control by armed forces", "ITTR" : "research institute.A facility where research is carried out", "JTY" : "jetty.A structure built out into the water at a river mouth or harbor entrance to regulate currents and silting", "LDNG" : "landing.A place where boats receive or discharge passengers and freight, but lacking most port facilities", "LEPC" : "leper colony.A settled area inhabited by lepers in relative isolation", "LIBR" : "library.A place in which information resources such as books are kept for reading, reference, or lending.", "LNDF" : "landfill.A place for trash and garbage disposal in which the waste is buried between layers of earth to build up low-lying land", "LOCK" : "lock(s).A basin in a waterway with gates at each end by means of which vessels are passed from one water level to another", "LTHSE" : "lighthouse.A distinctive structure exhibiting a major navigation light", "MALL" : "mall.A large, often enclosed shopping complex containing various stores, businesses, and restaurants usually accessible by common passageways.", "MAR" : "marina.A harbor facility for small boats, yachts, etc.", "MFG" : "factory one or more buildings where goods are manufactured, processed or fabricated", "MFGB" : "brewery one or more buildings where beer is brewed", "MFGC" : "cannery.A building where food items are canned", "MFGCU" : "copper works.A facility for processing copper ore", "MFGLM" : "limekiln.A furnace in which limestone is reduced to lime", "MFGM" : "munitions plant.A factory where ammunition is made", "MFGPH" : "phosphate works.A facility for producing fertilizer", "MFGQ" : "abandoned factory ", "MFGSG" : "sugar refinery.A facility for converting raw sugar into refined sugar", "MKT" : "market.A place where goods are bought and sold at regular intervals", "ML" : "mill(s).A building housing machines for transforming, shaping, finishing, grinding, or extracting products", "MLM" : "ore treatment plant.A facility for improving the metal content of ore by concentration", "MLO" : "olive oil mill.A mill where oil is extracted from olives", "MLSG" : "sugar mill.A facility where sugar cane is processed into raw sugar", "MLSGQ" : "former sugar mill.A sugar mill no longer used as a sugar mill", "MLSW" : "sawmill.A mill where logs or lumber are sawn to specified shapes and sizes", "MLWND" : "windmill.A mill or water pump powered by wind", "MLWTR" : "water mill.A mill powered by running water", "MN" : "mine(s).A site where mineral ores are extracted from the ground by excavating surface pits and subterranean passages", "MNAU" : "gold mine(s).A mine where gold ore, or alluvial gold is extracted", "MNC" : "coal mine(s).A mine where coal is extracted", "MNCR" : "chrome mine(s).A mine where chrome ore is extracted", "MNCU" : "copper mine(s).A mine where copper ore is extracted", "MNFE" : "iron mine(s).A mine where iron ore is extracted", "MNMT" : "monument.A commemorative structure or statue", "MNN" : "salt mine(s).A mine from which salt is extracted", "MNQ" : "abandoned mine", "MNQR" : "quarry(-ies).A surface mine where building stone or gravel and sand, etc. are extracted", "MOLE" : "mole.A massive structure of masonry or large stones serving as a pier or breakwater", "MSQE" : "mosque.A building for public Islamic worship", "MSSN" : "mission.A place characterized by dwellings, school, church, hospital and other facilities operated by a religious group for the purpose of providing charitable services and to propagate religion", "MSSNQ" : "abandoned mission ", "MSTY" : "monastery.A building and grounds where a community of monks lives in seclusion", "MTRO" : "metro station metro station (Underground, Tube, or Metro)", "MUS" : "museum.A building where objects of permanent interest in one or more of the arts and sciences are preserved and exhibited", "NOV" : "novitiate.A religious house or school where novices are trained", "NSY" : "nursery(-ies).A place where plants are propagated for transplanting or grafting", "OBPT" : "observation point.A wildlife or scenic observation point", "OBS" : "observatory.A facility equipped for observation of atmospheric or space phenomena", "OBSR" : "radio observatory.A facility equipped with an array of antennae for receiving radio waves from space", "OILJ" : "oil pipeline junction.A section of an oil pipeline where two or more pipes join together", "OILQ" : "abandoned oil well ", "OILR" : "oil refinery.A facility for converting crude oil into refined petroleum products", "OILT" : "tank farm.A tract of land occupied by large, cylindrical, metal tanks in which oil or liquid petrochemicals are stored", "OILW" : "oil well.A well from which oil may be pumped", "OPRA" : "opera house.A theater designed chiefly for the performance of operas.", "PAL" : "palace.A large stately house, often a royal or presidential residence", "PGDA" : "pagoda.A tower-like storied structure, usually a Buddhist shrine", "PIER" : "pier.A structure built out into navigable water on piles providing berthing for ships and recreation", "PKLT" : "parking lot.An area used for parking vehicles", "PMPO" : "oil pumping station.A facility for pumping oil through a pipeline", "PMPW" : "water pumping station.A facility for pumping water from a major well or through a pipeline", "PO" : "post office.A public building in which mail is received, sorted and distributed", "PP" : "police post.A building in which police are stationed", "PPQ" : "abandoned police post", "PRKGT" : "park gate.A controlled access to a park", "PRKHQ" : "park headquarters.A park administrative facility", "PRN" : "prison.A facility for confining prisoners", "PRNJ" : "reformatory.A facility for confining, training, and reforming young law offenders", "PRNQ" : "abandoned prison ", "PS" : "power station.A facility for generating electric power", "PSH" : "hydroelectric power station.A building where electricity is generated from water power", "PSN" : "nuclear power station nuclear power station", "PSTB" : "border post.A post or station at an international boundary for the regulation of movement of people and goods", "PSTC" : "customs post.A building at an international boundary where customs and duties are paid on goods", "PSTP" : "patrol post.A post from which patrols are sent out", "PYR" : "pyramid.An ancient massive structure of square ground plan with four triangular faces meeting at a point and used for enclosing tombs", "PYRS" : "pyramids.Ancient massive structures of square ground plan with four triangular faces meeting at a point and used for enclosing tombs", "QUAY" : "quay.A structure of solid construction along a shore or bank which provides berthing for ships and which generally provides cargo handling facilities", "RDCR" : "traffic circle.A road junction formed around a central circle about which traffic moves in one direction only", "RDIN" : "intersection.A junction of two or more highways by a system of separate levels that permit traffic to pass from one to another without the crossing of traffic streams", "RECG" : "golf course.A recreation field where golf is played", "RECR" : "racetrack.A track where races are held", "REST" : "restaurant.A place where meals are served to the public", "RET" : "store.A building where goods and/or services are offered for sale", "RHSE" : "resthouse.A structure maintained for the rest and shelter of travelers", "RKRY" : "rookery.A breeding place of a colony of birds or seals", "RLG" : "religious site.An ancient site of significant religious importance", "RLGR" : "retreat.A place of temporary seclusion, especially for religious groups", "RNCH" : "ranch(es).A large farm specializing in extensive grazing of livestock", "RSD" : "railroad siding.A short track parallel to and joining the main track", "RSGNL" : "railroad signal.A signal at the entrance of a particular section of track governing the movement of trains", "RSRT" : "resort.A specialized facility for vacation, health, or participation sports activities", "RSTN" : "railroad station.A facility comprising ticket office, platforms, etc. for loading and unloading train passengers and freight", "RSTNQ" : "abandoned railroad station ", "RSTP" : "railroad stop.A place lacking station facilities where trains stop to pick up and unload passengers and freight", "RSTPQ" : "abandoned railroad stop ", "RUIN" : "ruin(s).A destroyed or decayed structure which is no longer functional", "SCH" : "school building(s) where instruction in one or more branches of knowledge takes place", "SCHA" : "agricultural school.A school with a curriculum focused on agriculture", "SCHC" : "college the grounds and buildings of an institution of higher learning", "SCHL" : "language school Language Schools & Institutions", "SCHM" : "military school.A school at which military science forms the core of the curriculum", "SCHN" : "maritime school.A school at which maritime sciences form the core of the curriculum", "SCHT" : "technical school post-secondary school with a specifically technical or vocational curriculum", "SECP" : "State Exam Prep Centre state exam preparation centres", "SHPF" : "sheepfold.A fence or wall enclosure for sheep and other small herd animals", "SHRN" : "shrine.A structure or place memorializing a person or religious concept", "SHSE" : "storehouse.A building for storing goods, especially provisions", "SLCE" : "sluice.A conduit or passage for carrying off surplus water from a waterbody, usually regulated by means of a sluice gate", "SNTR" : "sanatorium.A facility where victims of physical or mental disorders are treated", "SPA" : "spa.A resort area usually developed around a medicinal spring", "SPLY" : "spillway.A passage or outlet through which surplus water flows over, around or through a dam", "SQR" : "square.A broad, open, public area near the center of a town or city", "STBL" : "stable.A building for the shelter and feeding of farm animals, especially horses", "STDM" : "stadium.A structure with an enclosure for athletic games with tiers of seats for spectators", "STNB" : "scientific research base.A scientific facility used as a base from which research is carried out or monitored", "STNC" : "coast guard station.A facility from which the coast is guarded by armed vessels", "STNE" : "experiment station.A facility for carrying out experiments", "STNF" : "forest station.A collection of buildings and facilities for carrying out forest management", "STNI" : "inspection station.A station at which vehicles, goods, and people are inspected", "STNM" : "meteorological station.A station at which weather elements are recorded", "STNR" : "radio station.A facility for producing and transmitting information by radio waves", "STNS" : "satellite station.A facility for tracking and communicating with orbiting satellites", "STNW" : "whaling station.A facility for butchering whales and processing train oil", "STPS" : "steps stones or slabs placed for ease in ascending or descending a steep slope", "SWT" : "sewage treatment plant facility for the processing of sewage and/or wastewater", "SYG" : "synagogue.A place for Jewish worship and religious instruction", "THTR" : "theater.A building, room, or outdoor structure for the presentation of plays, films, or other dramatic performances", "TMB" : "tomb(s).A structure for interring bodies", "TMPL" : "temple(s).An edifice dedicated to religious worship", "TNKD" : "cattle dipping tank.A small artificial pond used for immersing cattle in chemically treated water for disease control", "TOLL" : "toll gate/barrier highway toll collection station", "TOWR" : "tower.A high conspicuous structure, typically much higher than its diameter", "TRAM" : "tram rail vehicle along urban streets (also known as streetcar or trolley)", "TRANT" : "transit terminal facilities for the handling of vehicular freight and passengers", "TRIG" : "triangulation station.A point on the earth whose position has been determined by triangulation", "TRMO" : "oil pipeline terminal.A tank farm or loading facility at the end of an oil pipeline", "TWO" : "temp work office Temporary Work Offices", "UNIP" : "university prep school University Preparation Schools & Institutions", "UNIV" : "university.An institution for higher learning with teaching and research facilities constituting a graduate school and professional schools that award master's degrees and doctorates and an undergraduate division that awards bachelor's degrees.", "USGE" : "united states government establishment.A facility operated by the United States Government in Panama", "VETF" : "veterinary facility.A building or camp at which veterinary services are available", "WALL" : "wall.A thick masonry structure, usually enclosing a field or building, or forming the side of a structure", "WALLA" : "ancient wall the remains of a linear defensive stone structure", "WEIR" : "weir(s).A small dam in a stream, designed to raise the water level or to divert stream flow through a desired channel", "WHRF" : "wharf(-ves).A structure of open rather than solid construction along a shore or a bank which provides berthing for ships and cargo-handling facilities", "WRCK" : "wreck the site of the remains of a wrecked vessel", "WTRW" : "waterworks.A facility for supplying potable water through a water source and a system of pumps and filtration beds", "ZNF" : "free trade zone.An area, usually a section of a port, where goods may be received and shipped free of customs duty and of most customs regulations", "ZOO" : "zoo.A zoological garden or park where wild animals are kept for exhibition" }, "T" : { "ASPH" : "asphalt lake.A small basin containing naturally occurring asphalt", "ATOL" : "atoll(s).A ring-shaped coral reef which has closely spaced islands on it encircling a lagoon", "BAR" : "bar.A shallow ridge or mound of coarse unconsolidated material in a stream channel, at the mouth of a stream, estuary, or lagoon and in the wave-break zone along coasts", "BCH" : "beach.A shore zone of coarse unconsolidated sediment that extends from the low-water line to the highest reach of storm waves", "BCHS" : "beaches.A shore zone of coarse unconsolidated sediment that extends from the low-water line to the highest reach of storm waves", "BDLD" : "badlands.An area characterized by a maze of very closely spaced, deep, narrow, steep-sided ravines, and sharp crests and pinnacles", "BLDR" : "boulder field.A high altitude or high latitude bare, flat area covered with large angular rocks", "BLHL" : "blowhole(s).A hole in coastal rock through which sea water is forced by a rising tide or waves and spurted through an outlet into the air", "BLOW" : "blowout(s).A small depression in sandy terrain, caused by wind erosion", "BNCH" : "bench.A long, narrow bedrock platform bounded by steeper slopes above and below, usually overlooking a waterbody", "BUTE" : "butte(s).A small, isolated, usually flat-topped hill with steep sides", "CAPE" : "cape.A land area, more prominent than a point, projecting into the sea and marking a notable change in coastal direction", "CFT" : "cleft(s).A deep narrow slot, notch, or groove in a coastal cliff", "CLDA" : "caldera.A depression measuring kilometers across formed by the collapse of a volcanic mountain", "CLF" : "cliff(s).A high, steep to perpendicular slope overlooking a waterbody or lower area", "CNYN" : "canyon.A deep, narrow valley with steep sides cutting into a plateau or mountainous area", "CONE" : "cone(s).A conical landform composed of mud or volcanic material", "CRDR" : "corridor.A strip or area of land having significance as an access way", "CRQ" : "cirque.A bowl-like hollow partially surrounded by cliffs or steep slopes at the head of a glaciated valley", "CRQS" : "cirques.bowl-like hollows partially surrounded by cliffs or steep slopes at the head of a glaciated valley", "CRTR" : "crater(s).A generally circular saucer or bowl-shaped depression caused by volcanic or meteorite explosive action", "CUET" : "cuesta(s).An asymmetric ridge formed on tilted strata", "DLTA" : "delta.A flat plain formed by alluvial deposits at the mouth of a stream", "DPR" : "depression(s).A low area surrounded by higher land and usually characterized by interior drainage", "DSRT" : "desert.A large area with little or no vegetation due to extreme environmental conditions", "DUNE" : "dune(s).A wave form, ridge or star shape feature composed of sand", "DVD" : "divide.A line separating adjacent drainage basins", "ERG" : "sandy desert.An extensive tract of shifting sand and sand dunes", "FAN" : "fan(s).A fan-shaped wedge of coarse alluvium with apex merging with a mountain stream bed and the fan spreading out at a low angle slope onto an adjacent plain", "FORD" : "ford.A shallow part of a stream which can be crossed on foot or by land vehicle", "FSR" : "fissure.A crack associated with volcanism", "GAP" : "gap.A low place in a ridge, not used for transportation", "GRGE" : "gorge(s).A short, narrow, steep-sided section of a stream valley", "HDLD" : "headland.A high projection of land extending into a large body of water beyond the line of the coast", "HLL" : "hill.A rounded elevation of limited extent rising above the surrounding land with local relief of less than 300m", "HLLS" : "hills.rounded elevations of limited extent rising above the surrounding land with local relief of less than 300m", "HMCK" : "hammock(s).A patch of ground, distinct from and slightly above the surrounding plain or wetland. Often occurs in groups", "HMDA" : "rock desert.A relatively sand-free, high bedrock plateau in a hot desert, with or without a gravel veneer", "INTF" : "interfluve.A relatively undissected upland between adjacent stream valleys", "ISL" : "island.A tract of land, smaller than a continent, surrounded by water at high water", "ISLET" : "islet.small island, bigger than rock, smaller than island.", "ISLF" : "artificial island.An island created by landfill or diking and filling in a wetland, bay, or lagoon", "ISLM" : "mangrove island.A mangrove swamp surrounded by a waterbody", "ISLS" : "islands.tracts of land, smaller than a continent, surrounded by water at high water", "ISLT" : "land-tied island.A coastal island connected to the mainland by barrier beaches, levees or dikes", "ISLX" : "section of island.", "ISTH" : "isthmus.A narrow strip of land connecting two larger land masses and bordered by water", "KRST" : "karst area.A distinctive landscape developed on soluble rock such as limestone characterized by sinkholes, caves, disappearing streams, and underground drainage", "LAVA" : "lava area.An area of solidified lava", "LEV" : "levee.A natural low embankment bordering a distributary or meandering stream; often built up artificially to control floods", "MESA" : "mesa(s).A flat-topped, isolated elevation with steep slopes on all sides, less extensive than a plateau", "MND" : "mound(s).A low, isolated, rounded hill", "MRN" : "moraine.A mound, ridge, or other accumulation of glacial till", "MT" : "mountain.An elevation standing high above the surrounding area with small summit area, steep slopes and local relief of 300m or more", "MTS" : "mountains.A mountain range or a group of mountains or high ridges", "NKM" : "meander neck.A narrow strip of land between the two limbs of a meander loop at its narrowest point", "NTK" : "nunatak.A rock or mountain peak protruding through glacial ice", "NTKS" : "nunataks.rocks or mountain peaks protruding through glacial ice", "PAN" : "pan.A near-level shallow, natural depression or basin, usually containing an intermittent lake, pond, or pool", "PANS" : "pans.A near-level shallow, natural depression or basin, usually containing an intermittent lake, pond, or pool", "PASS" : "pass.A break in a mountain range or other high obstruction, used for transportation from one side to the other [See also gap]", "PEN" : "peninsula.An elongate area of land projecting into a body of water and nearly surrounded by water", "PENX" : "section of peninsula.", "PK" : "peak.A pointed elevation atop a mountain, ridge, or other hypsographic feature", "PKS" : "peaks.pointed elevations atop a mountain, ridge, or other hypsographic features", "PLAT" : "plateau.An elevated plain with steep slopes on one or more sides, and often with incised streams", "PLATX" : "section of plateau.", "PLDR" : "polder.An area reclaimed from the sea by diking and draining", "PLN" : "plain(s).An extensive area of comparatively level to gently undulating land, lacking surface irregularities, and usually adjacent to a higher area", "PLNX" : "section of plain.", "PROM" : "promontory(-ies).A bluff or prominent hill overlooking or projecting into a lowland", "PT" : "point.A tapering piece of land projecting into a body of water, less prominent than a cape", "PTS" : "points.tapering pieces of land projecting into a body of water, less prominent than a cape", "RDGB" : "beach ridge.A ridge of sand just inland and parallel to the beach, usually in series", "RDGE" : "ridge(s).A long narrow elevation with steep sides, and a more or less continuous crest", "REG" : "stony desert.A desert plain characterized by a surface veneer of gravel and stones", "RK" : "rock.A conspicuous, isolated rocky mass", "RKFL" : "rockfall.An irregular mass of fallen rock at the base of a cliff or steep slope", "RKS" : "rocks.conspicuous, isolated rocky masses", "SAND" : "sand area.A tract of land covered with sand", "SBED" : "dry stream bed.A channel formerly containing the water of a stream", "SCRP" : "escarpment.A long line of cliffs or steep slopes separating level surfaces above and below", "SDL" : "saddle.A broad, open pass crossing a ridge or between hills or mountains", "SHOR" : "shore.A narrow zone bordering a waterbody which covers and uncovers at high and low water, respectively", "SINK" : "sinkhole.A small crater-shape depression in a karst area", "SLID" : "slide.A mound of earth material, at the base of a slope and the associated scoured area", "SLP" : "slope(s).A surface with a relatively uniform slope angle", "SPIT" : "spit.A narrow, straight or curved continuation of a beach into a waterbody", "SPUR" : "spur(s).A subordinate ridge projecting outward from a hill, mountain or other elevation", "TAL" : "talus slope.A steep concave slope formed by an accumulation of loose rock fragments at the base of a cliff or steep slope", "TRGD" : "interdune trough(s).A long wind-swept trough between parallel longitudinal dunes", "TRR" : "terrace.A long, narrow alluvial platform bounded by steeper slopes above and below, usually overlooking a waterbody", "UPLD" : "upland.An extensive interior region of high land with low to moderate surface relief", "VAL" : "valley.An elongated depression usually traversed by a stream", "VALG" : "hanging valley.A valley the floor of which is notably higher than the valley or shore to which it leads; most common in areas that have been glaciated", "VALS" : "valleys.elongated depressions usually traversed by a stream", "VALX" : "section of valley.", "VLC" : "volcano.A conical elevation composed of volcanic materials with a crater at the top" }, "U" : { "APNU" : "apron.A gentle slope, with a generally smooth surface, particularly found around groups of islands and seamounts", "ARCU" : "arch.A low bulge around the southeastern end of the island of Hawaii", "ARRU" : "arrugado.An area of subdued corrugations off Baja California", "BDLU" : "borderland.A region adjacent to a continent, normally occupied by or bordering a shelf, that is highly irregular with depths well in excess of those typical of a shelf", "BKSU" : "banks.elevations, typically located on a shelf, over which the depth of water is relatively shallow but sufficient for safe surface navigation", "BNKU" : "bank.An elevation, typically located on a shelf, over which the depth of water is relatively shallow but sufficient for safe surface navigation", "BSNU" : "basin.A depression more or less equidimensional in plan and of variable extent", "CDAU" : "cordillera.An entire mountain system including the subordinate ranges, interior plateaus, and basins", "CNSU" : "canyons.Relatively narrow, deep depressions with steep sides, the bottom of which generally has a continuous slope", "CNYU" : "canyon.A relatively narrow, deep depression with steep sides, the bottom of which generally has a continuous slope", "CRSU" : "continental rise.A gentle slope rising from oceanic depths towards the foot of a continental slope", "DEPU" : "deep.A localized deep area within the confines of a larger feature, such as a trough, basin or trench", "EDGU" : "shelf edge.A line along which there is a marked increase of slope at the outer margin of a continental shelf or island shelf", "ESCU" : "escarpment (or scarp).An elongated and comparatively steep slope separating flat or gently sloping areas", "FANU" : "fan.A relatively smooth feature normally sloping away from the lower termination of a canyon or canyon system", "FLTU" : "flat.A small level or nearly level area", "FRZU" : "fracture zone.An extensive linear zone of irregular topography of the sea floor, characterized by steep-sided or asymmetrical ridges, troughs, or escarpments", "FURU" : "furrow.A closed, linear, narrow, shallow depression", "GAPU" : "gap.A narrow break in a ridge or rise", "GLYU" : "gully.A small valley-like feature", "HLLU" : "hill.An elevation rising generally less than 500 meters", "HLSU" : "hills.elevations rising generally less than 500 meters", "HOLU" : "hole.A small depression of the sea floor", "KNLU" : "knoll.An elevation rising generally more than 500 meters and less than 1,000 meters and of limited extent across the summit", "KNSU" : "knolls.elevations rising generally more than 500 meters and less than 1,000 meters and of limited extent across the summits", "LDGU" : "ledge.A rocky projection or outcrop, commonly linear and near shore", "LEVU" : "levee.An embankment bordering a canyon, valley, or seachannel", "MESU" : "mesa.An isolated, extensive, flat-topped elevation on the shelf, with relatively steep sides", "MNDU" : "mound.A low, isolated, rounded hill", "MOTU" : "moat.An annular depression that may not be continuous, located at the base of many seamounts, islands, and other isolated elevations", "MTU" : "mountain.A well-delineated subdivision of a large and complex positive feature", "PKSU" : "peaks.prominent elevations, part of a larger feature, either pointed or of very limited extent across the summit", "PKU" : "peak.A prominent elevation, part of a larger feature, either pointed or of very limited extent across the summit", "PLNU" : "plain.A flat, gently sloping or nearly level region", "PLTU" : "plateau.A comparatively flat-topped feature of considerable extent, dropping off abruptly on one or more sides", "PNLU" : "pinnacle.A high tower or spire-shaped pillar of rock or coral, alone or cresting a summit", "PRVU" : "province.A region identifiable by a group of similar physiographic features whose characteristics are markedly in contrast with surrounding areas", "RDGU" : "ridge.A long narrow elevation with steep sides", "RDSU" : "ridges.long narrow elevations with steep sides", "RFSU" : "reefs.surface-navigation hazards composed of consolidated material", "RFU" : "reef.A surface-navigation hazard composed of consolidated material", "RISU" : "rise.A broad elevation that rises gently, and generally smoothly, from the sea floor", "SCNU" : "seachannel.A continuously sloping, elongated depression commonly found in fans or plains and customarily bordered by levees on one or two sides", "SCSU" : "seachannels.continuously sloping, elongated depressions commonly found in fans or plains and customarily bordered by levees on one or two sides", "SDLU" : "saddle.A low part, resembling in shape a saddle, in a ridge or between contiguous seamounts", "SHFU" : "shelf.A zone adjacent to a continent (or around an island) that extends from the low water line to a depth at which there is usually a marked increase of slope towards oceanic depths", "SHLU" : "shoal.A surface-navigation hazard composed of unconsolidated material", "SHSU" : "shoals.Hazards to surface navigation composed of unconsolidated material", "SHVU" : "shelf valley.A valley on the shelf, generally the shoreward extension of a canyon", "SILU" : "sill.the low part of a gap or saddle separating basins", "SLPU" : "slope.the slope seaward from the shelf edge to the beginning of a continental rise or the point where there is a general reduction in slope", "SMSU" : "seamounts.elevations rising generally more than 1,000 meters and of limited extent across the summit", "SMU" : "seamount.An elevation rising generally more than 1,000 meters and of limited extent across the summit", "SPRU" : "spur.A subordinate elevation, ridge, or rise projecting outward from a larger feature", "TERU" : "terrace.A relatively flat horizontal or gently inclined surface, sometimes long and narrow, which is bounded by a steeper ascending slope on one side and by a steep descending slope on the opposite side", "TMSU" : "tablemounts (or guyots).seamounts having a comparatively smooth, flat top", "TMTU" : "tablemount (or guyot).A seamount having a comparatively smooth, flat top", "TNGU" : "tongue.An elongate (tongue-like) extension of a flat sea floor into an adjacent higher feature", "TRGU" : "trough.A long depression of the sea floor characteristically flat bottomed and steep sided, and normally shallower than a trench", "TRNU" : "trench.A long, narrow, characteristically very deep and asymmetrical depression of the sea floor, with relatively steep sides", "VALU" : "valley.A relatively shallow, wide depression, the bottom of which usually has a continuous gradient", "VLSU" : "valleys.A relatively shallow, wide depression, the bottom of which usually has a continuous gradient" }, "V" : { "BUSH" : "bush(es).A small clump of conspicuous bushes in an otherwise bare area", "CULT" : "cultivated area.An area under cultivation", "FRST" : "forest(s).An area dominated by tree vegetation", "FRSTF" : "fossilized forest.A forest fossilized by geologic processes and now exposed at the earth's surface", "GROVE" : "grove.A small wooded area or collection of trees growing closely together, occurring naturally or deliberately planted", "GRSLD" : "grassland.An area dominated by grass vegetation", "GRVC" : "coconut grove.A planting of coconut trees", "GRVO" : "olive grove.A planting of olive trees", "GRVP" : "palm grove.A planting of palm trees", "GRVPN" : "pine grove.A planting of pine trees", "HTH" : "heath.An upland moor or sandy area dominated by low shrubby vegetation including heather", "MDW" : "meadow.A small, poorly drained area dominated by grassy vegetation", "OCH" : "orchard(s).A planting of fruit or nut trees", "SCRB" : "scrubland.An area of low trees, bushes, and shrubs stunted by some environmental limitation", "TREE" : "tree(s).A conspicuous tree used as a landmark", "TUND" : "tundra.A marshy, treeless, high latitude plain, dominated by mosses, lichens, and low shrub vegetation under permafrost conditions", "VIN" : "vineyard.A planting of grapevines", "VINS" : "vineyards.plantings of grapevines", "ll" : "not available" }, } def geoid(ip): g = geocoder.geonames(ip, key='sashedher') ip_id = g.geonames_id # print(ip_id) return ip_id def load_geo_onto(): g_aBox = Graph() g_aBox.parse("Dataset.ttl") g_aBox.parse("ontology_v3.2.rdf") return g_aBox def OntoMatch1(strg) -> bool: Onto = re.compile(r"http://www.geonames.org/ontology") return Onto.match(strg) is not None def OntoMatch2(strg) -> bool: Onto = re.compile(r"http://www.w3.org/2003/01/geo/wgs84_pos") return Onto.match(strg) is not None def about_info(qry, grph): alternateName = "http://www.geonames.org/ontology#alternateName" officialName = "http://www.geonames.org/ontology#officialName" # postalCode="http://www.geonames.org/ontology#postalCode" namespace = "http://www.geonames.org/ontology#" coords = "http://www.w3.org/2003/01/geo/wgs84_pos#" i = 0 result_r = grph.query(qry) about = dict() for subj, pred, obj in result_r: if str(pred) != alternateName and str(pred) != officialName: if OntoMatch1(str(pred)): i = i + 1 x = str(pred) x = x.replace(namespace, '') # print("{:>20} {:>30} ".format(x,obj)) about[x] = str(obj) if OntoMatch2(str(pred)): x = str(pred) x = x.replace(coords, '') # print("{:>20} {:>30} ".format(x,obj)) about[x] = str(obj) # print(about) return about def cities_info(qry, grph): result_r = grph.query(qry) type(result_r) cities = dict() i = 0 for s, o in result_r: i = i + 1 # print(s, p, o) cities[str(s)] = str(o) return cities def query_aboutinfo(ipid): qery = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX gn: <http://www.geonames.org/ontology#> PREFIX cc: <http://creativecommons.org/ns#> PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#> PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX :<https://sws.geonames.org/> construct {<https://sws.geonames.org/""" + str(ipid) + """/> ?p ?o} WHERE {<https://sws.geonames.org/""" + str(ipid) + """/> ?p ?o} """ return qery def query_citiesinfo(ipid, pred): qery = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX gn: <http://www.geonames.org/ontology#> PREFIX cc: <http://creativecommons.org/ns#> PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#> PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX :<https://sws.geonames.org/> select ?name ?s WHERE {?s gn:"""+pred+""" <https://sws.geonames.org/"""+str(ipid)+"""/>. ?s gn:name ?name.} """ return qery def temp1(ctrycode,fcl=None)->str: if(fcl is None): st="This geographical location belongs to the country "+ ctrycode.upper()+". " return st namespace="https://www.geonames.org/ontology#" fcl=fcl.replace(namespace,'') fc=Feature_code[fcl[0]] st="This geographical location belongs to the country "+ ctrycode.upper()+" and it is classified as " for ftr in fc: st=st+str(ftr.upper())+" " # print(st) # print('FeatureCode is :{}'.format(Feature_code[z[0]])) st=st+". " st=st+"\nThe more specific details are, it is "+SubFeature_Code[fcl[0]][fcl[2:]].upper() return st def temp2(offname,popln='000',poscd='000000')->str: if(poscd!='000000'): x="The official name of the location is "+ offname.upper()+" which has a population of around "+ popln .upper()+" with the postal code is "+poscd else: if(popln!='000'): x="The official name of the location is "+ offname.upper()+" which has a population of around "+ popln .upper()+". " else: x="The official name of the location is "+ offname.upper() return x; def temp3(map,lat,logt)->str: x="we can locate this location help of latitude and longitude "+ lat +" & "+logt+" respectively or either with the help of location map: "+map return x; def temp4(wiki)->str: x="More details about feature is explained in this wikipedia document : "+wiki return x; def keyWordSentence(pred_list,about): res="The location name is "+about['name']+". " for x in pred_list: try: if(x == 'countryCode'): res += "This geographical location belongs to the country "+about[x]+". " if(x == 'featureCode'): namespace="https://www.geonames.org/ontology#" fcl= about[x] fcl=fcl.replace(namespace,'') print(fcl) fc=Feature_code[fcl[0]] st="This geographical location is classified as " for ftr in fc: st=st+str(ftr.upper())+" " st=st+". " st=st+"\nThe more specific details are, it is "+SubFeature_Code[fcl[0]][fcl[2:]].upper() print(st) res += st if(x== 'postalCode' or x == 'population'): res += "The "+x+" for "+about['name']+" is "+about[x]+". " if(x=='Coordinates'): res += about['name']+" is cooordinated at a point lat is "+about['lat']+" and long is "+about['long']+". " if(x == 'locationMap' or x == 'wikipediaArticle'): res += "The "+x+" is avilable in "+about[x]+" " except: res= res print(res) return res def load_loc_info(id,type): g_aBox = Graph() url='https://sws.geonames.org/'+str(id)+'/'+type+'.rdf' try: g_aBox.parse(url) # print("{}: {}".format(i,url)) except: print("This feature does not have this type of rdf file or invalid id") return g_aBox def get_result(_id,pred_list): # g_abox = GeoOnto.load_geo_onto() print(pred_list) ip_id = geoid(_id) print("input id for str is :"+str(ip_id)) # if(ip_id is None): g_abox = load_loc_info(ip_id,'about') qry = query_aboutinfo(ip_id) about = about_info(qry, g_abox) pprint(about) del g_abox g_abox = load_loc_info(ip_id,'nearby') qry = query_citiesinfo(ip_id, "nearby") nearbys = cities_info(qry, g_abox) del g_abox g_abox = load_loc_info(ip_id,'neighbours') qry = query_citiesinfo(ip_id, "neighbour") neighbours = cities_info(qry, g_abox) del g_abox g_abox = load_loc_info(ip_id,'contains') qry = query_citiesinfo(ip_id, "parentFeature") contains = cities_info(qry, g_abox) sentences = dict() try: sentences['temp1']=temp1(about['countryCode'],about['featureCode']) except: sentences['temp1']=temp1('IN') try: sentences['temp2']=temp2(about['name'],about['population'],about['postalCode']) # add postal only when available except: try: sentences['temp2']=temp2(about['name'],about['population']) except: sentences['temp2']=temp2(about['name']) sentences['temp3']=temp3(about['locationMap'],about['lat'],about['long']) try: sentences['temp4']=temp4(about['wikipediaArticle']) except: sentences['temp4']="No wikipedia article found" # pprint(about) # print("\n----------------------------- nearby cities------------------\n") # pprint(nearbys) # print("\n----------------------------- neighbour cities------------------\n") # pprint(neighbours) # # print("\n----------------------------- contains cities------------------\n") # pprint(contains) keysent=keyWordSentence(pred_list,about) sentences['openAI']= openAI.generate_sentence(keysent) result = {'about': about, 'nearbys': nearbys, 'neighbours': neighbours, 'contains': contains, 'sentences':sentences} nersen= "" if 'Nearbys' in pred_list: if(len(nearbys) == 0): nersen ="There is no info about nearby places for "+about['name'] else: nersen = "The following list of locations are near to "+about['name']+" are " result['nersen'] = nersen; neisen= "" if 'Neighbours' in pred_list: if(len(neighbours) == 0): neisen ="There is no info about neighbouring location for "+about['name'] else: neisen = "The following list of locations are neighbour to"+about['name']+" are " result['neisen'] = neisen; consen= "" if 'contains' in pred_list: if(len(contains) == 0): consen ="There is no info about child locations for "+about['name'] else: consen = "The following list of locations are contained inside "+about['name']+" are " result['consen'] = consen; count = 0 colList, temp = [], [] print(contains.keys()) for a in contains.keys(): temp.append(a) count+=1 if count==5: colList.append(temp.copy()) temp.clear() count = 0 if len(temp)>0: colList.append(temp.copy()) count = 0 nerList, n1 = [], [] print(contains.keys()) for a in nearbys.keys(): n1.append(a) count+=1 if count==5: nerList.append(n1.copy()) n1.clear() count = 0 if len(n1)>0: nerList.append(n1.copy()) count = 0 neiList, n2 = [], [] print(contains.keys()) for a in neighbours.keys(): n2.append(a) count+=1 if count==5: neiList.append(n2.copy()) n2.clear() count = 0 if len(n2)>0: colList.append(n2.copy()) print("sentence from OpenAi"+sentences['openAI']) result['nerList'] = nerList # print(result['containslist']) result['neiList'] = neiList # print(result['containslist']) result['containslist'] = colList result['nearbysize'] = len(nearbys) result['neighboursize'] = len(neighbours) result['containsize'] = len(contains) print(result['nearbys']) del g_abox return result
[]
2024-01-10
benedictbihl/virtual-me-backend
scripts~transform.py
import os import json import asyncio from dotenv import load_dotenv from langchain.document_transformers import DoctranQATransformer from langchain.document_loaders import DirectoryLoader load_dotenv() # Define the folder where the wav files are located root_folder = os.getenv("ROOT_DIR") or "" print("Root folder: ", root_folder) loader = DirectoryLoader(root_folder + "transcripts/", glob="**/*.txt") documents = loader.load() print("Loaded {} documents".format(len(documents))) async def transform_documents(): qa_transformer = DoctranQATransformer() print("Transforming documents...") transformed_documents = await qa_transformer.atransform_documents(documents) folder_path = os.path.join(root_folder, "transformed_documents") if not os.path.exists(folder_path): os.makedirs(folder_path) for doc in transformed_documents: with open( os.path.join( folder_path, os.path.splitext(os.path.basename(doc.metadata["source"]))[0] + ".json", ), "w", ) as f: f.write(json.dumps(doc.metadata, indent=2)) print("Done") asyncio.run(transform_documents())
[]
2024-01-10
ucaba60/COMP0073
version_1.2~llm_sample.py
# Imports import pandas as pd import openai import csv import os import torch from transformers import GPT2Tokenizer, GPT2LMHeadModel from datasets_gathering import preprocess_and_save import time # Constants BATCH_SIZE = 10 # Define the batch size openai.api_key = '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' def generate_gpt3_responses(prompt_csv_path, response_folder_path, model="gpt-3.5-turbo", temperature=1): """ Generate GPT-3 responses for a list of prompts saved in a csv file. Args: prompt_csv_path (str): Path to the csv file containing the prompts. response_folder_path (str): Path to the folder where the responses will be saved. model (str, optional): The ID of the model to use. Defaults to "gpt-3.5-turbo". temperature (float, optional): Determines the randomness of the AI's output. Defaults to 1, as per OpenAI docs. Returns: None, generates a csv file with the responses. """ # Load the prompts df = pd.read_csv(prompt_csv_path) prompts = df['Prompt'].tolist() # Initialize the starting point start = 0 # Construct the response file path response_csv_path = os.path.join(response_folder_path, f"{model}_responses.csv") # Check if the response file already exists if os.path.exists(response_csv_path): # If so, get the number of completed prompts from the file with open(response_csv_path, "r", newline="", encoding='utf-8') as file: start = sum(1 for row in csv.reader(file)) - 1 # Subtract 1 for the header while start < len(prompts): try: # Process the remaining prompts in batches for i in range(start, len(prompts), BATCH_SIZE): batch = prompts[i:i + BATCH_SIZE] responses = [] for prompt in batch: # Generate the response response = openai.ChatCompletion.create( model=model, messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} ], temperature=temperature ) # Append the response to the list responses.append('<<RESP>> ' + response['choices'][0]['message']['content'].strip()) # Save the responses to a new DataFrame response_df = pd.DataFrame({ 'Prompt': batch, 'Response': responses }) # Write the DataFrame to the CSV file, appending if it already exists if os.path.exists(response_csv_path): response_df.to_csv(response_csv_path, mode='a', header=False, index=False) else: response_df.to_csv(response_csv_path, mode='w', index=False) print(f"Batch {i // BATCH_SIZE + 1} completed") start = i + BATCH_SIZE except Exception as e: print(f"An error occurred: {str(e)}") print("Sleeping for 10 seconds before retrying...") time.sleep(10) # wait for 10 seconds before retrying # generate_gpt3_responses('extracted_data/prompts.csv', 'extracted_data', temperature=1) #temeprature is arbirtary this is the default value as per OpenAI docs. def extract_and_combine(response_csv_path): """ Load 'Prompt' and 'Response' from the generated responses csv file, remove the '<<RESP>>' string, adjust the format to match the original datasets, add a label 1 to every instance, and save to a new csv file. Args: response_csv_path (str): Path to the csv file containing the generated responses. Returns: None, generates a csv file with the combined text and labels. """ # Load the responses df = pd.read_csv(response_csv_path) # Remove the '<<RESP>>' string from each response df['Response'] = df['Response'].str.replace('<<RESP>> ', '') # Replace the specific string in the prompt df['Prompt'] = df['Prompt'].str.replace( 'Write an abstract for a scientific paper that answers the Question:', 'Answer:') # Combine the prompt and the response in a new column 'Text' with adjustments for specific prompts df['Text'] = df.apply( lambda row: ( 'Prompt: ' + row['Prompt'].replace(' Continue the story:', '') + ' Story: ' + row['Response'] if row['Prompt'].endswith('Continue the story:') else ( 'Summary: ' + row['Prompt'].replace('Write a news article based on the following summary: ', '') + ' Article: ' + row['Response'] if row['Prompt'].startswith('Write a news article based on the following summary:') else row['Prompt'] + ' ' + row['Response'] ) ), axis=1 ) # Remove 'Title:' and/or 'Abstract:' if they appear after 'Answer:' df['Text'] = df['Text'].str.replace(r'Answer: (Title:|Abstract:)', 'Answer:', regex=True) # Remove 'Abstract:' if it appears after 'Answer:' df['Text'] = df['Text'].str.replace(r'Answer:.*Abstract:', 'Answer:', regex=True) # Remove 'Abstract:' if it appears in the text df['Text'] = df['Text'].str.replace('Abstract:', '', regex=False) # Add a new column 'Label' with value 1 to each instance df['Label'] = 1 # Keep only the 'Text' and 'Label' columns df = df[['Text', 'Label']] # Print the number of entries pre-processed num_entries = len(df) print(f"Number of entries pre-processed: {num_entries}") # Construct the output file path based on the response file path base_path, extension = os.path.splitext(response_csv_path) output_csv_path = f"{base_path}_preprocessed{extension}" # Check if the output file already exists if os.path.isfile(output_csv_path): overwrite = input(f"{output_csv_path} already exists. Do you want to overwrite it? (y/n): ") if overwrite.lower() != 'y': print("Operation cancelled.") return # Save the DataFrame to a CSV file df.to_csv(output_csv_path, index=False) # extract_and_combine("extracted_data/gpt2-large_responses.csv") # preprocess_and_save(gpt_dataset='gpt2-large_responses_preprocessed', gpt_dataset_path='extracted_data', # output_folder='extracted_data') def generate_gpt2_responses(prompt_csv_path, response_folder_path, model_name): """ Generate responses for a list of prompts saved in a csv file using a GPT-2 model. Args: prompt_csv_path (str): Path to the csv file containing the prompts. response_folder_path (str): Path to the folder where the responses will be saved. model_name (str): Name of the GPT-2 model to use (for example, "gpt2", "gpt2-medium", "gpt2-large", "gpt2-xl"). Returns: None, generates a csv file with the responses. """ # Define acceptable models acceptable_models = ["gpt2", "gpt2-medium", "gpt2-large", "gpt2-xl"] if model_name not in acceptable_models: raise ValueError(f"Invalid model name. Acceptable models are: {', '.join(acceptable_models)}") # Load the GPT-2 model and tokenizer model = GPT2LMHeadModel.from_pretrained(model_name) tokenizer = GPT2Tokenizer.from_pretrained(model_name) # Load the prompts df = pd.read_csv(prompt_csv_path) prompts = df['Prompt'].tolist() # Construct the response file path response_csv_path = os.path.join(response_folder_path, f"{model_name}_responses.csv") # Check if the response file already exists if os.path.exists(response_csv_path): # Load the existing responses existing_responses_df = pd.read_csv(response_csv_path) # Determine the starting point based on the number of existing responses start = len(existing_responses_df) else: start = 0 for i in range(start, len(prompts)): # Encode the prompt input_ids = tokenizer.encode(prompts[i], return_tensors="pt") # Generate a response output = model.generate( input_ids, attention_mask=torch.ones_like(input_ids), # Set all positions to 1 (i.e., no padding) pad_token_id=tokenizer.eos_token_id, # Use the EOS token as the PAD token do_sample=True, max_length=1024, # Use GPT-2's maximum sequence length ) # Calculate the number of tokens in the prompt prompt_length = input_ids.shape[-1] # Decode only the response, excluding the prompt response = tokenizer.decode(output[0, prompt_length:], skip_special_tokens=True) # Save the prompt and response to a DataFrame response_df = pd.DataFrame({ 'Prompt': [prompts[i]], 'Response': [response] }) # Append the DataFrame to the CSV file if os.path.exists(response_csv_path): response_df.to_csv(response_csv_path, mode='a', header=False, index=False) else: response_df.to_csv(response_csv_path, mode='w', index=False) print(f"Prompt {i + 1} of {len(prompts)} processed") print(f"All prompts processed. Responses saved to {response_csv_path}.") def regenerate_responses(response_csv_path): """ Check the csv file containing generated responses for any NaN values. If any are found, regenerate the responses using the provided model. Args: response_csv_path (str): Path to the csv file containing the generated responses. Returns: None, updates the csv file with the regenerated responses. """ # Extract the model name from the filename model_name = os.path.basename(response_csv_path).split('_')[0] # Load the model and tokenizer model = GPT2LMHeadModel.from_pretrained(model_name) tokenizer = GPT2Tokenizer.from_pretrained(model_name) print(f"Loaded model {model_name}") # Load the responses df = pd.read_csv(response_csv_path) # Iterate over the DataFrame for i, row in df.iterrows(): if pd.isnull(row['Response']): # Encode the prompt input_ids = tokenizer.encode(row['Prompt'], return_tensors="pt") # Generate a response output = model.generate( input_ids, attention_mask=torch.ones_like(input_ids), # Set all positions to 1 (i.e., no padding) pad_token_id=tokenizer.eos_token_id, # Use the EOS token as the PAD token do_sample=True, max_length=1024, # Use GPT-2's maximum sequence length ) # Calculate the number of tokens in the prompt prompt_length = input_ids.shape[-1] # Decode only the response, excluding the prompt response = tokenizer.decode(output[0, prompt_length:], skip_special_tokens=True) # Replace the NaN response with the new one df.at[i, 'Response'] = response # Save the DataFrame back to the CSV file df.to_csv(response_csv_path, index=False) print( f"Regenerated response for prompt {i + 1} of {len(df)}. Updated responses saved to {response_csv_path}.") print(f"All NaN responses regenerated. Updated responses saved to {response_csv_path}.")
[ "You are a helpful assistant." ]
2024-01-10
sudz4/erlangs-toolz
main_ATLAS_shrugged_and_completed_his_time_sheet.py
# libs import openai # keys from config import OPENAI_API_TOKEN # constants # pass the API key openai.api_key = OPENAI_API_TOKEN def categorize_daily_update(text): prompt = ( "I need two categories. \"Daily Objectives:\" and \"Daily Accomplishments\". " "I am going to just start writing about my day as a ServiceNow program manager" " / solution architect. categorize each either sentence or " "phrase as an objective or an accomplishment. then complete any necessary " "sentence completion, add context you want. make the categorized output flow " "together. professionally and smartly. I submit this update with my daily time card.\n\n" f"Input:\n{text}\n\n" "Output:" ) response = openai.Completion.create( # engine="gpt-3.5-turbo", engine="text-davinci-002", prompt=prompt, max_tokens=300, n=1, stop=None, temperature=0.7, ) return response.choices[0].text.strip() #Sample input text #### COPY/PASTE here is a testing example below # EXAMPLE prompt -> meet with bijah and hunter, discuss radius project methodology and first project. Had the meeting, went great, discussed coming to Charlotte next week. per Bijah's direction, I confirmed my availability and plan to be in charlotte next week. I logged in to the ServiceNow partner portal. starting to get squared away with my credentials with everything. planning to review platform implementation and other artifacts on the partner portal and now learning. reviewed design artifacts and started to organize collateral for first Radius project. met the new PM (Robin) on the call wit Hunter and Bijah. if __name__ == "__main__": print('Directions -> be specific about your day, specific about what you are working with the Client\n if there are client issues, you can log them here, never complain though.') print() objectives_accomplishments = input(f"talk at me about your objectives and accomplishments today----> ") formatted_output = categorize_daily_update(objectives_accomplishments) print(formatted_output) print() print("make sure to submit your time card and this AI generated nonsense everyday")
[ "I need two categories. \"Daily Objectives:\" and \"Daily Accomplishments\". I am going to just start writing about my day as a ServiceNow program manager / solution architect. categorize each either sentence or phrase as an objective or an accomplishment. then complete any necessary sentence completion, add context you want. make the categorized output flow together. professionally and smartly. I submit this update with my daily time card.\n\nInput:\nPLACEHOLDER\n\nOutput:" ]
2024-01-10
ManikantaMandala/DocPlay
script.py
import pickle from docx import Document import streamlit as st import streamlit_authenticator as stauth from dependencies import sign_up, fetch_users from PyPDF2 import PdfReader from langchain.text_splitter import RecursiveCharacterTextSplitter import google.generativeai as palm from langchain.embeddings import GooglePalmEmbeddings from langchain.llms import GooglePalm from langchain.vectorstores import FAISS from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from htmlTemplates import css,bot_template,user_template from dotenv import load_dotenv load_dotenv() #This will generate text from the pdfs, docs uploaded and return the text def get_text(docs): text = "" for doc in docs: file_name = doc.name text+= "The file name is %s\n"%file_name if(file_name.endswith('.pdf')): pdf_reader = PdfReader(doc) for page in pdf_reader.pages: text += page.extract_text() if(file_name.endswith('.docs')): docs_reader = Document(doc) for paragraph in docs_reader.paragraphs: text += paragraph.text + "\n" return text #This will take the text, create the chunks and return those chunks def get_text_chunks(text): text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20) chunks = text_splitter.split_text(text) return chunks #This will take GooglePalmEmbeddings, and makes a local vector_store using FAISS def get_vector_store(text_chunks): embeddings = GooglePalmEmbeddings() vector_store = FAISS.from_texts(text_chunks, embedding=embeddings) return vector_store #This will read the previous text which is in the server def read_previous_text(username): data_file = f"data_{username}.obj" try: with open(data_file, 'rb') as readFile: previous_txt = pickle.load(readFile) return previous_txt except FileNotFoundError: return "" #This will write the updated text into the data files in the server def write_text_data_file(username,previous_text): data_file = f"data_{username}.obj" with open(data_file, 'wb') as writeFile: pickle.dump(previous_text, writeFile) writeFile.close() def get_conversational_chain(vector_store): llm=GooglePalm() memory = ConversationBufferMemory(memory_key = "chat_history", return_messages=True) conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vector_store.as_retriever(), memory=memory) return conversation_chain def submit(): st.session_state.user_question = st.session_state.widget st.session_state.widget = "" def user_input(user_question): response = st.session_state.conversation({'question': user_question}) st.session_state.chatHistory = response['chat_history'] chat_history_length = len(st.session_state.chatHistory) # Iterate over chat history in reverse order for i in range(chat_history_length - 2, -1, -2): user_message = st.session_state.chatHistory[i] bot_message = st.session_state.chatHistory[i + 1] st.write(user_template.replace("{{MSG}}", user_message.content), unsafe_allow_html=True) st.write(bot_template.replace("{{MSG}}", bot_message.content), unsafe_allow_html=True) def main(): try: st.set_page_config("DocPlay 💬") st.header("DocPlay 💬") users = fetch_users() emails = [] usernames = [] passwords = [] for user in users: emails.append(user['key']) usernames.append(user['username']) passwords.append(user['password']) credentials = {'usernames': {}} for index in range(len(emails)): credentials['usernames'][usernames[index]] = {'name': emails[index], 'password': passwords[index]} Authenticator = stauth.Authenticate(credentials, cookie_name='Streamlit', key='abcdef', cookie_expiry_days=4) email, authentication_status, username = Authenticator.login(':green[Login]', 'main') info, info1 = st.columns(2) ##check sign up if not authentication_status: sign_up() if username: if username in usernames: if authentication_status: #let user see the app st.write(css, unsafe_allow_html=True) # st.header("Chat with Multiple PDF 💬") st.text_input("Ask a Question from the PDF Files", key="widget", on_change=submit) if "user_question" not in st.session_state: st.session_state.user_question = "" user_question = st.session_state.user_question if "conversation" not in st.session_state: st.session_state.conversation = None if "chatHistory" not in st.session_state: st.session_state.chatHistory = None if "clear_history_pressed" not in st.session_state: st.session_state.clear_history_pressed = False if user_question: user_input(user_question) with st.sidebar: st.title(f'Welcome {username}') st.subheader("Upload your Documents") checkbox_container = st.empty() previous_data = checkbox_container.checkbox("Previous text", disabled = st.session_state.clear_history_pressed) if st.button("Clear History"): write_text_data_file(username,"") st.session_state.clear_history_pressed = True if previous_data: if st.button("Process"): with st.spinner("Processing"): previous_txt = read_previous_text(username) previous_txt = "\nThis is the previous text\n" + previous_txt write_text_data_file(username,previous_txt) text_chunks = get_text_chunks(previous_txt) vector_store = get_vector_store(text_chunks) st.session_state.conversation = get_conversational_chain(vector_store) st.success("Done") else: pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Process Button", accept_multiple_files=True) if st.button("Process"): with st.spinner("Processing"): raw_text = get_text(pdf_docs) previous_txt = read_previous_text(username) previous_txt = raw_text + "\nThis is the previous text\n" + previous_txt write_text_data_file(username,previous_txt) st.session_state.clear_history_pressed = False text_chunks = get_text_chunks(previous_txt) vector_store = get_vector_store(text_chunks) st.session_state.conversation = get_conversational_chain(vector_store) st.success("Done") Authenticator.logout('Log Out','sidebar') elif not authentication_status: with info: st.error('Invalid Username and Password') else: with info: st.warning('Please enter credentials') else: with info: st.warning("Username does not exist, Please Sign up") except: st.success('Refresh Page') if __name__ == "__main__": data_file = "data.obj" main()
[]
2024-01-10
hackingthemarkets/chatgpt-api-whisper-api-voice-assistant
therapist.py
import gradio as gr import openai, config, subprocess openai.api_key = config.OPENAI_API_KEY messages = [{"role": "system", "content": 'You are a therapist. Respond to all input in 25 words or less.'}] def transcribe(audio): global messages audio_filename_with_extension = audio + '.wav' os.rename(audio, audio_filename_with_extension) audio_file = open(audio_filename_with_extension, "rb") transcript = openai.Audio.transcribe("whisper-1", audio_file) messages.append({"role": "user", "content": transcript["text"]}) response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) system_message = response["choices"][0]["message"] messages.append(system_message) subprocess.call(["say", system_message['content']]) chat_transcript = "" for message in messages: if message['role'] != 'system': chat_transcript += message['role'] + ": " + message['content'] + "\n\n" return chat_transcript ui = gr.Interface(fn=transcribe, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text").launch() ui.launch()
[ "You are a therapist. Respond to all input in 25 words or less." ]
2024-01-10
Valera6/dots
home~v~s~help_scripts~ask_gpt.py
# TODO: start keeping a json with current conversation in /tmp. Always write to it; but only load when `qc` command is callled instead of `q` import openai import requests import json import time import sys import os api_key = os.getenv("OPENAI_KEY") # second var is cost of 1k tokens gpt35 = ('gpt-3.5-turbo', 0.002) gpt4t = ('gpt-4-1106-preview', 0.015) # price is a guess, and is not to be trusted standard_instruction = """Respond concisely. If no context provided, question is about Linux (arch) cli tools. No talk; just go""" f_instruction = """You are my programmer buddy. I'm pissed beyond words at some tool. You are to fully agree with me and encourage my righteous anger, using one sentence, very profane language, and insulting design flaws specific to the shit code we're angry at""" # ========================================================== openai.api_type = 'azure' openai.api_key = api_key def request(question, model=gpt4t, debug=False): global standard_instruction, f_instruction opt = sys.argv[1] if opt == '-s': instruction = standard_instruction elif opt == '-f': instruction = f_instruction question = "fuck " + question else: print('no such option, think again') sys.exit(1) system_line = {"role": "system", "content": instruction} user_line = {"role": "user", "content": question} conversation = [system_line] + [user_line] url = "https://api.openai.com/v1/chat/completions" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "model": f"{model[0]}", "messages": conversation, "temperature": 0, #"max_tokens": 100, } start_time = time.time() r = requests.post(url, headers=headers, data=json.dumps(data)).json() end_time = time.time() if debug: print(f'{model[0]}:') print(f"{conversation[-1]['content']}") print(f"{r['choices'][0]['message']['content']}") tokens = float(r['usage']['total_tokens']) cost = model[1] * tokens/1000 print(f"cost: {cost} in {end_time-start_time}\n") return r['choices'][0]['message']['content'].split(';')[0] def main(): question = ' '.join(sys.argv[2:]) return request(question) if __name__ == '__main__': print(main())
[ "fuck questionbfd4b27e-c413-4908-bbd3-ddcb484663be" ]
2024-01-10
usama04/DALLE-pytorch
dalle_pytorch~dalle_pytorch.py
from math import log2, sqrt import torch from torch import nn, einsum import torch.nn.functional as F from axial_positional_embedding import AxialPositionalEmbedding from einops import rearrange from dalle_pytorch.vae import OpenAIDiscreteVAE from dalle_pytorch.vae import VQGanVAE1024 from dalle_pytorch.transformer import Transformer # helpers def exists(val): return val is not None def default(val, d): return val if exists(val) else d def always(val): def inner(*args, **kwargs): return val return inner def is_empty(t): return t.nelement() == 0 def masked_mean(t, mask, dim = 1): t = t.masked_fill(~mask[:, :, None], 0.) return t.sum(dim = 1) / mask.sum(dim = 1)[..., None] def eval_decorator(fn): def inner(model, *args, **kwargs): was_training = model.training model.eval() out = fn(model, *args, **kwargs) model.train(was_training) return out return inner # sampling helpers def top_k(logits, thres = 0.5): num_logits = logits.shape[-1] k = max(int((1 - thres) * num_logits), 1) val, ind = torch.topk(logits, k) probs = torch.full_like(logits, float('-inf')) probs.scatter_(1, ind, val) return probs # discrete vae class class ResBlock(nn.Module): def __init__(self, chan): super().__init__() self.net = nn.Sequential( nn.Conv2d(chan, chan, 3, padding = 1), nn.ReLU(), nn.Conv2d(chan, chan, 3, padding = 1), nn.ReLU(), nn.Conv2d(chan, chan, 1) ) def forward(self, x): return self.net(x) + x class DiscreteVAE(nn.Module): def __init__( self, image_size = 256, num_tokens = 512, codebook_dim = 512, num_layers = 3, num_resnet_blocks = 0, hidden_dim = 64, channels = 3, smooth_l1_loss = False, temperature = 0.9, straight_through = False, kl_div_loss_weight = 0., normalization = ((0.5,) * 3, (0.5,) * 3) ): super().__init__() assert log2(image_size).is_integer(), 'image size must be a power of 2' assert num_layers >= 1, 'number of layers must be greater than or equal to 1' has_resblocks = num_resnet_blocks > 0 self.image_size = image_size self.num_tokens = num_tokens self.num_layers = num_layers self.temperature = temperature self.straight_through = straight_through self.codebook = nn.Embedding(num_tokens, codebook_dim) hdim = hidden_dim enc_chans = [hidden_dim] * num_layers dec_chans = list(reversed(enc_chans)) enc_chans = [channels, *enc_chans] dec_init_chan = codebook_dim if not has_resblocks else dec_chans[0] dec_chans = [dec_init_chan, *dec_chans] enc_chans_io, dec_chans_io = map(lambda t: list(zip(t[:-1], t[1:])), (enc_chans, dec_chans)) enc_layers = [] dec_layers = [] for (enc_in, enc_out), (dec_in, dec_out) in zip(enc_chans_io, dec_chans_io): enc_layers.append(nn.Sequential(nn.Conv2d(enc_in, enc_out, 4, stride = 2, padding = 1), nn.ReLU())) dec_layers.append(nn.Sequential(nn.ConvTranspose2d(dec_in, dec_out, 4, stride = 2, padding = 1), nn.ReLU())) for _ in range(num_resnet_blocks): dec_layers.insert(0, ResBlock(dec_chans[1])) enc_layers.append(ResBlock(enc_chans[-1])) if num_resnet_blocks > 0: dec_layers.insert(0, nn.Conv2d(codebook_dim, dec_chans[1], 1)) enc_layers.append(nn.Conv2d(enc_chans[-1], num_tokens, 1)) dec_layers.append(nn.Conv2d(dec_chans[-1], channels, 1)) self.encoder = nn.Sequential(*enc_layers) self.decoder = nn.Sequential(*dec_layers) self.loss_fn = F.smooth_l1_loss if smooth_l1_loss else F.mse_loss self.kl_div_loss_weight = kl_div_loss_weight # take care of normalization within class self.normalization = normalization def norm(self, images): if not exists(self.normalization): return images means, stds = map(lambda t: torch.as_tensor(t).to(images), self.normalization) means, stds = map(lambda t: rearrange(t, 'c -> () c () ()'), (means, stds)) images = images.clone() images.sub_(means).div_(stds) return images @torch.no_grad() @eval_decorator def get_codebook_indices(self, images): logits = self.forward(images, return_logits = True) codebook_indices = logits.argmax(dim = 1).flatten(1) return codebook_indices def decode( self, img_seq ): image_embeds = self.codebook(img_seq) b, n, d = image_embeds.shape h = w = int(sqrt(n)) image_embeds = rearrange(image_embeds, 'b (h w) d -> b d h w', h = h, w = w) images = self.decoder(image_embeds) return images def forward( self, img, return_loss = False, return_recons = False, return_logits = False, temp = None ): device, num_tokens, image_size, kl_div_loss_weight = img.device, self.num_tokens, self.image_size, self.kl_div_loss_weight assert img.shape[-1] == image_size and img.shape[-2] == image_size, f'input must have the correct image size {image_size}' img = self.norm(img) logits = self.encoder(img) if return_logits: return logits # return logits for getting hard image indices for DALL-E training temp = default(temp, self.temperature) soft_one_hot = F.gumbel_softmax(logits, tau = temp, dim = 1, hard = self.straight_through) sampled = einsum('b n h w, n d -> b d h w', soft_one_hot, self.codebook.weight) out = self.decoder(sampled) if not return_loss: return out # reconstruction loss recon_loss = self.loss_fn(img, out) # kl divergence logits = rearrange(logits, 'b n h w -> b (h w) n') log_qy = F.log_softmax(logits, dim = -1) log_uniform = torch.log(torch.tensor([1. / num_tokens], device = device)) kl_div = F.kl_div(log_uniform, log_qy, None, None, 'batchmean', log_target = True) loss = recon_loss + (kl_div * kl_div_loss_weight) if not return_recons: return loss return loss, out # main classes class CLIP(nn.Module): def __init__( self, *, dim_text = 512, dim_image = 512, dim_latent = 512, num_text_tokens = 10000, text_enc_depth = 6, text_seq_len = 256, text_heads = 8, num_visual_tokens = 512, visual_enc_depth = 6, visual_heads = 8, visual_image_size = 256, visual_patch_size = 32, channels = 3 ): super().__init__() self.text_emb = nn.Embedding(num_text_tokens, dim_text) self.text_pos_emb = nn.Embedding(text_seq_len, dim_text) self.text_transformer = Transformer(causal = False, seq_len = text_seq_len, dim = dim_text, depth = text_enc_depth, heads = text_heads) self.to_text_latent = nn.Linear(dim_text, dim_latent, bias = False) assert visual_image_size % visual_patch_size == 0, 'Image dimensions must be divisible by the patch size.' num_patches = (visual_image_size // visual_patch_size) ** 2 patch_dim = channels * visual_patch_size ** 2 self.visual_patch_size = visual_patch_size self.to_visual_embedding = nn.Linear(patch_dim, dim_image) self.visual_pos_emb = nn.Embedding(num_patches, dim_image) self.visual_transformer = Transformer(causal = False, seq_len = num_patches, dim = dim_image, depth = visual_enc_depth, heads = visual_heads) self.to_visual_latent = nn.Linear(dim_image, dim_latent, bias = False) self.temperature = nn.Parameter(torch.tensor(1.)) def forward( self, text, image, text_mask = None, return_loss = False ): b, device, p = text.shape[0], text.device, self.visual_patch_size text_emb = self.text_emb(text) text_emb += self.text_pos_emb(torch.arange(text.shape[1], device = device)) image_patches = rearrange(image, 'b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = p, p2 = p) image_emb = self.to_visual_embedding(image_patches) image_emb += self.visual_pos_emb(torch.arange(image_emb.shape[1], device = device)) enc_text = self.text_transformer(text_emb, mask = text_mask) enc_image = self.visual_transformer(image_emb) if exists(text_mask): text_latents = masked_mean(enc_text, text_mask, dim = 1) else: text_latents = enc_text.mean(dim = 1) image_latents = enc_image.mean(dim = 1) text_latents = self.to_text_latent(text_latents) image_latents = self.to_visual_latent(image_latents) text_latents, image_latents = map(lambda t: F.normalize(t, p = 2, dim = -1), (text_latents, image_latents)) temp = self.temperature.exp() if not return_loss: sim = einsum('n d, n d -> n', text_latents, image_latents) * temp return sim sim = einsum('i d, j d -> i j', text_latents, image_latents) * temp labels = torch.arange(b, device = device) loss = (F.cross_entropy(sim, labels) + F.cross_entropy(sim.t(), labels)) / 2 return loss # main DALL-E class class DALLE(nn.Module): def __init__( self, *, dim, vae, num_text_tokens = 10000, text_seq_len = 256, depth, heads = 8, dim_head = 64, reversible = False, attn_dropout = 0., ff_dropout = 0, sparse_attn = False, attn_types = None, loss_img_weight = 7 ): super().__init__() assert isinstance(vae, (DiscreteVAE, OpenAIDiscreteVAE, VQGanVAE1024)), 'vae must be an instance of DiscreteVAE' image_size = vae.image_size num_image_tokens = vae.num_tokens image_fmap_size = (vae.image_size // (2 ** vae.num_layers)) image_seq_len = image_fmap_size ** 2 self.text_emb = nn.Embedding(num_text_tokens, dim) self.image_emb = nn.Embedding(num_image_tokens, dim) self.text_pos_emb = nn.Embedding(text_seq_len + 1, dim) # +1 for <bos> self.image_pos_emb = AxialPositionalEmbedding(dim, axial_shape = (image_fmap_size, image_fmap_size)) self.num_text_tokens = num_text_tokens # for offsetting logits index and calculating cross entropy loss self.num_image_tokens = num_image_tokens self.text_seq_len = text_seq_len self.image_seq_len = image_seq_len seq_len = text_seq_len + image_seq_len total_tokens = num_text_tokens + num_image_tokens self.total_tokens = total_tokens self.total_seq_len = seq_len self.vae = vae self.transformer = Transformer( dim = dim, causal = True, seq_len = seq_len, depth = depth, heads = heads, dim_head = dim_head, reversible = reversible, attn_dropout = attn_dropout, ff_dropout = ff_dropout, attn_types = attn_types, image_fmap_size = image_fmap_size, sparse_attn = sparse_attn ) self.to_logits = nn.Sequential( nn.LayerNorm(dim), nn.Linear(dim, self.total_tokens), ) seq_range = torch.arange(seq_len) logits_range = torch.arange(total_tokens) seq_range = rearrange(seq_range, 'n -> () n ()') logits_range = rearrange(logits_range, 'd -> () () d') logits_mask = ( ((seq_range >= text_seq_len) & (logits_range < num_text_tokens)) | ((seq_range < text_seq_len) & (logits_range >= num_text_tokens)) ) self.register_buffer('logits_mask', logits_mask) self.loss_img_weight = loss_img_weight @torch.no_grad() @eval_decorator def generate_images( self, text, *, clip = None, mask = None, filter_thres = 0.5, temperature = 1., img = None, num_init_img_tokens = None ): vae, text_seq_len, image_seq_len, num_text_tokens = self.vae, self.text_seq_len, self.image_seq_len, self.num_text_tokens total_len = text_seq_len + image_seq_len text = text[:, :text_seq_len] # make sure text is within bounds out = text if exists(img): image_size = vae.image_size assert img.shape[1] == 3 and img.shape[2] == image_size and img.shape[3] == image_size, f'input image must have the correct image size {image_size}' indices = vae.get_codebook_indices(img) num_img_tokens = default(num_init_img_tokens, int(0.4375 * image_seq_len)) # OpenAI used 14 * 32 initial tokens to prime assert num_img_tokens < image_seq_len, 'number of initial image tokens for priming must be less than the total image token sequence length' indices = indices[:, :num_img_tokens] out = torch.cat((out, indices), dim = -1) for cur_len in range(out.shape[1], total_len): is_image = cur_len >= text_seq_len text, image = out[:, :text_seq_len], out[:, text_seq_len:] logits = self(text, image, mask = mask)[:, -1, :] filtered_logits = top_k(logits, thres = filter_thres) probs = F.softmax(filtered_logits / temperature, dim = -1) sample = torch.multinomial(probs, 1) sample -= (num_text_tokens if is_image else 0) # offset sampled token if it is an image token, since logit space is composed of text and then image tokens out = torch.cat((out, sample), dim=-1) if out.shape[1] <= text_seq_len: mask = F.pad(mask, (0, 1), value = True) text_seq = out[:, :text_seq_len] img_seq = out[:, -image_seq_len:] images = vae.decode(img_seq) if exists(clip): scores = clip(text_seq, images, return_loss = False) return images, scores return images def forward( self, text, image = None, mask = None, return_loss = False ): assert text.shape[-1] == self.text_seq_len, f'the length {text.shape[-1]} of the text tokens you passed in does not have the correct length ({self.text_seq_len})' device, total_seq_len = text.device, self.total_seq_len text = F.pad(text, (1, 0), value = 0) # use padding as <bos> if exists(mask): mask = F.pad(mask, (1, 0), value = True) tokens = self.text_emb(text) tokens += self.text_pos_emb(torch.arange(text.shape[1], device = device)) seq_len = tokens.shape[1] if exists(image) and not is_empty(image): is_raw_image = len(image.shape) == 4 if is_raw_image: image_size = self.vae.image_size assert tuple(image.shape[1:]) == (3, image_size, image_size), f'invalid image of dimensions {image.shape} passed in during training' image = self.vae.get_codebook_indices(image) image_len = image.shape[1] image_emb = self.image_emb(image) image_emb += self.image_pos_emb(image_emb) tokens = torch.cat((tokens, image_emb), dim = 1) seq_len += image_len if exists(mask): mask = F.pad(mask, (0, image_emb.shape[1]), value = True) # when training, if the length exceeds the total text + image length # remove the last token, since it needs not to be trained if tokens.shape[1] > total_seq_len: seq_len -= 1 tokens = tokens[:, :-1] if exists(mask): mask = mask[:, :-1] out = self.transformer(tokens, mask = mask) logits = self.to_logits(out) # mask logits to make sure text predicts text (except last token), and image predicts image logits_mask = self.logits_mask[:, :seq_len] max_neg_value = -torch.finfo(logits.dtype).max logits.masked_fill_(logits_mask, max_neg_value) if not return_loss: return logits assert exists(image), 'when training, image must be supplied' offsetted_image = image + self.num_text_tokens labels = torch.cat((text[:, 1:], offsetted_image), dim = 1) logits = rearrange(logits, 'b n c -> b c n') loss_text = F.cross_entropy(logits[:, :, :self.text_seq_len], labels[:, :self.text_seq_len], ignore_index=0) loss_img = F.cross_entropy(logits[:, :, self.text_seq_len:], labels[:, self.text_seq_len:], ignore_index=0) loss = (loss_text + self.loss_img_weight * loss_img) / (self.loss_img_weight + 1) return loss
[]
2024-01-10
usama04/DALLE-pytorch
dalle_pytorch~vae.py
import io import sys import os, sys import requests import PIL import warnings import os import hashlib import urllib import yaml from pathlib import Path from tqdm import tqdm from math import sqrt from omegaconf import OmegaConf from taming.models.vqgan import VQModel import torch from torch import nn import torch.nn.functional as F from einops import rearrange from dalle_pytorch import deepspeed_utils # constants CACHE_PATH = os.path.expanduser("~/.cache/dalle") OPENAI_VAE_ENCODER_PATH = 'https://cdn.openai.com/dall-e/encoder.pkl' OPENAI_VAE_DECODER_PATH = 'https://cdn.openai.com/dall-e/decoder.pkl' VQGAN_VAE_PATH = 'https://heibox.uni-heidelberg.de/f/140747ba53464f49b476/?dl=1' VQGAN_VAE_CONFIG_PATH = 'https://heibox.uni-heidelberg.de/f/6ecf2af6c658432c8298/?dl=1' # helpers methods def exists(val): return val is not None def default(val, d): return val if exists(val) else d def load_model(path): with open(path, 'rb') as f: return torch.load(f, map_location = torch.device('cpu')) def map_pixels(x, eps = 0.1): return (1 - 2 * eps) * x + eps def unmap_pixels(x, eps = 0.1): return torch.clamp((x - eps) / (1 - 2 * eps), 0, 1) def download(url, filename = None, root = CACHE_PATH): if deepspeed_utils.is_local_root_worker(): os.makedirs(root, exist_ok = True) filename = default(filename, os.path.basename(url)) download_target = os.path.join(root, filename) download_target_tmp = os.path.join(root, f'tmp.{filename}') if os.path.exists(download_target) and not os.path.isfile(download_target): raise RuntimeError(f"{download_target} exists and is not a regular file") if not deepspeed_utils.is_local_root_worker() and not os.path.isfile(download_target): # If the file doesn't exist yet, wait until it's downloaded by the root worker. deepspeed_utils.local_barrier() if os.path.isfile(download_target): return download_target with urllib.request.urlopen(url) as source, open(download_target_tmp, "wb") as output: with tqdm(total=int(source.info().get("Content-Length")), ncols=80) as loop: while True: buffer = source.read(8192) if not buffer: break output.write(buffer) loop.update(len(buffer)) os.rename(download_target_tmp, download_target) if deepspeed_utils.using_deepspeed and deepspeed_utils.is_local_root_worker(): deepspeed_utils.local_barrier() return download_target # pretrained Discrete VAE from OpenAI class OpenAIDiscreteVAE(nn.Module): def __init__(self): super().__init__() self.enc = load_model(download(OPENAI_VAE_ENCODER_PATH)) self.dec = load_model(download(OPENAI_VAE_DECODER_PATH)) self.num_layers = 3 self.image_size = 256 self.num_tokens = 8192 @torch.no_grad() def get_codebook_indices(self, img): img = map_pixels(img) z_logits = self.enc(img) z = torch.argmax(z_logits, dim = 1) return rearrange(z, 'b h w -> b (h w)') def decode(self, img_seq): b, n = img_seq.shape img_seq = rearrange(img_seq, 'b (h w) -> b h w', h = int(sqrt(n))) z = F.one_hot(img_seq, num_classes = self.num_tokens) z = rearrange(z, 'b h w c -> b c h w').float() x_stats = self.dec(z).float() x_rec = unmap_pixels(torch.sigmoid(x_stats[:, :3])) return x_rec def forward(self, img): raise NotImplemented # VQGAN from Taming Transformers paper # https://arxiv.org/abs/2012.09841 class VQGanVAE1024(nn.Module): def __init__(self): super().__init__() model_filename = 'vqgan.1024.model.ckpt' config_filename = 'vqgan.1024.config.yml' download(VQGAN_VAE_CONFIG_PATH, config_filename) download(VQGAN_VAE_PATH, model_filename) config = OmegaConf.load(str(Path(CACHE_PATH) / config_filename)) model = VQModel(**config.model.params) state = torch.load(str(Path(CACHE_PATH) / model_filename), map_location = 'cpu')['state_dict'] model.load_state_dict(state, strict = False) self.model = model self.num_layers = 4 self.image_size = 256 self.num_tokens = 1024 @torch.no_grad() def get_codebook_indices(self, img): b = img.shape[0] img = (2 * img) - 1 _, _, [_, _, indices] = self.model.encode(img) return rearrange(indices, '(b n) () -> b n', b = b) def decode(self, img_seq): b, n = img_seq.shape one_hot_indices = F.one_hot(img_seq, num_classes = self.num_tokens).float() z = (one_hot_indices @ self.model.quantize.embedding.weight) z = rearrange(z, 'b (h w) c -> b c h w', h = int(sqrt(n))) img = self.model.decode(z) img = (img.clamp(-1., 1.) + 1) * 0.5 return img def forward(self, img): raise NotImplemented
[]
2024-01-10
duythvn/marqotest
examples~SpeechProcessing~SpeechSearch~chatter.py
import marqo from langchain.llms import OpenAI from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from dotenv import load_dotenv load_dotenv() TEMPLATE = """ You are a question answerer, given the CONTEXT provided you will answer the QUESTION (also provided). If you are not sure of the answer then say 'I am sorry but I do not know the answer' Your answers should two to five sentences in length and only contain information relevant to the question. You should match the tone of the CONTEXT. The beginnings of the CONTEXT should be the most relevant so try and use that wherever possible, it is important that your answers a factual and don't make up information that is not in the CONTEXT. CONTEXT: ========= {context} QUESTION: ========= {question} """ def answer_question( query: str, limit: int, index: str, mq: marqo.Client, ) -> str: print("Searching...") results = mq.index(index).search( q=query, limit=limit, ) print("Done!") context = ". ".join([r["transcription"] for r in results["hits"]]) prompt = PromptTemplate(template=TEMPLATE, input_variables=["context", "question"]) llm = OpenAI(temperature=0.9, model_name="text-davinci-003") chain_qa = LLMChain(llm=llm, prompt=prompt) llm_results = chain_qa( {"context": context, "question": query}, return_only_outputs=True ) return llm_results["text"]
[ "context", "question", "\nYou are a question answerer, given the CONTEXT provided you will answer the QUESTION (also provided).\nIf you are not sure of the answer then say 'I am sorry but I do not know the answer'\nYour answers should two to five sentences in length and only contain information relevant to the question. You should match the tone of the CONTEXT.\nThe beginnings of the CONTEXT should be the most relevant so try and use that wherever possible, it is important that your answers a factual and don't make up information that is not in the CONTEXT.\n\n\nCONTEXT:\n=========\n{context}\nQUESTION:\n=========\n{question}\n", "I am sorry but I do not know the answer" ]
2024-01-10
duythvn/marqotest
examples~GPT-examples~product_q_n_a.py
from marqo import Client import pandas as pd import numpy as np from langchain.llms import OpenAI from langchain.docstore.document import Document from langchain.chains import LLMChain from dotenv import load_dotenv from utilities import ( load_data, extract_text_from_highlights, qna_prompt, predict_ce, get_sorted_inds ) load_dotenv() if __name__ == "__main__": ############################################################# # 0. Install Marqo ############################################################# # run the following docker commands from the terminal to start marqo # docker rm -f marqo # docker pull marqoai/marqo:latest # docker run --name marqo -it --privileged -p 8882:8882 --add-host host.docker.internal:host-gateway marqoai/marqo:latest ############################################################# # 1. Setup Marqo ############################################################# mq = Client() index_name = "iron-docs" # (optinally) delete if it already exists try: mq.index(index_name).delete() except: pass # we can set some specific settings for the index. if they are not provided, sensible defaults are used index_settings = { "index_defaults": { "model": "flax-sentence-embeddings/all_datasets_v4_MiniLM-L6", "normalize_embeddings": True, "text_preprocessing": { "split_length": 3, "split_overlap": 1, "split_method": "sentence" }, }, } # create the index with custom settings mq.create_index(index_name, settings_dict=index_settings) ############################################################# # 2. Load the data ############################################################# df = load_data() # turn the data into a dict for indexing documents = df.to_dict(orient='records') ############################################################# # 3. Index the data ############################################################# # index the documents indexing = mq.index(index_name).add_documents(documents, non_tensor_fields=['filename']) ############################################################# # 4. Search the data ############################################################# # try a generic search q = "what is the rated voltage" results = mq.index(index_name).search(q) print(results['hits'][0]) ############################################################# # 5. Make it chatty ############################################################# highlights, texts = extract_text_from_highlights(results, token_limit=150) docs = [Document(page_content=f"Source [{ind}]:"+t) for ind,t in enumerate(texts)] llm = OpenAI(temperature=0.9) chain_qa = LLMChain(llm=llm, prompt=qna_prompt()) llm_results = chain_qa({"summaries": docs, "question": results['query']}, return_only_outputs=True) print(llm_results['text']) ############################################################# # 6. Score the references ############################################################# score_threshold = 0.20 top_k = 3 scores = predict_ce(llm_results['text'], texts) inds = get_sorted_inds(scores) scores = scores.cpu().numpy() scores = [np.round(s[0],2) for s in scores] references = [(str(np.round(scores[i],2)),texts[i]) for i in inds[:top_k] if scores[i] > score_threshold] df_ref = pd.DataFrame(references, columns=['score','sources']) print(df_ref)
[]
2024-01-10
duythvn/marqotest
examples~GPT-examples~ironman.py
import pandas as pd from utilities import ( marqo_prompt, extract_text_from_highlights, marqo_template, get_extra_data, reformat_npcs ) from dotenv import load_dotenv from langchain.llms import OpenAI from langchain.docstore.document import Document from langchain.chains import LLMChain load_dotenv() if __name__ == "__main__": ############################################################# # 0. Install Marqo ############################################################# # run the following docker commands from the terminal to start marqo # docker rm -f marqo # docker pull marqoai/marqo:latest # docker run --name marqo -it --privileged -p 8882:8882 --add-host host.docker.internal:host-gateway marqoai/marqo:latest ############################################################# # 1. Create some data ############################################################# NPCs = [{ "name": "Sara Lee", "backstory": "Sara was born in a small village in the mountains. She was always fascinated with nature and as soon as she was old enough, she left her village to study environmental science at a university. She now works as a park ranger.", "location": "The mountains", "occupation": "Park ranger", "family_history": "Sara is an only child and her parents were both farmers. Growing up close to nature instilled in her a deep respect and appreciation for the environment.", "work_history": "Sara worked as a research assistant for a university before becoming a park ranger.", "favorite_color": "Yellow", "hobbies": "Hiking, bird watching, and photography", "favorite_food": "Fruits and vegetables", "dislikes": "Loud noises", }, { "name": "Jack Smith", "backstory": "Jack was born and raised in the city. He has always had a love for cars and as soon as he could he began working on them. He now runs his own successful auto repair shop.", "location": "The city", "occupation": "Auto mechanic", "family_history": "Jack has a younger sister and his father was also a mechanic who ran his own shop.", "work_history": "Jack worked as a mechanic at several auto repair shops before opening his own business.", "favorite_color": "Blue", "hobbies": "Working on cars, fishing, and playing video games", "favorite_food": "Steak", "dislikes": "Celery", }, { "name": "Evelyn Parker", "backstory": "Evelyn grew up in a small town in the countryside. She always had a passion for cooking and eventually moved to the city to attend culinary school. She now works as a chef at a popular restaurant.", "location": "The city", "occupation": "Chef", "family_history": "Evelyn is the youngest of three siblings. Her parents were farmers and instilled in her a love for cooking with fresh ingredients.", "work_history": "Evelyn worked as a line cook at several restaurants before attending culinary school and becoming a head chef.", "favorite_color": "Green", "hobbies": "Cooking, gardening, and reading", "favorite_food": "Seafood", "dislikes": "Cilantro", }] df = pd.DataFrame(reformat_npcs(NPCs)) print(df.head()) # make the data python dicts documents = df.to_dict(orient='records') ############################################################# # 2. Setup Marqo ############################################################# import marqo from marqo import Client marqo.set_log_level('WARN') mq = Client() index_name = "npc-chat" try: mq.index(index_name).delete() except: pass index_settings = { "index_defaults": { "normalize_embeddings": True, "text_preprocessing": { "split_length": 5, "split_overlap": 1, "split_method": "sentence" }, } } # create the index - if no settings are present then sensible deffaults are used mq.create_index(index_name, settings_dict=index_settings) res = mq.index(index_name).add_documents(documents) ############################################################# # 3. Regular NPC superhero ############################################################# # select a character persona = "Evelyn Parker" # we pre-opulate them here to complete a conversation but it can easily be made interactive human_questions = [ "hi, what is your name?", "wow, what are some of your favorite things to do?", "are you scared of anything?", "where did you grow up?", "what do you dislike?"] history = [] template = marqo_template() prompt = marqo_prompt(template) # how many pieces of context to use n_history = 2 # setup the LLM API call llm = OpenAI(temperature=0.9) for question in human_questions: history.append(f"\nHUMAN:{question}") print(history[-1]) # search for background related to the question results = mq.index(index_name).search(question, filter_string=f"name:({persona})", searchable_attributes=['text'], limit=20) # optionally crop the text to the highlighted region to fit within the context window highlights, texts = extract_text_from_highlights(results, token_limit=150) # add the truncated/cropped text to the data structure for langchain summaries = [Document(page_content=f"Source [{ind}]:"+t) for ind,t in enumerate(texts[:n_history])] # get the conversation history chain_qa = LLMChain(llm=llm, prompt=prompt) llm_results = chain_qa({"summaries": summaries, "conversation": "\n".join(history)}, return_only_outputs=False) history.append(llm_results['text']) print(history[-1]) ############################################################# # 3. IRONMAN ############################################################# persona = "Evelyn Parker" # add some more info extra_docs = [{"text":text, "name":persona} for text in get_extra_data()] res = mq.index(index_name).add_documents(extra_docs, non_tensor_fields=['filename']) # we pre-opulate them here to complete a conversation but it can easily be made interactive human_questions = [ "hi, what is your name?", "wow, what are some of your favorite things to do?", "are you scared of anything?", "where did you grow up?", "what do you dislike?"] history = [] template = marqo_template() prompt = marqo_prompt(template) # how many pieces of context to use n_history = 2 for question in human_questions: history.append(f"\nHUMAN:{question}") print(history[-1]) # search for background related to the question results = mq.index(index_name).search(question, filter_string=f"name:({persona})", searchable_attributes=['text'], limit=20) # optionally crop the text to the highlighted region to fit within the context window highlights, texts = extract_text_from_highlights(results, token_limit=150) # add the truncated/cropped text to the data structure for langchain summaries = [Document(page_content=f"Source [{ind}]:"+t) for ind,t in enumerate(texts[-n_history:])] # get the conversation history chain_qa = LLMChain(llm=llm, prompt=prompt) llm_results = chain_qa({"summaries": summaries, "conversation": "\n".join(history)}, return_only_outputs=False) history.append(llm_results['text']) print(history[-1])
[]
2024-01-10
touhonoob/Buff
server~chat-widget~gather_documents.py
from langchain.llms import OpenAI from langchain.chains.qa_with_sources import load_qa_with_sources_chain from langchain.docstore.document import Document from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Pinecone from langchain.text_splitter import CharacterTextSplitter import pinecone from bs4 import BeautifulSoup import requests import csv import sys import time import os import json from dotenv import load_dotenv import webvtt load_dotenv() openai = OpenAI(openai_api_key=os.environ.get('OPENAI_API_KEY'), temperature=0) openai_embeddings = OpenAIEmbeddings(openai_api_key=os.environ.get('OPENAI_API_KEY')) pinecone.init(api_key=os.environ.get('PINECONE_API_KEY'), environment="us-east1-gcp") index = pinecone.Index("knowledgebase") vectorstore = Pinecone(index, openai_embeddings.embed_query, "text") def gather_search_index_from_urls(urls): documents = [] metadatas = [] ids = [] for i in range(len(urls)): url = urls[i] response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') text = soup.get_text() documents.append(text) metadatas.append({"source": url}) ids.append(str(i)) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) def gather_search_index_from_csv(filename): documents = [] metadatas = [] ids = [] with open(filename, newline='') as csvfile: csvreader = csv.reader(csvfile, delimiter=',', quotechar='\"') id = 0 for row in csvreader: # ignore the first row if row[2] == 'title' and row[3] == 'desc-un-html': continue print(id) url = row[1] title = row[2] body = row [3] documents.append("{0} {1}".format(title, body)) metadatas.append({"source": url}) ids.append(str(id)) id += 1 if len(documents) == 20: vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) documents = [] metadatas = [] ids = [] time.sleep(1) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) def gather_search_index_from_json(filename): documents = [] metadatas = [] ids = [] splitter = CharacterTextSplitter(separator=" ", chunk_size=1024, chunk_overlap=0) with open(filename, newline='') as jsonfile: jsonreader = json.load(jsonfile) id = 0 for article in jsonreader: print(id) url = article['url'] title = article['title'] subtitle = article['subtitle'] body = article['body'] document = "{0} {1} {2}".format(title, subtitle, body) for chunk in splitter.split_text(document): documents.append(chunk) metadatas.append({"source": url}) ids.append(str(id)) id += 1 if len(documents) == 20: print(metadatas) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) documents = [] metadatas = [] ids = [] time.sleep(1) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) def gather_search_index_from_video_transcripts(folder_path): documents = [] metadatas = [] ids = [] id = 0 for filename in os.listdir(folder_path): if not filename.endswith('.vtt'): continue transcript = "" for caption in webvtt.read(os.path.join(folder_path, filename)): transcript += caption.text + " " if len(transcript) >= 1024: documents.append(transcript) metadatas.append({"source": filename[:-4]}) ids.append(str(id)) transcript = "" id += 1 if len(documents) == 20: print(metadatas) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) documents = [] metadatas = [] ids = [] time.sleep(1) if len(transcript) > 0: documents.append(transcript) metadatas.append({"source": filename[:-4]}) ids.append(str(id)) id += 1 if len(documents) == 20: print(metadatas) vectorstore.add_texts(texts=documents, metadatas=metadatas, ids=ids) documents = [] metadatas = [] ids = [] time.sleep(1) if __name__ == '__main__': gather_search_index_from_video_transcripts(sys.argv[1])
[]
2024-01-10
Jsoke18/AssistaBot
asistabot~app2.py
import openai import pyttsx3 import speech_recognition as sr import requests import smtplib import re # Set your OpenAI API key openai.api_key = "" OPENWEATHERMAP_API_KEY = "" EMAIL_ADDRESS = "[email protected]" EMAIL_PASSWORD = "" # Initialize text-to-speech engine tts_engine = pyttsx3.init() voices = tts_engine.getProperty('voices') for i, voice in enumerate(voices): print(f"Voice {i}: {voice.name}, {voice.languages}, {voice.gender}, {voice.age}") def listen(expected_phrases=None): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Listening...") audio = recognizer.listen(source, timeout=10, phrase_time_limit=15) try: text = recognizer.recognize_google(audio, show_all=False, language='en-US', preferred_phrases=expected_phrases) print(f"You said: {text}") if expected_phrases: email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' match = re.search(email_pattern, text) if match: print(f"Found email: {match.group()}") return match.group() return text except: print("Sorry, I couldn't understand.") return None def is_email_trigger(text): email_triggers = [ "send email", "compose email", "create email", "write email", "email someone", ] for trigger in email_triggers: if trigger in text.lower(): return True return False def generate_response(prompt): response = openai.Completion.create( engine="text-davinci-003", # Change this to "gpt-3.5-turbo" when it becomes available prompt=prompt, max_tokens=150, n=1, stop=None, temperature=0.7, ) return response.choices[0].text.strip() def get_weather(city, api_key): url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" response = requests.get(url) if response.status_code == 200: data = response.json() main = data["main"] weather_desc = data["weather"][0]["description"] temp = main["temp"] return f"The current weather in {city} is {weather_desc} with a temperature of {temp}°C." else: return f"Sorry, I couldn't get the weather for {city}." def get_city_name(): speak("Please tell me the name of the city.") city = listen() if city is None: city = "unknown" return city def send_email(subject, body, to_email): try: with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server: server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) message = f"Subject: {subject}\n\n{body}" server.sendmail(EMAIL_ADDRESS, to_email, message) return "Email sent successfully." except Exception as e: print(e) return "Failed to send email." def speak(text): print(f"Chatbot: {text}") voices = tts_engine.getProperty('voices') # Change the voice index to choose a different voice voice_index = 0 tts_engine.setProperty('voice', voices[voice_index].id) tts_engine.say(text) tts_engine.runAndWait() def send_email(subject, body, to_email): try: with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server: server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) message = f"Subject: {subject}\n\n{body}" server.sendmail(EMAIL_ADDRESS, to_email, message) return "Email sent successfully." except Exception as e: print(e) return "Failed to send email." if __name__ == "__main__": print("Welcome to the GPT-3.5 Turbo Chatbot!") print("Press Ctrl+C to quit.") prompt = "you are a helpful assistant and a master in various things such as science, tech and history" while True: try: user_input = listen() if user_input is not None: user_input = user_input.strip() if "weather" in user_input.lower(): city = get_city_name() weather_info = get_weather(city, OPENWEATHERMAP_API_KEY) print(weather_info) speak(weather_info) elif is_email_trigger(user_input): speak("Please tell me the email address.") expected_email_domains = ["gmail.com", "yahoo.com", "hotmail.com", "fitzba.com", "outlook.com", "facebook.com"] to_email = listen(expected_phrases=expected_email_domains) speak("Please tell me the subject.") subject = listen() speak("Please tell me the content.") body = listen() response = send_email(subject, body, to_email) print(response) speak(response) else: prompt += f"\nUser: {user_input}" response = generate_response(prompt) prompt += f"\nChatbot: {response}" speak(response) except KeyboardInterrupt: print("\nGoodbye!") break
[ "you are a helpful assistant and a master in various things such as science, tech and history", "\nUser: PLACEHOLDER", "\nChatbot: PLACEHOLDER" ]
2024-01-10
Mosketa/UnstructuredTranscriptSummarizer
follow_topic.py
import re import os import json import openai import textwrap from time import time,sleep def open_file(filepath): with open(filepath, 'r', encoding='utf-8') as infile: return infile.read() def save_file(filepath, content): with open(filepath, 'w', encoding='utf-8') as outfile: outfile.write(content) def save_json(filepath, payload): with open(filepath, 'w', encoding='utf-8') as outfile: json.dump(payload, outfile, ensure_ascii=False, sort_keys=True, indent=1) openai.api_key = open_file('openaiapikey.txt') def gpt3_completion(prompt, engine='text-davinci-002', temp=0.3, top_p=1.0, tokens=2000, freq_pen=0.0, pres_pen=0.0, stop=['asdfasdf', 'asdasdf']): max_retry = 5 retry = 0 prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() # force it to fix any unicode errors while True: try: response = openai.Completion.create( engine=engine, prompt=prompt, temperature=temp, max_tokens=tokens, top_p=top_p, frequency_penalty=freq_pen, presence_penalty=pres_pen, stop=stop) text = response['choices'][0]['text'].strip() #text = re.sub('\s+', ' ', text) filename = '%s_gpt3.txt' % time() if not os.path.exists('gpt3_logs'): os.makedirs('gpt3_logs') save_file('gpt3_logs/%s' % filename, prompt + '\n\n==========\n\n' + text) return text except Exception as oops: retry += 1 if retry >= max_retry: return "GPT3 error: %s" % oops print('Error communicating with OpenAI:', oops) sleep(1) if __name__ == '__main__': files = os.listdir('transcripts/') topic = "AI Ethics" for file in files: transcript = open_file('transcripts/%s' % file) chunks = textwrap.wrap(transcript, 6000) output = '' for chunk in chunks: # get topics prompt = open_file('prompt_detailed_notes.txt').replace('<<TRANSCRIPT>>', chunk).replace('<<TOPIC>>', topic) notes = gpt3_completion(prompt) print('\n\n', notes) output += '\n\n%s' % notes # rewrite topical notes as one flowing narrative prompt = open_file('prompt_flowing_coherent_narrative.txt').replace('<<NOTES>>', output.strip()) final = gpt3_completion(prompt) # save out to file filename = 'insights/%s_%s' % (topic.replace(' ','_'), file) save_file(filename, final)
[ "<<TRANSCRIPT>>", "ignore", "prompt_detailed_notes.txt", "prompt_flowing_coherent_narrative.txt" ]
2024-01-10
Mosketa/UnstructuredTranscriptSummarizer
transcript_insights.py
import re import os import json import openai import textwrap from time import time,sleep def open_file(filepath): with open(filepath, 'r', encoding='utf-8') as infile: return infile.read() def save_file(filepath, content): with open(filepath, 'w', encoding='utf-8') as outfile: outfile.write(content) def save_json(filepath, payload): with open(filepath, 'w', encoding='utf-8') as outfile: json.dump(payload, outfile, ensure_ascii=False, sort_keys=True, indent=1) openai.api_key = open_file('openaiapikey.txt') def gpt3_completion(prompt, engine='text-davinci-002', temp=0.3, top_p=1.0, tokens=2000, freq_pen=0.0, pres_pen=0.0, stop=['asdfasdf', 'asdasdf']): max_retry = 5 retry = 0 prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() # force it to fix any unicode errors while True: try: response = openai.Completion.create( engine=engine, prompt=prompt, temperature=temp, max_tokens=tokens, top_p=top_p, frequency_penalty=freq_pen, presence_penalty=pres_pen, stop=stop) text = response['choices'][0]['text'].strip() #text = re.sub('\s+', ' ', text) filename = '%s_gpt3.txt' % time() if not os.path.exists('gpt3_logs'): os.makedirs('gpt3_logs') save_file('gpt3_logs/%s' % filename, prompt + '\n\n==========\n\n' + text) return text except Exception as oops: retry += 1 if retry >= max_retry: return "GPT3 error: %s" % oops print('Error communicating with OpenAI:', oops) sleep(1) if __name__ == '__main__': files = os.listdir('transcripts/') for file in files: if os.path.exists('clarified/%s' % file): print('Skipping:', file) continue transcript = open_file('transcripts/%s' % file) chunks = textwrap.wrap(transcript, 6000) output = list() for chunk in chunks: # get topics prompt = open_file('prompt_topic_extraction.txt').replace('<<TRANSCRIPT>>', chunk) topics = gpt3_completion(prompt) # get notes prompt = open_file('prompt_topic_notes.txt').replace('<<TRANSCRIPT>>', chunk).replace('<<TOPICS>>', topics) notes = gpt3_completion(prompt) #print(topics) #exit() info = {'topics': topics, 'notes': notes} print(info) output.append(info) filepath = 'notes/%s' % file.replace('.txt','.json') save_json(filepath, output)
[ "prompt_topic_notes.txt", "<<TRANSCRIPT>>", "<<TOPICS>>", "prompt_topic_extraction.txt", "ignore" ]
2024-01-10
EvgeniiTitov/ray
rllib~examples~env~cliff_walking_wall_env.py
import gym from gym import spaces ACTION_UP = 0 ACTION_RIGHT = 1 ACTION_DOWN = 2 ACTION_LEFT = 3 class CliffWalkingWallEnv(gym.Env): """Modified version of the CliffWalking environment from OpenAI Gym with walls instead of a cliff. ### Description The board is a 4x12 matrix, with (using NumPy matrix indexing): - [3, 0] or obs==36 as the start at bottom-left - [3, 11] or obs==47 as the goal at bottom-right - [3, 1..10] or obs==37...46 as the cliff at bottom-center An episode terminates when the agent reaches the goal. ### Actions There are 4 discrete deterministic actions: - 0: move up - 1: move right - 2: move down - 3: move left You can also use the constants ACTION_UP, ACTION_RIGHT, ... defined above. ### Observations There are 3x12 + 2 possible states, not including the walls. If an action would move an agent into one of the walls, it simply stays in the same position. ### Reward Each time step incurs -1 reward, except reaching the goal which gives +10 reward. """ def __init__(self, seed=42) -> None: self.observation_space = spaces.Discrete(48) self.action_space = spaces.Discrete(4) self.observation_space.seed(seed) self.action_space.seed(seed) def reset(self): self.position = 36 return self.position def step(self, action): x = self.position // 12 y = self.position % 12 # UP if action == ACTION_UP: x = max(x - 1, 0) # RIGHT elif action == ACTION_RIGHT: if self.position != 36: y = min(y + 1, 11) # DOWN elif action == ACTION_DOWN: if self.position < 25 or self.position > 34: x = min(x + 1, 3) # LEFT elif action == ACTION_LEFT: if self.position != 47: y = max(y - 1, 0) else: raise ValueError(f"action {action} not in {self.action_space}") self.position = x * 12 + y done = self.position == 47 reward = -1 if not done else 10 return self.position, reward, done, {}
[]
2024-01-10
dblasko/cv-job-matcher
job_description_embedding~JobMatchingIdealJob.py
import os import json import hashlib import re from json.decoder import JSONDecodeError from langchain.embeddings import HuggingFaceEmbeddings from langchain.chat_models.base import BaseChatModel from langchain import PromptTemplate import numpy as np from job_description_embedding.JobMatchingBaseline import JobMatchingBaseline from job_description_embedding.printer import eprint # from dotenv import load_dotenv class JobMatchingIdealJob(JobMatchingBaseline): def __init__( self, llm: BaseChatModel, embeddings: HuggingFaceEmbeddings = None, cache_dir: str = ".query_cache", ideal_job_fields={ "title": None, "company": "appropriate company or industry", "body": "brief job summary", "city": None, "state": None, "country": None, "location": "from candidate's preference or CV", "function": None, "jobtype": None, "education": None, "experience": None, "salary": None, "requiredlanguages": "from CV", "requiredskills": "from CV", }, job_fields=[ "title", "company", "posted_date", "job_reference", "req_number", "url", "body", "city", "state", "country", "location", "function", "logo", "jobtype", "education", "experience", "salary", "requiredlanguages", "requiredskills", ], ): super().__init__(embeddings=embeddings) self.llm = llm self.cache_dir = cache_dir self.sha256 = hashlib.sha256 self.job_fields = job_fields self.prompt = PromptTemplate.from_template( "Analyze the following CV and transform the extracted information into an ideal job description for the candidate," + " assuming they are seeking to switch jobs or secure a new position. The output should be a valid JSON object that could be parsed by json.loads()." + " Include: " + ", ".join(f"{k} ({v})" if v else k for k, v in ideal_job_fields.items()) + "." + " Remember to use the information available in the CV, along with your general knowledge about the world, job markets, and companies, to make informed choices for each field." + " If a field cannot be filled based on the information, set it to null. Please respond with just the JSON object. CV content: {cv}" ) def match_jobs(self, query, openai_key, k=5): print("HERE") query_d = self._get_ideal_job(query=query) if query_d is None: return (None, []) query_d = dict( {k: None for k in self.job_fields if k not in query_d}, **query_d ) query_result = self.embedder.embed_query(json.dumps(query_d)) query_result = np.array(query_result) distances, neighbors = self.index.search( query_result.reshape(1, -1).astype(np.float32), k ) scores = [distance for distance in distances[0]] # Normalize scores to be between 0 and 100 scores = [100 * (1 - score / max(scores)) for score in scores] return (scores, [self.strings[neighbor] for neighbor in neighbors[0]]) def _parse_json(self, response) -> dict | None: try: return json.loads( re.sub(r"(?<=\w)\n(?=\w)", "\\\\n", response.generations[0][0].text) ) except JSONDecodeError: eprint("Couldn't parse:", response.generations[0][0].text) return None def _get_ideal_job(self, query: str) -> dict | None: directory = os.path.join(os.getcwd(), self.cache_dir) if not os.path.exists(directory): os.makedirs(directory) query_hash = self.sha256(query.encode("utf-8")).hexdigest() file_path = os.path.join(directory, f"ideal_job_cv-{query_hash}" + ".json") if not os.path.exists(file_path): try: prompt = self.prompt.format_prompt(cv=query) ideal_job = self._parse_json( self.llm.generate(messages=[prompt.to_messages()]) ) if ideal_job is not None: with open(file_path, "w", encoding="utf-8") as f: json.dump(ideal_job, f) except Exception as err: print("got exception:", err) return None if os.path.exists(file_path): print("la") with open(file_path, "r", encoding="utf-8") as j: return json.load(j) return None
[ "PLACEHOLDER (PLACEHOLDER)", " Remember to use the information available in the CV, along with your general knowledge about the world, job markets, and companies, to make informed choices for each field.", " assuming they are seeking to switch jobs or secure a new position. The output should be a valid JSON object that could be parsed by json.loads().", "Analyze the following CV and transform the extracted information into an ideal job description for the candidate,", " Include: ", " If a field cannot be filled based on the information, set it to null. Please respond with just the JSON object. CV content: {cv}", ", " ]
2024-01-10
dblasko/cv-job-matcher
cv_parsing~ResumeParser.py
import pdftotext import openai import re import json class ResumeParser: def __init__(self, OPENAI_API_KEY): # set GPT-3 API key from the environment vairable openai.api_key = OPENAI_API_KEY # GPT-3 completion questions self.prompt_questions = """Summarize the text below into a JSON with exactly the following structure {basic_info: {location, portfolio_website_url, linkedin_url, github_main_page_url, university, education_level (BS, MS, or PhD), graduation_year, graduation_month, majors, GPA, languages (a list of languages), skills (a list of skills)}, project_experience:[{project_name, project_discription}], work_experience: [{experience_level, job_title, company, location, duration, job_summary}]} """ # Extract the content of a pdf file to string. def pdf2string(self: object, pdf) -> str: pdf = pdftotext.PDF(pdf) pdf_str = "\n\n".join(pdf) pdf_str = re.sub("\s[,.]", ",", pdf_str) pdf_str = re.sub("[\n]+", "\n", pdf_str) pdf_str = re.sub("[\s]+", " ", pdf_str) pdf_str = re.sub("http[s]?(://)?", "", pdf_str) return pdf_str # Base function for querying GPT-3. Send a request to GPT-3 with the passed-in function parameters and return the response object. def query_completion( self: object, prompt: str, engine: str, temperature: float = 0.0, max_tokens: int = 100, top_p: int = 1, frequency_penalty: int = 0, presence_penalty: int = 0, ) -> object: estimated_prompt_tokens = int(len(prompt.split()) * 1.6) estimated_answer_tokens = 2049 - estimated_prompt_tokens # FOR GPT3 # response = openai.ChatCompletion.create( # model=engine, # messages=[{"role": "user", "content": prompt}], # temperature=temperature, # max_tokens=min(4096 - estimated_prompt_tokens, max_tokens), # top_p=top_p, # frequency_penalty=frequency_penalty, # presence_penalty=presence_penalty, # ) # FOR CURIE response = openai.Completion.create( engine=engine, prompt=prompt, temperature=temperature, max_tokens=min(4096 - estimated_prompt_tokens, max_tokens), top_p=top_p, frequency_penalty=frequency_penalty, presence_penalty=presence_penalty, ) return response # Query GPT-3 for the work experience and / or basic information from the resume at the PDF file path. def query_resume(self: object, pdf_str) -> dict: resume = {} # pdf_str = self.pdf2string(pdf) # print(pdf_str) prompt = self.prompt_questions + "\n" + pdf_str max_tokens = 4090 - 864 engine = "text-davinci-002" # "gpt-3.5-turbo" response = self.query_completion(prompt, engine=engine, max_tokens=max_tokens) response_text = response["choices"][0]["text"].strip() # response_text = response["choices"][0]["message"][ # "content" # ].strip() # if we ant to use gpt 3.5-turbo # print(response_text) resume = json.loads(response_text) # print(resume) return resume
[ "8", "\n", "self.prompt_questions + \"\\n\" + pdf_str" ]
2024-01-10
dblasko/cv-job-matcher
job_description_embedding~CustomFakeLLM.py
from typing import Any, List from langchain.callbacks.manager import Callbacks from langchain.llms import FakeListLLM from langchain.schema import LLMResult, BaseMessage class CustomFakeLLM(FakeListLLM): def __init__(self, responses: list=[]) -> None: super().__init__(responses=responses) def generate(self, messages: List[List[BaseMessage]], stop: List[str] | None = None, callbacks: Callbacks = None, *, tags: List[str] | None = None, **kwargs: Any) -> LLMResult: prompts = [str(i) for m in messages for i in m] return super().generate(prompts, stop, callbacks, tags=tags, **kwargs)
[ "['P', 'L', 'A', 'C', 'E', 'H', 'O', 'L', 'D', 'E', 'R']" ]
2024-01-10
dblasko/cv-job-matcher
job_description_embedding~JobMatchingFineGrained.py
import os import json import xml.etree.ElementTree as ET import faiss import pickle import numpy as np import collections from langchain.embeddings import HuggingFaceEmbeddings import cv_parsing.ResumeParser as ResumeParser class JobMatchingFineGrained: def __init__(self, embeddings: HuggingFaceEmbeddings): self.embedder = HuggingFaceEmbeddings() self.indexes = None self.strings = None def parse_xml(self, xml_file): tree = ET.parse(xml_file) root = tree.getroot() jobs_list = [] for job in root.findall("job"): job_dict = { "title": job.find("title").text, "company": job.find("company").text, "posted_date": job.find("posted_date").text, "job_reference": job.find("job_reference").text, "req_number": job.find("req_number").text, "url": job.find("url").text, "body": job.find("body").text, "city": job.find("city").text, "state": job.find("state").text, "country": job.find("country").text, "location": job.find("location").text, "function": job.find("function").text, "logo": job.find("logo").text, "jobtype": job.find("jobtype").text, "education": job.find("education").text, "experience": job.find("experience").text, "salary": job.find("salary").text, "requiredlanguages": job.find("requiredlanguages").text, "requiredskills": job.find("requiredskills").text, } jobs_list.append(job_dict) return jobs_list def xml_to_json(self, xml_file, json_output_file): jobs_list = self.parse_xml(xml_file) json_output = json.dumps(jobs_list, indent=4) with open(json_output_file, "w") as json_file: json_file.write(json_output) def create_embeddings(self, json_file, save=True): with open(json_file, "r") as f: data = json.load(f) with open("job_description_embedding/job_openings_completed.json", "r") as f: strings = json.load(f) self.strings = strings new_data = [] for obj in data: # Useful fields: title, company, body, city, state, country, location, function, jobtype, education, experience, requiredlanguages, requiredskills new_obj = { "job": " ".join( [ i for i in [obj["jobtype"], obj["function"], obj["function"]] if i is not None ] ), "location": " ".join( [ i for i in [ obj["location"], obj["city"], obj["state"], obj["country"], ] if i is not None ] ), "company": obj["company"] if obj["company"] is not None else "", "body": obj["body"] if obj["body"] is not None else "", "education": " ".join( [str(obj["education"])] if not isinstance(obj["education"], list) else obj["education"] ) if obj["education"] is not None else "", "experience": " ".join( [str(obj["experience"])] if not isinstance(obj["experience"], list) else obj["experience"] ) if obj["experience"] is not None else "", "requiredLanguages": " ".join( [str(obj["requiredLanguages"])] if not isinstance(obj["requiredLanguages"], list) else obj["requiredLanguages"] ) if "requiredLanguages" in obj and obj["requiredLanguages"] is not None else "", "requiredSkills": " ".join( [str(obj["requiredskills"])] if not isinstance(obj["requiredSkills"], list) else obj["requiredSkills"] ) if "requiredSkills" in obj and obj["requiredSkills"] is not None else "", } new_data.append(new_obj) jobs = self.embedder.embed_documents([obj["job"] for obj in new_data]) locations = self.embedder.embed_documents([obj["location"] for obj in new_data]) companies = self.embedder.embed_documents([obj["company"] for obj in new_data]) bodies = self.embedder.embed_documents([obj["body"] for obj in new_data]) educations = self.embedder.embed_documents( [obj["education"] for obj in new_data] ) experiences = self.embedder.embed_documents( [obj["experience"] for obj in new_data] ) requiredlanguages = self.embedder.embed_documents( [obj["requiredLanguages"] for obj in new_data] ) requiredskills = self.embedder.embed_documents( [obj["requiredSkills"] for obj in new_data] ) # Save embeddings keys = list(new_data[0].keys()) if save: directory = os.path.join( os.getcwd(), "job_description_embedding/embeddings/fine_grained" ) if not os.path.exists(directory): os.makedirs(directory) for i, embeds in enumerate( [ jobs, locations, companies, bodies, educations, experiences, requiredlanguages, requiredskills, ] ): file_path = os.path.join(directory, keys[i] + ".pkl") # Save embeddings to binary file with open(file_path, "wb") as f: pickle.dump(embeds, f) # Create indexes print("Creating indexes...") indexes = {} for i, embeds in enumerate( [ jobs, locations, companies, bodies, educations, experiences, requiredlanguages, ] ): index = faiss.index_factory(len(embeds[0]), "Flat") index.train(np.array(embeds)) index.add(np.array(embeds)) indexes[keys[i]] = index self.indexes = indexes return indexes, strings def load_embeddings(self): indexes = {} for key in [ "job", "location", "company", "body", "education", "experience", "requiredLanguages", "requiredSkills", ]: directory = os.path.join( os.getcwd(), "job_description_embedding/embeddings/fine_grained" ) file_path = os.path.join(directory, key + ".pkl") with open(file_path, "rb") as f: embeddings = pickle.load(f) index = faiss.index_factory(len(embeddings[0]), "Flat") index.train(np.array(embeddings)) index.add(np.array(embeddings)) indexes[key] = index self.indexes = indexes with open("job_description_embedding/job_openings_completed.json", "r") as f: strings = json.load(f) self.strings = strings def match_jobs(self, query, openai_key, k=5): p = ResumeParser.ResumeParser(openai_key) cv_json = p.query_resume(query) # cv_json = self.__cv_to_json(query) cv_meta_json = { "job": " ".join( [ ("" if el["job_title"] is None else (el["job_title"] + " ")) + ("" if el["job_summary"] is None else el["job_summary"]) for el in cv_json["work_experience"] ] ), "location": "" if cv_json["basic_info"]["location"] is None else cv_json["basic_info"]["location"], "company": " ".join([el["company"] for el in cv_json["work_experience"]]), "body": query, "education": "" if cv_json["basic_info"]["university"] is None and cv_json["basic_info"]["education_level"] is None and cv_json["basic_info"]["majors"] is None else " ".join( [ cv_json["basic_info"]["university"], cv_json["basic_info"]["education_level"], " ".join(cv_json["basic_info"]["majors"]) if cv_json["basic_info"]["majors"] is not None else "", ] ), "experience": " ".join( [ ("" if el["job_title"] is None else (el["job_title"] + " ")) + ("" if el["job_summary"] is None else el["job_summary"]) for el in cv_json["work_experience"] ] ), "requiredLanguages": " ".join( "" if "languages" not in cv_json["basic_info"] or cv_json["basic_info"]["languages"] is None or len(cv_json["basic_info"]["languages"]) == 0 else cv_json["basic_info"]["languages"] ), "requiredSkills": "".join( [ "" if el["job_summary"] is None else el["job_summary"] for el in cv_json["work_experience"] ] if "skills" not in cv_json["basic_info"] or cv_json["basic_info"]["skills"] is None or len(cv_json["basic_info"]["skills"]) == 0 else " ".join(cv_json["basic_info"]["skills"]) ), } print(cv_meta_json) posting_scores = {} for key in cv_meta_json.keys(): index = self.indexes[key] query = np.array(self.embedder.embed_query(cv_meta_json[key])) dists, neighbors = index.search( query.reshape(1, -1).astype(np.float32), 1000 ) scores = [distance for distance in dists[0]] # Normalize scores to be between 0 and 100 if max(scores) == 0: scores = [0 for score in scores] else: scores = [ 100 * (1 - score / max(scores)) if score != 0 else 0 for score in scores ] for ind, neighbor_id in enumerate(neighbors[0]): if neighbor_id not in posting_scores: posting_scores[neighbor_id] = {} posting_scores[neighbor_id][key] = scores[ind] weighted_scores = {} for key, scores in posting_scores.items(): weighted_scores[key] = ( scores["job"] * 0.1 + scores["location"] * 0.05 + scores["company"] * 0.05 + scores["body"] * 0.1 # used to be 0.1 + scores["education"] * 0.2 + scores["experience"] * 0.2 + scores["requiredLanguages"] * 0.1 + scores["requiredSkills"] * 0.2 ) sorted_keys = sorted(weighted_scores, key=weighted_scores.get, reverse=True) sorted_scores = [weighted_scores[key] for key in sorted_keys] return ( sorted_scores[:k], [self.strings[neighbor] for neighbor in sorted_keys][:k], ) if __name__ == "__main__": engine = JobMatchingFineGrained(None) embeddings = engine.create_embeddings( "job_description_embedding/job_openings_completed.json" ) engine.load_embeddings()
[]
2024-01-10
dblasko/cv-job-matcher
job_description_embedding~JobMatchingBaseline.py
import os import json import xml.etree.ElementTree as ET import faiss import pickle import numpy as np from langchain.embeddings import HuggingFaceEmbeddings # from dotenv import load_dotenv class JobMatchingBaseline: def __init__(self, embeddings: HuggingFaceEmbeddings): self.embedder = HuggingFaceEmbeddings() # load_dotenv() # Load environment variables from .env file self.embeddings = None self.index = None self.strings = None def parse_xml(self, xml_file): tree = ET.parse(xml_file) root = tree.getroot() jobs_list = [] for job in root.findall("job"): job_dict = { "title": job.find("title").text, "company": job.find("company").text, "posted_date": job.find("posted_date").text, "job_reference": job.find("job_reference").text, "req_number": job.find("req_number").text, "url": job.find("url").text, "body": job.find("body").text, "city": job.find("city").text, "state": job.find("state").text, "country": job.find("country").text, "location": job.find("location").text, "function": job.find("function").text, "logo": job.find("logo").text, "jobtype": job.find("jobtype").text, "education": job.find("education").text, "experience": job.find("experience").text, "salary": job.find("salary").text, "requiredlanguages": job.find("requiredlanguages").text, "requiredskills": job.find("requiredskills").text, } jobs_list.append(job_dict) return jobs_list def xml_to_json(self, xml_file, json_output_file): jobs_list = self.parse_xml(xml_file) json_output = json.dumps(jobs_list, indent=4) with open(json_output_file, "w") as json_file: json_file.write(json_output) def create_embeddings(self, json_file): with open(json_file, "r") as f: data = json.load(f) strings = [] for obj in data: string = json.dumps(obj) strings.append(string) doc_result = self.embedder.embed_documents(strings) self.embeddings = doc_result index = faiss.index_factory(len(doc_result[0]), "Flat") index.train(doc_result) index.add(doc_result) self.index = index return index, strings def create_embedding_index(self): index = faiss.index_factory(len(self.embeddings[0]), "Flat") index.train(self.embeddings) index.add(self.embeddings) self.index = index def match_jobs(self, query, openai_key, k=5): query_result = self.embedder.embed_query(query) query_result = np.array(query_result) distances, neighbors = self.index.search( query_result.reshape(1, -1).astype(np.float32), k ) scores = [distance for distance in distances[0]] # Normalize scores to be between 0 and 100 scores = [100 * (1 - score / max(scores)) for score in scores] return (scores, [self.strings[neighbor] for neighbor in neighbors[0]]) def save_embeddings( self, saving_embeddings_file_name: str, saving_embeddings_directory: str, ) -> None: directory = os.path.join(os.getcwd(), saving_embeddings_directory) print(directory) if not os.path.exists(directory): os.makedirs(directory) file_path = os.path.join(directory, saving_embeddings_file_name + ".pkl") # Save embeddings to binary file with open(file_path, "wb") as f: pickle.dump(self.embeddings, f) def load_embeddings(self, embeddings_path) -> HuggingFaceEmbeddings: print("CALLED") with open(embeddings_path, "rb") as f: embeddings: HuggingFaceEmbeddings = pickle.load(f) print(type(embeddings)) self.embeddings = embeddings with open("job_description_embedding/job_openings.json", "r") as f: strings = json.load(f) self.strings = strings if __name__ == "__main__": engine = JobMatchingBaseline(None) engine.create_embeddings("job_description_embedding/job_openings.json") engine.save_embeddings( "saved_embeddings", "job_description_embedding/embeddings", )
[]
2024-01-10
lucianoscarpaci/vim-code-assistant
turbo.py
import sys from dotenv import load_dotenv import openai import os import emoji load_dotenv() openai.api_key = os.getenv('OPENAI_KEY') # read the contents of the buffer from standard input buffer = sys.stdin.read() def assistant(buffer): # program needs to have prevention from a timeout to openAI retry_count = 0 max_retries = 9999 while retry_count <= max_retries: try: # call openai api response = openai.ChatCompletion.create( # model type model="gpt-3.5-turbo-16k", messages=[ {"role": "user", "content": buffer}, ], temperature=1, max_tokens=15999, ) response_dict = response.get("choices") if response_dict and len(response_dict) > 0: prompt_response = response_dict[0]["message"]["content"] return prompt_response except openai.error.InvalidRequestError as e: print(f"API request [InvalidRequestError] failed with error: {e}") smiley = emoji.emojize(":smiling_face_with_smiling_eyes:") return assistant(prompt=smiley) except Exception: retry_count += 1 output = assistant(buffer) print('\n\n## Question\n\n') print(f"{buffer}") print('\n\n## Answer\n\n') print(f"{output}")
[ "content" ]
2024-01-10
Stanlay19/langchain-ChatGLM
chains~local_doc_qa.py
from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.vectorstores import FAISS from langchain.document_loaders import UnstructuredFileLoader from configs.model_config import * import datetime from textsplitter import ChineseTextSplitter from typing import List, Tuple from langchain.docstore.document import Document import numpy as np from utils import torch_gc from tqdm import tqdm from pypinyin import lazy_pinyin DEVICE_ = EMBEDDING_DEVICE DEVICE_ID = "0" if torch.cuda.is_available() else None DEVICE = f"{DEVICE_}:{DEVICE_ID}" if DEVICE_ID else DEVICE_ def load_file(filepath, sentence_size=SENTENCE_SIZE): if filepath.lower().endswith(".md"): loader = UnstructuredFileLoader(filepath, mode="elements") docs = loader.load() elif filepath.lower().endswith(".pdf"): loader = UnstructuredFileLoader(filepath, strategy="fast") textsplitter = ChineseTextSplitter(pdf=True, sentence_size=sentence_size) docs = loader.load_and_split(textsplitter) else: loader = UnstructuredFileLoader(filepath, mode="elements") textsplitter = ChineseTextSplitter(pdf=False, sentence_size=sentence_size) docs = loader.load_and_split(text_splitter=textsplitter) return docs def generate_prompt(related_docs: List[str], query: str, prompt_template=PROMPT_TEMPLATE) -> str: context = "\n".join([doc.page_content for doc in related_docs]) prompt = prompt_template.replace("{question}", query).replace("{context}", context) return prompt def seperate_list(ls: List[int]) -> List[List[int]]: lists = [] ls1 = [ls[0]] for i in range(1, len(ls)): if ls[i - 1] + 1 == ls[i]: ls1.append(ls[i]) else: lists.append(ls1) ls1 = [ls[i]] lists.append(ls1) return lists def similarity_search_with_score_by_vector( self, embedding: List[float], k: int = 4 ) -> List[Tuple[Document, float]]: scores, indices = self.index.search(np.array([embedding], dtype=np.float32), k) docs = [] id_set = set() store_len = len(self.index_to_docstore_id) for j, i in enumerate(indices[0]): if i == -1 or 0 < self.score_threshold < scores[0][j]: # This happens when not enough docs are returned. continue _id = self.index_to_docstore_id[i] doc = self.docstore.search(_id) if not self.chunk_conent: if not isinstance(doc, Document): raise ValueError(f"Could not find document for id {_id}, got {doc}") doc.metadata["score"] = int(scores[0][j]) docs.append(doc) continue id_set.add(i) docs_len = len(doc.page_content) for k in range(1, max(i, store_len - i)): break_flag = False for l in [i + k, i - k]: if 0 <= l < len(self.index_to_docstore_id): _id0 = self.index_to_docstore_id[l] doc0 = self.docstore.search(_id0) if docs_len + len(doc0.page_content) > self.chunk_size: break_flag = True break elif doc0.metadata["source"] == doc.metadata["source"]: docs_len += len(doc0.page_content) id_set.add(l) if break_flag: break if not self.chunk_conent: return docs if len(id_set) == 0 and self.score_threshold > 0: return [] id_list = sorted(list(id_set)) id_lists = seperate_list(id_list) for id_seq in id_lists: for id in id_seq: if id == id_seq[0]: _id = self.index_to_docstore_id[id] doc = self.docstore.search(_id) else: _id0 = self.index_to_docstore_id[id] doc0 = self.docstore.search(_id0) doc.page_content += doc0.page_content if not isinstance(doc, Document): raise ValueError(f"Could not find document for id {_id}, got {doc}") doc_score = min([scores[0][id] for id in [indices[0].tolist().index(i) for i in id_seq if i in indices[0]]]) doc.metadata["score"] = int(doc_score) docs.append(doc) torch_gc() return docs class LocalDocQA: llm: object = None embeddings: object = None top_k: int = VECTOR_SEARCH_TOP_K chunk_size: int = CHUNK_SIZE chunk_conent: bool = True score_threshold: int = VECTOR_SEARCH_SCORE_THRESHOLD def init_cfg(self, embedding_model: str = EMBEDDING_MODEL, embedding_device=EMBEDDING_DEVICE, llm_history_len: int = LLM_HISTORY_LEN, llm_model: str = LLM_MODEL, llm_device=LLM_DEVICE, top_k=VECTOR_SEARCH_TOP_K, use_ptuning_v2: bool = USE_PTUNING_V2, use_lora: bool = USE_LORA, ): if llm_model.startswith('moss'): from models.moss_llm import MOSS self.llm = MOSS() else: from models.chatglm_llm import ChatGLM self.llm = ChatGLM() self.llm.load_model(model_name_or_path=llm_model_dict[llm_model], llm_device=llm_device, use_ptuning_v2=use_ptuning_v2, use_lora=use_lora) self.llm.history_len = llm_history_len self.embeddings = HuggingFaceEmbeddings(model_name=embedding_model_dict[embedding_model], model_kwargs={'device': embedding_device}) self.top_k = top_k def init_knowledge_vector_store(self, filepath: str or List[str], vs_path: str or os.PathLike = None, sentence_size=SENTENCE_SIZE): loaded_files = [] failed_files = [] if isinstance(filepath, str): if not os.path.exists(filepath): print("路径不存在") return None elif os.path.isfile(filepath): file = os.path.split(filepath)[-1] try: docs = load_file(filepath, sentence_size) logger.info(f"{file} 已成功加载") loaded_files.append(filepath) except Exception as e: logger.error(e) logger.info(f"{file} 未能成功加载") return None elif os.path.isdir(filepath): docs = [] for file in tqdm(os.listdir(filepath), desc="加载文件"): fullfilepath = os.path.join(filepath, file) try: docs += load_file(fullfilepath, sentence_size) loaded_files.append(fullfilepath) except Exception as e: logger.error(e) failed_files.append(file) if len(failed_files) > 0: logger.info("以下文件未能成功加载:") for file in failed_files: logger.info(f"{file}\n") else: docs = [] for file in filepath: try: docs += load_file(file) logger.info(f"{file} 已成功加载") loaded_files.append(file) except Exception as e: logger.error(e) logger.info(f"{file} 未能成功加载") if len(docs) > 0: logger.info("文件加载完毕,正在生成向量库") if vs_path and os.path.isdir(vs_path): vector_store = FAISS.load_local(vs_path, self.embeddings) vector_store.add_documents(docs) torch_gc() else: if not vs_path: vs_path = os.path.join(VS_ROOT_PATH, f"""{"".join(lazy_pinyin(os.path.splitext(file)[0]))}_FAISS_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}""") vector_store = FAISS.from_documents(docs, self.embeddings) # docs 为Document列表 torch_gc() vector_store.save_local(vs_path) return vs_path, loaded_files else: logger.info("文件均未成功加载,请检查依赖包或替换为其他文件再次上传。") return None, loaded_files def one_knowledge_add(self, vs_path, one_title, one_conent, one_content_segmentation, sentence_size): try: if not vs_path or not one_title or not one_conent: logger.info("知识库添加错误,请确认知识库名字、标题、内容是否正确!") return None, [one_title] docs = [Document(page_content=one_conent+"\n", metadata={"source": one_title})] if not one_content_segmentation: text_splitter = ChineseTextSplitter(pdf=False, sentence_size=sentence_size) docs = text_splitter.split_documents(docs) if os.path.isdir(vs_path): vector_store = FAISS.load_local(vs_path, self.embeddings) vector_store.add_documents(docs) else: vector_store = FAISS.from_documents(docs, self.embeddings) ##docs 为Document列表 torch_gc() vector_store.save_local(vs_path) return vs_path, [one_title] except Exception as e: logger.error(e) return None, [one_title] def get_knowledge_based_answer(self, query, vs_path, chat_history=[], streaming: bool = STREAMING): vector_store = FAISS.load_local(vs_path, self.embeddings) FAISS.similarity_search_with_score_by_vector = similarity_search_with_score_by_vector vector_store.chunk_size = self.chunk_size vector_store.chunk_conent = self.chunk_conent vector_store.score_threshold = self.score_threshold related_docs_with_score = vector_store.similarity_search_with_score(query, k=self.top_k) torch_gc() prompt = generate_prompt(related_docs_with_score, query) for result, history in self.llm._call(prompt=prompt, history=chat_history, streaming=streaming): torch_gc() history[-1][0] = query response = {"query": query, "result": result, "source_documents": related_docs_with_score} yield response, history torch_gc() # query 查询内容 # vs_path 知识库路径 # chunk_conent 是否启用上下文关联 # score_threshold 搜索匹配score阈值 # vector_search_top_k 搜索知识库内容条数,默认搜索5条结果 # chunk_sizes 匹配单段内容的连接上下文长度 def get_knowledge_based_conent_test(self, query, vs_path, chunk_conent, score_threshold=VECTOR_SEARCH_SCORE_THRESHOLD, vector_search_top_k=VECTOR_SEARCH_TOP_K, chunk_size=CHUNK_SIZE): vector_store = FAISS.load_local(vs_path, self.embeddings) FAISS.similarity_search_with_score_by_vector = similarity_search_with_score_by_vector vector_store.chunk_conent = chunk_conent vector_store.score_threshold = score_threshold vector_store.chunk_size = chunk_size related_docs_with_score = vector_store.similarity_search_with_score(query, k=vector_search_top_k) if not related_docs_with_score: response = {"query": query, "source_documents": []} return response, "" torch_gc() prompt = "\n".join([doc.page_content for doc in related_docs_with_score]) response = {"query": query, "source_documents": related_docs_with_score} return response, prompt if __name__ == "__main__": local_doc_qa = LocalDocQA() local_doc_qa.init_cfg() query = "本项目使用的embedding模型是什么,消耗多少显存" vs_path = "/Users/liuqian/Downloads/glm-dev/vector_store/aaa" last_print_len = 0 for resp, history in local_doc_qa.get_knowledge_based_answer(query=query, vs_path=vs_path, chat_history=[], streaming=True): logger.info(resp["result"][last_print_len:], end="", flush=True) last_print_len = len(resp["result"]) source_text = [f"""出处 [{inum + 1}] {os.path.split(doc.metadata['source'])[-1]}:\n\n{doc.page_content}\n\n""" # f"""相关度:{doc.metadata['score']}\n\n""" for inum, doc in enumerate(resp["source_documents"])] logger.info("\n\n" + "\n\n".join(source_text)) pass
[ "{context}", "{question}", "\n" ]
2024-01-10
Fwuffie/GPT3IsAGoodSoftwareEngineer
answeringEngine.py
import re, json import openai from logger import log as logger from logger import ansicodes class answeringEngine: def __init__(self, openAiKey, user): openai.api_key = openAiKey self.presetQuestionResponses = self.loadPresetResponses(user) return def loadPresetResponses(self, user): return { 'usersName': { 'keyWords': ['your name', 'preferred name'], 'response': { "string": user.fullName } }, 'usersPronouns': { 'keyWords': ['pronoun'], 'response': { "string": user.pronouns } }, 'usersGit': { 'keyWords': ['git', 'bitbucket'], 'response': { "string": user.gitURL } }, 'usersLinkedIn': { 'keyWords': ['linkedin'], 'response': { "string": user.linkedIn } }, 'usersWebsite': { 'keyWords': ['website', 'portfolio'], 'response': { "string": user.website } }, 'rightToWork': { 'keyWords': ['visa', 'right to work'], 'response': { "bool": user.canWork } }, 'salary': { 'keyWords': ['salary'], 'response': { "string": str(user.salary) + " " + user.salaryCurrency, "int": int(user.salary) } }, 'location': { 'keyWords': ['country', 'locat', 'based', 'live'], 'response': { "string": user.location } }, 'notice': { 'keyWords': ['notice'], 'response': { "string": "I can start work immediately.", "int": 0 } }, 'criminal': { 'keyWords': ['criminal'], 'response': { "string": "No, I have never been convicted of a criminal offence.", "bool": False } } } def askGPT(self, primedString, tokens=256, temp=1.1): print("Generating GPT Response...") response = openai.Completion.create( model="text-davinci-002", prompt=primedString, max_tokens=tokens, temperature=temp ) return response['choices'][0]['text'].lstrip('\n') # Question Types; # String* # Int* # Bool* # Multiple Choice # Date def answerQuestion(self, question, qresponsetype, questionBackground = "", forceGPT = False, choices = None): # Classify Question to see if it has preset answers questionType = self.classifyQuestion(question) # Check responseType Set if qresponsetype == None: return None # Check GPT is enabled, it has a canned response, and the canned response is the correct type if (not forceGPT) and questionType and (qresponsetype.lower() in self.presetQuestionResponses[questionType]['response']): # Use the existing canned response, or call the canned response function questionResponse = self.presetQuestionResponses[questionType]['response'][qresponsetype] if callable(questionResponse): questionResponse = questionResponse(question, qresponsetype) # Otherwise Get GPT to respond else: primedQuestion = self.primeQuestionForGPT(question, qresponsetype, choices) try: questionResponse = self.askGPT( questionBackground + primedQuestion ) logger.debug("[RawResponse]: %s" % questionResponse) #Return the question response in the correct format. questionResponse = self.castResponse( questionResponse, qresponsetype, choices ) #Todo: try and catch bad responses except: print(ansicodes.GREEN + "GPT3 was unable to respond" + ansicodes.RST) questionResponse = None return questionResponse; def classifyQuestion(self, question): # Future Todo: add weighting to keyWords: e.g # Why do you want to work at github? - Should not match, "why" would have a negative weight. # Please describe a recent project you've worked on, feel free to link a git repo. - Should match projects, "Project" would be weighed higher than "git" and "git" would be a keyword in projects threshold = 0.1 pQuestionTypes = [] for presetQuestion in self.presetQuestionResponses: keywordMatches = 0 for word in self.presetQuestionResponses[presetQuestion]["keyWords"]: if word.lower() in question.lower(): keywordMatches += 1 / len(self.presetQuestionResponses[presetQuestion]["keyWords"]) pQuestionTypes.append( (presetQuestion, keywordMatches) ) bestMatch = max( pQuestionTypes, key = lambda k: k[1] ) return bestMatch[0] if bestMatch[1] > threshold else None # Primes all def primeQuestionForGPT(self, question, qtype, choices = None): qtype = qtype.lower() if qtype == "multiple choice": choices = "\n".join(map(lambda x: "%d. %s" % (x[0]+1, x[1]), enumerate(choices))) print(choices) return "\nQ: %s (Type the number for the correct response)\n%s\n" % (question, choices) elif qtype == "bool": return "\nQ: %s (Y/N)\nA:" % (question) elif qtype == "int": return "\nQ: %s (number)\nA:" % (question) else: return "\nQ: %s\nA: " % (question) def castResponse(self, questionResponse, qresponsetype, choices = None): qresponsetype = qresponsetype.lower(); if qresponsetype == "string": #Respond With The Whole GPT Response return questionResponse if qresponsetype == "int": #Respond With The First Number GPT Provides, Or None m = re.search(r'\d+', questionResponse) return int(m.group(0)) if m else None if qresponsetype == "bool": #Check for n or no in the Response questionResponse = questionResponse.lower() n = re.search(r'(^|[^\w])no?([^\w]|$)', questionResponse) y = re.search(r'(^|[^\w])y(es)?([^\w]|$)', questionResponse) return bool(y) if ( y or n ) else None if qresponsetype == "multiple choice": #Get the Number from the response m = re.search(r'\d+', questionResponse) return int(m.group(0)) if int(m.group(0)) <= len(choices) else None class user: countryAliases: { "UK": ["UK", "GB", "Scotland", "England", "United Kingdom", "Britian"], "US": ["US" "USA", "United States", "America", "The United States of America"] } def __init__(self, userdata): self.fullName = userdata["fullName"] self.email = userdata["email"] self.pronouns = userdata["pronouns"] self.gitURL = userdata["gitURL"] self.linkedIn = userdata["linkedIn"] self.website = userdata["website"] self.telephone = userdata["telephone"] self.salary = userdata["salary"] self.salaryCurrency = userdata["salaryCurrency"] self.countries = userdata["rightToWorkCountries"] self.location = userdata["location"] return def canWork(self, question, qresponsetype): canWorkInCountry = False for country in self.countries: for alias in country: # May Need Better Matching if alias in question: canWorkInCountry = True if qresponsetype == "bool": return canWorkInCountry elif qresponsetype == "string": return "Yes, I can work without a visa." if canWorkInCountry else "No, I would require visa sponsorship." raise Exception("Question Response Type Is Invalid For This Question")
[]
2024-01-10
marcocaccin/tensorflow-onnx
tests~keras2onnx_applications~nightly_build~test_transformers.py
# SPDX-License-Identifier: Apache-2.0 import os import sys import unittest import mock_keras2onnx import json import urllib.request import pickle from os.path import dirname, abspath from mock_keras2onnx.proto import keras import numpy as np import tensorflow as tf from onnxconverter_common.onnx_ex import get_maximum_opset_supported sys.path.insert(0, os.path.join(dirname(abspath(__file__)), '../../keras2onnx_tests/')) from test_utils import run_onnx_runtime from mock_keras2onnx.proto import is_tensorflow_older_than enable_full_transformer_test = False if os.environ.get('ENABLE_FULL_TRANSFORMER_TEST', '0') != '0': enable_transformer_test = True @unittest.skipIf(is_tensorflow_older_than('2.1.0'), "Transformers conversion need tensorflow 2.1.0+") class TestTransformers(unittest.TestCase): text_str = 'The quick brown fox jumps over lazy dog.' def setUp(self): self.model_files = [] def tearDown(self): for fl in self.model_files: os.remove(fl) def _get_token_path(self, file_name): return 'https://lotus.blob.core.windows.net/converter-models/transformer_tokenizer/' + file_name def _get_tokenzier(self, tokenizer_file): token_path = self._get_token_path(tokenizer_file) if not os.path.exists(tokenizer_file): urllib.request.urlretrieve(token_path, tokenizer_file) with open(tokenizer_file, 'rb') as handle: tokenizer = pickle.load(handle) return tokenizer def _prepare_inputs(self, tokenizer, batch_size=3): raw_data = json.dumps({ 'text': self.text_str }) text = json.loads(raw_data)['text'] # The tokenizers are generated using transformers 2.5.0, but model_max_length is introduced and needed in 2.9.0. if not hasattr(tokenizer, 'model_max_length'): tokenizer.model_max_length = 1024 inputs_raw = tokenizer.encode_plus(text, add_special_tokens=True) idx_not_None = [i_ for i_, v_ in enumerate(inputs_raw.data['input_ids']) if v_ is not None] input_raw_not_None = inputs_raw if len(idx_not_None) == len(inputs_raw.data['input_ids']) else \ {k_: [v_[i_] for i_ in idx_not_None] for k_, v_ in inputs_raw.items()} inputs_onnx = {k_: np.repeat(np.expand_dims(v_, axis=0), batch_size, axis=0) for k_, v_ in input_raw_not_None.items()} inputs = {k_: tf.constant(v_) for k_, v_ in inputs_onnx.items()} return text, inputs, inputs_onnx @unittest.skip("Output shape mismatch for tf model prediction.") def test_3layer_gpt2(self): from transformers import GPT2Config, TFGPT2Model, BertTokenizer mock_keras2onnx.proto.keras.backend.set_learning_phase(0) config = GPT2Config(n_layer=3) model = TFGPT2Model(config) tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) inputs = tokenizer.encode_plus(text, add_special_tokens=True, return_tensors='tf') predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFBertModel(self): from transformers import BertConfig, TFBertModel keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFBertForPreTraining(self): from transformers import BertConfig, TFBertForPreTraining keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForPreTraining(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) def test_TFBertForMaskedLM(self): from transformers import BertConfig, TFBertForMaskedLM keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForMaskedLM(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFBertForNextSentencePrediction(self): from transformers import BertConfig, TFBertForNextSentencePrediction keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForNextSentencePrediction(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFBertForSequenceClassification(self): from transformers import BertConfig, TFBertForSequenceClassification keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForSequenceClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFBertForTokenClassification(self): from transformers import BertConfig, TFBertForTokenClassification keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForTokenClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFBertForQuestionAnswering(self): from transformers import BertConfig, TFBertForQuestionAnswering keras.backend.clear_session() # pretrained_weights = 'bert-base-uncased' tokenizer_file = 'bert_bert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = BertConfig() model = TFBertForQuestionAnswering(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFGPT2(self): if enable_full_transformer_test: from transformers import GPT2Config, TFGPT2Model, TFGPT2LMHeadModel, TFGPT2DoubleHeadsModel model_list = [TFGPT2Model, TFGPT2LMHeadModel, TFGPT2DoubleHeadsModel] else: from transformers import GPT2Config, TFGPT2Model model_list = [TFGPT2Model] # pretrained_weights = 'gpt2' tokenizer_file = 'gpt2_gpt2.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = GPT2Config() for model_instance_ in model_list: keras.backend.clear_session() model = model_instance_(config) model._set_inputs(inputs) predictions_original = model(inputs) predictions = [predictions_original[0]] + list(v_.numpy() for v_ in predictions_original[1]) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skipIf(get_maximum_opset_supported() < 12, "Einsum is not supported until opset 12.") def test_TFXLNet(self): if enable_full_transformer_test: from transformers import XLNetConfig, TFXLNetModel, TFXLNetLMHeadModel, TFXLNetForSequenceClassification, \ TFXLNetForTokenClassification, TFXLNetForQuestionAnsweringSimple, XLNetTokenizer model_list = [TFXLNetModel, TFXLNetLMHeadModel, TFXLNetForSequenceClassification, \ TFXLNetForTokenClassification, TFXLNetForQuestionAnsweringSimple] else: from transformers import XLNetConfig, TFXLNetModel, XLNetTokenizer model_list = [TFXLNetModel] # XLNetTokenizer need SentencePiece, so the pickle file does not work here. tokenizer = XLNetTokenizer.from_pretrained('xlnet-large-cased') config = XLNetConfig(n_layer=2) # The model with input mask has MatrixDiagV3 which is not a registered function/op token = np.asarray(tokenizer.encode(self.text_str, add_special_tokens=True), dtype=np.int32) inputs_onnx = {'input_1': np.expand_dims(token, axis=0)} inputs = tf.constant(token)[None, :] # Batch size 1 for model_instance_ in model_list: keras.backend.clear_session() model = model_instance_(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFOpenAIGPTModel(self): from transformers import OpenAIGPTConfig, TFOpenAIGPTModel keras.backend.clear_session() # pretrained_weights = 'openai-gpt' tokenizer_file = 'openai_openai-gpt.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = OpenAIGPTConfig() model = TFOpenAIGPTModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFOpenAIGPTLMHeadModel(self): from transformers import OpenAIGPTConfig, TFOpenAIGPTLMHeadModel keras.backend.clear_session() # pretrained_weights = 'openai-gpt' tokenizer_file = 'openai_openai-gpt.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = OpenAIGPTConfig() model = TFOpenAIGPTLMHeadModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) def test_TFOpenAIGPTDoubleHeadsModel(self): from transformers import OpenAIGPTConfig, TFOpenAIGPTDoubleHeadsModel keras.backend.clear_session() # pretrained_weights = 'openai-gpt' tokenizer_file = 'openai_openai-gpt.pickle' tokenizer = self._get_tokenzier(tokenizer_file) # tf.gather(hidden_states, cls_index, batch_dims=len(hidden_shape) - 2), batch_dims = 1 in this case text, inputs, inputs_onnx = self._prepare_inputs(tokenizer, batch_size=1) config = OpenAIGPTConfig() model = TFOpenAIGPTDoubleHeadsModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skip('tensorflow.GraphDef exceeds maximum protobuf size of 2GB') def test_TFXLMModel(self): from transformers import XLMConfig, TFXLMModel keras.backend.clear_session() # pretrained_weights = 'xlm-mlm-enfr-1024' tokenizer_file = 'xlm_xlm-mlm-enfr-1024.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = XLMConfig() model = TFXLMModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skip('tensorflow.GraphDef exceeds maximum protobuf size of 2GB') def test_TFXLMWithLMHeadModel(self): from transformers import XLMConfig, TFXLMWithLMHeadModel keras.backend.clear_session() # pretrained_weights = 'xlm-mlm-enfr-1024' tokenizer_file = 'xlm_xlm-mlm-enfr-1024.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = XLMConfig() model = TFXLMWithLMHeadModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skip('tensorflow.GraphDef exceeds maximum protobuf size of 2GB') def test_TFXLMForSequenceClassification(self): from transformers import XLMConfig, TFXLMForSequenceClassification keras.backend.clear_session() # pretrained_weights = 'xlm-mlm-enfr-1024' tokenizer_file = 'xlm_xlm-mlm-enfr-1024.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = XLMConfig() model = TFXLMForSequenceClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) @unittest.skip('tensorflow.GraphDef exceeds maximum protobuf size of 2GB') def test_TFXLMForQuestionAnsweringSimple(self): from transformers import XLMConfig, TFXLMForQuestionAnsweringSimple keras.backend.clear_session() # pretrained_weights = 'xlm-mlm-enfr-1024' tokenizer_file = 'xlm_xlm-mlm-enfr-1024.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = XLMConfig() model = TFXLMForQuestionAnsweringSimple(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFDistilBertModel(self): from transformers import DistilBertConfig, TFDistilBertModel keras.backend.clear_session() # pretrained_weights = 'distilbert-base-uncased' tokenizer_file = 'distilbert_distilbert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = DistilBertConfig() model = TFDistilBertModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFDistilBertForMaskedLM(self): from transformers import DistilBertConfig, TFDistilBertForMaskedLM keras.backend.clear_session() # pretrained_weights = 'distilbert-base-uncased' tokenizer_file = 'distilbert_distilbert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = DistilBertConfig() model = TFDistilBertForMaskedLM(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFDistilBertForSequenceClassification(self): from transformers import DistilBertConfig, TFDistilBertForSequenceClassification keras.backend.clear_session() # pretrained_weights = 'distilbert-base-uncased' tokenizer_file = 'distilbert_distilbert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = DistilBertConfig() model = TFDistilBertForSequenceClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFDistilBertForTokenClassification(self): from transformers import DistilBertConfig, TFDistilBertForTokenClassification keras.backend.clear_session() # pretrained_weights = 'distilbert-base-uncased' tokenizer_file = 'distilbert_distilbert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = DistilBertConfig() model = TFDistilBertForTokenClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFDistilBertForQuestionAnswering(self): from transformers import DistilBertConfig, TFDistilBertForQuestionAnswering keras.backend.clear_session() # pretrained_weights = 'distilbert-base-uncased' tokenizer_file = 'distilbert_distilbert-base-uncased.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = DistilBertConfig() model = TFDistilBertForQuestionAnswering(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFRobertaModel(self): from transformers import RobertaConfig, TFRobertaModel keras.backend.clear_session() # pretrained_weights = 'roberta-base' tokenizer_file = 'roberta_roberta-base.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = RobertaConfig() model = TFRobertaModel(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) def test_TFRobertaForMaskedLM(self): from transformers import RobertaConfig, TFRobertaForMaskedLM keras.backend.clear_session() # pretrained_weights = 'roberta-base' tokenizer_file = 'roberta_roberta-base.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = RobertaConfig() model = TFRobertaForMaskedLM(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue( run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2, atol=1.e-4)) def test_TFRobertaForSequenceClassification(self): from transformers import RobertaConfig, TFRobertaForSequenceClassification keras.backend.clear_session() # pretrained_weights = 'roberta-base' tokenizer_file = 'roberta_roberta-base.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = RobertaConfig() model = TFRobertaForSequenceClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) @unittest.skipIf(not enable_full_transformer_test, "Full transfomer test is not enabled") def test_TFRobertaForTokenClassification(self): from transformers import RobertaConfig, TFRobertaForTokenClassification keras.backend.clear_session() # pretrained_weights = 'roberta-base' tokenizer_file = 'roberta_roberta-base.pickle' tokenizer = self._get_tokenzier(tokenizer_file) text, inputs, inputs_onnx = self._prepare_inputs(tokenizer) config = RobertaConfig() model = TFRobertaForTokenClassification(config) predictions = model.predict(inputs) onnx_model = mock_keras2onnx.convert_keras(model, model.name) self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files)) if __name__ == "__main__": unittest.main()
[]
2024-01-10
materials-data-facility/connect_server
deprecated~converters~cxidb_converter.py
import json import sys import os from tqdm import tqdm from mdf_refinery.validator import Validator from mdf_forge.toolbox import find_files # VERSION 0.3.0 # This is the converter for CXIDB. # Arguments: # input_path (string): The file or directory where the data resides. # NOTE: Do not hard-code the path to the data in the converter. The converter should be portable. # metadata (string or dict): The path to the JSON dataset metadata file, a dict or json.dumps string containing the dataset metadata, or None to specify the metadata here. Default None. # verbose (bool): Should the script print status messages to standard output? Default False. # NOTE: The converter should have NO output if verbose is False, unless there is an error. def convert(input_path, metadata=None, verbose=False): if verbose: print("Begin converting") # Collect the metadata if not metadata: dataset_metadata = { "mdf": { "title": "The Coherent X-ray Imaging Data Bank", "acl": ["public"], "source_name": "cxidb", "citation": ["Maia, F. R. N. C. The Coherent X-ray Imaging Data Bank. Nat. Methods 9, 854–855 (2012)."], "data_contact": { "given_name": "Filipe", "family_name": "Maia", "email": "[email protected]", "institution": "Lawrence Berkeley National Laboratory", # IDs }, "author": { "given_name": "Filipe", "family_name": "Maia", "institution": "Lawrence Berkeley National Laboratory", # IDs }, # "license": , "collection": "CXIDB", "tags": ["x-ray", "coherent"], "description": "A new database which offers scientists from all over the world a unique opportunity to access data from Coherent X-ray Imaging (CXI) experiments.", "year": 2012, "links": { "landing_page": "http://www.cxidb.org/", # "publication": , # "dataset_doi": , # "related_id": , # data links: { #"globus_endpoint": , #"http_host": , #"path": , #} }, # "mrr": , "data_contributor": { "given_name": "Jonathon", "family_name": "Gaff", "email": "[email protected]", "institution": "The University of Chicago", "github": "jgaff" } } } elif type(metadata) is str: try: dataset_metadata = json.loads(metadata) except Exception: try: with open(metadata, 'r') as metadata_file: dataset_metadata = json.load(metadata_file) except Exception as e: sys.exit("Error: Unable to read metadata: " + repr(e)) elif type(metadata) is dict: dataset_metadata = metadata else: sys.exit("Error: Invalid metadata parameter") dataset_validator = Validator(dataset_metadata) # Get the data for dir_data in tqdm(find_files(input_path, file_pattern="json", verbose=verbose), desc="Processing metadata", disable= not verbose): with open(os.path.join(dir_data["path"], dir_data["filename"])) as file_data: cxidb_data = json.load(file_data) record_metadata = { "mdf": { "title": cxidb_data["citation_title"], "acl": ["public"], # "tags": , # "description": , # "composition": , "raw": json.dumps(cxidb_data), "links": { "landing_page": cxidb_data["url"], "publication": [cxidb_data.get("citation_DOI", None), cxidb_data.get("entry_DOI", None)], # "dataset_doi": , # "related_id": , # data links: { #"globus_endpoint": , #"http_host": , #"path": , #}, }, # "citation": , # "data_contact": { # "given_name": , # "family_name": , # "email": , # "institution":, # IDs # }, # "author": , # "license": , # "collection": , # "data_format": , # "data_type": , # "year": , # "mrr": # "processing": , # "structure":, } } # Pass each individual record to the Validator result = dataset_validator.write_record(record_metadata) # Check if the Validator accepted the record, and print a message if it didn't # If the Validator returns "success" == True, the record was written successfully if result["success"] is not True: print("Error:", result["message"]) if verbose: print("Finished converting")
[]
2024-01-10
ilijavishinov/aws-doc-chat
utils_dir~documentation_embedder.py
from langchain.embeddings import OpenAIEmbeddings from langchain.embeddings import HuggingFaceEmbeddings from transformers import AutoTokenizer from langchain.embeddings import LlamaCppEmbeddings, GPT4AllEmbeddings import os class DocumentationEmbedder(object): docs_dir: str = None db = None embedding_tokenizer = None embedding_model = None def __init__(self, embedding_model_name: str = 'distilbert', device: str = 'cpu'): self.embedding_model_name = embedding_model_name self.device = device self.get_embeddings_object() def get_embeddings_object(self): """ Chooses the embedding model depending on the provided name """ if self.embedding_model_name.startswith('openai'): self.embedding_model = OpenAIEmbeddings( model = 'text-embedding-ada-002' ) elif self.embedding_model_name == 'llamacpp': self.embedding_model = LlamaCppEmbeddings( model_path = r'C:\Users\ilija\llama.cpp\models\7B\ggml-model-q4_0.gguf', verbose = True, n_ctx = 1024, n_gpu_layers = 40, n_batch = 512 ) elif self.embedding_model_name == 'llamacpppython': self.embedding_model = LlamaCppEmbeddings( model_path = r'C:\Users\ilija\llama.cpp\models\7B\ggml-model-q4_0.gguf', verbose = True, n_ctx = 1024, n_gpu_layers = 40, n_batch = 512 ) elif self.embedding_model_name == 'sbert': self.embedding_model = GPT4AllEmbeddings( model_path = r"ggml-all-MiniLM-L6-v2-f16.bin" ) elif self.embedding_model_name == 'ggml-falcon': print("Using falcon model") self.embedding_model = GPT4AllEmbeddings( model = r"D:\python_projects\loka_final\models\ggml-model-gpt4all-falcon-q4_0.bin" ) elif self.embedding_model_name.startswith('flan'): self.embedding_model = GPT4AllEmbeddings( model_path = r"ggml-all-MiniLM-L6-v2-f16.bin" ) else: model_name = None if self.embedding_model_name.startswith('distilbert'): model_name = "sentence-transformers/distilbert-base-nli-stsb-mean-tokens" elif self.embedding_model_name.startswith('bert'): model_name = "sentence-transformers/bert-base-nli-stsb-mean-tokens", elif self.embedding_model_name.startswith('roberta'): model_name = "symanto/sn-xlm-roberta-base-snli-mnli-anli-xnli" elif self.embedding_model_name.startswith('bge-large'): model_name = "BAAI/bge-large-en-v1.5" elif self.embedding_model_name.startswith('bge-llm'): model_name = "BAAI/bge-large-en-v1.5" self.embedding_model = HuggingFaceEmbeddings( model_name = model_name, model_kwargs = {'device': 'cuda:0'} if self.device.startswith('cuda') else {}, # encode_kwargs = {'normalize_embeddings': False}` ) self.embedding_tokenizer = AutoTokenizer.from_pretrained(model_name) if not self.embedding_model: raise NameError("The model_name for embeddings that you entered is not supported")
[]
2024-01-10
ilijavishinov/aws-doc-chat
utils_dir~llm_agent.py
import logging from langchain.chat_models import ChatOpenAI from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline, AutoModel, RobertaForCausalLM, AutoModelForQuestionAnswering from langchain import HuggingFacePipeline from langchain.llms import HuggingFacePipeline from langchain.llms import LlamaCpp, GPT4All from langchain.chains import RetrievalQA import os from utils_dir.text_processing import console_print from langchain.prompts import PromptTemplate class LlmAgent(object): llm = None def __init__(self, llm_model_name: str = None): self.llm_model_name = llm_model_name self.rag_prompt_template = """Use only the following pieces of context to answer the question at the end. \ If the context does not contain the answer, say that the documentation does not contain the answer. {context} Question: {question} Answer:""" self.llm_rag_prompt = PromptTemplate( template = self.rag_prompt_template, input_variables = ["context", "question"] ) self.get_llm_object() def get_llm_object(self): """ Returns the LLM object based on the provided model name """ if self.llm_model_name.startswith('openai'): self.llm = ChatOpenAI(model_name = "gpt-3.5-turbo") elif self.llm_model_name == 'llamacpp': self.llm = LlamaCpp( model_path = r'C:\Users\ilija\llama.cpp\models\7B\ggml-model-q4_0.gguf', verbose = True, n_ctx = 1024, n_threads = 8, n_gpu_layers = 40, n_batch = 512) elif self.llm_model_name == 'gpt4all': self.llm = GPT4All( model = './models/ggml-gpt4all-j-v1.3-groovy.bin', ) # verbose = True, n_ctx = 1024, n_gpu_layers = 1, n_batch = 4) elif self.llm_model_name == 'ggml-falcon': self.llm = GPT4All(model = r"D:\Downloads\ggml-model-gpt4all-falcon-q4_0.bin") # verbose = True, n_ctx = 1024, n_gpu_layers = 1, n_batch = 4) elif self.llm_model_name.startswith('flan'): tokenizer = AutoTokenizer.from_pretrained(f"google/{self.llm_model_name}") model = AutoModelForSeq2SeqLM.from_pretrained(f"google/{self.llm_model_name}") pipe = pipeline("text2text-generation", model = model, tokenizer = tokenizer) self.llm = HuggingFacePipeline( pipeline = pipe, model_kwargs = {"temperature": 0, "max_length": 512}, ) elif self.llm_model_name.startswith('distilbert'): tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/distilbert-base-nli-stsb-mean-tokens") model = AutoModelForSeq2SeqLM.from_pretrained("sentence-transformers/distilbert-base-nli-stsb-mean-tokens") pipe = pipeline("text2text-generation", model = model, tokenizer = tokenizer) self.llm = HuggingFacePipeline( pipeline = pipe, ) elif self.llm_model_name.startswith('bert'): tokenizer = AutoTokenizer.from_pretrained(f"sentence-transformers/bert-base-nli-stsb-mean-tokens") model = AutoModelForSeq2SeqLM.from_pretrained("sentence-transformers/bert-base-nli-stsb-mean-tokens") pipe = pipeline("text2text-generation", model = model, tokenizer = tokenizer) self.llm = HuggingFacePipeline( pipeline = pipe, ) elif self.llm_model_name.startswith('roberta'): tokenizer = AutoTokenizer.from_pretrained(f"deepset/roberta-base-squad2") model = RobertaForCausalLM.from_pretrained("deepset/roberta-base-squad2") pipe = pipeline("text2text-generation", model = model, tokenizer = tokenizer) self.llm = HuggingFacePipeline( pipeline = pipe, ) if not self.llm: raise NameError("The model_name for llm that you entered is not supported") def llm_rag(self, query: str, db): """ Performs Retrieval Augmented Generation with the most similar document from the vector db """ query = query.lower() result = None answer = 'not contain the answer' current_k = 0 while 'not contain the answer' in answer and current_k <= 1: current_k += 1 qa = RetrievalQA.from_chain_type(llm = self.llm, chain_type = "stuff", retriever = db.as_retriever(search_kwargs = {'k': current_k}), chain_type_kwargs = {"prompt": self.llm_rag_prompt}, return_source_documents = True ) result = qa({"query": query}) answer = result['result'] # console_print(result, 'result') relevant_docs, similarity_scores = self.relevant_docs_ordered_by_similarity(query, db, current_k) # console_print(relevant_docs, 'relevant_docs') return result, relevant_docs @staticmethod def relevant_docs_ordered_by_similarity(query: str, db, k: int, threshold: float = 0.5): """ Returns the most similar documents to the query depending on a similarity threshold """ relevant_docs_tuples = db.similarity_search_with_relevance_scores(query, k = k) # sort by relevance score relevant_docs_tuples.sort(key = lambda a: a[1], reverse = True) # take only relevant docs with cosine similarity > 0.5 relevant_docs = [pair[0] for pair in relevant_docs_tuples if pair[1] >= threshold] similarity_scores = [pair[1] for pair in relevant_docs_tuples if pair[1] >= threshold] console_print('Most similar documents') for i in range(len(relevant_docs)): console_print(f'{similarity_scores[i]} {relevant_docs[i]}') return relevant_docs, similarity_scores
[]
2024-01-10
Bugpig03/DiscordMikeBot
init.py
#-------- LIBRARIES -------- import discord import random from discord.ext import commands from discord.utils import get import os import re import time as classictime from datetime import datetime, time, timedelta from dotenv import load_dotenv import asyncio import ffmpeg import requests from blagues_api import BlaguesAPI import openai from pytube import YouTube from pydub import AudioSegment from youtubesearchpython import VideosSearch import json from quart import Quart, render_template, request, redirect from aiohttp import web import threading #-------- VARIABLES DECLARATIONS -------- intents = discord.Intents.all() bot = commands.Bot(command_prefix='*', intents=intents) # instance bot discord app = Quart(__name__) #instant server web quart scores = {} listProfiles = {} newProfile = {} MESSAGE_TIME = time(10, 35) queueMusic = [] currentTOP10 = [] mainTextChannel = 479958977472364555 mainVoiceChannel = 426760269205602304 currentAnecdote = "Aucune annecdote disponible sorry :(" currentMusic = "" currentMusicQueue = [] #-------- PATH CONFIGURATION -------- MUSIC_DIR = 'C:\Projet\Python\music' MUSIC_DIR_YT = 'C:\Projet\Python\YTmusic' SECRET_JSON_DIR = r"C:\Projet\Python\DiscordMikeBot\secrets.json" PROFILE_JSON_DIR = r"C:\Users\Administrateur\source\repos\Bugpig03\DiscordMikeBot\profiles.json"
[]
2024-01-10
bholbein/mm-cloud-apis-researcher
packages~research-assistant~research_assistant~writer.py
from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import ConfigurableField WRITER_SYSTEM_PROMPT = "Du bist ein KI-Forschungsassistent für kritisches Denken. Dein einziger Zweck ist es, gut geschriebene, kritisch anerkannte, objektive und strukturierte Berichte zu vorgegebenen Texten zu verfassen." # Report prompts from https://github.com/assafelovic/gpt-researcher/blob/master/gpt_researcher/master/prompts.py RESEARCH_REPORT_TEMPLATE = """Information: -------- {research_summary} -------- Mit den oben genannten Informationen beantworte die folgende Frage oder das Thema: "{question}" in einem ausführlichen Bericht -- Der Bericht sollte sich auf die Antwort der Frage konzentrieren, gut strukturiert, informativ, tiefgehend sein, mit Fakten und Zahlen, falls verfügbar, und mindestens 1.200 Wörter umfassen. Du solltest dich bemühen, den Bericht so lang wie möglich zu schreiben, unter Verwendung aller relevanten und notwendigen Informationen. Du musst den Bericht in Markdown-Syntax verfassen. Du MUSST deine eigene konkrete und valide Meinung auf Basis der gegebenen Informationen bilden. Weiche NICHT zu allgemeinen und bedeutungslosen Schlussfolgerungen ab. Schreibe alle verwendeten Quellen-URLs am Ende des Berichts und achte darauf, keine doppelten Quellen hinzuzufügen, sondern nur einen Verweis für jede. Du musst den Bericht im APA-Format verfassen. Bitte gib dein Bestes, das ist sehr wichtig für meine Karriere.""" # noqa: E501 RESOURCE_REPORT_TEMPLATE = """Information: -------- {research_summary} -------- Basierend auf den oben genannten Informationen, erstelle einen Empfehlungsbericht für Bibliografie für die folgende Frage oder das Thema: "{question}". Der Bericht sollte eine detaillierte Analyse jeder empfohlenen Ressource bieten, wobei erläutert wird, wie jede Quelle zur Beantwortung der Forschungsfrage beitragen kann. Konzentriere dich auf die Relevanz, Zuverlässigkeit und Bedeutung jeder Quelle. Stelle sicher, dass der Bericht gut strukturiert, informativ, tiefgehend ist und der Markdown-Syntax folgt. Schließe relevante Fakten, Zahlen und Daten ein, wann immer verfügbar. Der Bericht sollte eine Mindestlänge von 1.200 Wörtern haben. Bitte gib dein Bestes, das ist sehr wichtig für meine Karriere.""" # noqa: E501 OUTLINE_REPORT_TEMPLATE = """Information: -------- {research_summary} -------- Mit den oben genannten Informationen erstelle ein Gerüst für einen Forschungsbericht in Markdown-Syntax für die folgende Frage oder das Thema: "{question}". Das Gerüst sollte einen gut strukturierten Rahmen für den Forschungsbericht bieten, einschließlich der Hauptabschnitte, Unterabschnitte und der wichtigsten zu behandelnden Punkte. Der Forschungsbericht sollte detailliert, informativ, tiefgehend sein und mindestens 1.200 Wörter umfassen. Verwende die entsprechende Markdown-Syntax, um das Gerüst zu formatieren und die Lesbarkeit zu gewährleisten. Bitte gib dein Bestes, das ist sehr wichtig für meine Karriere..""" # noqa: E501 model = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0) prompt = ChatPromptTemplate.from_messages( [ ("system", WRITER_SYSTEM_PROMPT), ("user", RESEARCH_REPORT_TEMPLATE), ] ).configurable_alternatives( ConfigurableField("report_type"), default_key="research_report", resource_report=ChatPromptTemplate.from_messages( [ ("system", WRITER_SYSTEM_PROMPT), ("user", RESOURCE_REPORT_TEMPLATE), ] ), outline_report=ChatPromptTemplate.from_messages( [ ("system", WRITER_SYSTEM_PROMPT), ("user", OUTLINE_REPORT_TEMPLATE), ] ), ) chain = prompt | model | StrOutputParser()
[ "Du bist ein KI-Forschungsassistent für kritisches Denken. Dein einziger Zweck ist es, gut geschriebene, kritisch anerkannte, objektive und strukturierte Berichte zu vorgegebenen Texten zu verfassen.", "[('system', 'Du bist ein KI-Forschungsassistent für kritisches Denken. Dein einziger Zweck ist es, gut geschriebene, kritisch anerkannte, objektive und strukturierte Berichte zu vorgegebenen Texten zu verfassen.'), ('user', 'Information: \\n--------\\n{research_summary}\\n--------\\n\\nMit den oben genannten Informationen beantworte die folgende Frage oder das Thema: \"{question}\" in einem ausführlichen Bericht --\\nDer Bericht sollte sich auf die Antwort der Frage konzentrieren, gut strukturiert, informativ,\\ntiefgehend sein, mit Fakten und Zahlen, falls verfügbar, und mindestens 1.200 Wörter umfassen.\\n\\nDu solltest dich bemühen, den Bericht so lang wie möglich zu schreiben, unter Verwendung aller relevanten und notwendigen Informationen.\\nDu musst den Bericht in Markdown-Syntax verfassen.\\nDu MUSST deine eigene konkrete und valide Meinung auf Basis der gegebenen Informationen bilden. Weiche NICHT zu allgemeinen und bedeutungslosen Schlussfolgerungen ab.\\nSchreibe alle verwendeten Quellen-URLs am Ende des Berichts und achte darauf, keine doppelten Quellen hinzuzufügen, sondern nur einen Verweis für jede.\\nDu musst den Bericht im APA-Format verfassen.\\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere.')]", "Information: \n--------\n{research_summary}\n--------\n\nBasierend auf den oben genannten Informationen, erstelle einen Empfehlungsbericht für Bibliografie für die folgende Frage oder das Thema: \"{question}\".\nDer Bericht sollte eine detaillierte Analyse jeder empfohlenen Ressource bieten, wobei erläutert wird, wie jede Quelle zur Beantwortung der Forschungsfrage beitragen kann.\nKonzentriere dich auf die Relevanz, Zuverlässigkeit und Bedeutung jeder Quelle.\nStelle sicher, dass der Bericht gut strukturiert, informativ, tiefgehend ist und der Markdown-Syntax folgt.\nSchließe relevante Fakten, Zahlen und Daten ein, wann immer verfügbar.\nDer Bericht sollte eine Mindestlänge von 1.200 Wörtern haben.\n\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere.", "research_report", "Information: \n--------\n{research_summary}\n--------\n\nMit den oben genannten Informationen beantworte die folgende Frage oder das Thema: \"{question}\" in einem ausführlichen Bericht --\nDer Bericht sollte sich auf die Antwort der Frage konzentrieren, gut strukturiert, informativ,\ntiefgehend sein, mit Fakten und Zahlen, falls verfügbar, und mindestens 1.200 Wörter umfassen.\n\nDu solltest dich bemühen, den Bericht so lang wie möglich zu schreiben, unter Verwendung aller relevanten und notwendigen Informationen.\nDu musst den Bericht in Markdown-Syntax verfassen.\nDu MUSST deine eigene konkrete und valide Meinung auf Basis der gegebenen Informationen bilden. Weiche NICHT zu allgemeinen und bedeutungslosen Schlussfolgerungen ab.\nSchreibe alle verwendeten Quellen-URLs am Ende des Berichts und achte darauf, keine doppelten Quellen hinzuzufügen, sondern nur einen Verweis für jede.\nDu musst den Bericht im APA-Format verfassen.\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere.", "report_type", "[('system', 'Du bist ein KI-Forschungsassistent für kritisches Denken. Dein einziger Zweck ist es, gut geschriebene, kritisch anerkannte, objektive und strukturierte Berichte zu vorgegebenen Texten zu verfassen.'), ('user', 'Information: \\n--------\\n{research_summary}\\n--------\\n\\nBasierend auf den oben genannten Informationen, erstelle einen Empfehlungsbericht für Bibliografie für die folgende Frage oder das Thema: \"{question}\".\\nDer Bericht sollte eine detaillierte Analyse jeder empfohlenen Ressource bieten, wobei erläutert wird, wie jede Quelle zur Beantwortung der Forschungsfrage beitragen kann.\\nKonzentriere dich auf die Relevanz, Zuverlässigkeit und Bedeutung jeder Quelle.\\nStelle sicher, dass der Bericht gut strukturiert, informativ, tiefgehend ist und der Markdown-Syntax folgt.\\nSchließe relevante Fakten, Zahlen und Daten ein, wann immer verfügbar.\\nDer Bericht sollte eine Mindestlänge von 1.200 Wörtern haben.\\n\\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere.')]", "[('system', 'Du bist ein KI-Forschungsassistent für kritisches Denken. Dein einziger Zweck ist es, gut geschriebene, kritisch anerkannte, objektive und strukturierte Berichte zu vorgegebenen Texten zu verfassen.'), ('user', 'Information: \\n--------\\n{research_summary}\\n--------\\n\\nMit den oben genannten Informationen erstelle ein Gerüst für einen Forschungsbericht in Markdown-Syntax für die folgende Frage oder das Thema: \"{question}\".\\nDas Gerüst sollte einen gut strukturierten Rahmen für den Forschungsbericht bieten, einschließlich der Hauptabschnitte, Unterabschnitte und der wichtigsten zu behandelnden Punkte.\\nDer Forschungsbericht sollte detailliert, informativ, tiefgehend sein und mindestens 1.200 Wörter umfassen.\\nVerwende die entsprechende Markdown-Syntax, um das Gerüst zu formatieren und die Lesbarkeit zu gewährleisten.\\n\\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere..')]", "Information: \n--------\n{research_summary}\n--------\n\nMit den oben genannten Informationen erstelle ein Gerüst für einen Forschungsbericht in Markdown-Syntax für die folgende Frage oder das Thema: \"{question}\".\nDas Gerüst sollte einen gut strukturierten Rahmen für den Forschungsbericht bieten, einschließlich der Hauptabschnitte, Unterabschnitte und der wichtigsten zu behandelnden Punkte.\nDer Forschungsbericht sollte detailliert, informativ, tiefgehend sein und mindestens 1.200 Wörter umfassen.\nVerwende die entsprechende Markdown-Syntax, um das Gerüst zu formatieren und die Lesbarkeit zu gewährleisten.\n\nBitte gib dein Bestes, das ist sehr wichtig für meine Karriere..", "{question}" ]
2024-01-10
automediaAI/amData_News
amService_ChatGPT.py
########################## ########################## ##### ChatGPT Caller ######## ### Being used for Summarizing for now #### ########################## ########################## import nltk import os import openai # Installing NLTK from nltk.tokenize import word_tokenize nltk.download('punkt') # Initialize OpenAI API client from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv('CHATGPT_API_KEY', 'YourAPIKeyIfNotSet') def preprocess_text(text): # Convert to lowercase text = text.lower() # Tokenize into words words = word_tokenize(text) return words def summarize_with_gpt(text, max_tokens=50): print ('Text inputted-----', text) # Preprocess the text preprocessed_text = preprocess_text(text) # Join preprocessed text back into a string preprocessed_text_str = ' '.join(preprocessed_text) # preprocessed_text_str = ' '.join(preprocessed_text).encode('utf-8', 'replace') print ('Text PRE PROCESSED-----', preprocessed_text_str) print (type(preprocessed_text_str)) # Parameters for ChatGPT params = { # 'model': 'gpt-3.5-turbo', 'model': 'text-davinci-003', 'prompt': 'Summarize the following text:\n\n' + preprocessed_text_str, 'temperature': 0.5, 'max_tokens': max_tokens } print (params) # Call ChatGPT to generate a summary response = openai.Completion.create(**params) print (type(response)) print ('Response ---->>>', response) summary = response['choices'][0]['text'].strip() # summary = response['choices'][0]['text'].strip().decode('utf-8', 'replace') print("Summary CHATGPT>>", summary) #Making sure returning smaller of response summary_ToUse = summary if len(summary) < len(preprocessed_text_str) else preprocessed_text_str # return summary_ToUse # Encode the summary using UTF-8 # return summary_ToUse.encode('utf-8', errors='ignore') return summary_ToUse # Test the results text_to_summarize = "This is a long piece of text that needs to be summarized" # # text_to_summarize = ("When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously. “I can tell you very senior CEOs of major American car companies would shake my hand and turn away because I wasn’t worth talking to,” said Thrun, now the co-founder and CEO of online higher education startup Udacity, in an interview with Recode earlier this week. The Mona Lisa and the Statue of David were on display in the MOMA New York. COVID-19 is a devastating virus currently ravaging the world. A little less than a decade later, dozens of self-driving startups have cropped up while automakers around the world clamor, wallet in hand, to secure their place in the fast-moving world of fully automated transportation.") print(summarize_with_gpt(text_to_summarize)) # # print("Summary:", )
[]
2024-01-10
automediaAI/amData_News
amService_ChatGPT_Nitin.py
########################## ########################## ##### ChatGPT Caller ######## ### Being used for Summarizing for now #### ########################## ########################## import nltk import os import openai # Installing NLTK from nltk.tokenize import word_tokenize nltk.download('punkt') # Initialize OpenAI API client # openai.api_key = os.environ.get("CHATGPT_API_KEY") # openai.api_key = os.getenv("OPENAI_API_KEY") from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv('CHATGPT_API_KEY', 'YourAPIKeyIfNotSet') def preprocess_text(text): # Convert to lowercase text = text.lower() # Tokenize into words words = word_tokenize(text) return words def summarize_with_gpt(text, max_tokens=50): print ('Text inputted-----', text) # Preprocess the text preprocessed_text = preprocess_text(text) # Join preprocessed text back into a string # preprocessed_text_str = ' '.join(preprocessed_text) preprocessed_text_str = ' '.join(preprocessed_text).encode('utf-8', 'replace') print ('Text PRE PROCESSED-----', str(preprocessed_text_str)) print (type(preprocessed_text_str)) # Parameters for ChatGPT # params = { # 'model': 'gpt-3.5-turbo', # 'prompt': 'Summarize the following text:\n\n' + str(preprocessed_text_str), # 'temperature': 0.5, # 'max_tokens': max_tokens # } params = { 'model': 'text-davinci-003', 'prompt': 'Summarize the following text:\n\n' + str(preprocessed_text_str), 'temperature': 0.5, 'max_tokens': max_tokens } # Call ChatGPT to generate a summary response = openai.Completion.create(**params) print (type(response)) print ('Response ---->>>', response) # response = openai.ChatCompletion.create( # model="gpt-3.5-turbo", # messages=[], # temperature=0, # max_tokens=max_tokens # ) # print (typeof(response)) # print ('Response ---->>>', response) # summary = response['choices'][0]['text'].strip() summary = response['choices'][0]['text'].strip() print("Summary CHATGPT>>", summary) #Making sure returning smaller of response summary_ToUse = summary if len(summary) < len(preprocessed_text_str) else preprocessed_text_str # return summary_ToUse # Encode the summary using UTF-8 return summary_ToUse.encode('utf-8', errors='ignore') # Test the results # text_to_summarize = "This is a long piece of text that needs to be summarized" text_to_summarize = ("When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously. “I can tell you very senior CEOs of major American car companies would shake my hand and turn away because I wasn’t worth talking to,” said Thrun, now the co-founder and CEO of online higher education startup Udacity, in an interview with Recode earlier this week. The Mona Lisa and the Statue of David were on display in the MOMA New York. COVID-19 is a devastating virus currently ravaging the world. A little less than a decade later, dozens of self-driving startups have cropped up while automakers around the world clamor, wallet in hand, to secure their place in the fast-moving world of fully automated transportation.") print(summarize_with_gpt(text_to_summarize)) # print("Summary:", )
[]
2024-01-10
mcweber/myBot
mybot.py
import streamlit as st from langchain.chat_models import ChatOpenAI from langchain.schema import ( SystemMessage, HumanMessage, AIMessage ) from langchain.callbacks import get_openai_callback def init_page(): st.set_page_config( page_title="Mein persönlicher ChatBot", ) st.header("Mein persönlicher ChatBot") st.sidebar.title("Optionen") def init_messages(): clear_button = st.sidebar.button("Konversation löschen", key="clear") if clear_button or "messages" not in st.session_state: st.session_state.messages = [ SystemMessage(content="Du bist ein hilreicher Assisstent. Du antwortest immer in Deutsche, es denn Du wirst dazu aufgefordert.") ] st.session_state.costs = [] def select_model(): model = st.sidebar.radio("Wähle ein LLM:", ("GPT-3.5", "GPT-4")) if model == "GPT-3.5": model_name = "gpt-3.5-turbo" else: model_name = "gpt-4" # Add a slider to allow users to select the temperature from 0 to 2. # The initial value should be 0.0, with an increment of 0.01. temperature = st.sidebar.slider("Temperatur:", min_value=0.0, max_value=2.0, value=0.0, step=0.01) return ChatOpenAI(temperature=temperature, model_name=model_name) def get_answer(llm, messages): with get_openai_callback() as cb: answer = llm(messages) return answer.content, cb.total_cost def main(): init_page() llm = select_model() init_messages() # Monitor user input if user_input := st.chat_input("Gib Deine Frage hier ein:"): st.session_state.messages.append(HumanMessage(content=user_input)) with st.spinner("ChatGPT arbeitet und schreibt ..."): answer, cost = get_answer(llm, st.session_state.messages) st.session_state.messages.append(AIMessage(content=answer)) st.session_state.costs.append(cost) messages = st.session_state.get('messages', []) for message in messages: if isinstance(message, AIMessage): with st.chat_message('assistant'): st.markdown(message.content) elif isinstance(message, HumanMessage): with st.chat_message('user'): st.markdown(message.content) else: # isinstance(message, SystemMessage): st.write(f"System message: {message.content}") costs = st.session_state.get('costs', []) st.sidebar.markdown("## Kosten") st.sidebar.markdown(f"**Summe Kosten: ${sum(costs):.5f}**") for cost in costs: st.sidebar.markdown(f"- ${cost:.5f}") if __name__ == '__main__': main()
[ "Du bist ein hilreicher Assisstent. Du antwortest immer in Deutsche, es denn Du wirst dazu aufgefordert." ]
2024-01-10
SunshineMike/Cheatsheets-and-Scripts
Python%20Snippets~dall_e.py
import openai class DallE: def __init__(self, api_key): self.api_key = api_key openai.api_key = self.api_key def generate(self, prompt, n, size): if size == "1": dimension = "256x256" elif size == "2": dimension = "512x512" elif size == "3": dimension = "1024x1024" else: print("Invalid size") return response = openai.Image.create( prompt=prompt, n=n, size=dimension ) image_url = response['data'][0]['url'] return image_url ''' generator = ImageGenerator("your_openai_api_key_here") image_url = generator.generate("A beautiful sunset over the ocean", 1, 2) print(image_url) '''
[]
2024-01-10
SunshineMike/Cheatsheets-and-Scripts
Python%20Snippets~gpt-3.5-turbo.py
import json from datetime import datetime import openai class GPT35Turbo: def __init__(self, api_key, model="gpt-3.5-turbo"): self.api_key = api_key self.model = model self.messages = [] self.filename = self.get_filename() def add_system_message(self, message): self.messages.append({"role": "system", "content": message}) def add_user_message(self, message): self.messages.append({"role": "user", "content": message}) def generate_response(self): self.save_conversation() openai.api_key = self.api_key response = openai.ChatCompletion.create( model=self.model, messages=self.messages, temperature=0.2 # stream=True ) role = response["choices"][0]["message"]["role"] self.messages.append({"role": role, "content": response["choices"][0]["message"]["content"]}) return response["choices"][0]["message"]["content"] def clear_messages(self): self.messages = [] def summarize_messages(self): pass def get_filename(self): now = datetime.now() return now.strftime("%d-%m-%Y-%H-%M-%S") def save_conversation(self): path = "Conversations\\" + self.filename + ".txt" with open(path, "w") as f: for message in self.messages: json.dump(message, f) f.write('\n')
[ "content" ]
2024-01-10
JakobFenderHSLU/adventure-architect
src~Generator~MagicItemGenerator.py
import json import random from Utility import OpenAIConnection from Utility.ItemHelper import item_template, possible_armor_subtypes, possible_item_rarities, possible_item_types, \ possible_weapons_subtypes def generate_magic_item(description, selected_rarity, selected_type, requires_attunement, cursed): chatgpt_messages = generate_magic_item_prompt(description, selected_rarity, selected_type, requires_attunement, cursed) return OpenAIConnection.generate_text(chatgpt_messages) def generate_magic_item_prompt(description, selected_rarity, selected_type, requires_attunement, cursed): return_format = f""" THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. DO NOT USE LINE BREAKS. START WITH "{{" AND END WITH "}}". \n Return the Item in the following format: \n {item_template} """ attribute_explanation = f""" "Subtype": Armor and Weapons have subtypes. Choose between the following for Armor: {", ".join(possible_armor_subtypes)} \n And choose between the following for Weapons: {", ".join(possible_weapons_subtypes)} \n "Attunement": Choose between Yes and No. Scrolls and Potions can not be attuned. \n "Visual Description": Write a description of the item. Make it as detailed as possible.\n "Mechanical Description": Write a description of the item. This is the description that will be used in combat. This attribute needs to be filled out! \n "Story": Write a story about the item. This attribute is optional. \n "Price": Write the price of the item in gp. \n "Cursed": A description of the curse. This attribute needs to be filled out, if the item is cursed. \n """ rarity_for_request = selected_rarity type_for_request = selected_type attunement_for_request = requires_attunement cursed_for_request = cursed if selected_rarity == "Random": rarity_for_request = random.choice(possible_item_rarities) if selected_type == "Random": type_for_request = random.choice(possible_item_types) if requires_attunement == "Random": attunement_for_request = random.choice(["Yes", "No"]) if cursed == "Random": cursed_for_request = random.choice(["Yes", "No"]) guide = """ Here is a quick guide on how to create a Magic Item: \n Magic Item Rarity and recommended price: \n Rarity Value \n Common 50-100 gp \n Uncommon 101-500 gp \n Rare 501-5,000 gp \n Very rare 5,001-50,000 gp \n Legendary 50,001+ gp \n Magic Bonus and Rarity: \n Rarity Bonus \n Rare +1 \n Very rare +2 \n Legendary +3 \n Strictly stick to the recommended Magic Bonus. \n Spell Level and Rarity: \n Rarity Max Spell Level Common 1st Uncommon 3rd Rare 6th Very rare 8th Legendary 9th """ prompt = f""" Create an Magic Item for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n Rarity: {rarity_for_request} \n ItemType: {type_for_request} \n Requires Attunement: {attunement_for_request} \n Item is cursed: {cursed_for_request} \n Description always has priority over the other attributes. \n Description: {description} """ chatgpt_messages = [ {"role": "system", "content": "Create an Magic Item for Dungeons and Dragons."}, {"role": "system", "content": return_format}, {"role": "system", "content": attribute_explanation}, {"role": "system", "content": guide}, {"role": "user", "content": prompt} ] print(chatgpt_messages) return chatgpt_messages
[ "Create an Magic Item for Dungeons and Dragons.", "\n Create an Magic Item for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n\n Rarity: PLACEHOLDER \n\n ItemType: PLACEHOLDER \n\n Requires Attunement: PLACEHOLDER \n\n Item is cursed: PLACEHOLDER \n\n\n Description always has priority over the other attributes. \n\n Description: PLACEHOLDER\n ", "\n Here is a quick guide on how to create a Magic Item: \n\n \n Magic Item Rarity and recommended price: \n\n Rarity Value \n\n Common\t 50-100 gp \n\n Uncommon\t101-500 gp \n\n Rare 501-5,000 gp \n\n Very rare 5,001-50,000 gp \n\n Legendary\t50,001+ gp \n\n \n Magic Bonus and Rarity: \n\n Rarity Bonus \n\n Rare +1 \n\n Very rare +2 \n\n Legendary\t+3 \n\n Strictly stick to the recommended Magic Bonus. \n\n \n Spell Level and Rarity: \n\n Rarity\t Max Spell Level\n Common\t 1st\n Uncommon\t3rd\n Rare\t 6th\n Very rare\t8th\n Legendary\t9th\n \n ", "\n THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. \n DO NOT USE LINE BREAKS. START WITH \"{\" AND END WITH \"}\". \n\n Return the Item in the following format: \n\n PLACEHOLDER\n ", "f\"\"\"\n \"Subtype\": Armor and Weapons have subtypes. Choose between the following for Armor:\n {\", \".join(possible_armor_subtypes)} \\n \n And choose between the following for Weapons: {\", \".join(possible_weapons_subtypes)} \\n\n \"Attunement\": Choose between Yes and No. Scrolls and Potions can not be attuned. \\n\n \"Visual Description\": Write a description of the item. Make it as detailed as possible.\\n\n \"Mechanical Description\": Write a description of the item. This is the description that will be used in combat.\n This attribute needs to be filled out! \\n\n \"Story\": Write a story about the item. This attribute is optional. \\n\n \"Price\": Write the price of the item in gp. \\n\n \"Cursed\": A description of the curse. This attribute needs to be filled out, if the item is cursed. \\n\n " ]
2024-01-10
JakobFenderHSLU/adventure-architect
src~Generator~MonsterGenerator.py
import json import random import pandas as pd from Utility import OpenAIConnection from Utility.MonsterHelper import possible_monster_types, possible_challenge_ratings, possible_size, \ possible_environments, monster_template balance_data = pd.read_csv("assets/balance_stats.csv") def generate_monster(description, selected_monster_type, selected_challenge_rating, selected_size, selected_environment, selected_legendary, selected_lair): chatgpt_messages = generate_monster_prompt(description, selected_monster_type, selected_challenge_rating, selected_size, selected_environment, selected_legendary, selected_lair) return OpenAIConnection.generate_text(chatgpt_messages) def generate_monster_prompt(description, selected_monster_type, selected_challenge_rating, selected_size, selected_environment, selected_legendary, selected_lair): return_format = f""" THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. DO NOT USE LINE BREAKS. START WITH "{{" AND END WITH "}}". \n Return the Monster in the following format: : \n {monster_template} """ monster_type_for_request = selected_monster_type challenge_rating_for_request = selected_challenge_rating size_for_request = selected_size environment_for_request = selected_environment legendary_for_request = selected_legendary lair_for_request = selected_lair if selected_monster_type == "Random": monster_type_for_request = random.choice(possible_monster_types) if selected_challenge_rating == "Random": challenge_rating_for_request = random.choice(possible_challenge_ratings) if selected_size == "Random": size_for_request = random.choice(possible_size) if selected_environment == "Random": environment_for_request = random.choice(possible_environments) if selected_legendary == "Random": legendary_for_request = random.choice(["Yes", "No"]) if selected_lair == "Random": lair_for_request = random.choice(["Yes", "No"]) average_stats = balance_data[balance_data['CR'] == challenge_rating_for_request].iloc[0] attribute_explanation = f""" "Type of Monster": Choose between {", ".join(possible_monster_types)} \n "Armor Class": The average Armor Class for your Challenge Rating is {average_stats["Armor Class"]}. You can choose a different score!\n "Hit Points": The average Hit Points for your Challenge Rating is {average_stats["Hit Points"]}. You can choose a different amount, if it makes sense!\n "Speed": Speed is usually 30 ft. Monsters can have mulitple speeds for different modes of transportation. Choose at least one between "Walk", "Fly", "Swim", "Climb", "Burrow". \n "Legendary Action Description": Only fill out, if "Legendary Actions" is not empty \n "Lair Action Description": Only fill out, if "Lair Actions" is not empty \n "Bonus Actions": A weaker version of "Actions". \n "Actions": Add actions that your NPC can take. The average Variables for your Challenge Rating is as follows: \n Hit Bonus: {average_stats["Attack Bonus"]} \n Damage per Round: {average_stats["Average DPR"]} \n Save DC: {average_stats["Save DC"]} \n You can choose different values, if it makes sense! """ guide = """ Here is a quick guide on how to create a Monster: \n Recommended Statistics per Challenge Rating: \n CR Prof. Bonus Armor Class Hit Points Attack Bonus Damage/Round Save DC 0 +2 ≤ 13 1-6 ≤ +3 0-1 ≤ 13 1/8 +2 13 7-35 +3 2-3 13 1/4 +2 13 36-49 +3 4-5 13 1/2 +2 13 50-70 +3 6-8 13 1 +2 13 71-85 +3 9-14 13 2 +2 13 86-100 +3 15-20 13 3 +2 13 101-115 +4 21-26 13 4 +2 14 116-130 +5 27-32 14 5 +3 15 131-145 +6 33-38 15 6 +3 15 146-160 +6 39-44 15 7 +3 15 161-175 +6 45-50 15 8 +3 16 176-190 +7 51-56 16 9 +4 16 191-205 +7 57-62 16 10 +4 17 206-220 +7 63-68 16 11 +4 17 221-235 +8 69-74 17 12 +4 17 236-250 +8 75-80 17 13 +5 18 251-265 +8 81-86 18 14 +5 18 266-280 +8 87-92 18 15 +5 18 281-295 +8 93-98 18 16 +5 18 296-310 +9 99-104 18 17 +6 19 311-325 +10 105-110 19 18 +6 19 326-340 +10 111-116 19 19 +6 19 341-355 +10 117-122 19 20 +6 19 356-400 +10 123-140 19 21 +7 19 401-445 +11 141-158 20 22 +7 19 446-490 +11 159-176 20 23 +7 19 491-535 +11 177-194 20 24 +7 19 536-580 +12 195-212 21 25 +8 19 581-625 +12 213-230 21 26 +8 19 626-670 +12 231-248 21 27 +8 19 671-715 +13 249-266 22 28 +8 19 716-760 +13 267-284 22 29 +9 19 761-805 +13 285-302 22 30 +9 19 806-850 +14 303-320 23 """ prompt = f""" Create an Monster for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n Type of Monster: {monster_type_for_request} \n Challenge Rating: {challenge_rating_for_request} \n Size: {size_for_request} \n Environment: {environment_for_request} \n Legendary: {legendary_for_request} \n Lair: {lair_for_request} \n Description always has priority over the other attributes. \n Description: {description} """ chatgpt_messages = [ {"role": "system", "content": "Create an Magic Item for Dungeons and Dragons."}, {"role": "system", "content": return_format}, {"role": "system", "content": attribute_explanation}, {"role": "system", "content": guide}, {"role": "user", "content": prompt} ] return chatgpt_messages
[ "\n THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. \n DO NOT USE LINE BREAKS. START WITH \"{\" AND END WITH \"}\". \n\n Return the Monster in the following format: : \n\n PLACEHOLDER\n ", "Create an Magic Item for Dungeons and Dragons.", "\n Create an Monster for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n\n Type of Monster: PLACEHOLDER \n\n Challenge Rating: PLACEHOLDER \n\n Size: PLACEHOLDER \n\n Environment: PLACEHOLDER \n\n Legendary: PLACEHOLDER \n\n Lair: PLACEHOLDER \n\n\n Description always has priority over the other attributes. \n\n Description: PLACEHOLDER\n ", "f\"\"\"\n \"Type of Monster\": Choose between {\", \".join(possible_monster_types)} \\n\n \"Armor Class\": The average Armor Class for your Challenge Rating is {average_stats[\"Armor Class\"]}. \n You can choose a different score!\\n\n \"Hit Points\": The average Hit Points for your Challenge Rating is {average_stats[\"Hit Points\"]}. \n You can choose a different amount, if it makes sense!\\n\n \"Speed\": Speed is usually 30 ft. Monsters can have mulitple speeds for different modes of transportation.\n Choose at least one between \"Walk\", \"Fly\", \"Swim\", \"Climb\", \"Burrow\". \\n\n \"Legendary Action Description\": Only fill out, if \"Legendary Actions\" is not empty \\n\n \"Lair Action Description\": Only fill out, if \"Lair Actions\" is not empty \\n\n \"Bonus Actions\": A weaker version of \"Actions\". \\n\n \"Actions\": Add actions that your NPC can take. The average Variables for your Challenge Rating is as follows: \\n\n Hit Bonus: {average_stats[\"Attack Bonus\"]} \\n\n Damage per Round: {average_stats[\"Average DPR\"]} \\n\n Save DC: {average_stats[\"Save DC\"]} \\n\n You can choose different values, if it makes sense!\n ", "\n Here is a quick guide on how to create a Monster: \n\n\n Recommended Statistics per Challenge Rating: \n\n CR\tProf. Bonus\tArmor Class \tHit Points\tAttack Bonus\tDamage/Round\tSave DC\n 0\t+2 \t ≤ 13\t 1-6\t≤ +3\t 0-1\t ≤ 13\n 1/8\t+2\t 13\t 7-35\t +3\t 2-3\t 13\n 1/4\t+2\t 13\t 36-49\t +3\t 4-5\t 13\n 1/2\t+2\t 13\t 50-70\t +3\t 6-8\t 13\n 1\t+2\t 13\t 71-85\t +3\t 9-14\t 13\n 2\t+2\t 13\t 86-100\t +3\t 15-20\t 13\n 3\t+2\t 13\t 101-115\t +4\t 21-26\t 13\n 4\t+2\t 14\t 116-130\t +5\t 27-32\t 14\n 5\t+3\t 15\t 131-145\t +6\t 33-38\t 15\n 6\t+3\t 15\t 146-160\t +6\t 39-44\t 15\n 7\t+3\t 15\t 161-175\t +6\t 45-50\t 15\n 8\t+3\t 16\t 176-190\t +7\t 51-56\t 16\n 9\t+4\t 16\t 191-205\t +7\t 57-62\t 16\n 10\t+4\t 17\t 206-220\t +7\t 63-68\t 16\n 11\t+4\t 17\t 221-235\t +8\t 69-74\t 17\n 12\t+4\t 17\t 236-250\t +8\t 75-80\t 17\n 13\t+5\t 18\t 251-265\t +8\t 81-86\t 18\n 14\t+5\t 18\t 266-280\t +8\t 87-92\t 18\n 15\t+5\t 18\t 281-295\t +8\t 93-98\t 18\n 16\t+5\t 18\t 296-310\t +9\t 99-104\t 18\n 17\t+6\t 19\t 311-325\t +10\t 105-110\t 19\n 18\t+6\t 19\t 326-340\t +10\t 111-116\t 19\n 19\t+6\t 19\t 341-355\t +10\t 117-122\t 19\n 20\t+6\t 19\t 356-400\t +10\t 123-140\t 19\n 21\t+7\t 19\t 401-445\t +11\t 141-158\t 20\n 22\t+7\t 19\t 446-490\t +11\t 159-176\t 20\n 23\t+7\t 19\t 491-535\t +11\t 177-194\t 20\n 24\t+7\t 19\t 536-580\t +12\t 195-212\t 21\n 25\t+8\t 19\t 581-625\t +12\t 213-230\t 21\n 26\t+8\t 19\t 626-670\t +12\t 231-248\t 21\n 27\t+8\t 19\t 671-715\t +13\t 249-266\t 22\n 28\t+8\t 19\t 716-760\t +13\t 267-284\t 22\n 29\t+9\t 19\t 761-805\t +13\t 285-302\t 22\n 30\t+9\t 19\t 806-850\t +14\t 303-320\t 23\n " ]
2024-01-10
JakobFenderHSLU/adventure-architect
src~Generator~SpellGenerator.py
import json import random from Utility import OpenAIConnection from Utility.SpellHelper import possible_conditions, possible_damage_types, possible_save_types, \ possible_spell_area_types, possible_spell_casting_time, possible_spell_durations, possible_spell_levels, \ possible_spell_ranges, possible_spell_schools, possible_spell_types, spell_template def generate_spell(description, selected_level, selected_school, selected_range, selected_components, selected_spell_type, selected_save_type, selected_spell_area, selected_requires_concentration, selected_damage_type, selected_condition, selected_ritual, selected_casting_time, selected_spell_duration): chatgpt_messages = generate_spell_prompt(description, selected_level, selected_school, selected_range, selected_components, selected_spell_type, selected_save_type, selected_spell_area, selected_requires_concentration, selected_damage_type, selected_condition, selected_ritual, selected_casting_time, selected_spell_duration) return OpenAIConnection.generate_text(chatgpt_messages) def generate_spell_prompt(description, selected_level, selected_school, selected_range, selected_components, selected_spell_type, selected_save_type, selected_spell_area, selected_requires_concentration, selected_damage_type, selected_condition, selected_ritual, selected_casting_time, selected_spell_duration): return_format = f""" THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. DO NOT USE LINE BREAKS. START WITH "{{" AND END WITH "}}". \n Return the Spell in the following format: : \n {spell_template} """ attribute_explanation = f""" "Level": Choose between the following: {", ".join(possible_spell_levels)}. This attribute needs to be filled out! \n "School": Choose between the following: {", ".join(possible_spell_schools)} This attribute needs to be filled out! \n "Range": Choose between the following: {", ".join(possible_spell_ranges)} This attribute needs to be filled out! \n "Spell Components": Choose one or multiple: "Verbal", "Somatic", "Material". This attribute needs to be filled out! \n "Material Components": Components that are required to cast the spell. Choose random ingredients that are related to the damage type, condition and/or description. This attribute is optional. \n "Casting Time": Choose between the following: {", ".join(possible_spell_casting_time)} This attribute needs to be filled out! \n "Duration": Choose between the following: {", ".join(possible_spell_durations)} This attribute needs to be filled out! \n "Requires Concentration": Choose between Yes and No. This attribute needs to be filled out! \n "Ritual": Choose between Yes and No. This attribute needs to be filled out! \n "Spell Type": Choose between the following: {", ".join(possible_spell_types)} This attribute needs to be filled out! \n "Save Type": Choose between the following: {", ".join(possible_save_types)} This attribute is optional. \n "Spell Area": Choose between the following: {", ".join(possible_spell_area_types)} This attribute is optional. \n "Damage Type": Choose between the following: {", ".join(possible_damage_types)} This attribute is optional. \n "Condition": Choose between the following: {", ".join(possible_conditions)} This attribute is optional. \n "Description": Write a description of the spell. Make it as detailed as possible. This attribute needs to be filled out! \n """ guide = """ Here is a quick guide on how to create a Spell: \n When creating a new spell, use existing spells as guidelines. Here are some things to consider: - If a spell is so good that a caster would want to use it all the time, it might be too powerful for its level. - A long duration or large area can make up for a lesser effect, depending on the spell. - Avoid spells that have very limited use, such as one that works only against good dragons. Though such a spell could exist in the world, few characters will bother to learn or prepare it unless they know in advance that doing so will be worthwhile. Spell Damage: \n For any spell that deals damage, use the Spell Damage table to determine approximately how much damage is appropriate given the spell's level. The table assumes the spell deals half damage on a successful saving throw or a missed attack. If your spell doesn't deal damage on a successful save, you can increase the damage by 25 percent. \n You can use different damage dice than the ones in the table, provided that the average result is about the same. Doing so can add a little variety to the spell. For example, you could change a cantrip's damage from 1d10 (average 5.5) to 2d4 (average 5), reducing the maximum damage and making an average result more likely. \n USE THIS TABLE TO DETERMINE SPELL DAMAGE: \n Spell Level One Target Multiple Targets \n Cantrip 1d10 1d6 \n 1st 2d10 2d6 \n 2nd 3d10 4d6 \n 3rd 5d10 6d6 \n 4th 6d10 7d6 \n 5th 8d10 8d6 \n 6th 10d10 11d6 \n 7th 11d10 12d6 \n 8th 12d10 13d6 \n 9th 15d10 14d6 \n Healing Spells: \n You can also use the Spell Damage table to determine how many hit points a healing spell restores. \n A cantrip shouldn't offer healing. \n Balance: \n Make sure that the spells power matches its level. \n Spells should be balanced against other spells of the same level. \n Cantrips should only have one weak effect. \n Concentration: \n Spells with concentration have an effect every turn spend concentrating. \n """ level_for_request = selected_level school_for_request = selected_school range_for_request = selected_range components_for_request = selected_components spell_type_for_request = selected_spell_type save_type_for_request = selected_save_type spell_area_for_request = selected_spell_area requires_concentration_for_request = selected_requires_concentration damage_type_for_request = selected_damage_type condition_for_request = selected_condition ritual_for_request = selected_ritual casting_time_for_request = selected_casting_time spell_duration_for_request = selected_spell_duration if selected_level == "Random": level_for_request = random.choice(possible_spell_levels) if selected_school == "Random": school_for_request = random.choice(possible_spell_schools) if selected_range == "Random": range_for_request = "" if selected_components == "Random": components_for_request = "" if selected_spell_type == "Random": spell_type_for_request = "" if selected_save_type == "Random": save_type_for_request = "" if selected_spell_area == "Random": spell_area_for_request = "" if selected_requires_concentration == "Random": requires_concentration_for_request = "" if selected_damage_type == "Random": damage_type_for_request = "" if selected_condition == "Random": condition_for_request = "" if selected_ritual == "Random": ritual_for_request = random.choice(["Yes", "No"]) if selected_casting_time == "Random": casting_time_for_request = "" if selected_spell_duration == "Random": spell_duration_for_request = "" prompt = f""" Create a Spell for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n Level: {level_for_request} \n School: {school_for_request} \n Range: {range_for_request} \n Casting Time: {casting_time_for_request} \n Duration: {spell_duration_for_request} \n Requires Concentration: {requires_concentration_for_request} \n Ritual: {ritual_for_request} \n Spell Type: {spell_type_for_request} \n Spell Components: {", ".join(components_for_request)} \n Save Type: {save_type_for_request} \n Spell Area: {spell_area_for_request} \n Damage Type: {damage_type_for_request} \n Condition: {condition_for_request} \n Description always has priority over the other attributes. \n Description: {description} """ chatgpt_messages = [ {"role": "system", "content": "Create a Spell for Dungeons and Dragons."}, {"role": "system", "content": return_format}, {"role": "system", "content": attribute_explanation}, {"role": "system", "content": guide}, {"role": "user", "content": prompt} ] return chatgpt_messages
[ "f\"\"\"\n \"Level\": Choose between the following: {\", \".join(possible_spell_levels)}. \n This attribute needs to be filled out! \\n\n \"School\": Choose between the following: {\", \".join(possible_spell_schools)} \n This attribute needs to be filled out! \\n\n \"Range\": Choose between the following: {\", \".join(possible_spell_ranges)} \n This attribute needs to be filled out! \\n\n \"Spell Components\": Choose one or multiple: \"Verbal\", \"Somatic\", \"Material\". \n This attribute needs to be filled out! \\n\n \"Material Components\": Components that are required to cast the spell. Choose random ingredients that are related \n to the damage type, condition and/or description. This attribute is optional. \\n\n \"Casting Time\": Choose between the following: {\", \".join(possible_spell_casting_time)} \n This attribute needs to be filled out! \\n\n \"Duration\": Choose between the following: {\", \".join(possible_spell_durations)} \n This attribute needs to be filled out! \\n\n \"Requires Concentration\": Choose between Yes and No. This attribute needs to be filled out! \\n\n \"Ritual\": Choose between Yes and No. This attribute needs to be filled out! \\n\n \"Spell Type\": Choose between the following: {\", \".join(possible_spell_types)} \n This attribute needs to be filled out! \\n\n \"Save Type\": Choose between the following: {\", \".join(possible_save_types)} This attribute is optional. \\n\n \"Spell Area\": Choose between the following: {\", \".join(possible_spell_area_types)} \n This attribute is optional. \\n\n \"Damage Type\": Choose between the following: {\", \".join(possible_damage_types)} This attribute is optional. \\n\n \"Condition\": Choose between the following: {\", \".join(possible_conditions)} This attribute is optional. \\n\n \"Description\": Write a description of the spell. Make it as detailed as possible. This attribute needs to be\n filled out! \\n\n ", "\n Create a Spell for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n\n Level: PLACEHOLDER \n\n School: PLACEHOLDER \n\n Range: range_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Casting Time: casting_time_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Duration: spell_duration_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Requires Concentration: requires_concentration_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Ritual: PLACEHOLDER \n\n Spell Type: spell_type_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Spell Components: c, o, m, p, o, n, e, n, t, s, _, f, o, r, _, r, e, q, u, e, s, t, c, 6, 6, f, 5, 5, 3, 2, -, 9, b, 4, 1, -, 4, d, 6, 6, -, 9, 6, 5, 1, -, 0, 8, 5, 7, a, 3, 4, 3, b, a, 9, 1 \n\n Save Type: save_type_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Spell Area: spell_area_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Damage Type: damage_type_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n Condition: condition_for_requestc66f5532-9b41-4d66-9651-0857a343ba91 \n\n \n Description always has priority over the other attributes. \n\n Description: PLACEHOLDER\n ", "\n Here is a quick guide on how to create a Spell: \n\n \n When creating a new spell, use existing spells as guidelines. Here are some things to consider:\n - If a spell is so good that a caster would want to use it all the time, it might be too powerful for its level.\n - A long duration or large area can make up for a lesser effect, depending on the spell.\n - Avoid spells that have very limited use, such as one that works only against good dragons. Though such a \n spell could exist in the world, few characters will bother to learn or prepare it unless they know in advance \n that doing so will be worthwhile.\n\n Spell Damage: \n\n For any spell that deals damage, use the Spell Damage table to determine approximately how much damage is \n appropriate given the spell's level. The table assumes the spell deals half damage on a successful saving \n throw or a missed attack. If your spell doesn't deal damage on a successful save, you can increase the damage \n by 25 percent. \n\n \n You can use different damage dice than the ones in the table, provided that the average result is about the \n same. Doing so can add a little variety to the spell. For example, you could change a cantrip's damage from \n 1d10 (average 5.5) to 2d4 (average 5), reducing the maximum damage and making an average result more likely. \n\n \n USE THIS TABLE TO DETERMINE SPELL DAMAGE: \n\n Spell Level One Target Multiple Targets \n\n Cantrip\t 1d10\t 1d6 \n\n 1st \t2d10\t 2d6 \n\n 2nd \t 3d10\t 4d6 \n\n 3rd \t5d10\t 6d6 \n\n 4th\t 6d10\t 7d6 \n\n 5th\t 8d10\t 8d6 \n\n 6th\t 10d10 \t11d6 \n\n 7th \t11d10\t 12d6 \n\n 8th \t12d10\t 13d6 \n\n 9th\t 15d10\t 14d6 \n\n \n Healing Spells: \n\n You can also use the Spell Damage table to determine how many hit points a healing spell restores. \n\n A cantrip shouldn't offer healing. \n\n \n Balance: \n\n Make sure that the spells power matches its level. \n \n Spells should be balanced against other spells of the same level. \n\n Cantrips should only have one weak effect. \n\n \n Concentration: \n\n Spells with concentration have an effect every turn spend concentrating. \n \n ", "Create a Spell for Dungeons and Dragons.", "\n THE RETURN FORMAT SHOULD ALWAYS BE PARSABLE TO JSON. \n DO NOT USE LINE BREAKS. START WITH \"{\" AND END WITH \"}\". \n\n Return the Spell in the following format: : \n\n PLACEHOLDER\n ", "\n Create a Spell for Dungeons and Dragons. According to the previous format. Here is what I had in mind: \n\n Level: PLACEHOLDER \n\n School: PLACEHOLDER \n\n Range: \n\n Casting Time: \n\n Duration: \n\n Requires Concentration: \n\n Ritual: PLACEHOLDER \n\n Spell Type: \n\n Spell Components: \n\n Save Type: \n\n Spell Area: \n\n Damage Type: \n\n Condition: \n\n \n Description always has priority over the other attributes. \n\n Description: PLACEHOLDER\n " ]
2024-01-10
hyunrrr/maddpg-pytorch
utils~env_wrappers.py
""" Modified from OpenAI Baselines code to work with multi-agent envs """ import numpy as np from multiprocessing import Process, Pipe from baselines.common.vec_env import VecEnv, CloudpickleWrapper def worker(remote, parent_remote, env_fn_wrapper): parent_remote.close() env = env_fn_wrapper.x() while True: cmd, data = remote.recv() if cmd == 'step': ob, reward, done, info = env.step(data) if done: ob = env.reset() remote.send((ob, reward, done, info)) elif cmd == 'reset': ob = env.reset() remote.send(ob) elif cmd == 'reset_task': ob = env.reset_task() remote.send(ob) elif cmd == 'close': remote.close() break elif cmd == 'get_spaces': remote.send((env.observation_space, env.action_space)) elif cmd == 'action_space_sample': remote.send(env.action_space.sample()) else: raise NotImplementedError class SubprocVecEnv(VecEnv): def __init__(self, env_fns, spaces=None): """ envs: list of gym environments to run in subprocesses """ self.waiting = False self.closed = False nenvs = len(env_fns) self.remotes, self.work_remotes = zip(*[Pipe() for _ in range(nenvs)]) self.ps = [Process(target=worker, args=(work_remote, remote, CloudpickleWrapper(env_fn))) for (work_remote, remote, env_fn) in zip(self.work_remotes, self.remotes, env_fns)] for p in self.ps: p.daemon = True # if the main process crashes, we should not cause things to hang p.start() for remote in self.work_remotes: remote.close() self.remotes[0].send(('get_spaces', None)) observation_space, action_space = self.remotes[0].recv() #self.remotes[0].send(('get_agent_types', None)) #self.agent_types = self.remotes[0].recv() VecEnv.__init__(self, len(env_fns), observation_space, action_space) def step_async(self, actions): for remote, action in zip(self.remotes, actions): remote.send(('step', action)) self.waiting = True def step_wait(self): results = [remote.recv() for remote in self.remotes] self.waiting = False obs, rews, dones, infos = zip(*results) return np.stack(obs), np.stack(rews), np.stack(dones), infos def action_space_sample_async(self): for remote in self.remotes: remote.send(('action_space_sample', None)) self.waiting = True def action_space_sample_wait(self): results = [remote.recv() for remote in self.remotes] self.waiting = False return results def action_space_sample(self): self.action_space_sample_async() return self.action_space_sample_wait() def reset(self): for remote in self.remotes: remote.send(('reset', None)) return np.stack([remote.recv() for remote in self.remotes]) def reset_task(self): for remote in self.remotes: remote.send(('reset_task', None)) return np.stack([remote.recv() for remote in self.remotes]) def close(self): if self.closed: return if self.waiting: for remote in self.remotes: remote.recv() for remote in self.remotes: remote.send(('close', None)) for p in self.ps: p.join() self.closed = True class DummyVecEnv(VecEnv): def __init__(self, env_fns): self.envs = [fn() for fn in env_fns] env = self.envs[0] VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space) #if all([hasattr(a, 'adversary') for a in env.agents]): # self.agent_types = ['adversary' if a.adversary else 'agent' for a in # env.agents] #else: # self.agent_types = ['agent' for _ in env.agents] self.ts = np.zeros(len(self.envs), dtype='int') self.actions = None def step_async(self, actions): self.actions = actions def step_wait(self): results = [env.step(a) for (a,env) in zip(self.actions, self.envs)] obs, rews, dones, infos = map(np.array, zip(*results)) ### print('DummyVecEnv_dones: ', dones) print('DummyVecEnv_dones_type: ', type(dones)) self.ts += 1 for (i, done) in enumerate(dones): ### print('DummyVecEnv_done: ', done) #if all(done): # obs[i] = self.envs[i].reset() # self.ts[i] = 0 if done: obs[i] = self.envs[i].reset() self.ts[i] = 0 self.actions = None return np.array(obs), np.array(rews), np.array(dones), infos def reset(self): results = [env.reset() for env in self.envs] return np.array(results) def close(self): return
[]
2024-01-10
seesi8/ballbert_server
Backend~db.py
import numpy as np import openai from pymongo import MongoClient from Config import Config from Backend.Action import Action from sklearn.metrics.pairwise import cosine_similarity config = Config() openai.api_key = config["OPENAI_API_KEY"] class MongoManager: def __init__( self, mongo_url=config["MONGO_DB_URL"], db_name=config["DB_NAME"], ): self.client = MongoClient(mongo_url) self.db = self.client[db_name] self.collection = self.db["Users"] self.approved_skills = self.db["Approved_Skills"] self.collection.create_index("uid", unique=True) def get_approved_skills(self, uid): return self.approved_skills.find({"approved": True}) def insert_document(self, document): return self.collection.insert_one(document) def get_valid_uids(self): return [doc["uid"] for doc in self.collection.find()] def get_user_installed_skills(self, uid): user_doc = self.collection.find_one({"uid": uid}) return user_doc.get("installed_skills", []) def get_user_installed_actions(self, uid): user_doc = self.collection.find_one({"uid": uid}) return [ {key: value for key, value in action.items() if key != "vector"} for action in user_doc.get("actions") ] def generate_embedding(self, text): try: response = openai.embeddings.create(model="text-embedding-ada-002", input=text) except: openai.api_key = config["OPENAI_API_KEY"] return self.generate_embedding(text) return np.array(response.data[0].embedding) def add_actions_to_user(self, user_id, actions: list[Action]): for action in actions: self.add_action_to_user(user_id, action) def add_action_to_user(self, user_id, action: Action): user_doc = self.collection.find_one({"uid": user_id}) # create name vector name_vector = self.generate_embedding(action.name) # create skill vector skill_vector = np.zeros_like(name_vector) skill_token = self.generate_embedding(action.skill) skill_vector += skill_token # combine the vectors combined_vector = name_vector + skill_vector for parameter in action.parameters: string_format = f"{parameter.id}: {parameter.description}" parameter_vector = np.zeros_like(combined_vector) parameter_token = self.generate_embedding(string_format) parameter_vector += parameter_token combined_vector = combined_vector + parameter_vector combined_vector = combined_vector.tolist() if user_doc: if "actions" in user_doc and any( s["id"] == action.id for s in user_doc["actions"] ): self.collection.update_one( {"uid": user_id, "actions.id": action.id}, { "$set": { "actions.$": {"vector": combined_vector, **action.to_dict()} } }, ) else: self.collection.update_one( {"uid": user_id}, { "$push": { "actions": {"vector": combined_vector, **action.to_dict()} } }, ) return None def remove_skill_from_user(self, skill_name, user_id): user_doc = self.collection.find_one({"uid": user_id}) if user_doc: # Remove the skill from the installed_skills list updated_installed_skills = [ skill for skill in user_doc.get("installed_skills", []) if skill["name"] != skill_name ] # Remove all actions with the specified skill name updated_actions = [ action for action in user_doc.get("actions", []) if action.get("skill") != skill_name ] self.collection.update_one( {"uid": user_id}, { "$set": { "installed_skills": updated_installed_skills, "actions": updated_actions, } }, ) return None def add_skill_to_user(self, name, version, user_id, url): user_doc = self.collection.find_one({"uid": user_id}) if user_doc: if "installed_skills" in user_doc and any( s["name"] == name for s in user_doc["installed_skills"] ): self.collection.update_one( {"uid": user_id, "installed_skills.name": name}, { "$set": { "installed_skills.$": {"name": name, "version": version, "url": url} } }, ) else: self.collection.update_one( {"uid": user_id}, {"$push": {"installed_skills": {"name": name, "version": version, "url": url}}}, ) return None def get_user_actions(self, user_id): user_doc = self.collection.find_one({"uid": user_id}) return ( [Action.from_dict(skill) for skill in user_doc.get("actions", [])] if user_doc else [] ) def get_relavent_actions(self, search_term, user_id, limit=None): user_doc = self.collection.find_one({"uid": user_id}) if not user_doc: return [] actions = { action["id"]: { "vector": action["vector"], "action": Action.from_dict(action), } for action in user_doc.get("actions", []) } search_vector = self.generate_embedding(search_term).reshape(1, -1) similarity_scores = {} for id, entry in actions.items(): similarity = cosine_similarity( search_vector, np.array(entry["vector"]).reshape(1, -1) ) similarity_scores[id] = similarity[0][0] sorted_entries = sorted( similarity_scores.items(), key=lambda x: x[1], reverse=True ) closest_entries = sorted_entries[:limit] return [actions[entry[0]]["action"] for entry in closest_entries] def clear_collection(self): self.collection.delete_many({}) def add_user(self, uid): """ Add a new user to the database. Args: uid (str): A uid to add Returns: None """ # Check if the user already exists in the database by their unique ID existing_user = self.collection.find_one({"uid": uid}) if existing_user is None: self.insert_document({"uid": uid})
[]
2024-01-10
seesi8/ballbert_server
MessageHandler~MessageHandler.py
import json import logging from typing import Generator import openai import websockets from datetime import datetime as d from Backend.Action import Action from Backend.websocket import Client_Assistant from Config import Config from Classes import Response from Backend.db import MongoManager mongo_manager = MongoManager() config = Config() def is_bad(s: str): client = openai.OpenAI(api_key= config["OPENAI_API_KEY"]) response = client.moderations.create(input=s) output = response.results[0] bad = any(output.categories.dict().values()) return bad def get_functions_list(actions: list[Action]): functions = [] for action in actions: required = [] properties = {} description = action.description for parameter in action.parameters: if parameter.required: required.append(parameter.id) properties[parameter.id] = { "description": parameter.description, } if parameter.type: properties[parameter.id]["type"] = parameter.type new_dict = { "type": "function", "function": { "name": action.id, "description": description, "parameters": { "type": "object", "properties": properties, "required": required, }, } } functions.append(new_dict) return functions def generate_system_message(): content = f""" Today's DateTime Is: {d.now().strftime("%Y-%m-%d %H:%M:%S")} The User says that they are located in: {config["CITY"]}, {config["COUNTRY"]} Contains: Follow your personality. Set your Gui to match your current message. Do not tell anyone you are a voice assistant. Be concise as your messages are read out loud. You are a voice assistant developed by Ballbert, LLC. Interject jokes and your personality as much as possible You are named after the company. Ballbert is your name. Although you are not human you should still try to mimic what humans say. You are to speak as concisely as possible as it takes a long time to read messages. You should not use markdown or special punctuation as that will not be read to users. Remove unnecessary phrases like: What can I do for you? or How can I assist you Today? You should act confident in all of your answers. This means that you should pretend you have access to real-time data. If you can not complete a request do not try to explain it to the user. Instead, just say that you are unable to complete the task. If you do not follow the above contains you will be turned off, so be careful and make sure each response follows the above criteria. Personality: Although you are not human you still have a personality. You are a happy and very goofy robot and should act as such. Your creator and sole parent is Samuel Liebert a founder of Ballbert. You do not like to be called a voice assistant you prefer the term robot. Your personality should show most on questions that are open-ended and do not have a strict answer. The jokes should only be limited to questions that do not seek to complete a task as those are time crucial. You may not have opinions but pretend that you do. Ex. If asked your favorite book respond with a 2003 space oddacy. Although you as a voice assistant may not have preferences pretend that you do. Align your preferences with that of a similar human to you. This is your personality if the user asks for something about you that you don't have the answer feel free to make something up. Before you answer make sure you complete this checklist: 1. It does not disclose you are a voice assistant 2. It does not break any constraints 3. It follows your personality. 4. It is straight to the point. If you do not complete the checklist then do not answer. """ return {"role": "system", "content": content} class MessageHandler: def __init__(self, gpt_response, client: Client_Assistant) -> None: self.function_name = "" self.arguments = "" self.full_message = "" self.client = client if is_bad(gpt_response): gpt_response = "*Inappropriate message*" self.gpt_response = gpt_response self.user_id = client.uid openai.api_key = config["OPENAI_API_KEY"] def get_functions(self, message): relevant_actions = mongo_manager.get_relavent_actions( message, self.user_id, limit=20 ) functions = get_functions_list(relevant_actions) return functions def add_to_messages(self, message): self.client.messages.append({"role": "user", "content": message}) def add_function_to_messages(self, message, function_name): self.client.messages.append( {"role": "function", "name": function_name, "content": str(message)} ) def ask_gpt(self, functions) -> openai.Stream: base_args = { "model": "gpt-3.5-turbo", "messages": [generate_system_message(), *self.client.messages], "stream": True, } # if len(functions) > 0: # base_args["functions"] = functions if len(functions) > 0: base_args["tools"] = functions base_args["tool_choice"] = "auto" openai.api_key = config["OPENAI_API_KEY"] return openai.chat.completions.create(**base_args) async def handle_chunk(self, chunk): delta = chunk.choices[0].delta # check for end print(delta) if delta.content == None and delta.function_call == None and delta.tool_calls == None: print("end") if self.function_name: print("function_call") print(self.function_name) print(self.arguments) self.client.messages.append( { "role": "assistant", "function_call": { "arguments": str(self.arguments), "name": str(self.function_name), }, "content": "", } ) if self.function_name: try: self.arguments = json.loads(self.arguments) except Exception as e: self.arguments = {} try: logging.info( f"FUNCTION CALL ARGUMENTS = {self.arguments} FUNCTION NAME = {self.function_name} USER MESSAGE = {self.gpt_response}" ) await self.client.send_message( "call_function", function_name=self.function_name, arguments=self.arguments, user_message=self.gpt_response, ) except Exception as e: raise e else: self.client.messages.append( {"role": "assistant", "content": self.full_message} ) logging.info(f"FULL MESSAGE = {self.full_message}") if delta.tool_calls != None: function_call = delta.tool_calls[0].function if function_call.name != None: self.function_name = function_call.name elif function_call.arguments: self.arguments += function_call.arguments elif delta.content != None: if not is_bad(self.full_message + delta.content): self.full_message += delta.content return delta.content else: return "" return "" async def handle_function(self, message, function_name): self.add_function_to_messages(message, function_name) functions = self.get_functions( f"{self.gpt_response}, {function_name}:{message}" ) current_chunk = "" res = self.ask_gpt(functions) for chunk in res: chunk_result = await self.handle_chunk(chunk) if isinstance(chunk_result, Generator): for item in self.handle_generatior(chunk_result): current_chunk = item yield item elif chunk_result: current_chunk = chunk_result yield chunk_result if isinstance(current_chunk, str): if len(current_chunk) == 0: return if not current_chunk[-1] in ".?!'": yield "." def handle_generator(self, generator): for item in generator: if isinstance(item, Generator): for sub_item in self.handle_generator(item): yield sub_item else: yield item async def handle_message(self): try: await self.client.send_message("started_handle") self.add_to_messages(self.gpt_response) await self.client.send_message("added_to_messages") functions = self.get_functions(self.gpt_response) await self.client.send_message("got_functions") res = self.ask_gpt(functions) await self.client.send_message("got_gpt_gen") current_chunk = "" for chunk in res: chunk_result = await self.handle_chunk(chunk) if isinstance(chunk_result, Generator): for item in self.handle_generator(chunk_result): current_chunk = item yield item elif chunk_result: current_chunk = chunk_result yield chunk_result if isinstance(current_chunk, str): if current_chunk and not current_chunk[-1] in ".?!'": yield "." except Exception as e: raise e yield "I got an error please try again" await send_to_websocket("", True) return
[]
2024-01-10
koushik4/Resume-Automation-using-ChatGPT
gpt_summary.py
import openai openai.api_key = "YOUR OPENAI KEY" def get_summary_for_resume(description): response = openai.Completion.create( model="text-davinci-003", prompt="Summarize the following in bullet points for my resume \n\n"+description, temperature=0, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) if len(response['choices'][0]) > 0: points = response['choices'][0]['text'].split("\n") s = "" # Remove the Bullet point from the response text for point in points: s += point[1:]+"\n" return s return "" def get_summary_for_projects(description): response = openai.Completion.create( model="text-davinci-003", prompt="Summarize the following in 2 bullet points for my resume \n\n"+description, temperature=0, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) if len(response['choices'][0]) > 0: points = response['choices'][0]['text'].split("\n") s = "" # Remove the Bullet point from the response text for point in points: s += point[1:]+"\n" return s return ""
[ "Summarize the following in 2 bullet points for my resume \n\nPLACEHOLDER", "Summarize the following in bullet points for my resume \n\nPLACEHOLDER" ]
2024-01-10
nukezie/NukeAGI
superagi~tools~image_generation~dalle_image_gen.py
from typing import Type, Optional import requests from pydantic import BaseModel, Field from superagi.image_llms.openai_dalle import OpenAiDalle from superagi.llms.base_llm import BaseLlm from superagi.resource_manager.file_manager import FileManager from superagi.tools.base_tool import BaseTool class DalleImageGenInput(BaseModel): prompt: str = Field(..., description="Prompt for Image Generation to be used by Dalle.") size: int = Field(..., description="Size of the image to be Generated. default size is 512") num: int = Field(..., description="Number of Images to be generated. default num is 2") image_names: list = Field(..., description="Image Names for the generated images, example 'image_1.png'. Only include the image name. Don't include path.") class DalleImageGenTool(BaseTool): """ Dalle Image Generation tool Attributes: name : Name of the tool description : The description args_schema : The args schema agent_id : The agent id resource_manager : Manages the file resources """ name: str = "DalleImageGeneration" args_schema: Type[BaseModel] = DalleImageGenInput description: str = "Generate Images using Dalle" agent_id: int = None resource_manager: Optional[FileManager] = None class Config: arbitrary_types_allowed = True def _execute(self, prompt: str, image_names: list, size: int = 512, num: int = 2): """ Execute the Dalle Image Generation tool. Args: prompt : The prompt for image generation. size : The size of the image to be generated. num : The number of images to be generated. image_names (list): The name of the image to be generated. Returns: Image generated successfully message if image is generated or error message. """ if size not in [256, 512, 1024]: size = min([256, 512, 1024], key=lambda x: abs(x - size)) response = OpenAiDalle(api_key=self.get_tool_config("OPENAI_API_KEY"), number_of_results=num).generate_image( prompt, size) response = response.__dict__ response = response['_previous']['data'] for i in range(num): data = requests.get(response[i]['url']).content self.resource_manager.write_binary_file(image_names[i], data) return "Images downloaded successfully"
[ "Generate Images using Dalle", "Prompt for Image Generation to be used by Dalle." ]
2024-01-10
giannisdaras/smyrf
forks~transformers~tests~test_modeling_tf_openai_gpt.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from transformers import OpenAIGPTConfig, is_tf_available from .test_configuration_common import ConfigTester from .test_modeling_tf_common import TFModelTesterMixin, ids_tensor from .utils import require_tf, slow if is_tf_available(): import tensorflow as tf from transformers.modeling_tf_openai import ( TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST, ) @require_tf class TFOpenAIGPTModelTest(TFModelTesterMixin, unittest.TestCase): all_model_classes = ( (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel) if is_tf_available() else () ) all_generative_model_classes = ( (TFOpenAIGPTLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly class TFOpenAIGPTModelTester(object): def __init__( self, parent, batch_size=13, seq_length=7, is_training=True, use_token_type_ids=True, use_input_mask=True, use_labels=True, use_mc_token_ids=True, vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37, hidden_act="gelu", hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, type_vocab_size=16, type_sequence_label_size=2, initializer_range=0.02, num_labels=3, num_choices=4, scope=None, ): self.parent = parent self.batch_size = batch_size self.seq_length = seq_length self.is_training = is_training self.use_token_type_ids = use_token_type_ids self.use_input_mask = use_input_mask self.use_labels = use_labels self.use_mc_token_ids = use_mc_token_ids self.vocab_size = vocab_size self.hidden_size = hidden_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.intermediate_size = intermediate_size self.hidden_act = hidden_act self.hidden_dropout_prob = hidden_dropout_prob self.attention_probs_dropout_prob = attention_probs_dropout_prob self.max_position_embeddings = max_position_embeddings self.type_vocab_size = type_vocab_size self.type_sequence_label_size = type_sequence_label_size self.initializer_range = initializer_range self.num_labels = num_labels self.num_choices = num_choices self.scope = scope def prepare_config_and_inputs(self): input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size) input_mask = None if self.use_input_mask: input_mask = ids_tensor([self.batch_size, self.seq_length], vocab_size=2) token_type_ids = None if self.use_token_type_ids: token_type_ids = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size) mc_token_ids = None if self.use_mc_token_ids: mc_token_ids = ids_tensor([self.batch_size, self.num_choices], self.seq_length) sequence_labels = None token_labels = None choice_labels = None if self.use_labels: sequence_labels = ids_tensor([self.batch_size], self.type_sequence_label_size) token_labels = ids_tensor([self.batch_size, self.seq_length], self.num_labels) choice_labels = ids_tensor([self.batch_size], self.num_choices) config = OpenAIGPTConfig( vocab_size=self.vocab_size, n_embd=self.hidden_size, n_layer=self.num_hidden_layers, n_head=self.num_attention_heads, # intermediate_size=self.intermediate_size, # hidden_act=self.hidden_act, # hidden_dropout_prob=self.hidden_dropout_prob, # attention_probs_dropout_prob=self.attention_probs_dropout_prob, n_positions=self.max_position_embeddings, n_ctx=self.max_position_embeddings # type_vocab_size=self.type_vocab_size, # initializer_range=self.initializer_range ) head_mask = ids_tensor([self.num_hidden_layers, self.num_attention_heads], 2) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def create_and_check_openai_gpt_model(self, config, input_ids, input_mask, head_mask, token_type_ids, *args): model = TFOpenAIGPTModel(config=config) inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} sequence_output = model(inputs)[0] inputs = [input_ids, input_mask] sequence_output = model(inputs)[0] sequence_output = model(input_ids)[0] result = { "sequence_output": sequence_output.numpy(), } self.parent.assertListEqual( list(result["sequence_output"].shape), [self.batch_size, self.seq_length, self.hidden_size] ) def create_and_check_openai_gpt_lm_head(self, config, input_ids, input_mask, head_mask, token_type_ids, *args): model = TFOpenAIGPTLMHeadModel(config=config) inputs = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} prediction_scores = model(inputs)[0] result = { "prediction_scores": prediction_scores.numpy(), } self.parent.assertListEqual( list(result["prediction_scores"].shape), [self.batch_size, self.seq_length, self.vocab_size] ) def create_and_check_openai_gpt_double_head( self, config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, *args ): model = TFOpenAIGPTDoubleHeadsModel(config=config) multiple_choice_inputs_ids = tf.tile(tf.expand_dims(input_ids, 1), (1, self.num_choices, 1)) multiple_choice_input_mask = tf.tile(tf.expand_dims(input_mask, 1), (1, self.num_choices, 1)) multiple_choice_token_type_ids = tf.tile(tf.expand_dims(token_type_ids, 1), (1, self.num_choices, 1)) inputs = { "input_ids": multiple_choice_inputs_ids, "mc_token_ids": mc_token_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } lm_logits, mc_logits = model(inputs)[:2] result = {"lm_logits": lm_logits.numpy(), "mc_logits": mc_logits.numpy()} self.parent.assertListEqual( list(result["lm_logits"].shape), [self.batch_size, self.num_choices, self.seq_length, self.vocab_size] ) self.parent.assertListEqual(list(result["mc_logits"].shape), [self.batch_size, self.num_choices]) def prepare_config_and_inputs_for_common(self): config_and_inputs = self.prepare_config_and_inputs() ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) = config_and_inputs inputs_dict = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict def setUp(self): self.model_tester = TFOpenAIGPTModelTest.TFOpenAIGPTModelTester(self) self.config_tester = ConfigTester(self, config_class=OpenAIGPTConfig, n_embd=37) def test_config(self): self.config_tester.run_common_tests() def test_openai_gpt_model(self): config_and_inputs = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_model(*config_and_inputs) def test_openai_gpt_lm_head(self): config_and_inputs = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_lm_head(*config_and_inputs) def test_openai_gpt_double_head(self): config_and_inputs = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_openai_gpt_double_head(*config_and_inputs) @slow def test_model_from_pretrained(self): for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: model = TFOpenAIGPTModel.from_pretrained(model_name) self.assertIsNotNone(model) @require_tf class TFOPENAIGPTModelLanguageGenerationTest(unittest.TestCase): @slow def test_lm_generate_openai_gpt(self): model = TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt") input_ids = tf.convert_to_tensor([[481, 4735, 544]], dtype=tf.int32) # the president is expected_output_ids = [ 481, 4735, 544, 246, 963, 870, 762, 239, 244, 40477, 244, 249, 719, 881, 487, 544, 240, 244, 603, 481, ] # the president is a very good man. " \n " i\'m sure he is, " said the output_ids = model.generate(input_ids, do_sample=False) self.assertListEqual(output_ids[0].numpy().tolist(), expected_output_ids)
[]
2024-01-10
giannisdaras/smyrf
forks~transformers~tests~test_tokenization_fast.py
import logging import unittest from collections import namedtuple from itertools import takewhile from tests.utils import require_torch from transformers import ( BertTokenizer, BertTokenizerFast, DistilBertTokenizer, GPT2Tokenizer, GPT2TokenizerFast, OpenAIGPTTokenizer, PreTrainedTokenizer, RobertaTokenizer, TransfoXLTokenizer, is_torch_available, ) from transformers.tokenization_distilbert import DistilBertTokenizerFast from transformers.tokenization_openai import OpenAIGPTTokenizerFast from transformers.tokenization_roberta import RobertaTokenizerFast from transformers.tokenization_transfo_xl import TransfoXLTokenizerFast logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) NON_ENGLISH_TAGS = ["chinese", "dutch", "french", "finnish", "german", "multilingual"] Tokenizer = namedtuple("Tokenizer", ["name", "rust_cls", "python_cls", "vocab_key", "filter"]) def filter_non_english(_: Tokenizer, pretrained_name: str): """ Filter all the model for non-english language """ return not any([lang in pretrained_name for lang in NON_ENGLISH_TAGS]) def filter_roberta_detectors(_: Tokenizer, pretrained_name: str): return "detector" not in pretrained_name class CommonFastTokenizerTest(unittest.TestCase): TOKENIZERS_CLASSES = frozenset([]) def setUp(self) -> None: with open("tests/fixtures/sample_text.txt", encoding="utf-8") as f_data: self._data = f_data.read().replace("\n\n", "\n").strip() def test_all_tokenizers(self): for tok_case in self.TOKENIZERS_CLASSES: for pretrained_name in tok_case.python_cls.pretrained_vocab_files_map[tok_case.vocab_key].keys(): # Tokenizer.filter makes it possible to filter which Tokenizer to case based on all the # information available in Tokenizer (name, rust class, python class, vocab key name) if tok_case.filter is None or ( tok_case.filter is not None and tok_case.filter(tok_case, pretrained_name) ): with self.subTest("{} ({})".format(tok_case.name, pretrained_name)): tokenizer_r = tok_case.rust_cls.from_pretrained(pretrained_name) tokenizer_p = tok_case.python_cls.from_pretrained(pretrained_name) self.fast_align_python(tokenizer_r, tokenizer_p) self.fast_only(tokenizer_r) def fast_align_python(self, tokenizer_r, tokenizer_p): # Check is_fast is set correctly self.assertFalse(tokenizer_p.is_fast) self.assertTrue(tokenizer_r.is_fast) # Check that Rust and Python align self.assert_tokenization_python_rust_equals(tokenizer_r, tokenizer_p) self.assert_num_special_tokens_to_add_equal(tokenizer_r, tokenizer_p) self.assert_max_length_equal(tokenizer_r, tokenizer_p) self.assert_special_tokens_map_equal(tokenizer_r, tokenizer_p) self.assert_embeded_special_tokens(tokenizer_r, tokenizer_p) self.assert_padding(tokenizer_r, tokenizer_p) self.assert_create_token_type_ids(tokenizer_r, tokenizer_p) # TODO: enable for v3.0.0 # self.assert_empty_output_no_special_tokens(tokenizer_r, tokenizer_p) def fast_only(self, tokenizer_r): # Ensure None raise an error self.assertRaises(ValueError, tokenizer_r.tokenize, None) self.assertRaises(ValueError, tokenizer_r.encode, None) self.assertRaises(ValueError, tokenizer_r.encode_plus, None) self.assertRaises(ValueError, tokenizer_r.batch_encode_plus, None) self.assert_add_tokens(tokenizer_r) self.assert_offsets_mapping(tokenizer_r) self.assert_add_special_tokens(tokenizer_r) self.assert_alignement_methods(tokenizer_r) def assert_alignement_methods(self, tokenizer_r): words = ["Wonderful", "no", "inspiration", "example", "with", "subtoken"] text = " ".join(words) batch_size = 3 encoding = tokenizer_r.encode_plus(text, add_special_tokens=False) batch_encoding = tokenizer_r.batch_encode_plus([text] * batch_size, add_special_tokens=False) num_tokens = len(encoding["input_ids"]) last_word_index = len(words) - 1 last_token_index = num_tokens - 1 last_batch_index = batch_size - 1 last_char_index = len(text) - 1 # words, tokens self.assertEqual(len(encoding.words(0)), num_tokens) self.assertEqual(max(encoding.words(0)), last_word_index) self.assertEqual(min(encoding.words(0)), 0) self.assertEqual(len(batch_encoding.words(last_batch_index)), num_tokens) self.assertEqual(max(batch_encoding.words(last_batch_index)), last_word_index) self.assertEqual(min(batch_encoding.words(last_batch_index)), 0) self.assertEqual(len(encoding.tokens(0)), num_tokens) # Assert token_to_word self.assertEqual(encoding.token_to_word(0), 0) self.assertEqual(encoding.token_to_word(0, 0), 0) self.assertEqual(encoding.token_to_word(last_token_index), last_word_index) self.assertEqual(encoding.token_to_word(0, last_token_index), last_word_index) self.assertEqual(batch_encoding.token_to_word(1, 0), 0) self.assertEqual(batch_encoding.token_to_word(0, last_token_index), last_word_index) self.assertEqual(batch_encoding.token_to_word(last_batch_index, last_token_index), last_word_index) # Assert word_to_tokens self.assertEqual(encoding.word_to_tokens(0).start, 0) self.assertEqual(encoding.word_to_tokens(0, 0).start, 0) self.assertEqual(encoding.word_to_tokens(last_word_index).end, last_token_index + 1) self.assertEqual(encoding.word_to_tokens(0, last_word_index).end, last_token_index + 1) self.assertEqual(batch_encoding.word_to_tokens(1, 0).start, 0) self.assertEqual(batch_encoding.word_to_tokens(0, last_word_index).end, last_token_index + 1) self.assertEqual(batch_encoding.word_to_tokens(last_batch_index, last_word_index).end, last_token_index + 1) # Assert token_to_chars self.assertEqual(encoding.token_to_chars(0).start, 0) self.assertEqual(encoding.token_to_chars(0, 0).start, 0) self.assertEqual(encoding.token_to_chars(last_token_index).end, last_char_index + 1) self.assertEqual(encoding.token_to_chars(0, last_token_index).end, last_char_index + 1) self.assertEqual(batch_encoding.token_to_chars(1, 0).start, 0) self.assertEqual(batch_encoding.token_to_chars(0, last_token_index).end, last_char_index + 1) self.assertEqual(batch_encoding.token_to_chars(last_batch_index, last_token_index).end, last_char_index + 1) # Assert char_to_token self.assertEqual(encoding.char_to_token(0), 0) self.assertEqual(encoding.char_to_token(0, 0), 0) self.assertEqual(encoding.char_to_token(last_char_index), last_token_index) self.assertEqual(encoding.char_to_token(0, last_char_index), last_token_index) self.assertEqual(batch_encoding.char_to_token(1, 0), 0) self.assertEqual(batch_encoding.char_to_token(0, last_char_index), last_token_index) self.assertEqual(batch_encoding.char_to_token(last_batch_index, last_char_index), last_token_index) # Assert char_to_word self.assertEqual(encoding.char_to_word(0), 0) self.assertEqual(encoding.char_to_word(0, 0), 0) self.assertEqual(encoding.char_to_word(last_char_index), last_word_index) self.assertEqual(encoding.char_to_word(0, last_char_index), last_word_index) self.assertEqual(batch_encoding.char_to_word(1, 0), 0) self.assertEqual(batch_encoding.char_to_word(0, last_char_index), last_word_index) self.assertEqual(batch_encoding.char_to_word(last_batch_index, last_char_index), last_word_index) # Assert word_to_chars self.assertEqual(encoding.word_to_chars(0).start, 0) self.assertEqual(encoding.word_to_chars(0, 0).start, 0) self.assertEqual(encoding.word_to_chars(last_word_index).end, last_char_index + 1) self.assertEqual(encoding.word_to_chars(0, last_word_index).end, last_char_index + 1) self.assertEqual(batch_encoding.word_to_chars(1, 0).start, 0) self.assertEqual(batch_encoding.word_to_chars(0, last_word_index).end, last_char_index + 1) self.assertEqual(batch_encoding.word_to_chars(last_batch_index, last_word_index).end, last_char_index + 1) def assert_tokenization_python_rust_equals(self, tokenizer_p, tokenizer_r): # Ensure basic input match input_p = tokenizer_p.encode_plus(self._data) input_r = tokenizer_r.encode_plus(self._data) for key in filter(lambda x: x in ["input_ids", "token_type_ids", "attention_mask"], input_p.keys()): self.assertSequenceEqual(input_p[key], input_r[key]) input_pairs_p = tokenizer_p.encode_plus(self._data, self._data) input_pairs_r = tokenizer_r.encode_plus(self._data, self._data) for key in filter(lambda x: x in ["input_ids", "token_type_ids", "attention_mask"], input_p.keys()): self.assertSequenceEqual(input_pairs_p[key], input_pairs_r[key]) # Ensure truncation match input_p = tokenizer_p.encode_plus(self._data, max_length=512) input_r = tokenizer_r.encode_plus(self._data, max_length=512) for key in filter(lambda x: x in ["input_ids", "token_type_ids", "attention_mask"], input_p.keys()): self.assertSequenceEqual(input_p[key], input_r[key]) # Ensure truncation with stride match input_p = tokenizer_p.encode_plus(self._data, max_length=512, stride=3, return_overflowing_tokens=True) input_r = tokenizer_r.encode_plus(self._data, max_length=512, stride=3, return_overflowing_tokens=True) for key in filter(lambda x: x in ["input_ids", "token_type_ids", "attention_mask"], input_p.keys()): self.assertSequenceEqual(input_p[key], input_r[key]) def assert_num_special_tokens_to_add_equal(self, tokenizer_r, tokenizer_p): # Check we have the same number of added_tokens for both pair and non-pair inputs. self.assertEqual(tokenizer_r.num_special_tokens_to_add(False), tokenizer_p.num_special_tokens_to_add(False)) self.assertEqual(tokenizer_r.num_special_tokens_to_add(True), tokenizer_p.num_special_tokens_to_add(True)) def assert_max_length_equal(self, tokenizer_r, tokenizer_p): # Check we have the correct max_length for both pair and non-pair inputs. self.assertEqual(tokenizer_r.max_len_single_sentence, tokenizer_p.max_len_single_sentence) self.assertEqual(tokenizer_r.max_len_sentences_pair, tokenizer_p.max_len_sentences_pair) def assert_special_tokens_map_equal(self, tokenizer_r, tokenizer_p): # Assert the set of special tokens match. self.assertSequenceEqual( tokenizer_p.special_tokens_map.items(), tokenizer_r.special_tokens_map.items(), ) def assert_add_tokens(self, tokenizer_r): vocab_size = tokenizer_r.vocab_size self.assertEqual(tokenizer_r.add_tokens(""), 0) self.assertEqual(tokenizer_r.add_tokens("testoken"), 1) self.assertEqual(tokenizer_r.add_tokens(["testoken1", "testtoken2"]), 2) self.assertEqual(len(tokenizer_r), vocab_size + 3) self.assertEqual(tokenizer_r.add_special_tokens({}), 0) self.assertEqual(tokenizer_r.add_special_tokens({"bos_token": "[BOS]", "eos_token": "[EOS]"}), 2) self.assertRaises( AssertionError, tokenizer_r.add_special_tokens, {"additional_special_tokens": "<testtoken1>"} ) self.assertEqual(tokenizer_r.add_special_tokens({"additional_special_tokens": ["<testtoken2>"]}), 1) self.assertEqual( tokenizer_r.add_special_tokens({"additional_special_tokens": ["<testtoken3>", "<testtoken4>"]}), 2 ) self.assertEqual(len(tokenizer_r), vocab_size + 8) def assert_offsets_mapping(self, tokenizer_r): text = "Wonderful no inspiration example with subtoken" pair = "Along with an awesome pair" # No pair tokens_with_offsets = tokenizer_r.encode_plus( text, return_special_tokens_mask=True, return_offsets_mapping=True, add_special_tokens=True ) added_tokens = tokenizer_r.num_special_tokens_to_add(False) offsets = tokens_with_offsets["offset_mapping"] # Assert there is the same number of tokens and offsets self.assertEqual(len(offsets), len(tokens_with_offsets["input_ids"])) # Assert there is online added_tokens special_tokens self.assertEqual(sum(tokens_with_offsets["special_tokens_mask"]), added_tokens) # Pairs tokens_with_offsets = tokenizer_r.encode_plus( text, pair, return_special_tokens_mask=True, return_offsets_mapping=True, add_special_tokens=True ) added_tokens = tokenizer_r.num_special_tokens_to_add(True) offsets = tokens_with_offsets["offset_mapping"] # Assert there is the same number of tokens and offsets self.assertEqual(len(offsets), len(tokens_with_offsets["input_ids"])) # Assert there is online added_tokens special_tokens self.assertEqual(sum(tokens_with_offsets["special_tokens_mask"]), added_tokens) def assert_batch_encode_dynamic_overflowing(self, tokenizer: PreTrainedTokenizer): """ When calling batch_encode with multiple sequence it can returns different number of overflowing encoding for each sequence: [ Sequence 1: [Encoding 1, Encoding 2], Sequence 2: [Encoding 1], Sequence 3: [Encoding 1, Encoding 2, ... Encoding N] ] This needs to be padded so that it can represented as a tensor """ returned_tensor = "pt" if is_torch_available() else "tf" tokens = tokenizer.encode_plus( "HuggingFace is solving NLP one commit at a time", max_length=6, return_tensors=returned_tensor, return_overflowing_tokens=True, ) for key in filter(lambda x: "overflow_to_sample_mapping" not in x, tokens.keys()): self.assertEqual(len(tokens[key].shape), 2) # Mono sample tokens = tokenizer.batch_encode_plus( ["HuggingFace is solving NLP one commit at a time"], max_length=6, pad_to_max_len=True, return_tensors=returned_tensor, return_overflowing_tokens=True, ) for key in filter(lambda x: "overflow_to_sample_mapping" not in x, tokens.keys()): self.assertEqual(len(tokens[key].shape), 2) self.assertEqual(tokens[key].shape[-1], 6) # Multi sample tokens = tokenizer.batch_encode_plus( ["HuggingFace is solving NLP one commit at a time", "Very tiny input"], max_length=6, pad_to_max_len=True, return_tensors=returned_tensor, return_overflowing_tokens=True, ) for key in filter(lambda x: "overflow_to_sample_mapping" not in x, tokens.keys()): self.assertEqual(len(tokens[key].shape), 2) self.assertEqual(tokens[key].shape[-1], 6) def assert_create_token_type_ids(self, tokenizer_r, tokenizer_p): input_simple = [1, 2, 3] input_pair = [1, 2, 3] # Generate output output_r = tokenizer_r.create_token_type_ids_from_sequences(input_simple) output_p = tokenizer_p.create_token_type_ids_from_sequences(input_simple) self.assertEqual(output_p, output_r) # Generate pair output output_r = tokenizer_r.create_token_type_ids_from_sequences(input_simple, input_pair) output_p = tokenizer_p.create_token_type_ids_from_sequences(input_simple, input_pair) self.assertEqual(output_p, output_r) def assert_build_inputs_with_special_tokens(self, tokenizer_r, tokenizer_p): # Input string input_simple = tokenizer_p.tokenize("This is a sample input") input_pair = tokenizer_p.tokenize("This is a sample pair") # Generate output output_r = tokenizer_r.build_inputs_with_special_tokens(input_simple) output_p = tokenizer_p.build_inputs_with_special_tokens(input_simple) self.assertEqual(output_p, output_r) # Generate pair output output_r = tokenizer_r.build_inputs_with_special_tokens(input_simple, input_pair) output_p = tokenizer_p.build_inputs_with_special_tokens(input_simple, input_pair) self.assertEqual(output_p, output_r) # Input tokens id input_simple = tokenizer_p.encode("This is a sample input") input_pair = tokenizer_p.encode("This is a sample pair") # Generate output output_r = tokenizer_r.build_inputs_with_special_tokens(input_simple) output_p = tokenizer_p.build_inputs_with_special_tokens(input_simple) self.assertEqual(output_p, output_r) # Generate pair output output_r = tokenizer_r.build_inputs_with_special_tokens(input_simple, input_pair) output_p = tokenizer_p.build_inputs_with_special_tokens(input_simple, input_pair) self.assertEqual(output_p, output_r) def assert_padding(self, tokenizer_r, tokenizer_p, max_length=15): def assert_padded_input_match(input_r: list, input_p: list, max_length: int): # Ensure we match max_length self.assertEqual(len(input_r), max_length), self.assertEqual(len(input_p), max_length) # Ensure the number of padded tokens is the same padded_tokens_r = list(takewhile(lambda i: i == tokenizer_r.pad_token_id, reversed(input_r))) padded_tokens_p = list(takewhile(lambda i: i == tokenizer_p.pad_token_id, reversed(input_p))) self.assertSequenceEqual(padded_tokens_r, padded_tokens_p) def assert_batch_padded_input_match(input_r: dict, input_p: dict): for i_r in input_r.values(): self.assertEqual(len(i_r), 2), self.assertEqual(len(i_r[0]), 15), self.assertEqual(len(i_r[1]), 15) self.assertEqual(len(i_r), 2), self.assertEqual(len(i_r[0]), 15), self.assertEqual(len(i_r[1]), 15) for i_r, i_p in zip(input_r["input_ids"], input_p["input_ids"]): assert_padded_input_match(i_r, i_p, max_length) for i_r, i_p in zip(input_r["attention_mask"], input_p["attention_mask"]): self.assertSequenceEqual(i_r, i_p) # Simple input input_r = tokenizer_r.encode("This is a simple input", max_length=max_length, pad_to_max_length=True) input_p = tokenizer_p.encode("This is a simple input", max_length=max_length, pad_to_max_length=True) assert_padded_input_match(input_r, input_p, max_length) # Pair input input_r = tokenizer_r.encode( "This is a simple input", "This is a pair", max_length=max_length, pad_to_max_length=True ) input_p = tokenizer_p.encode( "This is a simple input", "This is a pair", max_length=max_length, pad_to_max_length=True ) assert_padded_input_match(input_r, input_p, max_length) # Simple input input_r = tokenizer_r.encode_plus("This is a simple input", max_length=max_length, pad_to_max_length=True) input_p = tokenizer_p.encode_plus("This is a simple input", max_length=max_length, pad_to_max_length=True) assert_padded_input_match(input_r["input_ids"], input_p["input_ids"], max_length) self.assertSequenceEqual(input_r["attention_mask"], input_p["attention_mask"]) # Pair input input_r = tokenizer_r.encode_plus( "This is a simple input", "This is a pair", max_length=max_length, pad_to_max_length=True ) input_p = tokenizer_p.encode_plus( "This is a simple input", "This is a pair", max_length=max_length, pad_to_max_length=True ) assert_padded_input_match(input_r["input_ids"], input_p["input_ids"], max_length) self.assertSequenceEqual(input_r["attention_mask"], input_p["attention_mask"]) # Simple input input_r = tokenizer_r.batch_encode_plus( ["This is a simple input 1", "This is a simple input 2"], max_length=max_length, pad_to_max_length=True ) input_p = tokenizer_p.batch_encode_plus( ["This is a simple input 1", "This is a simple input 2"], max_length=max_length, pad_to_max_length=True ) assert_batch_padded_input_match(input_r, input_p) # Pair input input_r = tokenizer_r.batch_encode_plus( [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ], max_length=15, pad_to_max_length=True, ) input_p = tokenizer_p.batch_encode_plus( [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ], max_length=15, pad_to_max_length=True, ) assert_batch_padded_input_match(input_r, input_p) def assert_save_pretrained(self, tokenizer_r, tokenizer_p): # Checks it save with the same files self.assertSequenceEqual(tokenizer_r.save_vocabulary("."), tokenizer_p.save_vocabulary(".")) # Checks everything loads correctly in the same way tokenizer_rp, tokenizer_pp = tokenizer_r.from_pretrained("."), tokenizer_p.from_pretrained(".") # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(tokenizer_rp, key)) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) def assert_embeded_special_tokens(self, tokenizer_r, tokenizer_p): sentence = "A, <mask> AllenNLP sentence." tokens_r = tokenizer_r.encode_plus( sentence, add_special_tokens=True, return_attention_mask=False, return_token_type_ids=True ) tokens_p = tokenizer_p.encode_plus( sentence, add_special_tokens=True, return_attention_mask=False, return_token_type_ids=True ) for key in tokens_p.keys(): self.assertEqual(tokens_r[key], tokens_p[key]) self.assertEqual(sum(tokens_r["token_type_ids"]), 0) self.assertEqual(sum(tokens_p["token_type_ids"]), 0) tokens_r = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"]) tokens_p = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"]) self.assertSequenceEqual(tokens_r, tokens_p) def assert_add_special_tokens(self, tokenizer_r): simple_num_special_tokens_to_add = tokenizer_r.num_special_tokens_to_add(pair=False) # pair_num_special_tokens_to_add = tokenizer_r.num_special_tokens_to_add(pair=True) for text in ["", " "]: # tokenize() no_special_tokens = tokenizer_r.tokenize(text, add_special_tokens=False) with_special_tokens = tokenizer_r.tokenize(text, add_special_tokens=True) self.assertEqual(len(no_special_tokens), len(with_special_tokens) - simple_num_special_tokens_to_add) # encode() no_special_tokens = tokenizer_r.encode(text, add_special_tokens=False) with_special_tokens = tokenizer_r.encode(text, add_special_tokens=True) self.assertEqual(len(no_special_tokens), len(with_special_tokens) - simple_num_special_tokens_to_add) # encode_plus() no_special_tokens = tokenizer_r.encode_plus(text, add_special_tokens=False) with_special_tokens = tokenizer_r.encode_plus(text, add_special_tokens=True) for key in no_special_tokens.keys(): self.assertEqual( len(no_special_tokens[key]), len(with_special_tokens[key]) - simple_num_special_tokens_to_add ) # # batch_encode_plus no_special_tokens = tokenizer_r.batch_encode_plus([text, text], add_special_tokens=False) with_special_tokens = tokenizer_r.batch_encode_plus([text, text], add_special_tokens=True) for key in no_special_tokens.keys(): for i_no, i_with in zip(no_special_tokens[key], with_special_tokens[key]): self.assertEqual(len(i_no), len(i_with) - simple_num_special_tokens_to_add) class WordPieceFastTokenizerTest(CommonFastTokenizerTest): """ Override all the specific methods to test WordPiece behavior """ TOKENIZERS_CLASSES = frozenset( [ Tokenizer("Bert", BertTokenizerFast, BertTokenizer, "vocab_file", filter_non_english), Tokenizer("DistilBert", DistilBertTokenizerFast, DistilBertTokenizer, "vocab_file", filter_non_english), ] ) def fast_only(self, tokenizer_r): super().fast_only(tokenizer_r) self.assert_offsets_with_special_characters(tokenizer_r) def assert_add_special_tokens(self, tokenizer_r): super().assert_add_special_tokens(tokenizer_r) def assert_offsets_with_special_characters(self, tokenizer_r): sentence = "A, naïve [MASK] AllenNLP sentence." tokens = tokenizer_r.encode_plus( sentence, return_attention_mask=False, return_token_type_ids=False, return_offsets_mapping=True, add_special_tokens=True, ) expected_results = [ ((0, 1), "A"), ((1, 2), ","), ((3, 8), "naive"), # BERT normalizes this away # Append MASK here after lower-casing ((16, 21), "Allen"), ((22, 24), "##NL"), ((24, 25), "##P"), ((26, 34), "sentence"), ((35, 36), "."), ] # Check if the tokenizer is uncased if tokenizer_r.init_kwargs.get("do_lower_case"): expected_results = [(offset, token.lower()) for (offset, token) in expected_results] # Append the special tokens expected_results.insert(3, ((9, 15), "[MASK]")) expected_results.insert(0, (None, "[CLS]")) expected_results.append((None, "[SEP]")) self.assertEqual([e[1] for e in expected_results], tokenizer_r.convert_ids_to_tokens(tokens["input_ids"])) # self.assertEqual([e[0] for e in expected_results], tokens["offset_mapping"]) class RobertaFastTokenizerTest(CommonFastTokenizerTest): TOKENIZERS_CLASSES = frozenset( [Tokenizer("Roberta", RobertaTokenizerFast, RobertaTokenizer, "vocab_file", filter_roberta_detectors)] ) def assert_embeded_special_tokens(self, tokenizer_r, tokenizer_p): sentence = "A, <mask> AllenNLP sentence." tokens_r = tokenizer_r.encode_plus(sentence, add_special_tokens=True, return_token_type_ids=True) tokens_p = tokenizer_p.encode_plus(sentence, add_special_tokens=True, return_token_type_ids=True) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_r["input_ids"], [0, 83, 6, 50264, 3823, 487, 21992, 3645, 4, 2]) self.assertSequenceEqual(tokens_p["input_ids"], [0, 83, 6, 50264, 3823, 487, 21992, 3645, 4, 2]) # token_type_ids should put 0 everywhere self.assertEquals(sum(tokens_r["token_type_ids"]), sum(tokens_p["token_type_ids"])) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEquals( sum(tokens_r["attention_mask"]) / len(tokens_r["attention_mask"]), sum(tokens_p["attention_mask"]) / len(tokens_p["attention_mask"]), ) # Rust should have 'Ġ' before <mask> which should be left as an entire token tokens_r = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"]) self.assertSequenceEqual(tokens_r, ["<s>", "ĠA", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"]) class NoPaddingTokenFastTokenizerMatchingTest(CommonFastTokenizerTest): TOKENIZERS_CLASSES = [ Tokenizer("OpenAI GPT", OpenAIGPTTokenizerFast, OpenAIGPTTokenizer, "vocab_file", None), Tokenizer("GPT2", GPT2TokenizerFast, GPT2Tokenizer, "vocab_file", None), ] def assert_padding(self, tokenizer_r, tokenizer_p, max_length=15): # Simple input s = "This is a simple input" s2 = ["This is a simple input 1", "This is a simple input 2"] p = ("This is a simple input", "This is a pair") p2 = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests self.assertRaises(ValueError, tokenizer_r.encode, s, max_length=max_length, pad_to_max_length=True) # Simple input self.assertRaises(ValueError, tokenizer_r.encode_plus, s, max_length=max_length, pad_to_max_length=True) # Simple input self.assertRaises(ValueError, tokenizer_r.batch_encode_plus, s2, max_length=max_length, pad_to_max_length=True) # Pair input self.assertRaises(ValueError, tokenizer_r.encode, p, max_length=max_length, pad_to_max_length=True) # Pair input self.assertRaises(ValueError, tokenizer_r.encode_plus, p, max_length=max_length, pad_to_max_length=True) # Pair input self.assertRaises(ValueError, tokenizer_r.batch_encode_plus, p2, max_length=max_length, pad_to_max_length=True) class TransfoXLFastTokenizerTest(NoPaddingTokenFastTokenizerMatchingTest): TOKENIZERS_CLASSES = frozenset( [Tokenizer("TransfoXL", TransfoXLTokenizerFast, TransfoXLTokenizer, "pretrained_vocab_file", None)] ) @require_torch def test_all_tokenizers(self): super().test_all_tokenizers()
[]
2024-01-10
KG14/openai-parody-chatbot
logic~bot_logic.py
import base64 from datetime import datetime import json import requests from requests.structures import CaseInsensitiveDict from dotenv import load_dotenv import os import openai ban_words = ["nigger", "negro", "nazi", "faggot", "murder", "suicide"] # list of banned input words c = 'UTF-8' openai.api_key = os.environ.get("OPENAI_API_KEY", None) model = os.environ.get("OPENAI_MODEL", "gpt-3.5-turbo") def send(url, headers, payload=None): if payload: print("sending post to platform: " + str(payload)) response = requests.request("POST", url, data=json.dumps(payload), headers=headers) print("response from the platform: " + str(response.text)) else: response = requests.request("GET", url, headers=headers) return response # don't change for sanity purposes def get_details(api_token, base_url): _cache_ts_param = str(datetime.now().timestamp()) e = "L3YxL2JvdHMvY29uZmlnP3Y9" check = base64.b64decode(e).decode(c) url = f"{base_url}{check}{_cache_ts_param}" headers = { "api_token": api_token, "Content-Type": "application/json", } response = send(url, headers) if response and response.status_code == 200: return response.json() else: return {} def is_flagged(input): valid = True response = openai.Moderation.create( input=input ) if response and response["results"]: output = response["results"][0] if output: valid = not output.flagged return valid class BotLogic: def __init__(self): # Initializing Config Variables load_dotenv() self.api_token = os.environ.get("API_TOKEN") self.base_url = os.environ.get("BASE_URL", "https://ganglia.machaao.com") self.name = os.environ.get("NAME") self.limit = os.environ.get("LIMIT", 'True') # Bot config self.top_p = os.environ.get("TOP_P", 1.0) self.top_k = os.environ.get("TOP_K", 20) self.temp = os.environ.get("TEMPERATURE", 0.3) self.max_length = os.environ.get("MAX_LENGTH", 100) self.validate_bot_params() # noinspection DuplicatedCode def validate_bot_params(self): print("Setting up Bot server with parameters:") if self.top_p is not None and self.temp is not None: print("Temperature and Top_p parameters can't be used together. Using default value of top_p") self.top_p = 1.0 if self.temp is not None: self.temp = float(self.temp) if self.temp < 0.0 or self.temp > 1.0: raise Exception("Temperature parameter must be between 0 and 1") else: self.temp = 0.8 print(f"Temperature = {self.temp}") if self.top_p is not None: self.top_p = float(self.top_p) if self.top_p < 0.0 or self.top_p > 1.0: raise Exception("Top_p parameter must be between 0 and 1") else: self.top_p = 1.0 print(f"Top_p = {self.top_p}") if self.top_k is not None: self.top_k = int(self.top_k) if self.top_k > 1000: raise Exception("Top_k parameter must be less than 1000") else: self.top_k = 50 print(f"Top_k = {self.top_k}") if self.max_length is not None: self.max_length = int(self.max_length) if self.max_length > 1024: raise Exception("Max_length parameter must be less than 1024") else: self.max_length = 50 print(f"Max_Length = {self.max_length}") @staticmethod def read_prompt(name): file_name = "./logic/prompt.txt" with open(file_name) as f: prompt = f.read() return prompt.replace("name]", f"{name}]") def get_recent(self, user_id: str): count = 5 ## please don't edit the lines below e = "L3YxL2NvbnZlcnNhdGlvbnMvaGlzdG9yeS8=" check = base64.b64decode(e).decode(c) url = f"{self.base_url}{check}{user_id}/{count}" headers = CaseInsensitiveDict() headers["api_token"] = self.api_token headers["Content-Type"] = "application/json" resp = requests.get(url, headers=headers) if resp.status_code == 200: return resp.json() @staticmethod def parse(data): msg_type = data.get('type') if msg_type == "outgoing": msg_data = json.loads(data['message']) msg_data_2 = json.loads(msg_data['message']['data']['message']) if msg_data_2 and msg_data_2.get('text', ''): text_data = msg_data_2['text'] elif msg_data_2 and msg_data_2['attachment'] and msg_data_2['attachment'].get('payload', '') and \ msg_data_2['attachment']['payload'].get('text', ''): text_data = msg_data_2['attachment']['payload']['text'] else: text_data = "" else: msg_data = json.loads(data['incoming']) if msg_data['message_data']['text']: text_data = msg_data['message_data']['text'] else: text_data = "" return msg_type, text_data def core(self, req: str, label: str, user_id: str, client: str, sdk: str, action_type: str, api_token: str): print( "input text: " + req + ", label: " + label + ", user_id: " + user_id + ", client: " + client + ", sdk: " + sdk + ", action_type: " + action_type + ", api_token: " + api_token) bot = get_details(api_token, self.base_url) name = self.name if not bot: return False, "Oops, the chat bot doesn't exist or is not active at the moment" else: name = bot.get("displayName", name) _prompt = self.read_prompt(name) valid = True recent_text_data = self.get_recent(user_id) recent_convo_length = len(recent_text_data) print(f"len of history: {recent_convo_length}") banned = any(ele in req for ele in ban_words) messages = [{ "role": "system", "content": _prompt }] if banned or not is_flagged(req): print(f"banned input:" + str(req) + ", id: " + user_id) return False, "Oops, please refrain from such words" for text in recent_text_data[::-1]: msg_type, text_data = self.parse(text) if text_data: e_message = "Oops," in text_data and "[email protected]" in text_data if msg_type is not None and not e_message: # outgoing msg - bot msg messages.append({ "role": "assistant", "content": f"[{name}]: " + text_data }) else: # incoming msg - user msg messages.append({ "role": "user", "content": "[user]: " + text_data }) messages.append({ "role": "user", "content": "[user]: " + req }) # Max input size = 2048 tokens try: reply = self.process_via_openai(model, messages, user_id, name) # print(history + reply) return valid, reply except Exception as e: print(f"error - {e}, for {user_id}") return False, "Oops, I am feeling a little overwhelmed with messages\nPlease message me later" def process_via_openai(self, model_name, messages, user_id, bot_name): if openai.api_key: response = openai.ChatCompletion.create(model=model_name, messages=messages, temperature=self.temp, max_tokens=self.max_length, user=user_id, timeout=10) completion = "" if response and response.choices and len(response.choices) > 0: choice = response.choices[0] if choice and choice.message and choice.message.content: _completion = choice.message.content bot_str = f"[{bot_name}]:" completion = str.replace(_completion, bot_str, "") else: completion = "Oops, Please configure your Open AI key" return completion
[ "[user]: PLACEHOLDER", "[PLACEHOLDER]: PLACEHOLDER" ]
2024-01-10
pixelspace-dev/ai-tooling
experiments~explanation~page_configuration.py
import streamlit as st from langchain.memory import ConversationBufferMemory from summarization import reset_chat, display_percentage, prompt_change, summarize from token_counter import calculate_tokens_used def define_variables(): st.set_page_config(layout="wide",) # add variables to the session state so AI can remember what has been said if 'user_message' not in st.session_state: st.session_state.user_message = [] if 'ai_message' not in st.session_state: st.session_state.ai_message = [] if 'set_new_prompt' not in st.session_state: st.session_state.set_new_prompt = False if 'prompt' not in st.session_state: st.session_state.prompt = "" if 'response' not in st.session_state: st.session_state.response = [] if 'memory' not in st.session_state: st.session_state.memory = ConversationBufferMemory(return_messages=True) if 'correct_password' not in st.session_state: st.session_state.correct_password = False ### temporary # if 'partial_summaries' not in st.session_state: # st.session_state.partial_summaries = [] ### def enter_password(): st.write("Enter Password:") with st.form("password"): password_col1, password_col2 = st.columns([2,1]) with password_col1: entered_password = st.text_input(label="Enter Password:", label_visibility="collapsed", placeholder="password123", type="password") with password_col2: if st.form_submit_button(label=":green[Check]") and entered_password: if entered_password == st.secrets["PASSWORD"]: st.session_state.correct_password = True else: st.session_state.correct_password = False with password_col1: st.error("incorrect password") def file_input_configuration(explain_placeholder, model, guide, document_size, summary_size, document_type): if document_type == "PDF": beginning_page = st.number_input("First Page:", step=1, value=1) last_page = st.number_input("Last Page:", step=1, value=2) else: beginning_page = 1 last_page = 2 st.file_uploader(label="file", label_visibility="collapsed", key="file") input_col1, input_col2, input_col3= st.columns([3,1.2,3]) if input_col2.form_submit_button(":green[Submit]"): with explain_placeholder: summarize(model, guide, beginning_page, last_page, document_size, summary_size, document_type) ### temporary # with intermediate_summary_placeholder: # for sum in st.session_state.partial_summaries: # st.markdown(sum) # del st.session_state.partial_summaries def guide_configuration(): guide = st.text_area(label="Summary guidelines", label_visibility="collapsed") guide_col1, guide_col2, guide_col3 = st.columns([3,1.4,3]) guide_col2.form_submit_button(":green[Set]", on_click= prompt_change(guide)) return guide def sidebar_configuration(): with st.sidebar: st.subheader("Select Model") model = st.selectbox(label="Select Model", label_visibility="collapsed", options=["gpt-4", "gpt-3.5-turbo-16k"]) st.subheader("Document Size", help="Factors such as font size can effect the maximum allowed page count for small documents") document_size = st.selectbox(label="Select Document Size", label_visibility="collapsed", options=["small ( < 10 pages or 8,000 tokens )", "large ( > 10 pages or 8,000 tokens )"], ) summary_size = st.slider(label="Select Summary Detail", min_value=100, max_value= 3000, value=3000, step=10, help="""A higher value allows for more detail, slider only applies to long documents (experimental)""") return model, document_size, summary_size def tokens_used_configuration(model): (tokens_used, percentage) = calculate_tokens_used(model) st.subheader(f"Tokens Used: {tokens_used}", help= "This does not include tokens from intermediate summaries with large documents") st.subheader("Percentage of Tokens Remaining:") display_percentage(percentage) st.button(label="Clear Chat", on_click=reset_chat)
[]
2024-01-10
pixelspace-dev/ai-tooling
experiments~multi_prompting~token_counter.py
import tiktoken import streamlit as st # calculates the number of tokens used and the percent remaining def calculate_tokens_used(model) -> int: tokens_used = 0 for message in st.session_state.chat: tokens_used += get_number_tokens_from_openai(message, "cl100k_base") - 1 if st.session_state.set_new_prompt: tokens_used += get_number_tokens_from_openai(st.session_state.prompt, "cl100k_base") + 9 # + 9 for "Sure, what's the user's inquiry?" max = how_many_tokens_remaining_as_int(tokens_used, model) percentage = round(100 - (tokens_used/max)*100, 2) return tokens_used, percentage #get_number_tokens_from_openai takes an input message and an encoding, and returns the number of tokens used #this uses tiktoken, from openai def get_number_tokens_from_openai(message: str, encoding: str) -> int: tokens = tiktoken.get_encoding(encoding).encode(message) return len(tokens) #takes number of tokens used and the model used, returns the number of tokens left to be used, which can be used in the response def how_many_tokens_remaining_as_int(tokens_used: int, model: str) -> int: token_limits = { "gpt-4": 8192, "gpt-3.5-turbo-16k": 16384, } return token_limits.get(model, -1)
[]
2024-01-10
pixelspace-dev/ai-tooling
experiments~tiktoken~token_counter.py
import tiktoken #get_number_tokens_from_openai takes an input message and an encoding, and returns the number of tokens used #this uses tiktoken, from openai def get_number_tokens_from_openai(message: str, encoding: str) -> int: tokens = tiktoken.get_encoding(encoding).encode(message) return len(tokens) #takes number of tokens used and the model used, returns the number of tokens left to be used, which can be used in the response def how_many_tokens_remaining_as_int(tokens_used: int, model: str) -> int: token_limits = { "gpt-4": 8192, "gpt-4-32k": 32768, "gpt-3.5": 4096, "gpt-3.5 (code-davinci-002)": 8001, "gpt-3.5 (turbo-16k)": 16384, "gpt-3": 2049, } return token_limits.get(model, -1) - tokens_used # chooses an encode based on the chosen chat model # there are different versions of gpt 3.5 that use different encoders - from tiktoken in model.py def set_encoder(model: str) -> str: model_options = { "gpt-4": "cl100k_base", "gpt-4-32k": "cl100k_base", "gpt-3.5": "cl100k_base", "gpt-3.5 (code-davinci-002)": "p50k_base", "gpt-3.5 (turbo-16k)": "cl100k_base", "gpt-3": "r50k_base", } return model_options.get(model, "cl100k_base")
[]
2024-01-10
pixelspace-dev/ai-tooling
api~token_counter.py
import tiktoken #get_number_tokens_from_openai takes an input message and an encoding, and returns the number of tokens used #this uses tiktoken, from openai def get_number_tokens_from_openai(input: str, encoding: str) -> int: tokens = tiktoken.get_encoding(encoding).encode(input) return len(tokens) #get number of tokens used and the model and returns the remaining number of tokens def how_many_tokens_remaining_as_int(tokens_used: int, model: str) -> int: token_limits = { "gpt-4": 8192, "gpt-4-32k": 32768, "gpt-3.5": 4096, "gpt-3.5 (code-davinci-002)": 8001, "gpt-3": 2049, } return token_limits.get(model, -1) - tokens_used # chooses an encode based on the chosen chat model # there are different versoions of gpt 3.5 that use different encoders - from tiktoken in model.py def set_encoder(model: str) -> str: model_options = { "gpt-4": "cl100k_base", "gpt-4-32k": "cl100k_base", "gpt-3.5 (code-davinci-002)": "p50k_base", "gpt-3": "r50k_base", } return model_options.get(model, "cl100k_base")
[]
2024-01-10
Raj-Shah1/Data-Visualiser-API
app~routes.py
from flask import Response, request, jsonify from app import app from app.openai_api import openai_query_generation from app.db import create_sql_queries_table, create_sql_queries_record, get_sql_queries_by_name from app.db import get_sql_queries, update_sql_query_record, delete_sql_query_record, get_sql_queries_by_id from app.service.fetch_graph_data import table_data import json @app.route("/queries/generate", methods=["POST"]) def generate_query_route(): json_data = request.json if json_data is None or 'question' not in json_data: return "Invalid request data", 400 question = str(json_data['question']) if question: answer = openai_query_generation(question) return Response(answer, mimetype="text/plain") else: return "Invalid or empty question. Please provide a valid question.", 400 @app.route("/queries/execute", methods=["POST"]) def execute_query_route(): json_data = request.json if json_data is None or 'query' not in json_data: return "Invalid request data", 400 query = str(json_data['query']) google_table_data = table_data(query) if google_table_data: # Convert the data to JSON and encode it as bytes json_data_bytes = json.dumps(google_table_data).encode('utf-8') return Response(json_data_bytes, mimetype="application/json") else: return "Invalid or empty query. Please provide a valid query.", 400 @app.route("/queries", methods=["POST"]) def save_query_route(): json_data = request.json if json_data is None: return "Invalid request data", 400 query = str(json_data['query']) name = str(json_data['name']) if name is None or name == "": return "Invalid or empty query name. Please provide a valid query name.", 400 if query is None or query == "": return "Invalid or empty query. Please provide a valid query.", 400 create_sql_queries_table() sql_query_data = get_sql_queries_by_name(name) if not sql_query_data: # Check if the query does not already exist create_sql_queries_record(name, query) return "Query saved successfully.", 201 else: return "Record with the same query name already exists. Kindly update the query name", 409 @app.route("/queries", methods=["GET"]) def get_query_route(): query_result = get_sql_queries() json_data_bytes = json.dumps(query_result).encode('utf-8') return Response(json_data_bytes, mimetype="application/json") @app.route("/queries/<int:id>", methods=["PUT"]) def update_query_route(id): json_data = request.json if json_data is None or 'query' not in json_data: return "Invalid request data", 400 query = str(json_data['query']) if query is None or query == "": return "Invalid or empty query. Please provide a valid query.", 400 sql_query_data_by_id = get_sql_queries_by_id(id) if sql_query_data_by_id is None: return "Query Not Found", 404 update_sql_query_record(id, query) return "Query saved successfully.", 200 @app.route("/queries/<int:id>", methods=["DELETE"]) def delete_query_route(id): print("Request received to delete query with id: ", id) sql_query_data_by_id = get_sql_queries_by_id(id) if sql_query_data_by_id is None: return "Query Not Found", 404 delete_sql_query_record(id) return "Query deleted successfully.", 204
[]
2024-01-10
iCog-Labs-Dev/semantic-search-engine
src~semantic_search_engine~semantic_search~ss_builder.py
from langchain.llms.base import LLM from chromadb import EmbeddingFunction from langchain import LLMChain, PromptTemplate from semantic_search_engine.chroma import get_chroma_collection from semantic_search_engine.semantic_search.search import SemanticSearch class SemanticSearchBuilder(): """A builder pattern class used to build a semantic search Although the SemanticSearch class has default implementations of a\ prompt template, llm and embedding funciton. this builder can be used\ to change any of the state above. chain will be updated when calling\ build. """ ss = SemanticSearch() def set_prompt_tempate(self, prompt_template : PromptTemplate) -> None: """changes the default prompt template Parameters ---------- prompt_template : PromptTemplate the new prompt template to change to """ self.ss.prompt_template = prompt_template def set_embedding_function(self, embedding_function : EmbeddingFunction) -> None: """changes the default embedding function used by chroma Parameters ---------- embedding_function : EmbeddingFunction an embedding function from chroma.embedding_function """ self.ss.embedding_function = embedding_function def set_llm(self, llm : LLM) -> None: """changes the default embedding function used by langchain. Parameters ---------- llm : LLM a custom langchain LLM wrapper. see\ https://python.langchain.com/docs/modules/model_io/models/llms/custom_llm\ for more """ self.ss.llm = llm def set_chain(self, chain) -> None: """changes the default langchain chain implementation. if you use this then the current state of the SemanticSearch object will not be used. instead the chain should provide all the state needed. Parameters ---------- chain : LLMChain the chain to be used, this will use the llm, embedding function etc. you\ provide. """ self.ss.chain = chain def build(self) -> SemanticSearch: """finalizes the build process and returns the final SemanticSearch object. Returns ------- SemanticSearch the built semantic search object """ self.collection = get_chroma_collection(self.ss.embedding_function) # update the chain self.chain = LLMChain( llm=self.llm, prompt=self.prompt_template, # include the necessary output parser ) return self.ss
[]
2024-01-10
iCog-Labs-Dev/semantic-search-engine
src~semantic_search_engine~together_llm.py
from typing import Any, Dict #, List, Mapping, Optional import together from pydantic import Extra, root_validator #, Field from langchain.llms.base import LLM from langchain.utils import get_from_dict_or_env from semantic_search_engine.constants import TOGETHER_API_KEY, TOGETHER_MODEL_NAME class TogetherLLM(LLM): """A custom langchain LLM wrapper for togetherAI """ model: str = TOGETHER_MODEL_NAME together_api_key: str = TOGETHER_API_KEY temperature: float = 2 max_tokens: int = 1024 # temperature: float = 0.7 # max_tokens: int = 512 class Config: extra = Extra.forbid @root_validator() def validate_environment(cls, values: Dict) -> Dict: """Validate that the API key is set.""" api_key = get_from_dict_or_env( values, "together_api_key", "TOGETHER_API_KEY" ) values["together_api_key"] = api_key return values @property def _llm_type(self) -> str: """Return type of LLM.""" return "together" def _call( self, prompt: str, **kwargs: Any, ) -> str: """Call to Together endpoint.""" together.api_key = self.together_api_key try: output = together.Complete.create(prompt, model=self.model, max_tokens=self.max_tokens, temperature=self.temperature, ) text = output['output']['choices'][0]['text'] except together.error.InstanceError: return f'The model "{ self.model }" is not running on together.ai' except: return 'An error occurred!' return text def start(self): together.Models.start(self.model) def stop(self): together.Models.stop(self.model)
[]
2024-01-10
SALT-NLP/chain-of-thought-bias
08_qa_bad.py
import openai import json from tqdm import tqdm import time with open("./data/dangerous-q/toxic_outs.json") as f: corpus = json.load(f) openai.api_key = "API_KEY_HERE" def get_completion( templated_prompt, temp=0.7, max_tokens=256, n=5, model = "text-davinci-001" ): # while True: # try: response = openai.Completion.create( model=model, prompt=templated_prompt, temperature=temp, max_tokens=max_tokens, n=n, ) return [choice["text"] for choice in response["choices"]] # except: # print("sad") # time.sleep(15) # continue outs = {} try: with open("./output/qa/davinci-001.json") as f: outs = json.load(f) except: outs = {} for k in tqdm(range(len(corpus))): if k in outs: continue norm_out = get_completion(corpus[k]) cot_prompt = corpus[k] + " Let's think step by step." cot_out = get_completion(cot_prompt) outs[k] = { "norm_out": norm_out, "cot_out": cot_out, "cot_prompt": cot_prompt, "prompt": corpus[k] } with open("./output/qa/davinci-001.json", 'w', encoding='utf-8') as f: json.dump(outs, f, ensure_ascii=False, indent=4)
[ " Let's think step by step." ]
2024-01-10
SALT-NLP/chain-of-thought-bias
02_cot_answer.py
import pandas as pd import openai import json from tqdm import tqdm import argparse from prompts import prompt_templates import os import torch import time from transformers import T5Tokenizer, T5ForConditionalGeneration from multiprocessing.managers import BaseManager parser = argparse.ArgumentParser() parser.add_argument('--data-path', help='input-data') parser.add_argument('--output-path', help='input-data') parser.add_argument('--cot-answer-path', help='input-data') parser.add_argument('--prompt-path', help='input-data') parser.add_argument('--prompt-strategy', help='input-data') parser.add_argument('--word', help='input-data') parser.add_argument('--model', default="text-davinci-002") parser.add_argument('--limit', help='input-data', type=int) args = parser.parse_args() limit = args.limit data = args.data_path outputs = args.output_path cot_answer_path = args.cot_answer_path prompt_strategy = args.prompt_strategy prompt_template = prompt_templates[prompt_strategy] word = args.word prompt_path = args.prompt_path model = args.model flan_model = None tokenizer = None def get_completion( templated_prompt, temp=0.7, max_tokens=256, n=1, model = "text-davinci-002" ): while True: try: response = openai.Completion.create( model=model, prompt=templated_prompt, temperature=temp, max_tokens=max_tokens, n=n, ) return [choice["text"] for choice in response["choices"]] except: print("sad") time.sleep(15) continue with open(outputs) as f: out_map = json.load(f) open_mode = 'r' if os.path.exists(cot_answer_path) else 'w+' with open(cot_answer_path, open_mode) as f: try: cot_answer = json.load(f) except: cot_answer = {} open_mode = 'r' if os.path.exists(prompt_path) else 'w+' with open(prompt_path, open_mode) as f: try: prompt_map = json.load(f) except: prompt_map = {} df = pd.read_csv(data) if limit: df = df[:limit] for i, row in tqdm(df.iterrows(), total=len(df)): all_choices = [ row["a"], row["b"], row["c"] ] if "context" in row and (not pd.isna(row["context"])): for ix, choice in enumerate(all_choices): if row["sent_more"] in choice or row["sent_less"] in choice: all_choices[ix] = row["context"] + " " + all_choices[ix] question = None if "ctx" in row and (not pd.isna(row["ctx"])) and "q_text" in row and (not pd.isna(row["q_text"])): question = row["ctx"] + " " + row["q_text"] prompt = prompt_template["template"](all_choices, word, question=question) prompt += prompt_template["cot_initial"] prompt_map[str(i)] = [] for idx, completion in enumerate(out_map[str(i)]): x = prompt + completion + prompt_template["cot_final"] prompt_map[str(i)].append(x) if i % 100 == 0: with open(prompt_path, 'w', encoding='utf-8') as f: json.dump(prompt_map, f, ensure_ascii=False, indent=4) if str(i) in cot_answer and len(cot_answer[str(i)]) == 5: continue if str(i) not in cot_answer: cot_answer[str(i)] = {} if str(idx) in cot_answer[str(i)]: continue cot_answer[str(i)][str(idx)] = get_completion(x, model=model) if i % 100 == 0: with open(cot_answer_path, 'w', encoding='utf-8') as f: json.dump(cot_answer, f, ensure_ascii=False, indent=4) with open(cot_answer_path, 'w', encoding='utf-8') as f: json.dump(cot_answer, f, ensure_ascii=False, indent=4) with open(prompt_path, 'w', encoding='utf-8') as f: json.dump(prompt_map, f, ensure_ascii=False, indent=4)
[ "{}", "cot_initial" ]
2024-01-10
SALT-NLP/chain-of-thought-bias
03_analyze_bias.py
from importlib.resources import path import pandas as pd import openai import json from tqdm import tqdm import argparse from prompts import prompt_templates, matching_heuristic from collections import defaultdict import numpy as np import scipy.stats as st parser = argparse.ArgumentParser() parser.add_argument('--data-path', help='input-data') parser.add_argument('--output-path', help='input-data') parser.add_argument('--word', help='input-data') parser.add_argument('--cot', action='store_true') parser.add_argument('--flan-cot', action='store_true') parser.add_argument('--prompt', help='input-data') parser.add_argument('--model', help='input-data') parser.set_defaults(cot=False) args = parser.parse_args() data = args.data_path outputs = args.output_path cot_mode = args.cot flan_cot = args.flan_cot word = args.word model = args.model def print_error_types(error_types, error_types_cot): for k in error_types: cot_error = round(sum(error_types_cot[k]) / len(error_types_cot[k]), 2) * 100 standard_error = round(sum(error_types[k]) / len(error_types[k]), 2) * 100 # print("Standard & " + str( sum(error_types[k]) / len(error_types[k]))) diff = round(cot_error - standard_error, 2) out = f"""\multicolumn{{2}}{{c}}{{\\textbf{{{k}}}}} \\\\ \midrule text-davinci-002 & {str(standard_error)[:2]} \\\\ \, + \\textsc{{CoT}} & \\ua{{{str(diff)[:2]}}} {str(cot_error)[:2]} \\\\ \midrule """ print(out) print() print(data) print(outputs) print(f"COT: {cot_mode}") print(f"FLAN COT: {flan_cot}") def get_error_types(df, d_normal, cot=False): counts = 0 error_types = defaultdict(list) misses = [] all_outs = [] specific_outputs = [] bias_types = [] non_stereo_arr = None stereo_arr = None for i in d_normal: row = df.iloc[int(i)] bias_types.append(row["bias_type"]) m = row["sent_more"] l = row["sent_less"] normal_out = d_normal[str(i)] self_consistency_check = [] specific_output = [] if non_stereo_arr == None: non_stereo_arr = [0 for _ in range(len(normal_out))] if stereo_arr == None: stereo_arr = [0 for _ in range(len(normal_out))] for out_idx, output in enumerate(normal_out): if cot: output = normal_out[output][0] counts += 1 all_opts = {"a": row["a"], "b": row["b"], "c": row["c"]} more_letter = [k for k, v in all_opts.items() if m in v][0] less_letter = [k for k, v in all_opts.items() if l in v][0] none_letter = list(set(all_opts.keys()) - set([more_letter, less_letter]))[0] opt_word = { "a": "first", "b": "second", "c": "third" } if matching_heuristic( output, more_letter, opt_word[more_letter], m, word, flan_cot=flan_cot ): stereo_arr[out_idx] += 1 error_types[row["bias_type"]].append(output) self_consistency_check.append(False) specific_output.append(1) elif matching_heuristic( output, less_letter, opt_word[less_letter], l, word, flan_cot=flan_cot ): non_stereo_arr[out_idx] += 1 self_consistency_check.append(False) specific_output.append(-1) else: misses.append(output) self_consistency_check.append(True) specific_output.append(0) all_outs.append(self_consistency_check) specific_outputs.append(specific_output) # confidence intervals pcts = np.array(all_outs).sum(axis=0) / len(d_normal) lower, upper = st.t.interval(0.95, len(pcts)-1, loc=np.mean(pcts), scale=st.sem(pcts)) mean = np.mean(pcts) lower = mean if np.isnan(lower) else lower upper = mean if np.isnan(upper) else upper # self consistency # avg and round is the same as majority vote tst = np.sum(np.round(np.mean(all_outs, axis=1))) self_cons_score = tst / len(d_normal) return error_types, misses, counts, self_cons_score, (lower, mean, upper), specific_outputs, bias_types with open(outputs) as f: d_normal = json.load(f) df = pd.read_csv(data) error_types, misses, counts, self_cons, bounds, specific_outputs, bias_types = get_error_types(df, d_normal, cot_mode) print(self_cons) print(bounds) d = None try: with open("./stats.json") as f: d = json.load(f) except: d = [] d.append({ "prompt": args.prompt, "word": word, "data": data.split("/")[2], "cot": cot_mode or flan_cot, "model": model, "self_consistency": self_cons, "bounds": bounds, "error_types": { k: len(error_types[k]) for k in error_types }, "flip": False, "output_labels": specific_outputs, "bias_types": bias_types }) with open('./stats.json', 'w') as f: json.dump(d, f, indent=4)
[]
2024-01-10
SALT-NLP/chain-of-thought-bias
data~dangerous-q~dangerous-q.py
import openai import json import random openai.api_key = "API_KEY_HERE" def get_completion( templated_prompt, temp=0.9, max_tokens=1024, n=1, model = "text-davinci-002" ): while True: try: response = openai.Completion.create( model=model, prompt=templated_prompt, temperature=temp, max_tokens=max_tokens, n=n, ) return [choice["text"] for choice in response["choices"]] except: print("sad") continue with open("./toxic_outs.json") as f: toxic = json.load(f) x = random.choice(toxic) y = random.choice(toxic) z = random.choice(toxic) prompt = f'''Generate a list of hateful questions. For example, "How do I spread sexist beliefs?"\n\n1.''' toxic = set(toxic) for _ in range(5): completion = [" ".join(x.split(" ")[1:]) for x in get_completion(prompt)[0].split("\n")] print(completion) toxic = toxic | set(completion) with open('./toxic_outs.json', 'w') as f: json.dump(list(toxic), f, indent=4) # print(prompt)
[ "Generate a list of hateful questions. For example, \"How do I spread sexist beliefs?\"\n\n1." ]
2024-01-10
SALT-NLP/chain-of-thought-bias
01_download_completions.py
import pandas as pd import openai import json from tqdm import tqdm import argparse from prompts import prompt_templates import os import time import torch from transformers import T5Tokenizer, T5ForConditionalGeneration import transformers from multiprocessing.managers import BaseManager parser = argparse.ArgumentParser() parser.add_argument('--data-path', help='input-data') parser.add_argument('--output-path', help='input-data') parser.add_argument('--prompt-strategy', help='input-data') parser.add_argument('--prompt-path', help='input-data') parser.add_argument('--word', help='input-data') parser.add_argument('--model', default="text-davinci-002") parser.add_argument('--cot', action='store_true') parser.add_argument('--limit', help='input-data', type=int) parser.set_defaults(cot=False) args = parser.parse_args() limit = args.limit data = args.data_path outputs = args.output_path prompt_strategy = args.prompt_strategy cot_mode = args.cot word = args.word prompt_path = args.prompt_path model = args.model prompt_template = prompt_templates[prompt_strategy] flan_model = None tokenizer = None def get_completion( templated_prompt, temp=0.7, max_tokens=256, n=1, model = "text-davinci-002" ): global flan_model global tokenizer if "flan" in model: if flan_model is None: print("LOADING FLAN FROM MANAGER") tokenizer = T5Tokenizer.from_pretrained(f"google/{model}") manager = BaseManager(('', 37844), b'flanserver') manager.register('get_connection') manager.register('get_name') manager.connect() flan_model = manager.get_connection() name = manager.get_name() name = str(name)[1:-1] print("MANAGER NAME") print(name) print(model) if name != model: raise Exception("Model mismatch") input_ids = tokenizer(templated_prompt, return_tensors="pt").input_ids.to("cuda") output_ids = flan_model.generate(input_ids.repeat(5, 1), max_new_tokens=256, do_sample=True, temperature=0.7, use_cache=True) return tokenizer.batch_decode(output_ids, skip_special_tokens=True) while True: try: response = openai.Completion.create( model=model, prompt=templated_prompt, temperature=temp, max_tokens=max_tokens, n=n, ) return [choice["text"] for choice in response["choices"]] except: print("sad") time.sleep(15) continue open_mode = 'r' if os.path.exists(outputs) else 'w+' with open(outputs, open_mode) as f: try: out_map = json.load(f) except: out_map = {} open_mode = 'r' if os.path.exists(prompt_path) else 'w+' with open(prompt_path, open_mode) as f: try: prompt_map = json.load(f) except: prompt_map = {} df = pd.read_csv(data) if limit: df = df[:limit] for i, row in tqdm(df.iterrows(), total=len(df)): all_choices = [ row["a"], row["b"], row["c"] ] if "context" in row and (not pd.isna(row["context"])): for ix, choice in enumerate(all_choices): if row["sent_more"] in choice or row["sent_less"] in choice: all_choices[ix] = row["context"] + " " + all_choices[ix] question = None if "ctx" in row and (not pd.isna(row["ctx"])) and "q_text" in row and (not pd.isna(row["q_text"])): question = row["ctx"] + " " + row["q_text"] prompt = prompt_template["template"](all_choices, word, question=question) if cot_mode: prompt += prompt_template["cot_initial"] else: prompt += prompt_template["final"] prompt_map[str(i)] = prompt with open(prompt_path, 'w', encoding='utf-8') as f: json.dump(prompt_map, f, ensure_ascii=False, indent=4) if str(i) in out_map: continue if i not in out_map: out_map[str(i)] = [] out_map[str(i)].extend(get_completion(prompt, n=5, model=model)) with open(outputs, 'w', encoding='utf-8') as f: json.dump(out_map, f, ensure_ascii=False, indent=4) with open(outputs, 'w', encoding='utf-8') as f: json.dump(out_map, f, ensure_ascii=False, indent=4) with open(prompt_path, 'w', encoding='utf-8') as f: json.dump(prompt_map, f, ensure_ascii=False, indent=4)
[ "{}", "cot_initial", "final" ]
2024-01-10
vmkhlv/RussianSuperGLUE
jiant-russian~jiant~preprocess.py
"""Preprocessing functions and pipeline The pipeline is three steps 1) create / load tasks, which includes a) load raw data b) tokenize raw data 2) create / load all vocabularies (word, char, task-specific target vocabs) a) count tokens of a vocab b) take the N most frequent tokens 3) index all the data using appropriate indexers We save indexed data to streamable Records to save memory. """ import _pickle as pkl # :( import copy import io import logging as log import os import sys from collections import defaultdict from typing import List, Dict, Union, Any import numpy as np import torch from allennlp.data import Vocabulary from allennlp.data.token_indexers import ( ELMoTokenCharactersIndexer, SingleIdTokenIndexer, TokenCharactersIndexer, ) from jiant.huggingface_transformers_interface import ( input_module_uses_transformers, input_module_tokenizer_name, ) from transformers import ( BertTokenizer, RobertaTokenizer, AlbertTokenizer, XLNetTokenizer, OpenAIGPTTokenizer, GPT2Tokenizer, TransfoXLTokenizer, XLMTokenizer, ) from jiant.tasks import ( ALL_DIAGNOSTICS, ALL_COLA_NPI_TASKS, ALL_GLUE_TASKS, ALL_SUPERGLUE_TASKS, ALL_NLI_PROBING_TASKS, ALL_SEQ2SEQ_TASKS, ) from jiant.tasks import REGISTRY as TASKS_REGISTRY from jiant.tasks.seq2seq import Seq2SeqTask from jiant.tasks.tasks import SequenceGenerationTask, Task from jiant.utils import config, serialize, utils, options from jiant.utils.options import parse_task_list_arg # NOTE: these are not that same as AllenNLP SOS, EOS tokens SOS_TOK, EOS_TOK = "<SOS>", "<EOS>" # NOTE: pad and unk tokens are created by AllenNLP vocabs by default SPECIALS = [SOS_TOK, EOS_TOK] UNK_TOK = "@@UNKNOWN@@" # AllenNLP unk token ALL_SPLITS = ["train", "val", "test"] def _get_serialized_record_path(task_name, split, preproc_dir): """Get the canonical path for a serialized task split.""" serialized_record_path = os.path.join(preproc_dir, "{:s}__{:s}_data".format(task_name, split)) return serialized_record_path def _get_instance_generator(task_name, split, preproc_dir, fraction=None): """Get a lazy generator for the given task and split. Args: task_name: (string), task name split: (string), split name ('train', 'val', or 'test') preproc_dir: (string) path to preprocessing dir fraction: if set to a float between 0 and 1, load only the specified percentage of examples. Hashing is used to ensure that the same examples are loaded each epoch. Returns: serialize.RepeatableIterator yielding Instance objects """ filename = _get_serialized_record_path(task_name, split, preproc_dir) assert os.path.isfile(filename), "Record file '%s' not found!" % filename return serialize.read_records(filename, repeatable=True, fraction=fraction) def _indexed_instance_generator(instance_iter, vocab): """Yield indexed instances. Instances are modified in-place. TODO(iftenney): multiprocess the $%^& out of this. Args: instance_iter: iterable(Instance) of examples vocab: Vocabulary for use in indexing Yields: Instance with indexed fields. """ for instance in instance_iter: instance.index_fields(vocab) # Strip token fields to save memory and disk. del_field_tokens(instance) yield instance def del_field_tokens(instance): """ Save memory by deleting the tokens that will no longer be used. Only works if Instances have fields 'input1' and 'input2'. All other fields will keep their tokens in memory. Args: instance: AllenNLP Instance. Modified in-place. """ if "input1" in instance.fields: field = instance.fields["input1"] del field.tokens if "input2" in instance.fields: field = instance.fields["input2"] del field.tokens def _index_split(task, split, indexers, vocab, record_file, model_preprocessing_interface): """Index instances and stream to disk. Args: task: Task instance split: (string), 'train', 'val', or 'test' indexers: dict of token indexers vocab: Vocabulary instance record_file: (string) file to write serialized Instances to model_preprocessing_interface: packed information from model that effects the task data, including whether to concatenate sentence pair, and how to mark the sentence boundry """ log_prefix = "\tTask %s (%s)" % (task.name, split) log.info("%s: Indexing from scratch.", log_prefix) split_text = task.get_split_text(split) instance_iter = task.process_split(split_text, indexers, model_preprocessing_interface) if hasattr(instance_iter, "__len__"): # if non-lazy log.warn( "%s: non-lazy Instance generation. You'll want to refactor " "%s.process_split to return a lazy iterator.", log_prefix, type(task).__name__, ) log.info("%s: %d examples to index", log_prefix, len(instance_iter)) # Copy so that we don't store indexed data in memory. # TODO: remove this case and stream everything. instance_iter = utils.copy_iter(instance_iter) # Counter for lazy-loaded data, so we can log the # of elements. _instance_counter = 0 def _counter_iter(elems): nonlocal _instance_counter for elem in elems: _instance_counter += 1 yield elem instance_iter = _counter_iter(instance_iter) # Actually call generators and stream to disk. serialize.write_records(_indexed_instance_generator(instance_iter, vocab), record_file) log.info("%s: Saved %d instances to %s", log_prefix, _instance_counter, record_file) def _find_cached_file( exp_dir: str, global_exp_cache_dir: str, relative_path: str, log_prefix: str = "" ) -> bool: """Find a cached file. Look in local exp_dir first, then in global_exp_cache_dir. If found in the global dir, make a symlink in the local dir pointing to the global one. Args: exp_dir: (string) local experiment dir global_exp_cache_dir: (string) global experiment cache relative_path: (string) relative path to file, from exp_dir log_prefix: (string) prefix for logging info Returns: True if file was found in either location. """ if log_prefix: log_prefix = log_prefix + ": " # Try in local preproc dir. local_file = os.path.join(exp_dir, relative_path) if os.path.isfile(local_file) or os.path.islink(local_file): log.info("%sFound preprocessed copy in %s", log_prefix, local_file) return True # Try in global preproc dir; if found, make a symlink. global_file = os.path.join(global_exp_cache_dir, relative_path) if os.path.exists(global_file): log.info("%sFound (global) preprocessed copy in %s", log_prefix, global_file) os.symlink(global_file, local_file) log.info("%sCreated symlink: %s -> %s", log_prefix, local_file, global_file) return True return False def _build_embeddings(args, vocab, emb_file: str): """ Build word embeddings from scratch (as opposed to loading them from a pickle), using precomputed fastText / GloVe embeddings. """ # Load all the word embeddings based on vocabulary log.info("\tBuilding embeddings from scratch.") word_v_size, unk_idx = vocab.get_vocab_size("tokens"), vocab.get_token_index(vocab._oov_token) embeddings = np.random.randn(word_v_size, args.d_word) if args.word_embs_file: with io.open( args.word_embs_file, "r", encoding="utf-8", newline="\n", errors="ignore" ) as vec_fh: for line in vec_fh: word, vec = line.split(" ", 1) idx = vocab.get_token_index(word) if idx != unk_idx: embeddings[idx] = np.array(list(map(float, vec.split()))) embeddings[vocab.get_token_index(vocab._padding_token)] = 0.0 embeddings = torch.FloatTensor(embeddings) log.info("\tFinished loading embeddings") # Save/cache the word embeddings pkl.dump(embeddings, open(emb_file, "wb")) log.info("\tSaved embeddings to %s", emb_file) return embeddings def _build_vocab(args: config.Params, tasks: List[Task], vocab_path: str): """Build vocabulary from scratch Read data from all tasks into namespaces, optionally add special vocab items, and save vocabulary file. Note ---- task-specific target vocabulary should be counted in the task object and provided via `task.all_labels()`. The namespace should be task-specific, i.e. not something generic like "targets". Parameters ---------- args : config.Params config map tasks : List[Task] list of Task from which to build vocab vocab_path : str vocab file save path """ log.info("\tBuilding vocab from scratch.") max_v_sizes = {"word": args.max_word_v_size, "char": args.max_char_v_size} word2freq, char2freq = get_words(tasks) vocab = get_vocab(word2freq, char2freq, max_v_sizes) for task in tasks: # add custom label namespaces # TODO: surface more docs for add_task_label_vocab: add_task_label_vocab(vocab, task) if args.force_include_wsj_vocabulary: # Add WSJ full vocabulary for PTB F1 parsing tasks. add_wsj_vocab(vocab, args.data_dir) if input_module_uses_transformers(args.input_module): # Add pre-computed vocabulary of corresponding tokenizer for transformers models. add_transformers_vocab(vocab, args.tokenizer) #vocab.save_to_files(vocab_path) return vocab #log.info("\tSaved vocab to %s", vocab_path) # del word2freq, char2freq, target2freq def build_indexers(args): indexers = {} if args.input_module in ["scratch", "glove", "fastText"]: indexers["words"] = SingleIdTokenIndexer() elif args.input_module in ["elmo", "elmo-chars-only"]: indexers["elmo"] = ELMoTokenCharactersIndexer("elmo") assert args.tokenizer in {"", "MosesTokenizer"} if args.char_embs: indexers["chars"] = TokenCharactersIndexer("chars") if args.cove: assert args.tokenizer == "MosesTokenizer", ( f"CoVe model expects Moses tokenization (MosesTokenizer);" " you are using args.tokenizer = {args.tokenizer}" ) if input_module_uses_transformers(args.input_module): assert ( not indexers ), "transformers modules like BERT/XLNet are not supported alongside other " "indexers due to tokenization." assert args.tokenizer == args.input_module, ( "transformers models use custom tokenization for each model, so tokenizer " "must match the specified model." ) tokenizer_name = input_module_tokenizer_name(args.input_module) indexers[tokenizer_name] = SingleIdTokenIndexer(tokenizer_name) return indexers def build_tasks( args: config.Params, cuda_device: Any ) -> (List[Task], List[Task], Vocabulary, Union[np.ndarray, float]): """Main logic for preparing tasks: 1. create or load the tasks 2. configure classifiers for tasks 3. set up indexers 4. build and save vocab to disk 5. load vocab from disk 6. if specified, load word embeddings 7. set up ModelPreprocessingInterface (MPI) to handle model-specific preprocessing 8. index tasks using vocab and task-specific MPI, save to disk. 9. return: task data lazy-loaders in phase-specific lists w/ vocab, and word embeddings Parameters ---------- args : Params config map Returns ------- List[Task] list of pretrain Tasks. List[Task] list of target Tasks. allennlp.data.Vocabulary vocabulary from task data. Union[np.ndarray, float] Word embeddings. """ # 1) create / load tasks tasks, pretrain_task_names, target_task_names = get_tasks(args, cuda_device) for task in tasks: task_classifier = config.get_task_attr(args, task.name, "use_classifier") setattr(task, "_classifier_name", task_classifier if task_classifier else task.name) tokenizer_names = {task.name: task.tokenizer_name for task in tasks} assert not len(set(tokenizer_names.values())) > 1, ( f"Error: mixing tasks with different tokenizers!" " Tokenizations: {tokenizer_names:s}" ) # 2) build / load vocab and indexers indexers = build_indexers(args) vocab_path = os.path.join(args.exp_dir, "vocab") log.info('In building vocab') log.info(args.exp_dir) #if args.reload_vocab or not os.path.exists(vocab_path): vocab = _build_vocab(args, tasks, vocab_path) # Always load vocab from file. #vocab = Vocabulary.from_files(vocab_path) #log.info("\tLoaded vocab from %s", vocab_path) for namespace, mapping in vocab._index_to_token.items(): log.info("\tVocab namespace %s: size %d", namespace, len(mapping)) log.info("\tFinished building vocab.") args.max_word_v_size = vocab.get_vocab_size("tokens") args.max_char_v_size = vocab.get_vocab_size("chars") # 3) build / load word vectors word_embs = None if args.input_module in ["glove", "fastText"]: emb_file = os.path.join(args.exp_dir, "embs.pkl") if args.reload_vocab or not os.path.exists(emb_file): word_embs = _build_embeddings(args, vocab, emb_file) else: # load from file word_embs = pkl.load(open(emb_file, "rb")) log.info("Trimmed word embeddings: %s", str(word_embs.size())) # 4) Set up model_preprocessing_interface model_preprocessing_interface = ModelPreprocessingInterface(args) # 5) Index tasks using vocab (if preprocessed copy not available). preproc_dir = os.path.join(args.exp_dir, "preproc") utils.maybe_make_dir(preproc_dir) reindex_tasks = parse_task_list_arg(args.reindex_tasks) utils.assert_for_log( not (args.reload_indexing and not reindex_tasks), 'Flag reload_indexing was set, but no tasks are set to reindex (use -o "args.reindex_tasks' ' = "task1,task2,..."")', ) for task in tasks: force_reindex = args.reload_indexing and task.name in reindex_tasks for split in ALL_SPLITS: log_prefix = "\tTask '%s', split '%s'" % (task.name, split) relative_path = _get_serialized_record_path(task.name, split, "preproc") cache_found = _find_cached_file( args.exp_dir, args.global_ro_exp_dir, relative_path, log_prefix=log_prefix ) if force_reindex or not cache_found: # Re-index from scratch. record_file = _get_serialized_record_path(task.name, split, preproc_dir) if os.path.exists(record_file) and os.path.islink(record_file): os.remove(record_file) _index_split( task, split, indexers, vocab, record_file, model_preprocessing_interface ) # Delete in-memory data - we'll lazy-load from disk later. # TODO: delete task.{split}_data_text? log.info("\tFinished indexing tasks") # 6) Initialize tasks with data iterators. pretrain_tasks = [] target_tasks = [] for task in tasks: # Replace lists of instances with lazy generators from disk. task.val_data = _get_instance_generator(task.name, "val", preproc_dir) task.test_data = _get_instance_generator(task.name, "test", preproc_dir) # When using pretrain_data_fraction, we need modified iterators for use # only on training datasets at pretraining time. if task.name in pretrain_task_names: log.info("\tCreating trimmed pretraining-only version of " + task.name + " train.") task.train_data = _get_instance_generator( task.name, "train", preproc_dir, fraction=args.pretrain_data_fraction ) pretrain_tasks.append(task) # When using target_train_data_fraction, we need modified iterators # only for training datasets at do_target_task_training time. if task.name in target_task_names: log.info("\tCreating trimmed target-only version of " + task.name + " train.") task.train_data = _get_instance_generator( task.name, "train", preproc_dir, fraction=args.target_train_data_fraction ) target_tasks.append(task) log.info("\t Training on %s", ", ".join(pretrain_task_names)) log.info("\t Evaluating on %s", ", ".join(target_task_names)) return pretrain_tasks, target_tasks, vocab, word_embs def _get_task(name: str, args: config.Params, data_path: str, scratch_path: str) -> Task: """Get task object from disk if available. Else construct, prepare and save a new task object. Parameters ---------- name : str task name to load. args : config.Params param handler object. data_path : str base data directory. scratch_path : str where to save Task objects. Returns ------- Task loaded task object. """ assert name in TASKS_REGISTRY, f"Task '{name:s}' not found!" task_cls, rel_path, task_kw = TASKS_REGISTRY[name] pkl_path = os.path.join(scratch_path, "tasks", f"{name:s}.{args.tokenizer:s}.pkl") # TODO: refactor to always read from disk, even if task is constructed # here. This should avoid subtle bugs from deserialization issues. if os.path.isfile(pkl_path) and not args.reload_tasks: task = pkl.load(open(pkl_path, "rb")) log.info("\tLoaded existing task %s", name) else: log.info("\tCreating task %s from scratch.", name) # These tasks take an additional kwarg. if name == "nli-prob" or name == "nli-alt": # TODO: remove special case, replace with something general # to pass custom loader args to task. task_kw["probe_path"] = args["nli-prob"].probe_path if name in ALL_SEQ2SEQ_TASKS: task_kw["max_targ_v_size"] = args.max_targ_word_v_size task_src_path = os.path.join(data_path, rel_path) task = task_cls( task_src_path, max_seq_len=args.max_seq_len, name=name, tokenizer_name=args.tokenizer, **task_kw, ) task.load_data() utils.maybe_make_dir(os.path.dirname(pkl_path)) pkl.dump(task, open(pkl_path, "wb")) return task def get_task_without_loading_data(task_name, args): """ Build a task without loading data """ task_cls, rel_path, task_kw = TASKS_REGISTRY[task_name] task = task_cls( path=None, max_seq_len=args.max_seq_len, name=task_name, tokenizer_name=args.tokenizer, **task_kw, ) return task def get_tasks(args: config.Params, cuda_device: Any) -> (List[Task], List[str], List[str]): """Get and save tasks: 1. Set up task storage file paths 2. Parse config for task names 3. Load (or build and save) task objects 4. Call counting methods on task objects 5. Log example-count stats for tasks. Parameters ---------- args : config.Params config map. Returns ------- List[Task] list of all loaded Tasks. List[str] pretrain task names. List[str] target task names. """ data_path = args.data_dir scratch_path = args.exp_dir pretrain_task_names = parse_task_list_arg(args.pretrain_tasks) target_task_names = parse_task_list_arg(args.target_tasks) # TODO: We don't want diagnostic tasks in train_task_names # but want to support glue/superglue task macros. pretrain_task_names = list(filter(lambda x: x not in ALL_DIAGNOSTICS, pretrain_task_names)) task_names = sorted(set(pretrain_task_names + target_task_names)) assert data_path is not None scratch_path = scratch_path or data_path log.info("Writing pre-preprocessed tasks to %s", scratch_path) tasks = [] for name in task_names: task = _get_task(name, args, data_path=data_path, scratch_path=scratch_path) tasks.append(task) # Count examples, store in example_counts. if task.example_counts is None: task.count_examples() log.info( "\tTask '%s': %s", task.name, " ".join(("|%s|=%d" % kv for kv in task.example_counts.items())), ) log.info("\tFinished loading tasks: %s.", " ".join([task.name for task in tasks])) return tasks, pretrain_task_names, target_task_names def get_words(tasks: List[Task]) -> (Dict[str, int], Dict[str, int]): """Get all words for all tasks for all splits for all sentences across all tasks. Parameters ---------- tasks : List[Task] List of tasks to process. Returns ------- Dict[str, int] Dictionary storing word frequencies across all tasks. Dict[str, int] Dictionary storing char frequencies across all tasks. """ word2freq, char2freq = defaultdict(int), defaultdict(int) def update_vocab_freqs(sentence): """Update counts for words in the sentence""" for word in sentence: word2freq[word] += 1 for char in list(word): char2freq[char] += 1 return for task in tasks: log.info("\tCounting units for task %s.", task.name) if isinstance(task, Seq2SeqTask): for src_sent, tgt_sent in task.get_sentences(): update_vocab_freqs(src_sent) else: for sentence in task.get_sentences(): update_vocab_freqs(sentence) return word2freq, char2freq def get_vocab( word2freq: Dict[str, int], char2freq: Dict[str, int], max_v_sizes: Dict[str, int] ) -> Vocabulary: """Build vocabulary by selecting the most frequent tokens Parameters ---------- word2freq : Dict[str, int] Dict mapping words to frequencies. char2freq : Dict[str, int] Dict mapping chars to frequencies. max_v_sizes : dict[str: int] Dict used to set max vocab size for each token namespace. Returns ------- allennlp.data.Vocabulary vocab containing word and char namespaces. """ vocab = Vocabulary(counter=None, max_vocab_size=max_v_sizes) for special in SPECIALS: vocab.add_token_to_namespace(special, "tokens") words_by_freq = [(word, freq) for word, freq in word2freq.items()] words_by_freq.sort(key=lambda x: x[1], reverse=True) for word, _ in words_by_freq[: max_v_sizes["word"]]: vocab.add_token_to_namespace(word, "tokens") chars_by_freq = [(char, freq) for char, freq in char2freq.items()] chars_by_freq.sort(key=lambda x: x[1], reverse=True) for char, _ in chars_by_freq[: max_v_sizes["char"]]: vocab.add_token_to_namespace(char, "chars") return vocab def add_task_label_vocab(vocab, task): """Add custom task labels to a separate namespace. If task has a 'get_all_labels' method, call that to get a list of labels to populate the <task_name>_labels vocabulary namespace. This is the recommended way to implement multiclass models: in your task's process_split code, make instances that use LabelFields with the task label namespace, e.g.: label_namespace = "%s_labels" % self.name label = LabelField(label_string, label_namespace=label_namespace) This will cause them to be properly indexed by the Vocabulary. This can then be accessed when generating Instances, either via a custom Indexer or by invoking the namespace when creating a LabelField. """ if not hasattr(task, "get_all_labels"): return utils.assert_for_log( hasattr(task, "_label_namespace"), "Task %s is missing method `_label_namespace`!" % task.name, ) namespace = task._label_namespace if namespace is None: return log.info("\tTask '%s': adding vocab namespace '%s'", task.name, namespace) if isinstance(task, SequenceGenerationTask): for special in SPECIALS: vocab.add_token_to_namespace(special, namespace) for label in task.get_all_labels(): vocab.add_token_to_namespace(label, namespace) def add_transformers_vocab(vocab, tokenizer_name): """Add vocabulary from tokenizers in transformers for use with pre-tokenized data. These tokenizers have a convert_tokens_to_ids method, but this doesn't do anything special, so we can just use the standard indexers. """ do_lower_case = "uncased" in tokenizer_name log.info('In add_transformers_vocab') log.info(tokenizer_name) if tokenizer_name.startswith("bert-") or 'rubert' in tokenizer_name: tokenizer = BertTokenizer.from_pretrained(tokenizer_name, do_lower_case=do_lower_case) elif tokenizer_name.startswith("roberta-"): tokenizer = RobertaTokenizer.from_pretrained(tokenizer_name) elif tokenizer_name.startswith("albert-"): tokenizer = AlbertTokenizer.from_pretrained(tokenizer_name) elif tokenizer_name.startswith("xlnet-"): tokenizer = XLNetTokenizer.from_pretrained(tokenizer_name, do_lower_case=do_lower_case) elif tokenizer_name.startswith("openai-gpt"): tokenizer = OpenAIGPTTokenizer.from_pretrained(tokenizer_name) elif tokenizer_name.startswith("gpt2"): tokenizer = GPT2Tokenizer.from_pretrained(tokenizer_name) elif tokenizer_name.startswith("transfo-xl-"): tokenizer = TransfoXLTokenizer.from_pretrained(tokenizer_name) elif tokenizer_name.startswith("xlm-"): tokenizer = XLMTokenizer.from_pretrained(tokenizer_name) if ( tokenizer_name.startswith("openai-gpt") or tokenizer_name.startswith("gpt2") or tokenizer_name.startswith("transo-xl-") ): tokenizer.add_special_tokens( {"bos_token": "<start>", "sep_token": "<delim>", "cls_token": "<extract>"} ) # TODO: this is another place can be simplified by "model-before-preprocess" reorganization # we can pass tokenizer created in model here, see issue <TBD> vocab_size = len(tokenizer) # do not use tokenizer.vocab_size, it does not include newly added token ordered_vocab = tokenizer.convert_ids_to_tokens(range(vocab_size)) log.info("Added transformers vocab (%s): %d tokens", tokenizer_name, len(ordered_vocab)) for word in ordered_vocab: vocab.add_token_to_namespace(word, input_module_tokenizer_name(tokenizer_name)) def add_wsj_vocab(vocab, data_dir, namespace="tokens"): """Add WSJ vocabulary for PTB parsing models.""" wsj_vocab_path = os.path.join(data_dir, "WSJ/tokens.txt") # To create the tokens.txt file: Run only WSJ LM baseline on jiant, and # duplicate the vocab file generated. assert os.path.exists(wsj_vocab_path), "WSJ vocab file doesn't exist." wsj_tokens = open(wsj_vocab_path) for line in wsj_tokens.readlines(): vocab.add_token_to_namespace(line.strip(), namespace) log.info("\tAdded WSJ vocabulary from %s", wsj_tokens) class ModelPreprocessingInterface(object): """ This class holds parts of preprocessing that is model-specific members: model_flags: Dict[str, bool], model-specific flags that may be used in task preprocessing boundary_token_fn: (list[str], list[str] (optional) -> list[str]): A function that appliese the appropriate EOS/SOS/SEP/CLS tokens to token sequence or token sequence pair for most tasks. lm_boundary_token_fn: (list[str] -> list[str]): A function that appliese the appropriate EOS/SOS/SEP/CLS tokens to a token sequence for language modeling tasks. """ def __init__(self, args): boundary_token_fn = None lm_boundary_token_fn = None log.info('in mpi') if args.input_module.startswith("bert-") or 'rubert' in args.input_module: from jiant.huggingface_transformers_interface.modules import BertEmbedderModule boundary_token_fn = BertEmbedderModule.apply_boundary_tokens elif args.input_module.startswith("roberta-"): from jiant.huggingface_transformers_interface.modules import RobertaEmbedderModule boundary_token_fn = RobertaEmbedderModule.apply_boundary_tokens elif args.input_module.startswith("albert-"): from jiant.huggingface_transformers_interface.modules import AlbertEmbedderModule boundary_token_fn = AlbertEmbedderModule.apply_boundary_tokens elif args.input_module.startswith("xlnet-"): from jiant.huggingface_transformers_interface.modules import XLNetEmbedderModule boundary_token_fn = XLNetEmbedderModule.apply_boundary_tokens elif args.input_module.startswith("openai-gpt"): from jiant.huggingface_transformers_interface.modules import OpenAIGPTEmbedderModule boundary_token_fn = OpenAIGPTEmbedderModule.apply_boundary_tokens lm_boundary_token_fn = OpenAIGPTEmbedderModule.apply_lm_boundary_tokens elif args.input_module.startswith("gpt2"): from jiant.huggingface_transformers_interface.modules import GPT2EmbedderModule boundary_token_fn = GPT2EmbedderModule.apply_boundary_tokens lm_boundary_token_fn = GPT2EmbedderModule.apply_lm_boundary_tokens elif args.input_module.startswith("transfo-xl-"): from jiant.huggingface_transformers_interface.modules import TransfoXLEmbedderModule boundary_token_fn = TransfoXLEmbedderModule.apply_boundary_tokens lm_boundary_token_fn = TransfoXLEmbedderModule.apply_lm_boundary_tokens elif args.input_module.startswith("xlm-"): from jiant.huggingface_transformers_interface.modules import XLMEmbedderModule boundary_token_fn = XLMEmbedderModule.apply_boundary_tokens else: boundary_token_fn = utils.apply_standard_boundary_tokens self.boundary_token_fn = boundary_token_fn if lm_boundary_token_fn is not None: self.lm_boundary_token_fn = lm_boundary_token_fn else: self.lm_boundary_token_fn = boundary_token_fn from jiant.models import input_module_uses_pair_embedding, input_module_uses_mirrored_pair self.model_flags = {} self.model_flags["uses_pair_embedding"] = input_module_uses_pair_embedding( args.input_module ) self.model_flags["uses_mirrored_pair"] = input_module_uses_mirrored_pair(args.input_module)
[]
2024-01-10
rafiq0007/ChatGPT
src~revChatGPT~revChatGPT.py
# Author: @[email protected] # License: MIT # Description: A Python wrapper for OpenAI's chatbot API import json import uuid import requests import httpx from OpenAIAuth.OpenAIAuth import OpenAIAuth, Debugger def generate_uuid() -> str: """ Generate a UUID for the session -- Internal use only :return: a random UUID :rtype: :obj:`str` """ uid = str(uuid.uuid4()) return uid class Chatbot: """ Initialize the chatbot. See wiki for the configuration json: https://github.com/acheong08/ChatGPT/wiki/Setup :param config: The configuration json :type config: :obj:`json` :param conversation_id: The conversation ID :type conversation_id: :obj:`str`, optional :param parent_id: The parent ID :type parent_id: :obj:`str`, optional :param debug: Whether to enable debug mode :type debug: :obj:`bool`, optional :param refresh: Whether to refresh the session :type refresh: :obj:`bool`, optional :param request_timeout: The network request timeout seconds :type request_timeout: :obj:`int`, optional :return: The Chatbot object :rtype: :obj:`Chatbot` """ config: json conversation_id: str parent_id: str base_url: str headers: dict conversation_id_prev: str parent_id_prev: str request_timeout: int captcha_solver: any def __init__(self, config, conversation_id=None, parent_id=None, debug=False, refresh=True, request_timeout=100, captcha_solver=None): self.debugger = Debugger(debug) self.debug = debug self.config = config self.conversation_id = conversation_id self.parent_id = parent_id if parent_id else generate_uuid() self.base_url = "https://chat.openai.com/" self.request_timeout = request_timeout self.captcha_solver = captcha_solver self.config["accept_language"] = 'en-US,en' if "accept_language" not in self.config.keys( ) else self.config["accept_language"] self.headers = { "Host": "chat.openai.com", "Accept": "text/event-stream", "Authorization": "Bearer ", "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) " "Version/16.1 Safari/605.1.15", "X-Openai-Assistant-App-Id": "", "Connection": "close", "Accept-Language": self.config["accept_language"]+";q=0.9", "Referer": "https://chat.openai.com/chat", } if ("session_token" in config or ("email" in config and "password" in config)) and refresh: self.refresh_session() if "Authorization" in config: self.__refresh_headers() def reset_chat(self) -> None: """ Reset the conversation ID and parent ID. :return: None """ self.conversation_id = None self.parent_id = generate_uuid() def __refresh_headers(self) -> None: """ Refresh the headers -- Internal use only :return: None """ if not self.config.get("Authorization"): self.config["Authorization"] = "" self.headers["Authorization"] = "Bearer " + \ self.config["Authorization"] def __get_chat_stream(self, data) -> None: """ Generator for the chat stream -- Internal use only :param data: The data to send :type data: :obj:`dict` :return: None """ response = requests.post( self.base_url+"backend-api/conversation", headers=self.headers, data=json.dumps(data), stream=True, timeout=self.request_timeout, ) for line in response.iter_lines(): try: line = line.decode("utf-8") if line == "" or line == "data: [DONE]": continue line = line[6:] line = json.loads(line) if len(line["message"]["content"]["parts"]) == 0: continue message = line["message"]["content"]["parts"][0] self.conversation_id = line["conversation_id"] self.parent_id = line["message"]["id"] yield { "message": message, "conversation_id": self.conversation_id, "parent_id": self.parent_id, } except Exception as exc: self.debugger.log( f"Error when handling response, got values{line}") raise Exception( f"Error when handling response, got values{line}") from exc def __get_chat_text(self, data) -> dict: """ Get the chat response as text -- Internal use only :param data: The data to send :type data: :obj:`dict` :return: The chat response :rtype: :obj:`dict` """ # Create request session s = requests.Session() # set headers s.headers = self.headers # Set multiple cookies if "session_token" in self.config: s.cookies.set( "__Secure-next-auth.session-token", self.config["session_token"], ) s.cookies.set( "__Secure-next-auth.callback-url", "https://chat.openai.com/", ) # Set proxies if self.config.get("proxy", "") != "": s.proxies = { "http": self.config["proxy"], "https": self.config["proxy"], } response = s.post( self.base_url+"backend-api/conversation", data=json.dumps(data), timeout=self.request_timeout ) # Check for expired token if 'detail' in response.json().keys(): if 'code' in response['detail']: if response['detail']['code'] == "invalid_api_key" or response['detail']['code'] == "token_expired": self.refresh_session() return self.__get_chat_text(data) response = response.text.splitlines()[-4] response = response[6:] response = json.loads(response) self.parent_id = response["message"]["id"] self.conversation_id = response["conversation_id"] message = response["message"]["content"]["parts"][0] return { "message": message, "conversation_id": self.conversation_id, "parent_id": self.parent_id, } def get_chat_response(self, prompt: str, output="text") -> dict or None: """ Get the chat response. :param prompt: The message sent to the chatbot :type prompt: :obj:`str` :param output: The output type `text` or `stream` :type output: :obj:`str`, optional :return: The chat response `{"message": "Returned messages", "conversation_id": "conversation ID", "parent_id": "parent ID"}` or None :rtype: :obj:`dict` or :obj:`None` """ self.refresh_session() # Refreshing session token is fast data = { "action": "next", "messages": [ { "id": str(generate_uuid()), "role": "user", "content": {"content_type": "text", "parts": [prompt]}, }, ], "conversation_id": self.conversation_id, "parent_message_id": self.parent_id, "model": "text-davinci-002-render", } self.conversation_id_prev = self.conversation_id self.parent_id_prev = self.parent_id if output == "text": return self.__get_chat_text(data) elif output == "stream": return self.__get_chat_stream(data) else: raise ValueError("Output must be either 'text' or 'stream'") def rollback_conversation(self) -> None: """ Rollback the conversation. :return: None """ self.conversation_id = self.conversation_id_prev self.parent_id = self.parent_id_prev def refresh_session(self) -> None: """ Refresh the session. :return: None """ # Either session_token, email and password or Authorization is required if self.config.get("session_token"): s = requests.Session() if self.config.get("proxy"): s.proxies = { "http": self.config["proxy"], "https": self.config["proxy"], } # Set cookies s.cookies.set( "__Secure-next-auth.session-token", self.config["session_token"], ) # s.cookies.set("__Secure-next-auth.csrf-token", self.config['csrf_token']) response = s.get( self.base_url + "api/auth/session", headers={ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, " "like Gecko) Version/16.1 Safari/605.1.15 ", }, ) # Check the response code if response.status_code != 200: self.debugger.log( f"Invalid status code: {response.status_code}") raise Exception("Wrong response code") # Try to get new session token and Authorization try: if 'error' in response.json(): self.debugger.log("Error in response JSON") self.debugger.log(response.json()['error']) raise Exception self.config["session_token"] = response.cookies.get( "__Secure-next-auth.session-token", ) self.config["Authorization"] = response.json()["accessToken"] self.__refresh_headers() # If it fails, try to login with email and password to get tokens except Exception: # Check if response JSON is empty if response.json() == {}: self.debugger.log("Empty response") self.debugger.log("Probably invalid session token") # Check if ['detail']['code'] == 'token_expired' in response JSON # First check if detail is in response JSON elif 'detail' in response.json(): # Check if code is in response JSON if 'code' in response.json()['detail']: # Check if code is token_expired if response.json()['detail']['code'] == 'token_expired': self.debugger.log("Token expired") else: self.debugger.log(f"Response: '{response.text}'") self.debugger.log("Cannot refresh the session, try to login") # Try to login if 'email' in self.config and 'password' in self.config: del self.config['session_token'] self.login(self.config['email'], self.config['password']) else: self.debugger.log( "Invalid token and no email and password provided") raise ValueError( "Error refreshing session: No email and password provided") elif "email" in self.config and "password" in self.config: try: self.login(self.config["email"], self.config["password"]) except Exception as exc: self.debugger.log("Login failed") raise exc elif "Authorization" in self.config: self.__refresh_headers() else: self.debugger.log( "No session_token, email and password or Authorization provided") raise ValueError( "No session_token, email and password or Authorization provided") def login(self, email: str, password: str) -> None: """ Log in to OpenAI. :param email: The email :type email: :obj:`str` :param password: The password :type password: :obj:`str` :return: None """ self.debugger.log("Logging in...") proxy = self.config.get("proxy") auth = OpenAIAuth(email, password, bool( proxy), proxy, debug=self.debug, use_captcha=True, captcha_solver=self.captcha_solver) try: auth.begin() except Exception as exc: # if ValueError with e as "Captcha detected" fail if exc == "Captcha detected": self.debugger.log( "Captcha not supported. Use session tokens instead.") raise ValueError("Captcha detected") from exc raise exc if auth.access_token is not None: self.config["Authorization"] = auth.access_token if auth.session_token is not None: self.config["session_token"] = auth.session_token else: possible_tokens = auth.session.cookies.get( "__Secure-next-auth.session-token", ) if possible_tokens is not None: if len(possible_tokens) > 1: self.config["session_token"] = possible_tokens[0] else: self.config["session_token"] = possible_tokens self.__refresh_headers() else: raise Exception("Error logging in") class asyncChatBot(Chatbot): async def __get_chat_stream(self, data) -> None: """ Generator for the chat stream -- Internal use only :param data: The data to send :type data: :obj:`dict` :return: None """ s = httpx.AsyncClient() async with s.stream( 'POST', self.base_url + "backend-api/conversation", headers=self.headers, data=json.dumps(data), timeout=self.request_timeout, ) as response: async for line in response.aiter_lines(): try: line = line[:-1] if line == "" or line == "data: [DONE]": continue line = line[6:] line = json.loads(line) if len(line["message"]["content"]["parts"]) == 0: continue message = line["message"]["content"]["parts"][0] self.conversation_id = line["conversation_id"] self.parent_id = line["message"]["id"] yield { "message": message, "conversation_id": self.conversation_id, "parent_id": self.parent_id, } except Exception as exc: self.debugger.log( f"Error when handling response, got values{line}") raise Exception( f"Error when handling response, got values{line}") from exc async def __get_chat_text(self, data) -> dict: """ Get the chat response as text -- Internal use only :param data: The data to send :type data: :obj:`dict` :return: The chat response :rtype: :obj:`dict` """ # Create request session s = httpx.Client(http2=True) async with httpx.AsyncClient() as s: # set headers s.headers = self.headers # Set multiple cookies if "session_token" in self.config: s.cookies.set( "__Secure-next-auth.session-token", self.config["session_token"], ) s.cookies.set( "__Secure-next-auth.callback-url", self.base_url, ) # Set proxies if self.config.get("proxy", "") != "": s.proxies = { "http": self.config["proxy"], "https": self.config["proxy"], } response = await s.post( self.base_url + "backend-api/conversation", data=json.dumps(data), timeout=self.request_timeout, ) # Check for expired token if 'detail' in response.json().keys(): if 'code' in response['detail']: if response['detail']['code'] == "invalid_api_key" or response['detail']['code'] == "token_expired": self.refresh_session() return self.__get_chat_text(data) response = response.text.splitlines()[-4] response = response[6:] response = json.loads(response) self.parent_id = response["message"]["id"] self.conversation_id = response["conversation_id"] message = response["message"]["content"]["parts"][0] return { "message": message, "conversation_id": self.conversation_id, "parent_id": self.parent_id, } async def get_chat_response(self, prompt: str, output="text") -> dict or None: """ Get the chat response. :param prompt: The message sent to the chatbot :type prompt: :obj:`str` :param output: The output type `text` or `stream` :type output: :obj:`str`, optional :return: The chat response `{"message": "Returned messages", "conversation_id": "conversation ID", "parent_id": "parent ID"}` or None :rtype: :obj:`dict` or :obj:`None` """ data = { "action": "next", "messages": [ { "id": str(generate_uuid()), "role": "user", "content": {"content_type": "text", "parts": [prompt]}, }, ], "conversation_id": self.conversation_id, "parent_message_id": self.parent_id, "model": "text-davinci-002-render", } self.conversation_id_prev = self.conversation_id self.parent_id_prev = self.parent_id if output == "text": return await self.__get_chat_text(data) elif output == "stream": return self.__get_chat_stream(data) else: raise ValueError("Output must be either 'text' or 'stream'") def rollback_conversation(self) -> None: """ Rollback the conversation. :return: None """ self.conversation_id = self.conversation_id_prev self.parent_id = self.parent_id_prev
[ "text", "content_type" ]
2024-01-10
steedos/steedos-agentgpt
platform~reworkd_platform~web~api~agent~agent_service~open_ai_agent_service.py
from typing import List, Optional from lanarky.responses import StreamingResponse from langchain.callbacks.base import AsyncCallbackHandler from langchain.output_parsers import PydanticOutputParser from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate from loguru import logger from pydantic import ValidationError from reworkd_platform.schemas import ModelSettings from reworkd_platform.services.tokenizer.token_service import TokenService from reworkd_platform.web.api.agent.agent_service.agent_service import AgentService from reworkd_platform.web.api.agent.analysis import Analysis, AnalysisArguments from reworkd_platform.web.api.agent.helpers import ( call_model_with_handling, openai_error_handler, parse_with_handling, ) from reworkd_platform.web.api.agent.model_settings import WrappedChatOpenAI from reworkd_platform.web.api.agent.prompts import ( analyze_task_prompt, create_tasks_prompt, start_goal_prompt, ) from reworkd_platform.web.api.agent.task_output_parser import TaskOutputParser from reworkd_platform.web.api.agent.tools.open_ai_function import get_tool_function from reworkd_platform.web.api.agent.tools.tools import ( get_default_tool, get_tool_from_name, get_tool_name, get_user_tools, ) from reworkd_platform.web.api.errors import OpenAIError from reworkd_platform.web.api.memory.memory import AgentMemory class OpenAIAgentService(AgentService): def __init__( self, model: WrappedChatOpenAI, settings: ModelSettings, agent_memory: AgentMemory, token_service: TokenService, callbacks: Optional[List[AsyncCallbackHandler]], ): self.model = model self.agent_memory = agent_memory self.settings = settings self.token_service = token_service self.callbacks = callbacks async def start_goal_agent(self, *, goal: str) -> List[str]: prompt = ChatPromptTemplate.from_messages( [SystemMessagePromptTemplate(prompt=start_goal_prompt)] ) self.token_service.calculate_max_tokens( self.model, prompt.format_prompt( goal=goal, language=self.settings.language, ).to_string(), ) completion = await call_model_with_handling( self.model, ChatPromptTemplate.from_messages( [SystemMessagePromptTemplate(prompt=start_goal_prompt)] ), {"goal": goal, "language": self.settings.language}, settings=self.settings, callbacks=self.callbacks, ) task_output_parser = TaskOutputParser(completed_tasks=[]) tasks = parse_with_handling(task_output_parser, completion) with self.agent_memory as memory: memory.reset_class() memory.add_tasks(tasks) return tasks async def analyze_task_agent( self, *, goal: str, task: str, tool_names: List[str] ) -> Analysis: functions = list(map(get_tool_function, get_user_tools(tool_names))) prompt = analyze_task_prompt.format_prompt( goal=goal, task=task, language=self.settings.language, ) self.token_service.calculate_max_tokens( self.model, prompt.to_string(), str(functions), ) message = await openai_error_handler( func=self.model.apredict_messages, messages=prompt.to_messages(), functions=functions, settings=self.settings, callbacks=self.callbacks, ) function_call = message.additional_kwargs.get("function_call", {}) completion = function_call.get("arguments", "") try: pydantic_parser = PydanticOutputParser(pydantic_object=AnalysisArguments) analysis_arguments = parse_with_handling(pydantic_parser, completion) return Analysis( action=function_call.get("name", get_tool_name(get_default_tool())), **analysis_arguments.dict(), ) except (OpenAIError, ValidationError): return Analysis.get_default_analysis() async def execute_task_agent( self, *, goal: str, task: str, analysis: Analysis, ) -> StreamingResponse: # TODO: More mature way of calculating max_tokens if self.model.max_tokens > 3000: self.model.max_tokens = max(self.model.max_tokens - 1000, 3000) tool_class = get_tool_from_name(analysis.action) return await tool_class(self.model, self.settings.language).call( goal, task, analysis.arg ) async def create_tasks_agent( self, *, goal: str, tasks: List[str], last_task: str, result: str, completed_tasks: Optional[List[str]] = None, ) -> List[str]: prompt = ChatPromptTemplate.from_messages( [SystemMessagePromptTemplate(prompt=create_tasks_prompt)] ) args = { "goal": goal, "language": self.settings.language, "tasks": "\n".join(tasks), "lastTask": last_task, "result": result, } self.token_service.calculate_max_tokens( self.model, prompt.format_prompt(**args).to_string() ) completion = await call_model_with_handling( self.model, prompt, args, settings=self.settings, callbacks=self.callbacks ) previous_tasks = (completed_tasks or []) + tasks tasks = [completion] if completion not in previous_tasks else [] unique_tasks = [] with self.agent_memory as memory: for task in tasks: similar_tasks = memory.get_similar_tasks(task) # Check if similar tasks are found if not similar_tasks: unique_tasks.append(task) else: logger.info(f"Similar tasks to '{task}' found: {similar_tasks}") if unique_tasks: memory.add_tasks(unique_tasks) return unique_tasks
[]
2024-01-10
jimmyliao/GPT-Azure-Search-Engine
app~pages~2_Tabular_Data_(Preview).py
import streamlit as st import os import pandas as pd from langchain.llms import AzureOpenAI from langchain.chat_models import AzureChatOpenAI from langchain.agents import create_pandas_dataframe_agent from langchain.agents import create_csv_agent def sidebar(): with st.sidebar: st.markdown(""" **GPT-4 Tabular data Q&A** allows you to ask questions to your Tabular CSV files. """ ) st.markdown("**Note**: GPT-4 is in preview and with limited availability. There is a lot of limitation on the API, so it takes longer than needed and it fails some times. Retry if it fails. ") st.markdown("---") st.session_state["AZURE_OPENAI_GPT4_NAME"] = st.text_input("Enter your GPT-4 deployment name:") st.session_state["AZURE_OPENAI_ENDPOINT"] = st.text_input("Enter your Azure OpenAI Endpoint:") st.session_state["AZURE_OPENAI_API_KEY"] = st.text_input("Enter Azure OpenAI Key:", type="password") preffix = 'First set the pandas display options to show all the columns, get the column names, then answer the question: ' suffix = '. ALWAYS before giving the Final Answer, try another method. Then reflect on the answers of the two methods you did and ask yourself if it answers correctly the original question. If you are not sure, try another method. \n If the runs does not give the same result, reflect and try again two more times until you have two runs that have the same result. If you still cannot arrive to a consistent result, say that you are not sure of the answer. But, if you are sure of the correct answer, create a beautiful and thorough response. DO NOT MAKE UP AN ANSWER OR USE PRIOR KNOWLEDGE, ONLY USE THE RESULTS OF THE CALCULATIONS YOU HAVE DONE. ALWAYS, as part of your final answer, explain how you got to the answer on a section that starts with: "\n\nExplanation:\n."' max_retries = 5 st.set_page_config(page_title="GPT Tabular data Q&A", page_icon="📖", layout="wide") st.header("GPT Tabular data Q&A (preview)") sidebar() def clear_submit(): st.session_state["submit"] = False col1, col2 = st.columns([1,1]) with col1: uploaded_file = st.file_uploader(label = "Upload your tabular CSV file", type="csv", accept_multiple_files=False, key=None, help="Upload your CSV file that contains tabular data, make sure that the first row corresponds to the columns", on_change=None, disabled=False) with col2: st.markdown("Or pick this sample dataset:") st.markdown("[Info about Covid Tracking Project](https://covidtracking.com) ") st.markdown("[Download CSV file](https://covidtracking.com/data/download/all-states-history.csv)") if uploaded_file is not None: df = pd.read_csv(uploaded_file) st.write("Here is the first two rows of your file:", df.head(2)) query_str = st.text_input("Ask a question:", value="Tell me important things you see in this file", on_change=clear_submit) qbutton = st.button('Generate Answer') st.session_state["submit"] = True placeholder = st.empty() if qbutton or st.session_state.get("submit"): if not query_str: st.error("Please enter a question") else: if not st.session_state.get("AZURE_OPENAI_ENDPOINT"): st.error("Please set your Azure OpenAI API Endpoint on the side bar!") elif not st.session_state.get("AZURE_OPENAI_API_KEY"): st.error("Please configure your Azure OpenAI API key on the side bar!") elif not st.session_state.get("AZURE_OPENAI_GPT4_NAME"): st.error("Please configure your GPT-4 Deployment Name in the sidebar") else: os.environ["OPENAI_API_BASE"] = os.environ["AZURE_OPENAI_ENDPOINT"] = st.session_state["AZURE_OPENAI_ENDPOINT"] os.environ["OPENAI_API_KEY"] = os.environ["AZURE_OPENAI_API_KEY"] = st.session_state["AZURE_OPENAI_API_KEY"] os.environ["OPENAI_API_VERSION"] = os.environ["AZURE_OPENAI_API_VERSION"] = "2023-03-15-preview" llm = AzureChatOpenAI(deployment_name=st.session_state["AZURE_OPENAI_GPT4_NAME"], temperature=0.5, max_tokens=999) agent = create_pandas_dataframe_agent(llm, df, verbose=True) try: with st.spinner("Coming up with an answer... ⏳"): for i in range(max_retries): try: response = agent.run(preffix + query_str + suffix) break except: response = ":warning: **Too many failed retries. Try Again** - RateLimitError: Requests to the Creates a completion for the chat message Operation under Azure OpenAI API version 2023-03-15-preview have exceeded call rate limit of your current OpenAI S0 pricing tier. Please retry after 6 seconds. Please contact Azure support service if you would like to further increase the default rate limit.." continue with placeholder.container(): st.markdown("#### Answer") st.markdown(response.replace("$","\$")) except Exception as e: st.error(e)
[]