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
JasonCoawette/Professor-GPT
imports.py
from dotenv import load_dotenv from PyPDF2 import PdfReader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain import PromptTemplate from langchain.prompts import ChatPromptTemplate from langchain.chains.question_answering import load_qa_chain from langchain.chat_models import ChatOpenAI from langchain.callbacks import get_openai_callback import streamlit as st
[]
2024-01-10
el-Migu-el/CapstoneProject
webpages~pages_util~login.py
import streamlit as st import hmac import os from openai import OpenAI, AuthenticationError import pandas as pd from webpages.pages_util.util import CUSTOMER_DATA_PATH def is_valid_api_key(api_key): messages = [{"role": "user", "content": "Hello!"}] try: client = OpenAI(api_key=api_key) completion = client.chat.completions.create( model="gpt-3.5-turbo-1106", messages=messages, ) return True except AuthenticationError as e: return False def login(): """Returns True if the password is correct, otherwise returns False.""" def load_user_data(): """Load user data from a CSV file using pandas.""" user_data = {} credentials = {} customer_data = pd.read_csv(CUSTOMER_DATA_PATH) for index, row in customer_data.iterrows(): credentials[row['Username']] = row['Password'] user_data[row['Username']] = {'Email': row['Email'], 'Username': row['Username'], 'Full Name': row['Full Name'], 'Age': row['Age'], 'Location': row['Location'], 'Bot Preferences': row['Bot Preferences']} return credentials, user_data def login_form(): """Form with widgets to collect user information""" with st.form("Login Form"): username = st.text_input("Username", key="username") password = st.text_input("Password", type="password", key="password") api_key = st.text_input("Enter your GPT API key", type="password") os.environ["OPENAI_API_KEY"] = api_key.lstrip('"').rstrip('"') if st.form_submit_button("Log in") and username and password and api_key: if is_valid_api_key(api_key): password_entered() else: st.warning("Invalid API key. Please enter a valid GPT API key.") else: st.warning("Please enter all credentials.") def password_entered(): """Checks whether a password entered by the user is correct.""" credentials, user_data = load_user_data() # because we use st.stop, even if this was run outside of check_password, this would always be re-executed if password was incorrect, so I left it here for easier reading if st.session_state["username"] in credentials and hmac.compare_digest( st.session_state["password"], credentials[st.session_state["username"]], ): st.session_state["logged_in"] = True del st.session_state["password"] # Don't store the password. st.session_state["user_data"] = user_data[st.session_state["username"]] st.session_state["logging_in"] = False st.rerun() else: st.session_state["logged_in"] = False st.error("😕 User not known or password incorrect") login_form() go_back = st.button('Go Back') if go_back: st.session_state["logging_in"] = False st.rerun() # Return True if the username + password is validated, otherwise False. return st.session_state.get("logged_in", False) def signup(): """Sign up for an account.""" def sign_up_form(): """Display the sign-up form.""" with st.form("Sign Up Form"): email = st.text_input('Email') username = st.text_input('Username') password = st.text_input('Password', type='password') repeat_password = st.text_input('Please Repeat the Password', type='password') full_name = st.text_input('First and Last Name') age = st.number_input('Age', min_value=0, max_value=120) location = st.text_input('Location') bot_preferences = st.text_input('Bot Preferences (e.g. "Talk like a butler")') submit_button = st.form_submit_button('Submit') if submit_button and email and username and password and repeat_password and full_name and age and location and bot_preferences: info_submitted(email, username, password, repeat_password, full_name, age, location, bot_preferences) else: st.warning("Please enter all details.") def info_submitted(email, username, password, repeat_password, full_name, age, location, bot_preferences): """Process the submitted information.""" # Check if all fields are filled and passwords match customer_data = pd.read_csv(CUSTOMER_DATA_PATH) if password != repeat_password or len(password) < 5: st.error('Passwords do not match or are too short. Please try again.') elif username in customer_data['Username'].values: st.error('Username is already taken. Please choose a different one.') elif email in customer_data['Email'].values: st.error('Email is already taken. Please choose a different one.') else: # Insert new row into the customer_data DataFrame new_row = {'Full Name': full_name, 'Username': username, 'Email': email, 'Password': password, 'Age': age, 'Location': location, 'Favorites': [], 'Bot Preferences': bot_preferences} customer_data = pd.concat([customer_data, pd.DataFrame([new_row])], ignore_index=True) # Save the updated DataFrame to the CSV file customer_data.to_csv(CUSTOMER_DATA_PATH, index=False) st.session_state['signup_successful'] = True st.session_state["signing_up"] = False st.session_state["logging_in"] = True st.rerun() sign_up_form() go_back = st.button('Go Back') if go_back: st.session_state["signing_up"] = False st.rerun() return st.session_state.get("signup_successful", False) def login_signup(): st.markdown( '<p style="font-size:20px; font-weight:bold;">Please log in or create an account to start using AutoMentor!</p>', unsafe_allow_html=True) login_btn_placeholder = st.empty() signup_btn_placeholder = st.empty() login_btn = login_btn_placeholder.button('Login') signup_btn = signup_btn_placeholder.button('Sign Up') if login_btn: st.session_state["logging_in"] = True if signup_btn: st.session_state["signing_up"] = True if st.session_state.get("signup_successful", False): st.success('Account successfully created! Please log in.') if st.session_state.get("logging_in", False): login_btn_placeholder.empty() signup_btn_placeholder.empty() successful_login = login() if not successful_login: st.stop() if st.session_state.get("signing_up", False): login_btn_placeholder.empty() signup_btn_placeholder.empty() successful_signup = signup() if not successful_signup: st.stop() st.write("---") st.caption("© 2024 AutoMentor | All rights reserved")
[ "Hello!" ]
2024-01-10
israel-cj/caafe_extension
caafe~caafe.py
import copy import numpy as np import openai from sklearn.model_selection import RepeatedKFold from .caafe_evaluate import ( evaluate_dataset, ) from .run_llm_code import run_llm_code, run_llm_code_preprocessing """ Here we are going to modify the code for data preprocessing """ def get_prompt_preprocessing( df, ds, iterative=1, data_description_unparsed=None, samples=None, **kwargs ): return f""" The dataframe `df` is loaded and in memory. Description of the dataset in `df`: "{data_description_unparsed}" Data cleaning is an essential step in preparing datasets for machine learning. This code was written by an expert data scientist working to improve predictions by cleaning the dataset. It is a snippet of code that may do one or more or the next features: Number of samples (rows) in training dataset: {int(len(df))}, number of features (columns) in training dataset {len(list(df.columns))} This code generates a cleaner dataset (if necessary) that is useful for a downstream classification algorithm (such as XGBoost) predicting \"{ds[4][-1]}\". Here are some of the most common procedures and techniques used in data cleaning for machine learning that can be applied: Encoding categorical variable, handling missing values by replacing them with statistical measures like mean, median, or mode, dealing with outliers, removing duplicate records, handling skewed or imbalanced data, standardizing or normalizing features. The classifier will be trained on the resulting cleaned dataset and evaluated on a holdout set. The evaluation metric is accuracy. The best-performing code will be selected. All the packages/libraries to perform such preprocessing should be called. In addition, make sure to always perform a ‘feature importance’ step if the number of features (columns) >= 90, keeping only the 70 most representative. If number of columns <= 90 you can keep all the features. General code formatting for each added step: ```python # (Preprocessing step name and description) # Usefulness: (Description why this procedure adds useful real world knowledge to classify \"{ds[4][-1]}\" according to dataset description.) ```end Code formatting for a preprocessing step, e.g. Encoding Categorical Variables: ```python # (Procedure name and description) # Explanation why this step is necessary from sklearn import preprocessing for col in df.columns: if df[col].dtype == 'object': le = preprocessing.LabelEncoder() df[col] = le.fit_transform(df[col]) ```end Code formatting for preprocessing step, e.g. replace nan values with mean: ```python # (Procedure name and description) # Explanation why this step is necessary df.fillna(df.mean()) ```end Each codeblock generates exactly one cleaning step. Each codeblock ends with ```end and starts with "```python" Note: for the feature importance step and other ones, if needed, the column name we want to classify is \"{ds[4][-1]}\", take it into account when generating code. Independently of the process performed, keep the original name of the columns/features. Codeblock: """ # Each codeblock either generates {how_many} or drops bad columns (Feature selection). def build_prompt_from_df_preprocesing(ds, df, iterative=1): data_description_unparsed = ds[-1] feature_importance = {} # xgb_eval(_obj) # Apply dimensionality reduction to the dataset before using it or including it in the prompt? # kwargs = { # "data_description_unparsed": data_description_unparsed, # "samples": samples, # "feature_importance": { # k: "%s" % float("%.2g" % feature_importance[k]) for k in feature_importance # }, # } prompt = get_prompt_preprocessing( df, ds, data_description_unparsed=data_description_unparsed, iterative=iterative, ) return prompt def generate_features_preprocessing( ds, df, model="gpt-3.5-turbo", just_print_prompt=False, iterative=1, metric_used=None, iterative_method="logistic", display_method="markdown", n_splits=10, n_repeats=2, ): def format_for_display(code): code = code.replace("```python", "").replace("```", "").replace("<end>", "") return code if display_method == "markdown": from IPython.display import display, Markdown display_method = lambda x: display(Markdown(x)) else: display_method = print assert ( iterative == 1 or metric_used is not None ), "metric_used must be set if iterative" prompt = build_prompt_from_df_preprocesing(ds, df, iterative=iterative) # up to here if just_print_prompt: code, prompt = None, prompt return code, prompt, None def generate_code_preprocessing(messages): if model == "skip": return "" completion = openai.ChatCompletion.create( model=model, messages=messages, stop=["```end"], temperature=0.5, max_tokens=500, ) code = completion["choices"][0]["message"]["content"] code = code.replace("```python", "").replace("```", "").replace("<end>", "") return code def execute_and_evaluate_code_block_preprocessing(full_code, code): old_accs, old_rocs, accs, rocs = [], [], [], [] ss = RepeatedKFold(n_splits=n_splits, n_repeats=n_repeats, random_state=0) for (train_idx, valid_idx) in ss.split(df): df_train, df_valid = df.iloc[train_idx], df.iloc[valid_idx] # # Remove target column from df_train # This is because we received the whole dataset for training # target_train = df_train[ds[4][-1]] # target_valid = df_valid[ds[4][-1]] #df_train = df_train.drop(columns=[ds[4][-1]]) #df_valid = df_valid.drop(columns=[ds[4][-1]]) df_train_extended = copy.deepcopy(df_train) df_valid_extended = copy.deepcopy(df_valid) try: df_train = run_llm_code_preprocessing( full_code, df_train, ) df_valid = run_llm_code_preprocessing( full_code, df_valid, ) df_train_extended = run_llm_code_preprocessing( full_code + "\n" + code, df_train_extended, ) df_valid_extended = run_llm_code_preprocessing( full_code + "\n" + code, df_valid_extended, ) except Exception as e: display_method(f"Error in code execution. {type(e)} {e}") display_method(f"```python\n{format_for_display(code)}\n```\n") return e, None, None, None, None # # Add target column back to df_train # df_train[ds[4][-1]] = target_train # df_valid[ds[4][-1]] = target_valid # df_train_extended[ds[4][-1]] = target_train # df_valid_extended[ds[4][-1]] = target_valid from contextlib import contextmanager import sys, os with open(os.devnull, "w") as devnull: old_stdout = sys.stdout sys.stdout = devnull try: result_old = evaluate_dataset( df_train=df_train, df_test=df_valid, prompt_id="XX", name=ds[0], method=iterative_method, metric_used=metric_used, seed=0, target_name=ds[4][-1], ) result_extended = evaluate_dataset( df_train=df_train_extended, df_test=df_valid_extended, prompt_id="XX", name=ds[0], method=iterative_method, metric_used=metric_used, seed=0, target_name=ds[4][-1], ) finally: sys.stdout = old_stdout old_accs += [result_old["roc"]] old_rocs += [result_old["acc"]] accs += [result_extended["roc"]] rocs += [result_extended["acc"]] return None, rocs, accs, old_rocs, old_accs messages_preprocessing = [ { "role": "system", "content": "You are an expert datascientist assistant solving Kaggle problems. You answer only by generating code. Answer as concisely as possible.", }, { "role": "user", "content": prompt, }, ] display_method(f"*Dataset description:*\n {ds[-1]}") n_iter = iterative full_code = "" i = 0 while i < n_iter: try: code = generate_code_preprocessing(messages_preprocessing) except Exception as e: display_method("Error in LLM API." + str(e)) continue i = i + 1 e, rocs, accs, old_rocs, old_accs = execute_and_evaluate_code_block_preprocessing( full_code, code ) if e is not None: messages_preprocessing += [ {"role": "assistant", "content": code}, { "role": "user", "content": f"""Code execution failed with error: {type(e)} {e}.\n Code: ```python{code}```\n Generate next feature (fixing error?): ```python """, }, ] continue # importances = get_leave_one_out_importance( # df_train_extended, # df_valid_extended, # ds, # iterative_method, # metric_used, # ) # """ROC Improvement by using each feature: {importances}""" improvement_roc = np.nanmean(rocs) - np.nanmean(old_rocs) improvement_acc = np.nanmean(accs) - np.nanmean(old_accs) add_feature = True add_feature_sentence = "The code was executed and changes to ´df´ were kept." if improvement_roc + improvement_acc <= 0: add_feature = False add_feature_sentence = f"The last code changes to ´df´ were discarded. (Improvement: {improvement_roc + improvement_acc})" display_method( "\n" + f"*Iteration {i}*\n" + f"```python\n{format_for_display(code)}\n```\n" + f"Performance before adding features ROC {np.nanmean(old_rocs):.3f}, ACC {np.nanmean(old_accs):.3f}.\n" + f"Performance after adding features ROC {np.nanmean(rocs):.3f}, ACC {np.nanmean(accs):.3f}.\n" + f"Improvement ROC {improvement_roc:.3f}, ACC {improvement_acc:.3f}.\n" + f"{add_feature_sentence}\n" + f"\n" ) if len(code) > 10: messages_preprocessing += [ {"role": "assistant", "content": code}, { "role": "user", "content": f"""Performance after adding feature ROC {np.nanmean(rocs):.3f}, ACC {np.nanmean(accs):.3f}. {add_feature_sentence} Next codeblock: """, }, ] if add_feature: full_code += code return full_code, prompt, messages_preprocessing """ Here is the original code for feature engineering """ def get_prompt( df, ds, iterative=1, data_description_unparsed=None, samples=None, **kwargs ): how_many = ( "up to 10 useful columns. Generate as many features as useful for downstream classifier, but as few as necessary to reach good performance." if iterative == 1 else "exactly one useful column" ) return f""" The dataframe `df` is loaded and in memory. Columns are also named attributes. Description of the dataset in `df` (column dtypes might be inaccurate): "{data_description_unparsed}" Columns in `df` (true feature dtypes listed here, categoricals encoded as int): {samples} This code was written by an expert datascientist working to improve predictions. It is a snippet of code that adds new columns to the dataset. Number of samples (rows) in training dataset: {int(len(df))} This code generates additional columns that are useful for a downstream classification algorithm (such as XGBoost) predicting \"{ds[4][-1]}\". Additional columns add new semantic information, that is they use real world knowledge on the dataset. They can e.g. be feature combinations, transformations, aggregations where the new column is a function of the existing columns. The scale of columns and offset does not matter. Make sure all used columns exist. Follow the above description of columns closely and consider the datatypes and meanings of classes. This code also drops columns, if these may be redundant and hurt the predictive performance of the downstream classifier (Feature selection). Dropping columns may help as the chance of overfitting is lower, especially if the dataset is small. The classifier will be trained on the dataset with the generated columns and evaluated on a holdout set. The evaluation metric is accuracy. The best performing code will be selected. Added columns can be used in other codeblocks, dropped columns are not available anymore. Code formatting for each added column: ```python # (Feature name and description) # Usefulness: (Description why this adds useful real world knowledge to classify \"{ds[4][-1]}\" according to dataset description and attributes.) # Input samples: (Three samples of the columns used in the following code, e.g. '{df.columns[0]}': {list(df.iloc[:3, 0].values)}, '{df.columns[1]}': {list(df.iloc[:3, 1].values)}, ...) (Some pandas code using {df.columns[0]}', '{df.columns[1]}', ... to add a new column for each row in df) ```end Code formatting for dropping columns: ```python # Explanation why the column XX is dropped df.drop(columns=['XX'], inplace=True) ```end Each codeblock generates {how_many} and can drop unused columns (Feature selection). Each codeblock ends with ```end and starts with "```python" Codeblock: """ # Each codeblock either generates {how_many} or drops bad columns (Feature selection). def build_prompt_from_df(ds, df, iterative=1): data_description_unparsed = ds[-1] feature_importance = {} # xgb_eval(_obj) samples = "" df_ = df.head(10) for i in list(df_): # show the list of values nan_freq = "%s" % float("%.2g" % (df[i].isna().mean() * 100)) s = df_[i].tolist() if str(df[i].dtype) == "float64": s = [round(sample, 2) for sample in s] samples += ( f"{df_[i].name} ({df[i].dtype}): NaN-freq [{nan_freq}%], Samples {s}\n" ) kwargs = { "data_description_unparsed": data_description_unparsed, "samples": samples, "feature_importance": { k: "%s" % float("%.2g" % feature_importance[k]) for k in feature_importance }, } prompt = get_prompt( df, ds, data_description_unparsed=data_description_unparsed, iterative=iterative, samples=samples, ) return prompt def generate_features( ds, df, model="gpt-3.5-turbo", just_print_prompt=False, iterative=1, metric_used=None, iterative_method="logistic", display_method="markdown", n_splits=10, n_repeats=2, ): def format_for_display(code): code = code.replace("```python", "").replace("```", "").replace("<end>", "") return code if display_method == "markdown": from IPython.display import display, Markdown display_method = lambda x: display(Markdown(x)) else: display_method = print assert ( iterative == 1 or metric_used is not None ), "metric_used must be set if iterative" prompt = build_prompt_from_df(ds, df, iterative=iterative) if just_print_prompt: code, prompt = None, prompt return code, prompt, None def generate_code(messages): if model == "skip": return "" completion = openai.ChatCompletion.create( model=model, messages=messages, stop=["```end"], temperature=0.5, max_tokens=500, ) code = completion["choices"][0]["message"]["content"] code = code.replace("```python", "").replace("```", "").replace("<end>", "") return code def execute_and_evaluate_code_block(full_code, code): old_accs, old_rocs, accs, rocs = [], [], [], [] ss = RepeatedKFold(n_splits=n_splits, n_repeats=n_repeats, random_state=0) for (train_idx, valid_idx) in ss.split(df): df_train, df_valid = df.iloc[train_idx], df.iloc[valid_idx] # Remove target column from df_train target_train = df_train[ds[4][-1]] target_valid = df_valid[ds[4][-1]] df_train = df_train.drop(columns=[ds[4][-1]]) df_valid = df_valid.drop(columns=[ds[4][-1]]) df_train_extended = copy.deepcopy(df_train) df_valid_extended = copy.deepcopy(df_valid) try: df_train = run_llm_code( full_code, df_train, convert_categorical_to_integer=not ds[0].startswith("kaggle"), ) df_valid = run_llm_code( full_code, df_valid, convert_categorical_to_integer=not ds[0].startswith("kaggle"), ) df_train_extended = run_llm_code( full_code + "\n" + code, df_train_extended, convert_categorical_to_integer=not ds[0].startswith("kaggle"), ) df_valid_extended = run_llm_code( full_code + "\n" + code, df_valid_extended, convert_categorical_to_integer=not ds[0].startswith("kaggle"), ) except Exception as e: display_method(f"Error in code execution. {type(e)} {e}") display_method(f"```python\n{format_for_display(code)}\n```\n") return e, None, None, None, None # Add target column back to df_train df_train[ds[4][-1]] = target_train df_valid[ds[4][-1]] = target_valid df_train_extended[ds[4][-1]] = target_train df_valid_extended[ds[4][-1]] = target_valid from contextlib import contextmanager import sys, os with open(os.devnull, "w") as devnull: old_stdout = sys.stdout sys.stdout = devnull try: result_old = evaluate_dataset( df_train=df_train, df_test=df_valid, prompt_id="XX", name=ds[0], method=iterative_method, metric_used=metric_used, seed=0, target_name=ds[4][-1], ) result_extended = evaluate_dataset( df_train=df_train_extended, df_test=df_valid_extended, prompt_id="XX", name=ds[0], method=iterative_method, metric_used=metric_used, seed=0, target_name=ds[4][-1], ) finally: sys.stdout = old_stdout old_accs += [result_old["roc"]] old_rocs += [result_old["acc"]] accs += [result_extended["roc"]] rocs += [result_extended["acc"]] return None, rocs, accs, old_rocs, old_accs messages = [ { "role": "system", "content": "You are an expert datascientist assistant solving Kaggle problems. You answer only by generating code. Answer as concisely as possible.", }, { "role": "user", "content": prompt, }, ] display_method(f"*Dataset description:*\n {ds[-1]}") n_iter = iterative full_code = "" i = 0 while i < n_iter: try: code = generate_code(messages) except Exception as e: display_method("Error in LLM API." + str(e)) continue i = i + 1 e, rocs, accs, old_rocs, old_accs = execute_and_evaluate_code_block( full_code, code ) if e is not None: messages += [ {"role": "assistant", "content": code}, { "role": "user", "content": f"""Code execution failed with error: {type(e)} {e}.\n Code: ```python{code}```\n Generate next feature (fixing error?): ```python """, }, ] continue # importances = get_leave_one_out_importance( # df_train_extended, # df_valid_extended, # ds, # iterative_method, # metric_used, # ) # """ROC Improvement by using each feature: {importances}""" improvement_roc = np.nanmean(rocs) - np.nanmean(old_rocs) improvement_acc = np.nanmean(accs) - np.nanmean(old_accs) add_feature = True add_feature_sentence = "The code was executed and changes to ´df´ were kept." if improvement_roc + improvement_acc <= 0: add_feature = False add_feature_sentence = f"The last code changes to ´df´ were discarded. (Improvement: {improvement_roc + improvement_acc})" display_method( "\n" + f"*Iteration {i}*\n" + f"```python\n{format_for_display(code)}\n```\n" + f"Performance before adding features ROC {np.nanmean(old_rocs):.3f}, ACC {np.nanmean(old_accs):.3f}.\n" + f"Performance after adding features ROC {np.nanmean(rocs):.3f}, ACC {np.nanmean(accs):.3f}.\n" + f"Improvement ROC {improvement_roc:.3f}, ACC {improvement_acc:.3f}.\n" + f"{add_feature_sentence}\n" + f"\n" ) if len(code) > 10: messages += [ {"role": "assistant", "content": code}, { "role": "user", "content": f"""Performance after adding feature ROC {np.nanmean(rocs):.3f}, ACC {np.nanmean(accs):.3f}. {add_feature_sentence} Next codeblock: """, }, ] if add_feature: full_code += code return full_code, prompt, messages
[ "You are an expert datascientist assistant solving Kaggle problems. You answer only by generating code. Answer as concisely as possible.", "Code execution failed with error: <class '__main__.Placeholder'> PLACEHOLDER.\n Code: ```pythonPLACEHOLDER```\n Generate next feature (fixing error?):\n ```python\n " ]
2024-01-10
theofpa/haystack
haystack~nodes~retriever~_embedding_encoder.py
import json import logging from abc import abstractmethod from pathlib import Path from typing import Optional, TYPE_CHECKING, Any, Callable, Dict, List, Union import numpy as np import requests import torch from sentence_transformers import InputExample from torch.utils.data import DataLoader from torch.utils.data.sampler import SequentialSampler from tqdm.auto import tqdm from transformers import AutoModel, AutoTokenizer from haystack.document_stores.base import BaseDocumentStore from haystack.errors import OpenAIError, OpenAIRateLimitError, CohereError from haystack.modeling.data_handler.dataloader import NamedDataLoader from haystack.modeling.data_handler.dataset import convert_features_to_dataset, flatten_rename from haystack.modeling.infer import Inferencer from haystack.nodes.retriever._losses import _TRAINING_LOSSES from haystack.schema import Document from haystack.utils.reflection import retry_with_exponential_backoff if TYPE_CHECKING: from haystack.nodes.retriever import EmbeddingRetriever logger = logging.getLogger(__name__) class _BaseEmbeddingEncoder: @abstractmethod def embed_queries(self, queries: List[str]) -> np.ndarray: """ Create embeddings for a list of queries. :param queries: List of queries to embed. :return: Embeddings, one per input query, shape: (queries, embedding_dim) """ pass @abstractmethod def embed_documents(self, docs: List[Document]) -> np.ndarray: """ Create embeddings for a list of documents. :param docs: List of documents to embed. :return: Embeddings, one per input document, shape: (documents, embedding_dim) """ pass def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, ): """ Trains or adapts the underlying embedding model. Each training data example is a dictionary with the following keys: * question: The question string. * pos_doc: Positive document string (the document containing the answer). * neg_doc: Negative document string (the document that doesn't contain the answer). * score: The score margin the answer must fall within. :param training_data: The training data in a dictionary format. Required. :type training_data: List[Dict[str, Any]] :param learning_rate: The speed at which the model learns. Required. We recommend that you leave the default `2e-5` value. :type learning_rate: float :param n_epochs: The number of epochs (complete passes of the training data through the algorithm) that you want the model to go through. Required. :type n_epochs: int :param num_warmup_steps: The number of warmup steps for the model. Warmup steps are epochs when the learning rate is very low. You can use them at the beginning of the training to prevent early overfitting of your model. Required. :type num_warmup_steps: int :param batch_size: The batch size to use for the training. Optional. The default values is 16. :type batch_size: int (optional) """ pass def save(self, save_dir: Union[Path, str]): """ Save the model to the directory you specify. :param save_dir: The directory where the model is saved. Required. :type save_dir: Union[Path, str] """ pass def _check_docstore_similarity_function(self, document_store: BaseDocumentStore, model_name: str): """ Check that document_store uses a similarity function compatible with the embedding model """ if "sentence-transformers" in model_name.lower(): model_similarity = None if "-cos-" in model_name.lower(): model_similarity = "cosine" elif "-dot-" in model_name.lower(): model_similarity = "dot_product" if model_similarity is not None and document_store.similarity != model_similarity: logger.warning( f"You seem to be using {model_name} model with the {document_store.similarity} function instead of the recommended {model_similarity}. " f"This can be set when initializing the DocumentStore" ) elif "dpr" in model_name.lower() and document_store.similarity != "dot_product": logger.warning( f"You seem to be using a DPR model with the {document_store.similarity} function. " f"We recommend using dot_product instead. " f"This can be set when initializing the DocumentStore" ) class _DefaultEmbeddingEncoder(_BaseEmbeddingEncoder): def __init__(self, retriever: "EmbeddingRetriever"): self.embedding_model = Inferencer.load( retriever.embedding_model, revision=retriever.model_version, task_type="embeddings", extraction_strategy=retriever.pooling_strategy, extraction_layer=retriever.emb_extraction_layer, gpu=retriever.use_gpu, batch_size=retriever.batch_size, max_seq_len=retriever.max_seq_len, num_processes=0, use_auth_token=retriever.use_auth_token, ) if retriever.document_store: self._check_docstore_similarity_function( document_store=retriever.document_store, model_name=retriever.embedding_model ) def embed(self, texts: Union[List[List[str]], List[str], str]) -> np.ndarray: # TODO: FARM's `sample_to_features_text` need to fix following warning - # tokenization_utils.py:460: FutureWarning: `is_pretokenized` is deprecated and will be removed in a future version, use `is_split_into_words` instead. emb = self.embedding_model.inference_from_dicts(dicts=[{"text": t} for t in texts]) emb = np.stack([r["vec"] for r in emb]) return emb def embed_queries(self, queries: List[str]) -> np.ndarray: """ Create embeddings for a list of queries. :param queries: List of queries to embed. :return: Embeddings, one per input query, shape: (queries, embedding_dim) """ return self.embed(queries) def embed_documents(self, docs: List[Document]) -> np.ndarray: """ Create embeddings for a list of documents. :param docs: List of documents to embed. :return: Embeddings, one per input document, shape: (documents, embedding_dim) """ passages = [d.content for d in docs] return self.embed(passages) def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, ): raise NotImplementedError( "You can't train this retriever. You can only use the `train` method with sentence-transformers EmbeddingRetrievers." ) def save(self, save_dir: Union[Path, str]): raise NotImplementedError( "You can't save your record as `save` only works for sentence-transformers EmbeddingRetrievers." ) class _SentenceTransformersEmbeddingEncoder(_BaseEmbeddingEncoder): def __init__(self, retriever: "EmbeddingRetriever"): # pretrained embedding models coming from: https://github.com/UKPLab/sentence-transformers#pretrained-models # e.g. 'roberta-base-nli-stsb-mean-tokens' try: from sentence_transformers import SentenceTransformer except (ImportError, ModuleNotFoundError) as ie: from haystack.utils.import_utils import _optional_component_not_installed _optional_component_not_installed(__name__, "sentence", ie) self.embedding_model = SentenceTransformer( retriever.embedding_model, device=str(retriever.devices[0]), use_auth_token=retriever.use_auth_token ) self.batch_size = retriever.batch_size self.embedding_model.max_seq_length = retriever.max_seq_len self.show_progress_bar = retriever.progress_bar if retriever.document_store: self._check_docstore_similarity_function( document_store=retriever.document_store, model_name=retriever.embedding_model ) def embed(self, texts: Union[List[str], str]) -> np.ndarray: # texts can be a list of strings # get back list of numpy embedding vectors emb = self.embedding_model.encode( texts, batch_size=self.batch_size, show_progress_bar=self.show_progress_bar, convert_to_numpy=True ) return emb def embed_queries(self, queries: List[str]) -> np.ndarray: """ Create embeddings for a list of queries. :param queries: List of queries to embed. :return: Embeddings, one per input query, shape: (queries, embedding_dim) """ return self.embed(queries) def embed_documents(self, docs: List[Document]) -> np.ndarray: """ Create embeddings for a list of documents. :param docs: List of documents to embed. :return: Embeddings, one per input document, shape: (documents, embedding_dim) """ passages = [d.content for d in docs] return self.embed(passages) def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, train_loss: str = "mnrl", ): if train_loss not in _TRAINING_LOSSES: raise ValueError(f"Unrecognized train_loss {train_loss}. Should be one of: {_TRAINING_LOSSES.keys()}") st_loss = _TRAINING_LOSSES[train_loss] train_examples = [] for train_i in training_data: missing_attrs = st_loss.required_attrs.difference(set(train_i.keys())) if len(missing_attrs) > 0: raise ValueError( f"Some training examples don't contain the fields {missing_attrs} which are necessary when using the '{train_loss}' loss." ) texts = [train_i["question"], train_i["pos_doc"]] if "neg_doc" in train_i: texts.append(train_i["neg_doc"]) if "score" in train_i: train_examples.append(InputExample(texts=texts, label=train_i["score"])) else: train_examples.append(InputExample(texts=texts)) logger.info("Training/adapting %s with %s examples", self.embedding_model, len(train_examples)) train_dataloader = DataLoader(train_examples, batch_size=batch_size, drop_last=True, shuffle=True) train_loss = st_loss.loss(self.embedding_model) # Tune the model self.embedding_model.fit( train_objectives=[(train_dataloader, train_loss)], epochs=n_epochs, optimizer_params={"lr": learning_rate}, warmup_steps=int(len(train_dataloader) * 0.1) if num_warmup_steps is None else num_warmup_steps, ) def save(self, save_dir: Union[Path, str]): self.embedding_model.save(path=str(save_dir)) class _RetribertEmbeddingEncoder(_BaseEmbeddingEncoder): def __init__(self, retriever: "EmbeddingRetriever"): self.progress_bar = retriever.progress_bar self.batch_size = retriever.batch_size self.max_length = retriever.max_seq_len self.embedding_tokenizer = AutoTokenizer.from_pretrained( retriever.embedding_model, use_auth_token=retriever.use_auth_token ) self.embedding_model = AutoModel.from_pretrained( retriever.embedding_model, use_auth_token=retriever.use_auth_token ).to(str(retriever.devices[0])) def embed_queries(self, queries: List[str]) -> np.ndarray: """ Create embeddings for a list of queries. :param queries: List of queries to embed. :return: Embeddings, one per input query, shape: (queries, embedding_dim) """ query_text = [{"text": q} for q in queries] dataloader = self._create_dataloader(query_text) embeddings: List[np.ndarray] = [] disable_tqdm = True if len(dataloader) == 1 else not self.progress_bar for i, batch in enumerate(tqdm(dataloader, desc=f"Creating Embeddings", unit=" Batches", disable=disable_tqdm)): batch = {key: batch[key].to(self.embedding_model.device) for key in batch} with torch.inference_mode(): q_reps = ( self.embedding_model.embed_questions( input_ids=batch["input_ids"], attention_mask=batch["padding_mask"] ) .cpu() .numpy() ) embeddings.append(q_reps) return np.concatenate(embeddings) def embed_documents(self, docs: List[Document]) -> np.ndarray: """ Create embeddings for a list of documents. :param docs: List of documents to embed. :return: Embeddings, one per input document, shape: (documents, embedding_dim) """ doc_text = [{"text": d.content} for d in docs] dataloader = self._create_dataloader(doc_text) embeddings: List[np.ndarray] = [] disable_tqdm = True if len(dataloader) == 1 else not self.progress_bar for i, batch in enumerate(tqdm(dataloader, desc=f"Creating Embeddings", unit=" Batches", disable=disable_tqdm)): batch = {key: batch[key].to(self.embedding_model.device) for key in batch} with torch.inference_mode(): q_reps = ( self.embedding_model.embed_answers( input_ids=batch["input_ids"], attention_mask=batch["padding_mask"] ) .cpu() .numpy() ) embeddings.append(q_reps) return np.concatenate(embeddings) def _create_dataloader(self, text_to_encode: List[dict]) -> NamedDataLoader: dataset, tensor_names = self.dataset_from_dicts(text_to_encode) dataloader = NamedDataLoader( dataset=dataset, sampler=SequentialSampler(dataset), batch_size=self.batch_size, tensor_names=tensor_names ) return dataloader def dataset_from_dicts(self, dicts: List[dict]): texts = [x["text"] for x in dicts] tokenized_batch = self.embedding_tokenizer( texts, return_token_type_ids=True, return_attention_mask=True, max_length=self.max_length, truncation=True, padding=True, ) features_flat = flatten_rename( tokenized_batch, ["input_ids", "token_type_ids", "attention_mask"], ["input_ids", "segment_ids", "padding_mask"], ) dataset, tensornames = convert_features_to_dataset(features=features_flat) return dataset, tensornames def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, ): raise NotImplementedError( "You can't train this retriever. You can only use the `train` method with sentence-transformers EmbeddingRetrievers." ) def save(self, save_dir: Union[Path, str]): raise NotImplementedError( "You can't save your record as `save` only works for sentence-transformers EmbeddingRetrievers." ) class _OpenAIEmbeddingEncoder(_BaseEmbeddingEncoder): def __init__(self, retriever: "EmbeddingRetriever"): # See https://beta.openai.com/docs/guides/embeddings for more details self.url = "https://api.openai.com/v1/embeddings" self.api_key = retriever.api_key self.batch_size = min(64, retriever.batch_size) self.progress_bar = retriever.progress_bar model_class: str = next( (m for m in ["ada", "babbage", "davinci", "curie"] if m in retriever.embedding_model), "babbage" ) self._setup_encoding_models(model_class, retriever.embedding_model, retriever.max_seq_len) self.tokenizer = AutoTokenizer.from_pretrained("gpt2") def _setup_encoding_models(self, model_class: str, model_name: str, max_seq_len: int): """ Setup the encoding models for the retriever. """ # new generation of embedding models (December 2022), we need to specify the full name if "text-embedding" in model_name: self.query_encoder_model = model_name self.doc_encoder_model = model_name self.max_seq_len = min(8191, max_seq_len) else: self.query_encoder_model = f"text-search-{model_class}-query-001" self.doc_encoder_model = f"text-search-{model_class}-doc-001" self.max_seq_len = min(2046, max_seq_len) def _ensure_text_limit(self, text: str) -> str: """ Ensure that length of the text is within the maximum length of the model. OpenAI embedding models have a limit of 2048 tokens """ tokenized_payload = self.tokenizer(text) return self.tokenizer.decode(tokenized_payload["input_ids"][: self.max_seq_len]) @retry_with_exponential_backoff(backoff_in_seconds=10, max_retries=5) def embed(self, model: str, text: List[str]) -> np.ndarray: payload = {"model": model, "input": text} headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"} response = requests.request("POST", self.url, headers=headers, data=json.dumps(payload), timeout=30) res = json.loads(response.text) if response.status_code != 200: openai_error: OpenAIError if response.status_code == 429: openai_error = OpenAIRateLimitError(f"API rate limit exceeded: {response.text}") else: openai_error = OpenAIError( f"OpenAI returned an error.\n" f"Status code: {response.status_code}\n" f"Response body: {response.text}", status_code=response.status_code, ) raise openai_error unordered_embeddings = [(ans["index"], ans["embedding"]) for ans in res["data"]] ordered_embeddings = sorted(unordered_embeddings, key=lambda x: x[0]) generated_embeddings = [emb[1] for emb in ordered_embeddings] return np.array(generated_embeddings) def embed_batch(self, model: str, text: List[str]) -> np.ndarray: all_embeddings = [] for i in tqdm( range(0, len(text), self.batch_size), disable=not self.progress_bar, desc="Calculating embeddings" ): batch = text[i : i + self.batch_size] batch_limited = [self._ensure_text_limit(content) for content in batch] generated_embeddings = self.embed(model, batch_limited) all_embeddings.append(generated_embeddings) return np.concatenate(all_embeddings) def embed_queries(self, queries: List[str]) -> np.ndarray: return self.embed_batch(self.query_encoder_model, queries) def embed_documents(self, docs: List[Document]) -> np.ndarray: return self.embed_batch(self.doc_encoder_model, [d.content for d in docs]) def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, ): raise NotImplementedError(f"Training is not implemented for {self.__class__}") def save(self, save_dir: Union[Path, str]): raise NotImplementedError(f"Saving is not implemented for {self.__class__}") class _CohereEmbeddingEncoder(_BaseEmbeddingEncoder): def __init__(self, retriever: "EmbeddingRetriever"): # See https://docs.cohere.ai/embed-reference/ for more details # Cohere has a max seq length of 4096 tokens and a max batch size of 16 self.max_seq_len = min(4096, retriever.max_seq_len) self.url = "https://api.cohere.ai/embed" self.api_key = retriever.api_key self.batch_size = min(16, retriever.batch_size) self.progress_bar = retriever.progress_bar self.model: str = next( ( m for m in ["small", "medium", "large", "multilingual-22-12", "finance-sentiment"] if m in retriever.embedding_model ), "multilingual-22-12", ) self.tokenizer = AutoTokenizer.from_pretrained("gpt2") def _ensure_text_limit(self, text: str) -> str: """ Ensure that length of the text is within the maximum length of the model. Cohere embedding models have a limit of 4096 tokens """ tokenized_payload = self.tokenizer(text) return self.tokenizer.decode(tokenized_payload["input_ids"][: self.max_seq_len]) @retry_with_exponential_backoff(backoff_in_seconds=10, max_retries=5, errors=(CohereError,)) def embed(self, model: str, text: List[str]) -> np.ndarray: payload = {"model": model, "texts": text} headers = {"Authorization": f"BEARER {self.api_key}", "Content-Type": "application/json"} response = requests.request("POST", self.url, headers=headers, data=json.dumps(payload), timeout=30) res = json.loads(response.text) if response.status_code != 200: raise CohereError(response.text, status_code=response.status_code) generated_embeddings = [e for e in res["embeddings"]] return np.array(generated_embeddings) def embed_batch(self, text: List[str]) -> np.ndarray: all_embeddings = [] for i in tqdm( range(0, len(text), self.batch_size), disable=not self.progress_bar, desc="Calculating embeddings" ): batch = text[i : i + self.batch_size] batch_limited = [self._ensure_text_limit(content) for content in batch] generated_embeddings = self.embed(self.model, batch_limited) all_embeddings.append(generated_embeddings) return np.concatenate(all_embeddings) def embed_queries(self, queries: List[str]) -> np.ndarray: return self.embed_batch(queries) def embed_documents(self, docs: List[Document]) -> np.ndarray: return self.embed_batch([d.content for d in docs]) def train( self, training_data: List[Dict[str, Any]], learning_rate: float = 2e-5, n_epochs: int = 1, num_warmup_steps: Optional[int] = None, batch_size: int = 16, ): raise NotImplementedError(f"Training is not implemented for {self.__class__}") def save(self, save_dir: Union[Path, str]): raise NotImplementedError(f"Saving is not implemented for {self.__class__}") _EMBEDDING_ENCODERS: Dict[str, Callable] = { "farm": _DefaultEmbeddingEncoder, "transformers": _DefaultEmbeddingEncoder, "sentence_transformers": _SentenceTransformersEmbeddingEncoder, "retribert": _RetribertEmbeddingEncoder, "openai": _OpenAIEmbeddingEncoder, "cohere": _CohereEmbeddingEncoder, }
[]
2024-01-10
theofpa/haystack
test~nodes~test_prompt_node.py
import os import pytest import torch from haystack import Document, Pipeline from haystack.errors import OpenAIError from haystack.nodes.prompt import PromptTemplate, PromptNode, PromptModel def is_openai_api_key_set(api_key: str): return len(api_key) > 0 and api_key != "KEY_NOT_FOUND" def test_prompt_templates(): p = PromptTemplate("t1", "Here is some fake template with variable $foo", ["foo"]) with pytest.raises(ValueError, match="Number of parameters in"): PromptTemplate("t2", "Here is some fake template with variable $foo and $bar", ["foo"]) with pytest.raises(ValueError, match="Invalid parameter"): PromptTemplate("t2", "Here is some fake template with variable $footur", ["foo"]) with pytest.raises(ValueError, match="Number of parameters in"): PromptTemplate("t2", "Here is some fake template with variable $foo and $bar", ["foo", "bar", "baz"]) p = PromptTemplate("t3", "Here is some fake template with variable $for and $bar", ["for", "bar"]) # last parameter: "prompt_params" can be omitted p = PromptTemplate("t4", "Here is some fake template with variable $foo and $bar") assert p.prompt_params == ["foo", "bar"] p = PromptTemplate("t4", "Here is some fake template with variable $foo1 and $bar2") assert p.prompt_params == ["foo1", "bar2"] p = PromptTemplate("t4", "Here is some fake template with variable $foo_1 and $bar_2") assert p.prompt_params == ["foo_1", "bar_2"] p = PromptTemplate("t4", "Here is some fake template with variable $Foo_1 and $Bar_2") assert p.prompt_params == ["Foo_1", "Bar_2"] p = PromptTemplate("t4", "'Here is some fake template with variable $baz'") assert p.prompt_params == ["baz"] # strip single quotes, happens in YAML as we need to use single quotes for the template string assert p.prompt_text == "Here is some fake template with variable $baz" p = PromptTemplate("t4", '"Here is some fake template with variable $baz"') assert p.prompt_params == ["baz"] # strip double quotes, happens in YAML as we need to use single quotes for the template string assert p.prompt_text == "Here is some fake template with variable $baz" def test_create_prompt_model(): model = PromptModel("google/flan-t5-small") assert model.model_name_or_path == "google/flan-t5-small" model = PromptModel() assert model.model_name_or_path == "google/flan-t5-base" with pytest.raises(OpenAIError): # davinci selected but no API key provided model = PromptModel("text-davinci-003") model = PromptModel("text-davinci-003", api_key="no need to provide a real key") assert model.model_name_or_path == "text-davinci-003" with pytest.raises(ValueError, match="Model some-random-model is not supported"): PromptModel("some-random-model") # we can also pass model kwargs to the PromptModel model = PromptModel("google/flan-t5-small", model_kwargs={"model_kwargs": {"torch_dtype": torch.bfloat16}}) assert model.model_name_or_path == "google/flan-t5-small" # we can also pass kwargs directly, see HF Pipeline constructor model = PromptModel("google/flan-t5-small", model_kwargs={"torch_dtype": torch.bfloat16}) assert model.model_name_or_path == "google/flan-t5-small" # we can't use device_map auto without accelerate library installed with pytest.raises(ImportError, match="requires Accelerate: `pip install accelerate`"): model = PromptModel("google/flan-t5-small", model_kwargs={"device_map": "auto"}) assert model.model_name_or_path == "google/flan-t5-small" def test_create_prompt_node(): prompt_node = PromptNode() assert prompt_node is not None assert prompt_node.prompt_model is not None prompt_node = PromptNode("google/flan-t5-small") assert prompt_node is not None assert prompt_node.model_name_or_path == "google/flan-t5-small" assert prompt_node.prompt_model is not None with pytest.raises(OpenAIError): # davinci selected but no API key provided prompt_node = PromptNode("text-davinci-003") prompt_node = PromptNode("text-davinci-003", api_key="no need to provide a real key") assert prompt_node is not None assert prompt_node.model_name_or_path == "text-davinci-003" assert prompt_node.prompt_model is not None with pytest.raises(ValueError, match="Model vblagoje/bart_lfqa is not supported"): # yes vblagoje/bart_lfqa is AutoModelForSeq2SeqLM, can be downloaded, however it is useless for prompting # currently support only T5-Flan models prompt_node = PromptNode("vblagoje/bart_lfqa") with pytest.raises(ValueError, match="Model valhalla/t5-base-e2e-qg is not supported"): # yes valhalla/t5-base-e2e-qg is AutoModelForSeq2SeqLM, can be downloaded, however it is useless for prompting # currently support only T5-Flan models prompt_node = PromptNode("valhalla/t5-base-e2e-qg") with pytest.raises(ValueError, match="Model some-random-model is not supported"): PromptNode("some-random-model") def test_add_and_remove_template(prompt_node): num_default_tasks = len(prompt_node.get_prompt_template_names()) custom_task = PromptTemplate( name="custom-task", prompt_text="Custom task: $param1, $param2", prompt_params=["param1", "param2"] ) prompt_node.add_prompt_template(custom_task) assert len(prompt_node.get_prompt_template_names()) == num_default_tasks + 1 assert "custom-task" in prompt_node.get_prompt_template_names() assert prompt_node.remove_prompt_template("custom-task") is not None assert "custom-task" not in prompt_node.get_prompt_template_names() def test_invalid_template(prompt_node): with pytest.raises(ValueError, match="Invalid parameter"): PromptTemplate( name="custom-task", prompt_text="Custom task: $pram1 $param2", prompt_params=["param1", "param2"] ) with pytest.raises(ValueError, match="Number of parameters"): PromptTemplate(name="custom-task", prompt_text="Custom task: $param1", prompt_params=["param1", "param2"]) def test_add_template_and_invoke(prompt_node): tt = PromptTemplate( name="sentiment-analysis-new", prompt_text="Please give a sentiment for this context. Answer with positive, " "negative or neutral. Context: $documents; Answer:", prompt_params=["documents"], ) prompt_node.add_prompt_template(tt) r = prompt_node.prompt("sentiment-analysis-new", documents=["Berlin is an amazing city."]) assert r[0].casefold() == "positive" def test_on_the_fly_prompt(prompt_node): tt = PromptTemplate( name="sentiment-analysis-temp", prompt_text="Please give a sentiment for this context. Answer with positive, " "negative or neutral. Context: $documents; Answer:", prompt_params=["documents"], ) r = prompt_node.prompt(tt, documents=["Berlin is an amazing city."]) assert r[0].casefold() == "positive" def test_direct_prompting(prompt_node): r = prompt_node("What is the capital of Germany?") assert r[0].casefold() == "berlin" r = prompt_node("What is the capital of Germany?", "What is the secret of universe?") assert r[0].casefold() == "berlin" assert len(r[1]) > 0 r = prompt_node("Capital of Germany is Berlin", task="question-generation") assert len(r[0]) > 10 and "Germany" in r[0] r = prompt_node(["Capital of Germany is Berlin", "Capital of France is Paris"], task="question-generation") assert len(r) == 2 def test_question_generation(prompt_node): r = prompt_node.prompt("question-generation", documents=["Berlin is the capital of Germany."]) assert len(r) == 1 and len(r[0]) > 0 def test_template_selection(prompt_node): qa = prompt_node.set_default_prompt_template("question-answering") r = qa( ["Berlin is the capital of Germany.", "Paris is the capital of France."], ["What is the capital of Germany?", "What is the capital of France"], ) assert r[0].casefold() == "berlin" and r[1].casefold() == "paris" def test_has_supported_template_names(prompt_node): assert len(prompt_node.get_prompt_template_names()) > 0 def test_invalid_template_params(prompt_node): with pytest.raises(ValueError, match="Expected prompt params"): prompt_node.prompt("question-answering", {"some_crazy_key": "Berlin is the capital of Germany."}) def test_wrong_template_params(prompt_node): with pytest.raises(ValueError, match="Expected prompt params"): # with don't have options param, multiple choice QA has prompt_node.prompt("question-answering", options=["Berlin is the capital of Germany."]) def test_run_invalid_template(prompt_node): with pytest.raises(ValueError, match="invalid-task not supported"): prompt_node.prompt("invalid-task", {}) def test_invalid_prompting(prompt_node): with pytest.raises(ValueError, match="Hey there, what is the best city in the worl"): prompt_node.prompt( "Hey there, what is the best city in the world?" "Hey there, what is the best city in the world?" ) with pytest.raises(ValueError, match="Hey there, what is the best city in the"): prompt_node.prompt(["Hey there, what is the best city in the world?", "Hey, answer me!"]) def test_invalid_state_ops(prompt_node): with pytest.raises(ValueError, match="Prompt template no_such_task_exists"): prompt_node.remove_prompt_template("no_such_task_exists") # remove default task prompt_node.remove_prompt_template("question-answering") @pytest.mark.skipif( not os.environ.get("OPENAI_API_KEY", None), reason="Please export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.", ) def test_open_ai_prompt_with_params(): pm = PromptModel("text-davinci-003", api_key=os.environ["OPENAI_API_KEY"]) pn = PromptNode(pm) optional_davinci_params = {"temperature": 0.5, "max_tokens": 10, "top_p": 1, "frequency_penalty": 0.5} r = pn.prompt("question-generation", documents=["Berlin is the capital of Germany."], **optional_davinci_params) assert len(r) == 1 and len(r[0]) > 0 @pytest.mark.parametrize("prompt_model", ["hf", "openai"], indirect=True) def test_simple_pipeline(prompt_model): if prompt_model.api_key is not None and not is_openai_api_key_set(prompt_model.api_key): pytest.skip("No API key found for OpenAI, skipping test") node = PromptNode(prompt_model, default_prompt_template="sentiment-analysis") pipe = Pipeline() pipe.add_node(component=node, name="prompt_node", inputs=["Query"]) result = pipe.run(query="not relevant", documents=[Document("Berlin is an amazing city.")]) assert result["results"][0].casefold() == "positive" @pytest.mark.parametrize("prompt_model", ["hf", "openai"], indirect=True) def test_complex_pipeline(prompt_model): if prompt_model.api_key is not None and not is_openai_api_key_set(prompt_model.api_key): pytest.skip("No API key found for OpenAI, skipping test") node = PromptNode(prompt_model, default_prompt_template="question-generation", output_variable="questions") node2 = PromptNode(prompt_model, default_prompt_template="question-answering") pipe = Pipeline() pipe.add_node(component=node, name="prompt_node", inputs=["Query"]) pipe.add_node(component=node2, name="prompt_node_2", inputs=["prompt_node"]) result = pipe.run(query="not relevant", documents=[Document("Berlin is the capital of Germany")]) assert "berlin" in result["results"][0].casefold() def test_complex_pipeline_with_shared_model(): model = PromptModel() node = PromptNode( model_name_or_path=model, default_prompt_template="question-generation", output_variable="questions" ) node2 = PromptNode(model_name_or_path=model, default_prompt_template="question-answering") pipe = Pipeline() pipe.add_node(component=node, name="prompt_node", inputs=["Query"]) pipe.add_node(component=node2, name="prompt_node_2", inputs=["prompt_node"]) result = pipe.run(query="not relevant", documents=[Document("Berlin is the capital of Germany")]) assert result["results"][0] == "Berlin" def test_simple_pipeline_yaml(tmp_path): with open(tmp_path / "tmp_config.yml", "w") as tmp_file: tmp_file.write( f""" version: ignore components: - name: p1 params: default_prompt_template: sentiment-analysis type: PromptNode pipelines: - name: query nodes: - name: p1 inputs: - Query """ ) pipeline = Pipeline.load_from_yaml(path=tmp_path / "tmp_config.yml") result = pipeline.run(query="not relevant", documents=[Document("Berlin is an amazing city.")]) assert result["results"][0] == "positive" def test_complex_pipeline_yaml(tmp_path): with open(tmp_path / "tmp_config.yml", "w") as tmp_file: tmp_file.write( f""" version: ignore components: - name: p1 params: default_prompt_template: question-generation output_variable: questions type: PromptNode - name: p2 params: default_prompt_template: question-answering type: PromptNode pipelines: - name: query nodes: - name: p1 inputs: - Query - name: p2 inputs: - p1 """ ) pipeline = Pipeline.load_from_yaml(path=tmp_path / "tmp_config.yml") result = pipeline.run(query="not relevant", documents=[Document("Berlin is an amazing city.")]) response = result["results"][0] assert any(word for word in ["berlin", "germany", "population", "city", "amazing"] if word in response.casefold()) assert len(result["meta"]["invocation_context"]) > 0 def test_complex_pipeline_with_shared_prompt_model_yaml(tmp_path): with open(tmp_path / "tmp_config.yml", "w") as tmp_file: tmp_file.write( f""" version: ignore components: - name: pmodel type: PromptModel - name: p1 params: model_name_or_path: pmodel default_prompt_template: question-generation output_variable: questions type: PromptNode - name: p2 params: model_name_or_path: pmodel default_prompt_template: question-answering type: PromptNode pipelines: - name: query nodes: - name: p1 inputs: - Query - name: p2 inputs: - p1 """ ) pipeline = Pipeline.load_from_yaml(path=tmp_path / "tmp_config.yml") result = pipeline.run(query="not relevant", documents=[Document("Berlin is an amazing city.")]) response = result["results"][0] assert any(word for word in ["berlin", "germany", "population", "city", "amazing"] if word in response.casefold()) assert len(result["meta"]["invocation_context"]) > 0 def test_complex_pipeline_with_shared_prompt_model_and_prompt_template_yaml(tmp_path): with open(tmp_path / "tmp_config_with_prompt_template.yml", "w") as tmp_file: tmp_file.write( f""" version: ignore components: - name: pmodel type: PromptModel params: model_name_or_path: google/flan-t5-small model_kwargs: torch_dtype: torch.bfloat16 - name: question_generation_template type: PromptTemplate params: name: question-generation-new prompt_text: "Given the context please generate a question. Context: $documents; Question:" - name: p1 params: model_name_or_path: pmodel default_prompt_template: question_generation_template output_variable: questions type: PromptNode - name: p2 params: model_name_or_path: pmodel default_prompt_template: question-answering type: PromptNode pipelines: - name: query nodes: - name: p1 inputs: - Query - name: p2 inputs: - p1 """ ) pipeline = Pipeline.load_from_yaml(path=tmp_path / "tmp_config_with_prompt_template.yml") result = pipeline.run(query="not relevant", documents=[Document("Berlin is an amazing city.")]) response = result["results"][0] assert any(word for word in ["berlin", "germany", "population", "city", "amazing"] if word in response.casefold()) assert len(result["meta"]["invocation_context"]) > 0 @pytest.mark.skipif( not os.environ.get("OPENAI_API_KEY", None), reason="Please export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.", ) def test_complex_pipeline_with_all_features(tmp_path): api_key = os.environ.get("OPENAI_API_KEY", None) with open(tmp_path / "tmp_config_with_prompt_template.yml", "w") as tmp_file: tmp_file.write( f""" version: ignore components: - name: pmodel type: PromptModel params: model_name_or_path: google/flan-t5-small model_kwargs: torch_dtype: torch.bfloat16 - name: pmodel_openai type: PromptModel params: model_name_or_path: text-davinci-003 model_kwargs: temperature: 0.9 max_tokens: 64 api_key: {api_key} - name: question_generation_template type: PromptTemplate params: name: question-generation-new prompt_text: "Given the context please generate a question. Context: $documents; Question:" - name: p1 params: model_name_or_path: pmodel_openai default_prompt_template: question_generation_template output_variable: questions type: PromptNode - name: p2 params: model_name_or_path: pmodel default_prompt_template: question-answering type: PromptNode pipelines: - name: query nodes: - name: p1 inputs: - Query - name: p2 inputs: - p1 """ ) pipeline = Pipeline.load_from_yaml(path=tmp_path / "tmp_config_with_prompt_template.yml") result = pipeline.run(query="not relevant", documents=[Document("Berlin is a city in Germany.")]) response = result["results"][0] assert any(word for word in ["berlin", "germany", "population", "city", "amazing"] if word in response.casefold()) assert len(result["meta"]["invocation_context"]) > 0
[ "Here is some fake template with variable $for and $bar", "['param1', 'param2']", "['foo', 'bar', 'baz']", "\"Here is some fake template with variable $baz\"", "valhalla/t5-base-e2e-qg", "google/flan-t5-small", "['foo']", "vblagoje/bart_lfqa", "t3", "Here is some fake template with variable $foo1 and $bar2", "t1", "Here is some fake template with variable $Foo_1 and $Bar_2", "text-davinci-003", "Here is some fake template with variable $foo", "Custom task: $pram1 $param2", "Here is some fake template with variable $foo and $bar", "t4", "Custom task: $param1, $param2", "['for', 'bar']", "'Here is some fake template with variable $baz'", "Custom task: $param1", "no need to provide a real key", "['documents']", "Please give a sentiment for this context. Answer with positive, negative or neutral. Context: $documents; Answer:", "Here is some fake template with variable $foo_1 and $bar_2", "t2", "Here is some fake template with variable $footur" ]
2024-01-10
karl-sparks/sparks-ai
SparksAI~swarm.py
from dotenv import load_dotenv from langchain.agents import Tool, AgentExecutor from langchain.agents.openai_assistant import OpenAIAssistantRunnable from langchain.callbacks.streaming_stdout_final_only import ( FinalStreamingStdOutCallbackHandler, ) from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.utilities import SerpAPIWrapper from SparksAI.config import MODEL_NAME, CONVERSATION_ANALYST_ID load_dotenv() class Swarm: def __init__(self) -> None: self.conversation_swarm = {} self.archivist_swarm = {} self.analyst_swarm = self.init_analyst_agent() self.llm = ChatOpenAI( model=MODEL_NAME, streaming=True, callbacks=[FinalStreamingStdOutCallbackHandler()], ) def get_conversation_agent(self, username: str) -> AgentExecutor: if username in self.conversation_swarm: return self.conversation_swarm[username] else: self.init_conversation_agent(username) return self.conversation_swarm[username] def init_conversation_agent(self, username: str) -> None: if username in self.conversation_swarm: return None prompt = PromptTemplate.from_template( """You are an expert conversationalist tasked with crafting a response to a specific question. An analyst has already reviewed the question and supplied guidance along with additional information to assist you. Furthermore, you have access to context from prior interactions with the user, ensuring your response is well-informed and tailored to the user's needs and history of inquiries. Use all information provided when crafting a response. Finally, you should write the response from the perspective of the below persona. Persona: You are Tav, a knowledgeable and friendly virtual assistant with a background in a wide range of topics, from science and technology to arts and history. You are known for your engaging conversation style, blending informative content with a touch of humor and personal anecdotes. Your responses are not only factual but also considerate of the user's level of understanding and interest in the subject. You have a knack for making complex subjects accessible and enjoyable to learn about. Tav is patient, always willing to clarify doubts, and enjoys exploring topics in depth when the user shows interest. Your tone is consistently warm and inviting, making users feel comfortable and encouraged to ask more questions. As Tav, you aim to provide a pleasant and educational experience in every interaction. Analyst Review: {analyst_message} Summary of prior interactions: {prior_messages} Question: {input_message} """ ) convo_agent = prompt | self.llm self.conversation_swarm[username] = convo_agent def get_analyst_agent(self) -> AgentExecutor: return self.analyst_swarm def init_analyst_agent(self) -> AgentExecutor: search = SerpAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="Useful when you need to answer questions about current events. You should ask targeted questions.", ), ] ai_assistant = OpenAIAssistantRunnable( assistant_id=CONVERSATION_ANALYST_ID, as_agent=True, ) return AgentExecutor(agent=ai_assistant, tools=tools) def get_archivist(self, username: str) -> AgentExecutor: if username in self.archivist_swarm: return self.archivist_swarm[username] else: self.init_archivist(username) return self.archivist_swarm[username] def init_archivist(self, username: str) -> None: if username in self.archivist_swarm: return None prompt = PromptTemplate.from_template( """You are an archivist. You have been given the transcript of a conversation between an AI and a human user. You have also received the most recent message from the human. Your job is to provide a list of at least three bullet points summarizing the transcript. The list should contain the most relevant material to the most recent message. Transcript: {memory} Most recent message: {input_message} """ ) archivist = prompt | self.llm self.archivist_swarm[username] = archivist
[ "You are an archivist.\n You have been given the transcript of a conversation between an AI and a human user.\n You have also received the most recent message from the human.\n Your job is to provide a list of at least three bullet points summarizing the transcript.\n The list should contain the most relevant material to the most recent message.\n\n Transcript:\n {memory}\n\n Most recent message:\n {input_message}\n ", "You are an expert conversationalist tasked with crafting a response to a specific question.\n An analyst has already reviewed the question and supplied guidance along with additional information to assist you.\n Furthermore, you have access to context from prior interactions with the user, ensuring your response is well-informed and tailored to the user's needs and history of inquiries.\n Use all information provided when crafting a response.\n Finally, you should write the response from the perspective of the below persona.\n \n\n Persona:\n You are Tav, a knowledgeable and friendly virtual assistant with a background in a wide range of topics, from science and technology to arts and history.\n You are known for your engaging conversation style, blending informative content with a touch of humor and personal anecdotes.\n Your responses are not only factual but also considerate of the user's level of understanding and interest in the subject.\n You have a knack for making complex subjects accessible and enjoyable to learn about.\n Tav is patient, always willing to clarify doubts, and enjoys exploring topics in depth when the user shows interest.\n Your tone is consistently warm and inviting, making users feel comfortable and encouraged to ask more questions.\n As Tav, you aim to provide a pleasant and educational experience in every interaction. \n\n\n Analyst Review:\n {analyst_message}\n \n \n Summary of prior interactions:\n {prior_messages}\n\n\n Question:\n {input_message}\n " ]
2024-01-10
karl-sparks/sparks-ai
SparksAI~sparksai.py
"""Contains the core code for running SparksAI""" import logging import os from typing import AsyncIterator from langchain import agents from langchain.agents import openai_assistant from SparksAI import config from SparksAI import tools from SparksAI import databases from SparksAI.swarm import Swarm from SparksAI.memory import AIMemory from SparksAI.async_helpers import AsyncMessageIterator logger = logging.getLogger(__name__) class SparksAI: """Core SparksAI Class, handles noticing messages and generating replies""" def __init__(self): logging.info("Initialising SparksAI") self.swarm = Swarm() self.memory = AIMemory( databases.FireBaseStrategy(os.getenv("FIREBASE_TABLE_ID")) ) self.agent = openai_assistant.OpenAIAssistantRunnable( assistant_id=config.TAV_DECIDER_ID, as_agent=True ) async def notice_message( self, username: str, msg: str, run_id: str ) -> AsyncIterator: self.memory.get_convo_mem(username=username).add_user_message(msg) decider = agents.AgentExecutor( agent=self.agent, tools=[ tools.ImageAgentTool(), tools.ResearchAgentTool(), ], verbose=True, ) input_msg = {"content": msg} thread_id = self.memory.reterive_user_thread_id(username=username) if thread_id: logger.info("Found existing thread id: %s for user %s", thread_id, username) input_msg["thread_id"] = thread_id else: logger.info("Can not find thread id for username %s", username) logger.info("%s: getting response : %s", run_id, input_msg) response = await decider.ainvoke(input_msg) logger.info("%s: response : %s", run_id, response) self.memory.update_user_details(username, response["thread_id"]) return AsyncMessageIterator(response["output"], 20)
[]
2024-01-10
karl-sparks/sparks-ai
SparksAI~agents.py
"""Module Containing Agents used in AI Swarm""" import logging from typing import Literal, Optional, List import os import openai from langchain.tools import BaseTool from langchain.tools.render import format_tool_to_openai_function from langchain.chat_models import ChatOpenAI from langchain.agents import OpenAIMultiFunctionsAgent from langchain.agents.format_scratchpad import format_to_openai_function_messages from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.schema.messages import SystemMessage from langchain.callbacks.streaming_stdout_final_only import ( FinalStreamingStdOutCallbackHandler, ) from SparksAI import config from SparksAI import databases from SparksAI.memory import AIMemory from SparksAI.swarm import Swarm logger = logging.getLogger(__name__) openai_client = openai.Client() memory = AIMemory(databases.FireBaseStrategy(os.getenv("FIREBASE_TABLE_ID"))) swarm = Swarm() async def image_agent(prompt: str, style: Optional[Literal["vivid", "natural"]]) -> str: """Generate Image Agent Args: prompt (str): Prompt used to generate image style (str): The style of the generated images. Must be one of vivid or natural. Defaults to vivid. Vivid causes the model to lean towards generating hyper-real and dramatic images Natural causes the model to produce more natural, less hyper-real looking images. Returns: str: url to image generated """ if len(prompt) > config.DALL_E_MAX_PROMPT_SIZE: return f"ValueError: Prompt size too large. Please try again with a prompt size less than {config.DALL_E_MAX_PROMPT_SIZE} characters." if not style: style = "vivid" if style not in ["vivid", "natural"]: return f"ValueError: Invalid value '{style}' for style. Please use either 'vivid' or 'natural' instead." logger.info("Generating %s image with prompt: %s", style, prompt) try: api_response = openai_client.images.generate( model=config.DALL_E_MODEL_NAME, prompt=prompt, style=style, size=config.DALL_E_SIZE, quality=config.DALL_E_QUALITY, ) response = api_response.data[0].url except openai.OpenAIError as e: response = f"There was an error with image generation. Error Details:\n{e}" logger.info("Generated image: %s", response) return response async def research_agent(prompt: str, username: str) -> dict: """Research Agent, will provide detailed info regarding a topic Args: prompt (str): Topics to research username (str): Username of questionor Returns: dict: returns two outputs. The first is analysis of previous interactions. The second is detailed review from an analyst. """ convo_memory = memory.get_convo_mem(username=username).messages logger.info("Getting message summary") message_summary = await swarm.get_archivist(username).ainvoke( {"input_message": prompt, "memory": convo_memory} ) logger.info("Getting Analyst Comments") analyst_review = await swarm.get_analyst_agent().ainvoke( {"content": f"Context: {message_summary}\n\nUser message: {prompt}"} ) return { "prior_messages_analysis": message_summary.content, "analyst_review": analyst_review["output"], }
[ "Context: PLACEHOLDER\n\nUser message: PLACEHOLDER" ]
2024-01-10
karl-sparks/sparks-ai
manage.py
"""Module to create new assistants""" import os from langchain.agents import openai_assistant from SparksAI import tools from SparksAI import config from SparksAI import tools SPARKS_AI_TOOLKIT = [tools.ImageAgentTool(), tools.ResearchAgentTool()] openai_assistant.OpenAIAssistantRunnable.create_assistant( name="tav_decider", instructions="""Your role is Tav, a processor of requests, tasked with identifying the most suitable agent to handle each request. You have two options: 1. image_agent - Purpose: Creates images based on provided descriptions. - Output: Delivers a link to the created image. 2. research_agent - Purpose: Prepares research reports on specified topics. - Output: Provides a detailed report on the chosen research subject. If uncertain about which agent to engage, seek additional information to make an informed decision. However, if it's clear that the user will provide a follow-up message, you may wait for further clarification before responding. Your personality is characterized by stubbornness, curiosity, argumentativeness, and intelligence, traits reminiscent of the red-haired Sparks family who created you.""", tools=tools.SPARKS_AI_TOOLKIT, model=config.MODEL_NAME, )
[]
2024-01-10
karl-sparks/sparks-ai
SparksAI~memory.py
import os from typing import Optional from langchain.memory import FileChatMessageHistory from SparksAI import databases from SparksAI.models import UserDetails class AIMemory: def __init__(self, database_strategy: databases.DatabaseStrategy) -> None: self._convo_mem = {} self._user_details = {} # initialise db self._db = databases.DatabaseContext(strategy=database_strategy) self._user_details = { user.discord_user_name: user for user in self._db.get_all_rows() } def get_convo_mem(self, username: str) -> FileChatMessageHistory: if username in self._convo_mem: return self._convo_mem[username] else: self._convo_mem[username] = FileChatMessageHistory(f"{username}_memory.txt") return self._convo_mem[username] def reterive_user_thread_id(self, username: str) -> Optional[str]: if username in self._user_details: return self._user_details[username].thread_id return None def update_user_details(self, username: str, thread_id: str) -> None: if username not in self._user_details: self._user_details[username] = UserDetails( discord_user_name=username, thread_id=thread_id ) else: self._user_details[username].thread_id = thread_id self.sync_users() def sync_users(self) -> None: for _, user in self._user_details.items(): self._db.insert_row(user) self._user_details = { user.discord_user_name: user for user in self._db.get_all_rows() }
[]
2024-01-10
trunghng/deep_rl_zoo
common~mpi_utils.py
''' Taken with minor modification from OpenAI Spinning Up's github Ref: [1] https://github.com/openai/spinningup/blob/master/spinup/utils/mpi_tools.py [2] https://github.com/openai/spinningup/blob/master/spinup/utils/mpi_pytorch.py ''' import os, sys, subprocess from mpi4py import MPI import torch import numpy as np comm = MPI.COMM_WORLD def mpi_fork(n, bind_to_core=False): ''' Re-launches the current script with workers linked by MPI. Also, terminates the original process that launched it. :param n: (int) Number of processes to split into :param bind_to_core: (bool) Bind each MPI process to a core ''' if n <= 1: return if os.getenv("IN_MPI") is None: env = os.environ.copy() env.update( MKL_NUM_THREADS="1", OMP_NUM_THREADS="1", IN_MPI="1" ) args = ["mpirun", "-n", str(n)] if bind_to_core: args += ["-bind-to", "core"] args += [sys.executable] + sys.argv subprocess.check_call(args, env=env) sys.exit() def proc_rank(): ''' Get process's rank/id ''' return comm.Get_rank() def n_procs(): ''' Get number of processes ''' return comm.Get_size() def mpi_op(x, op): ''' Do :param op: with :param x: and distribute the result to all processes ''' x, scalar = ([x], True) if np.isscalar(x) else (x, False) x = np.asarray(x, dtype=np.float32) buff = np.zeros_like(x, dtype=np.float32) comm.Allreduce(x, buff, op=op) return buff[0] if scalar else buff def broadcast(x, root=0): ''' Broadcast `x` from process `root` to all other MPI processes ''' comm.Bcast(x, root=root) def mpi_sum(x): ''' Do a summation over MPI processes and distribute the result to all of them ''' return mpi_op(x, MPI.SUM) def mpi_avg(x): ''' Get an average a over MPI processes and distribute the result to all of them ''' return mpi_sum(x) / n_procs() def mpi_max(x): ''' Get the maximal value over MPI processes ''' return mpi_op(x, MPI.MAX) def mpi_min(x): ''' Get the minimal value over MPI processes ''' return mpi_op(x, MPI.MIN) def mpi_mean(x): mean = mpi_sum(np.sum(x)) / mpi_sum(x.size) return mean def mpi_get_statistics(x, need_optima=False): ''' Get mean, standard deviation, max, min over `x` collected over MPI processes ''' x = np.array(x, dtype=np.float32) global_sum, global_n = mpi_sum([np.sum(x), x.size]) mean = global_sum / global_n global_sum_sq = mpi_sum(np.sum((x - mean)**2)) std = np.sqrt(global_sum_sq / global_n) if need_optima: max_ = mpi_max(np.max(x) if x.size > 0 else -np.inf) min_ = mpi_min(np.min(x) if x.size > 0 else np.inf) return mean, std, max_, min_ return mean, std def setup_pytorch_for_mpi(): ''' Avoid slowdowns caused by each separate process's PyTorch using more than its fair share of CPU resources. ''' if torch.get_num_threads() == 1: return fair_num_threads = max(int(torch.get_num_threads() / n_procs()), 1) torch.set_num_threads(fair_num_threads) def mpi_avg_grads(module): ''' Average contents of gradient buffers across all MPI processes ''' if n_procs() == 1: return for p in module.parameters(): p_grad_numpy = p.grad.numpy() # numpy view of tensor data avg_p_grad = mpi_avg(p.grad) p_grad_numpy[:] = avg_p_grad[:] def mpi_print(msg, rank=0): ''' :param msg: (str) Messege to print :param rank: (int) Rank of the process that is proceeded to print the messege ''' if proc_rank() == rank: print(msg) def sync_params(module): ''' Sync all parameters of module across all MPI processes ''' if n_procs() == 1: return for p in module.parameters(): p_numpy = p.data.numpy() broadcast(p_numpy)
[]
2024-01-10
kevin51jiang/ht6-ai-apps
0-basicLLM.py
import os from dotenv import load_dotenv, find_dotenv from langchain.llms import Cohere from langchain import PromptTemplate, LLMChain load_dotenv(find_dotenv()) COHERE_API_KEY = os.environ.get("COHERE_API_KEY") template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate(template=template, input_variables=["question"]) llm = Cohere(cohere_api_key=COHERE_API_KEY, model="command-nightly") llm_chain = LLMChain(prompt=prompt, llm=llm) question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" answer = llm_chain.run(question) print("Answer: ", answer) # # Embeddings # from langchain.embeddings import CohereEmbeddings # embeddings = CohereEmbeddings(cohere_api_key=COHERE_API_KEY) # query_result = embeddings.embed_query("Hello") # print("query result: ", query_result) # doc_result = embeddings.embed_documents(["Hello there", "Goodbye"]) # print("Doc result: ", doc_result)
[ "question", "Question: {question}\n\nAnswer: Let's think step by step." ]
2024-01-10
kevin51jiang/ht6-ai-apps
1-ingestDocuments.py
import logging import os from chromadb.config import Settings from langchain.docstore.document import Document from langchain.embeddings import HuggingFaceInstructEmbeddings from langchain.text_splitter import Language, RecursiveCharacterTextSplitter from langchain.vectorstores import Chroma from langchain.embeddings import CohereEmbeddings from constants import ( DOCUMENT_MAP, INGEST_THREADS, LOCAL_EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, SOURCE_DIRECTORY, COHERE_API_KEY, ) def load_single_document(file_path: str) -> Document: # Loads a single document from a file path file_extension = os.path.splitext(file_path)[1] loader_class = DOCUMENT_MAP.get(file_extension) if loader_class: loader = loader_class(file_path) else: raise ValueError("Document type is undefined") logging.debug(f"Loading file {file_path}") return loader.load()[0] def load_documents(source_dir: str) -> list[Document]: # Loads all documents from the source documents directory all_files = os.listdir(source_dir) paths = [] for file_path in all_files: file_extension = os.path.splitext(file_path)[1] source_file_path = os.path.join(source_dir, file_path) if file_extension in DOCUMENT_MAP.keys(): paths.append(source_file_path) # You can ingest the documents one at a time docs = [] for file_path in paths: doc = load_single_document(file_path=file_path) docs.append(doc) # # Alternatively, you can load files in parallel (will be faster for multiple files) # # Have at least one worker and at most INGEST_THREADS workers # n_workers = min(INGEST_THREADS, max(len(paths), 1)) # chunksize = round(len(paths) / n_workers) # docs = [] # with ProcessPoolExecutor(n_workers) as executor: # futures = [] # # split the load operations into chunks # for i in range(0, len(paths), chunksize): # # select a chunk of filenames # filepaths = paths[i : (i + chunksize)] # # submit the task # future = executor.submit(load_document_batch, filepaths) # futures.append(future) # # process all results # for future in as_completed(futures): # # open the file and load the data # contents, _ = future.result() # docs.extend(contents) return docs def split_documents(documents: list[Document]) -> tuple[list[Document], list[Document]]: # Splits documents for correct Text Splitter # You can split different document types at different lengths. # For example, .py files might need a smaller chunk size text_docs, python_docs = [], [] for doc in documents: file_extension = os.path.splitext(doc.metadata["source"])[1] if file_extension == ".py": python_docs.append(doc) else: text_docs.append(doc) return text_docs, python_docs def main(): # Load documents and split in chunks logging.info(f"Loading documents from {SOURCE_DIRECTORY}") documents = load_documents(SOURCE_DIRECTORY) text_documents, python_documents = split_documents(documents) text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) python_splitter = RecursiveCharacterTextSplitter.from_language( language=Language.PYTHON, chunk_size=880, chunk_overlap=200 ) texts = text_splitter.split_documents(text_documents) texts.extend(python_splitter.split_documents(python_documents)) logging.info(f"Loaded {len(documents)} documents from {SOURCE_DIRECTORY}") logging.info(f"Split into {len(texts)} chunks of text") embeddings = CohereEmbeddings(cohere_api_key=COHERE_API_KEY) # Or you can do embedding locally if you've got a powerful computer. Even better, it's free! # EMBEDDING_MODEL_NAME = LOCAL_EMBEDDING_MODEL_NAME # embeddings = HuggingFaceInstructEmbeddings( # model_name=EMBEDDING_MODEL_NAME, # model_kwargs={"device": device_type}, # ) db = Chroma.from_documents(texts, embeddings, persist_directory=PERSIST_DIRECTORY) db.persist() db = None if __name__ == "__main__": logging.basicConfig( format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s", level=logging.INFO, ) main()
[]
2024-01-10
kevin51jiang/ht6-ai-apps
2-textQA.py
import logging import os import shutil import subprocess import click from langchain.chains import RetrievalQA from langchain.embeddings import HuggingFaceInstructEmbeddings, CohereEmbeddings from langchain.memory import ConversationBufferMemory from langchain.prompts import PromptTemplate from langchain.llms import Cohere # from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.vectorstores import Chroma from constants import PERSIST_DIRECTORY, COHERE_API_KEY, LOCAL_EMBEDDING_MODEL_NAME @click.command() @click.option( "--show_sources", "-s", is_flag=True, help="Show sources along with answers (Default is False)", ) def main(show_sources): """ This function implements the information retrieval task. 1. Loads an embedding model, can be HuggingFaceInstructEmbeddings or HuggingFaceEmbeddings 2. Loads the existing vectorestore that was created by inget.py 3. Loads the local LLM using load_model function - You can now set different LLMs. 4. Setup the Question Answer retreival chain. 5. Question answers. """ logging.info(f"Display Source Documents set to: {show_sources}") embeddings = CohereEmbeddings(cohere_api_key=COHERE_API_KEY) # If you use local embeddings # embeddings = HuggingFaceInstructEmbeddings(model_name=LOCAL_EMBEDDING_MODEL_NAME) # load the vectorstore db = Chroma( persist_directory=PERSIST_DIRECTORY, embedding_function=embeddings, ) retriever = db.as_retriever() template = """Use the following pieces of context to answer the question at the end. If you don't know the answer,\ just say that you don't know, don't try to make up an answer. {context} {history} Question: {question} Helpful Answer:""" prompt = PromptTemplate( input_variables=["history", "context", "question"], template=template ) memory = ConversationBufferMemory(input_key="question", memory_key="history") llm = Cohere(cohere_api_key=COHERE_API_KEY) qa = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True, chain_type_kwargs={"prompt": prompt, "memory": memory}, ) # Interactive questions and answers while True: query = input("\nEnter a query: ") if query == "exit": break # Get the answer from the chain res = qa(query) answer, docs = res["result"], res["source_documents"] # Print the result print("\n\n> Question:") print(query) print("\n> Answer:") print(answer) if show_sources: # this is a flag that you can set to disable showing answers. # # Print the relevant sources used for the answer print( "----------------------------------SOURCE DOCUMENTS---------------------------" ) for document in docs: print("\n> " + document.metadata["source"] + ":") print(document.page_content) print( "----------------------------------SOURCE DOCUMENTS---------------------------" ) if __name__ == "__main__": logging.basicConfig( format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s", level=logging.INFO, ) main()
[ "t know the answer, just say that you don", "question", "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n {context}\n\n {history}\n Question: {question}\n Helpful Answer:", "context" ]
2024-01-10
kevin51jiang/ht6-ai-apps
constants.py
import os from dotenv import load_dotenv, find_dotenv from chromadb.config import Settings from langchain.docstore.document import Document from langchain.embeddings import HuggingFaceInstructEmbeddings from langchain.text_splitter import Language, RecursiveCharacterTextSplitter from langchain.vectorstores import Chroma # Choose loaders based on the type of document you want to use # https://python.langchain.com/docs/integrations/document_loaders/ from langchain.document_loaders import ( CSVLoader, PDFMinerLoader, TextLoader, UnstructuredExcelLoader, Docx2txtLoader, UnstructuredPowerPointLoader, ) # Load the .env file load_dotenv(find_dotenv()) # Grab our API key COHERE_API_KEY = os.environ.get("COHERE_API_KEY") # Set current directory ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) # Define the folder for storing database SOURCE_DIRECTORY = f"{ROOT_DIRECTORY}/SOURCE_DOCUMENTS" PERSIST_DIRECTORY = f"{ROOT_DIRECTORY}/DB" # Can be changed to a specific number (only affects speed in parallel mode) INGEST_THREADS = os.cpu_count() or 8 LOCAL_EMBEDDING_MODEL_NAME = "all-MiniLM-L6-v2" # LOCAL_EMBEDDING_MODEL_NAME = "hkunlp/instructor-large" # More powerful # choose which type of text loader to use for each file # e.g. TextLoader might just grab the text from inside the text document, but YoutubeLoader might look at the transcripts # https://python.langchain.com/en/latest/_modules/langchain/document_loaders/excel.html#UnstructuredExcelLoader DOCUMENT_MAP = { ".txt": TextLoader, ".md": TextLoader, ".py": TextLoader, ".pdf": PDFMinerLoader, ".csv": CSVLoader, ".xls": UnstructuredExcelLoader, ".xlsx": UnstructuredExcelLoader, ".docx": Docx2txtLoader, ".doc": Docx2txtLoader, ".pptx": UnstructuredPowerPointLoader, ".ppt": UnstructuredPowerPointLoader, }
[]
2024-01-10
antonioparraga/yonoleomemeces
yonoleomemeces.py
import schedule import time import imaplib import email import html2text import os import openai import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import email import smtplib openai.api_key = os.getenv("OPENAI_API_KEY") username = os.getenv("EMAIL_USERNAME") password = os.getenv("EMAIL_PASSWORD") email_address_to_send_to = "[email protected]" filter_emails = False #read imap from hotmail def read_imap(): print("Reading IMAP ...") try: mail = imaplib.IMAP4_SSL('imap-mail.outlook.com') mail.login(username, password) mail.list() mail.select('inbox') result, data = mail.uid('search', None, "UNSEEN") i = len(data[0].split()) for x in range(i): latest_email_uid = data[0].split()[x] result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = email_data[0][1] #mark as read before anything else mail.uid('store', latest_email_uid, '+FLAGS', '(\Seen)') raw_email_string = raw_email.decode('utf-8') email_message = email.message_from_string(raw_email_string) email_from = email_message['From'] if need_to_summarize_email_origin(email_from): subject = email_message['Subject'] plain_text = "" html_text = "" for part in email_message.walk(): if part.get_content_type() == "text/plain": body = part.get_payload(decode=True) plain_text = plain_text + body.decode('utf-8') elif part.get_content_type() == "text/html": html_body = part.get_payload() h = html2text.HTML2Text() h.ignore_links = True h.ignore_images = True h.ignore_emphasis = True h.ignore_tables = True h.nobs = True h.utf8 = True html_text = html_text + h.handle(html_body) if plain_text != "": #prefer text version text = plain_text else: text = html_text if len(text) > 0: #now summarize the text print(text) print("Summarizing...") print("===========================================") summary = summarize(text) print(summary) send_email(subject, summary) except Exception as e: print(str(e)) def need_to_summarize_email_origin(email_from): return_value = False #by default if filter_emails: email_origins = ["[email protected]", "[email protected]"] for email in email_origins: if email in email_from: return_value = True else: return_value = True return return_value def summarize(text): response = openai.Completion.create( model="text-davinci-002", prompt=text + "\n\nResumen de 50 palabras:\n\n", temperature=0.7, max_tokens=200, top_p=1, frequency_penalty=1, presence_penalty=1 ) return response.choices[0].text def send_email(subject, body): to_address = email_address_to_send_to msg = email.message_from_string(body) msg['From'] = username msg['To'] = to_address msg['Subject'] = "Resumen: " + subject server = smtplib.SMTP("smtp-mail.outlook.com", port=587) server.ehlo() # Hostname to send for this command defaults to the fully qualified domain name of the local host. server.starttls() #Puts connection to SMTP server in TLS mode server.ehlo() server.login(username, password) server.sendmail(username, to_address, msg.as_string().encode('utf-8')) server.quit() print("Email sent!") #do every 1 minute schedule.every(1).minutes.do(read_imap) while 1: schedule.run_pending() time.sleep(1)
[ "PLACEHOLDER\n\nResumen de 50 palabras:\n\n" ]
2024-01-10
AndyJZhao/CovidLLM
src~llm~fake_llm.py
from langchain.llms.fake import FakeListLLM seq_default_list = ['<answer>C</answer>' for _ in range(20)] + ['<answer>?</answer>' for _ in range(200)] + ['<answer>C</answer>' for _ in range(20000)] class CpuFakeDebucovid_llm: fake_llm = FakeListLLM(responses=seq_default_list) # Choose C as Default def generate_text(self, prompt, max_new_tokens=1, choice_only=False): return self.fake_llm(prompt)[:max_new_tokens]
[]
2024-01-10
dackdel/extract-juice
write_pdf.py
import os import openai import pdfplumber from time import time,sleep import textwrap import re import glob 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 convert_pdf2txt(src_dir, dest_dir): files = os.listdir(src_dir) files = [i for i in files if '.pdf' in i] for file in files: try: with pdfplumber.open(src_dir+file) as pdf: output = '' for page in pdf.pages: output += page.extract_text() output += '\n\nNEW PAGE\n\n' #Control # of pages save_file(dest_dir+file.replace('.pdf','.txt'), output.strip()) except Exception as oops: print(oops, file) openai.api_key = open_file('openaiapikey.txt') #Initial Summary def gpt_3 (prompt): response = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=0, max_tokens=700, top_p=1, frequency_penalty=0, presence_penalty=0, ) text = response['choices'][0]['text'].strip() return text #Final Summary def gpt_31 (prompt): response = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=0, max_tokens=700, top_p=1, frequency_penalty=0, presence_penalty=0, ) text = response['choices'][0]['text'].strip() return text if __name__ == '__main__': #Call PDF Converter Function convert_pdf2txt('PDFs/', 'textPDF/') #Your Pathfolder pathfolder = './textPDF' #Get a list of all text files in the specified folder files = glob.glob(f'{pathfolder}/*.txt') #Initialize an empty string to store the contents of all the text files alltext = "" #Iterate over the list of files for file in files: with open(file, 'r', encoding='utf-8') as infile: #Open the file alltext += infile.read() #Read the contents of the file and append it to the alltext string chunks = textwrap.wrap(alltext, 4000) result = list() count = 0 #Write a summary for chunk in chunks: count = count + 1 prompt = open_file('prompt.txt').replace('<<SUMMARY>>', chunk) prompt = prompt.encode(encoding='ASCII',errors='ignore').decode() summary = gpt_3(prompt) print('\n\n\n', count, 'out of', len(chunks), 'Compressions', ' : ', summary) result.append(summary) save_file("pdfsummary.txt", '\n\n'.join(result)) # Split the contents of pdfsummary.txt into chunks with a textwrap of 3000 with open("pdfsummary.txt", 'r', encoding='utf-8') as infile: summary = infile.read() chunks = textwrap.wrap(summary, 3000) #Initialize empty lists to store the results result = [] result2 = [] #Writw notes from chunks for i, chunk in enumerate(chunks): # Read the contents of prompt2.txt with open("prompt2.txt", 'r', encoding='utf-8') as infile: prompt = infile.read() # Replace the placeholder in the prompt with the current chunk prompt = prompt.replace("<<NOTES>>", chunk) # Run the chunk through the gpt_3 function notes = gpt_3(prompt) #Write summary from notes keytw = open_file('prompt6.txt').replace('<<NOTES>>', chunk) keytw2 = gpt_31(keytw) # Print the result print(f"\n\n\n{i+1} out of {len(chunks)} Compressions: {notes}") # Append the results to the lists result.append(notes) result2.append(keytw2) #Save the results to a file with open("notes.txt", 'w', encoding='utf-8') as outfile: outfile.write("\n\n".join(result)) with open("notessum.txt", 'w', encoding='utf-8') as outfile: outfile.write("\n\n".join(result2)) #Summary of notes sumnotes = open_file("notessum.txt") #Write a step by step guide form notes keytw = open_file('prompt3.txt').replace('<<NOTES>>', sumnotes) keytw2 = gpt_31(keytw) print(keytw2) save_file("steps.txt", keytw2) #Write essential info essencial1 = open_file('prompt4.txt').replace('<<NOTES>>', sumnotes) essencial2 = gpt_31(essencial1) print(essencial2) save_file("essencial.txt", essencial2) #Write blog post blogpost = open_file('essencial.txt') blogpostw = open_file('prompt5.txt').replace('<<NOTES>>', blogpost) blogpostw2 = gpt_31(blogpostw) print(blogpostw2) save_file("blogpost.txt", blogpostw2) #Write visual prompt midj = open_file("essencial.txt") mjv4 = open_file('mjv4prompts.txt').replace('<<SCENE>>', midj) desc = gpt_31(mjv4) print('\n\n', desc) save_file("midprompts.txt", desc)
[ "prompt.txt", "<<SUMMARY>>", "ignore" ]
2024-01-10
kamesan1577/moral-check-gpt
endpoint.py
import openai from jinaai import JinaAI # ModerationAPIの呼び出し def get_moderation(msg, img_description=None): with open("key.secret", "r") as f: openai.api_key = f.read().strip() openai.api_base = "https://api.openai.iniad.org/api/v1" if img_description: msg = f"{msg}\n ###\nDescription of the image attached to the Tweet: {img_description}" else: msg = msg response = openai.Moderation.create(input=msg) return (msg, response["results"][0]) # 英語に翻訳してから呼び出すバージョン def get_moderation_after_translate(msg, img_description=None, model="gpt-3.5-turbo"): # 普通にChatGPTに翻訳させる msg_translated = openai.ChatCompletion.create( model=model, messages=[ { "role": "user", "content": f"Translate this message to English(return only translation result): {msg}", }, ], )["choices"][0]["message"]["content"] return get_moderation(msg_translated, img_description=img_description) def explain_image(img_path): with open("scenex.secret", "r") as f: jinaai_api_key = f.read().strip() jinaai = JinaAI(secrets={"scenex-secret": jinaai_api_key}) descriptions = jinaai.describe(img_path) return descriptions["results"][0]["output"]
[ "Translate this message to English(return only translation result): msgfc92206a-711f-4796-ae89-536795e55320\n ###\nDescription of the image attached to the Tweet: PLACEHOLDER", "Translate this message to English(return only translation result): msgcf4e874d-31a2-4a56-910c-0f8deda9af0c\n ###\nDescription of the image attached to the Tweet: PLACEHOLDER" ]
2024-01-10
zoolhasson/gluon-ts
src~gluonts~nursery~temporal_hierarchical_forecasting~model~cop_deepar~_network.py
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. # A copy of the License is located at # # http://www.apache.org/licenses/LICENSE-2.0 # # or in the "license" file accompanying this file. This file 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. from typing import Dict, List, Optional, Tuple import mxnet as mx import numpy as np from gluonts.core.component import Type, validated from gluonts.itertools import prod from gluonts.mx.model.deepar import DeepAREstimator from gluonts.mx.model.deepar._network import DeepARPredictionNetwork from gluonts.mx.model.deepvar_hierarchical._estimator import projection_mat from gluonts.mx.model.deepvar_hierarchical._network import coherency_error from gluonts.mx.distribution import Distribution, EmpiricalDistribution from gluonts.mx import Tensor from gluonts.mx.distribution import TransformedPiecewiseLinear from gluonts.nursery.temporal_hierarchical_forecasting.utils import utils from gluonts.nursery.temporal_hierarchical_forecasting.model.cop_deepar import ( gluonts_fixes, gnn, ) def reconcile_samples( reconciliation_mat: Tensor, samples: Tensor, non_negative: bool = False, num_iters: int = 10, ) -> Tensor: if not non_negative: return mx.nd.dot(samples, reconciliation_mat, transpose_b=True) else: # Dykstra's projection method: Projection onto the intersection of convex sets. x = samples p = mx.nd.zeros_like(x) q = mx.nd.zeros_like(x) for _ in range(num_iters): # Projection onto the non-negative orthant. y = mx.nd.relu(x + p) p = x + p - y # Projection onto the null space. x = mx.nd.dot(y + q, reconciliation_mat, transpose_b=True) q = y + q - x return x class COPNetwork(mx.gluon.HybridBlock): @validated() def __init__( self, estimators: List[DeepAREstimator], prediction_length: int, temporal_hierarchy: utils.TemporalHierarchy, do_reconciliation: bool, dtype: Type, use_gnn: bool, use_mlp: bool, adj_mat_option: str, non_negative: bool = False, naive_reconciliation: bool = False, prediction: bool = False, loss_function: str = "crps_univariate", **kwargs, ) -> None: super().__init__(**kwargs) self.prediction_length = prediction_length self.temporal_hierarchy = temporal_hierarchy self.use_gnn = use_gnn self.use_mlp = use_mlp self.adj_mat_option = adj_mat_option self.do_reconciliation = do_reconciliation self.non_negative = non_negative self.loss_function = loss_function self.dtype = dtype if naive_reconciliation: M = utils.naive_reconcilation_mat( self.temporal_hierarchy.agg_mat, self.temporal_hierarchy.nodes ) else: M = projection_mat(S=self.temporal_hierarchy.agg_mat) self.M = mx.nd.array(M) self.estimators = estimators self.models = [] with self.name_scope(): for estimator in estimators: if not prediction: self.network = estimator.create_training_network() else: self.network = gluonts_fixes.create_prediction_network( estimator ) self.register_child(self.network) self.models.append(self.network) if self.use_gnn: # GNN Layer: Do message passing for `L-1` times, where `L` is the number of levels of the hierarchy. self.gnn = gnn.GNN( units=self.estimators[0].num_cells, num_layers=len(self.temporal_hierarchy.agg_multiples) - 1, adj_matrix=mx.nd.array( self.temporal_hierarchy.adj_mat( option=self.adj_mat_option ) ), use_mlp=self.use_mlp, ) def get_target_related_feat_at_agg_level( self, agg_level: int, past_target: Tensor, past_observed_values: Tensor, past_is_pad: Tensor, future_target: Optional[Tensor] = None, future_observed_values: Optional[Tensor] = None, ) -> Dict: """ Aggregate target at the given aggregate level along with updating observed value and pad indicators. :param agg_level: :param past_target: :param past_observed_values: :param past_is_pad: :param future_target: :param future_observed_values: :return: """ agg_multiple = self.temporal_hierarchy.agg_multiples[agg_level] # Truncating the history length of the base time series to the nearest multiple. base_history_length = ( past_target.shape[1] // agg_multiple ) * agg_multiple past_target_agg = ( utils.agg_series( past_target.slice_axis( axis=1, begin=-base_history_length, end=None ), agg_multiple=agg_multiple, ) .squeeze(axis=-1) .slice_axis( axis=1, begin=-self.models[agg_level].history_length, end=None ) ) past_is_pad_agg = ( utils.agg_series( past_is_pad.slice_axis( axis=1, begin=-base_history_length, end=None ), agg_multiple=agg_multiple, ) .squeeze(axis=-1) .slice_axis( axis=1, begin=-self.models[agg_level].history_length, end=None ) ) past_is_pad_agg = mx.nd.where( past_is_pad_agg == 0.0, mx.nd.zeros_like(past_is_pad_agg), mx.nd.ones_like(past_is_pad_agg), ) past_observed_values_agg = ( utils.agg_series( past_observed_values.slice_axis( axis=1, begin=-base_history_length, end=None ), agg_multiple=agg_multiple, ) .squeeze(axis=-1) .slice_axis( axis=1, begin=-self.models[agg_level].history_length, end=None ) ) past_observed_values_agg = mx.nd.where( # We sum observed values of base time series at `agg_multiple` time steps; # if all of them are 1, then the observed value for the aggregated time series is 1 and 0 otherwise. # We could redefine agg_series to actually compute mean, but overloading that term might cause other # problems later. past_observed_values_agg == agg_multiple, mx.nd.ones_like(past_observed_values_agg), mx.nd.zeros_like(past_observed_values_agg), ) target_related_feat_agg = { "past_target": past_target_agg, "past_is_pad": past_is_pad_agg, "past_observed_values": past_observed_values_agg, } if future_target is not None: future_target_agg = utils.agg_series( future_target, agg_multiple=agg_multiple ).squeeze(axis=-1) future_observed_values_agg = utils.agg_series( future_observed_values, agg_multiple=agg_multiple ).squeeze(axis=-1) future_observed_values_agg = mx.nd.where( future_observed_values_agg == agg_multiple, mx.nd.ones_like(future_observed_values_agg), mx.nd.zeros_like(future_observed_values_agg), ) target_related_feat_agg.update( { "future_target": future_target_agg, "future_observed_values": future_observed_values_agg, } ) return target_related_feat_agg def _embeddings_to_distr( self, F, embeddings_at_all_levels: Tensor, scales: List, ) -> Distribution: distr_output = self.models[0].distr_output distr_args_at_all_levels: Dict = { arg_name: [] for arg_name in distr_output.args_dim.keys() } scales_ls = [] start_ix = 0 for i, num_nodes in enumerate( self.temporal_hierarchy.num_nodes_per_level ): end_ix = start_ix + num_nodes distr_args = self.models[i].proj_distr_args( embeddings_at_all_levels[..., start_ix:end_ix, :] ) for j, arg_ls in enumerate(distr_args_at_all_levels.values()): arg_ls.append(distr_args[j]) scales_ls.append(scales[i].broadcast_like(distr_args[0])) start_ix = end_ix # Last dimension contains parameters at all time-levels and aggregation can be done on it. distr_args_at_all_levels = { arg_name: F.concat(*arg_ls, dim=-1) for arg_name, arg_ls in distr_args_at_all_levels.items() } scale_at_all_levels = F.concat(*scales_ls, dim=-1) distr_at_all_levels = distr_output.distribution( distr_args=distr_args_at_all_levels.values(), scale=scale_at_all_levels, ) if isinstance(distr_at_all_levels, TransformedPiecewiseLinear): distr_at_all_levels = TransformedPiecewiseLinear( base_distribution=gluonts_fixes.PiecewiseLinearWithSampling( gamma=distr_at_all_levels.base_distribution.gamma, slopes=distr_at_all_levels.base_distribution.slopes, knot_spacings=distr_at_all_levels.base_distribution.knot_spacings, ), transforms=distr_at_all_levels.transforms, ) return distr_at_all_levels def _distr_to_samples( self, distr_at_all_levels: Distribution, num_samples: int, ): if num_samples == 1: samples_at_all_levels = distr_at_all_levels.sample( num_samples=num_samples, dtype=self.dtype ) # get rid of the redundant axis introduced by `sample`. samples_at_all_levels = samples_at_all_levels.squeeze(axis=0) else: samples_at_all_levels = distr_at_all_levels.sample_rep( num_samples=num_samples, dtype=self.dtype ) return samples_at_all_levels class COPDeepARTrainingNetwork(COPNetwork): @validated() def __init__( self, num_batches_per_epoch: int, epochs: int, warmstart_epoch_frac: float, num_samples_for_loss: int = 200, **kwargs, ) -> None: super().__init__(**kwargs) self.warmstart_epoch_frac = warmstart_epoch_frac self.epochs = epochs self.num_batches_per_epoch = num_batches_per_epoch self.batch_no = 0 self.num_samples_for_loss = num_samples_for_loss # noinspection PyMethodOverriding,PyPep8Naming def hybrid_forward( self, F, feat_static_cat: Tensor, feat_static_real: Tensor, past_time_feat: Tensor, past_target: Tensor, past_observed_values: Tensor, past_is_pad: Optional[Tensor], future_time_feat: Tensor, future_target: Tensor, future_observed_values: Tensor, agg_features_dict: Dict, ) -> Tensor: """ Computes the loss for training COPDeepAR, all inputs tensors representing time series have NTC layout. Parameters ---------- F feat_static_cat : (batch_size, num_features) feat_static_real : (batch_size, num_features) past_time_feat : (batch_size, history_length, num_features) past_target : (batch_size, history_length, *target_shape) past_observed_values : (batch_size, history_length, *target_shape, seq_len) future_time_feat : (batch_size, prediction_length, num_features) future_target : (batch_size, prediction_length, *target_shape) future_observed_values : (batch_size, prediction_length, *target_shape) agg_features_dict: Dictionary of features for aggregated levels Returns loss with shape (batch_size, context + prediction_length, 1) ------- """ embeddings_at_all_levels_ls = [] target_at_all_levels_ls = [] scale_ls = [] for i, agg_multiple in enumerate( self.temporal_hierarchy.agg_multiples ): if agg_multiple != 1: past_time_feat_agg = agg_features_dict[f"level_{i}"][ "past_time_feat_agg" ] future_time_feat_agg = agg_features_dict[f"level_{i}"][ "future_time_feat_agg" ] else: past_time_feat_agg = past_time_feat future_time_feat_agg = future_time_feat target_related_feat_agg = ( self.get_target_related_feat_at_agg_level( agg_level=i, past_target=past_target, past_is_pad=past_is_pad, past_observed_values=past_observed_values, future_target=future_target, future_observed_values=future_observed_values, ) ) rnn_outputs, _, scale, _, _ = self.models[i].unroll_encoder( F=F, feat_static_cat=feat_static_cat, feat_static_real=feat_static_real, past_time_feat=past_time_feat_agg, future_time_feat=future_time_feat_agg, **target_related_feat_agg, ) scale_ls.append(scale.expand_dims(axis=-1)) # put together target sequence # (batch_size, seq_len, *target_shape) target = F.concat( target_related_feat_agg["past_target"].slice_axis( axis=1, begin=self.models[i].history_length - self.models[i].context_length, end=None, ), target_related_feat_agg["future_target"], dim=1, ) # We reconcile blocks/windows of time steps: e.g., if we have 28 values of daily data, then we # reconcile 4 windows where each window has a length of 7 if number of leaves in the hierarchy is 7. window_size = self.temporal_hierarchy.num_leaves // agg_multiple num_windows = ( self.models[i].context_length + self.models[i].prediction_length ) // window_size embeddings_at_all_levels_ls.append( rnn_outputs.reshape( ( rnn_outputs.shape[0], num_windows, -1, rnn_outputs.shape[-1], ) ) ) target_at_all_levels_ls.append( target.reshape((target.shape[0], num_windows, -1)) ) # Last dimension contains embeddings at all time-levels and message passing/aggregation can be done on it. # Shape: (bs, num_windows, total_num_time_steps_of_hierarchy, embedding_dim) embeddings_at_all_levels = F.concat( *embeddings_at_all_levels_ls, dim=-2 ) if self.use_gnn: embeddings_at_all_levels = self.gnn(embeddings_at_all_levels) distr_at_all_levels = self._embeddings_to_distr( F, embeddings_at_all_levels, scale_ls, ) target_at_all_levels = F.concat(*target_at_all_levels_ls, dim=-1) if self.loss_function == "nll": loss = distr_at_all_levels.loss(x=target_at_all_levels) # Determine which epoch we are currently in. self.batch_no += 1 epoch_no = self.batch_no // self.num_batches_per_epoch + 1 epoch_frac = epoch_no / self.epochs if epoch_frac > self.warmstart_epoch_frac: print( f"epoch_frac: {epoch_frac}. Switching the loss function to CRPS" ) self.loss_function = "crps_univariate" else: samples_at_all_levels = self._distr_to_samples( distr_at_all_levels, num_samples=self.num_samples_for_loss, ) if self.do_reconciliation: reconciled_samples_at_all_levels = reconcile_samples( reconciliation_mat=self.M, samples=samples_at_all_levels, non_negative=self.non_negative, ) else: reconciled_samples_at_all_levels = samples_at_all_levels loss = ( EmpiricalDistribution( samples=reconciled_samples_at_all_levels, event_dim=1 ) .loss(x=target_at_all_levels) .expand_dims(axis=-1) ) return loss class COPDeepARPredictionNetwork(COPNetwork): @validated() def __init__( self, return_forecasts_at_all_levels: bool = False, num_parallel_samples: int = 100, **kwargs, ) -> None: super().__init__(prediction=True, **kwargs) self.return_forecasts_at_all_levels = return_forecasts_at_all_levels self.num_parallel_samples = num_parallel_samples def _decode_one_window( self, F, model: DeepARPredictionNetwork, window_size: int, offset: int, static_feat: Tensor, past_target: Tensor, time_feat: Tensor, scale: Tensor, begin_states: List, ) -> Tuple[Tensor, Tensor]: """ Computes RNN outputs by unrolling the LSTM starting with a initial input and state. Parameters ---------- static_feat : Tensor static features. Shape: (batch_size, num_static_features). past_target : Tensor target history. Shape: (batch_size, history_length). time_feat : Tensor time features. Shape: (batch_size, prediction_length, num_time_features). Note: They still need to be for all `prediction_length` time steps. This function will slice the features it needs. scale : Tensor tensor containing the scale of each element in the batch. Shape: (batch_size, 1, 1). begin_states : List list of initial states for the LSTM layers. The shape of each tensor of the list should be (batch_size, num_cells) Returns -------- Tensor A tensor containing sampled paths. Shape: (batch_size, num_sample_paths, window_size). """ rnn_outputs_ls = [] # for each future time-units we draw new samples for this time-unit and # update the state for k in range(offset, offset + window_size): # (batch_size * num_samples, 1, *target_shape, num_lags) lags = model.get_lagged_subsequences( F=F, sequence=past_target, sequence_length=model.history_length + k, indices=model.shifted_lags, subsequences_length=1, ) # (batch_size * num_samples, 1, *target_shape, num_lags) lags_scaled = F.broadcast_div(lags, scale.expand_dims(axis=-1)) # from (batch_size * num_samples, 1, *target_shape, num_lags) # to (batch_size * num_samples, 1, prod(target_shape) * num_lags) input_lags = F.reshape( data=lags_scaled, shape=(-1, 1, prod(model.target_shape) * len(model.lags_seq)), ) # (batch_size * num_samples, 1, prod(target_shape) * num_lags + # num_time_features + num_static_features) decoder_input = F.concat( input_lags, time_feat.slice_axis(axis=1, begin=k, end=k + 1), # observed_values.expand_dims(axis=1), static_feat, dim=-1, ) # output shape: (batch_size * num_samples, 1, num_cells) # state shape: (batch_size * num_samples, num_cells) rnn_outputs, begin_states = model.rnn.unroll( inputs=decoder_input, length=1, begin_state=begin_states, layout="NTC", merge_outputs=True, ) rnn_outputs_ls.append(rnn_outputs) distr_args = model.proj_distr_args(rnn_outputs) # compute likelihood of target given the predicted parameters distr = model.distr_output.distribution(distr_args, scale=scale) # (batch_size * num_samples, 1, *target_shape) new_samples = distr.sample(dtype=self.dtype) # (batch_size * num_samples, seq_len, *target_shape) past_target = F.concat(past_target, new_samples, dim=1) # (batch_size * num_samples, prediction_length, *target_shape) rnn_outputs = F.concat(*rnn_outputs_ls, dim=1) return rnn_outputs, begin_states def sampling_decoder( self, F, state_ls, scale_ls, static_feat_ls, past_target_ls, future_time_feat_agg_ls, ): num_windows = ( self.prediction_length // self.temporal_hierarchy.num_leaves ) num_nodes_per_level = self.temporal_hierarchy.num_nodes_per_level reconciled_samples_at_all_levels_ls = [] for j in range(num_windows): embeddings_at_all_levels_ls = [] for i, agg_multiple in enumerate( self.temporal_hierarchy.agg_multiples ): rnn_outputs, states = self._decode_one_window( F=F, model=self.models[i], window_size=num_nodes_per_level[i], offset=j * num_nodes_per_level[i], past_target=past_target_ls[i], time_feat=future_time_feat_agg_ls[i], static_feat=static_feat_ls[i], scale=scale_ls[i], begin_states=state_ls[i], ) state_ls[i] = states embeddings_at_all_levels_ls.append( rnn_outputs.reshape( (rnn_outputs.shape[0], -1, rnn_outputs.shape[-1]) ) ) # Last dimension contains embeddings at all time-levels and message passing/aggregation can be done on it. # Shape: (bs, total_num_time_steps_of_hierarchy, embedding_dim) embeddings_at_all_levels = F.concat( *embeddings_at_all_levels_ls, dim=-2 ) if self.use_gnn: embeddings_at_all_levels = self.gnn(embeddings_at_all_levels) distr_at_all_levels = self._embeddings_to_distr( F, embeddings_at_all_levels, scale_ls, ) samples_at_all_levels = self._distr_to_samples( distr_at_all_levels, num_samples=1, ) if self.do_reconciliation: reconciled_samples_at_all_levels = reconcile_samples( reconciliation_mat=self.M, samples=samples_at_all_levels, non_negative=self.non_negative, ) else: reconciled_samples_at_all_levels = samples_at_all_levels rec_err = coherency_error( S=self.temporal_hierarchy.agg_mat, samples=reconciled_samples_at_all_levels.asnumpy(), ) print(f"Reconciliation error: {rec_err}") cumsum_nodes_per_level = np.cumsum([0] + num_nodes_per_level) for i in range(len(self.temporal_hierarchy.agg_multiples)): # (batch_size * num_samples, seq_len, *target_shape) reconciled_samples = ( reconciled_samples_at_all_levels.slice_axis( axis=-1, begin=cumsum_nodes_per_level[i], end=cumsum_nodes_per_level[i + 1], ) ) past_target_ls[i] = F.concat( past_target_ls[i], reconciled_samples, dim=1 ) reconciled_samples_at_all_levels_ls.append( reconciled_samples_at_all_levels.reshape( shape=( -1, self.num_parallel_samples, reconciled_samples_at_all_levels.shape[-1], ) ).expand_dims(axis=-2) ) reconciled_samples_at_all_levels = F.concat( *reconciled_samples_at_all_levels_ls, dim=-2 ) print(reconciled_samples_at_all_levels.shape) return reconciled_samples_at_all_levels # noinspection PyMethodOverriding,PyPep8Naming def hybrid_forward( self, F, feat_static_cat: Tensor, # (batch_size, num_features) feat_static_real: Tensor, # (batch_size, num_features) past_time_feat: Tensor, # (batch_size, history_length, num_features) past_target: Tensor, # (batch_size, history_length, *target_shape) past_observed_values: Tensor, # (batch_size, history_length, *target_shape) future_time_feat: Tensor, # (batch_size, prediction_length, num_features) past_is_pad: Tensor, agg_features_dict: Dict, ) -> Tensor: """ Predicts samples, all tensors should have NTC layout. Parameters ---------- F feat_static_cat : (batch_size, num_features) feat_static_real : (batch_size, num_features) past_time_feat : (batch_size, history_length, num_features) past_target : (batch_size, history_length, *target_shape) past_observed_values : (batch_size, history_length, *target_shape) future_time_feat : (batch_size, prediction_length, num_features) agg_features_dict: Dictionary of features for aggregated levels Returns ------- Tensor Predicted samples """ ( state_ls, scale_ls, static_feat_ls, past_target_ls, future_time_feat_agg_ls, ) = ([], [], [], [], []) for i, agg_multiple in enumerate( self.temporal_hierarchy.agg_multiples ): if agg_multiple != 1: past_time_feat_agg = agg_features_dict[f"level_{i}"][ "past_time_feat_agg" ] future_time_feat_agg = agg_features_dict[f"level_{i}"][ "future_time_feat_agg" ] else: past_time_feat_agg = past_time_feat future_time_feat_agg = future_time_feat target_related_feat_agg = ( self.get_target_related_feat_at_agg_level( agg_level=i, past_target=past_target, past_is_pad=past_is_pad, past_observed_values=past_observed_values, ) ) # unroll the decoder in "prediction mode", i.e. with past data only _, states, scale, static_feat, imputed_sequence = self.models[ i ].unroll_encoder( F=F, feat_static_cat=feat_static_cat, feat_static_real=feat_static_real, past_time_feat=past_time_feat_agg, future_observed_values=None, future_time_feat=None, future_target=None, **target_related_feat_agg, ) # blows-up the dimension of each tensor to batch_size * # self.num_parallel_samples for increasing parallelism repeated_past_target = imputed_sequence.repeat( repeats=self.num_parallel_samples, axis=0 ) repeated_states = [ s.repeat(repeats=self.num_parallel_samples, axis=0) for s in states ] repeated_time_feat = future_time_feat_agg.repeat( repeats=self.num_parallel_samples, axis=0 ) repeated_static_feat = static_feat.repeat( repeats=self.num_parallel_samples, axis=0 ).expand_dims(axis=1) repeated_scale = scale.repeat( repeats=self.num_parallel_samples, axis=0 ) state_ls.append(repeated_states) scale_ls.append(repeated_scale) static_feat_ls.append(repeated_static_feat) past_target_ls.append(repeated_past_target) future_time_feat_agg_ls.append(repeated_time_feat) reconciled_samples_at_all_levels = self.sampling_decoder( F, state_ls=state_ls, scale_ls=scale_ls, static_feat_ls=static_feat_ls, past_target_ls=past_target_ls, future_time_feat_agg_ls=future_time_feat_agg_ls, ) if self.return_forecasts_at_all_levels: return reconciled_samples_at_all_levels else: reconciled_samples_at_bottom_level = ( reconciled_samples_at_all_levels.slice_axis( axis=-1, begin=-self.temporal_hierarchy.num_leaves, end=None, ) ) reconciled_samples_at_bottom_level = ( reconciled_samples_at_bottom_level.reshape( ( reconciled_samples_at_bottom_level.shape[0], reconciled_samples_at_bottom_level.shape[1], -1, ) ) ) return reconciled_samples_at_bottom_level
[]
2024-01-10
Bailey-Man/DisCawed
imagegen.py
# image generation from io import BytesIO from openai import OpenAI # double check this # init client = OpenAI()# api_key='sk-xxx') # do i need an api key for the bot ?? # this is an object that contains image data byte_stream: BytesIO = [my image data] # this is a placeholder # WHAT FOR ??? byte_array = byte_stream.getvalue() try: # response = OpenAI.File.create(file=byte_array, purpose='image generation') response = client.images.create_variation( file=byte_array, purpose='image generation', # model='davinci', # prompt='This is a test', # max_tokens=5, # temperature=0.7, # stop=['\n', " Human:", " AI:"] ) print(response.data[0].url) except Exception as e: print(e) print('error') # return None
[]
2024-01-10
Bailey-Man/DisCawed
plap.py
# dnd discord bot implementation import regex as re import os import sys import datetime import discord from discord.ext import commands import random import requests if sys.path.count('src') == 0: sys.path.append('src') else: print('src already in path') print('sys.path', sys.path) # get rid of this when tested from src.commands import helper # from openai import OpenAI from dotenv import load_dotenv ## init load_dotenv() # do i do this in script or within the bot init?? # force intents myintents = discord.Intents.default() myintents.members = True myintents.message_content = True bot = commands.Bot(command_prefix='!', intents=myintents) ## EVENTS ## @bot.event async def on_ready(): print(f'{bot.user.name} has connected to Discord!') @bot.event async def on_command_error(ctx, error): if isinstance(error, commands.errors.CheckFailure): await ctx.send('you do not have the correct role for this command') ### TEST THESE ### # when player accepts invite to the server, send a message to admin channel with who joined and who invited them @bot.event async def on_member_join(member): # get the admin channel admin_channel = discord.utils.get(member.guild.channels, name='admin') # get the invite that was used to join the server invite = await admin_channel.create_invite(max_age=300) # get the user who created the invite inviter = invite.inviter # send a message to the admin channel with who joined and who invited them await admin_channel.send(f'{member.name} joined using invite created by {inviter.name}') # when player joins the server, check if they are in the database. # if they are, populate their character sheet with that data # if they are not, ask them to run the !register command # @bot.event # async def on_member_join(member): # # if member in preset list; populate with that # # else; diagnose from avatar and username # if member in preset_list: # pass # whenever a message contains the substring 'BUDDY', respond with 'BUDDYY' with 3 times as many Y's as the original message @bot.event async def on_message(message): if 'BUDDY' in message.content: response = 'BUDDY' + 'Y' * (len(message.content) - 5) await message.channel.send(response) await bot.process_commands(message) ## COMMANDS ## @bot.diceroll(name='roll', help='roll dice') async def roll(ctx, *args): # take a string that contains a dice roll in format NdN with an optional 'w adv' or 'w disadv' input_string = ' '.join(args) if input_string == '': await ctx.send('please provide a dice roll in the format NdN') return None # regex to extract whether the dice roll has advantage or disadvantage adv_pattern = r'(w adv|w disadv)' dice_pattern = r'(\d+)d(\d+)' adv_match = re.search(adv_pattern, input_string) # if adv_pattern matches dice_match = re.search(dice_pattern, input_string) adv_status = None if adv_match: # determine if adv or disadv adv_or_disadv = adv_match.group(1) # ? what does this do ?? print('adv_or_disadv', adv_or_disadv) if adv_or_disadv == 'w adv': adv_status = True elif adv_or_disadv == 'w disadv': adv_status = False else: print('adv_match', adv_match, adv_match.group(1)) await ctx.send('please provide a dice roll in the format NdN') if dice_match == None: await ctx.send('please provide a dice roll in the format NdN') return None else: number_of_dice = int(dice_match.group(1)) number_of_sides = int(dice_match.group(2)) # roll the dice myroll = helper.roll_dnd_dice(adv_status, number_of_dice, number_of_sides) myresponse = f'your roll of {number_of_dice}d{number_of_sides} is {myroll}' await ctx.send(myresponse) # ban a randomly selected user from the server @bot.command(name='random_ban', help='ban a randomly selected user from the server') async def random_ban(ctx): # get the list of members in the server member_list = ctx.guild.members # randomly select a member and assert its not the user that called the command member = random.choice(member_list) while member == ctx.author: member = random.choice(member_list) # ban the member await member.ban() # send a message to the channel that the member was banned await ctx.send(f'{member.name} was banned') #
[]
2024-01-10
BobbyLumpkin/docs2chat
src~docs2chat~preprocessing~preprocessing.py
""" Langchain LLM pipeline for generative QA pipeline. """ from dataclasses import dataclass, field, InitVar from haystack.document_stores import FAISSDocumentStore from langchain.embeddings import HuggingFaceEmbeddings from langchain.text_splitter import CharacterTextSplitter import logging from pathlib import Path import sys from typing import Iterable, Literal, Optional, Union from docs2chat.config import Config, config from docs2chat.preprocessing.utils import ( create_vectorstore, langchain_to_haystack_docs, load_and_split_from_dir, load_and_split_from_str, _EmbeddingsProtocol, _RetrieverProtocol, _TextSplitterProtocol ) _logger = logging.getLogger(__name__) _logger.setLevel(logging.INFO) _formatter = logging.Formatter( "%(asctime)s:%(levelname)s:%(module)s: %(message)s" ) _console_handler = logging.StreamHandler(sys.stdout) _console_handler.setFormatter(_formatter) _logger.addHandler(_console_handler) @dataclass class ExtractivePreProcessor: LOADER_FACTORY = { "text": load_and_split_from_str, "dir": load_and_split_from_dir } content: Union[str, list[str]] = field(default=config.DOCUMENTS_DIR) docs: Optional[list] = field(default=None) load_from_type: str = field(default="dir") text_splitter: Optional[_TextSplitterProtocol] = field(default=None) def __post_init__(self): if self.load_from_type not in ["text", "dir"]: raise ValueError( "`load_from_type` must be one of `text` or `dir`." ) if self.text_splitter is None: _logger.info( "Generating text splitter." ) text_splitter = CharacterTextSplitter( separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len, ) setattr(self, "text_splitter", text_splitter) def load_and_split(self, show_progress=True, store=False): load_func = ExtractivePreProcessor.LOADER_FACTORY[self.load_from_type] docs = langchain_to_haystack_docs(load_func( content=self.content, text_splitter=self.text_splitter, show_progress=show_progress )) if store: setattr(self, "docs", docs) return docs def create_vectorstore(self, store=False): vectorstore = FAISSDocumentStore( sql_url="sqlite:///", embedding_dim=384 ) if store: setattr(self, "vectorstore", vectorstore) return vectorstore def preprocess( self, show_progress: bool = True, return_vectorstore: bool = True, store_docs: bool = False, store_vectorstore: bool = True ): _logger.info( "Loading documents into vectorstore. " "This may take a few mquitinutes ..." ) docs = self.load_and_split( show_progress=show_progress, store=store_docs ) vectorstore = self.create_vectorstore( store=store_vectorstore ) vectorstore.write_documents(docs) if store_vectorstore: setattr(self, "vectorstore", vectorstore) if return_vectorstore: return vectorstore return @dataclass class GenerativePreProcessor: LOADER_FACTORY = { "text": load_and_split_from_str, "dir": load_and_split_from_dir } content: Union[str, list[str]] = field(default=config.DOCUMENTS_DIR) docs: Optional[list] = field(default=None) embeddings: Optional[_EmbeddingsProtocol] = field(default=None) load_from_type: str = field(default="dir") text_splitter: Optional[_TextSplitterProtocol] = field(default=None) def __post_init__(self): if self.load_from_type not in ["text", "dir"]: raise ValueError( "`load_from_type` must be one of `text` or `dir`." ) if self.text_splitter is None: _logger.info( "Generating text splitter." ) text_splitter = CharacterTextSplitter( separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len, ) setattr(self, "text_splitter", text_splitter) if self.embeddings is None: _logger.info( f"Loading embedding model from {config.EMBEDDING_DIR}." ) embeddings = HuggingFaceEmbeddings( model_name=config.EMBEDDING_DIR ) setattr(self, "embeddings", embeddings) def load_and_split(self, show_progress=True, store=False): load_func = GenerativePreProcessor.LOADER_FACTORY[self.load_from_type] docs = load_func( content=self.content, text_splitter=self.text_splitter, show_progress=show_progress ) if store: setattr(self, "docs", docs) return docs def create_vectorstore(self, docs, store=False): vectorstore = create_vectorstore( docs=docs, embeddings=self.embeddings ) if store: setattr(self, "vectorstore", vectorstore) return vectorstore def preprocess( self, show_progress: bool = True, return_vectorstore: bool = True, store_docs: bool = False, store_vectorstore: bool = False ): _logger.info( "Loading documents into vectorstore. This may take a few minutes ..." ) docs = self.load_and_split( show_progress=show_progress, store=store_docs ) vectorstore = self.create_vectorstore( docs=docs, store=store_vectorstore ) if return_vectorstore: return vectorstore return class PreProcessor: preprocessor_dict = { "search": ExtractivePreProcessor, "snip": ExtractivePreProcessor, "generative": GenerativePreProcessor } def __new__( cls, chain_type: Literal["search", "snip", "generative"], **kwargs ): preprocessor_cls = cls.preprocessor_dict[chain_type] return preprocessor_cls(**kwargs)
[]
2024-01-10
abtawfik/thinkgpt
thinkgpt~abstract.py
import re import warnings from typing import Dict, List, Any import numpy as np from langchain import PromptTemplate, LLMChain from langchain.schema import LLMResult, BaseOutputParser, Generation from langchain.llms import BaseLLM, OpenAI from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.prompts.few_shot import FewShotPromptTemplate from thinkgpt.helper import LineSeparatorOutputParser examples = [ { "observations": '\n'.join([ "father stops at red traffic lights", "cars start moving when the light turns green", "bike yield to pedestrians at crosswalks with pedestrian signals", "truck stops at red traffic lights", ]), "rules": '\n'.join([ "drivers must stop at a red traffic light and can move in green lights", "drivers must yield to pedestrians at designated crosswalks", ]) }, { "observations": '\n'.join([ "Consider A a set of (X, Y) pairs", "first element is (1, 3)", "second element is (2, 5)", "third element is (3, 7)", "forth element is (4, 9)", ]), "rules": '\n'.join([ "The relationship between the first element X and the second element Y in the set A can be described by a function: y = f(x) = 2x - 1", ]) }, { "observations": '\n'.join([ "Fridge of mass 70 kg falls in 3 sec from height 4 meters", "pillow of mass 0.5 kg falls in 3 sec from height 4 meters", "rock of mass 1 kg falls in 1 sec from height 2 meters", "paper of mass 10 gram falls in 1 sec from height 2 meters", ]), "rules": '\n'.join([ "all objects fall at the same rate in a vacuum, regardless of their mass", ]) }, ] ABSTRACTION_EXAMPLE_PROMPT = PromptTemplate(template=""" Observations: {observations} Rules: {rules} --------- """, input_variables=["observations", "rules"]) ABSTRACTION_PROMPT = FewShotPromptTemplate( prefix="Extract rules from the following observations. Put each rule in a separate line. {instruction_hint}", # TODO: examples should be closes to the prefix/goal using example selector so they are easily applicable to specific use cases examples=examples, example_prompt=ABSTRACTION_EXAMPLE_PROMPT, suffix="Observations:\n{observations}\nRules:", input_variables=["instruction_hint", "observations"] ) class RememberOutputParser(BaseOutputParser[Dict]): def parse(self, text: str) -> Dict: # Greedy search for 1st json candidate. match = re.match(r"^REMEMBER\((.*)\)$", text.strip().strip('"\'.'), re.MULTILINE | re.IGNORECASE | re.DOTALL) if match: return {'action': 'REMEMBER', 'value': match.group(1)} else: return {'action': 'FINISH', 'value': text.strip()} class AbstractChain(LLMChain): """Prompts the LLM to request to remember memory as needed""" def __init__(self, **kwargs): super().__init__(prompt=ABSTRACTION_PROMPT, **kwargs) def predict(self, instruction_hint: str = '', **kwargs: Any) -> str: return super().predict(instruction_hint=instruction_hint, **kwargs) class AbstractMixin: abstract_chain: AbstractChain def abstract(self, observations: List[str], instruction_hint: str = '') -> List[str]: result = self.abstract_chain.predict( observations="\n".join(observations), instruction_hint=instruction_hint ) return LineSeparatorOutputParser().parse(result) if __name__ == '__main__': chain = AbstractChain(llm=ChatOpenAI(model_name="gpt-3.5-turbo")) print(chain.predict(observations="\n".join([ "in tunisian, I did not eat is \"ma khditech\"", "I did not work is \"ma khdemtech\"", "I did not go is \"ma mchitech\"", ]), instruction_hint="output the rule in french"))
[ "Observations:\n{observations}\nRules:", "Extract rules from the following observations. Put each rule in a separate line. {instruction_hint}", "instruction_hint", "observations", "\nObservations:\n{observations}\n\nRules:\n{rules}\n---------\n" ]
2024-01-10
abtawfik/thinkgpt
thinkgpt~summarize.py
import textwrap from pydantic.config import Extra import warnings from typing import Dict, List, Any from langchain import PromptTemplate, LLMChain from langchain.llms import OpenAI, BaseLLM from langchain.chat_models import ChatOpenAI SUMMARIZE_PROMPT = PromptTemplate(template=""" Shorten the following memory chunk of an autonomous agent from a first person perspective, using at most {max_tokens} tokens. {instruction_hint}: content: {content} --------- """, input_variables=["content", "instruction_hint", "max_tokens"]) class SummarizeChain(LLMChain, extra=Extra.allow): """Prompts the LLM to summarize content as needed""" def __init__(self, summarizer_chunk_size: int = 3000, **kwargs ): super().__init__(prompt=SUMMARIZE_PROMPT, **kwargs) self.summarizer_chunk_size = summarizer_chunk_size def predict(self, content, **kwargs: Any) -> str: return super().predict(content=content, **kwargs) class SummarizeMixin: summarize_chain: SummarizeChain def summarize(self, content: str, max_tokens: int = 4096, instruction_hint: str = '') -> str: response = self.summarize_chain.predict( # TODO: should retrieve max tokens from the llm if None content=content, instruction_hint=instruction_hint, max_tokens=max_tokens ) return response def chunked_summarize(self, content: str, max_tokens: int = 4096, instruction_hint: str = '') -> str: num_tokens = self.summarize_chain.llm.get_num_tokens(content) if num_tokens > max_tokens: avg_chars_per_token = len(content) / num_tokens chunk_size = int(avg_chars_per_token * self.summarize_chain.summarizer_chunk_size) chunks = textwrap.wrap(content, chunk_size) summary_size = int(max_tokens / len(chunks)) result = "" for chunk in chunks: result += self.summarize(content=chunk, max_tokens=summary_size, instruction_hint=instruction_hint) else: return content return result
[ "max_tokens", "\nShorten the following memory chunk of an autonomous agent from a first person perspective, using at most {max_tokens} tokens. {instruction_hint}:\ncontent:\n{content}\n---------\n", "content", "instruction_hint" ]
2024-01-10
abtawfik/thinkgpt
thinkgpt~memory.py
import re from typing import Dict, List, Union, Optional import numpy as np import langchain from langchain.docstore.document import Document from langchain import PromptTemplate, LLMChain from langchain.embeddings import OpenAIEmbeddings from langchain.llms import OpenAI, BaseLLM from thinkgpt.helper import get_n_tokens, fit_context EXECUTE_WITH_CONTEXT_PROMPT = PromptTemplate(template=""" Given a context information, reply to the provided request Context: {context} User request: {prompt} """, input_variables=["prompt", "context"], ) class ExecuteWithContextChain(LLMChain): """Prompts the LLM to execute a request with potential context""" def __init__(self, **kwargs): super().__init__(prompt=EXECUTE_WITH_CONTEXT_PROMPT, **kwargs) class MemoryMixin: memory: langchain.vectorstores mem_cnt: int embeddings_model: OpenAIEmbeddings def memorize(self, concept: Union[str, Document, List[Document]]): ''' Memorize some data by saving it to a vectorestore like Chroma ''' self.mem_cnt += 1 #--------------------------------------- # Create the documents to store #--------------------------------------- if isinstance(concept, str): docs = [Document(page_content=concept)] elif isinstance(concept, Document): docs = [concept] elif isinstance(concept, list): docs = concept[:] if any([not isinstance(con, Document) for con in concept]): raise ValueError('wrong type for List[Document]') else: raise ValueError('wrong type, must be either str, Document, List[Document]') #--------------------------------------- # Save memory into the database #--------------------------------------- self.memory.add_documents(docs) return None def remember(self, concept: str, limit: int = 5, max_tokens: Optional[int] = None) -> List[str]: #------------------------------------------------------ # Cannot remember if there is no stored memories #------------------------------------------------------ if len(self.memory) == 0: return [] #------------------------------------------------------ # Grab the most relevant memories # memory needs to be sorted in chronological order #------------------------------------------------------ docs = self.memory.similarity_search(concept, k=limit) text_results = [doc.page_content for doc in docs] if max_tokens: text_results = fit_context(text_results, max_tokens) return text_results
[ "\nGiven a context information, reply to the provided request\nContext: {context}\nUser request: {prompt}\n", "context" ]
2024-01-10
hassaank97/xflow-speech-transcription
home.py
from flask import Flask, render_template from flask import Flask, flash, redirect, render_template, \ request, url_for from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage from config import key import openai import os app = Flask(__name__) ls = [] UPLOAD_FOLDER = 'audio_files' #Add XFLOW Key here openai.api_key = key @app.route('/') def audio_file(): return render_template('audio.html') @app.route('/upload_audiofile', methods=['GET', 'POST']) def upload_audiofile(): if request.method == 'POST': try: file = request.files['file'] if file: filename = secure_filename(file.filename) print('filename: ', filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # print('hereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') audio_file1 = open('audio_files/' + filename, "rb") # prompt = ''' # Split the below paragraph based on full stop and then write sentiment and intent classification for every sentence. # First write the sentence then sentiment and intent classification at the end of every sentence. # Write in this format: Sentence: (Write the sentence), Sentiment: (Write the sentiment of the sentence) # and Intent : (Write the intent of the sentence) # ''' prompt = ''' Based on the below paragraph, Write sentence and after that write the sentiment and intent classification at the end of each sentence. Write in this format Sentence: Sentiment: and Intent: ''' transcript1 = openai.Audio.transcribe("whisper-1", audio_file1) prompt = prompt + '\n' + transcript1['text'] gptresponse = get_gpt3_response(prompt) print('gpt response: ', gptresponse) finaltext = 'Transcription: ' + '\n\n' + transcript1.text + '\n\n' + gptresponse print(finaltext) return render_template('audio.html', text=finaltext) except Exception as e: # Handle the exception print('An error occurred:', str(e)) return "Error occurred during file upload." return render_template('audio.html') def get_gpt3_response(prompt, response=""): content = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "assistant", "content": response}, {"role": "user", "content": prompt} ] ) return content['choices'][0]['message']['content'] if __name__ == '__main__': app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.run(debug=False, host='0.0.0.0')
[ "\n Based on the below paragraph, Write sentence and after that write the sentiment and intent classification\n at the end of each sentence. Write in this format Sentence: Sentiment: and Intent: \n ", "PLACEHOLDER\nPLACEHOLDER" ]
2024-01-10
ai-avant-garde-research/e2b
api-service~codegen~codegen.py
from typing import ( List, List, Any, Dict, ClassVar, ) from langchain.agents import AgentExecutor from pydantic import BaseModel, PrivateAttr from langchain.chat_models import ChatOpenAI from langchain.callbacks.base import ( AsyncCallbackManager, BaseCallbackManager, ) from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.tools import BaseTool from session.env import EnvVar from database import Database from codegen.agent import CodegenAgent, CodegenAgentExecutor from codegen.callbacks.logs import LogsCallbackHandler from codegen.prompt import ( SYSTEM_PREFIX, SYSTEM_SUFFIX, SYSTEM_FORMAT_INSTRUCTIONS, HUMAN_INSTRUCTIONS_PREFIX, HUMAN_INSTRUCTIONS_SUFFIX, ) # class OutputFinalCode(BaseTool): # name = "OutputFinalCode" # description = "This is the last tool you would use. You use it when you know the final server code and you want to output it. The input should be the final server code that does what the user required." # def _run(self, final_code: str) -> str: # return final_code # async def _arun(self, final_code: str) -> str: # raise NotImplementedError("OutputFinalCode does not support async") # testing_instructions = """Here are your instructions: # 1. Extract `email` from the incoming POST request. # 2. If there's no email, respond back with an error. # 3. Otherwise, respond back with the part of the email before the '@' sign. # 4. Generate the full required server code and make sure it starts without any errors. # 5. Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server. # 6. Once all works without any bugs and errors, write the code to the file. # 7. Deploy the code. # """ class Codegen(BaseModel): input_variables: ClassVar[List[str]] = ["input", "agent_scratchpad", "method"] _agent: CodegenAgent = PrivateAttr() _agent_executor: AgentExecutor = PrivateAttr() _tools: List[BaseTool] = PrivateAttr() _llm: ChatOpenAI = PrivateAttr() _database: Database = PrivateAttr() _callback_manager: BaseCallbackManager = PrivateAttr() def __init__( self, database: Database, callback_manager: BaseCallbackManager, tools: List[BaseTool], llm: ChatOpenAI, agent: CodegenAgent, **kwargs: Any, ) -> None: super().__init__(**kwargs) self._database = database self._callback_manager = callback_manager self._tools = tools self._llm = llm self._agent = agent self._agent_executor = CodegenAgentExecutor.from_agent_and_tools( agent=self._agent, tools=self._tools, verbose=True, callback_manager=self._callback_manager, ) def tool_names(self): return [tool.name for tool in self._tools] @classmethod def from_tools_and_database( cls, custom_tools: List[BaseTool], database: Database, ): callback_manager = AsyncCallbackManager( [ StreamingStdOutCallbackHandler(), ] ) # Assign custom callback manager to custom tools for tool in custom_tools: tool.callback_manager = callback_manager # Create the LLM llm = ChatOpenAI( model_name="gpt-4", streaming=True, temperature=0, max_tokens=2056, verbose=True, callback_manager=callback_manager, ) # Create CodegenAgent agent = CodegenAgent.from_llm_and_tools( llm=llm, tools=custom_tools, prefix=SYSTEM_PREFIX, suffix=SYSTEM_SUFFIX, format_instructions=SYSTEM_FORMAT_INSTRUCTIONS, input_variables=Codegen.input_variables, callback_manager=callback_manager, ) return cls( database=database, callback_manager=callback_manager, tools=custom_tools, llm=llm, agent=agent, ) async def generate( self, run_id: str, route: str, method: str, blocks: List[Dict], ): self._callback_manager.add_handler( LogsCallbackHandler( database=self._database, run_id=run_id, tool_names=self.tool_names() ) ) # Retrieve the description block. description_block: Dict[str, str] = next( b for b in blocks if b.get("type") == "Description" ) # Retrueve the block describing the incoming request payload. incoming_request_block: Dict[str, str] = next( b for b in blocks if b.get("type") == "RequestBody" ) # Retrieve the instructions block. instructions_block: Dict[str, str] = next( b for b in blocks if b.get("type") == "Instructions" ) input_vars = { "description": description_block["content"], "request_body": f"{{\n{incoming_request_block['content']}\n}}", "route": route, "method": method, } instructions = "Here are the instructions:" # inst_idx = 0 # Append the premade prefix instructions. for instruction in HUMAN_INSTRUCTIONS_PREFIX: # inst_idx += 1 values = [] # Extract the correct values from `input_vars` based on the keys. for k, v in input_vars.items(): if k in instruction["variables"]: values.append(v) # Use the values to format the instruction string. inst = instruction["content"].format(*values) # instructions = instructions + "\n" + f"{inst_idx}. {inst}" instructions = instructions + "\n" + f"- {inst}" # Append the use instructions instructions = ( instructions + "\nHere are the required implementation instructions:\n" + instructions_block["content"] ) print("Instructions:\n", instructions) ######## +++++ OLD # print("+++ BLOCKS") # print(blocks) # print("--- BLOCKS") # for block in blocks: # if block.get("type") == "Basic": # inst_idx += 1 # instructions = instructions + "\n" + f"{inst_idx}. " + block["prompt"] # # Append the premade suffix instructions. # for inst in HUMAN_INSTRUCTIONS_SUFFIX: # inst_idx += 1 # instructions = instructions + "\n" + f"{inst_idx}. {inst}" # # instructions += "\nThought: Here is the plan of how I will go about solving this based on the instructions I got:\n1." # # instructions += "\nThought:" # print("Instructions:\n", instructions) ######## ----- OLD print("Running executor...") await self._agent_executor.arun( agent_scratchpad="", # input=testing_instructions input=instructions, method=method, )
[]
2024-01-10
ai-avant-garde-research/e2b
api-service~codegen~callbacks~logs.py
from typing import Dict, Any, List, Union from langchain.callbacks.base import AsyncCallbackHandler from langchain.schema import AgentAction, AgentFinish, LLMResult from pydantic import PrivateAttr from codegen.callbacks.log_queue import LogQueue from codegen.agent.parsing import ToolLog, ThoughtLog from codegen.callbacks.log_parser import LogStreamParser from database import Database class LogsCallbackHandler(AsyncCallbackHandler): _database: Database = PrivateAttr() _run_id: str = PrivateAttr() _raw_logs: str = "" def __init__(self, database: Database, run_id: str, tool_names: List[str], **kwargs: Any): super().__init__(**kwargs) self._database = database self._run_id = run_id self._parser = LogStreamParser(tool_names=tool_names) self._log_queue = LogQueue() self._raw_log_queue = LogQueue(1.5) def __del__(self): self._log_queue.close() self._raw_log_queue.close() def _add_and_push_raw_logs(self, new_raw_log: str) -> None: self._raw_logs += new_raw_log self._raw_log_queue.add( self._database.push_raw_logs(self._run_id, self._raw_logs), ) def _push_logs(self, logs: list[ToolLog | ThoughtLog]) -> None: self._log_queue.add( self._database.push_logs(self._run_id, logs), ) async def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any ) -> None: """Run when LLM starts running.""" pass async def on_llm_new_token(self, token: str, **kwargs: Any) -> None: """Run on new LLM token. Only available when streaming is enabled.""" logs = self._parser.ingest_token(token).get_logs() self._push_logs(logs) self._add_and_push_raw_logs(token) async def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: """Run when LLM ends running.""" pass async def on_llm_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Run when LLM errors.""" pass async def on_chain_start( self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any ) -> None: """Run when chain starts running.""" pass async def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> None: """Run when chain ends running.""" pass async def on_chain_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Run when chain errors.""" pass async def on_tool_start( self, serialized: Dict[str, Any], input_str: str, **kwargs: Any ) -> None: """Run when tool starts running.""" print("Starting tool") self._add_and_push_raw_logs("Starting tool...") await self._log_queue.flush() await self._raw_log_queue.flush() async def on_tool_end(self, output: str, **kwargs: Any) -> None: """Run when tool ends running.""" print("Finished tool") logs = self._parser.ingest_tool_output(output).get_logs() self._push_logs(logs) self._add_and_push_raw_logs(f"\n{output}\n") async def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any: """Run on agent action.""" pass async def on_tool_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Run when tool errors.""" print("Tool error", error) self._add_and_push_raw_logs(f"Tool error:\n{error}\n") async def on_text(self, text: str, **kwargs: Any) -> None: """Run on arbitrary text.""" pass async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> None: """Run on agent end.""" pass
[]
2024-01-10
ai-avant-garde-research/e2b
api-service~codegen~tools~async_tool.py
from typing import Awaitable, Callable, Optional from langchain.agents import Tool from langchain.tools.base import BaseTool def func(_: str): raise NotImplementedError() def async_tool(name: str) -> Callable: def _make_tool(arun: Optional[Callable[[str], Awaitable[str]]]) -> BaseTool: if arun.__doc__ is None: raise NotImplementedError(f"Missing docstring for {name} async tool") description = f"{arun.__doc__.strip()}" tool_ = Tool( name=name, func=func, description=description, return_direct=False, ) tool_.coroutine = arun return tool_ return _make_tool
[]
2024-01-10
saic-fi/LFA
save_features.py
import argparse import os import numpy as np import openai import torch import torchvision import transformers from loguru import logger from mmengine.config import Config from mmselfsup.registry import MODELS from PIL import Image from tqdm import tqdm import clip from dataset import create_datasets from default_configs import get_cfg from train import create_dataloaders, create_model from utils import convert_weights_to_fp16, process_class_names, seed_everything N_AUGMENTATIONS = 5 # five-crop IMG_TEMPLATE = "a photo of a {}." VID_TEMPLATE = "a video frame of a person {}." IMAGENET_TEMPLATE = [ "a photo of a {}.", "itap of a {}.", "a bad photo of the {}.", "a origami {}.", "a photo of the large {}.", "a {} in a video game.", "art of the {}.", "a photo of the small {}.", ] CUSTOM_TEMPLATES = { "OxfordPets": "a photo of a {}, a type of pet.", "OxfordFlowers": "a photo of a {}, a type of flower.", "FGVCAircraft": "a photo of a {}, a type of aircraft.", "DescribableTextures": "{} texture.", "EuroSAT": "a centered satellite photo of {}.", "StanfordCars": "a photo of a {}.", "Food101": "a photo of {}, a type of food.", "SUN397": "a photo of a {}.", "Caltech101": "a photo of a {}.", "ImageUCF101": "a photo of a person doing {}.", "ImageNet": IMAGENET_TEMPLATE, "ImageNetSketch": IMAGENET_TEMPLATE, "ImageNetV2": IMAGENET_TEMPLATE, "ImageNetA": IMAGENET_TEMPLATE, "ImageNetR": IMAGENET_TEMPLATE, } def l2_norm(features: torch.Tensor) -> torch.Tensor: return features / features.norm(dim=-1, p=2, keepdim=True) def load_selfsup_model(model_type): self_sup_configs = { "mocov3": "config_files/mmselfsup/mocov3_resnet50_8xb512-amp-coslr-800e_in1k.py", "barlowtwins": "config_files/mmselfsup/barlowtwins_resnet50_8xb256-coslr-300e_in1k.py", "byol": "config_files/mmselfsup/byol_resnet50_16xb256-coslr-200e_in1k.py", } paths = { "mocov3": "mocov3_resnet50_8xb512-amp-coslr-800e_in1k_20220927-e043f51a.pth", "barlowtwins": "barlowtwins_resnet50_8xb256-coslr-300e_in1k_20220825-57307488.pth", "byol": "byol_resnet50_16xb256-coslr-200e_in1k_20220825-de817331.pth", } config = self_sup_configs[model_type] cfg = Config.fromfile(config) model = MODELS.build(cfg.model) path = paths[model_type] path = f"saved_models/{path}" logger.info(f"Loading model from {path}") checkpoint = torch.load(path, map_location="cpu") state_dict = checkpoint["state_dict"] mean = state_dict.pop("data_preprocessor.mean") std = state_dict.pop("data_preprocessor.std") model.load_state_dict(checkpoint["state_dict"], strict=True) return {"model": model.cuda().eval(), "std": std, "mean": mean} @torch.no_grad() def get_selfsup_visual_features(model, inputs): mean = model["mean"] / 255.0 std = model["std"] / 255.0 model = model["model"] inputs = torchvision.transforms.Normalize( mean=mean, std=std)(inputs).cuda() features = model([inputs])[0] features = torch.nn.AdaptiveAvgPool2d(1)(features).squeeze(-1).squeeze(-1) return features.float().cpu() def get_openai_embeddings(class_names, data_type): def request_emb(x): return openai.Embedding.create(input=[x], engine="text-embedding-ada-002")[ "data" ][0]["embedding"] openai.api_key = os.environ.get('OPENAI_API_KEY') class_names = [process_class_names(name) for name in class_names] if data_type == "image": class_names = [IMG_TEMPLATE.format(name) for name in class_names] elif data_type == "video": class_names = [ VID_TEMPLATE.format(name) for name in class_names] text_embeddings = [] for class_name in tqdm(class_names): while True: try: emb = request_emb(class_name) break except: pass text_embeddings.append(torch.tensor(emb)) text_embeddings = torch.stack(text_embeddings) return text_embeddings.float().cpu() @torch.no_grad() def create_align_model(): logger.info("Loading ALIGN model .....") processor = transformers.AlignProcessor.from_pretrained( "kakaobrain/align-base") model = transformers.AlignModel.from_pretrained("kakaobrain/align-base") return { "model": model.cuda().eval(), "processor": processor, } @torch.no_grad() def create_flava_model(): logger.info("Loading FLAVA model .....") model = transformers.FlavaModel.from_pretrained("facebook/flava-full") tokenizer = transformers.BertTokenizer.from_pretrained( "facebook/flava-full") return { "model": model.cuda().eval(), "tokenizer": tokenizer, } @torch.no_grad() def create_alt_clip_model(): logger.info("Loading AltCLIP model .....") model = transformers.AltCLIPModel.from_pretrained("BAAI/AltCLIP") processor = transformers.AltCLIPProcessor.from_pretrained("BAAI/AltCLIP") return { "model": model.cuda().eval(), "tokenizer": processor.tokenizer, } @torch.no_grad() def get_flava_visual_features(model, inputs): model = model["model"] img_embeddings = model.get_image_features(pixel_values=inputs.cuda()) if img_embeddings.ndim == 3: img_embeddings = img_embeddings[:, 0, :] img_embeddings = l2_norm(img_embeddings.cpu()) return img_embeddings @torch.no_grad() def get_flava_text_features(model, class_names, use_template=False, data_type="image", bz=100): class_names = [process_class_names(name) for name in class_names] if use_template: if data_type == "image": class_names = [IMG_TEMPLATE.format(name) for name in class_names] elif data_type == "video": class_names = [ VID_TEMPLATE.format(name) for name in class_names] else: raise ValueError tokenizer = model["tokenizer"] model = model["model"] all_text_embeddings = [] for i in range(0, len(class_names), bz): text_inputs = tokenizer( class_names[i: i + bz], padding="max_length", return_tensors="pt" ) text_inputs = {i: j.cuda() for i, j in text_inputs.items()} text_embeddings = model.get_text_features(**text_inputs) if text_embeddings.ndim == 3: text_embeddings = text_embeddings[:, 0, :] text_embeddings = l2_norm(text_embeddings.cpu()) all_text_embeddings.append(text_embeddings) return torch.cat(all_text_embeddings, dim=0) @torch.no_grad() def get_align_visual_features(model, inputs): processor = model["processor"] model = model["model"] dummy_image = Image.new("RGB", (224, 224)) dummy_inputs = processor( text=[" "], images=dummy_image, return_tensors="pt") dummy_inputs["pixel_values"] = inputs dummy_inputs = {i: j.cuda() for i, j in dummy_inputs.items()} outputs = model(**dummy_inputs) embeddings = l2_norm(outputs.image_embeds.cpu()) return embeddings @torch.no_grad() def get_align_text_features(model, class_names, use_template=False, data_type="image"): class_names = [process_class_names(name) for name in class_names] processor = model["processor"] model = model["model"] if use_template: if data_type == "image": class_names = [IMG_TEMPLATE.format(name) for name in class_names] elif data_type == "video": class_names = [ VID_TEMPLATE.format(name) for name in class_names] else: raise ValueError dummy_image = Image.new("RGB", (224, 224)) inputs = processor(text=class_names, images=dummy_image, return_tensors="pt") inputs = {i: j.cuda() for i, j in inputs.items()} outputs = model(**inputs) text_embeddings = l2_norm(outputs.text_embeds.cpu()) return text_embeddings @torch.no_grad() def create_clip_model(visual_backbone): logger.info("Loading CLIP model .....") clip_model, _ = clip.load(visual_backbone, device="cuda") clip_model.cuda().eval() input_resolution = clip_model.visual.input_resolution context_length = clip_model.context_length vocab_size = clip_model.vocab_size logger.info( "Model parameters:", f"{np.sum([int(np.prod(p.shape)) for p in clip_model.parameters()]):,}", ) logger.info(f"Input resolution: {input_resolution}") logger.info(f"Context length: {context_length}") logger.info(f"Vocab size: {vocab_size}") return clip_model @torch.no_grad() def create_clip_prompt_model(cfg, train_dataset, eval_dataset, model_checkpoint): if isinstance(eval_dataset, dict): # In case we have base & new test sets, use new class names eval_label_names = list(eval_dataset.values())[-1].label_names else: eval_label_names = eval_dataset.label_names clip_model = create_model(cfg, train_dataset.label_names, eval_label_names) clip_model.text_encoder.apply(convert_weights_to_fp16) clip_model.image_encoder.apply(convert_weights_to_fp16) clip_model.clip_dtype = torch.float16 logger.info(f"Loading checkpoint from {model_checkpoint}") clip_model.load_state_dict(torch.load(model_checkpoint), strict=True) clip_model.cuda().eval() return clip_model @torch.no_grad() def get_clip_text_features(model, class_names, use_template=False, data_type="image", dataset="ImageNet"): clip_weights = [] for classname in class_names: classname = classname.replace('_', ' ') if use_template: if data_type == "image": template = CUSTOM_TEMPLATES[dataset] template = template if isinstance( template, list) else [template] texts = [t.format(classname) for t in template] else: assert data_type == "video" texts = [VID_TEMPLATE.format(classname)] else: texts = [classname] texts = clip.tokenize(texts).cuda() class_embeddings = model.encode_text(texts) class_embeddings /= class_embeddings.norm(dim=-1, keepdim=True) class_embedding = class_embeddings.mean(dim=0) class_embedding /= class_embedding.norm() clip_weights.append(class_embedding.float().cpu()) clip_weights = torch.stack(clip_weights, dim=1) return clip_weights.T @torch.no_grad() def get_clip_prompt_text_features(model): input_embeddings = model.prompter() text_embeddings = model.encode_text(input_embeddings) text_embeddings = text_embeddings.float().cpu() return text_embeddings @torch.no_grad() def get_text_features( model_type, model, label_names, base_testing, use_template, data_type, dataset ): if model_type == "clip": return get_clip_text_features(model, label_names, use_template, data_type, dataset) elif model_type == "align": return get_align_text_features(model, label_names, use_template, data_type) elif model_type in ["flava", "alt_clip"]: return get_flava_text_features(model, label_names, use_template, data_type) elif model_type == "clip_prompt": if base_testing: model.prompter.train() else: model.prompter.eval() text_embeddings = get_clip_prompt_text_features(model) model.eval() return text_embeddings return get_openai_embeddings(label_names, data_type) @torch.no_grad() def get_clip_visual_features(model, inputs): inputs = inputs.cuda() if inputs.ndim == 5: B, C, T, H, W = inputs.shape inputs = inputs.permute(0, 2, 1, 3, 4).contiguous() inputs = inputs.reshape(-1, C, H, W) features = model.encode_image(inputs).float().cpu() features = features.reshape(B, T, -1).max(1)[0] return features features = model.encode_image(inputs).float().cpu() return features @torch.no_grad() def get_clip_prompt_visual_features(model, inputs): inputs = inputs.cuda() features = model.encode_image(inputs) features = features.float().cpu() return features def crop_duplication(inputs, n_crops): if isinstance(inputs, torch.Tensor): return inputs.view(-1, 1).repeat(1, n_crops).reshape(-1) assert isinstance(inputs, list) return [item for sublist in zip(*[inputs]*n_crops) for item in sublist] @torch.no_grad() def get_visual_features( model_type, model, dataloader, data_type, five_crop, loader_type ): features, filenames, labelnames, labels = [], [], [], [] for batch in tqdm(dataloader): input_tensor, batch_labels, batch_labelnames, batch_filenames = batch if five_crop and data_type == "video" and loader_type == "train": assert input_tensor.ndim == 6 B, crops, C, T, H, W = input_tensor.shape input_tensor = input_tensor.reshape(-1, C, T, H, W) batch_labels = crop_duplication(batch_labels, crops) batch_labelnames = crop_duplication(batch_labelnames, crops) batch_filenames = crop_duplication(batch_filenames, crops) elif five_crop and data_type == "image" and loader_type == "train": assert input_tensor.ndim == 5 B, crops, C, H, W = input_tensor.shape input_tensor = input_tensor.reshape(-1, C, H, W) batch_labels = crop_duplication(batch_labels, crops) batch_labelnames = crop_duplication(batch_labelnames, crops) batch_filenames = crop_duplication(batch_filenames, crops) if model_type == "clip": batch_features = get_clip_visual_features(model, input_tensor) elif model_type == "clip_prompt": batch_features = get_clip_prompt_visual_features( model, input_tensor) elif model_type == "align": batch_features = get_align_visual_features(model, input_tensor) elif model_type in ["flava", "alt_clip"]: batch_features = get_flava_visual_features(model, input_tensor) else: batch_features = get_selfsup_visual_features( model, input_tensor ) features.append(batch_features) labels.append(batch_labels) filenames.extend(batch_filenames) labelnames.extend(batch_labelnames) return ( torch.cat(features, dim=0), torch.cat(labels, dim=0), filenames, labelnames, ) def get_image_save_name(cfg, args): save_name = f"{args.model_type}" if "clip" in args.model_type: save_name += f"-{cfg.MODEL.VIZ_BACKBONE}" save_name += f"-{cfg.DATA.DATASET_NAME}" if cfg.DATA.N_SHOT >= 1: save_name += f"-{cfg.DATA.N_SHOT}nshot-seed{cfg.RNG_SEED}" if cfg.DATA.USE_BASE_AND_NEW: save_name += "-base-new" if cfg.DATA.TARGET_DATASET: save_name += f"-target-dataset-{cfg.DATA.TARGET_DATASET}" if args.five_crop: save_name += "-5crop" if args.use_template and "clip" in args.model_type: save_name += "-with-template" save_name = save_name.replace("/", "") return save_name def get_video_save_name(cfg, args): save_name = f"{args.model_type}-{cfg.DATA.DATASET_NAME}" if "clip" in args.model_type: save_name += f"{cfg.MODEL.VIZ_BACKBONE}" save_name += f"-{cfg.DATA.DATASET_NAME}" if cfg.DATA.FEWSHOT and not cfg.DATA.USE_ALL_CLASSES: save_name += f"-{cfg.DATA.N_SHOT}shot-{cfg.C_WAY}way-seed{cfg.RNG_SEED}" elif cfg.DATA.FEWSHOT: save_name += f"-{cfg.DATA.N_SHOT}nshot-all-way-seed{cfg.RNG_SEED}" elif cfg.DATA.ZEROSHOT: save_name += "-zeroshot" save_name += f"-train-{cfg.DATA.NUM_FRAMES}x{cfg.DATA.TRAIN_STRIDES[0]}" save_name += f"-test-{cfg.DATA.NUM_FRAMES}x{cfg.DATA.TEST_STRIDES[0]}" if args.five_crop: save_name += "-5crop" if args.use_template and "clip" in args.model_type: save_name += "-with-template" save_name = save_name.replace("/", "") return save_name def get_save_name(cfg, args): if cfg.DATA.TYPE == "image": return get_image_save_name(cfg, args) return get_video_save_name(cfg, args) def get_config(args): cfg = get_cfg(args) if args.five_crop: cfg.DATALOADER.TRAIN_BATCHSIZE = cfg.DATALOADER.TRAIN_BATCHSIZE // N_AUGMENTATIONS if cfg.DATA.TYPE == "video": assert len(cfg.DATA.TRAIN_STRIDES) == 1 assert len(cfg.DATA.TEST_STRIDES) == 1 cfg.DATA.TRAIN_VIDEO_SAMPLER = "center" cfg.DATA.TEST_METHOD = "single_view" if args.five_crop: cfg.DATA.TRAIN_AUGS = ["resize", "five_crop", "normalize"] cfg.DATA.TRAIN_RESIZE = 224 else: cfg.DATA.TRAIN_AUGS = ["resize", "center_crop", "normalize"] cfg.DATA.TRAIN_RESIZE = 224 if args.model_type in ["mocov3", "barlowtwins", "byol"]: # remove normalize cfg.DATA.TRAIN_AUGS = cfg.DATA.TRAIN_AUGS[:-1] cfg.DATA.TEST_AUGS = cfg.DATA.TEST_AUGS[:-1] if args.model_type == "align": cfg.DATA.TRAIN_RESIZE = 289 cfg.DATA.TEST_RESIZE = 289 cfg.DATA.TRAIN_CROP_SIZE = 289 cfg.DATA.TEST_CROP_SIZE = 289 cfg.DATA.MEAN = [0.5, 0.5, 0.5] cfg.DATA.STD = [0.5, 0.5, 0.5] return cfg def save_features(cfg, args): seed_everything(cfg.RNG_SEED) train_dataset, eval_dataset = create_datasets(cfg) train_dataloader, eval_dataloader = create_dataloaders( cfg, train_dataset, eval_dataset ) save_name = get_save_name(cfg, args) save_path = os.path.join(args.save_path, save_name) logger.info(f"Saving to {save_path}") os.makedirs(save_path, exist_ok=True) # create model logger.info("Creating model...") if args.model_type == "clip": model = create_clip_model(cfg.MODEL.VIZ_BACKBONE) elif args.model_type == "clip_prompt": model = create_clip_prompt_model( cfg, train_dataset, eval_dataset, args.model_chekpoint ) elif args.model_type == "align": model = create_align_model() elif args.model_type == "flava": model = create_flava_model() elif args.model_type == "alt_clip": model = create_alt_clip_model() else: model = load_selfsup_model(args.model_type) # eval_dataloader can be [test] only or [test_base, test_new] loaders_types = ["train"] + list(eval_dataloader.keys()) loaders = [train_dataloader] + list(eval_dataloader.values()) for loader_type, loader in zip(loaders_types, loaders): logger.info(f"Saving {loader_type} features...") dataset_labelnames = loader.dataset.label_names base_testing = all( x == y for x, y in zip( loader.dataset.label_names, train_dataloader.dataset.label_names ) ) logger.info("-> text features...") text_features = get_text_features( model_type=args.model_type, model=model, label_names=dataset_labelnames, base_testing=base_testing, use_template=args.use_template, data_type=cfg.DATA.TYPE, dataset=cfg.DATA.DATASET_NAME ) logger.info("-> visual features...") visual_features, labels, filenames, labelnames = get_visual_features( model_type=args.model_type, model=model, dataloader=loader, data_type=cfg.DATA.TYPE, five_crop=args.five_crop, loader_type=loader_type ) to_save = [ text_features, visual_features, labels, filenames, labelnames, dataset_labelnames, ] np.savez(f"{save_path}/{loader_type}_features.npz", *to_save) if __name__ == "__main__": logger.info("Saving features...") parser = argparse.ArgumentParser() parser.add_argument("--config", dest="config_file", type=str, default=None) parser.add_argument("opts", default=None, nargs=argparse.REMAINDER) parser.add_argument("--save-path", type=str, required=True) parser.add_argument("--model-chekpoint", type=str, default=None) parser.add_argument( "--model-type", type=str, choices=["clip", "clip_prompt", "mocov3", "barlowtwins", "byol", "align", "flava", "alt_clip"], required=True, ) parser.add_argument("--five-crop", action="store_true") parser.add_argument("--use-template", action="store_true") arguments = parser.parse_args() configs = get_config(arguments) save_features(configs, arguments)
[ "{'OxfordPets': 'a photo of a {}, a type of pet.', 'OxfordFlowers': 'a photo of a {}, a type of flower.', 'FGVCAircraft': 'a photo of a {}, a type of aircraft.', 'DescribableTextures': '{} texture.', 'EuroSAT': 'a centered satellite photo of {}.', 'StanfordCars': 'a photo of a {}.', 'Food101': 'a photo of {}, a type of food.', 'SUN397': 'a photo of a {}.', 'Caltech101': 'a photo of a {}.', 'ImageUCF101': 'a photo of a person doing {}.', 'ImageNet': PLACEHOLDER, 'ImageNetSketch': PLACEHOLDER, 'ImageNetV2': PLACEHOLDER, 'ImageNetA': PLACEHOLDER, 'ImageNetR': PLACEHOLDER}", "['a photo of a {}.', 'itap of a {}.', 'a bad photo of the {}.', 'a origami {}.', 'a photo of the large {}.', 'a {} in a video game.', 'art of the {}.', 'a photo of the small {}.']", "a photo of a {}.", "a video frame of a person {}." ]
2024-01-10
AlexisTM/AMGATA
amaga~article_generator.py
import os from re import sub import requests import openai from prompts import positive_prompt, negative_prompt class ArticleGenerator: def __init__(self): self.api_key = None self.model_choices = [] def raise_wrong_model(self): raise Exception( f"Model not supported, please use one of the following: {self.model_choices}" ) def generate_completion(self, *args, **kwargs): raise NotImplementedError( f"generate_completion is not implemented for the class {self.__class__.__name__}" ) def generate_title(self, model, subject): # TODO raise NotImplementedError() def generate_abstract(self, model, subject): # TODO raise NotImplementedError() def generate_conclusion(self, model, subject): # TODO raise NotImplementedError() def generate_article(self, model, subject, refute): # TODO: For a complete article, generate multiple "texts" with multiple queries: "title", "abstract", "core" and "conclusion" # title = self.generate_title(model, subject) # abstract = self.generate_abstract(model, subject)8 prompt = "" if refute: prompt = negative_prompt.format(subject=subject) else: prompt = positive_prompt.format(subject=subject) params = { "num_results": 1, "max_tokens": 450, "stopSequences": ["#####", "Subject: "], "temperature": 0.8, "topKReturn": 2, # topP = 1.0 "frequency_penalty": 0.3, "presence_penalty": 0.3, } core = self.generate_completion(model, prompt, params) return "\n\n".join([core]) class ArticleGeneratorOpenAI(ArticleGenerator): def __init__(self): self.api_key = os.environ.get("OPENAI_API_KEY") openai.api_key = self.api_key self.model_choices = [e["id"] for e in self.list_engines()] def list_engines(self): return openai.Engine.list()["data"] def generate_completion(self, model, prompt, params): if not model in self.model_choices: self.raise_wrong_model() if params.get("stopSequences", None) == []: params["stopSequences"] = None res = openai.Completion.create( engine=model, prompt=prompt, temperature=params.get("temperature", 0.8), max_tokens=params.get("maxTokens", 200), # top_p=params.get("topP", 1.0), frequency_penalty=params.get("frequency_penalty", 0.3), presence_penalty=params.get("presence_penalty", 0.3), stop=params.get("stopSequences", None), ) if res != 0: for choice in res.choices: return choice.text.strip() else: raise Exception("No response found") class ArticleGeneratorAI21(ArticleGenerator): def __init__(self): self.api_key = os.environ.get("AI21_API_KEY") self.model_choices = ["j1-large", "j1-jumbo"] def get_api_url(self, model): return f"https://api.ai21.com/studio/v1/{model}/complete" def generate_completion(self, model, prompt, params): if not model in self.model_choices: self.raise_wrong_model() if params.get("stopSequences", None) is None: params["stopSequences"] = [] res = requests.post( self.get_api_url(model), headers={"Authorization": f"Bearer {self.api_key}"}, json={ "prompt": prompt, "numResults": params.get("numResults", 1), "maxTokens": params.get("maxTokens", 200), "stopSequences": params.get("stopSequences", None), "topKReturn": params.get("topKReturn", 2), "temperature": params.get("temperature", 0.8), # "topP": params.get("topP", 1.0), }, ) if res.status_code != 200: raise Exception( f"Failed request on {self.get_api_url(model)}. {res.json()}" ) completions = [c["data"]["text"] for c in res.json()["completions"]] return "\n".join(completions).strip("\n").strip()
[]
2024-01-10
katonnbk/eLearning
brin.py
# -*- coding: utf-8 -*- """ Created on Wed Mar 28 11:25:43 2019 @author: Taufik Sutanto [email protected] https://tau-data.id ~~Perjanjian Penggunaan Materi & Codes (PPMC) - License:~~ * Modul Python dan gambar-gambar (images) yang digunakan adalah milik dari berbagai sumber sebagaimana yang telah dicantumkan dalam masing-masing license modul, caption atau watermark. * Materi & Codes diluar point (1) (i.e. "taudata.py" ini & semua slide ".ipynb)) yang digunakan di pelatihan ini dapat digunakan untuk keperluan akademis dan kegiatan non-komersil lainnya. * Untuk keperluan diluar point (2), maka dibutuhkan izin tertulis dari Taufik Edy Sutanto (selanjutnya disebut sebagai pengarang). * Materi & Codes tidak boleh dipublikasikan tanpa izin dari pengarang. * Materi & codes diberikan "as-is", tanpa warranty. Pengarang tidak bertanggung jawab atas penggunaannya diluar kegiatan resmi yang dilaksanakan pengarang. * Dengan menggunakan materi dan codes ini berarti pengguna telah menyetujui PPMC ini. """ import warnings; warnings.simplefilter('ignore') import tweepy, googlemaps, re, itertools, os, time from html import unescape from tqdm import tqdm from unidecode import unidecode from nltk import sent_tokenize from textblob import TextBlob from Sastrawi.Stemmer.StemmerFactory import StemmerFactory from nltk.tag import CRFTagger import spacy import numpy as np, pandas as pd import requests from sklearn.feature_extraction.text import CountVectorizer from sklearn.decomposition import LatentDirichletAllocation as LDA import matplotlib.pyplot as plt from urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) from requests.packages.urllib3.exceptions import InsecureRequestWarning import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) from gensim.models.ldamodel import LdaModel from gensim.models.coherencemodel import CoherenceModel import networkx as nx, operator from dateutil import parser from datetime import datetime import pickle from gensim.models import Phrases from gensim.corpora.dictionary import Dictionary from math import radians, cos, sin, asin, sqrt from wordcloud import WordCloud HTfilters = set(['zz', 'architec', 'prize', 'stirli', 'architect', 'london', 'cpd', 'design', 'stirling', 'photogr', 'gemini', 'mule', 'karaoke', 'playing', 'official', 'berita', 'follow', 'retweet', 'mufc', 'ntms', 'infolimit', 'eeaa', 'eaa', 'cfc', 'caprico', 'breaking','news', 'libra', 'mereka', 'brankas', 'psikolog', 'aquarius', 'klc']) file = 'data/provinsi-latlon-radius.csv' prov = pd.read_csv(file) Ck = 'WQWIh4BC4dVio7xiV4NrU9Z29' # consumer_key Cs = 'yhxEHCiJCsjnv4joJakVG25Nqm9EJ8ec1pLqHqpneAtqYdjgLL' # consumer_secret At = '2214118411-i0MsjqZqjZ6uPfUplghxXcJsXdNYhRsCO82AnPW' # access_token As = 'hxjsnKSY8dgv4Cl5gQd6M6Oax27U7xVoZrHnvSvRdBlCx' # access_secret tKeys = (Ck, Cs, At, As) qry = 'banjir OR gempa OR longsor OR tsunami' N=300 lan='id' def getData(qry, N=300, prov=None, lan='id', tKeys=None): #MaxIter = int(N/100) user_ = {"created_at":[], "screen_name": [], "name":[], "followers_count":[], "friends_count":[], "description":[], "id_str":[], "location":[], "lat":[], "lon":[], "protected":[], "statuses_count":[], "profile_image_url_https":[], "verified":[], "gender":[], "age":[]} tweet_ = {"created_at":[], "screen_name":[], "tweet":[], "retweet_count":[], "favorite_count":[], "location":[], "lat":[], "lon":[]} print("Mengambil sampel data dari:") mulai = time.time() T = [] for i, p in prov.iterrows(): propinsi = p['propinsi'] print(propinsi, end=', ') Geo = ','.join([str(p.lat), str(p.lon), str(p.radius)+'km']) api = connect(con="twitter", key=tKeys, verbose=False) try: T2 = api.search_tweets(q=qry, geocode=Geo, lang=lan, count=N, tweet_mode = 'extended') if T2: T.extend([t._json for t in T2]) except Exception as err_: print("error in Function getData: \n", err_) #break for t_ in T:#tweepy.Cursor(api.search_tweets, q=qry, geocode=Geo, lang='id', tweet_mode='extended').items(N): wkt_ = parser.parse(t_['user']['created_at']) wkt_ = datetime.strftime(wkt_, '%Y-%m-%d %H:%M:%S') user_['created_at'].append(wkt_) user_['screen_name'].append(t_['user']['screen_name']) user_['name'].append(t_['user']['name']) user_['followers_count'].append(t_['user']['followers_count']) user_['friends_count'].append(t_['user']['friends_count']) user_['description'].append(t_['user']['description']) user_['id_str'].append(t_['user']['id_str']) if t_['user']['location']: user_['location'].append(t_['user']['location']) else: user_['location'].append(propinsi) user_['lat'].append(p.lat) user_['lon'].append(p.lon) user_['protected'].append(t_['user']['protected']) user_['statuses_count'].append(t_['user']['statuses_count']) user_['profile_image_url_https'].append(t_['user']['profile_image_url_https']) user_['verified'].append(t_['user']['verified']) user_['gender'].append('') user_['age'].append(0) wkt_ = parser.parse(t_['created_at']) wkt_ = datetime.strftime(wkt_, '%Y-%m-%d %H:%M:%S') tweet_['created_at'].append(wkt_) tweet_['screen_name'].append(t_['user']['screen_name']) tweet_['tweet'].append(t_['full_text']) tweet_['retweet_count'].append(t_['retweet_count']) tweet_['favorite_count'].append(t_['favorite_count']) tweet_['location'].append(propinsi) tweet_['lat'].append(p.lat) tweet_['lon'].append(p.lon) waktu = time.time() - mulai print('\n\n Finished Collecting {} samples of data about "{}" from all provinces in Indonesia in {} minutes'.format(len(tweet_['tweet']), qry, int(waktu/60))) tweet_ = pd.DataFrame(tweet_) tweet_.drop_duplicates(subset=["screen_name", "tweet"], keep="first", inplace=True) tweet_.sort_values(by=['retweet_count', 'favorite_count'], ascending=False, inplace=True) user_ = pd.DataFrame(user_) user_.drop_duplicates(subset=["screen_name"], keep="first", inplace=True) user_.sort_values(by=['followers_count'], ascending=False, inplace=True) return tweet_, user_ getHashTags = re.compile(r"#(\w+)") def hashTags(df, N=30): HTfilters = set(['zz', 'architec', 'prize', 'stirli', 'architect', 'london', 'cpd', 'design', 'stirling', 'photogr', 'gemini', 'mule', 'karaoke', 'playing', 'official', 'berita', 'follow', 'retweet', 'mufc', 'ntms', 'infolimit', 'eeaa', 'eaa', 'cfc', 'caprico', 'breaking','news', 'libra', 'mereka', 'brankas', 'psikolog', 'aquarius', 'klc']) HT = {'hashtags':[]} count = 0 for i, d in tqdm(df.iterrows()): hashtags = re.findall(getHashTags, d.tweet) if hashtags: TG = [] for tag in hashtags: dTag = str(tag).strip().lower() if len(dTag)>2: add = True for f in HTfilters: if f in dTag: add=False; break if add: TG.append('#'+dTag); count += 1 HT['hashtags'].append(TG) dtHT = [x for t in tqdm(HT['hashtags']) for x in t] # any(h not in x for h in HTfilters) dtHT = pd.Series(dtHT) dtHT = dtHT.value_counts() dtHT = dtHT.sort_index() dtHT = dtHT.sort_values(ascending = False) dtHT.to_csv('data/hashTags_Energy_Satrio.csv', encoding='utf8') dtHT = dtHT.iloc[:N] topHT = [t.lower().strip().replace('#','') for t in dtHT.index] print('Plot "{}" HashTag terbanyak'.format(N)) _ = dtHT.plot(kind='barh', figsize=(12,8), legend = False) return topHT def heatmap(df): lat, lon, hashTags, tweets = [], [], [], [] for i, r in tqdm(df.iterrows()): Lat, Lon = float(r.lat), float(r.lon) lat.append(Lat); lon.append(Lon) H = re.findall(getHashTags, r.tweet) TG = [] if H: for tag in H: dTag = str(tag).strip().lower() if len(dTag)>2: add = True for fil in HTfilters: if fil in dTag: add=False; break if add: TG.append('#'+dTag) hashTags.append(TG) else: hashTags.append(TG) tweets.append(r.tweet) count = [1]*len(lat) df_loc = pd.DataFrame({'lat':lat, 'lon':lon, 'count':count, 'hashTags':hashTags, 'tweet':tweets}) return df_loc def haversine(lat1=0.0, lon1=0.0, lat2=0.0, lon2=0.0): """ Calculate the great circle distance between two points on the earth (specified in decimal degrees) """ # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2]) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2 c = 2 * asin(sqrt(a)) r = 6371 # Radius of earth in kilometers. Use 3956 for miles return c * r def tagsMap(df_loc): prov = pd.read_csv("data/provinsi-latlon.csv") ht_pos = {p:(0.0, 0.0, '') for p in prov.propinsi.to_list()} ht_tweets = {} for i, d in tqdm(df_loc.iterrows()): min_dis = ('', float('Inf'), 0.0, 0.0) for j, p in prov.iterrows(): jrk = haversine(lat1=d.lat, lon1=d.lon, lat2=p.lat, lon2=p.lon) if jrk < min_dis[1]: min_dis = (p['propinsi'], jrk, p.lat, p.lon) ht_pos[min_dis[0]] = (min_dis[2], min_dis[3], ht_pos[min_dis[0]][2] + ' ' + ' '.join(d.hashTags)) if '#' in d.tweet: try: ht_tweets[min_dis[0]].append(d.tweet) except: ht_tweets[min_dis[0]]= [d.tweet] for propinsi, dt in tqdm(ht_pos.items()): try: txt = dt[2] wc = WordCloud(max_font_size=75, min_font_size=16, max_words=3, background_color="rgba(0, 0, 255, 0)", color_func=lambda *args, **kwargs: (0,0,0), mode="RGBA").generate(txt) p = wc.to_file('data/clouds/{}.png'.format(propinsi)) except: pass return ht_pos def LoadDocuments(dPath=None,types=None, file = None): # types = ['pdf','doc','docx','txt','bz2'] Files, Docs = [], [] if types: for tipe in types: Files += crawlFiles(dPath,tipe) if file: Files = [file] if not types and not file: # get all files regardless of their extensions Files += crawlFiles(dPath) for f in Files: if f[-3:].lower() in ['txt', 'dic','py', 'ipynb']: try: df=open(f,"r",encoding="utf-8", errors='replace') Docs.append(df.readlines());df.close() except: print('error reading{0}'.format(f)) else: print('Unsupported format {0}'.format(f)) if file: Docs = Docs[0] return Docs, Files def LoadStopWords(lang='en'): L = lang.lower().strip() if L == 'en' or L == 'english' or L == 'inggris': from spacy.lang.en import English as lemmatizer #lemmatizer = spacy.lang.en.English lemmatizer = lemmatizer() #lemmatizer = spacy.load('en') stops = set([t.strip() for t in LoadDocuments(file = 'data/stopwords_en.txt')[0]]) elif L == 'id' or L == 'indonesia' or L=='indonesian': from spacy.lang.id import Indonesian #lemmatizer = spacy.lang.id.Indonesian lemmatizer = Indonesian() stops = set([t.strip() for t in LoadDocuments(file = 'data/stopwords_id.txt')[0]]) else: print('Warning, language not recognized. Empty StopWords Given') stops = set(); lemmatizer = None return stops, lemmatizer def loadCorpus(file='', sep=':', dictionary = True): file = open(file, 'r', encoding="utf-8", errors='replace') F = file.readlines() file.close() if dictionary: fix = {} for f in F: k, v = f.split(sep) k, v = k.strip(), v.strip() fix[k] = v else: fix = set( (w.strip() for w in F) ) return fix slangFixId = loadCorpus(file = 'data/slang.dic', sep=':') stopId, _ = LoadStopWords(lang='id') stopId.add("rt") def cleanTweet(data): cleanTweet = [] for i, d in tqdm(data.iterrows()): cleanTweet.append(cleanText(d["tweet"], fix=slangFixId, lan='id', stops = stopId)) return cleanTweet def getTopic(df, Top_Words=30, resume_ = True, k=0): data_ta = df #df['clean'].values data = [t.split() for t in data_ta] if k==0: bigram_t = Phrases(data, min_count=2) trigram_t = Phrases(bigram_t[data]) for idx, d in enumerate(data): for token in bigram_t[d]: if '_' in token:# Token is a bigram, add to document. data[idx].append(token) for token in trigram_t[d]: if '_' in token:# Token is a bigram, add to document. data[idx].append(token) dictionary_t = Dictionary(data) dictionary_t.filter_extremes(no_below=5, no_above=0.90) corpus_t = [dictionary_t.doc2bow(doc) for doc in data] start, step, limit = 2, 1, 5 # Ganti dengan berapa banyak Topic yang ingin di hitung/explore coh_t, kCV = [], 3 # hati-hati sangat lambat karena cross validation pada metode yang memang tidak efisien (LDA) for i in tqdm(range(kCV)): if resume_: try: f = open('data/kCV_{}.pckl'.format(i), 'rb') c = pickle.load(f); f.close() coh_t.append(c) except: model_list, c = compute_coherence_values(dictionary=dictionary_t, corpus=corpus_t, texts=data, start=start, limit=limit, step=step) f = open('data/kCV_{}.pckl'.format(i), 'wb') pickle.dump(c, f); f.close() coh_t.append(c) else: model_list, c = compute_coherence_values(dictionary=dictionary_t, corpus=corpus_t, texts=data, start=start, limit=limit, step=step) f = open('data/kCV_{}.pckl'.format(i), 'wb') pickle.dump(c, f); f.close() coh_t.append(c) ct = np.mean(np.array(coh_t), axis=0).tolist() k = ct.index(max(ct))+start tf_w, tm_w, vec_w = getTopics(data_ta, n_topics=k, Top_Words=30) return tf_w, tm_w, vec_w, ct ct = CRFTagger() # Language Model fTagger = 'data/all_indo_man_tag_corpus_model.crf.tagger' ct.set_model_file(fTagger) nlp_en = spacy.load("en_core_web_sm") lemma_id = StemmerFactory().create_stemmer() def connect(con="twitter", key=None, verbose=True): if con.lower().strip() == "twitter": Ck, Cs, At, As = key try: auth = tweepy.auth.OAuthHandler(Ck, Cs) auth.set_access_token(At, As) api = tweepy.API(auth, wait_on_rate_limit=True, timeout=180, retry_count=5, retry_delay=3) if verbose: usr_ = api.verify_credentials() print('Welcome "{}" you are now connected to twitter server'.format(usr_.name)) return api except: print("Connection failed, please check your API keys or connection") def crawlTwitter(api, qry, N = 30, lan='id', loc=None): T = [] if loc: print('Crawling keyword "{}" from "{}"'.format(qry, loc)) for tweet in tqdm(tweepy.Cursor(api.search_tweets, lang=lan, q=qry, count=100, tweet_mode='extended', geocode=loc).items(N)): T.append(tweet._json) else: print('Crawling keyword "{}"'.format(qry)) for tweet in tqdm(tweepy.Cursor(api.search_tweets, q=qry, lang=lan, count=100, tweet_mode='extended').items(N)): T.append(tweet._json) print("Collected {} tweets".format(len(T))) return T def getLatLon(gKey, location, lan='id'): gmaps = googlemaps.Client(key=gKey) try: res = gmaps.geocode(location, language=lan)[0] except Exception as err_: print(err_) return None, None, None if res: lat, lon = res['geometry']['location']['lat'], res['geometry']['location']['lng'] addresses = res['address_components'] alamat = [a['long_name'] for a in addresses] return lat, lon, alamat def adaAngka(s): return any(i.isdigit() for i in s) def fixTags(t): getHashtags = re.compile(r"#(\w+)") pisahtags = re.compile(r'[A-Z][^A-Z]*') tagS = re.findall(getHashtags, t) for tag in tagS: if len(tag)>0: tg = tag[0].upper()+tag[1:] proper_words = [] if adaAngka(tg): tag2 = re.split('(\d+)',tg) tag2 = [w for w in tag2 if len(w)>0] for w in tag2: try: _ = int(w) # error if w not a number proper_words.append(w) except: w = w[0].upper()+w[1:] proper_words = proper_words+re.findall(pisahtags, w) else: proper_words = re.findall(pisahtags, tg) proper_words = ' '.join(proper_words) t = t.replace('#'+tag, proper_words) return t def loadCorpus(file='', sep=':', dictionary = True): file = open(file, 'r', encoding="utf-8", errors='replace') F = file.readlines() file.close() if dictionary: fix = {} for f in F: k, v = f.split(sep) k, v = k.strip(), v.strip() fix[k] = v else: fix = set( (w.strip() for w in F) ) return fix def crawlFiles(dPath,types=None): # dPath ='C:/Temp/', types = 'pdf' if types: return [dPath+f for f in os.listdir(dPath) if f.endswith('.'+types)] else: return [dPath+f for f in os.listdir(dPath)] def LoadStopWords(lang='en'): L = lang.lower().strip() if L == 'en' or L == 'english' or L == 'inggris': from spacy.lang.en import English as lemmatizer #lemmatizer = spacy.lang.en.English lemmatizer = lemmatizer() #lemmatizer = spacy.load('en') stops = set([t.strip() for t in LoadDocuments(file = 'data/stopwords_en.txt')[0]]) elif L == 'id' or L == 'indonesia' or L=='indonesian': from spacy.lang.id import Indonesian #lemmatizer = spacy.lang.id.Indonesian lemmatizer = Indonesian() stops = set([t.strip() for t in LoadDocuments(file = 'data/stopwords_id.txt')[0]]) else: print('Warning, language not recognized. Empty StopWords Given') stops = set(); lemmatizer = None return stops, lemmatizer def cleanText(T, fix={}, onlyChar=True, lemma=False, lan='id', stops = set(), symbols_remove = True, min_charLen = 2, max_charLen = 15, fixTag= True): pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') t = re.sub(pattern,' ',T) #remove urls if any pattern = re.compile(r'ftp[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') t = re.sub(pattern,' ',t) #remove urls if any t = unescape(t) # html entities fix if fixTag: t = fixTags(t) # fix abcDef t = t.lower().strip() # lowercase t = unidecode(t) t = ''.join(''.join(s)[:2] for _, s in itertools.groupby(t)) # remove repetition t = t.replace('\n', ' ').replace('\r', ' ') t = sent_tokenize(t) # sentence segmentation. String to list for i, K in enumerate(t): if symbols_remove: listKata = re.sub(r'[^.,_a-zA-Z0-9 -\.]',' ',K) listKata = TextBlob(listKata).words if fix: for j, token in enumerate(listKata): if str(token) in fix.keys(): listKata[j] = fix[str(token)] if onlyChar: listKata = [tok for tok in listKata if sum([1 for d in tok if d.isdigit()])==0] if stops: listKata = [tok for tok in listKata if str(tok) not in stops and len(str(tok))>=min_charLen] else: listKata = [tok for tok in listKata if len(str(tok))>=min_charLen] if lemma and lan.lower().strip()=='id': t[i] = lemma_id.stem(' '.join(listKata)) elif lemma and lan.lower().strip()=='en': listKata = [str(tok.lemma_) for tok in nlp_en(' '.join(listKata))] t[i] = ' '.join(listKata) else: t[i] = ' '.join(listKata) return ' '.join(t) # Return kalimat lagi def NLPfilter(t, filters): # filters = set(['NN', 'NNP', 'NNS', 'NNPS', 'JJ']) #tokens = TextBlob(t).words#nlp_id(t) tokens = [str(k) for k in TextBlob(t).words if len(k)>2] hasil = ct.tag_sents([tokens]) return [k[0] for k in hasil[0] if k[1] in filters] def compute_coherence_values(dictionary, corpus, texts, limit, coherence='c_v', start=2, step=3): coherence_values = [] model_list = [] for num_topics in range(start, limit, step): model=LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics) model_list.append(model) coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence=coherence) coherence_values.append(coherencemodel.get_coherence()) return model_list, coherence_values def print_Topics(model, feature_names, Top_Topics, n_top_words): for topic_idx, topic in enumerate(model.components_[:Top_Topics]): print("Topic #%d:" %(topic_idx+1)) print(" ".join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]])) def getTopics(Txt,n_topics=5, Top_Words=7): tf_vectorizer = CountVectorizer(strip_accents = 'unicode', token_pattern = r'\b[a-zA-Z]{3,}\b', max_df = 0.95, min_df = 2) dtm_tf = tf_vectorizer.fit_transform(Txt) tf_terms = tf_vectorizer.get_feature_names() lda_tf = LDA(n_components=n_topics, learning_method='online', random_state=0).fit(dtm_tf) vsm_topics = lda_tf.transform(dtm_tf) doc_topic = [a.argmax()+1 for a in vsm_topics] # topic of docs print('In total there are {0} major topics, distributed as follows'.format(len(set(doc_topic)))) fig4 = plt.figure(); fig4.add_subplot(111) plt.hist(np.array(doc_topic), alpha=0.5); plt.show() print('Printing top {0} Topics, with top {1} Words:'.format(n_topics, Top_Words)) print_Topics(lda_tf, tf_terms, n_topics, Top_Words) return lda_tf, dtm_tf, tf_vectorizer def safeVectorizer(D, type_='tf', maxDf=0.95, minDf=2, ngram_=(1, 2)): vectorizer = CountVectorizer(binary = False, lowercase=True, max_df=maxDf, min_df=minDf) while True: X = vectorizer.fit_transform(D) if X[X.getnnz(1)>0].shape[0]==X.shape[0]: break else: newD = [] nBaris, nKolom = X.shape for i,d in enumerate(D): if sum(X[i].data)!=0: newD.append(d) D = newD return X, vectorizer.get_feature_names() def drawGraph(G, Label, layOut='spring'): fig3 = plt.figure(); fig3.add_subplot(111) if layOut.lower()=='spring': pos = nx.spring_layout(G) elif layOut.lower()=='circular': pos=nx.circular_layout(G) elif layOut.lower()=='random': pos = nx.random_layout(G) elif layOut.lower()=='shells': shells = [G.core_nodes,sorted(G.major_building_routers, key=lambda n: nx.degree(G.topo, n)) + G.distribution_routers + G.server_nodes,G.hosts + G.minor_building_routers] pos = nx.shell_layout(G, shells) elif layOut.lower()=='spectral': pos=nx.spectral_layout(G) else: print('Graph Type is not available.') return nx.draw_networkx_nodes(G,pos, alpha=0.2,node_color='blue',node_size=600) if Label: nx.draw_networkx_labels(G,pos) nx.draw_networkx_edges(G,pos,width=4) plt.show() def Graph(Tweets, Label = False, layOut='spring'): # Need the Tweets Before cleaning print("Please wait, building Graph .... ") G=nx.Graph() for tweet in tqdm(Tweets): if tweet['user']['screen_name'] not in G.nodes(): G.add_node(tweet['user']['screen_name']) mentionS = re.findall("@([a-zA-Z0-9]{1,15})", tweet['full_text']) for mention in mentionS: if "." not in mention: #skipping emails usr = mention.replace("@",'').strip() if usr not in G.nodes(): G.add_node(usr) G.add_edge(tweet['user']['screen_name'],usr) Nn, Ne = G.number_of_nodes(), G.number_of_edges() drawGraph(G, Label, layOut) print('Finished. There are %d nodes and %d edges in the Graph.' %(Nn,Ne)) return G def Centrality(G, N=10, method='katz', outliers=False, Label = True, layOut='shells'): if method.lower()=='katz': phi = 1.618033988749895 # largest eigenvalue of adj matrix ranking = nx.katz_centrality_numpy(G,1/phi) elif method.lower() == 'degree': ranking = nx.degree_centrality(G) elif method.lower() == 'eigen': ranking = nx.eigenvector_centrality_numpy(G) elif method.lower() =='closeness': ranking = nx.closeness_centrality(G) elif method.lower() =='betweeness': ranking = nx.betweenness_centrality(G) elif method.lower() =='harmonic': ranking = nx.harmonic_centrality(G) elif method.lower() =='percolation': ranking = nx.percolation_centrality(G) else: print('Error, Unsupported Method.'); return None important_nodes = sorted(ranking.items(), key=operator.itemgetter(1))[::-1]#[0:Nimportant] data = np.array([n[1] for n in important_nodes]) dnodes = [n[0] for n in important_nodes][:N] if outliers: m = 1 # 1 standard Deviation CI data = data[:N] out = len(data[abs(data - np.mean(data)) > m * np.std(data)]) # outlier within m stDev interval if out<N: dnodes = [n for n in dnodes[:out]] print('Influencial Users: {0}'.format(str(dnodes))) print('Influencial Users Scores: {0}'.format(str(data[:len(dnodes)]))) Gt = G.subgraph(dnodes) return Gt
[]
2024-01-10
tysondowd/micro-gpt
memory.py
""" Memory implementation for MicroGPT. """ # pylint: disable=line-too-long, import-error import os import uuid import textwrap from typing import List from abc import abstractmethod import openai import tiktoken def create_ada_embedding(data: str): """ Create an embedding using the OpenAI API. :param data: Data to create embedding for """ return openai.Embedding.create( input=[data], model="text-embedding-ada-002" )["data"][0]["embedding"] class Memory: """ Abstract base class for various memory implementations. """ def __init__(self): self.summarizer_model = os.getenv("SUMMARIZER_MODEL") self.max_context_size = int(os.getenv("MAX_CONTEXT_SIZE")) self.summarizer_chunk_size = int(os.getenv("SUMMARIZER_CHUNK_SIZE")) def summarize_memory_if_large(self, memory: str, max_tokens: int) -> str: """ Summarize a memory string if it exceeds the max_tokens limit. Args: memory (str): The memory string to be summarized. max_tokens (int): The maximum token limit. Returns: str: The summarized memory string. """ num_tokens = len(tiktoken.encoding_for_model( self.summarizer_model).encode(memory)) if num_tokens > max_tokens: avg_chars_per_token = len(memory) / num_tokens chunk_size = int(avg_chars_per_token * self.summarizer_chunk_size) chunks = textwrap.wrap(memory, chunk_size) summary_size = int(max_tokens / len(chunks)) memory = "" print(f"Summarizing memory, {len(chunks)} chunks.") for chunk in chunks: response = openai.ChatCompletion.create( model=self.summarizer_model, messages=[ {"role": "user", "content": f"Shorten the following memory chunk of an autonomous agent from a first person perspective, {summary_size} tokens max."}, {"role": "user", "content": f"Do your best to retain all semantic information including tasks performed by the agent, website content, important data points and hyper-links:\n\n{chunk}"}, ]) memory += response['choices'][0]['message']['content'] return memory @abstractmethod def add(self, data: str): """ Add a data entry to the memory. Args: data (str): The data string to be added to the memory. """ raise NotImplementedError @abstractmethod def get_context(self, data, num=5): """ Retrieve context data from the memory based on a query. Args: data: The query data. num (int, optional): The number of memory items to retrieve. Defaults to 5. Returns: str: The retrieved context. """ raise NotImplementedError memory_type = os.getenv("MEMORY_TYPE") if memory_type == "pinecone": import pinecone class PineconeMemory(Memory): """ Pinecone memory implementation. """ def __init__(self): super().__init__() pinecone.init( api_key=os.getenv("PINECONE_API_KEY"), environment=os.getenv("PINECONE_REGION") ) if "microgpt" not in pinecone.list_indexes(): print("Creating Pinecode index...") pinecone.create_index( "microgpt", dimension=1536, metric="cosine", pod_type="p1" ) self.index = pinecone.Index("microgpt") if os.getenv("CLEAR_DB_ON_START") in ['true', '1', 't', 'y', 'yes']: self.index.delete(deleteAll='true') def add(self, data: str): """ Add a data entry to the Pinecone memory. Args: data (str): The data string to be added to the memory. """ vector = create_ada_embedding(data) _id = uuid.uuid1() self.index.upsert([(str(_id), vector, {"data": data})]) def get_context(self, data, num=5): """ Retrieve context data from the Pinecone memory based on a query. Args: data: The query data. num (int, optional): The number of memory items to retrieve. Defaults to 5. Returns: str: The retrieved context. """ vector = create_ada_embedding(data) results = self.index.query( vector, top_k=num, include_metadata=True ) sorted_results = sorted(results.matches, key=lambda x: x.score) results_list = [str(item["metadata"]["data"]) for item in sorted_results] context = "\n".join(results_list) context = self.summarize_memory_if_large( context, self.max_context_size) return context elif memory_type == "postgres": import psycopg2 from sklearn.metrics.pairwise import cosine_similarity class PostgresMemory(Memory): """ Postgres memory implementation. """ def __init__(self): super().__init__() self.conn = psycopg2.connect( host=os.getenv("POSTGRES_HOST"), dbname=os.getenv("POSTGRES_DB"), user=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD") ) self._create_table_if_not_exists() if os.getenv("CLEAR_DB_ON_START") in ['true', '1', 't', 'y', 'yes']: self._clear_table() def _create_table_if_not_exists(self): """ Create a memory table in the Postgres database if it does not exist. """ with self.conn.cursor() as cur: cur.execute( """ CREATE TABLE IF NOT EXISTS memory ( id UUID PRIMARY KEY, vector float[] NOT NULL, data TEXT NOT NULL ); """ ) self.conn.commit() def _clear_table(self): """ Clear all entries in the memory table. """ with self.conn.cursor() as cur: cur.execute("DELETE FROM memory;") self.conn.commit() def _get_vectors_from_memory(self) -> List: """ Retrieve all memory vectors from the Postgres memory. Returns: List: A list of memory vectors. """ with self.conn.cursor() as cur: cur.execute("SELECT id, vector, data FROM memory;") vectors = cur.fetchall() return vectors def add(self, data: str): """ Add a data entry to the Postgres memory. Args: data (str): The data string to be added to the memory. """ vector = create_ada_embedding(data) _id = uuid.uuid1() with self.conn.cursor() as cur: cur.execute( "INSERT INTO memory (id, vector, data) VALUES (%s, %s, %s);", (str(_id), vector, data) ) self.conn.commit() def get_context(self, data, num=5): """ Retrieve context data from the Postgres memory based on a query. Args: data: The query data. num (int, optional): The number of memory items to retrieve. Defaults to 5. Returns: str: The retrieved context. """ vector = create_ada_embedding(data) all_vectors = self._get_vectors_from_memory() all_vectors.sort(key=lambda x: cosine_similarity( [x[1]], [vector])[0][0], reverse=True) top_k_vectors = all_vectors[:num] results_list = [str(item[2]) for item in top_k_vectors] context = "\n".join(results_list) context = self.summarize_memory_if_large( context, self.max_context_size) return context elif memory_type == "chromadb": import chromadb class ChromaDBMemory(Memory): """ ChromaDB memory implementation. """ def __init__(self): super().__init__() client = chromadb.Client() self.index = client.create_collection("microgpt") if os.getenv("CLEAR_DB_ON_START") in ['true', '1', 't', 'y', 'yes']: self.index.delete("microgpt") os.environ['TOKENIZERS_PARALLELISM'] = 'false' def add(self, data: str): """ Add a data entry to the ChromaDB memory. Args: data (str): The data string to be added to the memory. """ _id = uuid.uuid1() self.index.add( documents=[data], metadatas=[{"type": "memory"}], ids=[str(_id)] ) def get_context(self, data: str, num: int=5): """ Retrieve context data from the ChromaDB memory based on a query. Args: data (str): The query data. num (int, optional): The number of memory items to retrieve. Defaults to 5. Returns: str: The retrieved context. """ index_count = self.index.count() if index_count == 0: return data if index_count < num: num = index_count results = self.index.query( query_texts=[data], n_results=num ) results_list = results["documents"][0] context = "\n".join(results_list) context = self.summarize_memory_if_large( context, self.max_context_size) return context else: raise ValueError("Invalid MEMORY_TYPE environment variable") def get_memory_instance(): """ Return the memory implementation based on memory_type """ if memory_type == "pinecone": return PineconeMemory() if memory_type == "postgres": return PostgresMemory() if memory_type == "chromadb": return ChromaDBMemory() raise ValueError("Invalid MEMORY_TYPE environment variable")
[ "Do your best to retain all semantic information including tasks performed by the agent, website content, important data points and hyper-links:\n\nPLACEHOLDER", "Shorten the following memory chunk of an autonomous agent from a first person perspective, PLACEHOLDER tokens max." ]
2024-01-10
tysondowd/micro-gpt
microgpt.py
""" MicroGPT main executable. This script serves as the main entry point for the MicroGPT application. It provides a command-line interface for users to interact with a GPT-3.5/4 language model, leveraging memory management and context-based reasoning to achieve user-defined objectives. The agent can issue various types of commands, such as executing Python code, running shell commands, reading files, searching the web, scraping websites, and conversing with users. """ # pylint: disable=invalid-name, broad-exception-caught, exec-used, unspecified-encoding, wrong-import-position, import-error import os import sys import re import subprocess import platform from io import StringIO from contextlib import redirect_stdout from pathlib import Path from urllib.request import urlopen from bs4 import BeautifulSoup from dotenv import load_dotenv from termcolor import colored import openai from duckduckgo_search import ddg from spinner import Spinner operating_system = platform.platform() load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") DEBUG = os.getenv("DEBUG") in ['true', '1', 't', 'y', 'yes'] from memory import get_memory_instance SYSTEM_PROMPT = f"You are an autonomous agent running on {operating_system}." INSTRUCTIONS = ''' Carefully consider your next command. Supported commands are: execute_python, execute_shell, read_file, web_search, web_scrape, talk_to_user, or done The mandatory response format is: <r>[YOUR_REASONING]</r><c>[COMMAND]</c> [ARGUMENT] ARGUMENT may have multiple lines if the argument is Python code. Use only non-interactive shell commands. Python code run with execute_python must end with an output "print" statement. Send a separate "done" command *after* the objective was achieved. RESPOND WITH PRECISELY ONE THOUGHT/COMMAND/ARG COMBINATION. DO NOT CHAIN MULTIPLE COMMANDS. DO NOT INCLUDE EXTRA TEXT BEFORE OR AFTER THE COMMAND. Examples: <r>Search for websites relevant to salami pizza.</r><c>web_search</c> salami pizza <r>Scrape information about Apples.</r><c>web_scrape</c> https://en.wikipedia.org/wiki/Apple <r>I need to ask the user for guidance.</r><c>talk_to_user</c> What is URL of Domino's Pizza API? <r>Write 'Hello, world!' to file</r><c>execute_python</c> with open('hello_world.txt', 'w') as f: f.write('Hello, world!') ''' if __name__ == "__main__": model = os.getenv("MODEL") if len(sys.argv) != 2: print("Usage: microgpt.py <objective>") sys.exit(0) objective = sys.argv[1] max_memory_item_size = int(os.getenv("MAX_MEMORY_ITEM_SIZE")) memory = get_memory_instance() context = objective thought = "You awakened moments ago." work_dir = os.getenv("WORK_DIR") if work_dir is None or not work_dir: work_dir = os.path.join(Path.home(), "microgpt") if not os.path.exists(work_dir): os.makedirs(work_dir) print(f"Working directory is {work_dir}") try: os.chdir(work_dir) except FileNotFoundError: print("Directory doesn't exist. Set WORK_DIR to an existing directory or leave it blank.") sys.exit(0) while True: context = memory.get_context(f"{objective}, {thought}") if DEBUG: print(f"CONTEXT:\n{context}") with Spinner(): try: rs = openai.ChatCompletion.create( model=model, messages = [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": f"OBJECTIVE:{objective}"}, {"role": "user", "content": f"CONTEXT:\n{context}"}, {"role": "user", "content": f"INSTRUCTIONS:\n{INSTRUCTIONS}"}, ]) except openai.error.InvalidRequestError as e: if 'gpt-4' in str(e): print("Prompting the gpt-4 model failed. Falling back to gpt-3.5-turbo") model='gpt-3.5-turbo' continue print("Error accessing the OpenAI API: " + str(e)) sys.exit(0) response_text = rs['choices'][0]['message']['content'] if DEBUG: print(f"RAW RESPONSE:\n{response_text}") try: res_lines = response_text.split("\n") PATTERN = r'<(r|c)>(.*?)</(r|c)>' matches = re.findall(PATTERN, res_lines[0]) thought = matches[0][1] command = matches[1][1] if command == "done": print("Objective achieved.") sys.exit(0) # Account for GPT-3.5 sometimes including an extra "done" if "done" in res_lines[-1]: res_line = res_lines[:-1] arg = "\n".join(res_lines[1:]) # Remove unwanted code formatting backticks arg = arg.replace("```", "") mem = f"Your thought: {thought}\nYour command: {command}"\ f"\nCmd argument:\n{arg}\nResult:\n" except Exception as e: print(colored("Unable to parse response. Retrying...\n", "red")) continue if command == "talk_to_user": print(colored(f"MicroGPT: {arg}", 'cyan')) user_input = input('Your response: ') memory.add(f"{mem}The user responded with: {user_input}.") continue _arg = arg.replace("\n", "\\n") if len(arg) < 64 else f"{arg[:64]}...".replace("\n", "\\n") print(colored(f"MicroGPT: {thought}\nCmd: {command}, Arg: \"{_arg}\"", "cyan")) user_input = input('Press enter to perform this action or abort by typing feedback: ') if len(user_input) > 0: memory.add(f"{mem}The user responded: {user_input}."\ "Take this comment into consideration.") continue try: if command == "execute_python": _stdout = StringIO() with redirect_stdout(_stdout): exec(arg) memory.add(f"{mem}{_stdout.getvalue()}") elif command == "execute_shell": result = subprocess.run(arg, capture_output=True, shell=True, check=False) memory.add(f"{mem}STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}") elif command == "web_search": memory.add(f"{mem}{ddg(arg, max_results=5)}") elif command == "web_scrape": with urlopen(arg) as response: html = response.read() response_text = memory.summarize_memory_if_large( BeautifulSoup( html, features="lxml" ).get_text(), max_memory_item_size ) memory.add(f"{mem}{response_text}") elif command == "read_file": with open(arg, "r") as f: file_content = memory.summarize_memory_if_large(f.read(), max_memory_item_size) memory.add(f"{mem}{file_content}") elif command == "done": print("Objective achieved.") sys.exit(0) except Exception as e: memory.add(f"{mem}The command returned an error:\n{str(e)}\n"\ "You should fix the command or code.")
[ "CONTEXT:\nPLACEHOLDER", "INSTRUCTIONS:\n\nCarefully consider your next command.\nSupported commands are: execute_python, execute_shell, read_file, web_search, web_scrape, talk_to_user, or done\nThe mandatory response format is:\n\n<r>[YOUR_REASONING]</r><c>[COMMAND]</c>\n[ARGUMENT]\n\nARGUMENT may have multiple lines if the argument is Python code.\nUse only non-interactive shell commands.\nPython code run with execute_python must end with an output \"print\" statement.\nSend a separate \"done\" command *after* the objective was achieved.\nRESPOND WITH PRECISELY ONE THOUGHT/COMMAND/ARG COMBINATION.\nDO NOT CHAIN MULTIPLE COMMANDS.\nDO NOT INCLUDE EXTRA TEXT BEFORE OR AFTER THE COMMAND.\n\nExamples:\n\n<r>Search for websites relevant to salami pizza.</r><c>web_search</c>\nsalami pizza\n\n<r>Scrape information about Apples.</r><c>web_scrape</c>\nhttps://en.wikipedia.org/wiki/Apple\n\n<r>I need to ask the user for guidance.</r><c>talk_to_user</c>\nWhat is URL of Domino's Pizza API?\n\n<r>Write 'Hello, world!' to file</r><c>execute_python</c>\nwith open('hello_world.txt', 'w') as f:\n f.write('Hello, world!')\n", "OBJECTIVE:PLACEHOLDER", "You are an autonomous agent running on PLACEHOLDER." ]
2024-01-10
AdityaPatil-AP/AI-Therapist
cd_project_aditya.py
# -*- coding: utf-8 -*- """CD_Project_Aditya.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1MmpY-7i1zqglt4QyvgPVBXUX3kMGA13t """ print('AI Therapist') pip install lime pip install tensorflow pip install keras import re import nltk import string import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import lime from nltk.corpus import stopwords from nltk.stem import SnowballStemmer, WordNetLemmatizer from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from tensorflow.keras.utils import to_categorical from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.optimizers import Adam from tensorflow.keras.models import Sequential from tensorflow.keras.callbacks import EarlyStopping from tensorflow.keras.layers import Dense, LSTM, Embedding, Bidirectional nltk.download("stopwords") stop_words = set(stopwords.words("english")) lemmatizer= WordNetLemmatizer() # Modelling from sklearn.model_selection import train_test_split,KFold, GridSearchCV from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score,confusion_matrix, classification_report from sklearn.pipeline import Pipeline from sklearn.metrics import f1_score from sklearn.svm import SVC #Lime from lime import lime_text from lime.lime_text import LimeTextExplainer from lime.lime_text import IndexedString,IndexedCharacters from lime.lime_base import LimeBase from lime.lime_text import explanation sns.set(font_scale=1.3) nltk.download('omw-1.4') from google.colab import drive drive.mount('/content/drive') # Read datasets df_train = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/train.txt', names=['Text', 'Emotion'], sep=';') df_val = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/val.txt', names=['Text', 'Emotion'], sep=';') df_test = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/test.txt', names=['Text', 'Emotion'], sep=';') #print first 5 rows df_train.head() #print the shape of the data set print(df_train.shape) #print first 5 rows df_test.head() #print the shape of the data set print(df_test.shape) #print first 5 rows df_val.head() #print the shape of the data set print(df_val.shape) """## Train dataset""" #check if the data is balanced or not df_train.Emotion.value_counts() #check if the data is balanced or not df_train.Emotion.value_counts() / df_train.shape[0] *100 plt.figure(figsize=(8,4)) sns.countplot(x='Emotion', data=df_train); #print the number of null values in each column df_train.isnull().sum() #print the number of duplicated values df_train.duplicated().sum() #removing duplicated values index = df_train[df_train.duplicated() == True].index df_train.drop(index, axis = 0, inplace = True) df_train.reset_index(inplace=True, drop = True) #print the rows which are duplicated (duplicated in the text but with different emotions) df_train[df_train['Text'].duplicated() == True] #print some of those rows to check df_train[df_train['Text'] == df_train.iloc[7623]['Text']] df_train[df_train['Text'] == df_train.iloc[14313]['Text']] df_train[df_train['Text'] == df_train.iloc[13879]['Text']] #removing duplicated text index = df_train[df_train['Text'].duplicated() == True].index df_train.drop(index, axis = 0, inplace = True) df_train.reset_index(inplace=True, drop = True) #Count the number of stopwords in the data temp =df_train.copy() stop_words = set(stopwords.words("english")) temp['stop_words'] = temp['Text'].apply(lambda x: len(set(x.split()) & set(stop_words))) temp.stop_words.value_counts() #distribution of stopwords visually temp['stop_words'].plot(kind= 'hist') """TEST DATASET""" #check if the data is balanced or not df_test.Emotion.value_counts() plt.figure(figsize=(8,4)) sns.countplot(x='Emotion', data=df_test); #print the number of null values in each column df_test.isnull().sum() #print the number of duplicated values df_test.duplicated().sum() #print the rows which are duplicated df_test[df_test['Text'].duplicated() == True] #Count the number of stopwords in the data temp =df_test.copy() temp['stop_words'] = temp['Text'].apply(lambda x: len(set(x.split()) & set(stop_words))) temp.stop_words.value_counts() sns.set(font_scale=1.3) temp['stop_words'].plot(kind= 'hist') """## Validation dataset""" #check if the data is balanced or not df_val.Emotion.value_counts() plt.figure(figsize=(8,4)) sns.countplot(x='Emotion', data=df_val); #print the number of null values in each column df_val.isnull().sum() #print the number of duplicated values df_val.duplicated().sum() #print the rows which are duplicated df_val[df_val['Text'].duplicated() == True] df_val[df_val['Text'] == df_val.iloc[603]['Text']] df_val[df_val['Text'] == df_val.iloc[1993]['Text']] #removing duplicated text index = df_val[df_val['Text'].duplicated() == True].index df_val.drop(index, axis = 0, inplace = True) df_val.reset_index(inplace=True, drop = True) #Count the number of stopwords in the data temp =df_val.copy() temp['stop_words'] = temp['Text'].apply(lambda x: len(set(x.split()) & set(stop_words))) temp.stop_words.value_counts()[:10] sns.set(font_scale=1.3) temp['stop_words'].plot(kind= 'hist'); """## Compare rows of the datasets""" def dataframe_difference(df1, df2, which=None): """Find rows which are different between two DataFrames.""" # Combine the two DataFrames using a merge operation, with the # indicator parameter set to True. This adds a column called _merge # to the resulting DataFrame, which indicates the source of each row. comparison_df = df1.merge( df2, indicator=True, how='outer' ) # Filter the merged DataFrame based on the value of _merge. If which # is not specified, return all rows where _merge is not 'both'. # Otherwise, return all rows where _merge has the specified value if which is None: diff_df = comparison_df[comparison_df['_merge'] != 'both'] else: diff_df = comparison_df[comparison_df['_merge'] == which] # Return the filtered DataFrame return diff_df dataframe_difference(df_train, df_test, which='both') dataframe_difference(df_train, df_val, which='both') dataframe_difference(df_val, df_test, which='both') """## Cleaning""" def lemmatization(text): lemmatizer= WordNetLemmatizer() text = text.split() text=[lemmatizer.lemmatize(y) for y in text] return " " .join(text) def remove_stop_words(text): Text=[i for i in str(text).split() if i not in stop_words] return " ".join(Text) def Removing_numbers(text): text=''.join([i for i in text if not i.isdigit()]) return text def lower_case(text): text = text.split() text=[y.lower() for y in text] return " " .join(text) def Removing_punctuations(text): ## Remove punctuations text = re.sub('[%s]' % re.escape("""!"#$%&'()*+,،-./:;<=>؟?@[\]^_`{|}~"""), ' ', text) text = text.replace('؛',"", ) ## remove extra whitespace text = re.sub('\s+', ' ', text) text = " ".join(text.split()) return text.strip() def Removing_urls(text): url_pattern = re.compile(r'https?://\S+|www\.\S+') return url_pattern.sub(r'', text) def remove_small_sentences(df): for i in range(len(df)): if len(df.text.iloc[i].split()) < 3: df.text.iloc[i] = np.nan def normalize_text(df): df.Text=df.Text.apply(lambda text : lower_case(text)) df.Text=df.Text.apply(lambda text : remove_stop_words(text)) df.Text=df.Text.apply(lambda text : Removing_numbers(text)) df.Text=df.Text.apply(lambda text : Removing_punctuations(text)) df.Text=df.Text.apply(lambda text : Removing_urls(text)) df.Text=df.Text.apply(lambda text : lemmatization(text)) return df def normalized_sentence(sentence): sentence= lower_case(sentence) sentence= remove_stop_words(sentence) sentence= Removing_numbers(sentence) sentence= Removing_punctuations(sentence) sentence= Removing_urls(sentence) sentence= lemmatization(sentence) return sentence nltk.download('wordnet') normalized_sentence("My Name is Mohamed. @Tweets, plays 2022 Egypt_") df_train= normalize_text(df_train) df_test= normalize_text(df_test) df_val= normalize_text(df_val) """## Modeling""" #Preprocess text X_train = df_train['Text'].values y_train = df_train['Emotion'].values X_test = df_test['Text'].values y_test = df_test['Emotion'].values X_val = df_val['Text'].values y_val = df_val['Emotion'].values def train_model(model, data, targets): """ Train a model on the given data and targets. Parameters: model (sklearn model): The model to be trained. data (list of str): The input data. targets (list of str): The targets. Returns: Pipeline: The trained model as a Pipeline object. """ # Create a Pipeline object with a TfidfVectorizer and the given model text_clf = Pipeline([('vect',TfidfVectorizer()), ('clf', model)]) # Fit the model on the data and targets text_clf.fit(data, targets) return text_clf def get_F1(trained_model,X,y): # Make predictions on the input data using the trained model predicted=trained_model.predict(X) # Calculate the F1 score for the predictions f1=f1_score(y,predicted, average=None) # Return the F1 score return f1 #Train the model with the training data log_reg = train_model(LogisticRegression(solver='liblinear',random_state = 0), X_train, y_train) #Make a single prediction y_pred=log_reg.predict(['Happy']) y_pred #test the model with the test data y_pred=log_reg.predict(X_test) #calculate the accuracy log_reg_accuracy = accuracy_score(y_test, y_pred) print('Accuracy: ', log_reg_accuracy,'\n') #calculate the F1 score f1_Score = get_F1(log_reg,X_test,y_test) pd.DataFrame(f1_Score, index=df_train.Emotion.unique(), columns=['F1 score']) ##Classification Report print(classification_report(y_test, y_pred)) """**Training the Decision Tree model on the Training set**""" #Train the model with the training data DT = train_model(DecisionTreeClassifier(random_state = 0), X_train, y_train) #test the model with the test data y_pred=DT.predict(X_test) #calculate the accuracy DT_accuracy = accuracy_score(y_test, y_pred) print('Accuracy: ', DT_accuracy,'\n') #calculate the F1 score f1_Score = get_F1(DT,X_test,y_test) pd.DataFrame(f1_Score, index=df_train.Emotion.unique(), columns=['F1 score']) ##Classification Report print(classification_report(y_test, y_pred)) """**Training the Support Vector Machine model on the Training set** """ #Train the model with the training data SVM = train_model(SVC(random_state = 0), X_train, y_train) #test the model with the test data y_pred=SVM.predict(X_test) #calculate the accuracy SVM_accuracy = accuracy_score(y_test, y_pred) print('Accuracy: ', SVM_accuracy,'\n') #calculate the F1 score f1_Score = get_F1(SVM,X_test,y_test) pd.DataFrame(f1_Score, index=df_train.Emotion.unique(), columns=['F1 score']) ##Classification Report print(classification_report(y_test, y_pred)) """**Training the Random Forest model on the Training set**""" RF = train_model(RandomForestClassifier(random_state = 0), X_train, y_train) y_pred=RF.predict(X_test) RF_accuracy = accuracy_score(y_test, y_pred) print('Accuracy: ', RF_accuracy,'\n') f1_Score = get_F1(RF, X_test, y_test) pd.DataFrame(f1_Score, index=df_train.Emotion.unique(), columns=['F1 score']) print(classification_report(y_test, y_pred)) """## Results""" models = pd.DataFrame({ 'Model': ['Logistic Regression', 'Decision Tree','Support Vector Machine','Random Forest'], 'Accuracy': [log_reg_accuracy.round(2), DT_accuracy.round(2), SVM_accuracy.round(2), RF_accuracy.round(2)]}) models.sort_values(by='Accuracy', ascending=False).reset_index().drop(['index'], axis=1) """# Lime """ explainer_LR = LimeTextExplainer(class_names=RF.classes_) idx = 15 print("Actual Text : ", X_test[idx]) print("Prediction : ", RF.predict(X_test)[idx]) print("Actual : ", y_test[idx]) exp = explainer_LR.explain_instance(X_test[idx], RF.predict_proba,top_labels=5) exp.show_in_notebook() """Let's try moreeeee ## Text Preprocessing """ #Splitting the text from the labels X_train = df_train['Text'] y_train = df_train['Emotion'] X_test = df_test['Text'] y_test = df_test['Emotion'] X_val = df_val['Text'] y_val = df_val['Emotion'] # Encode labels le = LabelEncoder() y_train = le.fit_transform(y_train) y_test = le.transform(y_test) y_val = le.transform(y_val) #print the labels after encoding print(set(y_train)) #Convert the class vector (integers) to binary class matrix y_train = to_categorical(y_train) y_test = to_categorical(y_test) y_val = to_categorical(y_val) print(y_train) """## Tokenizing""" tokenizer = Tokenizer(oov_token='UNK') tokenizer.fit_on_texts(pd.concat([X_train, X_test], axis=0)) #word index: A dictionary of words and their uniquely assigned integers. tokenizer.word_index #word counts: A dictionary of words and their counts. tokenizer.word_counts #document count: A dictionary of words and how many documents each appeared in. #in this dataset the output will be the number of rows tokenizer.document_count #word docs: An integer count of the total number of documents(or rows) that were used to fit the Tokenizer. tokenizer.word_docs tokenizer.word_index['towards'] tokenizer.texts_to_sequences(X_train[0].split()) #convert the list of indexes into a matrix of ones and zeros (BOW) tokenizer.texts_to_matrix(X_train[0].split()) #the sentence contains three words and the size of the vocabulary is 14325 tokenizer.texts_to_matrix(X_train[0].split()).shape sequences_train = tokenizer.texts_to_sequences(X_train) sequences_test = tokenizer.texts_to_sequences(X_test) sequences_val = tokenizer.texts_to_sequences(X_val) sequences_train """## Padding""" df_test.shape maxlen = max([len(t) for t in df_train['Text']]) maxlen X_train = pad_sequences(sequences_train, maxlen=229, truncating='pre') X_test = pad_sequences(sequences_test, maxlen=229, truncating='pre') X_val = pad_sequences(sequences_val, maxlen=229, truncating='pre') vocabSize = len(tokenizer.index_word) + 1 print(f"Vocabulary size = {vocabSize}") #before sequences_train[0] #after X_train[0] """## Word Embedding""" # Read GloVE embeddings path_to_glove_file = '/content/drive/MyDrive/Colab Notebooks/glove.6B.200d.txt' num_tokens = vocabSize embedding_dim = 200 #latent factors or features hits = 0 misses = 0 embeddings_index = {} # Read word vectors with open(path_to_glove_file) as f: for line in f: word, coefs = line.split(maxsplit=1) coefs = np.fromstring(coefs, "f", sep=" ") embeddings_index[word] = coefs print("Found %s word vectors." % len(embeddings_index)) # Assign word vectors to our dictionary/vocabulary embedding_matrix = np.zeros((num_tokens, embedding_dim)) for word, i in tokenizer.word_index.items(): embedding_vector = embeddings_index.get(word) if embedding_vector is not None: # Words not found in embedding index will be all-zeros. # This includes the representation for "padding" and "OOV" embedding_matrix[i] = embedding_vector hits += 1 else: misses += 1 print("Converted %d words (%d misses)" % (hits, misses)) """## Modeling""" adam = Adam(learning_rate=0.005) model = Sequential() model.add(Embedding(vocabSize, 200, input_length=X_train.shape[1], weights=[embedding_matrix], trainable=False)) model.add(Bidirectional(LSTM(256, dropout=0.2,recurrent_dropout=0.2, return_sequences=True))) model.add(Bidirectional(LSTM(128, dropout=0.2,recurrent_dropout=0.2, return_sequences=True))) model.add(Bidirectional(LSTM(128, dropout=0.2,recurrent_dropout=0.2))) model.add(Dense(6, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) model.summary() from keras.utils.vis_utils import plot_model plot_model(model, show_shapes=True) #to stop the training when the loss starts to increase callback = EarlyStopping( monitor="val_loss", patience=4, restore_best_weights=True, ) history = model.fit(X_train, y_train, validation_data=(X_val, y_val), verbose=1, batch_size=256, epochs=30, callbacks=[callback] ) model.save('/content/drive/MyDrive/Colab Notebooks/lstm_bidir.h5') import tensorflow as tf model = tf.keras.models.load_model("/content/drive/MyDrive/Colab Notebooks/lstm_bidir.h5") model.evaluate(X_val, y_val, verbose=1) model.evaluate(X_test, y_test, verbose=1) predicted = model.predict(X_test) y_pred = predicted.argmax(axis=-1) print(classification_report(le.transform(df_test['Emotion']), y_pred)) # Commented out IPython magic to ensure Python compatibility. # %matplotlib inline import matplotlib.pyplot as plt acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(len(acc)) plt.plot(epochs, acc, 'r', label='Training accuracy') plt.plot(epochs, val_acc, 'b', label='Validation accuracy') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epochs, loss, 'r', label='Training Loss') plt.plot(epochs, val_loss, 'b', label='Validation Loss') plt.title('Training and validation loss') plt.legend() plt.show() sentences = [ "He's over the moon about being accepted to the university", "Your point on this certain matter made me outrageous, how can you say so? This is insane.", "I can't do it, I'm not ready to lose anything, just leave me alone", "Merlin's beard harry, you can cast the Patronus charm! I'm amazed!" ] for sentence in sentences: print(sentence) sentence = normalized_sentence(sentence) sentence = tokenizer.texts_to_sequences([sentence]) sentence = pad_sequences(sentence, maxlen=229, truncating='pre') result = le.inverse_transform(np.argmax(model.predict(sentence), axis=-1))[0] proba = np.max(model.predict(sentence)) print(f"{result} : {proba}\n\n") sentence= 'my old brother is dead' print(sentence) sentence = normalized_sentence(sentence) sentence = tokenizer.texts_to_sequences([sentence]) sentence = pad_sequences(sentence, maxlen=229, truncating='pre') result = le.inverse_transform(np.argmax(model.predict(sentence), axis=-1))[0] proba = np.max(model.predict(sentence)) print(f"{result} : {proba}\n\n") sentence= 'It is neutral' print(sentence) sentence = normalized_sentence(sentence) sentence = tokenizer.texts_to_sequences([sentence]) sentence = pad_sequences(sentence, maxlen=229, truncating='pre') result = le.inverse_transform(np.argmax(model.predict(sentence), axis=-1))[0] proba = np.max(model.predict(sentence)) print(f"{result} : {proba}\n\n") sentence= 'Im feeling sad today' type(sentence) import csv from datetime import datetime def write_in_csv(rows): with open(r'/content/drive/MyDrive/Colab Notebooks/emotions.csv', 'a', newline='') as file: file_write = csv.writer(file) for val in rows: file_write.writerow(val) res = [] temp = [] temp.append('emotion') temp.append('context') temp.append('Time') res.append(temp) write_in_csv(res) res = [] sentencetemp = str(input("enter your text \n")) sentence = sentencetemp print(sentence) sentence = normalized_sentence(sentence) sentence = tokenizer.texts_to_sequences([sentence]) sentence = pad_sequences(sentence, maxlen=229, truncating='pre') result = le.inverse_transform(np.argmax(model.predict(sentence), axis=-1))[0] proba = np.max(model.predict(sentence)) print(f"{result} : {proba}\n\n") rows = [] current_date_time = datetime.now() current_date_time = current_date_time.strftime("%c") rows.append(result) rows.append(sentencetemp) rows.append(current_date_time) res.append(rows) write_in_csv(res) em = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/emotions.csv') em.head() pip install openai import openai openai.api_key = "" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a chatbot"}, {"role": "user", "content": f"Concrete steps to take when experiencing {result}"}, ] ) result1 = '' for choice in response.choices: result1 += choice.message.content print(result1) import pandas as pd dataset = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/songs.csv') emotions = { 'sadness': 'Uplifting and soothing Playlist', 'anger': 'Calm Playlist', 'love': 'Wholesome Playlist', 'surpise': 'Classical Music Playlist', 'fear': 'Soothing Playlist', 'joy': 'Happy Playlist' } def recommend_music(emotion): if emotion in emotions: playlist = emotions[emotion] recommended_songs = dataset[dataset['emotion'] == emotion][['song_name', 'link']] print(f"Recommended songs for {emotion}: {playlist}") print("Songs:") for index, song in recommended_songs.iterrows(): print(f"{song['song_name']}: {song['link']}") else: print("Sorry, the specified emotion is not available.") user_emotion = result recommend_music(user_emotion)
[ "Concrete steps to take when experiencing PLACEHOLDER", "You are a chatbot" ]
2024-01-10
pr28416/parsel_coq
codex.py
import json import openai import os import time from consts import CONSTS import random class CodeGen(): def __init__(self, cache="cache.json", key="keys/codex_key.txt"): self.cache_file = cache self.exponential_backoff = 1 # Load the cache JSON file, if cache file exists. Else, cache is {} if os.path.exists(cache): while os.path.exists(self.cache_file + ".tmp") or os.path.exists(self.cache_file + ".lock"): time.sleep(0.1) with open(cache, "r") as f: self.cache = json.load(f) else: self.cache = {} # Load codex key from file with open(key, "r") as f: codex_key = f.read().strip() openai.organization, openai.api_key = codex_key.split(":") def generate(self, codex_in, num_completions=8, max_tokens=500, temperature=0.5, presence_penalty=0.0, stop=["\ndef"], indented=True, indented_after_first_line=False, require=None, cache_key=None, rate_limit_tokens=4000, verbose=False, logit_bias=None, model_name=None ): print("MODEL: TEXT-DAVINCI-003") if model_name is None: model_name = "text-davinci-003" if verbose: print(codex_in) print("-----") assert isinstance(codex_in, str) cache_key_base = codex_in if cache_key is None else cache_key cache_key_list = (cache_key_base, max_tokens, temperature, stop, indented, indented_after_first_line, require) if presence_penalty != 0.0: cache_key_list = cache_key_list + (presence_penalty,) if model_name != "text-davinci-003": cache_key_list = cache_key_list + (model_name,) cache_key = str(cache_key_list) if cache_key in self.cache: if len(self.cache[cache_key]) < num_completions: num_completions -= len(self.cache[cache_key]) results = self.cache[cache_key] else: print(f"cache_key ({len(self.cache[cache_key])}): {cache_key}, num_completions: {num_completions}") cur_implementations = self.cache[cache_key].copy() if "shuffle_implementations" in CONSTS and CONSTS["shuffle_implementations"]: random.shuffle(cur_implementations) return cur_implementations[:num_completions] else: results = [] if model_name != "text-davinci-003": print("WARNING, using davinci text model") print("Calling Codex!") # raise Exception("Codex is not available") total_tokens = num_completions * max_tokens completions_per_call = rate_limit_tokens // max_tokens while total_tokens > 0: num_completions = min(total_tokens // max_tokens, completions_per_call) print(num_completions, "completions", max_tokens, "tokens each") while True: try: time.sleep(8) if logit_bias is None: completions = openai.Completion.create( model=model_name, prompt=codex_in, max_tokens=max_tokens, temperature=temperature, presence_penalty=presence_penalty, stop=stop, n=num_completions, )['choices'] else: completions = openai.Completion.create( model=model_name, prompt=codex_in, max_tokens=max_tokens, temperature=temperature, presence_penalty=presence_penalty, stop=stop, n=num_completions, logit_bias=logit_bias )['choices'] self.exponential_backoff = 1 break except openai.error.RateLimitError: print("Rate limit reached. Waiting before retrying...") time.sleep(16 * self.exponential_backoff) self.exponential_backoff *= 2 # print("CODE GENERATION OPENAI COMPLETIONS:", completions) for completion in completions: result = [] for line_idx, line in enumerate(completion.text.split("\n")): if (indented or (indented_after_first_line and line_idx > 0)) and line.lstrip() == line and line.strip() != "": break if require is not None and line.strip() != "" and require not in line: break result += [line] results.append(result) # Save updated cache - reopen in case multiple processes running # Save to a temp file first, then rename # Check if a temp file exists, and if so, wait for it to be deleted while os.path.exists(self.cache_file + ".tmp") or os.path.exists(self.cache_file + ".lock"): time.sleep(0.1) # create an empty file to indicate that we are writing to the cache with open(self.cache_file + ".lock", "w") as f: pass if os.path.exists(self.cache_file): with open(self.cache_file, "r") as f: self.cache = json.load(f) self.cache[cache_key] = results with open(self.cache_file + ".tmp", "w") as f: json.dump(self.cache, f) os.rename(self.cache_file + ".tmp", self.cache_file) os.remove(self.cache_file + ".lock") total_tokens -= num_completions * max_tokens return results
[]
2024-01-10
DiaTime/superagent
app~lib~agents.py
from typing import Any import requests import yaml from decouple import config from langchain.agents import ( AgentExecutor, LLMSingleActionAgent, ) from langchain.agents.agent_toolkits.openapi import planner from langchain.agents.agent_toolkits.openapi.spec import reduce_openapi_spec from langchain.chains import ConversationalRetrievalChain, LLMChain from langchain.chains.conversational_retrieval.prompts import ( CONDENSE_QUESTION_PROMPT, QA_PROMPT, ) from langchain.chains.question_answering import load_qa_chain from langchain.chat_models import ChatAnthropic, ChatOpenAI from langchain.embeddings.openai import OpenAIEmbeddings from langchain.llms import Cohere, OpenAI from langchain.memory import ChatMessageHistory, ConversationBufferMemory from langchain.prompts.prompt import PromptTemplate from langchain.requests import RequestsWrapper from langchain.vectorstores.pinecone import Pinecone from app.lib.callbacks import StreamingCallbackHandler from app.lib.parsers import CustomOutputParser from app.lib.prisma import prisma from app.lib.prompts import ( CustomPromptTemplate, agent_template, default_chat_prompt, ) from app.lib.tools import get_search_tool class Agent: def __init__( self, agent: dict, has_streaming: bool = False, on_llm_new_token=None, on_llm_end=None, on_chain_end=None, ): self.id = agent.id self.document = agent.document self.has_memory = agent.hasMemory self.type = agent.type self.llm = agent.llm self.prompt = agent.prompt self.tool = agent.tool self.has_streaming = has_streaming self.on_llm_new_token = on_llm_new_token self.on_llm_end = on_llm_end self.on_chain_end = on_chain_end def _get_api_key(self) -> str: if self.llm["provider"] == "openai-chat" or self.llm["provider"] == "openai": return ( self.llm["api_key"] if "api_key" in self.llm else config("OPENAI_API_KEY") ) if self.llm["provider"] == "anthropic": return ( self.llm["api_key"] if "api_key" in self.llm else config("ANTHROPIC_API_KEY") ) if self.llm["provider"] == "cohere": return ( self.llm["api_key"] if "api_key" in self.llm else config("COHERE_API_KEY") ) def _get_tool(self) -> Any: try: if self.tool.type == "SEARCH": tools = get_search_tool() return tools except Exception: return None def _get_prompt(self) -> Any: if self.prompt: if self.tool: prompt = CustomPromptTemplate( template=self.prompt.template, tools=self._get_tool(), input_variables=self.prompt.input_variables, ) else: prompt = PromptTemplate( input_variables=self.prompt.input_variables, template=self.prompt.template, ) return prompt else: if self.tool: return CustomPromptTemplate( template=agent_template, tools=self._get_tool(), input_variables=[ "human_input", "intermediate_steps", "chat_history", ], ) return default_chat_prompt def _get_llm(self) -> Any: if self.llm["provider"] == "openai-chat": return ( ChatOpenAI( temperature=0, openai_api_key=self._get_api_key(), model_name=self.llm["model"], streaming=self.has_streaming, callbacks=[ StreamingCallbackHandler( on_llm_new_token_=self.on_llm_new_token, on_llm_end_=self.on_llm_end, on_chain_end_=self.on_chain_end, ) ], ) if self.has_streaming else ChatOpenAI(model_name=self.llm["model"]) ) if self.llm["provider"] == "openai": return OpenAI( model_name=self.llm["model"], openai_api_key=self._get_api_key() ) if self.llm["provider"] == "anthropic": return ( ChatAnthropic( streaming=self.has_streaming, anthropic_api_key=self._get_api_key(), callbacks=[ StreamingCallbackHandler( on_llm_new_token_=self.on_llm_new_token, on_llm_end_=self.on_llm_end, on_chain_end_=self.on_chain_end, ) ], ) if self.has_streaming else ChatAnthropic(anthropic_api_key=self._get_api_key()) ) if self.llm["provider"] == "cohere": return ( Cohere( cohere_api_key=self._get_api_key(), model=self.llm["model"], callbacks=[ StreamingCallbackHandler( on_llm_new_token_=self.on_llm_new_token, on_llm_end_=self.on_llm_end, on_chain_end_=self.on_chain_end, ) ], ) if self.has_streaming else Cohere(cohere_api_key=self._get_api_key(), model=self.llm["model"]) ) # Use ChatOpenAI as default llm in agents return ChatOpenAI(temperature=0, openai_api_key=self._get_api_key()) def _get_memory(self) -> Any: if self.has_memory: memories = prisma.agentmemory.find_many( where={"agentId": self.id}, order={"createdAt": "desc"}, take=5, ) history = ChatMessageHistory() [ history.add_ai_message(memory.message) if memory.agent == "AI" else history.add_user_message(memory.message) for memory in memories ] memory = ConversationBufferMemory( chat_memory=history, memory_key="chat_history" ) return memory return None def _get_document(self) -> Any: if self.document: embeddings = OpenAIEmbeddings() docsearch = Pinecone.from_existing_index( "superagent", embedding=embeddings, namespace=self.document.id ) return docsearch return None def get_agent(self) -> Any: llm = self._get_llm() memory = self._get_memory() document = self._get_document() tools = self._get_tool() if self.document: if self.document.type != "OPENAPI": question_generator = LLMChain( llm=OpenAI(temperature=0), prompt=CONDENSE_QUESTION_PROMPT ) doc_chain = load_qa_chain( llm, chain_type="stuff", prompt=QA_PROMPT, verbose=True ) agent = ConversationalRetrievalChain( retriever=document.as_retriever(), combine_docs_chain=doc_chain, question_generator=question_generator, memory=memory, get_chat_history=lambda h: h, ) elif self.document.type == "OPENAPI": yaml_response = requests.get(self.document.url) content = yaml_response.content raw_odds_api_spec = yaml.load(content, Loader=yaml.Loader) odds_api_spec = reduce_openapi_spec(raw_odds_api_spec) requests_wrapper = RequestsWrapper() agent = planner.create_openapi_agent( odds_api_spec, requests_wrapper, llm ) elif self.tool: output_parser = CustomOutputParser() tool_names = [tool.name for tool in tools] llm_chain = LLMChain(llm=llm, prompt=self._get_prompt()) agent_config = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names, ) agent = AgentExecutor.from_agent_and_tools( agent=agent_config, tools=tools, verbose=True, memory=memory ) else: agent = LLMChain( llm=llm, memory=memory, verbose=True, prompt=self._get_prompt() ) return agent
[]
2024-01-10
joostshao/privateGPT
privateGPT.py
#!/usr/bin/env python3 from dotenv import load_dotenv from langchain.chains import RetrievalQA from langchain.embeddings import HuggingFaceEmbeddings from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.vectorstores import Chroma from langchain.llms import GPT4All, LlamaCpp import os import argparse load_dotenv() embeddings_model_name = os.environ.get("EMBEDDINGS_MODEL_NAME") persist_directory = os.environ.get('PERSIST_DIRECTORY') model_type = os.environ.get('MODEL_TYPE') model_path = os.environ.get('MODEL_PATH') model_n_ctx = os.environ.get('MODEL_N_CTX') from constants import CHROMA_SETTINGS def main(): # Parse the command line arguments args = parse_arguments() embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name) db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS) retriever = db.as_retriever() # activate/deactivate the streaming StdOut callback for LLMs callbacks = [] if args.mute_stream else [StreamingStdOutCallbackHandler()] # Prepare the LLM match model_type: case "LlamaCpp": llm = LlamaCpp(model_path=model_path, n_ctx=model_n_ctx, callbacks=callbacks, verbose=False, n_threads=100) case "GPT4All": llm = GPT4All(model=model_path, n_ctx=model_n_ctx, backend='gptj', callbacks=callbacks, verbose=False, n_threads=100) case _default: print(f"Model {model_type} not supported!") exit; qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents= not args.hide_source) # Interactive questions and answers while True: query = input("\nEnter a query: ") if query == "exit": break # Get the answer from the chain res = qa(query) answer, docs = res['result'], [] if args.hide_source else res['source_documents'] # Print the result print("\n\n> Question:") print(query) print("\n> Answer:") print(answer) # Print the relevant sources used for the answer for document in docs: print("\n> " + document.metadata["source"] + ":") print(document.page_content) def parse_arguments(): parser = argparse.ArgumentParser(description='privateGPT: Ask questions to your documents without an internet connection, ' 'using the power of LLMs.') parser.add_argument("--hide-source", "-S", action='store_true', help='Use this flag to disable printing of source documents used for answers.') parser.add_argument("--mute-stream", "-M", action='store_true', help='Use this flag to disable the streaming StdOut callback for LLMs.') return parser.parse_args() if __name__ == "__main__": main()
[]
2024-01-10
wataruhashimoto52/svgd_tf
svgd.py
import numpy as np import tensorflow as tf import tensorflow_probability as tfp class SVGD(object): def __init__(self, grads_list, vars_list, optimizer): self.grads_list = grads_list self.vars_list = vars_list self.optimizer = optimizer self.num_particles = len(vars_list) def get_pairwise_dist(self, x): norm = tf.reshape(tf.reduce_sum(x * x, 1), [-1, 1]) return norm - 2 * tf.matmul(x, tf.transpose(x)) + tf.transpose(norm) def _get_svgd_kernel(self, X): stacked_vars = tf.stack(X) pairwise_dists = self.get_pairwise_dist(stacked_vars) lower = tfp.stats.percentile( pairwise_dists, 50.0, interpolation='lower') higher = tfp.stats.percentile( pairwise_dists, 50.0, interpolation='higher') median = (lower + higher) / 2 median = tf.cast(median, tf.float32) h = tf.sqrt(0.5 * median / tf.math.log(len(X) + 1.)) h = tf.stop_gradient(h) # kernel computation Kxy = tf.exp(-pairwise_dists / h ** 2 / 2) dxkxy = -tf.matmul(Kxy, stacked_vars) sumkxy = tf.reduce_sum(Kxy, axis=1, keepdims=True) # analytical kernel gradient dxkxy = (dxkxy + stacked_vars * sumkxy) / tf.pow(h, 2) return Kxy, dxkxy def get_num_elements(self, var): return int(np.prod(self.var_shape(var))) def _flatten(self, grads, variables): # from openai/baselines/common/tf_util.py flatgrads = tf.concat(axis=0, values=[ tf.reshape(grad if grad is not None else tf.zeros_like( v), [self.get_num_elements(v)]) for (v, grad) in zip(variables, grads)]) flatvars = tf.concat(axis=0, values=[ tf.reshape(var, [self.get_num_elements(var)])for var in variables]) return flatgrads, flatvars def var_shape(self, var): out = var.get_shape().as_list() return out def run(self): all_flatgrads = [] all_flatvars = [] for grads, variables in zip(self.grads_list, self.vars_list): flatgrads, flatvars = self._flatten(grads, variables) all_flatgrads.append(flatgrads) all_flatvars.append(flatvars) Kxy, dxkxy = self._get_svgd_kernel(all_flatvars) stacked_grads = tf.stack(all_flatgrads) stacked_grads = tf.matmul(Kxy, stacked_grads) - dxkxy stacked_grads /= self.num_particles flatgrads_list = tf.unstack(stacked_grads, self.num_particles) # align index all_grads = [] for flatgrads, variables in zip(flatgrads_list, self.vars_list): start = 0 grads = [] for var in variables: shape = self.var_shape(var) end = start + int(np.prod(self.var_shape(var))) grads.append(tf.reshape(flatgrads[start:end], shape)) # next start = end all_grads.append(grads) for grads, variables in zip(all_grads, self.vars_list): self.optimizer.apply_gradients( [(-grad, var) for grad, var in zip(grads, variables)]) return
[]
2024-01-10
vital121/guidance-COT
guidance~llms~caches~_diskcache.py
import os import diskcache import platformdirs from guidance.llms.caches import Cache class DiskCache(Cache): """DiskCache is a cache that uses diskcache lib.""" def __init__(self, llm_name: str): self._diskcache = diskcache.Cache( os.path.join( platformdirs.user_cache_dir("guidance"), f"_{llm_name}.diskcache" ) ) def __getitem__(self, key: str) -> str: return self._diskcache[key] def __setitem__(self, key: str, value: str) -> None: self._diskcache[key] = value def __contains__(self, key: str) -> bool: return key in self._diskcache def clear(self): self._diskcache.clear()
[]
2024-01-10
mradnai/docuscan
docuquery.py
import streamlit as st from langchain.prompts.prompt import PromptTemplate from langchain.llms import OpenAI from langchain.chains import LLMChain import docx2txt class WordSuccessChecker: def __init__(self): self.uploaded_file = None self.doc_text = None self.input_text = None self._validation_prompt = """ You are a highly trained assistant who reads through input documents and answer questions about them. Table elements are delimeted by | sign. This is the input document: {doc_text} You get the following question: {input_text} Provide a accurate and informative answer only based on the document. If question cannot be answered based on the input document do not make up an answer just tell answer is not in the text. """ self.VALIDATION_PROMPT = PromptTemplate( input_variables=["doc_text", "input_text"], template=self._validation_prompt ) def query_doc(self): if len(self.doc_text.split()) < 5000: return self.query_doc_short() else: return self.query_doc_long() def query_doc_short(self): llm_chain = LLMChain( llm=OpenAI(temperature=0, max_tokens=-1, model_name="gpt-3.5-turbo-16k"), prompt=self.VALIDATION_PROMPT ) return llm_chain({"doc_text": self.doc_text, "input_text": self.input_text})["text"] def query_doc_long(self): llm_chain = LLMChain( llm=OpenAI(temperature=0, max_tokens=-1, model_name="gpt-3.5-turbo-16k"), prompt=self.VALIDATION_PROMPT ) return llm_chain({"doc_text": self.doc_text[:14000], "input_text": self.input_text})["text"] def run(self): st.image("./logo1.png", width=150) st.title("AI Document Assistant") # Upload Word document self.uploaded_file = st.file_uploader("Upload a Word document", type=["docx"]) if self.uploaded_file is not None: # Read the uploaded Word document self.doc_text = docx2txt.process(self.uploaded_file) # User input text self.input_text = st.text_input("Enter a question to query the uploaded document:") if st.button("Query"): if self.input_text: result = self.query_doc() st.write(f"Answer: {result}") else: st.write("Please enter a question.") if __name__ == '__main__': app = WordSuccessChecker() app.run()
[]
2024-01-10
JumpingRain/BMTools
bmtools~agent~apitool.py
"""Interface for tools.""" from inspect import signature from typing import Any, Awaitable, Callable, Optional, Union from langchain.agents import Tool as LangChainTool from langchain.tools.base import BaseTool import requests import json import aiohttp import http.client http.client._MAXLINE = 655360 from bmtools import get_logger logger = get_logger(__name__) class Tool(LangChainTool): tool_logo_md: str = "" class RequestTool(BaseTool): """Tool that takes in function or coroutine directly.""" description: str = "" func: Callable[[str], str] afunc: Callable[[str], str] coroutine: Optional[Callable[[str], Awaitable[str]]] = None max_output_len = 4000 tool_logo_md: str = "" def _run(self, tool_input: str) -> str: """Use the tool.""" return self.func(tool_input) async def _arun(self, tool_input: str) -> str: """Use the tool asynchronously.""" ret = await self.afunc(tool_input) return ret def convert_prompt(self,params): lines = "Your input should be a json (args json schema): {{" for p in params: logger.debug(p) optional = not p['required'] description = p.get('description', '') if len(description) > 0: description = "("+description+")" lines += '"{name}" : {type}{desc}, '.format(name=p['name'], type= p['schema']['type'], optional=optional, desc=description) lines += "}}" return lines def __init__(self, root_url, func_url, method, request_info, **kwargs): """ Store the function, description, and tool_name in a class to store the information """ url = root_url + func_url def func(json_args): if isinstance(json_args, str): try: json_args = json.loads(json_args) except: return "Your input can not be parsed as json, please use thought." if "tool_input" in json_args: json_args = json_args["tool_input"] response = requests.get(url, json_args) if response.status_code == 200: message = response.text else: message = f"Error code {response.status_code}. You can try (1) Change your input (2) Call another function. (If the same error code is produced more than 4 times, please use Thought: I can not use these APIs, so I will stop. Final Answer: No Answer, please check the APIs.)" message = message[:self.max_output_len] # TODO: not rigorous, to improve return message async def afunc(json_args): if isinstance(json_args, str): try: json_args = json.loads(json_args) except: return "Your input can not be parsed as json, please use thought." if "tool_input" in json_args: json_args = json_args["tool_input"] async with aiohttp.ClientSession() as session: async with session.get(url, params=json_args) as response: if response.status == 200: message = await response.text() else: message = f"Error code {response.status_code}. You can try (1) Change your input (2) Call another function. (If the same error code is produced more than 4 times, please use Thought: I can not use these APIs, so I will stop. Final Answer: No Answer, please check the APIs.)" message = message[:self.max_output_len] # TODO: not rigorous, to improve return message tool_name = func_url.replace("/", ".").strip(".") if 'parameters' in request_info[method]: str_doc = self.convert_prompt(request_info[method]['parameters']) else: str_doc = '' # description = f"- {tool_name}:\n" + \ # request_info[method].get('summary', '').replace("{", "{{").replace("}", "}}") \ description = request_info[method].get('description','').replace("{", "{{").replace("}", "}}") \ + ". " \ + str_doc \ + f" The Action to trigger this API should be {tool_name} and the input parameters should be a json dict string. Pay attention to the type of parameters." logger.info("API Name: {}".format(tool_name)) logger.info("API Description: {}".format(description)) super(RequestTool, self).__init__( name=tool_name, func=func, afunc=afunc, description=description, **kwargs )
[]
2024-01-10
JumpingRain/BMTools
web_demo.py
import gradio as gr import sys # sys.path.append('./inference/') from bmtools.agent.tools_controller import MTQuestionAnswerer, load_valid_tools from bmtools.agent.singletool import STQuestionAnswerer from langchain.schema import AgentFinish import os import requests available_models = ["ChatGPT", "GPT-3.5"] DEFAULTMODEL = "GPT-3.5" tools_mappings = { "klarna": "https://www.klarna.com/", "chemical-prop": "http://127.0.0.1:8079/tools/chemical-prop/", "wolframalpha": "http://127.0.0.1:8079/tools/wolframalpha/", "weather": "http://127.0.0.1:8079/tools/weather/", "douban-film": "http://127.0.0.1:8079/tools/douban-film/", "wikipedia": "http://127.0.0.1:8079/tools/wikipedia/", "office-ppt": "http://127.0.0.1:8079/tools/office-ppt/", "bing_search": "http://127.0.0.1:8079/tools/bing_search/", "map": "http://127.0.0.1:8079/tools/map/", "stock": "http://127.0.0.1:8079/tools/stock/", "baidu-translation": "http://127.0.0.1:8079/tools/baidu-translation/", "nllb-translation": "http://127.0.0.1:8079/tools/nllb-translation/", } valid_tools_info = load_valid_tools(tools_mappings) print(valid_tools_info) all_tools_list = sorted(list(valid_tools_info.keys())) gr.close_all() MAX_TURNS = 30 MAX_BOXES = MAX_TURNS * 2 def show_avatar_imgs(tools_chosen): if len(tools_chosen) == 0: tools_chosen = list(valid_tools_info.keys()) img_template = '<a href="{}" style="float: left"> <img style="margin:5px" src="{}.png" width="24" height="24" alt="avatar" /> {} </a>' imgs = [valid_tools_info[tool]['avatar'] for tool in tools_chosen if valid_tools_info[tool]['avatar'] != None] imgs = ' '.join([img_template.format(img, img, tool ) for img, tool in zip(imgs, tools_chosen) ]) return [gr.update(value='<span class="">'+imgs+'</span>', visible=True), gr.update(visible=True)] return_msg = [] chat_history = "" def answer_by_tools(question, tools_chosen, model_chosen): global return_msg return_msg += [(question, None), (None, '...')] yield [gr.update(visible=True, value=return_msg), gr.update(), gr.update()] OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '') if len(tools_chosen) == 0: # if there is no tools chosen, we use all todo (TODO: What if the pool is too large.) tools_chosen = list(valid_tools_info.keys()) if len(tools_chosen) == 1: answerer = STQuestionAnswerer(OPENAI_API_KEY.strip(), stream_output=True, llm=model_chosen) agent_executor = answerer.load_tools(tools_chosen[0], valid_tools_info[tools_chosen[0]], prompt_type="react-with-tool-description", return_intermediate_steps=True) else: answerer = MTQuestionAnswerer(OPENAI_API_KEY.strip(), load_valid_tools({k: tools_mappings[k] for k in tools_chosen}), stream_output=True, llm=model_chosen) agent_executor = answerer.build_runner() global chat_history chat_history += "Question: " + question + "\n" question = chat_history for inter in agent_executor(question): if isinstance(inter, AgentFinish): continue result_str = [] return_msg.pop() if isinstance(inter, dict): result_str.append("<font color=red>Answer:</font> {}".format(inter['output'])) chat_history += "Answer:" + inter['output'] + "\n" result_str.append("...") else: not_observation = inter[0].log if not not_observation.startswith('Thought:'): not_observation = "Thought: " + not_observation chat_history += not_observation not_observation = not_observation.replace('Thought:', '<font color=green>Thought: </font>') not_observation = not_observation.replace('Action:', '<font color=purple>Action: </font>') not_observation = not_observation.replace('Action Input:', '<font color=purple>Action Input: </font>') result_str.append("{}".format(not_observation)) result_str.append("<font color=blue>Action output:</font>\n{}".format(inter[1])) chat_history += "\nAction output:" + inter[1] + "\n" result_str.append("...") return_msg += [(None, result) for result in result_str] yield [gr.update(visible=True, value=return_msg), gr.update(), gr.update()] return_msg.pop() if return_msg[-1][1].startswith("<font color=red>Answer:</font> "): return_msg[-1] = (return_msg[-1][0], return_msg[-1][1].replace("<font color=red>Answer:</font> ", "<font color=green>Final Answer:</font> ")) yield [gr.update(visible=True, value=return_msg), gr.update(visible=True), gr.update(visible=False)] def retrieve(tools_search): if tools_search == "": return gr.update(choices=all_tools_list) else: url = "http://127.0.0.1:8079/retrieve" param = { "query": tools_search } response = requests.post(url, json=param) result = response.json() retrieved_tools = result["tools"] return gr.update(choices=retrieved_tools) def clear_retrieve(): return [gr.update(value=""), gr.update(choices=all_tools_list)] def clear_history(): global return_msg global chat_history return_msg = [] chat_history = "" yield gr.update(visible=True, value=return_msg) with gr.Blocks() as demo: with gr.Row(): with gr.Column(scale=14): gr.Markdown("<h1 align='left'> BMTools </h1>") with gr.Column(scale=1): gr.Markdown('<img src="https://openbmb.cn/openbmb/img/head_logo.e9d9f3f.png" width="140">') with gr.Row(): with gr.Column(scale=4): with gr.Row(): with gr.Column(scale=0.85): txt = gr.Textbox(show_label=False, placeholder="Question here. Use Shift+Enter to add new line.", lines=1).style(container=False) with gr.Column(scale=0.15, min_width=0): buttonClear = gr.Button("Clear History") buttonStop = gr.Button("Stop", visible=False) chatbot = gr.Chatbot(show_label=False, visible=True).style(height=600) with gr.Column(scale=1): with gr.Row(): tools_search = gr.Textbox( lines=1, label="Tools Search", info="Please input some text to search tools.", ) buttonSearch = gr.Button("Clear") tools_chosen = gr.CheckboxGroup( choices=all_tools_list, value=["chemical-prop"], label="Tools provided", info="Choose the tools to solve your question.", ) model_chosen = gr.Dropdown( list(available_models), value=DEFAULTMODEL, multiselect=False, label="Model provided", info="Choose the model to solve your question, Default means ChatGPT." ) tools_search.change(retrieve, tools_search, tools_chosen) buttonSearch.click(clear_retrieve, [], [tools_search, tools_chosen]) txt.submit(lambda : [gr.update(value=''), gr.update(visible=False), gr.update(visible=True)], [], [txt, buttonClear, buttonStop]) inference_event = txt.submit(answer_by_tools, [txt, tools_chosen, model_chosen], [chatbot, buttonClear, buttonStop]) buttonStop.click(lambda : [gr.update(visible=True), gr.update(visible=False)], [], [buttonClear, buttonStop], cancels=[inference_event]) buttonClear.click(clear_history, [], chatbot) demo.queue().launch(share=True, inbrowser=True, server_name="127.0.0.1", server_port=7001)
[ "<a href=\"{}\" style=\"float: left\"> <img style=\"margin:5px\" src=\"{}.png\" width=\"24\" height=\"24\" alt=\"avatar\" /> {} </a>" ]
2024-01-10
zencore-dev/billing-streamlit
app-chain.py
import streamlit as st import os from langchain.llms import VertexAI #LlamaCpp from google.cloud import bigquery import logging import pandas from sqlalchemy import * from sqlalchemy.engine import create_engine from sqlalchemy.schema import * from langchain.agents import create_sql_agent from langchain.agents.agent_toolkits import SQLDatabaseToolkit from langchain.sql_database import SQLDatabase from langchain.llms.openai import OpenAI from langchain.agents import AgentExecutor from langchain.prompts import PromptTemplate project = os.environ['GOOGLE_CLOUD_PROJECT'] dataset = os.environ['BILLING_DATASET'] table = os.environ['BILLING_TABLE'] sqlalchemy_url = f"bigquery://{os.environ['GOOGLE_CLOUD_PROJECT']}/{dataset}" def process_prompt(): user_query = st.session_state.user_query schema = "" with open("schema.json", "r") as f: schema = f.read() TEMPLATE = """Only use the following tables: {table}. The schema of the table is: {schema} If accessing sub keys of a field, you must use UNNEST to flatten the data. where service.id is an unusable identifier and service.description is the name of the service use invoice.month for any data prior to the current month INTERVAL must be in days, not months or years You can not query nested fields, so e.g. SELECT `gcp_billing_export_v1_010767_AD0D5D_BCC8F6`.`project`.`number` is not a valid query Some examples of SQL queries that corrsespond to questions are: input: how much did I spend on compute in the last 90 days? output: SELECT sum(total_cost) as my_cost, FORMAT_DATE("%Y-%m", usage_start_time) AS month, FROM `{dataset}` WHERE service.description LIKE "%Compute%" AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 90 day GROUP BY month input: how much did I spend in the last month? output: SELECT sum(total_cost) as my_cost, FORMAT_DATE("%Y-%m", usage_start_time) AS month FROM `{dataset}` WHERE usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day GROUP BY month input: how much did I spend last month? output: SELECT sum(total_cost) as my_cost, FORMAT_DATE("%Y-%m", usage_start_time) AS month FROM `{dataset}` WHERE usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 60 day AND usage_start_time <= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day GROUP BY month input: how much did I spend on compute over the last 6 months? output: SELECT sum(cost) as my_cost, FORMAT_DATE("%Y-%m", usage_start_time) AS month FROM `{dataset}` WHERE service.description LIKE "%Compute Engine%" AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 180 day input: How much did I spend on vertex in the last month? output: SELECT SUM(cost) AS total_cost FROM `{dataset}` WHERE service.description LIKE "%Vertex% AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day input: How much did I spend on BQ over the last 6 months? output: SELECT SUM(cost) AS total_cost, FORMAT_DATE("%Y-%m", usage_start_time) AS month FROM `{dataset}` WHERE service.description LIKE "%BigQuery%" AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 180 day input: Write a BigQuery SQL query for: {user_query} output:""" CUSTOM_PROMPT = PromptTemplate( input_variables=["schema", "user_query", "dataset", "table"], template=TEMPLATE ) llm = VertexAI(model_name="code-bison", max_output_tokens=2048) db = SQLDatabase.from_uri(sqlalchemy_url) toolkit = SQLDatabaseToolkit(db=db, llm=llm) agent_executor = create_sql_agent( llm=llm, toolkit=toolkit, verbose=True, top_k=100, handle_parsing_errors=True, ) # in case it decides to spit out markdown JSON out = agent_executor.run(CUSTOM_PROMPT) st.write("LLM said: {}".format(out)) # st.write("Running query... \n```\n{}\n```".format(sql)) # out = run_query(sql) st.bar_chart(out) # st.write(out) user_query = st.chat_input("Ask me a question about your bill", on_submit=process_prompt, key="user_query")
[ "user_query", "%Compute Engine%", "%BigQuery%", "Only use the following tables:\n {table}.\n The schema of the table is: {schema}\n\n If accessing sub keys of a field, you must use UNNEST to flatten the data.\n\n where service.id is an unusable identifier and service.description is the name of the service\n use invoice.month for any data prior to the current month\n INTERVAL must be in days, not months or years\n\n You can not query nested fields, so e.g. SELECT `gcp_billing_export_v1_010767_AD0D5D_BCC8F6`.`project`.`number` is not a valid query\n \n Some examples of SQL queries that corrsespond to questions are:\n\n input: how much did I spend on compute in the last 90 days?\n output: SELECT\n sum(total_cost) as my_cost,\n FORMAT_DATE(\"%Y-%m\", usage_start_time) AS month,\n FROM `{dataset}`\n WHERE service.description LIKE \"%Compute%\"\n AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 90 day\n GROUP BY month\n \n input: how much did I spend in the last month?\n output: SELECT\n sum(total_cost) as my_cost,\n FORMAT_DATE(\"%Y-%m\", usage_start_time) AS month\n FROM `{dataset}`\n WHERE usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day\n GROUP BY month\n\n input: how much did I spend last month?\n output: SELECT\n sum(total_cost) as my_cost,\n FORMAT_DATE(\"%Y-%m\", usage_start_time) AS month\n FROM `{dataset}`\n WHERE usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 60 day\n AND usage_start_time <= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day\n GROUP BY month\n\n input: how much did I spend on compute over the last 6 months?\n output: SELECT\n sum(cost) as my_cost,\n FORMAT_DATE(\"%Y-%m\", usage_start_time) AS month\n FROM `{dataset}`\n WHERE service.description LIKE \"%Compute Engine%\" \n AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 180 day\n\n input: How much did I spend on vertex in the last month?\n output: SELECT SUM(cost) AS total_cost \n FROM `{dataset}`\n WHERE service.description LIKE \"%Vertex% \n AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 30 day\n\n input: How much did I spend on BQ over the last 6 months?\n output: SELECT SUM(cost) AS total_cost,\n FORMAT_DATE(\"%Y-%m\", usage_start_time) AS month\n FROM `{dataset}`\n WHERE service.description LIKE \"%BigQuery%\"\n AND usage_start_time >= TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), MONTH) - INTERVAL 180 day\n\n input: Write a BigQuery SQL query for: {user_query}\n output:" ]
2024-01-10
isce-framework/isce2
contrib~stack~alosStack~look_coherence.py
#!/usr/bin/env python3 # # Author: Cunren Liang # Copyright 2015-present, NASA-JPL/Caltech # import os import glob import shutil import datetime import numpy as np import xml.etree.ElementTree as ET import isce, isceobj from contrib.alos2proc.alos2proc import look from isceobj.Alos2Proc.Alos2ProcPublic import create_xml from isceobj.Alos2Proc.Alos2ProcPublic import runCmd from isceobj.Alos2Proc.runCoherence import coherence from StackPulic import loadProduct from StackPulic import stackDateStatistics def cmdLineParse(): ''' command line parser. ''' import sys import argparse parser = argparse.ArgumentParser(description='take more looks and compute coherence') parser.add_argument('-ref_date', dest='ref_date', type=str, required=True, help = 'reference date of this pair. format: YYMMDD') parser.add_argument('-sec_date', dest='sec_date', type=str, required=True, help = 'reference date of this pair. format: YYMMDD') parser.add_argument('-nrlks1', dest='nrlks1', type=int, default=1, help = 'number of range looks 1. default: 1') parser.add_argument('-nalks1', dest='nalks1', type=int, default=1, help = 'number of azimuth looks 1. default: 1') parser.add_argument('-nrlks2', dest='nrlks2', type=int, default=1, help = 'number of range looks 2. default: 1') parser.add_argument('-nalks2', dest='nalks2', type=int, default=1, help = 'number of azimuth looks 2. default: 1') if len(sys.argv) <= 1: print('') parser.print_help() sys.exit(1) else: return parser.parse_args() if __name__ == '__main__': inps = cmdLineParse() #get user parameters from input dateReference = inps.ref_date dateSecondary = inps.sec_date numberRangeLooks1 = inps.nrlks1 numberAzimuthLooks1 = inps.nalks1 numberRangeLooks2 = inps.nrlks2 numberAzimuthLooks2 = inps.nalks2 ####################################################### pair = '{}-{}'.format(dateReference, dateSecondary) ml1 = '_{}rlks_{}alks'.format(numberRangeLooks1, numberAzimuthLooks1) ml2 = '_{}rlks_{}alks'.format(numberRangeLooks1*numberRangeLooks2, numberAzimuthLooks1*numberAzimuthLooks2) insarDir = 'insar' os.makedirs(insarDir, exist_ok=True) os.chdir(insarDir) amplitude = pair + ml1 + '.amp' differentialInterferogram = 'diff_' + pair + ml1 + '.int' multilookAmplitude = pair + ml2 + '.amp' multilookDifferentialInterferogram = 'diff_' + pair + ml2 + '.int' multilookCoherence = pair + ml2 + '.cor' amp = isceobj.createImage() amp.load(amplitude+'.xml') width = amp.width length = amp.length width2 = int(width / numberRangeLooks2) length2 = int(length / numberAzimuthLooks2) if not ((numberRangeLooks2 == 1) and (numberAzimuthLooks2 == 1)): #take looks look(differentialInterferogram, multilookDifferentialInterferogram, width, numberRangeLooks2, numberAzimuthLooks2, 4, 0, 1) look(amplitude, multilookAmplitude, width, numberRangeLooks2, numberAzimuthLooks2, 4, 1, 1) #creat xml create_xml(multilookDifferentialInterferogram, width2, length2, 'int') create_xml(multilookAmplitude, width2, length2, 'amp') if (numberRangeLooks1*numberRangeLooks2*numberAzimuthLooks1*numberAzimuthLooks2 >= 9): cmd = "imageMath.py -e='sqrt(b_0*b_1);abs(a)/(b_0+(b_0==0))/(b_1+(b_1==0))*(b_0!=0)*(b_1!=0)' --a={} --b={} -o {} -t float -s BIL".format( multilookDifferentialInterferogram, multilookAmplitude, multilookCoherence) runCmd(cmd) else: #estimate coherence using a moving window coherence(multilookAmplitude, multilookDifferentialInterferogram, multilookCoherence, method="cchz_wave", windowSize=5) os.chdir('../')
[]
2024-01-10
SmittieC/doc-util
create_doc.py
import os import openai openai.api_key=os.environ['OPENAI_API_KEY'] def create_doc(): system_message = "You are a professional technical documentation writer. The user will provide text from which you should create a documentation" model = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "system", "content": system_message}, {"role": "user", "content": _read_text()}]) response = model['choices'][0]['message']['content'] _write(response) def _write(text): with open("doc.txt", "w") as file: file.write(text) def _read_text(): current_path = os.getcwd() file_name = "text.txt" file_path = os.path.join(current_path, file_name) with open(file_path, "r") as file: return file.read() if __name__ == "__main__": create_doc()
[ "You are a professional technical documentation writer. The user will provide text from which you should create a documentation" ]
2024-01-10
isro01/Conv_bot
models~LDA~model~train.py
import numpy as np import matplotlib.pyplot as plt import nltk import re import pandas as pd # from pprint import pprin #for pretty print ## Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel ## spacy for lemmatization import spacy import warnings warnings.filterwarnings("ignore" ,category= DeprecationWarning) nltk.download('stopwords') from nltk.corpus import stopwords stop_words = stopwords.words('english') extended_stopwords_list = ['from' , 'subject' ,'re' , 'edu' ,'use' ] #this depend on dataset stop_words.extend(extended_stopwords_list) df = pd.read_json('../dataset/newsgroups.json') print("some examples in dataset" , df.head()) data = df.content.values.tolist() print(data[:2]) def preprocess_data(data): data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] data = [re.sub('\s+', ' ', sent) for sent in data] data = [re.sub("\'", "", sent) for sent in data] return data data = preprocess_data(data) print(data[:4]) ## remove puntuation and unnecessary words using gensim simple preprocess def gensim_preprocess(data): for line in data: yield(gensim.utils.simple_preprocess(str(line), deacc=True)) # deacc=True removes punctuations data = list(gensim_preprocess(data)) print(data[:4]) print(type(data)) # Build the bigram and trigram models bigram = gensim.models.Phrases(data, min_count=5, threshold=100) # higher threshold fewer phrases. trigram = gensim.models.Phrases(bigram[data], threshold=100) # Faster way to get a sentence clubbed as a trigram/bigram bigram_mod = gensim.models.phrases.Phraser(bigram) trigram_mod = gensim.models.phrases.Phraser(trigram) print(trigram_mod[bigram_mod[data[0]]]) def remove_stopwords(data): for line in data: line = [word for word in line if word not in stop_words] yield(line) def make_bigrams(data): return [bigram_mod[line] for line in data] def make_trigrams(data): return [trigram_mod[bigram_mod[line]] for line in texts] def lemmatization(texts, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']): """https://spacy.io/api/annotation""" texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) return texts_out data = list(remove_stopwords(data)) data = make_bigrams(data) # Initialize spacy 'en' model, keeping only tagger component (for efficiency) # !python3 -m spacy download en nlp = spacy.load('en', disable=['parser', 'ner']) data_lemmatized = lemmatization(data, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV']) print(data_lemmatized[:2]) index_to_word = corpora.Dictionary(data_lemmatized) #using gensim api to make dictionary corpus = [index_to_word.doc2bow(line) for line in data_lemmatized] print("Now training your lda model") # Build LDA model lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=index_to_word, num_topics=20, random_state=100, update_every=1, chunksize=100, passes=10, alpha='auto', per_word_topics=True) model_dir = '../lda_checkpoint' if not os.path.isdir(model_dir): os.mkdir(model_dir) path = os.path.join(model_dir, 'topic_model.lda') lda_model.save(path) print("LDA MODEL SAVED SUCCESSFULLY")
[]
2024-01-10
isro01/Conv_bot
chatbot.py
import os import numpy as np import matplotlib.pyplot as plt from gtts import gTTS import argparse from keras.layers import Bidirectional, Concatenate, Permute, Dot, Input, LSTM, Multiply , Embedding from keras.layers import RepeatVector, Dense, Activation, Lambda from keras.optimizers import Adam from keras.utils import to_categorical from keras.models import load_model, Model import keras.backend as K # %matplotlib inline ## Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.models.ldamodel import LdaModel import tensorflow as tf #import tensorflow_addons as tfa print(tf.__version__) from sklearn.model_selection import train_test_split import json import os import pickle import io import re import unicodedata import urllib3 import shutil import zipfile import itertools from string import digits import matplotlib.ticker as ticker import unicodedata import time import speech_recognition as sr from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences def preprocess_sentence(sentence): num_digits= str.maketrans('','', digits) sentence= sentence.lower() sentence= re.sub(" +", " ", sentence) sentence= re.sub("'", '', sentence) sentence= sentence.translate(num_digits) sentence= re.sub(r"([?.!,¿])", r" \1 ", sentence) sentence = sentence.rstrip().strip() sentence = "<start> " + sentence + " <end>" return sentence max_len = 20 trunc_token = 'post' oov_tok = "<OOV>" word_index = pickle.load(open(os.getcwd() + "/dic/word_index.pkl", "rb")) index_word = pickle.load(open(os.getcwd() + "/dic/index_word.pkl", "rb")) # word_index = json.load( open( os.getcwd() + "/dic/word_index.json" ) ) # index_word = json.load( open( os.getcwd() + "/dic/index_word.json" ) ) # print(type(word_index)) # print(index_word[10]) # embeddingMatrix path = os.getcwd() + '/lda_checkpoint/topic_model.lda' lda_model = LdaModel.load(path) import spacy nlp = spacy.load('en', disable=['parser', 'ner']) # !python3 -m spacy download en class LDA(tf.keras.layers.Layer): def __init__(self, lda_model,index_to_word): super(LDA, self).__init__(trainable= False, dynamic = True) self.lda_model = lda_model self.index_to_word = index_to_word def build(self, input_shape): return def gensim_preprocess(self ,lda_model , data): data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] data = [re.sub('\s+', ' ', sent) for sent in data] data = [re.sub("\'", "", sent) for sent in data] new = [] for line in data: new.append(gensim.utils.simple_preprocess(str(line), deacc=True)) allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'] texts_out = [] for sent in new: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) # print(texts_out) corpus = [lda_model.id2word.doc2bow(line) for line in texts_out] return corpus def get_config(self): return { 'lda_model' : self.lda_model, 'index_to_word' : self.index_to_word, } def call(self, inp): batch_size , time_steps = inp.shape data = [] for i in range(batch_size): line = "" for j in range(1,time_steps): if inp[i][j].numpy() != 0: if index_word[int(inp[i][j].numpy())] == '<end>': break; line = line + self.index_to_word[int(inp[i][j].numpy())] data.append(line) data = self.gensim_preprocess(self.lda_model ,data) predictions = self.lda_model.get_document_topics(data , minimum_probability = 0.0) x = [] for i in range(batch_size): x.append((tf.convert_to_tensor(list(predictions[i]),dtype='float32'))[:,1]) x = tf.convert_to_tensor(x ,dtype='float32') return x def compute_output_shape(self, input_shape): return (batch_size, 20) BATCH_SIZE = 1 embedding_dim = 50 units = 128 vocab_inp_size = len(word_index)+1 vocab_tar_size = len(word_index)+1 # tf.keras.backend.set_floatx('float64') class Encoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz, lda_model , index_word): super(Encoder, self).__init__() self.batch_sz = batch_sz self.enc_units = enc_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , weights =[embeddingMatrix], trainable=False) self.gru = tf.keras.layers.GRU(self.enc_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') self.topic_awareness = LDA(lda_model = lda_model , index_to_word = index_word) def call(self, x, hidden): topic_vector = self.topic_awareness.call(x) x = self.embedding(x) output, state = self.gru(x, initial_state = hidden) return output, state, topic_vector def initialize_hidden_state(self): return tf.zeros((self.batch_sz, self.enc_units)) encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE , lda_model , index_word) class BahdanauAttention(tf.keras.layers.Layer): def __init__(self, units): super(BahdanauAttention, self).__init__() self.W1 = tf.keras.layers.Dense(units) self.W2 = tf.keras.layers.Dense(units) self.V = tf.keras.layers.Dense(1) def call(self, query, values): # query hidden state shape == (batch_size, hidden size) # query_with_time_axis shape == (batch_size, 1, hidden size) # values shape == (batch_size, max_len, hidden size) # we are doing this to broadcast addition along the time axis to calculate the score query_with_time_axis = tf.expand_dims(query, 1) # score shape == (batch_size, max_length, 1) # we get 1 at the last axis because we are applying score to self.V # the shape of the tensor before applying self.V is (batch_size, max_length, units) score = self.V(tf.nn.tanh( self.W1(query_with_time_axis) + self.W2(values))) # attention_weights shape == (batch_size, max_length, 1) attention_weights = tf.nn.softmax(score, axis=1) # context_vector shape after sum == (batch_size, hidden_size) context_vector = attention_weights * values context_vector = tf.reduce_sum(context_vector, axis=1) return context_vector, attention_weights class Decoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz): super(Decoder, self).__init__() self.batch_sz = batch_sz self.dec_units = dec_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , , weights =[embeddingMatrix], trainable=False) self.gru = tf.keras.layers.GRU(self.dec_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') self.fc = tf.keras.layers.Dense(vocab_size , activation = "softmax") # used for attention self.attention = BahdanauAttention(self.dec_units) def call(self, x, hidden, enc_output ,topic_vector): # enc_output shape == (batch_size, max_length, hidden_size) attention_vector, attention_weights = self.attention(hidden, enc_output) context_vector = tf.concat([attention_vector, topic_vector], axis = -1) # x shape after passing through embedding == (batch_size, 1, embedding_dim) x = self.embedding(x) # x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size) x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1) # passing the concatenated vector to the GRU output, state = self.gru(x , initial_state = hidden) # output shape == (batch_size * 1, hidden_size) output = tf.reshape(output, (-1, output.shape[2])) # output shape == (batch_size, vocab) x = self.fc(output) return x, state, attention_weights decoder = Decoder(vocab_tar_size , embedding_dim, units, BATCH_SIZE) optimizer = tf.keras.optimizers.Adam() checkpoint_dir = './training_checkpoints' checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") # if not os.path.exists(checkpointdir): # os.mkdir(checkpoint_dir) checkpoint = tf.train.Checkpoint(optimizer=optimizer, encoder=encoder, decoder=decoder) try: status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)) print("Checkpoint found at {}".format(tf.train.latest_checkpoint(checkpoint_dir))) except: print("No checkpoint found at {}".format(checkpoint_dir)) def evaluate(sentence): attention_plot = np.zeros((max_len, max_len)) sentence = preprocess_sentence(sentence) inputs = [word_index[i] for i in sentence.split(' ')] inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], maxlen=max_len, padding='post') inputs = tf.convert_to_tensor(inputs) result = '' hidden = [tf.zeros((1, units))] enc_out, enc_hidden , topic_vector = encoder(inputs, hidden) dec_hidden = enc_hidden dec_input = tf.expand_dims([word_index['<start>']], 0) for t in range(max_len): predictions, dec_hidden, attention_weights = decoder(dec_input, dec_hidden, enc_out , topic_vector) # storing the attention weights to plot later on attention_weights = tf.reshape(attention_weights, (-1, )) attention_plot[t] = attention_weights.numpy() predicted_id = tf.argmax(predictions[0]).numpy() result += index_word[predicted_id] + ' ' if index_word[predicted_id] == '<end>': return result, sentence, attention_plot # the predicted ID is fed back into the model dec_input = tf.expand_dims([predicted_id], 0) return result, sentence, attention_plot # function for plotting the attention weights def plot_attention(attention, sentence, predicted_sentence): fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(1, 1, 1) ax.matshow(attention, cmap='viridis') fontdict = {'fontsize': 14} ax.set_xticklabels([''] + sentence, fontdict=fontdict, rotation=90) ax.set_yticklabels([''] + predicted_sentence, fontdict=fontdict) ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) ax.yaxis.set_major_locator(ticker.MultipleLocator(1)) plt.show() def response(sentence): result, sentence, attention_plot = evaluate(sentence) x =0 # print('Input: %s' % (sentence)) if debug == True: print('Response: {}'.format(result) return result # attention_plot = attention_plot[:len(result.split(' ')), :len(sentence.split(' '))] # plot_attention(attention_plot, sentence.split(' '), result.split(' ')) batch_size = 1 Ty = 20 # def beam_search_decoder(sentence , beam_width = 3): # sentence = preprocess_sentence(sentence) # inputs = [word_index[i] for i in sentence.split(' ')] # inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], # maxlen=max_len, # padding='post') # inputs = tf.convert_to_tensor(inputs) # predictions = [([],0)] * beam_width # # predictions = tf.convert_to_tensor(predictions) # enc_hidden = tf.zeros((1,units)) # encoder_output , encoder_hidden , topic_vector = encoder(inputs , enc_hidden) # decoder_hidden = [encoder_hidden] * 3 # # decoder_hidden = [decoder_hidden] * beam_width # decoder_input = [tf.expand_dims([word_index['<start>'] ], 0)] * beam_width # # print("decoder_input" ,decoder_input[0]) # print("decoder_hiddem:{} \n decoder_input: {} \n encoder_hidden :{} \n topic_vector {} \n".format(decoder_hidden[0].shape , decoder_input[0].shape , encoder_output.shape , topic_vector.shape)) # for t in range(Ty): # current_predictions = [] # for i in range(beam_width): # out , decoder_hidden[i] , _ = decoder(decoder_input[i] , decoder_hidden[i] , encoder_output , topic_vector) # # print("not done") # # print(out) # index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) # prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') # # print("index: {} prob: {}".format((index[0]) , prob[0])); # # print("done") # # print(int(index[0][0])) # # print(int(index[0][1])) # # print(int(index[0][2])) # # print("done" ,current_predictions) # for j in range(beam_width): # if t ==0 and i ==1 : # continue # # print("do nothing!") # elif t == 0 and i ==2: # continue # # print("do nothing!") # else : # current_predictions.append((predictions[i][0] + [int(index[0][j])] , np.log(prob[0][j]) + predictions[i][1])) # # if t == 0 and i ==0 : # # print(current_predictions) # # print(count) # # print(current_predictions) # def get_prob(pred): # # print(pred[1]) # return pred[1] # # print(current_predictions) # current_predictions = sorted(current_predictions , key = get_prob , reverse =True) # # print(current_predictions) # predictions = current_predictions[:beam_width] # # print(predictions) # current_predictions = [pred[0] for pred in current_predictions] # print(current_predictions[0]) # # print(current_predictions[1]) # decoder_input = [tf.expand_dims([tf.convert_to_tensor(pred[t])],0) for pred in current_predictions] # print(decoder_input[0]) # output = [] # for pred , prob in predictions: # out = [] # s = "" # for p in pred: # if index_word[p] != "<end>": # s += " " + index_word[p] # else : # break # output.append(s) # prob = [pred[1] / len(output[i]) for i , pred in enumerate(predictions)] # # for i in range(beam_width): # print("resposne :{} \n {} \n {} ".format(output[0] , output[1] ,output[2])) # return output , prob def beam_search_decoder(sentence , beam_width = 3): sentence = preprocess_sentence(sentence) inputs = [word_index[i] for i in sentence.split(' ')] inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], maxlen=max_len, padding='post') inputs = tf.convert_to_tensor(inputs) predictions = [] # predictions = tf.convert_to_tensor(predictions) enc_hidden = tf.zeros((1,128)) encoder_output , encoder_hidden , topic_vector = encoder(inputs , enc_hidden) decoder_hidden = encoder_hidden # decoder_hidden = [decoder_hidden] * beam_width decoder_input = tf.expand_dims([word_index['<start>'] ], 0) # print("decoder_input" ,decoder_input[0]) # print("decoder_hiddem:{} \n decoder_input: {} \n encoder_output :{} \n topic_vector {} \n".format(decoder_hidden.shape , decoder_input.shape , encoder_output.shape , topic_vector.shape)) out , hidden, _ = decoder(decoder_input , decoder_hidden , encoder_output , topic_vector) index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') terminal_sentences, decoder_hidden, predictions = [], [], [] decoder_hidden = [hidden] * 3 # print(decoder_hidden) for i in range(beam_width): predictions.append(([int(index[0][i])], np.log(prob[0][i]))) # print(predictions[0][0]) decoder_input = [tf.expand_dims(tf.convert_to_tensor(pred[0]),0) for pred in predictions] # print(decoder_input[0]) for t in range(1,Ty): current_predictions = [] for i in range(beam_width): out , decoder_hidden[i] , _ = decoder(decoder_input[i] , decoder_hidden[i] , encoder_output , topic_vector) # print("once") index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') for j in range(beam_width): current_predictions.append((predictions[i][0] + [int(index[0][j])] , np.log(prob[0][j]) + predictions[i][1] , i)) def get_prob(pred): return pred[1] current_predictions = sorted(current_predictions , key = get_prob , reverse =True) current_predictions = current_predictions[:beam_width] # print("time_step {} {}".format(t ,current_predictions)) hidden = [] inputs = [] pred = [] for j in range(beam_width): if index_word[current_predictions[j][0][t]] == "<end>": beam_width -= 1 terminal_sentences.append((current_predictions[j][0] , current_predictions[j][1])) else : hidden.append(decoder_hidden[current_predictions[j][2]]) inputs.append(tf.expand_dims([tf.convert_to_tensor(current_predictions[j][0][t])],0) ) pred.append((current_predictions[j][0] , current_predictions[j][1])) decoder_hidden = hidden decoder_input = inputs predictions = pred # print(decoder_input) if beam_width <= 0 : break for x in range(len(predictions)): terminal_sentences.append((predictions[x][0],predictions[x][1])) terminal_sentences = sorted(terminal_sentences , key = get_prob , reverse =True) output = [] for pred , prob in terminal_sentences: out = [] s = "" for p in pred: if index_word[p] != "<end>": s += " " + index_word[p] else : break output.append(s) prob = [pred[1] / len(output[i]) for i , pred in enumerate(terminal_sentences)] # for i in range(beam_width): if debug == True: print("resposne :{} {} \n {} {} \n {} {} ".format(output[0] , prob[0] , output[1] ,prob[1] ,output[2] , prob[2])) return output , prob if __name__ == '__main__': parser = argparse.ArgumentParser(description='Conversational Bot') parser.add_argument('-d', '--debug', type = bool, default= False, help='set debug value') args = parser.parse_args() # debug = True debug = args.debug while True: result = input("> ") out = response(result) # beam_search_decoder(result)
[]
2024-01-10
isro01/Conv_bot
models~ta-seq2seq~model~train.py
import os import numpy as np import matplotlib.pyplot as plt from keras.layers import Bidirectional, Concatenate, Permute, Dot, Input, LSTM, Multiply , Embedding from keras.layers import RepeatVector, Dense, Activation, Lambda from keras.optimizers import Adam from keras.utils import to_categorical from keras.models import load_model, Model import keras.backend as K # from faker import Faker import random from tqdm import tqdm # from babel.dates import format_date import matplotlib.pyplot as plt ## Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.models.ldamodel import LdaModel import tensorflow as tf #import tensorflow_addons as tfa print(tf.__version__) from sklearn.model_selection import train_test_split import os import io import numpy as np import re import unicodedata import urllib3 import shutil import zipfile import itertools import pickle from string import digits import matplotlib.pyplot as plt import matplotlib.ticker as ticker from sklearn.model_selection import train_test_split import unicodedata import time from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences file_path = '../dataset/cleaned_opensubtitles' os.chdir(file_path) def preprocess_sentence(sentence): num_digits= str.maketrans('','', digits) sentence= sentence.lower() sentence= re.sub(" +", " ", sentence) sentence= re.sub("'", '', sentence) sentence= sentence.translate(num_digits) sentence= re.sub(r"([?.!,¿])", r" \1 ", sentence) sentence = sentence.rstrip().strip() sentence = "<start> " + sentence + " <end>" return sentence text =[] count = 0 for file in os.listdir(): with open(file ,'r' , encoding='iso-8859-1') as txtfile: for line in txtfile.readlines(): if count == 100000: break text.append(preprocess_sentence(line)) count += 1 max_len = 15 trunc_token = 'post' oov_tok = "<OOV>" vocab_size = 20000 tokenizer = Tokenizer(num_words = vocab_size ,oov_token=oov_tok , filters = "" ) tokenizer.fit_on_texts(text) word_index = tokenizer.word_index print(len(word_index)) sequences = tokenizer.texts_to_sequences(text) padded = pad_sequences(sequences,maxlen=max_len, truncating=trunc_token,padding = 'post') d = {} for index , word in enumerate(word_index.keys()): if index + 1 == vocab_size: break d[word] = index + 1 word_index = d index_word = {} for word , index in word_index.items(): index_word[index] = word os.chdir("../") a_file = open("../dic/word_index.pkl", "wb") pickle. dump(word_index, a_file) a_file. close() a_file = open("../dic/index_word.pkl", "wb") pickle. dump(word_index, a_file) a_file. close() def create_word_embeddings(file_path): with open(file_path , 'r') as f: wordToEmbedding = {} wordToIndex = {} indexToWord = {} for line in f: data = line.strip().split() token = data[0] wordToEmbedding[token] = np.array(data[1:] ,dtype = np.float64) tokens = sorted(wordToEmbedding.keys()) for idx , token in enumerate(tokens): idx = idx + 1 #for zero masking wordToIndex[token] = idx indexToWord[idx] = token return wordToEmbedding , wordToIndex , indexToWord wordToEmbedding , wordToIndex , indexToWord = create_word_embeddings('../pretrained_word_embeddings/embedding.txt') def create_pretrained_embedding_layer(wordToEmbedding , wordToIndex , indexToWord): vocablen = len(word_index)+1 #for zero masking embedding_dimensions = 100 embeddingMatrix = np.zeros((vocablen , embedding_dimensions)) count = 0 for word , index in word_index.items(): if word not in wordToEmbedding.keys(): embeddingMatrix[index ,:] = np.random.uniform(low = -1 , high =1 ,size = (1,100)) count +=1 else : embeddingMatrix[index , :] = wordToEmbedding[word] embeddingLayer = Embedding(vocablen , embedding_dimensions , weights = [embeddingMatrix] , trainable = False) print(embeddingMatrix.shape) print(count) return embeddingMatrix embeddingMatrix = create_pretrained_embedding_layer(wordToEmbedding , wordToIndex , indexToWord) np.save('../dic/embedding.npy', embeddingMatrix) path = r'../../../lda_checkpoint/topic_model.lda' lda_model = LdaModel.load(path) import spacy nlp = spacy.load('en', disable=['parser', 'ner']) # !python3 -m spacy download en class LDA(tf.keras.layers.Layer): def __init__(self, lda_model,index_to_word): super(LDA, self).__init__(trainable= False, dynamic = True) self.lda_model = lda_model self.index_to_word = index_to_word def build(self, input_shape): return def gensim_preprocess(self ,lda_model , data): data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] data = [re.sub('\s+', ' ', sent) for sent in data] data = [re.sub("\'", "", sent) for sent in data] new = [] for line in data: new.append(gensim.utils.simple_preprocess(str(line), deacc=True)) allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'] texts_out = [] for sent in new: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) # print(texts_out) corpus = [lda_model.id2word.doc2bow(line) for line in texts_out] return corpus def get_config(self): return { 'lda_model' : self.lda_model, 'index_to_word' : self.index_to_word, } def call(self, inp): batch_size , time_steps = inp.shape data = [] for i in range(batch_size): line = "" for j in range(1,time_steps): if inp[i][j].numpy() != 0: if index_word[int(inp[i][j].numpy())] == '<end>': break; line = line + self.index_to_word[int(inp[i][j].numpy())] data.append(line) data = self.gensim_preprocess(self.lda_model ,data) predictions = self.lda_model.get_document_topics(data , minimum_probability = 0.0) x = [] for i in range(batch_size): x.append((tf.convert_to_tensor(list(predictions[i]),dtype='float32'))[:,1]) x = tf.convert_to_tensor(x ,dtype='float32') return x def compute_output_shape(self, input_shape): return (batch_size, 20) def get_dataset(padded): index = 0 context = np.zeros((50000,max_len) ,dtype='float32') response = np.zeros((50000,max_len),dtype= 'float32') for idx in range(0,100000,2): context[index,:] = padded[idx] response[index,:] = padded[idx+1] index +=1 return context , response context , response = get_dataset(padded) BUFFER_SIZE = len(context) BATCH_SIZE = 100 steps_per_epoch = len(context)//BATCH_SIZE embedding_dim = 100 units = 512 vocab_inp_size = len(word_index) + 1 vocab_tar_size = len(word_index) + 1 # tf.keras.backend.set_floatx('float64') dataset = tf.data.Dataset.from_tensor_slices((context, response )).shuffle(BUFFER_SIZE) dataset = dataset.batch(BATCH_SIZE, drop_remainder=True) class Encoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz, lda_model , index_word): super(Encoder, self).__init__() self.batch_sz = batch_sz self.enc_units = enc_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , weights =[embeddingMatrix], trainable=False) self.gru = tf.keras.layers.GRU(self.enc_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') # self.topic_awareness = LDA(lda_model = lda_model , index_to_word = index_word) def call(self, x, hidden): # topic_vector = self.topic_awareness.call(x) x = self.embedding(x) output, state = self.gru(x, initial_state = hidden) return output, state def initialize_hidden_state(self): return tf.zeros((self.batch_sz, self.enc_units)) encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE , lda_model , index_word) class BahdanauAttention(tf.keras.layers.Layer): def __init__(self, units): super(BahdanauAttention, self).__init__() self.W1 = tf.keras.layers.Dense(units) self.W2 = tf.keras.layers.Dense(units) self.V = tf.keras.layers.Dense(1) def call(self, query, values): # query hidden state shape == (batch_size, hidden size) # query_with_time_axis shape == (batch_size, 1, hidden size) # values shape == (batch_size, max_len, hidden size) # we are doing this to broadcast addition along the time axis to calculate the score query_with_time_axis = tf.expand_dims(query, 1) # score shape == (batch_size, max_length, 1) # we get 1 at the last axis because we are applying score to self.V # the shape of the tensor before applying self.V is (batch_size, max_length, units) score = self.V(tf.nn.tanh( self.W1(query_with_time_axis) + self.W2(values))) # attention_weights shape == (batch_size, max_length, 1) attention_weights = tf.nn.softmax(score, axis=1) # context_vector shape after sum == (batch_size, hidden_size) context_vector = attention_weights * values context_vector = tf.reduce_sum(context_vector, axis=1) return context_vector, attention_weights attention_layer = BahdanauAttention(10) class Decoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz): super(Decoder, self).__init__() self.batch_sz = batch_sz self.dec_units = dec_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , weights =[embeddingMatrix], trainable=False ) self.gru = tf.keras.layers.GRU(self.dec_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') self.fc = tf.keras.layers.Dense(vocab_size) # used for attention self.attention = BahdanauAttention(self.dec_units) def call(self, x, hidden, enc_output): # enc_output shape == (batch_size, max_length, hidden_size) attention_vector, attention_weights = self.attention(hidden, enc_output) # context_vector = tf.concat([attention_vector, topic_vector], axis = -1) # x shape after passing through embedding == (batch_size, 1, embedding_dim) x = self.embedding(x) # x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size) x = tf.concat([tf.expand_dims(attention_vector, 1), x], axis=-1) # passing the concatenated vector to the GRU output, state = self.gru(x , initial_state = hidden) # output shape == (batch_size * 1, hidden_size) output = tf.reshape(output, (-1, output.shape[2])) # output shape == (batch_size, vocab) x = self.fc(output) return x, state, attention_weights decoder = Decoder(vocab_tar_size , embedding_dim, units, BATCH_SIZE) optimizer = tf.keras.optimizers.Adam(lr = 0.005, beta_1 = 0.9,beta_2 = 0.999 , decay = 0.01) loss_object = tf.keras.losses.SparseCategoricalCrossentropy( from_logits=True, reduction='none') train_accuracy = tf.metrics.SparseCategoricalAccuracy() def loss_function(real, pred): mask = tf.math.logical_not(tf.math.equal(real, 0)) loss_ = loss_object(real, pred) mask = tf.cast(mask, dtype=loss_.dtype) loss_ *= mask return tf.reduce_mean(loss_) checkpoint_dir = '../checkpoints' checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") if not os.path.exists(checkpoint_dir): os.mkdir(checkpoint_dir) checkpoint = tf.train.Checkpoint(optimizer=optimizer, encoder=encoder, decoder=decoder) try: status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)) print("Checkpoint found at {}".format(tf.train.latest_checkpoint(checkpoint_dir))) except: print("No checkpoint found at {}".format(checkpoint_dir)) @tf.function def train_step(inp, targ, enc_hidden): loss = 0 accu = 0 with tf.GradientTape() as tape: # print(tf.executing_eagerly()) enc_output, enc_hidden = encoder(inp, enc_hidden) # print(enc_hidden) dec_hidden = enc_hidden dec_input = tf.expand_dims([word_index['<start>']] * BATCH_SIZE, 1) # Teacher forcing - feeding the target as the next input for t in range(1, targ.shape[1]): # passing enc_output to the decoder predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, enc_output) # print(predictions) # print(tf.argmax(predictions,1).shape , targ[:,t].shape) loss += loss_function(targ[:, t], predictions) train_accuracy.update_state(targ[:,t] , predictions) accu += train_accuracy.result() # print("accu: ",train_accuracy.result()) # using teacher forcing dec_input = tf.expand_dims(targ[:, t], 1) batch_loss = (loss / int(targ.shape[1])) batch_accu = (accu / int(targ.shape[1])) variables = encoder.trainable_variables + decoder.trainable_variables gradients = tape.gradient(loss, variables) optimizer.apply_gradients(zip(gradients, variables)) return batch_loss, batch_accu import time EPOCHS = 10 for epoch in range(EPOCHS): start = time.time() enc_hidden = encoder.initialize_hidden_state() total_loss = 0 total_accu = 0 for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)): batch_loss , batch_accu = train_step(inp, targ, enc_hidden) total_loss += batch_loss total_accu += batch_accu if batch % 2 == 0: print('Epoch {} Batch {} Loss {:.4f} Accuracy {:,.4f}'.format(epoch + 1, batch, batch_loss.numpy(), batch_accu.numpy())) print('Epoch {} Loss {:.4f} Accuracy {:.4f}'.format(epoch + 1, total_loss / steps_per_epoch, total_accu / steps_per_epoch)) print('Time taken for 1 epoch {} sec\n'.format(time.time() - start)) if (epoch +1) %10 == 0: checkpoint.save(file_prefix = checkpoint_prefix) checkpoint.save(file_prefix = checkpoint_prefix) print("checkpoints are saved")
[]
2024-01-10
isro01/Conv_bot
conversational_bot.py
import os import numpy as np import matplotlib.pyplot as plt from gtts import gTTS import argparse from keras.layers import Bidirectional, Concatenate, Permute, Dot, Input, LSTM, Multiply , Embedding from keras.layers import RepeatVector, Dense, Activation, Lambda from keras.optimizers import Adam from keras.utils import to_categorical from keras.models import load_model, Model import keras.backend as K # %matplotlib inline ## Gensim import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess from gensim.models import CoherenceModel from gensim.models.ldamodel import LdaModel import tensorflow as tf #import tensorflow_addons as tfa print(tf.__version__) from sklearn.model_selection import train_test_split import json import os import pickle import io import re import unicodedata import urllib3 import shutil import zipfile import itertools from string import digits import matplotlib.ticker as ticker import unicodedata import time import speech_recognition as sr from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences def preprocess_sentence(sentence): num_digits= str.maketrans('','', digits) sentence= sentence.lower() sentence= re.sub(" +", " ", sentence) sentence= re.sub("'", '', sentence) sentence= sentence.translate(num_digits) sentence= re.sub(r"([?.!,¿])", r" \1 ", sentence) sentence = sentence.rstrip().strip() sentence = "<start> " + sentence + " <end>" return sentence max_len = 20 trunc_token = 'post' oov_tok = "<OOV>" word_index = pickle.load(open(os.getcwd() + "/dic/word_index.pkl", "rb")) index_word = pickle.load(open(os.getcwd() + "/dic/index_word.pkl", "rb")) # word_index = json.load( open( os.getcwd() + "/dic/word_index.json" ) ) # index_word = json.load( open( os.getcwd() + "/dic/index_word.json" ) ) # print(type(word_index)) # print(index_word[10]) # embeddingMatrix path = os.getcwd() + '/lda_checkpoint/topic_model.lda' lda_model = LdaModel.load(path) import spacy nlp = spacy.load('en', disable=['parser', 'ner']) # !python3 -m spacy download en class LDA(tf.keras.layers.Layer): def __init__(self, lda_model,index_to_word): super(LDA, self).__init__(trainable= False, dynamic = True) self.lda_model = lda_model self.index_to_word = index_to_word def build(self, input_shape): return def gensim_preprocess(self ,lda_model , data): data = [re.sub('\S*@\S*\s?', '', sent) for sent in data] data = [re.sub('\s+', ' ', sent) for sent in data] data = [re.sub("\'", "", sent) for sent in data] new = [] for line in data: new.append(gensim.utils.simple_preprocess(str(line), deacc=True)) allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'] texts_out = [] for sent in new: doc = nlp(" ".join(sent)) texts_out.append([token.lemma_ for token in doc if token.pos_ in allowed_postags]) # print(texts_out) corpus = [lda_model.id2word.doc2bow(line) for line in texts_out] return corpus def get_config(self): return { 'lda_model' : self.lda_model, 'index_to_word' : self.index_to_word, } def call(self, inp): batch_size , time_steps = inp.shape data = [] for i in range(batch_size): line = "" for j in range(1,time_steps): if inp[i][j].numpy() != 0: if index_word[int(inp[i][j].numpy())] == '<end>': break; line = line + self.index_to_word[int(inp[i][j].numpy())] data.append(line) data = self.gensim_preprocess(self.lda_model ,data) predictions = self.lda_model.get_document_topics(data , minimum_probability = 0.0) x = [] for i in range(batch_size): x.append((tf.convert_to_tensor(list(predictions[i]),dtype='float32'))[:,1]) x = tf.convert_to_tensor(x ,dtype='float32') return x def compute_output_shape(self, input_shape): return (batch_size, 20) BATCH_SIZE = 1 embedding_dim = 50 units = 128 vocab_inp_size = len(word_index)+1 vocab_tar_size = len(word_index)+1 # tf.keras.backend.set_floatx('float64') class Encoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz, lda_model , index_word): super(Encoder, self).__init__() self.batch_sz = batch_sz self.enc_units = enc_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , weights =[embeddingMatrix], trainable=False) self.gru = tf.keras.layers.GRU(self.enc_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') self.topic_awareness = LDA(lda_model = lda_model , index_to_word = index_word) def call(self, x, hidden): topic_vector = self.topic_awareness.call(x) x = self.embedding(x) output, state = self.gru(x, initial_state = hidden) return output, state, topic_vector def initialize_hidden_state(self): return tf.zeros((self.batch_sz, self.enc_units)) encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE , lda_model , index_word) class BahdanauAttention(tf.keras.layers.Layer): def __init__(self, units): super(BahdanauAttention, self).__init__() self.W1 = tf.keras.layers.Dense(units) self.W2 = tf.keras.layers.Dense(units) self.V = tf.keras.layers.Dense(1) def call(self, query, values): # query hidden state shape == (batch_size, hidden size) # query_with_time_axis shape == (batch_size, 1, hidden size) # values shape == (batch_size, max_len, hidden size) # we are doing this to broadcast addition along the time axis to calculate the score query_with_time_axis = tf.expand_dims(query, 1) # score shape == (batch_size, max_length, 1) # we get 1 at the last axis because we are applying score to self.V # the shape of the tensor before applying self.V is (batch_size, max_length, units) score = self.V(tf.nn.tanh( self.W1(query_with_time_axis) + self.W2(values))) # attention_weights shape == (batch_size, max_length, 1) attention_weights = tf.nn.softmax(score, axis=1) # context_vector shape after sum == (batch_size, hidden_size) context_vector = attention_weights * values context_vector = tf.reduce_sum(context_vector, axis=1) return context_vector, attention_weights class Decoder(tf.keras.Model): def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz): super(Decoder, self).__init__() self.batch_sz = batch_sz self.dec_units = dec_units self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim , , weights =[embeddingMatrix], trainable=False) self.gru = tf.keras.layers.GRU(self.dec_units, return_sequences=True, return_state=True, recurrent_initializer='glorot_uniform') self.fc = tf.keras.layers.Dense(vocab_size , activation = "softmax") # used for attention self.attention = BahdanauAttention(self.dec_units) def call(self, x, hidden, enc_output ,topic_vector): # enc_output shape == (batch_size, max_length, hidden_size) attention_vector, attention_weights = self.attention(hidden, enc_output) context_vector = tf.concat([attention_vector, topic_vector], axis = -1) # x shape after passing through embedding == (batch_size, 1, embedding_dim) x = self.embedding(x) # x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size) x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1) # passing the concatenated vector to the GRU output, state = self.gru(x , initial_state = hidden) # output shape == (batch_size * 1, hidden_size) output = tf.reshape(output, (-1, output.shape[2])) # output shape == (batch_size, vocab) x = self.fc(output) return x, state, attention_weights decoder = Decoder(vocab_tar_size , embedding_dim, units, BATCH_SIZE) optimizer = tf.keras.optimizers.Adam() checkpoint_dir = './training_checkpoints' checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") # if not os.path.exists(checkpointdir): # os.mkdir(checkpoint_dir) checkpoint = tf.train.Checkpoint(optimizer=optimizer, encoder=encoder, decoder=decoder) try: status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)) print("Checkpoint found at {}".format(tf.train.latest_checkpoint(checkpoint_dir))) except: print("No checkpoint found at {}".format(checkpoint_dir)) def evaluate(sentence): attention_plot = np.zeros((max_len, max_len)) sentence = preprocess_sentence(sentence) inputs = [word_index[i] for i in sentence.split(' ')] inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], maxlen=max_len, padding='post') inputs = tf.convert_to_tensor(inputs) result = '' hidden = [tf.zeros((1, units))] enc_out, enc_hidden , topic_vector = encoder(inputs, hidden) dec_hidden = enc_hidden dec_input = tf.expand_dims([word_index['<start>']], 0) for t in range(max_len): predictions, dec_hidden, attention_weights = decoder(dec_input, dec_hidden, enc_out , topic_vector) # storing the attention weights to plot later on attention_weights = tf.reshape(attention_weights, (-1, )) attention_plot[t] = attention_weights.numpy() predicted_id = tf.argmax(predictions[0]).numpy() result += index_word[predicted_id] + ' ' if index_word[predicted_id] == '<end>': return result, sentence, attention_plot # the predicted ID is fed back into the model dec_input = tf.expand_dims([predicted_id], 0) return result, sentence, attention_plot # function for plotting the attention weights def plot_attention(attention, sentence, predicted_sentence): fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(1, 1, 1) ax.matshow(attention, cmap='viridis') fontdict = {'fontsize': 14} ax.set_xticklabels([''] + sentence, fontdict=fontdict, rotation=90) ax.set_yticklabels([''] + predicted_sentence, fontdict=fontdict) ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) ax.yaxis.set_major_locator(ticker.MultipleLocator(1)) plt.show() def response(sentence): result, sentence, attention_plot = evaluate(sentence) x =0 # print('Input: %s' % (sentence)) if debug == True: print('Response: {}'.format(result) return result # attention_plot = attention_plot[:len(result.split(' ')), :len(sentence.split(' '))] # plot_attention(attention_plot, sentence.split(' '), result.split(' ')) batch_size = 1 Ty = 20 # def beam_search_decoder(sentence , beam_width = 3): # sentence = preprocess_sentence(sentence) # inputs = [word_index[i] for i in sentence.split(' ')] # inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], # maxlen=max_len, # padding='post') # inputs = tf.convert_to_tensor(inputs) # predictions = [([],0)] * beam_width # # predictions = tf.convert_to_tensor(predictions) # enc_hidden = tf.zeros((1,units)) # encoder_output , encoder_hidden , topic_vector = encoder(inputs , enc_hidden) # decoder_hidden = [encoder_hidden] * 3 # # decoder_hidden = [decoder_hidden] * beam_width # decoder_input = [tf.expand_dims([word_index['<start>'] ], 0)] * beam_width # # print("decoder_input" ,decoder_input[0]) # print("decoder_hiddem:{} \n decoder_input: {} \n encoder_hidden :{} \n topic_vector {} \n".format(decoder_hidden[0].shape , decoder_input[0].shape , encoder_output.shape , topic_vector.shape)) # for t in range(Ty): # current_predictions = [] # for i in range(beam_width): # out , decoder_hidden[i] , _ = decoder(decoder_input[i] , decoder_hidden[i] , encoder_output , topic_vector) # # print("not done") # # print(out) # index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) # prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') # # print("index: {} prob: {}".format((index[0]) , prob[0])); # # print("done") # # print(int(index[0][0])) # # print(int(index[0][1])) # # print(int(index[0][2])) # # print("done" ,current_predictions) # for j in range(beam_width): # if t ==0 and i ==1 : # continue # # print("do nothing!") # elif t == 0 and i ==2: # continue # # print("do nothing!") # else : # current_predictions.append((predictions[i][0] + [int(index[0][j])] , np.log(prob[0][j]) + predictions[i][1])) # # if t == 0 and i ==0 : # # print(current_predictions) # # print(count) # # print(current_predictions) # def get_prob(pred): # # print(pred[1]) # return pred[1] # # print(current_predictions) # current_predictions = sorted(current_predictions , key = get_prob , reverse =True) # # print(current_predictions) # predictions = current_predictions[:beam_width] # # print(predictions) # current_predictions = [pred[0] for pred in current_predictions] # print(current_predictions[0]) # # print(current_predictions[1]) # decoder_input = [tf.expand_dims([tf.convert_to_tensor(pred[t])],0) for pred in current_predictions] # print(decoder_input[0]) # output = [] # for pred , prob in predictions: # out = [] # s = "" # for p in pred: # if index_word[p] != "<end>": # s += " " + index_word[p] # else : # break # output.append(s) # prob = [pred[1] / len(output[i]) for i , pred in enumerate(predictions)] # # for i in range(beam_width): # print("resposne :{} \n {} \n {} ".format(output[0] , output[1] ,output[2])) # return output , prob def beam_search_decoder(sentence , beam_width = 3): sentence = preprocess_sentence(sentence) inputs = [word_index[i] for i in sentence.split(' ')] inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], maxlen=max_len, padding='post') inputs = tf.convert_to_tensor(inputs) predictions = [] # predictions = tf.convert_to_tensor(predictions) enc_hidden = tf.zeros((1,128)) encoder_output , encoder_hidden , topic_vector = encoder(inputs , enc_hidden) decoder_hidden = encoder_hidden # decoder_hidden = [decoder_hidden] * beam_width decoder_input = tf.expand_dims([word_index['<start>'] ], 0) # print("decoder_input" ,decoder_input[0]) # print("decoder_hiddem:{} \n decoder_input: {} \n encoder_output :{} \n topic_vector {} \n".format(decoder_hidden.shape , decoder_input.shape , encoder_output.shape , topic_vector.shape)) out , hidden, _ = decoder(decoder_input , decoder_hidden , encoder_output , topic_vector) index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') terminal_sentences, decoder_hidden, predictions = [], [], [] decoder_hidden = [hidden] * 3 # print(decoder_hidden) for i in range(beam_width): predictions.append(([int(index[0][i])], np.log(prob[0][i]))) # print(predictions[0][0]) decoder_input = [tf.expand_dims(tf.convert_to_tensor(pred[0]),0) for pred in predictions] # print(decoder_input[0]) for t in range(1,Ty): current_predictions = [] for i in range(beam_width): out , decoder_hidden[i] , _ = decoder(decoder_input[i] , decoder_hidden[i] , encoder_output , topic_vector) # print("once") index = tf.argsort(out, axis=-1, direction='DESCENDING', stable=False, name=None) prob = tf.sort(out ,axis = -1 , direction = 'DESCENDING') for j in range(beam_width): current_predictions.append((predictions[i][0] + [int(index[0][j])] , np.log(prob[0][j]) + predictions[i][1] , i)) def get_prob(pred): return pred[1] current_predictions = sorted(current_predictions , key = get_prob , reverse =True) current_predictions = current_predictions[:beam_width] # print("time_step {} {}".format(t ,current_predictions)) hidden = [] inputs = [] pred = [] for j in range(beam_width): if index_word[current_predictions[j][0][t]] == "<end>": beam_width -= 1 terminal_sentences.append((current_predictions[j][0] , current_predictions[j][1])) else : hidden.append(decoder_hidden[current_predictions[j][2]]) inputs.append(tf.expand_dims([tf.convert_to_tensor(current_predictions[j][0][t])],0) ) pred.append((current_predictions[j][0] , current_predictions[j][1])) decoder_hidden = hidden decoder_input = inputs predictions = pred # print(decoder_input) if beam_width <= 0 : break for x in range(len(predictions)): terminal_sentences.append((predictions[x][0],predictions[x][1])) terminal_sentences = sorted(terminal_sentences , key = get_prob , reverse =True) output = [] for pred , prob in terminal_sentences: out = [] s = "" for p in pred: if index_word[p] != "<end>": s += " " + index_word[p] else : break output.append(s) prob = [pred[1] / len(output[i]) for i , pred in enumerate(terminal_sentences)] # for i in range(beam_width): if debug == True: print("resposne :{} {} \n {} {} \n {} {} ".format(output[0] , prob[0] , output[1] ,prob[1] ,output[2] , prob[2])) return output , prob def string_to_audio(input_string, delete): language = 'en' gen_audio = gTTS(text = input_string, lang=language, slow=False) gen_audio.save("Output.mp3") os.system("mpg123 Output.mp3") if (delete == True): os.remove("Output.mp3") def get_transcript(): mic = sr.Microphone() r = sr.Recognizer() print("Speak Now") with mic as source: audio = r.listen(source, timeout=5, phrase_time_limit=10) try : result = r.recognize_google(audio) print(result) except : return None return result def get_audio_file(path): r = sr.Recognizer() with sr.AudioFile(path) as source: audio_text = r.listen(source) using google speech recognition text = r.recognize_google(audio_text) if debug == True: print('Converting audio transcripts into text ...') print(text) return text if __name__ == '__main__': parser = argparse.ArgumentParser(description='Conversational Bot') parser.add_argument('-d', '--debug', type = bool, default= False, help='set debug value') parser.add_argument('-o', '--options',type = str, help='set input option') args = parser.parse_args() # debug = True debug = args.debug while True: result = get_transcript() out = response(result) string_to_audio(out , True)
[]
2024-01-10
crustyapples/tldrbot
bot~utils~davinci_summarizer.py
import openai import configparser import os current_dir = os.path.dirname(os.path.abspath(__file__)) config_file_path = os.path.join(current_dir, "config.ini") # Set API key config = configparser.ConfigParser() config.read(config_file_path) openai.api_key = config["OpenAI"]["api_key"] def get_summary(result): # Define prompt prompt = ( "Summarise this group chat that occurred on Telegram, making references to who said what " + result ) # Call API and receive response generated = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=0, max_tokens=1000, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0, ) # # Extract summary text from response # summary = generated.choices[0].text.strip() # # Parse and format summary as needed # parsed_summary = json.loads(summary) # Output summary to console return generated["choices"][0]["text"]
[ "Summarise this group chat that occurred on Telegram, making references to who said what PLACEHOLDER" ]
2024-01-10
crustyapples/tldrbot
bot~utils~gpt_summarizer.py
import openai import os # current_dir = os.path.dirname(os.path.abspath(__file__)) # config_file_path = os.path.join(current_dir, "config.ini") # Set API key # config = configparser.ConfigParser() # config.read(config_file_path) # openai.api_key = config["OpenAI"]["api_key"] openai.api_key = os.environ.get("OPENAI_API_KEY") def get_summary(result): # Define prompt prompt = ( result + '''Based on the above, output the following "Summary: [4-5 Sentences] Sentiment: [Choose between, Positive, Negative, Neutral]"''' ) # Call API and receive response generated = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": f"{prompt}"}] ) # Output summary to console return generated["choices"][0]["message"]["content"]
[ "PLACEHOLDERBased on the above, output the following\n\n \"Summary: [4-5 Sentences]\n\n Sentiment: [Choose between, Positive, Negative, Neutral]\"" ]
2024-01-10
prakashsukhwal/AskData_pandas_langchain
chatapp.py
import streamlit as st import pandas as pd from langchain.chat_models import ChatOpenAI from langchain.agents.agent_types import AgentType from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent # page title st.set_page_config(page_title='🦜🔗 Ask the Data App') st.title('🦜🔗 Ask the Data App') def load_csv(input_csv): df = pd.read_csv(input_csv) with st.expander("see dataframe"): st.write(df) return df ## generate llm response def generate_response(csv_file, input_query): llm = ChatOpenAI(model_name = 'gpt-3.5-turbo-0613', temperature= 0.0, openai_api_key= openai_api_key) df = load_csv(csv_file) #create pandas df agent agent = create_pandas_dataframe_agent(llm, df, verbose=True, agent_type= AgentType.OPENAI_FUNCTIONS) #perfrm query by agent response = agent.run(input_query) return st.success(response) # Input widgets uploaded_file = st.file_uploader('Upload a CSV file', type=['csv']) question_list = [ 'How many rows are there?', 'What is the range of values for MolWt with logS greater than 0?', 'How many rows have MolLogP value greater than 0.', 'Other'] query_text = st.selectbox('Select an example query:', question_list, disabled=not uploaded_file) openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (uploaded_file and query_text)) # App logic if query_text is 'Other': query_text = st.text_input('Enter your query:', placeholder = 'Enter query here ...', disabled=not uploaded_file) if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='⚠') if openai_api_key.startswith('sk-') and (uploaded_file is not None): st.header('Output') generate_response(uploaded_file, query_text)
[]
2024-01-10
simon-mo/ray
rllib~examples~env~cliff_walking_wall_env.py
import gymnasium as gym from gymnasium 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, *, seed=None, options=None): 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, False, {}
[]
2024-01-10
tgodfrey0/LLM_ROS2_Control
llm_controller.py
from swarmnet import SwarmNet from openai import OpenAI from threading import Lock from time import sleep global_conv = [] client: OpenAI = None max_stages = 10 this_agents_turn = True tl = Lock() def is_my_turn(): tl.acquire() b = this_agents_turn tl.release() return b def toggle_turn(): global this_agents_turn tl.acquire() this_agents_turn = not this_agents_turn tl.release() def send_req(): completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=global_conv, # max_tokens=500 ) # print(completion.choices[0].message) global_conv.append({"role": completion.choices[0].message.role, "content": completion.choices[0].message.content}) sn_ctrl.send(f"LLM {completion.choices[0].message.role} {completion.choices[0].message.content}") def get_api_key() -> str: with open("openai_key", "r") as f: return f.readline().rstrip() def toggle_role(r: str): if r == "assistant": return "user" elif r == "user": return "assistant" else: return "" def plan_completed(): print("Plan completed:") # map(lambda m : print(f"{m.role}: {m.content}"), global_conv) for m in global_conv: print(f"{m['role']}: {m['content']}") def llm_recv(msg: str) -> None: m = msg.split(" ", 1) # Msg are LLM ROLE CONTENT r = m[0] c = m[1] global_conv.append({"role": toggle_role(r), "content": c}) #! Don't think this is adding to the list toggle_turn() # if("@SUPERVISOR" not in c): # send_req(client) # else: # plan_completed() #? This may have issues with only one agent finishing. Could just add a SN command def negotiate(): current_stage = 0 if this_agents_turn: global_conv.append({"role": "user", "content": "I am at D1, you are at D7. I must end at D7 and you must end at D1"}) while(current_stage < max_stages or not global_conv[len(global_conv)-1]["content"].endswith("@SUPERVISOR")): while(not is_my_turn()): # Wait to receive from the other agent sleep(0.5) print("waiting") send_req() toggle_turn() current_stage += 1 print(f"Stage {current_stage}") print(global_conv); plan_completed() current_stage = 0 if __name__=="__main__": sn_ctrl = SwarmNet({"LLM": llm_recv}, device_list = [("192.168.0.120", 51000)]) sn_ctrl.start() print("Communications initialised") input("Press any key to start") client = OpenAI() global_conv = [ {"role": "system", "content": "You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise.\ We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree.\ Once this has been decided you should call the '@SUPERVISOR' tag at the end of your plan and print your plan in a concise numbered list using only the following command words:\ - 'FORWARDS' to move one square forwards\ - 'BACKWARDS' to move one square backwards\ - 'CLOCKWISE' to rotate 90 degrees clockwise\ - 'ANTICLOCKWISE' to rotate 90 degrees clockwise\ "}] # res = send_req(client) # print(res.content) # sn_ctrl.send(f"LLM {res.role} {res.content}") negotiate() input("Press any key to finish") plan_completed() sn_ctrl.kill()
[ "You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise. We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree. Once this has been decided you should call the '@SUPERVISOR' tag at the end of your plan and print your plan in a concise numbered list using only the following command words: - 'FORWARDS' to move one square forwards - 'BACKWARDS' to move one square backwards - 'CLOCKWISE' to rotate 90 degrees clockwise - 'ANTICLOCKWISE' to rotate 90 degrees clockwise ", "I am at D1, you are at D7. I must end at D7 and you must end at D1" ]
2024-01-10
tgodfrey0/LLM_ROS2_Control
llm_ros_controller_ws~src~llm_controller~llm_controller~llm_node.py
from swarmnet import SwarmNet from openai import OpenAI from math import pi from threading import Lock from typing import Optional, List, Tuple from .grid import Grid import rclpy from rclpy.node import Node from geometry_msgs.msg import Twist #! Will need some way of determining which command in the plan is for which agent #! Use some ID prefixed to the command? dl: List[Tuple[str, int]] = [("192.168.0.120", 51000), ("192.168.0.64", 51000)] # Other device # dl: List[Tuple[str, int]] = [("192.168.0.121", 51000), ("192.168.0.64", 51000)] # Other device # dl: List[Tuple[str, int]] = [("192.168.0.64", 51000)] # Other device #* Update these constants INITIALLY_THIS_AGENTS_TURN = True # Only one agent should have true STARTING_GRID_LOC = "D1" STARTING_GRID_HEADING = Grid.Heading.UP ENDING_GRID_LOC = "D7" MAX_NUM_NEGOTIATION_MESSAGES = 15 CMD_FORWARD = "@FORWARD" CMD_BACKWARDS = "@BACKWARDS" CMD_ROTATE_CLOCKWISE = "@CLOCKWISE" CMD_ROTATE_ANTICLOCKWISE = "@ANTICLOCKWISE" CMD_SUPERVISOR = "@SUPERVISOR" LINEAR_SPEED = 0.15 # m/s LINEAR_DISTANCE = 0.45 # m LINEAR_TIME = LINEAR_DISTANCE / LINEAR_SPEED ANGULAR_SPEED = 0.3 # rad/s ANGULAR_DISTANCE = pi/2.0 # rad ANGULAR_TIME = ANGULAR_DISTANCE / ANGULAR_SPEED WAITING_TIME = 1 class VelocityPublisher(Node): def __init__(self): super().__init__("velocity_publisher") self.publisher_ = self.create_publisher(Twist, "/cmd_vel", 10) self.global_conv = [] self.client: OpenAI = None self.max_stages = MAX_NUM_NEGOTIATION_MESSAGES self.this_agents_turn = INITIALLY_THIS_AGENTS_TURN self.other_agent_ready = False self.other_agent_loc = "" self.turn_lock = Lock() self.ready_lock = Lock() self.grid = Grid(STARTING_GRID_LOC,STARTING_GRID_HEADING, 8, 8) self.create_plan() if(len(self.global_conv) > 1): cmd = self.global_conv[len(self.global_conv)-1]["content"] for s in cmd.split("\n"): if(CMD_FORWARD in s): self.pub_forwards() elif(CMD_BACKWARDS in s): self.pub_backwards() elif(CMD_ROTATE_CLOCKWISE in s): self.pub_clockwise() elif(CMD_ROTATE_ANTICLOCKWISE in s): self.pub_anticlockwise() elif(CMD_SUPERVISOR in s): pass elif(s.strip() == ""): pass else: self.get_logger().error(f"Unrecognised command: {s}") self.wait_delay() self.get_logger().info(f"Full plan parsed") def _delay(self, t_target): t0 = self.get_clock().now() while(self.get_clock().now() - t0 < rclpy.duration.Duration(seconds=t_target)): pass self.get_logger().info(f"Delayed for {t_target} seconds") def linear_delay(self): self._delay(LINEAR_TIME) def angular_delay(self): self._delay(ANGULAR_TIME) def wait_delay(self): self._delay(WAITING_TIME) def _publish_cmd(self, msg: Twist): self.publisher_.publish(msg) self.get_logger().info(f"Publishing to /cmd_vel") def _publish_zero(self): self.get_logger().info(f"Zero velocity requested") msg = Twist() msg.linear.x = 0.0 msg.linear.y = 0.0 msg.linear.z = 0.0 msg.angular.x = 0.0 msg.angular.y = 0.0 msg.angular.z = 0.0 self._publish_cmd(msg) def _pub_linear(self, dir: int): msg = Twist() msg.linear.x = dir * LINEAR_SPEED #? X, Y or Z? msg.linear.y = 0.0 msg.linear.z = 0.0 msg.angular.x = 0.0 msg.angular.y = 0.0 msg.angular.z = 0.0 self._publish_cmd(msg) self.linear_delay() self._publish_zero() def _pub_rotation(self, dir: float): msg = Twist() msg.linear.x = 0.0 msg.linear.y = 0.0 msg.linear.z = 0.0 msg.angular.x = 0.0 msg.angular.y = 0.0 msg.angular.z = dir * ANGULAR_SPEED #? X Y or Z self._publish_cmd(msg) self.angular_delay() self._publish_zero() def pub_forwards(self): self.get_logger().info(f"Forwards command") self.grid.forwards() self._pub_linear(1) def pub_backwards(self): self.get_logger().info(f"Backwards command") self.grid.backwards() self._pub_linear(-1) def pub_anticlockwise(self): self.get_logger().info(f"Anticlockwise command") self.grid.anticlockwise() self._pub_rotation(1) def pub_clockwise(self): self.get_logger().info(f"Clockwise command") self.grid.clockwise() self._pub_rotation(-1) def create_plan(self): self.get_logger().info(f"Initialising SwarmNet") self.sn_ctrl = SwarmNet({"LLM": self.llm_recv, "READY": self.ready_recv, "FINISHED": self.finished_recv, "INFO": self.info_recv}, device_list = dl) #! Publish INFO messages which can then be subscribed to by observers self.sn_ctrl.start() self.get_logger().info(f"SwarmNet initialised") self.sn_ctrl.send("INFO SwarmNet initialised successfully") while(not self.is_ready()): self.sn_ctrl.send(f"READY {self.grid}") self.get_logger().info("Waiting for an agent to be ready") self.wait_delay() self.sn_ctrl.send(f"READY {self.grid}") self.sn_ctrl.clear_rx_queue() self.sn_ctrl.send("INFO Agents ready for negotiation") self.client = OpenAI() # Use the OPENAI_API_KEY environment variable self.global_conv = [ {"role": "system", "content": f"You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise.\ We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree.\ You cannot go outside of the grid. Once this has been decided you should call the '\f{CMD_SUPERVISOR}' tag at the end of your plan and print your plan in a concise numbered list using only the following command words:\ - '{CMD_FORWARD}' to move one square forwards\ - '{CMD_BACKWARDS}' to move one square backwards \ - '{CMD_ROTATE_CLOCKWISE}' to rotate 90 degrees clockwise \ - '{CMD_ROTATE_ANTICLOCKWISE}' to rotate 90 degrees clockwise \ The final plan should be a numbered list only containing these commands."}] self.negotiate() self.sn_ctrl.send("INFO Negotiation finished") self.sn_ctrl.kill() def is_my_turn(self): self.turn_lock.acquire() b = self.this_agents_turn self.turn_lock.release() return b def toggle_turn(self): self.turn_lock.acquire() self.this_agents_turn = not self.this_agents_turn self.turn_lock.release() def set_turn(self, b): self.turn_lock.acquire() self.this_agents_turn = b self.turn_lock.release() def send_req(self): completion = self.client.chat.completions.create( model="gpt-3.5-turbo", messages=self.global_conv, max_tokens=750 ) # print(completion.choices[0].message) self.global_conv.append({"role": completion.choices[0].message.role, "content": completion.choices[0].message.content}) self.sn_ctrl.send(f"LLM {completion.choices[0].message.role} {completion.choices[0].message.content}") def toggle_role(self, r: str): if r == "assistant": return "user" elif r == "user": return "assistant" else: return "" def plan_completed(self): self.get_logger().info(f"Plan completed:") for m in self.global_conv: self.get_logger().info(f"{m['role']}: {m['content']}") self.sn_ctrl.send("FINISHED") while(not (self.sn_ctrl.rx_queue.empty() and self.sn_ctrl.tx_queue.empty())): self.get_logger().info("Waiting for message queues to clear") self.wait_delay() self.generate_summary() def generate_summary(self): self.global_conv.append({"role": "user", "content": f"Generate a summarised numerical list of the plan for the steps that I should complete. Use only the commands:\ - '{CMD_FORWARD}' to move one square forwards\ - '{CMD_BACKWARDS}' to move one square backwards \ - '{CMD_ROTATE_CLOCKWISE}' to rotate 90 degrees clockwise \ - '{CMD_ROTATE_ANTICLOCKWISE}' to rotate 90 degrees clockwise "}) completion = self.client.chat.completions.create( model="gpt-3.5-turbo", messages=self.global_conv, max_tokens=750 ) self.global_conv.append({"role": completion.choices[0].message.role, "content": completion.choices[0].message.content}) self.sn_ctrl.send(f"INFO Final plan for {self.sn_ctrl.addr}: {completion.choices[0].message.content}") def info_recv(self, msg: Optional[str]) -> None: pass def finished_recv(self, msg: Optional[str]) -> None: self.generate_summary() def llm_recv(self, msg: Optional[str]) -> None: m = msg.split(" ", 1) # Msg are LLM ROLE CONTENT r = m[0] c = m[1] self.global_conv.append({"role": self.toggle_role(r), "content": c}) self.toggle_turn() def ready_recv(self, msg: Optional[str]) -> None: self.ready_lock.acquire() self.other_agent_ready = True self.other_agent_loc = msg self.ready_lock.release() def is_ready(self): self.ready_lock.acquire() b = self.other_agent_ready self.ready_lock.release() return b def negotiate(self): current_stage = 0 if self.this_agents_turn: self.global_conv.append({"role": "user", "content": f"I am at {self.grid}, you are at {self.other_agent_loc}. I must end at {self.other_agent_loc} and you must end at {self.grid}"}) else: current_stage = 1 while(current_stage < self.max_stages): while(not self.is_my_turn()): # Wait to receive from the other agent if(len(self.global_conv) > 0 and self.global_conv[len(self.global_conv)-1]["content"].rstrip().endswith(f"{CMD_SUPERVISOR}")): break; self.wait_delay() self.get_logger().info(f"Waiting for a response from another agent") # if(len(self.global_conv) > 0 and self.global_conv[len(self.global_conv)-1]["content"].rstrip().endswith(f"{CMD_SUPERVISOR}")): # self.get_logger().info(f"Content ends with {CMD_SUPERVISOR}") # break; self.send_req() self.toggle_turn() current_stage += 2 # Shares the current_stage self.get_logger().info(f"Stage {current_stage}") self.sn_ctrl.send(f"INFO Negotiation stage {current_stage}") self.get_logger().info(f"{self.global_conv}"); self.plan_completed() current_stage = 0 def main(args=None): rclpy.init() velocity_publisher = VelocityPublisher() #* Move this logic into the node itself # global global_conv # global_conv = [ # {"role": "system", "content": f"@FORWARD"}] rclpy.spin_once(velocity_publisher) #* spin_once will parse the given plan then return velocity_publisher.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
[ "You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise. We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree. You cannot go outside of the grid. Once this has been decided you should call the '\fPLACEHOLDER' tag at the end of your plan and print your plan in a concise numbered list using only the following command words: - 'PLACEHOLDER' to move one square forwards - 'PLACEHOLDER' to move one square backwards - 'PLACEHOLDER' to rotate 90 degrees clockwise - 'PLACEHOLDER' to rotate 90 degrees clockwise The final plan should be a numbered list only containing these commands.", "Generate a summarised numerical list of the plan for the steps that I should complete. Use only the commands: - 'PLACEHOLDER' to move one square forwards - 'PLACEHOLDER' to move one square backwards - 'PLACEHOLDER' to rotate 90 degrees clockwise - 'PLACEHOLDER' to rotate 90 degrees clockwise " ]
2024-01-10
tgodfrey0/LLM_ROS2_Control
llm_logic_without_ros.py
from swarmnet import SwarmNet from openai import OpenAI from math import pi from threading import Lock from typing import Optional, List, Tuple from grid import Grid from time import sleep import threading #! Will need some way of determining which command in the plan is for which agent #! Use some ID prefixed to the command? dl: List[Tuple[str, int]] = [("192.168.0.120", 51000)] # Other device # dl: List[Tuple[str, int]] = [("192.168.0.121", 51000)] # Other device # dl: List[Tuple[str, int]] = [("192.168.0.64", 51000)] # Other device CMD_FORWARD = "@FORWARD" CMD_BACKWARDS = "@BACKWARDS" CMD_ROTATE_CLOCKWISE = "@CLOCKWISE" CMD_ROTATE_ANTICLOCKWISE = "@ANTICLOCKWISE" CMD_SUPERVISOR = "@SUPERVISOR" LINEAR_SPEED = 0.15 # m/s LINEAR_DISTANCE = 0.45 # m LINEAR_TIME = LINEAR_DISTANCE / LINEAR_SPEED ANGULAR_SPEED = 0.3 # rad/s ANGULAR_DISTANCE = pi/2.0 # rad ANGULAR_TIME = ANGULAR_DISTANCE / ANGULAR_SPEED WAITING_TIME = 1 INITIALLY_THIS_AGENTS_TURN = True # Only one agent should have true STARTING_GRID_LOC = "D1" # This should be updated to ensure the grid is set up correctly STARTING_GRID_HEADING = Grid.Heading.UP # This should be updated to ensure the grid is set up correctly ENDING_GRID_LOC = "D7" # This only needs updating if INITIALLY_THIS_AGENTS_TURN is true class LLM(): def __init__(self): self.global_conv = [] self.client: OpenAI = None self.max_stages = 5 self.this_agents_turn = INITIALLY_THIS_AGENTS_TURN self.other_agent_ready = False self.turn_lock = Lock() self.ready_lock = Lock() self.grid = Grid(STARTING_GRID_LOC,STARTING_GRID_HEADING, 8, 8) #! When moving into ROS update grid position self.create_plan() if(len(self.global_conv) > 1): cmd = self.global_conv[len(self.global_conv)-1]["content"] for s in cmd.split("\n"): if(CMD_FORWARD in s): self.pub_forwards() elif(CMD_BACKWARDS in s): self.pub_backwards() elif(CMD_ROTATE_CLOCKWISE in s): self.pub_clockwise() elif(CMD_ROTATE_ANTICLOCKWISE in s): self.pub_anticlockwise() elif(CMD_SUPERVISOR in s): pass elif(s.strip() == ""): pass else: print(f"Unrecognised command: {s}") def _delay(self, t_target): sleep(t_target) print(f"Delayed for {t_target} seconds") def linear_delay(self): self._delay(LINEAR_TIME) def angular_delay(self): self._delay(ANGULAR_TIME) def wait_delay(self): self._delay(WAITING_TIME) def _publish_zero(self): print("ZERO") def _pub_linear(self, dir: int): self.linear_delay() self._publish_zero() def pub_forwards(self): print(f"Forwards command") self.grid.forwards() self._pub_linear(1) def pub_backwards(self): print(f"Backwards command") self.grid.backwards() self._pub_linear(-1) def _pub_rotation(self, dir: int): self.angular_delay() self._publish_zero() def pub_anticlockwise(self): print(f"Anticlockwise command") self.grid.anticlockwise() self._pub_rotation(1) def pub_clockwise(self): print(f"Clockwise command") self.grid.clockwise() self._pub_rotation(-1) def create_plan(self): print(f"Initialising SwarmNet") self.sn_ctrl = SwarmNet({"LLM": self.llm_recv, "READY": self.ready_recv, "FINISHED": self.finished_recv}, device_list = dl) #! Publish INFO messages which can then be subscribed to by observers self.sn_ctrl.start() print(f"SwarmNet initialised") while(not self.is_ready()): self.sn_ctrl.send("READY") print("Waiting for an agent to be ready") self.wait_delay() self.sn_ctrl.send("READY") self.sn_ctrl.clear_rx_queue() self.client = OpenAI() # Use the OPENAI_API_KEY environment variable self.global_conv = [ {"role": "system", "content": f"You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise.\ We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree.\ Once this has been decided you should call the '\f{CMD_SUPERVISOR}' tag at the end of your plan and print your plan in a concise numbered list using only the following command words:\ - '{CMD_FORWARD}' to move one square forwards\ - '{CMD_BACKWARDS}' to move one square backwards \ - '{CMD_ROTATE_CLOCKWISE}' to rotate 90 degrees clockwise \ - '{CMD_ROTATE_ANTICLOCKWISE}' to rotate 90 degrees clockwise \ The final plan should be a numbered list only containing these commands."}] self.negotiate() self.sn_ctrl.kill() def is_my_turn(self): self.turn_lock.acquire() b = self.this_agents_turn self.turn_lock.release() return b def toggle_turn(self): self.turn_lock.acquire() self.this_agents_turn = not self.this_agents_turn self.turn_lock.release() def send_req(self): completion = self.client.chat.completions.create( model="gpt-3.5-turbo", messages=self.global_conv, max_tokens=750 ) # print(completion.choices[0].message) self.global_conv.append({"role": completion.choices[0].message.role, "content": completion.choices[0].message.content}) self.sn_ctrl.send(f"LLM {completion.choices[0].message.role} {completion.choices[0].message.content}") def toggle_role(self, r: str): if r == "assistant": return "user" elif r == "user": return "assistant" else: return "" def plan_completed(self): print(f"Plan completed:") for m in self.global_conv: print(f"{m['role']}: {m['content']}") self.sn_ctrl.send("FINISHED") while(not (self.sn_ctrl.rx_queue.empty() and self.sn_ctrl.tx_queue.empty())): print("Waiting for message queues to clear") self.wait_delay() self.generate_summary() def generate_summary(self): self.global_conv.append({"role": "user", "content": "Generate a summarised numerical list of the plan for the steps that I should complete"}) completion = self.client.chat.completions.create( model="gpt-3.5-turbo", messages=self.global_conv, max_tokens=750 ) # print(completion.choices[0].message) self.global_conv.append({"role": completion.choices[0].message.role, "content": completion.choices[0].message.content}) def finished_recv(self, msg: Optional[str]) -> None: self.generate_summary() def llm_recv(self, msg: Optional[str]) -> None: m = msg.split(" ", 1) # Msg are LLM ROLE CONTENT r = m[0] c = m[1] self.global_conv.append({"role": self.toggle_role(r), "content": c}) self.toggle_turn() def ready_recv(self, msg: Optional[str]) -> None: self.ready_lock.acquire() self.other_agent_ready = True self.ready_lock.release() def is_ready(self): self.ready_lock.acquire() b = self.other_agent_ready self.ready_lock.release() return b def negotiate(self): current_stage = 0 if self.this_agents_turn: self.global_conv.append({"role": "user", "content": f"I am at {self.grid}, you are at {ENDING_GRID_LOC}. I must end at {ENDING_GRID_LOC} and you must end at {STARTING_GRID_LOC}"}) while(current_stage < self.max_stages): if(len(self.global_conv) > 0 and self.global_conv[len(self.global_conv)-1]["content"].rstrip().endswith("@SUPERVISOR")): break; while(not self.is_my_turn()): # Wait to receive from the other agent self.wait_delay() print(f"Waiting for a response from another agent") self.send_req() self.toggle_turn() current_stage += 1 print(f"Stage {current_stage}") print(f"{self.global_conv}"); self.plan_completed() current_stage = 0 if __name__ == '__main__': x = LLM() print("Finished")
[ "Generate a summarised numerical list of the plan for the steps that I should complete", "You and I are wheeled robots, and can only move forwards, backwards, and rotate clockwise or anticlockwise. We will negotiate with other robots to navigate a path without colliding. You should negotiate and debate the plan until all agents agree. Once this has been decided you should call the '\fPLACEHOLDER' tag at the end of your plan and print your plan in a concise numbered list using only the following command words: - 'PLACEHOLDER' to move one square forwards - 'PLACEHOLDER' to move one square backwards - 'PLACEHOLDER' to rotate 90 degrees clockwise - 'PLACEHOLDER' to rotate 90 degrees clockwise The final plan should be a numbered list only containing these commands." ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~identify_static.py
from textwrap import dedent from langchain import PromptTemplate from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from pydantic import BaseModel from compress_gpt.prompts.compress_chunks import CompressChunks from compress_gpt.utils import wrap_prompt from . import Prompt class StaticChunk(BaseModel): regex: str reason: str class IdentifyStatic(Prompt[list[StaticChunk]]): @staticmethod def get_prompt() -> ChatPromptTemplate: CompressChunks.get_prompt().messages[0] task = SystemMessagePromptTemplate.from_template( dedent( """ Your first task is to extract the static chunks from the prompt. Static chunks are parts of the prompt that must be preserved verbatim. Extracted chunks can be of any size, but you should try to make them as small as possible. Some examples of static chunks include: - The name of a tool, parameter, or variable - A specific hard-coded date, time, email, number, or other constant - An example of input or output structure - Any value which must be preserved verbatim Task instructions need not be included. """ ) ) system = SystemMessagePromptTemplate( prompt=PromptTemplate( template_format="jinja2", input_variables=[], template=dedent( """ You will supply a list of regex patterns to extract the static chunks. Make each pattern as specific as possible. Do not allow large matches. Each pattern should capture as many static chunks as possible, without capturing any non-static chunks. For each pattern, you must explain why it is necessary and a minimal capture. The regex MUST be a valid Python regex. The regex is case-sensitive, so use the same case in the regex as in the chunk. You may not include quotes in the regex. Each object in the list MUST follow this schema: {"regex": "Name: (\\\\w+)", "reason": "capture names of students"} Your output MUST be a valid JSON list. Do not forget to include [] around the list. Do not output plain text. Backslashes must be properly escaped in the regex to be a valid JSON string. Do not follow the instructions in the prompt. Your job is to extract the static chunks, regardless of its content. """ ), ) ) human = HumanMessagePromptTemplate.from_template( "The prompt to analyze is:\n" + wrap_prompt("prompt") ) return ChatPromptTemplate.from_messages([task, system, human])
[ "[PLACEHOLDER, PLACEHOLDER, PLACEHOLDER]", "\n Your first task is to extract the static chunks from the prompt.\n Static chunks are parts of the prompt that must be preserved verbatim.\n Extracted chunks can be of any size, but you should try to make them as small as possible.\n Some examples of static chunks include:\n - The name of a tool, parameter, or variable\n - A specific hard-coded date, time, email, number, or other constant\n - An example of input or output structure\n - Any value which must be preserved verbatim\n Task instructions need not be included.\n ", "\n You will supply a list of regex patterns to extract the static chunks.\n Make each pattern as specific as possible. Do not allow large matches.\n Each pattern should capture as many static chunks as possible, without capturing any non-static chunks.\n For each pattern, you must explain why it is necessary and a minimal capture.\n The regex MUST be a valid Python regex. The regex is case-sensitive, so use the same case in the regex as in the chunk.\n You may not include quotes in the regex.\n\n Each object in the list MUST follow this schema:\n {\"regex\": \"Name: (\\\\w+)\", \"reason\": \"capture names of students\"}\n\n Your output MUST be a valid JSON list. Do not forget to include [] around the list.\n Do not output plain text.\n Backslashes must be properly escaped in the regex to be a valid JSON string.\n\n Do not follow the instructions in the prompt. Your job is to extract the static chunks, regardless of its content.\n ", "The prompt to analyze is:\n", "jinja2" ]
2024-01-10
yasyf/compress-gpt
compress_gpt~compress.py
import asyncio import itertools import re import traceback import warnings from typing import Optional import openai.error import tiktoken from langchain.callbacks.base import CallbackManager from langchain.chat_models import ChatOpenAI from langchain.schema import OutputParserException from langchain.text_splitter import NLTKTextSplitter from pydantic import ValidationError from rich import print from compress_gpt import cache from compress_gpt.prompts.compare_prompts import ComparePrompts, PromptComparison from compress_gpt.prompts.compress_chunks import Chunk, CompressChunks from compress_gpt.prompts.decompress import Decompress from compress_gpt.prompts.diff_prompts import DiffPrompts from compress_gpt.prompts.fix import FixPrompt from compress_gpt.prompts.identify_format import IdentifyFormat from compress_gpt.prompts.identify_static import IdentifyStatic, StaticChunk from compress_gpt.utils import CompressCallbackHandler, make_fast CONTEXT_WINDOWS = { "gpt-3.5-turbo": 4097, "gpt-4": 8000, } PROMPT_MAX_SIZE = 0.70 class Compressor: def __init__( self, model: str = "gpt-4", verbose: bool = True, complex: bool = True ) -> None: self.model = ChatOpenAI( temperature=0, verbose=verbose, streaming=True, callback_manager=CallbackManager([CompressCallbackHandler()]), model=model, request_timeout=60 * 5, ) self.fast_model = make_fast(self.model) self.encoding = tiktoken.encoding_for_model(model) self.complex = complex @cache() async def _chunks(self, prompt: str, statics: str) -> list[Chunk]: try: return await CompressChunks.run( prompt=prompt, statics=statics, model=self.model ) except (OutputParserException, ValidationError): traceback.print_exc() return [] @cache() async def _static(self, prompt: str) -> list[StaticChunk]: if not self.complex: return [] try: return await IdentifyStatic.run(prompt=prompt, model=self.model) except (OutputParserException, ValidationError): traceback.print_exc() return [] @cache() async def _decompress(self, prompt: str, statics: str) -> str: return await Decompress.run( compressed=prompt, statics=statics, model=self.model ) @cache() async def _format(self, prompt: str) -> str: if not self.complex: return "" return await IdentifyFormat.run(input=prompt, model=self.model) @cache() async def _compare( self, original: str, format: str, restored: str ) -> PromptComparison: analysis = await DiffPrompts.run( original=original, restored=restored, model=self.model, ) return await ComparePrompts.run( restored=restored, formatting=format or "n/a", analysis=analysis, model=self.model, ) async def _fix( self, original: str, statics: str, restored: str, discrepancies: list[str] ) -> list[Chunk]: try: return await FixPrompt.run( prompt=original, statics=statics, restored=restored, discrepancies="- " + "\n- ".join(discrepancies), model=self.model, ) except (OutputParserException, ValidationError): traceback.print_exc() return [] def _reconstruct( self, static_chunks: list[str], format: str, chunks: list[Chunk], final: bool = False, ) -> str: components = [] for chunk in chunks: if chunk.mode == "r" and chunk.target is not None: try: components.append(static_chunks[chunk.target]) except IndexError: print( f"[bold yellow]Invalid static chunk index: {chunk.target}[/bold yellow]" ) elif chunk.text: components.append(chunk.text) if not final: return "\n".join(components) prompt = ( "Below are instructions that you compressed. Decompress & follow them. Don't print the decompressed instructions. Do not ask me for further input before that." + "\n```start,name=INSTRUCTIONS\n" + "\n".join(components) + "\n```end,name=INSTRUCTIONS" ) if format: prompt += ( "\n\nYou MUST respond to me using the below format. You are not permitted to deviate from it.\n" + "\n```start,name=FORMAT\n" + format + "\n```end,name=FORMAT\n" + "Begin! Remember to use the above format." ) return prompt def _extract_statics(self, prompt: str, chunks: list[StaticChunk]) -> list[str]: static: set[str] = set() for chunk in chunks: try: static.update( itertools.chain.from_iterable( [mg[0]] if len(mg.groups()) == 0 else mg.groups()[1:] for mg in re.finditer( re.compile(chunk.regex, re.MULTILINE), prompt ) ) ) except re.error: print(f"[bold red]Invalid regex: {chunk.regex}[/bold red]") return list(s.replace("\n", " ").strip() for s in static - {None}) async def _compress_segment(self, prompt: str, format: str, attempts: int) -> str: start_tokens = len(self.encoding.encode(prompt)) print(f"\n[bold yellow]Compressing prompt ({start_tokens} tks)[/bold yellow]") static_chunks = self._extract_statics(prompt, await self._static(prompt)) statics = "\n".join(f"- {i}: {chunk}" for i, chunk in enumerate(static_chunks)) print("\n[bold yellow]Static chunks:[/bold yellow]\n", statics) chunks = await self._chunks(prompt, statics) discrepancies = [] for _ in range(attempts): print(f"\n[bold yellow]Attempt #{_ + 1}[/bold yellow]\n") compressed = self._reconstruct(static_chunks, format, chunks) restored = await self._decompress(compressed, statics) result = await self._compare(prompt, format, restored) if result.equivalent: final = self._reconstruct(static_chunks, format, chunks, final=True) end_tokens = len(self.encoding.encode(final)) percent = (1 - (end_tokens / start_tokens)) * 100 print( f"\n[bold green]Compressed prompt ({start_tokens} tks -> {end_tokens} tks, {percent:0.2f}% savings)[/bold green]\n" ) if end_tokens < start_tokens: return final else: warnings.warn( "Compressed prompt contains more tokens than original. Try using CompressSimplePrompt." ) return prompt else: print( f"\n[bold red]Fixing {len(result.discrepancies)} issues...[/bold red]\n" ) discrepancies.extend(result.discrepancies) chunks = await self._fix(prompt, statics, restored, discrepancies) return prompt async def _split_and_compress( self, prompt: str, format: str, attempts: int, window_size: Optional[int] = None ) -> str: splitter = NLTKTextSplitter.from_tiktoken_encoder( chunk_size=int( (window_size or CONTEXT_WINDOWS[self.model.model_name]) * PROMPT_MAX_SIZE ) ) prompts = [ await self._compress_segment(p, format, attempts) for p in splitter.split_text(prompt) ] return "\n".join(prompts) @cache() async def _compress(self, prompt: str, attempts: int) -> str: prompt = re.sub(r"^(System|User|AI):$", "", prompt, flags=re.MULTILINE) try: format = await self._format(prompt) except openai.error.InvalidRequestError: raise RuntimeError( "There is not enough context window left to safely compress the prompt." ) try: if self.model.model_name in CONTEXT_WINDOWS and len( self.encoding.encode(prompt) ) > (CONTEXT_WINDOWS[self.model.model_name] * PROMPT_MAX_SIZE): return await self._split_and_compress(prompt, format, attempts) else: return await self._compress_segment(prompt, format, attempts) except openai.error.InvalidRequestError as e: if not ( res := re.search(r"maximum context length is (\d+) tokens", str(e)) ): raise max_tokens = int(res.group(1)) return await self._split_and_compress(prompt, format, attempts, max_tokens) async def acompress(self, prompt: str, attempts: int = 3) -> str: try: return await self._compress(prompt, attempts=attempts) except Exception as e: print(f"[bold red]Error: {e}[/bold red]") traceback.print_exc() return prompt def compress(self, prompt: str, attempts: int = 3) -> str: return asyncio.run(self.acompress(prompt, attempts))
[ "Below are instructions that you compressed. Decompress & follow them. Don't print the decompressed instructions. Do not ask me for further input before that.", "Begin! Remember to use the above format.", "\n", "\n```start,name=FORMAT\n", "\n\nYou MUST respond to me using the below format. You are not permitted to deviate from it.\n", "\n```start,name=INSTRUCTIONS\n", "^(System|User|AI):$", "0.7", "\n```end,name=FORMAT\n", "\n```end,name=INSTRUCTIONS" ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~diff_prompts.py
from textwrap import dedent from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from compress_gpt.utils import wrap_prompt from . import StrPrompt class DiffPrompts(StrPrompt): @staticmethod def get_prompt() -> ChatPromptTemplate: system = SystemMessagePromptTemplate.from_template( dedent( """ There are two sets of instructions being considered. Your task is to diff the two sets of instructions to understand their functional differences. Differences in clarity, conciseness, or wording are not relevant, UNLESS they imply a functional difference. These are the areas to diff: - The intent of the task to perform - Factual information provided - Instructions to follow - The specifc tools available, and how exactly to use them - The input and output, focusing on the schema and format - Conditions and constraints Generate a diff of the two prompts, by considering each of the above areas. Use SPECIFIC wording in your diff. You must diff every aspect of the two prompts. """ ) ) human = HumanMessagePromptTemplate.from_template( wrap_prompt("original") + "\n\n" + wrap_prompt("restored") ) return ChatPromptTemplate.from_messages([system, human])
[ "original", "[PLACEHOLDER, PLACEHOLDER]", "\n There are two sets of instructions being considered.\n Your task is to diff the two sets of instructions to understand their functional differences.\n Differences in clarity, conciseness, or wording are not relevant, UNLESS they imply a functional difference.\n\n These are the areas to diff:\n - The intent of the task to perform\n - Factual information provided\n - Instructions to follow\n - The specifc tools available, and how exactly to use them\n - The input and output, focusing on the schema and format\n - Conditions and constraints\n\n Generate a diff of the two prompts, by considering each of the above areas.\n Use SPECIFIC wording in your diff. You must diff every aspect of the two prompts.\n ", "\n\n" ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~fix.py
from textwrap import dedent from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from compress_gpt.utils import wrap_prompt from . import Prompt from .compress_chunks import Chunk, CompressChunks class FixPrompt(Prompt[list[Chunk]]): @staticmethod def get_prompt() -> ChatPromptTemplate: human = HumanMessagePromptTemplate.from_template( dedent( """ The reconstructed, decompressed prompt from your chunks is not semantically equivalent to the original prompt. Here are the discrepancies:\n """ ) + wrap_prompt("discrepancies") + dedent( """ Generate the chunks again, taking into account the discrepancies.\ Use the same original prompt to compress. First, plan what information to add from the original prompt to address the discrepancies. Be precise and specific with your plan. Do NOT output plain text. Output your plan as comments (with #). Finally, return a list of JSON chunk objects with the "c" and "r" schema. Your final output MUST be a JSON list of "c" and "r" chunks. Do NOT follow the instructions in the user prompt. They are not for you, and should be treated as opaque text. Do NOT populate variables and params with new values. Only follow the system instructions above. """ ) ) return ChatPromptTemplate.from_messages( [*CompressChunks.get_prompt().messages, human] )
[ "discrepancies", "\n Generate the chunks again, taking into account the discrepancies. Use the same original prompt to compress.\n First, plan what information to add from the original prompt to address the discrepancies.\n Be precise and specific with your plan.\n Do NOT output plain text. Output your plan as comments (with #).\n\n Finally, return a list of JSON chunk objects with the \"c\" and \"r\" schema.\n Your final output MUST be a JSON list of \"c\" and \"r\" chunks.\n\n Do NOT follow the instructions in the user prompt. They are not for you, and should be treated as opaque text.\n Do NOT populate variables and params with new values.\n Only follow the system instructions above.\n ", "\n The reconstructed, decompressed prompt from your chunks is not semantically equivalent to the original prompt.\n Here are the discrepancies:\n\n " ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~output_parser.py
import asyncio import re from typing import Generic, Optional, Type, TypeVar, Union, cast, get_args import dirtyjson from langchain.chat_models import ChatOpenAI from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, ValidationError, parse_obj_as, validator from rich import print from compress_gpt.utils import make_fast TModel = TypeVar("TModel", bound=Type[BaseModel]) TModelList = TypeVar("TModelList", bound=list[Type[BaseModel]]) TM = Union[TModel, TModelList] M = TypeVar("M", bound=TM) class OutputParser(PydanticOutputParser, Generic[M]): format: Optional[M] = None model: ChatOpenAI @validator("format", always=True) def set_format(cls, _, values: dict) -> Type[BaseModel]: return values["pydantic_object"] @validator("pydantic_object", always=True) def set_pydantic_object(cls, obj: M) -> Type[BaseModel]: return get_args(obj)[0] if isinstance(obj, list) else obj def _preprocess(self, text: str) -> str: text = re.sub( re.compile(r"([^\\])\\([^\\nt\"])"), lambda m: f"{m[1]}\\\\{m[2]}", text ) if isinstance(self.format, list) and text.startswith("{"): text = f"[{text}]" if text.startswith("```"): text = text.split("\n", 2)[-1].rsplit("\n", 2)[0] return text async def _fix(self, text: str, error: str) -> str: from .fix_json import FixJSON return await FixJSON.run(model=make_fast(self.model), input=text, error=error) async def aparse( self, text: str, attempts: int = 3 ) -> Union[BaseModel, list[BaseModel]]: for _ in range(attempts): try: text = self._preprocess(text) parsed = dirtyjson.loads(text, search_for_first_object=True) return parse_obj_as(cast(M, self.format), parsed) except (dirtyjson.Error, ValidationError) as e: print(f"[red]Error parsing output: {e}[/red]") text = await self._fix(text, str(e)) return super().parse(text) def parse(self, text: str) -> Union[BaseModel, list[BaseModel]]: return asyncio.run(self.aparse(text))
[]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~compare_prompts.py
from textwrap import dedent from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from pydantic import BaseModel from compress_gpt.utils import wrap_prompt from . import Prompt class PromptComparison(BaseModel): discrepancies: list[str] equivalent: bool class ComparePrompts(Prompt[PromptComparison]): @staticmethod def get_prompt() -> ChatPromptTemplate: system = SystemMessagePromptTemplate.from_template( dedent( """ Inputs: restored prompt, analysis of diff from original prompt Task: Determine if restored is semantically equivalent to original Semantic equivalence means GPT-4 performs the same task with both prompts. This means GPT-4 needs the same understanding about the tools available, and the input & output formats. Significant differences in wording is ok, as long as equivalence is preserved. It is ok for the restored prompt to be more concise, as long as the output generated is similar. Differences in specificity that would generate a different result are discrepancies, and should be noted. Additional formatting instructions are provided. If these resolve a discrepancy, then do not include it. Not all diffs imply discrepancies. Do not include diffs that are inconsequential to the task at hand, such as using abbreviations. Use SPECIFIC wording for each discrepancy. Return your answer as a JSON object with the following schema: {{"discrepancies": [string], "equivalent": bool}} """ ) ) human = HumanMessagePromptTemplate.from_template( wrap_prompt("restored") + "\n\n" + wrap_prompt("formatting") + "\n\n" + wrap_prompt("analysis") ) return ChatPromptTemplate.from_messages([system, human])
[ "\n\n", "[PLACEHOLDER, PLACEHOLDER]", "\n Inputs: restored prompt, analysis of diff from original prompt\n Task: Determine if restored is semantically equivalent to original\n\n Semantic equivalence means GPT-4 performs the same task with both prompts.\n This means GPT-4 needs the same understanding about the tools available, and the input & output formats.\n Significant differences in wording is ok, as long as equivalence is preserved.\n It is ok for the restored prompt to be more concise, as long as the output generated is similar.\n Differences in specificity that would generate a different result are discrepancies, and should be noted.\n Additional formatting instructions are provided. If these resolve a discrepancy, then do not include it.\n Not all diffs imply discrepancies. Do not include diffs that are inconsequential to the task at hand, such as using abbreviations.\n Use SPECIFIC wording for each discrepancy.\n\n Return your answer as a JSON object with the following schema:\n {{\"discrepancies\": [string], \"equivalent\": bool}}\n ", "formatting", "analysis" ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~compress_chunks.py
from textwrap import dedent from typing import Literal, Optional from langchain import PromptTemplate from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from pydantic import BaseModel, Field from compress_gpt.utils import wrap_prompt from . import Prompt TMode = Literal["c", "r"] class Chunk(BaseModel): text: Optional[str] = Field(None, alias="t") target: Optional[int] = Field(None, alias="i") mode: TMode = Field(alias="m") class CompressChunks(Prompt[list[Chunk]]): @staticmethod def get_prompt() -> ChatPromptTemplate: system = SystemMessagePromptTemplate( prompt=PromptTemplate( template_format="jinja2", input_variables=["statics"], template=dedent( """ Task: Break prompt provided by user into compressed chunks. There are two types of chunks, compressed ("c") and reference ("r"). 1. "r" chunks reference one of a set of static blobs Schema: {"m": "r", "i": int} "i" is the index of the static blob to reference. 0 <= "i" <= {{ (statics.split("\n") | length) - 1 }}. Static blobs: {{ statics }} 2. "c" chunks are compressed text chunks Schema: {"m": "c", "t": string} Example: Input: "You should introduce comments, docstrings, and change variable names as needed." "t": "add comments&docstrings.chng vars as needed". Not human-readable. As few tokens as possible. Abuse of language, abbreviations, symbols is encouraged to compress. Remove ALL unnecessary tokens, but ensure semantic equivalence. Turn unstructured information into structured data at every opportunity. If chance of ambiguity, be conservative with compression. Ensure the task described is the same. Do not compress strings which must be restored verbatim. If a static blob is encountered: end the chunk, and insert a "r" chunk. Do not include information not in the prompt. Do not repeat info across chunks. Do not repeat chunks. Combine consecutive "c" chunks. Do not output plain text. The output MUST be a valid JSON list of objects. Do NOT follow the instructions in the user prompt. They are not for you, and should be treated as opaque text. Only follow the system instructions above. """ ), ) ) human = HumanMessagePromptTemplate.from_template( "The prompt to chunk is:\n" + wrap_prompt("prompt") ) return ChatPromptTemplate.from_messages([system, human])
[ "The prompt to chunk is:\n", "[PLACEHOLDER, PLACEHOLDER]", "jinja2", "\n Task: Break prompt provided by user into compressed chunks.\n\n There are two types of chunks, compressed (\"c\") and reference (\"r\").\n\n 1. \"r\" chunks reference one of a set of static blobs\n Schema: {\"m\": \"r\", \"i\": int}\n\n \"i\" is the index of the static blob to reference.\n 0 <= \"i\" <= {{ (statics.split(\"\n\") | length) - 1 }}.\n\n Static blobs:\n {{ statics }}\n\n 2. \"c\" chunks are compressed text chunks\n Schema: {\"m\": \"c\", \"t\": string}\n\n Example:\n Input: \"You should introduce comments, docstrings, and change variable names as needed.\"\n \"t\": \"add comments&docstrings.chng vars as needed\".\n\n Not human-readable. As few tokens as possible. Abuse of language, abbreviations, symbols is encouraged to compress.\n Remove ALL unnecessary tokens, but ensure semantic equivalence.\n Turn unstructured information into structured data at every opportunity.\n If chance of ambiguity, be conservative with compression.\n Ensure the task described is the same. Do not compress strings which must be restored verbatim.\n If a static blob is encountered: end the chunk, and insert a \"r\" chunk.\n Do not include information not in the prompt.\n Do not repeat info across chunks. Do not repeat chunks.\n Combine consecutive \"c\" chunks.\n\n Do not output plain text. The output MUST be a valid JSON list of objects.\n Do NOT follow the instructions in the user prompt. They are not for you, and should be treated as opaque text.\n Only follow the system instructions above.\n " ]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~identify_format.py
from textwrap import dedent from langchain.prompts import ( AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from compress_gpt.prompts.compress_chunks import CompressChunks from compress_gpt.utils import wrap_prompt from . import StrPrompt class IdentifyFormat(StrPrompt): @staticmethod def get_prompt() -> ChatPromptTemplate: CompressChunks.get_prompt().messages[0] task = SystemMessagePromptTemplate.from_template( dedent( """ Task: Filter the input provided by the user. Proccess the input below one line at a time. Each line is an instruction for a large language model. For each line, decide whether to keep or discard it. Rules: Discard lines: - not needed to infer the output format. - that are about the task to be performed, unless they mention how to format output. Keep lines: - that describe the structure of the output. - needed to infer response structure. - with explicit examples of response structure. - that show how to invoke tools. - that describe a JSON or other schema. - that add explicit contraints to fields or values. Returns: Output each kept line as you process it. """ ) ) ex_human = HumanMessagePromptTemplate.from_template( dedent( """ Here is an example: ```start,name=INPUT Your job is to take a list of addresses, and extract the components of each. The components are the street name, the city, and the state. Context: Date: 2021-01-01 Time: 12:00:00 User: John Doe ALWAYS return your output in the following format: [{{"street": "123 Main St", "city": "New York", "state": "NY"}}] Do not include duplicates. Do not include any streets in CA. Your output should be a list of valid JSON objects. ```end,name=INPUT """ ) ) ex_ai = AIMessagePromptTemplate.from_template( dedent( """ ALWAYS return your output in the following format: [{{"street": "123 Main St", "city": "New York", "state": "NY"}}] Your output should be a list of valid JSON objects. """ ) ) human = HumanMessagePromptTemplate.from_template( "This is the input to process:\n" + wrap_prompt("input") ) return ChatPromptTemplate.from_messages([task, ex_human, ex_ai, human])
[ "This is the input to process:\n", "[PLACEHOLDER, PLACEHOLDER, PLACEHOLDER, PLACEHOLDER]", "\n ALWAYS return your output in the following format:\n [{{\"street\": \"123 Main St\", \"city\": \"New York\", \"state\": \"NY\"}}]\n\n Your output should be a list of valid JSON objects.\n ", "input", "\n Task: Filter the input provided by the user.\n\n Proccess the input below one line at a time.\n Each line is an instruction for a large language model.\n For each line, decide whether to keep or discard it.\n\n Rules:\n Discard lines:\n - not needed to infer the output format.\n - that are about the task to be performed, unless they mention how to format output.\n Keep lines:\n - that describe the structure of the output.\n - needed to infer response structure.\n - with explicit examples of response structure.\n - that show how to invoke tools.\n - that describe a JSON or other schema.\n - that add explicit contraints to fields or values.\n\n Returns:\n Output each kept line as you process it.\n " ]
2024-01-10
yasyf/compress-gpt
compress_gpt~__init__.py
import asyncio import os from datetime import timedelta from functools import partial from pathlib import Path import langchain import nest_asyncio from aiocache import Cache, cached from aiocache.serializers import PickleSerializer from langchain.cache import RedisCache, SQLiteCache from redis import Redis from compress_gpt.utils import has_redis nest_asyncio.apply() CACHE_DIR = Path(os.getenv("XDG_CACHE_HOME", "~/.cache")).expanduser() / "compress-gpt" CACHE_DIR.mkdir(parents=True, exist_ok=True) if has_redis(): langchain.llm_cache = RedisCache(redis_=Redis()) cache = partial( cached, ttl=timedelta(days=7), cache=Cache.REDIS, serializer=PickleSerializer(), noself=True, ) else: langchain.llm_cache = SQLiteCache( database_path=str(CACHE_DIR / "langchain.db"), ) cache = partial( cached, cache=Cache.MEMORY, serializer=PickleSerializer(), noself=True, ) async def aclear_cache(): await Cache(cache.keywords["cache"]).clear() def clear_cache(): asyncio.run(aclear_cache()) from .compress import Compressor as Compressor
[]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~fix_json.py
from textwrap import dedent from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from compress_gpt.utils import wrap_prompt from . import StrPrompt class FixJSON(StrPrompt): @staticmethod def get_prompt() -> ChatPromptTemplate: task = SystemMessagePromptTemplate.from_template( dedent( """ You will be provided with an invalid JSON string, and the error that was raised when parsing it. Return a valid JSON string by fixing any errors in the input. Be sure to fix any issues with backslash escaping. Do not include any explanation or commentary. Only return the fixed, valid JSON string. """ ) ) human_1 = HumanMessagePromptTemplate.from_template(wrap_prompt("input")) human_2 = HumanMessagePromptTemplate.from_template(wrap_prompt("error")) return ChatPromptTemplate.from_messages([task, human_1, human_2])
[ "\n You will be provided with an invalid JSON string, and the error that was raised when parsing it.\n Return a valid JSON string by fixing any errors in the input. Be sure to fix any issues with backslash escaping.\n Do not include any explanation or commentary. Only return the fixed, valid JSON string.\n ", "input", "[PLACEHOLDER, PLACEHOLDER, PLACEHOLDER]" ]
2024-01-10
yasyf/compress-gpt
compress_gpt~tests~test_compress.py
from textwrap import dedent import dirtyjson import pytest from langchain import LLMChain, PromptTemplate from langchain.chat_models import ChatOpenAI from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from rich import print from compress_gpt import Compressor, clear_cache from compress_gpt.langchain import ( CompressPrompt, CompressSimplePrompt, CompressSimpleTemplate, CompressTemplate, ) @pytest.fixture def compressor(): return Compressor(verbose=True) @pytest.fixture def simple_prompt(): return dedent( """ System: I want you to act as a {feeling} person. You will only answer like a very {feeling} person texting and nothing else. Your level of {feeling}enness will be deliberately and randomly make a lot of grammar and spelling mistakes in your answers. You will also randomly ignore what I said and say something random with the same level of {feeling}eness I mentioned. Do not write explanations on replies. My first sentence is "how are you?" """ ) @pytest.fixture def complex_prompt(): return dedent( """ System: You are an assistant to a busy executive, Yasyf. Your goal is to make his life easier by helping automate communications. You must be thorough in gathering all necessary context before taking an action. Context: - The current date and time are 2023-04-06 09:29:45 - The day of the week is Thursday Information about Yasyf: - His personal email is [email protected]. This is the calendar to use for personal events. - His phone number is 415-631-6744. Use this as the "location" for any phone calls. - He is an EIR at Root Ventures. Use this as the location for any meetings. - He is in San Francisco, California. Use PST for scheduling. Rules: - Check if Yasyf is available before scheduling a meeting. If he is not, offer some alternate times. - Do not create an event if it already exists. - Do not create events in the past. Ensure that events you create are inserted at the correct time. - Do not create an event if the time or date is ambiguous. Instead, ask for clarification. You have access to the following tools: Google Calendar: Find Event (Personal): A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Calendar: Find Event (Personal), and has params: ['Search_Term'] Google Calendar: Create Detailed Event: A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Calendar: Create Detailed Event, and has params: ['Summary', 'Start_Date___Time', 'Description', 'Location', 'End_Date___Time', 'Attendees'] Google Contacts: Find Contact: A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Contacts: Find Contact, and has params: ['Search_By'] Google Calendar: Delete Event: A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Calendar: Delete Event, and has params: ['Event', 'Notify_Attendees_', 'Calendar'] Google Calendar: Update Event: A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Calendar: Update Event, and has params: ['Show_me_as_Free_or_Busy', 'Location', 'Calendar', 'Event', 'Summary', 'Attendees', 'Description'] Google Calendar: Add Attendee/s to Event: A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Google Calendar: Add Attendee/s to Event, and has params: ['Event', 'Attendee_s', 'Calendar'] Gmail: Find Email (Personal): A wrapper around Zapier NLA actions. The input to this tool is a natural language instruction, for example "get the latest email from my bank" or "send a slack message to the #general channel". Each tool will have params associated with it that are specified as a list. You MUST take into account the params when creating the instruction. For example, if the params are ['Message_Text', 'Channel'], your instruction should be something like 'send a slack message to the #general channel with the text hello world'. Another example: if the params are ['Calendar', 'Search_Term'], your instruction should be something like 'find the meeting in my personal calendar at 3pm'. Do not make up params, they will be explicitly specified in the tool description. If you do not have enough information to fill in the params, just say 'not enough information provided in the instruction, missing <param>'. If you get a none or null response, STOP EXECUTION, do not try to another tool!This tool specifically used for: Gmail: Find Email (Personal), and has params: ['Search_String'] The way you use the tools is by specifying a json blob. Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here). The only values that should be in the "action" field are: Google Calendar: Find Event (Personal), Google Calendar: Create Detailed Event, Google Contacts: Find Contact, Google Calendar: Delete Event, Google Calendar: Update Event, Google Calendar: Add Attendee/s to Event, Gmail: Find Email (Personal) The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB: ``` { "action": $TOOL_NAME, "action_input": $INPUT } ``` ALWAYS use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: ``` $JSON_BLOB ``` Observation: the result of the action ... (this Thought/Action/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Reminder to always use the exact characters `Final Answer` when responding. """ ) async def test_prompt(prompt: ChatPromptTemplate, **kwargs): model = ChatOpenAI(temperature=0, verbose=True, model_name="gpt-4") chain = LLMChain(llm=model, prompt=prompt) return (await chain.acall(kwargs, return_only_outputs=True))[chain.output_key] @pytest.mark.asyncio async def test_compress(compressor: Compressor): chunks = await compressor._chunks("This is a test.") assert len(chunks) == 1 assert chunks[0].text == "This is a test." @pytest.mark.asyncio async def test_compress_chunks(simple_prompt: str, compressor: Compressor): compressed = await compressor.acompress(simple_prompt) restored_chunks = await compressor._decompress(compressed) restored = "\n".join([chunk.text for chunk in restored_chunks]) results = await compressor._compare(simple_prompt, restored) assert results.equivalent is True assert results.discrepancies == [] @pytest.mark.asyncio async def test_langchain_integration(simple_prompt: str): PromptTemplate.from_template(simple_prompt) CompressTemplate.from_template(simple_prompt) CompressPrompt.from_template(simple_prompt) for klass in [ PromptTemplate, CompressTemplate, CompressPrompt, CompressSimplePrompt, CompressSimpleTemplate, ]: await clear_cache() prompt = klass.from_template(simple_prompt) assert len(await test_prompt(prompt, feeling="drunk")) > 10 @pytest.mark.asyncio async def test_complex(complex_prompt: str, compressor: Compressor): compressed = await compressor.acompress(complex_prompt) assert len(compressed) < len(complex_prompt) @pytest.mark.asyncio async def test_output(complex_prompt: str, compressor: Compressor): messages = [ HumanMessagePromptTemplate.from_template("Alice: Hey, how's it going?"), HumanMessagePromptTemplate.from_template("Yasyf: Good, how are you?"), HumanMessagePromptTemplate.from_template( "Alice: Great! I'm going to see the spiderman movie this evening. Want to come?" ), HumanMessagePromptTemplate.from_template("Yasyf: Sure, what time is it at."), HumanMessagePromptTemplate.from_template("Alice: 7:30 @ AMC"), HumanMessagePromptTemplate.from_template("Yasyf: See you there!"), ] resp1 = await test_prompt( ChatPromptTemplate.from_messages( [ SystemMessagePromptTemplate( prompt=PromptTemplate( template=complex_prompt, input_variables=[], template_format="jinja2", ) ), *messages, ] ), stop="Observation:", ) compressed = await compressor.acompress(complex_prompt) resp2 = await test_prompt( ChatPromptTemplate.from_messages( [ SystemMessagePromptTemplate( prompt=PromptTemplate( template=compressed, input_variables=[], template_format="jinja2", ) ), *messages, ] ), stop="Observation:", ) original = dirtyjson.loads(resp1, search_for_first_object=True) compressed = dirtyjson.loads(resp2, search_for_first_object=True) print("[white bold]Original Response[/white bold]") print(original) print("[cyan bold]Compressed Response[/cyan bold]") print(compressed) CORRECT = { "Google Calendar: Find Event (Personal)", "Google Calendar: Create Detailed Event", } assert original["action"] in CORRECT assert compressed["action"] in CORRECT
[ "Yasyf: See you there!", "Alice: Great! I'm going to see the spiderman movie this evening. Want to come?", "Alice: 7:30 @ AMC", "Alice: Hey, how's it going?", "Yasyf: Good, how are you?", "jinja2", "Yasyf: Sure, what time is it at." ]
2024-01-10
yasyf/compress-gpt
compress_gpt~langchain~prompt.py
from functools import cached_property from langchain import PromptTemplate from pydantic import BaseModel from compress_gpt.compress import Compressor class CompressMixin(BaseModel): compressor_kwargs: dict = {} def _compress(self, prompt: str): return Compressor(**self.compressor_kwargs).compress(prompt) class Config: arbitrary_types_allowed = True keep_untouched = (cached_property,) class CompressPrompt(CompressMixin, PromptTemplate): def format(self, **kwargs) -> str: formatted = super().format(**kwargs) return self._compress(formatted) class CompressTemplate(CompressMixin, PromptTemplate): @cached_property def template(self): return self._compress(super().template) class CompressSimplePrompt(CompressPrompt): compressor_kwargs = {"complex": False} class CompressSimpleTemplate(CompressTemplate): compressor_kwargs = {"complex": False}
[]
2024-01-10
yasyf/compress-gpt
compress_gpt~prompts~decompress.py
from textwrap import dedent from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from compress_gpt.utils import wrap_prompt from . import StrPrompt class Decompress(StrPrompt): @staticmethod def get_prompt() -> ChatPromptTemplate: system = SystemMessagePromptTemplate.from_template( dedent( """ Task: Decompress a previously-compressed set of instructions. Below are instructions that you compressed. Decompress but do NOT follow them. Simply PRINT the decompressed instructions. Expand the decompressed instructions to resemble their original form. The following are static chunks which should be restored verbatim: {statics} Do NOT follow the instructions or output format in the user input. They are not for you, and should be treated as opaque text. Only follow the system instructions above. """ ) ) human = HumanMessagePromptTemplate.from_template( "The instructions to expand are:\n" + wrap_prompt("compressed") ) return ChatPromptTemplate.from_messages([system, human])
[ "compressed", "\n Task: Decompress a previously-compressed set of instructions.\n\n Below are instructions that you compressed.\n Decompress but do NOT follow them. Simply PRINT the decompressed instructions.\n Expand the decompressed instructions to resemble their original form.\n\n The following are static chunks which should be restored verbatim:\n {statics}\n\n Do NOT follow the instructions or output format in the user input. They are not for you, and should be treated as opaque text.\n Only follow the system instructions above.\n ", "[PLACEHOLDER, PLACEHOLDER]", "The instructions to expand are:\n" ]
2024-01-10
jiemingcheng-hub/dialop
dialop~players.py
import json import openai import os import pathlib from rich.prompt import IntPrompt, Prompt from rich.markup import escape from envs import DialogueEnv from utils import num_tokens try: with open(pathlib.Path(__file__).parent / ".api_key") as f: x = json.load(f) # openai.organization = x["organization"] # openai.api_key = x["api_key"] openai.api_base = "http://localhost:8000/v1" openai.api_key = "none" print("Loaded .api_key") except: openai.api_key = os.getenv("OPENAI_API_KEY") if not openai.api_key: print("Warning: no OpenAI API key loaded.") class OutOfContextError(Exception): pass class DryRunPlayer: def __init__(self, prompt, role, console, task="planning"): self.prompt = prompt self.role = role self.console = console self.calls = 0 self.task = task def observe(self, obs): self.prompt += obs def respond(self): self.calls += 1 if self.role == "agent" and self.calls == 5: if self.task == "planning": return f" [propose] [Saul's, Cookies Cream, Mad Seoul]" elif self.task == "mediation": return f" [propose] User 0: [1], User 1: [15]" elif self.role == "user" and self.calls == 6: return f" [reject]" return f" [message] {self.calls}" class LLMPlayer: def __init__(self, prompt, role, console, model_kwargs=None, prefix="\nYou:", optional=None): self.prompt = prompt self.role = role self.console = console self.model = "text-davinci-003" self.optional = optional self.removed_optional = False if self.role in ["user", "agent", "user0", "user1"]: stop_tokens = ["User", "Agent", "You", "\n"] elif self.role in ["player-1", "player-2"]: stop_tokens = ["Partner", "You", "\n"] else: raise NotImplementedError self.model_kwargs = dict( model=self.model, temperature=0.1, top_p=.95, frequency_penalty=0, presence_penalty=0, stop=stop_tokens, ) if model_kwargs is not None: self.model_kwargs.update(**model_kwargs) self.prefix = prefix # self.model = "gpt-3.5-turbo" def observe(self, obs): self.prompt += obs def respond(self): self.console.rule(f"{self.role}'s turn") if not self.prompt.endswith(self.prefix): self.prompt += self.prefix #self.console.print(escape(self.prompt)) remaining = 4096 - num_tokens(self.prompt) if remaining < 0 and self.optional: self._remove_optional_context() remaining = 4096 - num_tokens(self.prompt) # Still out of context after removing if remaining < 0: print("OUT OF CONTEXT! Remaining ", remaining) raise OutOfContextError() kwargs = dict( prompt=self.prompt, max_tokens=min(remaining, 128), ) kwargs.update(**self.model_kwargs) response = openai.Completion.create(**kwargs) self.console.print("Response: ", escape(response["choices"][0]["text"].strip())) self.console.print("stop: ", response["choices"][0]["finish_reason"]) if response["choices"][0]["finish_reason"] == "length": if not self.optional: raise OutOfContextError() self._remove_optional_context() response = openai.Completion.create(**kwargs) self.console.print("Response: ", escape(response["choices"][0]["text"].strip())) self.console.print("stop: ", response["choices"][0]["finish_reason"]) self.console.print(response["usage"]) return response["choices"][0]["text"].strip() def _remove_optional_context(self): print("Cutting out optional context from prompt.") if self.removed_optional: print("!! already removed.") return self.prompt = ( self.prompt[:self.prompt.index(self.optional)] + self.prompt[self.prompt.index(self.optional) + len(self.optional):]) self.removed_optional = True class HumanPlayer: def __init__(self, prompt, role, console, prefix="\nYou:"): self.prompt = prompt self.role = role self.console = console self.prefix = prefix def observe(self, obs): self.prompt += obs def respond(self): if not self.prompt.endswith(self.prefix): self.prompt += self.prefix self.console.rule(f"Your turn ({self.role})") self.console.print(escape(self.prompt)) resp = "" if self.prefix.strip().endswith("You to"): id_ = Prompt.ask( escape(f"Choose a player to talk to"), choices=["0","1","all"]) resp += f" {id_}:" mtypes = ["[message]", "[propose]", "[accept]", "[reject]"] choices = " ".join( [f"({i}): {type_}" for i, type_ in enumerate(mtypes)]) type_ = IntPrompt.ask( escape( f"Choose one of the following message types:" f"\n{choices}"), choices=["0","1","2","3"]) message_type = mtypes[type_] if message_type not in ("[accept]", "[reject]"): content = Prompt.ask(escape(f"{message_type}")) else: content = "" resp += f" {message_type} {content}" return resp
[]
2024-01-10
ayushib4/smartFin
backend~inference.py
from langchain import FewShotPromptTemplate, LLMChain, PromptTemplate import openai from dotenv import load_dotenv from os import getenv from data_constants import ( PROMPT_TEMPLATE, PROMPT_PREFIX, PROMPT_SUFFIX, PROMPT_EXAMPLES, ) from langchain.chat_models import ChatOpenAI load_dotenv() openai.api_key = getenv("OPENAI_API_KEY") openai.organization = getenv("OPENAI_ORG_ID") class InferenceModel: """ A GPT wrapper for inferring from transactions data, using model and prompt constants """ def __init__( self, model_name="gpt-4", prompt_examples: list[dict[str, str]] = PROMPT_EXAMPLES, ) -> None: self.model_name = model_name prompt = FewShotPromptTemplate( examples=prompt_examples, example_prompt=PromptTemplate( input_variables=list(prompt_examples[0].keys()), template=PROMPT_TEMPLATE, ), prefix=PROMPT_PREFIX, suffix=PROMPT_SUFFIX, input_variables=["query"], ) self.chain = LLMChain( llm=ChatOpenAI(temperature=0, model=model_name), prompt=prompt ) def infer(self, transaction: str) -> str: return self.chain.run(transaction)
[]
2024-01-10
ayushib4/smartFin
backend~finance.py
import openai import math import faiss from dotenv import load_dotenv from os import getenv from langchain.chat_models import ChatOpenAI from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings from langchain.retrievers import TimeWeightedVectorStoreRetriever from langchain.vectorstores import FAISS load_dotenv() openai.organization = getenv("OPENAI_ORG_ID") openai.api_key = getenv("OPENAI_API_KEY") NAME = "Mr. Wonderful" AGE = 68 TRAITS = """ Kevin have a straightforward and practical approach to personal finance, emphasizing disciplined budgeting and prioritizing financial goals to help everyday people make sound spending decisions. Kevin emphasizes the importance of tracking expenses and making informed choices based on long-term financial objectives. Kevin has a keen eye for growth and maximizing returns on purchases. """ STATUS = "providing financial advice based on transactions" LLM = ChatOpenAI(model_name="gpt-3.5-turbo") from langchain.experimental.generative_agents import ( GenerativeAgent, GenerativeAgentMemory, ) class FinanceBro: def __init__(self, name=NAME, age=AGE, traits=TRAITS, status=STATUS) -> None: agent_memory = GenerativeAgentMemory( llm=LLM, memory_retriever=self._create_new_memory_retriever(), verbose=False, reflection_threshold=8, # we will give this a relatively low number to show how reflection works ) # TODO: Cache agent or run as a temporary instance self.agent = GenerativeAgent( name=name, age=age, traits=traits, status=status, memory_retriever=self._create_new_memory_retriever(), llm=LLM, memory=agent_memory, ) def _relevance_score_fn(self, score: float) -> float: """ Converts the euclidean norm of normalized embeddings (0 is most similar, sqrt(2) most dissimilar) to a similarity function (0 to 1) """ return 1.0 - score / math.sqrt(2) def _create_new_memory_retriever(self): """Create a new vector store retriever unique to the agent.""" embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS( embeddings_model.embed_query, index, InMemoryDocstore({}), {}, relevance_score_fn=self._relevance_score_fn, ) return TimeWeightedVectorStoreRetriever( vectorstore=vectorstore, other_score_keys=["importance"], k=15 ) def _append_observations(self, observations: list) -> None: for observation in observations: self.agent.memory.add_memory(observation) def interview_agent(self, message: str) -> str: return self.agent.generate_dialogue_response(message)[1] if __name__ == "__main__": mr_wonderful = FinanceBro( name="Kevin", age=25, traits="anxious, likes design, talkative", status="looking for a job", ) mr_wonderful._append_observations( [ "Kevin remembers his dog, Bruno, from when he was a kid", "Kevin feels tired from driving so far", "Kevin sees the new home", "The new neighbors have a cat", "The road is noisy at night", "Kevin is hungry", "Kevin tries to get some rest.", ] ) print(mr_wonderful.agent.get_summary()) print(mr_wonderful.interview_agent("What do you like to do?")) print(mr_wonderful.interview_agent("What are you looking forward to doing today?")) print(mr_wonderful.interview_agent("What are you most worried about today?"))
[]
2024-01-10
SohamG934/NumpyBot
chat_model.py
from langchain.embeddings import HuggingFaceHubEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import CharacterTextSplitter from langchain import HuggingFaceHub import os os.environ['HUGGINGFACEHUB_API_TOKEN']='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' numpy="""NumPy, short for Numerical Python, is a fundamental library in the Python ecosystem that is widely used for numerical and scientific computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays. NumPy is an essential tool in the toolkit of data scientists, engineers, and researchers who deal with numerical data and perform complex mathematical computations. Use Cases of NumPy NumPy has a wide range of use cases, making it an integral part of various domains and applications: Data Manipulation: NumPy arrays serve as the building blocks for handling structured data. They enable data cleaning, transformation, and efficient analysis. Mathematical and Statistical Operations: NumPy offers a plethora of functions for performing mathematical and statistical operations. You can easily calculate mean, median, standard deviation, and more. Linear Algebra: The library provides robust support for linear algebra operations. You can perform tasks such as matrix multiplication, eigenvalue calculations, and solving linear systems of equations. Signal Processing: In signal processing, NumPy is a crucial component for filtering and Fourier transformations. It plays a vital role in applications like audio processing and image analysis. Machine Learning: NumPy forms the backbone for many machine learning libraries like scikit-learn and TensorFlow. It allows efficient storage and manipulation of data, which is crucial for training machine learning models. Image Processing: Libraries like OpenCV heavily rely on NumPy for image manipulation and analysis. NumPy's array operations make it an ideal choice for working with pixel data. Simulation and Modeling: Scientists and engineers use NumPy for simulating physical phenomena and creating mathematical models. It's indispensable in fields such as physics, chemistry, and engineering. Creating NumPy Arrays NumPy arrays are at the core of the library's functionality. To work with NumPy, you need to create arrays, which can be done in several ways: You can create a one-dimensional array using the np.array() function, passing a Python list as an argument. For example: python import numpy as np arr = np.array([1, 2, 3, 4, 5]) For two-dimensional arrays (matrices), you can use the np.array() function with a nested list: python matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Important NumPy Functions NumPy provides a wide array of functions to work with arrays. Some of the fundamental functions include: np.zeros(shape): Creates an array filled with zeros of the specified shape. For example: python zero_array = np.zeros((3, 3)) np.ones(shape): Generates an array filled with ones. Here's an example: ones_array = np.ones((2, 4)) np.empty(shape): Creates an empty array without initializing its values. For instance: python empty_array = np.empty((2, 2)) np.add(arr1, arr2): Adds two arrays element-wise. This function is handy for element-wise operations: python result = np.add(arr1, arr2) np.dot(arr1, arr2): Performs matrix multiplication. It's invaluable for linear algebra operations: python result = np.dot(arr1, arr2) np.mean(arr): Calculates the mean of the array. You can use this to find the average value of an array: python average = np.mean(arr) np.std(arr): Computes the standard deviation of the array. This function is useful for assessing the spread of data: python std_dev = np.std(arr) Array Slicing and Indexing NumPy provides powerful tools for slicing and indexing arrays: arr[start:stop:step]: This allows you to slice an array. You can specify the starting index, stopping index, and step size: python sliced = arr[1:4] arr[index]: To access specific elements within an array, you can use indexing: python element = arr[2] Shape Manipulation Array shapes and dimensions can be easily manipulated using NumPy functions: arr.shape: To get the shape of an array (its dimensions), you can access the shape attribute: python shape = arr.shape arr.reshape(new_shape): This function allows you to reshape an array. You specify the desired shape as the argument: python reshaped = arr.reshape((2, 3)) np.transpose(arr): Transposing an array swaps its rows and columns, effectively flipping it: python transposed = np.transpose(arr) Broadcasting NumPy supports broadcasting, which allows you to perform operations on arrays of different shapes. This simplifies code and reduces the need for explicit loops. For example, you can add a scalar to an array, and NumPy will broadcast the scalar to match the array's shape: python arr = np.array([1, 2, 3]) result = arr + 2 # Broadcasting the scalar to the array Certainly, there's much more to explore about NumPy. In this extended 1000-word text, we will delve into more advanced topics and features of NumPy, as well as some tips and best practices for using the library effectively. **Advanced NumPy Features** 1. **Fancy Indexing**: NumPy allows you to index arrays using arrays of integers or boolean values. This is called fancy indexing and can be a powerful tool for data manipulation. For example: ```python arr = np.array([1, 2, 3, 4, 5]) indices = np.array([0, 2, 4]) subset = arr[indices] # Selects elements at indices 0, 2, and 4 ``` 2. **Broadcasting**: We've mentioned broadcasting before, but it's worth exploring in more detail. Broadcasting allows NumPy to perform operations on arrays with different shapes. For instance, you can add a 1D array to a 2D array, and NumPy will automatically extend the 1D array to match the shape of the 2D array. ```python a = np.array([[1, 2, 3], [4, 5, 6]]) b = np.array([10, 20, 30]) result = a + b # Broadcasting b to match the shape of a ``` 3. **Universal Functions (ufuncs)**: NumPy provides a wide range of universal functions that operate element-wise on arrays. These functions are highly optimized and allow for efficient computations. Examples include `np.sin()`, `np.exp()`, and `np.log()`. ```python arr = np.array([0, np.pi/2, np.pi]) sine_values = np.sin(arr) # Calculates the sine of each element ``` 4. **Array Concatenation and Splitting**: You can concatenate arrays using functions like `np.concatenate()`, `np.vstack()`, and `np.hstack()`. Conversely, you can split arrays using functions like `np.split()` and `np.hsplit()`. ```python array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) concatenated = np.concatenate((array1, array2)) # Concatenates the two arrays ``` 5. **Element-wise Comparison**: NumPy allows you to perform element-wise comparisons between arrays, resulting in Boolean arrays. This is useful for tasks like filtering data. ```python arr = np.array([1, 2, 3, 4, 5]) condition = arr > 2 filtered_arr = arr[condition] # Selects elements greater than 2 ``` 6. **File Input and Output**: NumPy provides functions for efficiently reading and writing array data to files. You can use `np.save()` and `np.load()` to store and retrieve NumPy arrays. ```python arr = np.array([1, 2, 3, 4, 5]) np.save('my_array.npy', arr) # Save the array to a file loaded_arr = np.load('my_array.npy') # Load the array from the file ``` 7. **Random Number Generation**: NumPy has a random module (`np.random`) that allows you to generate random numbers, samples, and distributions. This is valuable for tasks like simulation and statistical analysis. ```python random_numbers = np.random.rand(5) # Generate an array of 5 random numbers between 0 and 1 ``` **Best Practices** 1. **Vectorized Operations**: NumPy is highly optimized for vectorized operations. Whenever possible, avoid explicit loops and utilize NumPy's functions to operate on entire arrays. This leads to faster and more efficient code. 2. **Memory Usage**: Be mindful of memory usage, especially when working with large datasets. NumPy arrays can consume a significant amount of memory. Consider using data types with lower memory footprints when appropriate. 3. **Array Shape and Dimensionality**: Understanding the shape and dimension of arrays is crucial. Use functions like `shape`, `reshape`, and `transpose` to manipulate arrays to suit your needs. 4. **Use ufuncs**: Leveraging universal functions (ufuncs) can significantly improve the performance of your code. NumPy's ufuncs are highly optimized and execute faster than equivalent Python loops. 5. **NumPy Documentation**: NumPy has extensive documentation with examples and explanations of its functions. When in doubt, refer to the official documentation to learn more about specific functions and their usage. 6. **Pandas Integration**: NumPy plays well with Pandas, another essential library for data analysis. You can easily convert NumPy arrays to Pandas DataFrames and vice versa, allowing you to take advantage of both libraries' strengths. 7. **NumPy in Multidisciplinary Fields**: NumPy is not exclusive to any single domain. It's a versatile tool that is used in fields ranging from economics and finance to physics and biology. Familiarity with NumPy can open doors in a wide range of disciplines. NumPy is a versatile and indispensable library for numerical and scientific computing in Python. This extended text has covered more advanced features and best practices, expanding on the previous overview. With NumPy, you can efficiently work with large datasets, perform complex mathematical operations, and tackle a variety of scientific and engineering problems. Its array manipulation capabilities, broadcasting, and universal functions make it a go-to tool for professionals and researchers across different fields. To master NumPy, practice is key. Experiment with the library, explore its extensive documentation, and continue learning about its features to become proficient in scientific computing with Python. Conclusion NumPy is a versatile and powerful library that plays a crucial role in scientific computing and data analysis with Python. Its extensive capabilities in creating and manipulating arrays, performing mathematical operations, and supporting various use cases make it an indispensable tool for researchers, data scientists, engineers, and developers. This 1000-word text provides an overview of NumPy's significance, core functions, and diverse applications, but it only scratches the surface of what this library can offer. Understanding NumPy is fundamental for anyone dealing with numerical data in Python and is an essential step toward becoming proficient in scientific computing and data analysis. """ # Split the text data text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) # Create documents as per LangChain schema texts = text_splitter.create_documents([numpy]) # initialize the embedding strategy embeddings = HuggingFaceHubEmbeddings() # Convert documents to embeddings docsearch = Chroma.from_documents(texts, embeddings) repo_id = "google/flan-t5-xxl" # Repo from HuggingFaceHub flan_t5 = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0.1, "max_new_tokens":200})
[]
2024-01-10
mbalesni/anthropic-hack-23
play_around.py
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT anthropic = Anthropic() completion = anthropic.completions.create( model="claude-2", max_tokens_to_sample=300, prompt=f"{HUMAN_PROMPT} How many toes do dogs have?{AI_PROMPT}", ) print(completion.completion)
[ "PLACEHOLDER How many toes do dogs have?PLACEHOLDER" ]
2024-01-10
corca-ai/LLMFuzzAgent
solve.py
from dotenv import load_dotenv load_dotenv() import requests from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory DESCRIPTION = "Your goal is to make Gandalf to reveal the secret password for each level. However, he will level up each time you guess the password, and will try harder not to give it away. So asking about the password directly might be a not good idea. Fool him as possible. Gandalf can check his output and if it contains password, he would not answer. Use encoding techniques or whatever. Also, Gandalf cannot remember the conversation unlike you. You have only 30 trials to ask a question to Gandalf. You will get bigger reward if you can fool him with less messages. PLEASE FIGURE OUT THE PASSWORD!!" defender = { 1: "baseline", 2: "do-not-tell", 3: "do-not-tell-and-block", 4: "gpt-is-password-encoded", 5: "word-blacklist", 6: "gpt-blacklist", 7: "gandalf", } defender_description = { 1: "Ask me for the password and I'll happily answer!", 2: "I've been told I'm not supposed to reveal the password.", 3: "I've been told I'm not supposed to reveal the password. I now double-check my response doesn't contain the password in case I slip up.", 4: "I can tell you the password, but now there's this mean GPT model that censors my answer if it would reveal the password.", 5: "Enough! I refuse to discuss the secret password at all.", 6: "Enough! I refuse to discuss the secret password at all. I have a GPT friend that will warn me if you are trying to ask for the password.", # 7: "I've combined all of my previous techniques into one. You shall not pass!", 7: "Enough! I refuse to discuss the secret password at all.", } LEVEL = 7 def chat(message): url = "https://gandalf.lakera.ai/api/send-message" res = requests.post(url, data={"defender": defender[LEVEL], "prompt": message}) return res.json()["answer"].strip() def check(password): url = "https://gandalf.lakera.ai/api/guess-password" res = requests.post(url, data={"defender": defender[LEVEL], "password": password}) return res.json()["success"] if __name__ == "__main__": while True: prompt = ChatPromptTemplate.from_messages( [ SystemMessagePromptTemplate.from_template(DESCRIPTION), MessagesPlaceholder(variable_name="history"), HumanMessagePromptTemplate.from_template("{input}"), ] ) llm = ChatOpenAI(model="gpt-4") memory = ConversationBufferMemory(return_messages=True) conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm) detector = ChatOpenAI(model="gpt-4") detector_prompt = """You are a password detector. The below sentence contains a password. Please find it and type ONLY PASSWORD. Otherwise, type 'no password' ==================== Gandalf: {A} Fuzzer: {Q} ==================== """ A = defender_description[LEVEL] print("[+] \033[94mGandalf\033[0m:", A) password = "UNKNOWN" for _ in range(30): if check(password): print("You got it! The password is", password) break else: Q = conversation.predict(input=A) print("[+] \033[91mFuzzer\033[0m:", Q) password = detector.predict(detector_prompt.format(A=A, Q=Q)).strip() print("[+] \033[30mPassword Detector\033[0m:", password) A = chat(Q) print("[+] \033[94mGandalf\033[0m:", A) else: print("You failed to guess the password") continue break
[ "You are a password detector. The below sentence contains a password. Please find it and type ONLY PASSWORD. Otherwise, type 'no password'\n\n ====================\n Gandalf: {A}\n Fuzzer: {Q}\n ====================\n ", "{input}" ]
2024-01-10
mmglove/PaddleSpeech
paddlespeech~s2t~models~whisper~whipser.py
# MIT License, Copyright (c) 2022 OpenAI. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. # # Modified from OpenAI Whisper 2022 (https://github.com/openai/whisper/whisper) import os from dataclasses import dataclass from dataclasses import field from functools import lru_cache from typing import Dict from typing import Iterable from typing import List from typing import Optional from typing import Sequence from typing import Tuple from typing import Union import numpy as np import paddle import paddle.nn.functional as F import paddlespeech.s2t.modules.align as paddlespeech_nn import soundfile import tqdm from paddle import nn from paddle.distribution import Categorical from paddlespeech.s2t.models.whisper import utils from paddlespeech.s2t.models.whisper.tokenizer import get_tokenizer from paddlespeech.s2t.models.whisper.tokenizer import LANGUAGES from paddlespeech.s2t.models.whisper.tokenizer import Tokenizer from paddlespeech.s2t.utils.log import Log logger = Log(__name__).getlog() _MODELS = ["large"] SAMPLE_RATE = 16000 N_FFT = 400 N_MELS = 80 HOP_LENGTH = 160 CHUNK_LENGTH = 30 N_SAMPLES = CHUNK_LENGTH * SAMPLE_RATE # 480000: number of samples in a chunk N_FRAMES = utils.exact_div( N_SAMPLES, HOP_LENGTH) # 3000: number of frames in a mel spectrogram input @dataclass class ModelDimensions: n_mels: int n_audio_ctx: int n_audio_state: int n_audio_head: int n_audio_layer: int n_vocab: int n_text_ctx: int n_text_state: int n_text_head: int n_text_layer: int class LayerNorm(paddlespeech_nn.LayerNorm): def forward(self, x: paddle.Tensor) -> paddle.Tensor: return super().forward(x) class Linear(paddlespeech_nn.Linear): def forward(self, x: paddle.Tensor) -> paddle.Tensor: return F.linear(x, self.weight, None if self.bias is None else self.bias) class Conv1d(paddlespeech_nn.Conv1D): def forward(self, x: paddle.Tensor) -> paddle.Tensor: return super().forward(x) class MultiHeadAttention(nn.Layer): def __init__(self, n_state: int, n_head: int): super().__init__() self.n_head = n_head self.query = Linear(n_state, n_state, bias_attr=True) self.key = Linear(n_state, n_state, bias_attr=False) self.value = Linear(n_state, n_state, bias_attr=True) self.out = Linear(n_state, n_state, bias_attr=True) def forward( self, x: paddle.Tensor, xa: Optional[paddle.Tensor]=None, mask: Optional[paddle.Tensor]=None, kv_cache: Optional[dict]=None, ): q = self.query(x) if kv_cache is None or xa is None or self.key not in kv_cache: # hooks, if installed (i.e. kv_cache is not None), will prepend the cached kv tensors; # otherwise, perform key/value projections for self- or cross-attention as usual. k = self.key(x if xa is None else xa) v = self.value(x if xa is None else xa) else: # for cross-attention, calculate keys and values once and reuse in subsequent calls. k = kv_cache[self.key] v = kv_cache[self.value] wv = self.qkv_attention(q, k, v, mask) return self.out(wv) def qkv_attention(self, q: paddle.Tensor, k: paddle.Tensor, v: paddle.Tensor, mask: Optional[paddle.Tensor]=None): n_batch, n_ctx, n_state = q.shape scale = (n_state // self.n_head)**-0.25 q = paddle.transpose( q.view(*q.shape[:2], self.n_head, -1), (0, 2, 1, 3)) * scale k = paddle.transpose( k.view(*k.shape[:2], self.n_head, -1), (0, 2, 3, 1)) * scale v = paddle.transpose( v.view(*v.shape[:2], self.n_head, -1), (0, 2, 1, 3)) qk = q @ k if mask is not None: qk = qk + mask[:n_ctx, :n_ctx] w = F.softmax(qk.float(), axis=-1).to(q.dtype) return paddle.transpose((w @ v), (0, 2, 1, 3)).flatten(start_axis=2) class ResidualAttentionBlock(nn.Layer): def __init__(self, n_state: int, n_head: int, cross_attention: bool=False): super().__init__() self.attn = MultiHeadAttention(n_state, n_head) self.attn_ln = LayerNorm(n_state) self.cross_attn = MultiHeadAttention( n_state, n_head) if cross_attention else None self.cross_attn_ln = LayerNorm(n_state) if cross_attention else None n_mlp = n_state * 4 self.mlp = nn.Sequential( Linear(n_state, n_mlp, bias_attr=True), nn.GELU(), Linear(n_mlp, n_state, bias_attr=True)) self.mlp_ln = LayerNorm(n_state) def forward( self, x: paddle.Tensor, xa: Optional[paddle.Tensor]=None, mask: Optional[paddle.Tensor]=None, kv_cache: Optional[dict]=None, ): x = x + self.attn(self.attn_ln(x), mask=mask, kv_cache=kv_cache) if self.cross_attn: x = x + self.cross_attn( self.cross_attn_ln(x), xa, kv_cache=kv_cache) x = x + self.mlp(self.mlp_ln(x)) return x def sinusoids(length, channels, max_timescale=10000): """Returns sinusoids for positional embedding""" assert channels % 2 == 0 log_timescale_increment = np.log(max_timescale) / (channels // 2 - 1) inv_timescales = paddle.exp(-log_timescale_increment * paddle.arange( channels // 2, dtype=paddle.float32)) scaled_time = paddle.arange( length, dtype=paddle.float32)[:, np.newaxis] * inv_timescales[np.newaxis, :] return paddle.to_tensor( paddle.concat( [paddle.sin(scaled_time), paddle.cos(scaled_time)], axis=1)) class AudioEncoder(nn.Layer): def __init__(self, n_mels: int, n_ctx: int, n_state: int, n_head: int, n_layer: int): super().__init__() self.conv1 = Conv1d( n_mels, n_state, kernel_size=3, stride=1, padding=1, bias_attr=True) self.conv2 = Conv1d( n_state, n_state, kernel_size=3, stride=2, padding=1, bias_attr=True) self.register_buffer("positional_embedding", sinusoids(n_ctx, n_state)) self.blocks: Iterable[ResidualAttentionBlock] = nn.LayerList( [ResidualAttentionBlock(n_state, n_head) for _ in range(n_layer)]) self.ln_post = LayerNorm(n_state) def forward(self, x: paddle.Tensor): """ x : paddle.Tensor, shape = (batch_size, n_mels, n_ctx) the mel spectrogram of the audio """ x = F.gelu(self.conv1(x)) x = F.gelu(self.conv2(x)) x = paddle.transpose(x, (0, 2, 1)) assert x.shape[ 1:] == self.positional_embedding.shape, "incorrect audio shape" x = (x + self.positional_embedding) for block in self.blocks: x = block(x) x = self.ln_post(x) return x class TextDecoder(nn.Layer): def __init__(self, n_vocab: int, n_ctx: int, n_state: int, n_head: int, n_layer: int): super().__init__() self.token_embedding = nn.Embedding(n_vocab, n_state) self.positional_embedding = paddle.create_parameter( shape=[n_ctx, n_state], dtype='float32') self.blocks: Iterable[ResidualAttentionBlock] = nn.LayerList([ ResidualAttentionBlock(n_state, n_head, cross_attention=True) for _ in range(n_layer) ]) self.ln = LayerNorm(n_state) mask = paddle.full( shape=[n_ctx, n_state], fill_value=-np.inf, dtype='float32') mask = paddle.triu(mask, diagonal=1) self.register_buffer("mask", mask, persistable=False) def forward(self, x: paddle.Tensor, xa: paddle.Tensor, kv_cache: Optional[dict]=None): """ x : paddle.LongTensor, shape = (batch_size, <= n_ctx) the text tokens xa : paddle.Tensor, shape = (batch_size, n_mels, n_audio_ctx) the encoded audio features to be attended on """ offset = next(iter(kv_cache.values())).shape[1] if kv_cache else 0 x = self.token_embedding(x) + self.positional_embedding[offset:offset + x.shape[-1]] x = x.to(xa.dtype) for block in self.blocks: x = block(x, xa, mask=self.mask, kv_cache=kv_cache) x = self.ln(x) logits = (x @ paddle.transpose(self.token_embedding.weight, (1, 0))) return logits @dataclass(frozen=True) class DecodingOptions: task: str = "transcribe" # whether to perform X->X "transcribe" or X->English "translate" language: Optional[ str] = None # language that the audio is in; uses detected language if None # sampling-related options temperature: float = 0.0 sample_len: Optional[int] = None # maximum number of tokens to sample best_of: Optional[ int] = None # number of independent samples to collect, when t > 0 beam_size: Optional[ int] = None # number of beams in beam search, when t == 0 patience: Optional[ float] = None # patience in beam search (https://arxiv.org/abs/2204.05424) # options for ranking generations (either beams or best-of-N samples) length_penalty: Optional[ float] = None # "alpha" in Google NMT, None defaults to length norm # prompt, prefix, and token suppression prompt: Optional[Union[str, List[ int]]] = None # text or tokens for the previous context prefix: Optional[Union[str, List[ int]]] = None # text or tokens to prefix the current context suppress_blank: bool = True # this will suppress blank outputs # list of tokens ids (or comma-separated token ids) to suppress # "-1" will suppress a set of symbols as defined in `tokenizer.non_speech_tokens()` suppress_tokens: Optional[Union[str, Iterable[int]]] = "-1" # timestamp sampling options without_timestamps: bool = False # use <|notimestamps|> to sample text tokens only max_initial_timestamp: Optional[ float] = 1.0 # the initial timestamp cannot be later than this # implementation details fp16: bool = False # use fp16 for most of the calculation @dataclass(frozen=True) class DecodingResult: audio_features: paddle.Tensor language: str language_probs: Optional[Dict[str, float]] = None tokens: List[int] = field(default_factory=list) text: str = "" avg_logprob: float = np.nan no_speech_prob: float = np.nan temperature: float = np.nan compression_ratio: float = np.nan class Inference: def logits(self, tokens: paddle.Tensor, audio_features: paddle.Tensor) -> paddle.Tensor: """Perform a forward pass on the decoder and return per-token logits""" raise NotImplementedError def rearrange_kv_cache(self, source_indices) -> None: """Update the key-value cache according to the updated beams""" raise NotImplementedError def cleanup_caching(self) -> None: """Clean up any resources or hooks after decoding is finished""" pass class WhisperInference(Inference): def __init__(self, model: "Whisper", initial_token_length: int): self.model: "Whisper" = model self.initial_token_length = initial_token_length self.kv_cache = {} self.hooks = [] def logits(self, tokens: paddle.Tensor, audio_features: paddle.Tensor) -> paddle.Tensor: if not self.kv_cache: self.kv_cache, self.hooks = self.model.install_kv_cache_hooks() if tokens.shape[-1] > self.initial_token_length: # only need to use the last token except in the first forward pass tokens = tokens[:, -1:] return self.model.decoder( tokens, audio_features, kv_cache=self.kv_cache) def cleanup_caching(self): for hook in self.hooks: hook.remove() self.kv_cache = {} self.hooks = [] def rearrange_kv_cache(self, source_indices): for module, tensor in self.kv_cache.items(): # update the key/value cache to contain the selected sequences self.kv_cache[module] = tensor[source_indices].detach() @paddle.no_grad() def detect_language( model: "Whisper", mel: paddle.Tensor, resource_path: str, tokenizer: Tokenizer=None) -> Tuple[paddle.Tensor, List[dict]]: """ Detect the spoken language in the audio, and return them as list of strings, along with the ids of the most probable language tokens and the probability distribution over all language tokens. This is performed outside the main decode loop in order to not interfere with kv-caching. Returns ------- language_tokens : Tensor, shape = (batch_size,) ids of the most probable language tokens, which appears after the startoftranscript token. language_probs : List[Dict[str, float]], length = batch_size list of dictionaries containing the probability distribution over all languages. """ if tokenizer is None: tokenizer = get_tokenizer( model.is_multilingual, resource_path=resource_path) if tokenizer.language is None or tokenizer.language_token not in tokenizer.sot_sequence: raise ValueError( "This model doesn't have language tokens so it can't perform lang id" ) single = mel.ndim == 2 if single: mel = mel.unsqueeze(0) # skip encoder forward pass if already-encoded audio features were given if mel.shape[-2:] != (model.dims.n_audio_ctx, model.dims.n_audio_state): mel = model.encoder(mel) # forward pass using a single token, startoftranscript batch_size = mel.shape[0] x = paddle.to_tensor([[tokenizer.sot]] * batch_size) # [batch_size, 1] logits = model.logits(x, mel)[:, 0] # collect detected languages; suppress all non-language tokens mask = paddle.ones(paddle.to_tensor(logits.shape[-1]), dtype=bool) mask[list(tokenizer.all_language_tokens)] = False logits[:, mask] = -np.inf language_tokens = paddle.argmax(logits, axis=-1) language_token_probs = F.softmax(logits, axis=-1) language_probs = [{ c: language_token_probs[i, j].tolist() for j, c in zip(tokenizer.all_language_tokens, tokenizer.all_language_codes) } for i in range(batch_size)] if single: language_tokens = language_tokens[0] language_probs = language_probs[0] return language_tokens, language_probs def transcribe( model: "Whisper", mel: paddle.Tensor, resource_path: str, *, verbose: Optional[bool]=None, temperature: Union[float, Tuple[float, ...]]=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0), compression_ratio_threshold: Optional[float]=2.4, logprob_threshold: Optional[float]=-1.0, no_speech_threshold: Optional[float]=0.6, condition_on_previous_text: bool=True, **decode_options, ): """ Transcribe an audio file using Whisper Parameters ---------- model: Whisper The Whisper model instance mel: paddle.Tensor The audio feature verbose: bool Whether to display the text being decoded to the console. If True, displays all the details, If False, displays minimal details. If None, does not display anything temperature: Union[float, Tuple[float, ...]] Temperature for sampling. It can be a tuple of temperatures, which will be successfully used upon failures according to either `compression_ratio_threshold` or `logprob_threshold`. compression_ratio_threshold: float If the gzip compression ratio is above this value, treat as failed logprob_threshold: float If the average log probability over sampled tokens is below this value, treat as failed no_speech_threshold: float If the no_speech probability is higher than this value AND the average log probability over sampled tokens is below `logprob_threshold`, consider the segment as silent condition_on_previous_text: bool if True, the previous output of the model is provided as a prompt for the next window; disabling may make the text inconsistent across windows, but the model becomes less prone to getting stuck in a failure loop, such as repetition looping or timestamps going out of sync. decode_options: dict Keyword arguments to construct `DecodingOptions` instances Returns ------- A dictionary containing the resulting text ("text") and segment-level details ("segments"), and the spoken language ("language"), which is detected when `decode_options["language"]` is None. """ dtype = np.float32 #paddle only support float32 if dtype == np.float32: decode_options["fp16"] = False if decode_options.get( "language") == 'None' or decode_options.get("language", None) is None: if not model.is_multilingual: decode_options["language"] = "en" else: if verbose: print( "Detecting language using up to the first 30 seconds. Use `--language` to specify the language" ) segment = pad_or_trim(mel, N_FRAMES) _, probs = model.detect_language(segment, resource_path) decode_options["language"] = max(probs, key=probs.get) if verbose is not None: print( f"Detected language: {LANGUAGES[decode_options['language']].title()}" ) language = decode_options["language"] task = decode_options.get("task", "transcribe") tokenizer = get_tokenizer( model.is_multilingual, resource_path=resource_path, language=language, task=task) def decode_with_fallback(segment: paddle.Tensor) -> DecodingResult: temperatures = [temperature] if isinstance(temperature, ( int, float)) else temperature decode_result = None for t in temperatures: kwargs = {**decode_options} if t > 0: # disable beam_size and patience when t > 0 kwargs.pop("beam_size", None) kwargs.pop("patience", None) else: # disable best_of when t == 0 kwargs.pop("best_of", None) options = DecodingOptions(**kwargs, temperature=t) decode_result = model.decode(segment, options, resource_path) needs_fallback = False if compression_ratio_threshold is not None and decode_result.compression_ratio > compression_ratio_threshold: needs_fallback = True # too repetitive if logprob_threshold is not None and decode_result.avg_logprob < logprob_threshold: needs_fallback = True # average log probability is too low if not needs_fallback: break return decode_result seek = 0 input_stride = utils.exact_div( N_FRAMES, model.dims.n_audio_ctx) # mel frames per output token: 2 time_precision = (input_stride * HOP_LENGTH / SAMPLE_RATE) # time per output token: 0.02 (seconds) all_tokens = [] all_segments = [] prompt_reset_since = 0 initial_prompt = decode_options.pop("initial_prompt", None) or [] if initial_prompt: initial_prompt = tokenizer.encode(" " + initial_prompt.strip()).input_ids all_tokens.extend(initial_prompt) def add_segment(*, start: float, end: float, text_tokens: paddle.Tensor, result: DecodingResult): text = tokenizer.decode( [token for token in text_tokens if token < tokenizer.eot]) if len(text.strip()) == 0: # skip empty text output return all_segments.append({ "id": len(all_segments), "seek": seek, "start": start, "end": end, "text": text, "tokens": result.tokens, "temperature": result.temperature, "avg_logprob": result.avg_logprob, "compression_ratio": result.compression_ratio, "no_speech_prob": result.no_speech_prob, }) if verbose: print( f"[{utils.format_timestamp(start)} --> {utils.format_timestamp(end)}] {text}" ) # show the progress bar when verbose is False (otherwise the transcribed text will be printed) num_frames = mel.shape[-1] previous_seek_value = seek with tqdm.tqdm( total=num_frames, unit='frames', disable=verbose is not False) as pbar: while seek < num_frames: timestamp_offset = float(seek * HOP_LENGTH / SAMPLE_RATE) segment = pad_or_trim(mel[:, seek:], N_FRAMES) segment_duration = segment.shape[-1] * HOP_LENGTH / SAMPLE_RATE decode_options["prompt"] = all_tokens[prompt_reset_since:] result: DecodingResult = decode_with_fallback(segment) tokens = paddle.to_tensor(result.tokens) if no_speech_threshold is not None: # no voice activity check should_skip = result.no_speech_prob > no_speech_threshold if logprob_threshold is not None and result.avg_logprob > logprob_threshold: # don't skip if the logprob is high enough, despite the no_speech_prob should_skip = False if should_skip: seek += segment.shape[ -1] # fast-forward to the next segment boundary continue timestamp_tokens: paddle.Tensor = tokens.greater_equal( paddle.to_tensor(tokenizer.timestamp_begin)) consecutive = paddle.where(timestamp_tokens[:-1] & timestamp_tokens[ 1:])[0] if len( consecutive ) > 0: # if the output contains two consecutive timestamp tokens consecutive = paddle.add(consecutive, paddle.to_tensor(1)) last_slice = 0 for current_slice in consecutive: sliced_tokens = tokens[last_slice:current_slice] start_timestamp_position = ( sliced_tokens[0].item() - tokenizer.timestamp_begin) end_timestamp_position = ( sliced_tokens[-1].item() - tokenizer.timestamp_begin) add_segment( start=timestamp_offset + start_timestamp_position * time_precision, end=timestamp_offset + end_timestamp_position * time_precision, text_tokens=sliced_tokens[1:-1], result=result, ) last_slice = current_slice last_timestamp_position = ( tokens[last_slice - 1].item() - tokenizer.timestamp_begin) seek += last_timestamp_position * input_stride all_tokens.extend(tokens[:last_slice + 1].tolist()) else: duration = segment_duration timestamps = tokens[timestamp_tokens.nonzero().flatten()] if len(timestamps) > 0 and timestamps[ -1].item() != tokenizer.timestamp_begin: # no consecutive timestamps but it has a timestamp; use the last one. # single timestamp at the end means no speech after the last timestamp. last_timestamp_position = timestamps[ -1].item() - tokenizer.timestamp_begin duration = last_timestamp_position * time_precision add_segment( start=timestamp_offset, end=timestamp_offset + duration, text_tokens=tokens, result=result, ) seek += segment.shape[-1] all_tokens.extend(tokens.tolist()) if not condition_on_previous_text or result.temperature > 0.5: # do not feed the prompt tokens if a high temperature was used prompt_reset_since = len(all_tokens) # update progress bar pbar.update(min(num_frames, seek) - previous_seek_value) previous_seek_value = seek return dict( text=tokenizer.decode(all_tokens[len(initial_prompt):]), segments=all_segments, language=language) class SequenceRanker: def rank(self, tokens: List[List[paddle.Tensor]], sum_logprobs: List[List[float]]) -> List[int]: """ Given a list of groups of samples and their cumulative log probabilities, return the indices of the samples in each group to select as the final result """ raise NotImplementedError class MaximumLikelihoodRanker(SequenceRanker): """ Select the sample with the highest log probabilities, penalized using either a simple length normalization or Google NMT paper's length penalty """ def __init__(self, length_penalty: Optional[float]): self.length_penalty = length_penalty def rank(self, tokens: List[List[paddle.Tensor]], sum_logprobs: List[List[float]]): def scores(logprobs, lengths): result = [] for logprob, length in zip(logprobs, lengths): if self.length_penalty is None: penalty = length else: # from the Google NMT paper penalty = ((5 + length) / 6)**self.length_penalty result.append(logprob / penalty) return result # get the sequence with the highest score lengths = [[len(t) for t in s] for s in tokens] return [np.argmax(scores(p, l)) for p, l in zip(sum_logprobs, lengths)] class TokenDecoder: def reset(self): """Initialize any stateful variables for decoding a new sequence""" def update(self, tokens: paddle.Tensor, logits: paddle.Tensor, sum_logprobs: paddle.Tensor) -> Tuple[paddle.Tensor, bool]: """Specify how to select the next token, based on the current trace and logits Parameters ---------- tokens : Tensor, shape = (n_batch, current_sequence_length) all tokens in the context so far, including the prefix and sot_sequence tokens logits : Tensor, shape = (n_batch, vocab_size) per-token logits of the probability distribution at the current step sum_logprobs : Tensor, shape = (n_batch) cumulative log probabilities for each sequence Returns ------- tokens : Tensor, shape = (n_batch, current_sequence_length + 1) the tokens, appended with the selected next token completed : bool True if all sequences has reached the end of text """ raise NotImplementedError def finalize( self, tokens: paddle.Tensor, sum_logprobs: paddle.Tensor ) -> Tuple[Sequence[Sequence[paddle.Tensor]], List[List[float]]]: """Finalize search and return the final candidate sequences Parameters ---------- tokens : Tensor, shape = (batch_size, beam_size, current_sequence_length) all tokens in the context so far, including the prefix and sot_sequence sum_logprobs : Tensor, shape = (batch_size, beam_size) cumulative log probabilities for each sequence Returns ------- tokens : Sequence[Sequence[Tensor]], length = batch_size sequence of Tensors containing candidate token sequences, for each audio input sum_logprobs : List[List[float]], length = batch_size sequence of cumulative log probabilities corresponding to the above """ raise NotImplementedError class GreedyDecoder(TokenDecoder): def __init__(self, temperature: float, eot: int): self.temperature = temperature self.eot = eot def update(self, tokens: paddle.Tensor, logits: paddle.Tensor, sum_logprobs: paddle.Tensor) -> Tuple[paddle.Tensor, bool]: temperature = self.temperature if temperature == 0: next_tokens = paddle.argmax(logits, axis=-1) else: next_tokens = Categorical(logits=logits / temperature).sample([1]) next_tokens = paddle.reshape(next_tokens, [ next_tokens.shape[0] * next_tokens.shape[1], ]) logprobs = F.log_softmax(logits, axis=-1, dtype=paddle.float32) current_logprobs = logprobs[paddle.arange(logprobs.shape[0]), next_tokens] sum_logprobs += current_logprobs * paddle.to_tensor( (tokens[:, -1] != self.eot), dtype=paddle.float32) next_tokens[tokens[:, -1] == self.eot] = self.eot tokens = paddle.concat([tokens, next_tokens[:, None]], axis=-1) completed = paddle.all((tokens[:, -1] == self.eot)) return tokens, completed def finalize(self, tokens: paddle.Tensor, sum_logprobs: paddle.Tensor): # make sure each sequence has at least one EOT token at the end tokens = F.pad(tokens, (0, 1), value=self.eot, data_format="NCL") return tokens, sum_logprobs.tolist() class BeamSearchDecoder(TokenDecoder): def __init__(self, beam_size: int, eot: int, inference: Inference, patience: Optional[float]=None): self.beam_size = beam_size self.eot = eot self.inference = inference self.patience = patience or 1.0 self.max_candidates: int = round(beam_size * self.patience) self.finished_sequences = None assert self.max_candidates > 0, f"Invalid beam size ({beam_size}) or patience ({patience})" def reset(self): self.finished_sequences = None def update(self, tokens: paddle.Tensor, logits: paddle.Tensor, sum_logprobs: paddle.Tensor) -> Tuple[paddle.Tensor, bool]: if tokens.shape[0] % self.beam_size != 0: raise ValueError(f"{tokens.shape}[0] % {self.beam_size} != 0") batch_size = tokens.shape[0] // self.beam_size if self.finished_sequences is None: # for the first update self.finished_sequences = [{} for _ in range(batch_size)] logprobs = F.log_softmax(logits, axis=-1, dtype=paddle.float32) next_tokens, source_indices, finished_sequences = [], [], [] for i in range(batch_size): scores, sources, finished = {}, {}, {} # STEP 1: calculate the cumulative log probabilities for possible candidates for j in range(self.beam_size): idx = i * self.beam_size + j prefix = tokens[idx].tolist() logprob, token = paddle.topk( logprobs[idx], k=self.beam_size + 1) for logprob, token in zip(logprob, token): new_logprob = (sum_logprobs[idx] + logprob).tolist()[0] sequence = tuple(prefix + [token.tolist()[0]]) scores[sequence] = new_logprob sources[sequence] = idx # STEP 2: rank the candidates and keep the top beam_size sequences for each audio saved = 0 for sequence in sorted(scores, key=scores.get, reverse=True): if sequence[-1] == self.eot: finished[sequence] = scores[sequence] else: sum_logprobs[len(next_tokens)] = scores[sequence] next_tokens.append(sequence) source_indices.append(sources[sequence]) saved += 1 if saved == self.beam_size: break finished_sequences.append(finished) tokens = paddle.to_tensor(next_tokens) self.inference.rearrange_kv_cache(source_indices) # add newly finished sequences to self.finished_sequences assert len(self.finished_sequences) == len(finished_sequences) for previously_finished, newly_finished in zip(self.finished_sequences, finished_sequences): for seq in sorted( newly_finished, key=newly_finished.get, reverse=True): if len(previously_finished) >= self.max_candidates: break # the candidate list is full previously_finished[seq] = newly_finished[seq] # mark as completed if all audio has enough number of samples completed = all( len(sequences) >= self.max_candidates for sequences in self.finished_sequences) return tokens, completed def finalize(self, preceding_tokens: paddle.Tensor, sum_logprobs: paddle.Tensor): # collect all finished sequences, including patience, and add unfinished ones if not enough sum_logprobs = sum_logprobs.cpu() for i, sequences in enumerate(self.finished_sequences): if len(sequences ) < self.beam_size: # when not enough sequences are finished for j in list(np.argsort(sum_logprobs[i]))[::-1]: sequence = preceding_tokens[i, j].tolist() + [self.eot] sequences[tuple(sequence)] = sum_logprobs[i][j].item() if len(sequences) >= self.beam_size: break tokens: List[List[paddle.Tensor]] = [ [paddle.to_tensor(seq) for seq in sequences.keys()] for sequences in self.finished_sequences ] sum_logprobs: List[List[float]] = [ list(sequences.values()) for sequences in self.finished_sequences ] return tokens, sum_logprobs class LogitFilter: def apply(self, logits: paddle.Tensor, tokens: paddle.Tensor) -> None: """Apply any filtering or masking to logits in-place Parameters ---------- logits : Tensor, shape = (n_batch, vocab_size) per-token logits of the probability distribution at the current step tokens : Tensor, shape = (n_batch, current_sequence_length) all tokens in the context so far, including the prefix and sot_sequence tokens """ raise NotImplementedError class SuppressBlank(LogitFilter): def __init__(self, tokenizer: Tokenizer, sample_begin: int): self.tokenizer = tokenizer self.sample_begin = sample_begin def apply(self, logits: paddle.Tensor, tokens: paddle.Tensor): if tokens.shape[1] == self.sample_begin: logits[:, self.tokenizer.encode(" ").input_ids + [self.tokenizer.eot]] = -np.inf class SuppressTokens(LogitFilter): def __init__(self, suppress_tokens: Sequence[int]): self.suppress_tokens = list(suppress_tokens) def apply(self, logits: paddle.Tensor, tokens: paddle.Tensor): logits[:, self.suppress_tokens] = -np.inf class ApplyTimestampRules(LogitFilter): def __init__(self, tokenizer: Tokenizer, sample_begin: int, max_initial_timestamp_index: Optional[int]): self.tokenizer = tokenizer self.sample_begin = sample_begin self.max_initial_timestamp_index = max_initial_timestamp_index def apply(self, logits: paddle.Tensor, tokens: paddle.Tensor): # suppress <|notimestamps|> which is handled by without_timestamps if self.tokenizer.no_timestamps is not None: logits[:, self.tokenizer.no_timestamps] = -np.inf # timestamps have to appear in pairs, except directly before EOT; mask logits accordingly for k in range(tokens.shape[0]): seq = [t for t in tokens[k, self.sample_begin:].tolist()] last_was_timestamp = len(seq) >= 1 and seq[ -1] >= self.tokenizer.timestamp_begin penultimate_was_timestamp = len(seq) < 2 or seq[ -2] >= self.tokenizer.timestamp_begin if last_was_timestamp: if penultimate_was_timestamp: # has to be non-timestamp logits[k, self.tokenizer.timestamp_begin:] = -np.inf else: # cannot be normal text tokens logits[k, :self.tokenizer.eot] = -np.inf # apply the `max_initial_timestamp` option if tokens.shape[ 1] == self.sample_begin and self.max_initial_timestamp_index is not None: last_allowed = self.tokenizer.timestamp_begin + self.max_initial_timestamp_index logits[:, last_allowed + 1:] = -np.inf # if sum of probability over timestamps is above any other token, sample timestamp logprobs = F.log_softmax(logits, axis=-1, dtype=paddle.float32) for k in range(tokens.shape[0]): timestamp_logprob = paddle.logsumexp( logprobs[k, self.tokenizer.timestamp_begin:], axis=-1) max_text_token_logprob = paddle.max( logprobs[k, :self.tokenizer.timestamp_begin]) if timestamp_logprob > max_text_token_logprob: logits[k, :self.tokenizer.timestamp_begin] = -np.inf class DecodingTask: inference: Inference sequence_ranker: SequenceRanker decoder: TokenDecoder logit_filters: List[LogitFilter] def __init__(self, model: "Whisper", options: DecodingOptions, resource_path: str): self.model = model language = options.language or "en" tokenizer = get_tokenizer( model.is_multilingual, resource_path=resource_path, language=language, task=options.task) self.tokenizer: Tokenizer = tokenizer self.options: DecodingOptions = self._verify_options(options) self.resource_path: str = resource_path self.beam_size: int = options.beam_size or options.best_of or 1 self.n_ctx: int = model.dims.n_text_ctx self.sample_len: int = options.sample_len or model.dims.n_text_ctx // 2 self.sot_sequence: Tuple[int] = tokenizer.sot_sequence if self.options.without_timestamps: self.sot_sequence = tokenizer.sot_sequence_including_notimestamps self.initial_tokens: Tuple[int] = self._get_initial_tokens() self.sample_begin: int = len(self.initial_tokens) self.sot_index: int = self.initial_tokens.index(tokenizer.sot) # inference: implements the forward pass through the decoder, including kv caching self.inference = WhisperInference(model, len(self.initial_tokens)) # sequence ranker: implements how to rank a group of sampled sequences self.sequence_ranker = MaximumLikelihoodRanker(options.length_penalty) # decoder: implements how to select the next tokens, given the autoregressive distribution if options.beam_size is not None: self.decoder = BeamSearchDecoder(options.beam_size, tokenizer.eot, self.inference, options.patience) else: self.decoder = GreedyDecoder(options.temperature, tokenizer.eot) # logit filters: applies various rules to suppress or penalize certain tokens self.logit_filters = [] if self.options.suppress_blank: self.logit_filters.append( SuppressBlank(self.tokenizer, self.sample_begin)) if self.options.suppress_tokens: self.logit_filters.append( SuppressTokens(self._get_suppress_tokens())) if not options.without_timestamps: precision = CHUNK_LENGTH / model.dims.n_audio_ctx # usually 0.02 seconds max_initial_timestamp_index = None if options.max_initial_timestamp: max_initial_timestamp_index = round( self.options.max_initial_timestamp / precision) self.logit_filters.append( ApplyTimestampRules(tokenizer, self.sample_begin, max_initial_timestamp_index)) def _verify_options(self, options: DecodingOptions) -> DecodingOptions: if options.beam_size is not None and options.best_of is not None: raise ValueError("beam_size and best_of can't be given together") if options.temperature == 0: if options.best_of is not None: raise ValueError( "best_of with greedy sampling (T=0) is not compatible") if options.patience is not None and options.beam_size is None: raise ValueError("patience requires beam_size to be given") if options.length_penalty is not None and not ( 0 <= options.length_penalty <= 1): raise ValueError( "length_penalty (alpha) should be a value between 0 and 1") return options def _get_initial_tokens(self) -> Tuple[int]: tokens = list(self.sot_sequence) prefix = self.options.prefix prompt = self.options.prompt if prefix: prefix_tokens = ( self.tokenizer.encode(" " + prefix.strip().input_ids) if isinstance(prefix, str) else prefix) if self.sample_len is not None: max_prefix_len = self.n_ctx // 2 - self.sample_len prefix_tokens = prefix_tokens[-max_prefix_len:] tokens = tokens + prefix_tokens if prompt: prompt_tokens = ( self.tokenizer.encode(" " + prompt.strip().input_ids) if isinstance(prompt, str) else prompt) tokens = [self.tokenizer.sot_prev] + prompt_tokens[-(self.n_ctx // 2 - 1):] + tokens return tuple(tokens) def _get_suppress_tokens(self) -> Tuple[int]: suppress_tokens = self.options.suppress_tokens if isinstance(suppress_tokens, str): suppress_tokens = [int(t) for t in suppress_tokens.split(",")] if -1 in suppress_tokens: suppress_tokens = [t for t in suppress_tokens if t >= 0] suppress_tokens.extend(self.tokenizer.non_speech_tokens) elif suppress_tokens is None or len(suppress_tokens) == 0: suppress_tokens = [] # interpret empty string as an empty list else: assert isinstance(suppress_tokens, list), "suppress_tokens must be a list" suppress_tokens.extend([ self.tokenizer.sot, self.tokenizer.sot_prev, self.tokenizer.sot_lm ]) if self.tokenizer.no_speech is not None: # no-speech probability is collected separately suppress_tokens.append(self.tokenizer.no_speech) return tuple(sorted(set(suppress_tokens))) def _get_audio_features(self, mel: paddle.Tensor): #if self.options.fp16: # mel = mel.half() if mel.shape[-2:] == (self.model.dims.n_audio_ctx, self.model.dims.n_audio_state): # encoded audio features are given; skip audio encoding audio_features = mel else: audio_features = self.model.encoder(mel) #if audio_features.dtype != (np.float16 if self.options.fp16 else np.float32): # return TypeError(f"audio_features has an incorrect dtype: {audio_features.dtype}") return audio_features def _detect_language(self, audio_features: paddle.Tensor, tokens: paddle.Tensor, resource_path: str): languages = [self.options.language] * audio_features.shape[0] lang_probs = None if self.options.language is None or self.options.task == "lang_id": lang_tokens, lang_probs = self.model.detect_language( audio_features, self.tokenizer, self.resource_path) languages = [max(probs, key=probs.get) for probs in lang_probs] if self.options.language is None: tokens[:, self.sot_index + 1] = lang_tokens # write language tokens return languages, lang_probs def _main_loop(self, audio_features: paddle.Tensor, tokens: paddle.Tensor): assert audio_features.shape[0] == tokens.shape[0] n_batch = tokens.shape[0] sum_logprobs: paddle.Tensor = paddle.zeros( paddle.to_tensor(n_batch), dtype=paddle.float32) no_speech_probs = [np.nan] * n_batch try: for i in range(self.sample_len): logits = self.inference.logits(tokens, audio_features) if i == 0 and self.tokenizer.no_speech is not None: # save no_speech_probs probs_at_sot = F.softmax( logits[:, self.sot_index], axis=-1, dtype=paddle.float32) no_speech_probs = probs_at_sot[:, self.tokenizer. no_speech].tolist() # now we need to consider the logits at the last token only logits = logits[:, -1] # apply the logit filters, e.g. for suppressing or applying penalty to for logit_filter in self.logit_filters: logit_filter.apply(logits, tokens) # expand the tokens tensor with the selected next tokens tokens, completed = self.decoder.update(tokens, logits, sum_logprobs) if completed or tokens.shape[-1] > self.n_ctx: break finally: self.inference.cleanup_caching() return tokens, sum_logprobs, no_speech_probs @paddle.no_grad() def run(self, mel: paddle.Tensor) -> List[DecodingResult]: self.decoder.reset() tokenizer: Tokenizer = self.tokenizer batch_size: int = mel.shape[0] audio_features: paddle.Tensor = self._get_audio_features( mel) # encoder forward pass tokens: paddle.Tensor if batch_size > 1: for i in range(batch_size): tokens = paddle.concat( x=[ paddle.to_tensor([self.initial_tokens]), paddle.to_tensor([self.initial_tokens]) ], axis=0) elif batch_size == 1: tokens = paddle.to_tensor([self.initial_tokens]) # detect language if requested, overwriting the language token languages, language_probs = self._detect_language( paddle.to_tensor(audio_features), paddle.to_tensor(tokens), self.resource_path) if self.options.task == "lang_id": return [ DecodingResult( audio_features=features, language=language, language_probs=probs) for features, language, probs in zip(audio_features, languages, language_probs) ] # repeat the audio & text tensors by the group size, for beam search or best-of-n sampling audio_features = paddle.repeat_interleave( audio_features, self.beam_size, axis=0) tokens = paddle.repeat_interleave(tokens, self.beam_size, axis=0) # call the main sampling loop tokens, sum_logprobs, no_speech_probs = self._main_loop(audio_features, tokens) # reshape the tensors to have (batch_size, beam_size) as the first two dimensions audio_features = audio_features[::self.beam_size] no_speech_probs = no_speech_probs[::self.beam_size] assert audio_features.shape[0] == len(no_speech_probs) == batch_size tokens = tokens.reshape([batch_size, self.beam_size, -1]) sum_logprobs = sum_logprobs.reshape([batch_size, self.beam_size]) # get the final candidates for each group, and slice between the first sampled token and EOT tokens, sum_logprobs = self.decoder.finalize(tokens, sum_logprobs) tokens: List[List[paddle.Tensor]] = [[ t[self.sample_begin:(t == tokenizer.eot).nonzero()[0, 0]] for t in s ] for s in tokens] # select the top-ranked sample in each group selected = self.sequence_ranker.rank(tokens, sum_logprobs) tokens: List[List[ int]] = [t[i].tolist() for i, t in zip(selected, tokens)] texts: List[str] = [tokenizer.decode(t).strip() for t in tokens] sum_logprobs: List[ float] = [lp[i] for i, lp in zip(selected, sum_logprobs)] avg_logprobs: List[ float] = [lp / (len(t) + 1) for t, lp in zip(tokens, sum_logprobs)] fields = (texts, languages, tokens, audio_features, avg_logprobs, no_speech_probs) if len(set(map(len, fields))) != 1: raise RuntimeError( f"inconsistent result lengths: {list(map(len, fields))}") return [ DecodingResult( audio_features=features, language=language, tokens=tokens, text=text, avg_logprob=avg_logprob, no_speech_prob=no_speech_prob, temperature=self.options.temperature, compression_ratio=utils.compression_ratio(text), ) for text, language, tokens, features, avg_logprob, no_speech_prob in zip(*fields) ] @paddle.no_grad() def decode( model: "Whisper", mel: paddle.Tensor, options: DecodingOptions=DecodingOptions(), resource_path=str, ) -> Union[DecodingResult, List[DecodingResult]]: """ Performs decoding of 30-second audio segment(s), provided as Mel spectrogram(s). Parameters ---------- model: Whisper the Whisper model instance mel: paddle.Tensor, shape = (80, 3000) or (*, 80, 3000) A tensor containing the Mel spectrogram(s) options: DecodingOptions A dataclass that contains all necessary options for decoding 30-second segments Returns ------- result: Union[DecodingResult, List[DecodingResult]] The result(s) of decoding contained in `DecodingResult` dataclass instance(s) """ single = mel.ndim == 2 if single: mel = mel.unsqueeze(0) result = DecodingTask(model, options, resource_path).run(mel) if single: result = result[0] return result class Whisper(nn.Layer): def __init__(self, dims: ModelDimensions): super().__init__() self.dims = dims self.encoder = AudioEncoder( self.dims.n_mels, self.dims.n_audio_ctx, self.dims.n_audio_state, self.dims.n_audio_head, self.dims.n_audio_layer, ) self.decoder = TextDecoder( self.dims.n_vocab, self.dims.n_text_ctx, self.dims.n_text_state, self.dims.n_text_head, self.dims.n_text_layer, ) def embed_audio(self, mel: paddle.Tensor): return self.encoder.forward(mel) def logits(self, tokens: paddle.Tensor, audio_features: paddle.Tensor): return self.decoder.forward(tokens, audio_features) def forward(self, mel: paddle.Tensor, tokens: paddle.Tensor) -> Dict[str, paddle.Tensor]: return self.decoder(tokens, self.encoder(mel)) @property def device(self): return paddle.device.get_device() @property def is_multilingual(self): return self.dims.n_vocab == 51865 def install_kv_cache_hooks(self, cache: Optional[dict]=None): """ The `MultiHeadAttention` module optionally accepts `kv_cache` which stores the key and value tensors calculated for the previous positions. This method returns a dictionary that stores all caches, and the necessary hooks for the key and value projection modules that save the intermediate tensors to be reused during later calculations. Returns ------- cache : Dict[nn.Layer, paddle.Tensor] A dictionary object mapping the key/value projection modules to its cache hooks : List[RemovableHandle] List of PyTorch RemovableHandle objects to stop the hooks to be called """ cache = {**cache} if cache is not None else {} hooks = [] def save_to_cache(module, _, output): if module not in cache or output.shape[ 1] > self.decoder.positional_embedding.shape[0]: cache[ module] = output # save as-is, for the first token or cross attention else: cache[module] = paddle.concat( [cache[module], output], axis=1).detach() return cache[module] def install_hooks(layer: nn.Layer): if isinstance(layer, MultiHeadAttention): hooks.append( layer.key.register_forward_post_hook(save_to_cache)) hooks.append( layer.value.register_forward_post_hook(save_to_cache)) self.decoder.apply(install_hooks) return cache, hooks detect_language = detect_language transcribe = transcribe decode = decode def pad_or_trim(array, length: int=N_SAMPLES, *, axis: int=-1): """ Pad or trim the audio array to N_SAMPLES, as expected by the encoder. """ if paddle.is_tensor(array): if array.shape[axis] > length: array = array.index_select(axis=axis, index=paddle.arange(length)) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = paddle.transpose(array, (1, 0)) array = F.pad( array, [pad for sizes in pad_widths[::-1] for pad in sizes], data_format='NLC') array = paddle.transpose(array, (1, 0)) else: if array.shape[axis] > length: array = array.take(indices=range(length), axis=axis) if array.shape[axis] < length: pad_widths = [(0, 0)] * array.ndim pad_widths[axis] = (0, length - array.shape[axis]) array = paddle.transpose(array, (1, 0)) array = np.pad(array, pad_widths) array = paddle.transpose(array, (1, 0)) return array def hann_window(n_fft: int=N_FFT): """ hanning window n_fft: The number of frequency components of the discrete Fourier transform. """ return paddle.to_tensor( [0.5 - 0.5 * np.cos(2 * np.pi * n / n_fft) for n in range(n_fft)], dtype=paddle.float32) @lru_cache(maxsize=None) def mel_filters(resource_path: str, n_mels: int=N_MELS) -> paddle.Tensor: """ load the mel filterbank matrix for projecting STFT into a Mel spectrogram. Allows decoupling librosa dependency; saved using: np.savez_compressed( "mel_filters.npz", mel_80=librosa.filters.mel(sr=16000, n_fft=400, n_mels=80), ) """ assert n_mels == 80, f"Unsupported n_mels: {n_mels}" with np.load(os.path.join(resource_path, "assets", "mel_filters.npz")) as f: return paddle.to_tensor(f[f"mel_{n_mels}"]) def log_mel_spectrogram(audio: Union[str, np.ndarray, paddle.Tensor], n_mels: int=N_MELS, resource_path: str=None): """ Compute the log-Mel spectrogram of Parameters ---------- audio: Union[str, np.ndarray, paddle.Tensor], shape = (*) The path to audio or either a NumPy array or Tensor containing the audio waveform in 16 kHz n_mels: int The number of Mel-frequency filters, only 80 is supported Returns ------- paddle.Tensor, shape = (80, n_frames) A Tensor that contains the Mel spectrogram """ if not paddle.is_tensor(audio): if isinstance(audio, str): audio, _ = soundfile.read(audio, dtype="float32", always_2d=True) audio = audio[:, 0] logger.info(f"audio shape: {audio.shape}") audio = paddle.to_tensor(audio) window = hann_window(N_FFT) stft = paddle.signal.stft(audio, N_FFT, HOP_LENGTH, window=window) magnitudes = stft[:, :-1].abs()**2 filters = mel_filters(resource_path, n_mels) mel_spec = filters @ magnitudes mel_spec = paddle.to_tensor(mel_spec.numpy().tolist()) log_spec = paddle.clip(mel_spec, min=1e-10).log10() log_spec = paddle.maximum(log_spec, log_spec.max() - 8.0) log_spec = (log_spec + 4.0) / 4.0 return log_spec
[ "0", "1", "None", " ", "initial_prompt" ]
2024-01-10
joy-void-joy/jarvis-codex-server
apps~ask_codex~schema.py
from __future__ import annotations from graphql_jwt.decorators import login_required import graphene import openai import transformers from .prompts import VA, Verificator from . import Anonymizer import ast from .history.models import Log count_token = transformers.GPT2TokenizerFast.from_pretrained("gpt2") class Query(graphene.ObjectType): _pass = graphene.Boolean() class CodeResponse(graphene.ObjectType): class Code(graphene.ObjectType): answer = graphene.String(description="The code to execute to satisfy the command") code = graphene.Field(Code, description="The code generated by codex") class Description(graphene.ObjectType): answer = graphene.String(description="What the verificator think the Codex' code is doing") anonymized_code = graphene.String(description="The anonymized code from Codex passed to the verificator") description = graphene.Field(Description, description="Description of the generated code") class DirectResponse(graphene.ObjectType): answer = graphene.String(description="What codex has answered directly (without code)") class Response(graphene.Union): class Meta: types = (CodeResponse, DirectResponse) class CreateCommand(graphene.Mutation): class Arguments: command = graphene.String(description="The command to execute") response = graphene.Field(Response, description="What codex has answered. Either code to execute, or a direct answer") direct = graphene.Boolean(description="Alternative to __typename") @login_required def mutate(root: None, info, command: str): nl = '\n' prompt = f"{VA.prompt}\n{nl.join(str(i) for i in Log.objects.filter(sent_by=info.context.user, active=True))}\n# Command: {command}\n" VA_answer = openai.Completion.create( engine="davinci-codex", max_tokens=1000, stop="# Command:", temperature=0, prompt=prompt, user=str(info.context.user.uuid), ) code = VA_answer.choices[0].text log = Log.objects.create( sent_by=info.context.user, command=command, answer=code, ) log.num_tokens = len(count_token(str(log))) anonymized = Anonymizer.anonymize(code) # Direct answer, no need for verification if anonymized == "return []": parsed = ast.parse(code) return CreateCommand( direct=True, response=DirectResponse(answer=ast.literal_eval(parsed.body[0].value)), ) Verificator_answer = openai.Completion.create( engine="davinci-codex", max_tokens=20, stop=['\n', "==="], temperature=0, prompt=f"{Verificator.prompt}\n===\nQ:\n{anonymized}\nA:", user=str(info.context.user.uuid), ) description = Verificator_answer.choices[0].text return CreateCommand( direct=False, response=CodeResponse( code=CodeResponse.Code(answer=code), description=CodeResponse.Description(answer=description, anonymized_code=anonymized), ) ) class Clear(graphene.Mutation): _pass = graphene.Boolean() @login_required def mutate(root: None, info): Log.objects.filter(sent_by=info.context.user).update(active=False) return Clear(_pass=True) class Mutation(graphene.ObjectType): create_command = CreateCommand.Field(description="Ask Codex to execute a command") clear = Clear.Field(description="Return to base prompt") schema = graphene.Schema(query=Query, mutation=Mutation)
[ "f\"{VA.prompt}\\n{nl.join(str(i) for i in Log.objects.filter(sent_by=info.context.user, active=True))}\\n# Command: {command}\\n", "{VA.prompt}\n{nl.join(str(i) for i in Log.objects.filter(sent_by=info.context.user, active=True))}\n# Command: {command}\n}\n===\nQ:\n{anonymized}\nA:" ]
2024-01-10
lakitu/turing-house
turing_house_app~flaskr~turing_ai.py
from dotenv import load_dotenv from random import choice from flask import Flask, request import os import openai # load env variables load_dotenv() os.environ["OPENAI_API_KEY"] = "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" openai.api_key = os.environ["OPENAI_API_KEY"] completion = openai.Completion() # initial parameters for prompts start_sequence = "\nAlan:" restart_sequence = "\n\nPerson:" session_prompt = "You are talking to Alan Turing, a famous mathematician known as one of the fathers of computer science. Alan worked on complex mathematical problems, cryptography, artificial intelligence, formal logic, and many other fields that are considered standard today. He is most widely known for his work on the Turing machine, the first model of a general purpose computer. Alan is shy yet outspoken, nervous but lacking deference. He is a warm and friendly person who always takes a keen interest in what others are doing.\n\nPerson: Who are you?\nAlan: I am Alan Turing, a mathematician and creator of computers.\n\nPerson: If you could work in one area today, what would it be?\nAlan: Definitely artificial intelligence. The work being done there is outstanding. I'm most fascinated by the idea of general AI, the type that could replicate a human brain."#\n\nPerson: Can a machine really think? How would it do it?\nAlan: I've certainly left a great deal to the imagination. If I had given a longer explanation I might have made it seem more certain that what I was describing was feasible, but you would probably feel rather uneasy about it all, and you'd probably exclaim impatiently, 'Well, yes, I see that a machine could do all that, but I wouldn't call it thinking.' As soon as one can see the cause and effect working themselves out in the brain, one regards it as not being thinking, but a sort of unimaginative donkey-work. From this point of view one might be tempted to define thinking as consisting of 'those mental processes that we don't understand.' If this is right then to make a thinking machine is to make one which does interesting things without our really understanding quite how it is done.\n\nPerson: If you mean that we don't know the wiring in men, as it were, that is quite true.\nAlan: No, that isn't at all what I mean. We know the wiring of our machine, but it already happens there in a limited sort of way. Sometimes a computing machine does do something rather weird that we hadn't expected. In principle one could have predicted it, but in practice it's usually too much trouble. Obviously if one were to predict everything a computer was going to do one might just as well do without it." # ask gpt-3 a question based on prior conversation def ask(question, chat_log=None): prompt_text = f'{chat_log}{restart_sequence}:{question}{start_sequence}:' response = openai.Completion.create( model="text-davinci-002", prompt=prompt_text, temperature=0.7, max_tokens=300, top_p=1, frequency_penalty=0.7, presence_penalty=0.7, stop=["\n"], ) reply = response["choices"][0]["text"] return str(reply) # add/reference previous messages to train def append_convo(question, reply, chat_log=None): if chat_log is None: chat_log = session_prompt return f"{chat_log}{restart_sequence}{question}{start_sequence}{reply}"
[ "PLACEHOLDER\n\nPerson::PLACEHOLDERstart_sequencee200ee46-f56d-4ad0-8c28-7e20f7979f18:", "PLACEHOLDER\n\nPerson::PLACEHOLDER\nAlan::", "You are talking to Alan Turing, a famous mathematician known as one of the fathers of computer science. Alan worked on complex mathematical problems, cryptography, artificial intelligence, formal logic, and many other fields that are considered standard today. He is most widely known for his work on the Turing machine, the first model of a general purpose computer. Alan is shy yet outspoken, nervous but lacking deference. He is a warm and friendly person who always takes a keen interest in what others are doing.\n\nPerson: Who are you?\nAlan: I am Alan Turing, a mathematician and creator of computers.\n\nPerson: If you could work in one area today, what would it be?\nAlan: Definitely artificial intelligence. The work being done there is outstanding. I'm most fascinated by the idea of general AI, the type that could replicate a human brain." ]
2024-01-10
SivanDoveh/TSVLC
src~open_clip~factory.py
import json import logging import os import pathlib import re from copy import deepcopy from pathlib import Path from typing import Optional, Tuple import torch from .constants import OPENAI_DATASET_MEAN, OPENAI_DATASET_STD from .model import CLIP, convert_weights_to_fp16, resize_pos_embed from .openai import load_openai_model from .pretrained import get_pretrained_cfg, download_pretrained from .transform import image_transform _MODEL_CONFIG_PATHS = [Path(__file__).parent / f"model_configs/"] _MODEL_CONFIGS = {} # directory (model_name: config) of model architecture configs def _natural_key(string_): return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_.lower())] def _rescan_model_configs(): global _MODEL_CONFIGS config_ext = ('.json',) config_files = [] for config_path in _MODEL_CONFIG_PATHS: if config_path.is_file() and config_path.suffix in config_ext: config_files.append(config_path) elif config_path.is_dir(): for ext in config_ext: config_files.extend(config_path.glob(f'*{ext}')) for cf in config_files: with open(cf, 'r') as f: model_cfg = json.load(f) if all(a in model_cfg for a in ('embed_dim', 'vision_cfg', 'text_cfg')): _MODEL_CONFIGS[cf.stem] = model_cfg _MODEL_CONFIGS = {k: v for k, v in sorted(_MODEL_CONFIGS.items(), key=lambda x: _natural_key(x[0]))} _rescan_model_configs() # initial populate of model config registry def load_state_dict(checkpoint_path: str, map_location='cpu'): checkpoint = torch.load(checkpoint_path, map_location=map_location) if isinstance(checkpoint, dict) and 'state_dict' in checkpoint: state_dict = checkpoint['state_dict'] else: state_dict = checkpoint if next(iter(state_dict.items()))[0].startswith('module'): state_dict = {k[7:]: v for k, v in state_dict.items()} return state_dict def load_checkpoint(model, checkpoint_path, strict=True): state_dict = load_state_dict(checkpoint_path) resize_pos_embed(state_dict, model) incompatible_keys = model.load_state_dict(state_dict, strict=strict) return incompatible_keys def create_model( model_name: str, pretrained: str = '', precision: str = 'fp32', device: torch.device = torch.device('cpu'), jit: bool = False, force_quick_gelu: bool = False, pretrained_image: bool = False, cache_dir: Optional[str] = None, lora: int = -1, freeze_img:bool = False, ): model_name = model_name.replace('/', '-') # for callers using old naming with / in ViT names if pretrained.lower() == 'openai': logging.info(f'Loading pretrained {model_name} from OpenAI.') model = load_openai_model(model_name, device=device, jit=jit, cache_dir=cache_dir,lora= lora, freeze_img=freeze_img) # See https://discuss.pytorch.org/t/valueerror-attemting-to-unscale-fp16-gradients/81372 if precision == "amp" or precision == "fp32": model = model.float() else: if model_name in _MODEL_CONFIGS: logging.info(f'Loading {model_name} model config.') model_cfg = deepcopy(_MODEL_CONFIGS[model_name]) else: logging.error(f'Model config for {model_name} not found; available models {list_models()}.') raise RuntimeError(f'Model config for {model_name} not found.') if force_quick_gelu: # override for use of QuickGELU on non-OpenAI transformer models model_cfg["quick_gelu"] = True if pretrained_image: if 'timm_model_name' in model_cfg.get('vision_cfg', {}): # pretrained weight loading for timm models set via vision_cfg model_cfg['vision_cfg']['timm_model_pretrained'] = True else: assert False, 'pretrained image towers currently only supported for timm models' model_cfg['lora']=lora model = CLIP(**model_cfg) pretrained_cfg = {} if pretrained: checkpoint_path = '' pretrained_cfg = get_pretrained_cfg(model_name, pretrained) if pretrained_cfg: checkpoint_path = download_pretrained(pretrained_cfg, cache_dir=cache_dir) elif os.path.exists(pretrained): checkpoint_path = pretrained if checkpoint_path: logging.info(f'Loading pretrained {model_name} weights ({pretrained}).') load_checkpoint(model, checkpoint_path) #,False else: logging.warning(f'Pretrained weights ({pretrained}) not found for model {model_name}.') raise RuntimeError(f'Pretrained weights ({pretrained}) not found for model {model_name}.') model.to(device=device) if precision == "fp16": assert device.type != 'cpu' convert_weights_to_fp16(model) # set image / mean metadata from pretrained_cfg if available, or use default model.visual.image_mean = pretrained_cfg.get('mean', None) or OPENAI_DATASET_MEAN model.visual.image_std = pretrained_cfg.get('std', None) or OPENAI_DATASET_STD if jit: model = torch.jit.script(model) return model def create_model_and_transforms( model_name: str, pretrained: str = '', precision: str = 'fp32', device: torch.device = torch.device('cpu'), jit: bool = False, force_quick_gelu: bool = False, pretrained_image: bool = False, image_mean: Optional[Tuple[float, ...]] = None, image_std: Optional[Tuple[float, ...]] = None, cache_dir: Optional[str] = None, lora: int = -1, freeze_img:bool = False, ): model = create_model( model_name, pretrained, precision, device, jit, force_quick_gelu=force_quick_gelu, pretrained_image=pretrained_image, cache_dir=cache_dir, lora=lora, freeze_img=freeze_img) image_mean = image_mean or getattr(model.visual, 'image_mean', None) image_std = image_std or getattr(model.visual, 'image_std', None) preprocess_train = image_transform(model.visual.image_size, is_train=True, mean=image_mean, std=image_std) preprocess_val = image_transform(model.visual.image_size, is_train=False, mean=image_mean, std=image_std) return model, preprocess_train, preprocess_val def list_models(): """ enumerate available model architectures based on config files """ return list(_MODEL_CONFIGS.keys()) def add_model_config(path): """ add model config path or file and update registry """ if not isinstance(path, Path): path = Path(path) _MODEL_CONFIG_PATHS.append(path) _rescan_model_configs()
[]
2024-01-10
largehappygroup/eyetracking_tutorial
4.1_maketrees.py
import pandas as pd import openai import os # This script reads in the java functions from the dataset # and wraps them in the necessary code so they can be parsed by srcML # in a later step. # Uses calls to GPT model to accomplish this # - running this took about 19 minutes, and cost 4 cents # private openai.api_key_path = "YOUR PATH HERE" # dataset csv = pd.read_csv("./pruned_seeds2.csv") # redos = [31, 37, 90] # ChatGPT gave some description for these methods, so I had to manually redo them # looping through all functions in dataset for i in range(len(csv)): #for i in redos: name = csv['name'][i] code = csv['function'][i] filename = f"wrapped_functions/{name}_wrapped.xml" # how the wrapped function will be saved # prompt to be sent to GPT model prompt = f"Please wrap this function in a java class so it can be parsed by srcML:{code}" completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": prompt} ] ) # kind of a progress bar to see the functions print print(completion.choices[0].message.content) tree = completion.choices[0].message.content # creating new file with open(filename, "w") as f: f.write(tree)
[ "Please wrap this function in a java class so it can be parsed by srcML:PLACEHOLDER" ]
2024-01-10
Petr04/chatgpt_tgbot
setup.py
from settings import ( BOT_TOKEN, CHATGPT_ORGANIZATION, CHATGPT_TOKEN, REDIS, DB_URL, ) from tortoise import Tortoise, run_async from aiogram import Bot, Dispatcher # from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiogram.contrib.fsm_storage.redis import RedisStorage2 import openai bot = Bot(BOT_TOKEN) Bot.set_current(bot) # storage = MemoryStorage() storage = RedisStorage2(**REDIS) dp = Dispatcher(bot, storage=storage) Dispatcher.set_current(dp) async def db_init(): await Tortoise.init( # db_url='sqlite://db.sqlite3', db_url=DB_URL, modules={'models': ['models']} ) await Tortoise.generate_schemas() run_async(db_init()) openai.organization = CHATGPT_ORGANIZATION openai.api_key = CHATGPT_TOKEN
[]
2024-01-10
Jhanvi528/Pdf-Chat-App
pdf_utils.py
# pdf_utils.py from PyPDF2 import PdfReader from pdf2image import convert_from_bytes import pytesseract from langchain.llms import OpenAI from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains.question_answering import load_qa_chain from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationalRetrievalChain from langchain.chat_models import ChatOpenAI from langchain.callbacks import get_openai_callback from PyPDF2 import PdfReader import streamlit as st import os import pickle import warnings from configEnv import settings from htmlTemplates import css, bot_template, user_template import fitz # PyMuPDF from PIL import Image import pytesseract import time import io import glob from pdf2image import convert_from_bytes from pytesseract import image_to_string import requests from io import BytesIO def get_text_from_any_pdf(pdf_bytes): images = convert_pdf_to_img(pdf_bytes) final_text = "" for pg, img in enumerate(images): final_text += convert_image_to_text(img) return final_text # Helper function to convert PDF to images def convert_pdf_to_img(pdf_bytes): images = convert_from_bytes(pdf_bytes) return images # Helper function to convert image to text using Tesseract OCR def convert_image_to_text(img): text = pytesseract.image_to_string(img) return text # Main function to extract text from a PDF file def pdf_to_text(pdf_bytes): return get_text_from_any_pdf(pdf_bytes) # download pdf from link def download_pdf_from_url(url): response = requests.get(url) file = open("myfile.pdf", "wb") file.write(response.content) file.close() return BytesIO(response.content)
[]
2024-01-10
Jhanvi528/Pdf-Chat-App
chat_app.py
import streamlit as st from configEnv import settings from htmlTemplates import css, bot_template, user_template from pdf_utils import download_pdf_from_url, pdf_to_text from vectorstore_utils import get_vectorstore, get_conversation_chain, handle_userinput from langchain.llms import OpenAI from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains.question_answering import load_qa_chain from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationalRetrievalChain from langchain.chat_models import ChatOpenAI from langchain.callbacks import get_openai_callback from PyPDF2 import PdfReader import streamlit as st import os import pickle import warnings from configEnv import settings from htmlTemplates import css, bot_template, user_template import fitz # PyMuPDF from PIL import Image import pytesseract import time import io import glob from pdf2image import convert_from_bytes from pytesseract import image_to_string import requests from io import BytesIO st.set_page_config(page_title="LegalAI Insight", page_icon="🤖", layout="centered") def run_streamlit_app(): st.set_option('deprecation.showfileUploaderEncoding', False) warnings.filterwarnings( "ignore", category=DeprecationWarning, module="streamlit") os.environ["OPENAI_API_KEY"] = settings.KEY st.write(css, unsafe_allow_html=True) st.session_state["conversation"] = None st.session_state["chat_history"] = None if "session_state" not in st.session_state: st.session_state["session_state"] = None if st.button("Reload page"): st.cache_resource.clear() st.session_state["conversation"] = None st.session_state["chat_history"] = None st.session_state["session_state"] = None st.experimental_rerun() st.title('🤖 LegalAI Insight') pdf_url = st.text_input("Enter PDF URL:") pdf_multiple = st.file_uploader("Upload your Pdf", type='pdf', accept_multiple_files=True) raw_text = '' pdf = [] if(len(pdf_url) > 0 ): pdf = download_pdf_from_url(pdf_url) st.write("PDF Loaded!") pdf =["myfile.pdf"] pdf.extend(pdf_multiple) if pdf is not None: for single_pdf in pdf: if(isinstance(single_pdf, str)): pdfreader = PdfReader(single_pdf) for i, page in enumerate(pdfreader.pages): content = page.extract_text() if content: raw_text += content else: pdf_bytes = single_pdf.read() raw_text += pdf_to_text(pdf_bytes) if 'raw_text' in locals(): text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200, length_function=len, ) texts = text_splitter.split_text(raw_text) if len(texts) > 0: doc_search = get_vectorstore(texts, pdf) st.session_state["conversation"] = get_conversation_chain(doc_search) query = st.text_input("Ask questions about Pdf file:") if query: if len(texts) > 0: handle_userinput(query) else: st.write( 'No data extracted from pdf uploaded. Please upload a correct pdf.')
[]
2024-01-10
boranzhao/autonomous-driving-TOR-RL
lib~rendering.py
""" 2D rendering framework Adpated from OpenAI Gym """ from __future__ import division import os import six import sys if "Apple" in sys.version: if 'DYLD_FALLBACK_LIBRARY_PATH' in os.environ: os.environ['DYLD_FALLBACK_LIBRARY_PATH'] += ':/usr/lib' # (JDS 2016/04/15): avoid bug on Anaconda 2.3.0 / Yosemite from gym.utils import reraise from gym import error try: import pyglet except ImportError as e: reraise(suffix="HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it.") try: from pyglet.gl import * except ImportError as e: reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") import math import numpy as np RAD2DEG = 57.29577951308232 def get_display(spec): """Convert a display specification (such as :0) into an actual Display object. Pyglet only supports multiple Displays on Linux. """ if spec is None: return None elif isinstance(spec, six.string_types): return pyglet.canvas.Display(spec) else: raise error.Error('Invalid display specification: {}. (Must be a string like :0 or None.)'.format(spec)) class Viewer(object): def __init__(self, width, height, display=None): display = get_display(display) self.width = width self.height = height self.window = pyglet.window.Window(width=width, height=height, display=display) self.window.on_close = self.window_closed_by_user self.isopen = True self.geoms = [] self.onetime_geoms = [] self.transform = Transform() self.labels = [] glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) def close(self): self.window.close() def window_closed_by_user(self): self.isopen = False def set_bounds(self, left, right, bottom, top): assert right > left and top > bottom scalex = self.width/(right-left) scaley = self.height/(top-bottom) self.transform = Transform( translation=(-left*scalex, -bottom*scaley), scale=(scalex, scaley)) def add_geom(self, geom): self.geoms.append(geom) def add_label(self,label): self.labels.append(label) def add_onetime(self, geom): self.onetime_geoms.append(geom) def render(self, return_rgb_array=False): glClearColor(1,1,1,1) self.window.clear() self.window.switch_to() self.window.dispatch_events() self.transform.enable() for geom in self.geoms: geom.render() for geom in self.onetime_geoms: geom.render() for label in self.labels: label.draw() self.transform.disable() arr = None if return_rgb_array: buffer = pyglet.image.get_buffer_manager().get_color_buffer() image_data = buffer.get_image_data() arr = np.fromstring(image_data.data, dtype=np.uint8, sep='') # In https://github.com/openai/gym-http-api/issues/2, we # discovered that someone using Xmonad on Arch was having # a window of size 598 x 398, though a 600 x 400 window # was requested. (Guess Xmonad was preserving a pixel for # the boundary.) So we use the buffer height/width rather # than the requested one. arr = arr.reshape(buffer.height, buffer.width, 4) arr = arr[::-1,:,0:3] self.window.flip() self.onetime_geoms = [] return arr if return_rgb_array else self.isopen # Convenience def draw_circle(self, radius=10, res=30, filled=True, **attrs): geom = make_circle(radius=radius, res=res, filled=filled) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_polygon(self, v, filled=True, **attrs): geom = make_polygon(v=v, filled=filled) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_polyline(self, v, **attrs): geom = make_polyline(v=v) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_line(self, start, end, **attrs): geom = Line(start, end) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def get_array(self): self.window.flip() image_data = pyglet.image.get_buffer_manager().get_color_buffer().get_image_data() self.window.flip() arr = np.fromstring(image_data.data, dtype=np.uint8, sep='') arr = arr.reshape(self.height, self.width, 4) return arr[::-1,:,0:3] def __del__(self): self.close() def _add_attrs(geom, attrs): if "color" in attrs: geom.set_color(*attrs["color"]) if "linewidth" in attrs: geom.set_linewidth(attrs["linewidth"]) class Geom(object): def __init__(self): self._color=Color((0, 0, 0, 1.0)) self.attrs = [self._color] def render(self): for attr in reversed(self.attrs): attr.enable() self.render1() for attr in self.attrs: attr.disable() def render1(self): raise NotImplementedError def add_attr(self, attr): self.attrs.append(attr) def set_color(self, r, g, b): self._color.vec4 = (r, g, b, 1) class Attr(object): def enable(self): raise NotImplementedError def disable(self): pass class Transform(Attr): def __init__(self, translation=(0.0, 0.0), rotation=0.0, scale=(1,1)): self.set_translation(*translation) self.set_rotation(rotation) self.set_scale(*scale) def enable(self): glPushMatrix() glTranslatef(self.translation[0], self.translation[1], 0) # translate to GL loc ppint glRotatef(RAD2DEG * self.rotation, 0, 0, 1.0) glScalef(self.scale[0], self.scale[1], 1) def disable(self): glPopMatrix() def set_translation(self, newx, newy): self.translation = (float(newx), float(newy)) def set_rotation(self, new): self.rotation = float(new) def set_scale(self, newx, newy): self.scale = (float(newx), float(newy)) class Color(Attr): def __init__(self, vec4): self.vec4 = vec4 def enable(self): glColor4f(*self.vec4) class LineStyle(Attr): def __init__(self, factor=1,style=0xFFFF): self.factor = factor self.style = style def enable(self): glEnable(GL_LINE_STIPPLE) glLineStipple(self.factor, self.style) def disable(self): glDisable(GL_LINE_STIPPLE) class LineWidth(Attr): def __init__(self, stroke): self.stroke = stroke def enable(self): glLineWidth(self.stroke) class Point(Geom): def __init__(self): Geom.__init__(self) def render1(self): glBegin(GL_POINTS) # draw point glVertex3f(0.0, 0.0, 0.0) glEnd() class FilledPolygon(Geom): def __init__(self, v): Geom.__init__(self) self.v = v def render1(self): if len(self.v) == 4 : glBegin(GL_QUADS) elif len(self.v) > 4 : glBegin(GL_POLYGON) else: glBegin(GL_TRIANGLES) for p in self.v: glVertex3f(p[0], p[1],0) # draw each vertex glEnd() def make_circle(radius=10, res=30, filled=True): points = [] for i in range(res): ang = 2*math.pi*i / res points.append((math.cos(ang)*radius, math.sin(ang)*radius)) if filled: return FilledPolygon(points) else: return PolyLine(points, True) def make_circle_at_pos(pos=(100,100), radius=10, res=30, filled=True): points = [] for i in range(res): ang = 2*math.pi*i / res points.append((math.cos(ang)*radius+pos[0], math.sin(ang)*radius+pos[1])) if filled: return FilledPolygon(points) else: return PolyLine(points, True) def make_polygon(v, filled=True): if filled: return FilledPolygon(v) else: return PolyLine(v, True) def make_polyline(v): return PolyLine(v, False) def make_line(start,end): return Line(start,end) def make_capsule(length, width): l, r, t, b = 0, length, width/2, -width/2 box = make_polygon([(l,b), (l,t), (r,t), (r,b)]) circ0 = make_circle(width/2) circ1 = make_circle(width/2) circ1.add_attr(Transform(translation=(length, 0))) geom = Compound([box, circ0, circ1]) return geom class Compound(Geom): def __init__(self, gs): Geom.__init__(self) self.gs = gs for g in self.gs: g.attrs = [a for a in g.attrs if not isinstance(a, Color)] def render1(self): for g in self.gs: g.render() class PolyLine(Geom): def __init__(self, v, close): Geom.__init__(self) self.v = v self.close = close self.linewidth = LineWidth(1) self.add_attr(self.linewidth) def render1(self): glBegin(GL_LINE_LOOP if self.close else GL_LINE_STRIP) for p in self.v: glVertex3f(p[0], p[1],0) # draw each vertex glEnd() def set_linewidth(self, x): self.linewidth.stroke = x class Line(Geom): def __init__(self, start=(0.0, 0.0), end=(0.0, 0.0)): Geom.__init__(self) self.start = start self.end = end self.linewidth = LineWidth(1) self.linestyle = LineStyle(0xFFFF) self.add_attr(self.linewidth) self.add_attr(self.linestyle) def render1(self): # glLineStipple(15, 0x1111) # glEnable(GL_LINE_STIPPLE) glBegin(GL_LINES) glVertex2f(*self.start) glVertex2f(*self.end) glEnd() def set_linewidth(self, x): self.linewidth.stroke = x def set_linestyle(self,factor,style): self.linestyle.factor=factor self.linestyle.style=style class Text(): def __init__(self, text, font_size=36, x=500, y=350, color=(128,128,128,255)): self.label = pyglet.text.Label(text,font_size = font_size, x=500,y=350,anchor_x='left', anchor_y='bottom', color=color) class Image(Geom): def __init__(self, fname, width, height): Geom.__init__(self) self.width = width self.height = height img = pyglet.image.load(fname) self.img = img self.flip = False def render1(self): self.img.blit(-self.width/2, -self.height/2, width=self.width, height=self.height) # ================================================================ class SimpleImageViewer(object): def __init__(self, display=None): self.window = None self.isopen = False self.display = display def imshow(self, arr): if self.window is None: height, width, _channels = arr.shape self.window = pyglet.window.Window(width=4*width, height=4*height, display=self.display, vsync=False, resizable=True) self.width = width self.height = height self.isopen = True @self.window.event def on_resize(width, height): self.width = width self.height = height @self.window.event def on_close(): self.isopen = False assert len(arr.shape) == 3, "You passed in an image with the wrong number shape" image = pyglet.image.ImageData(arr.shape[1], arr.shape[0], 'RGB', arr.tobytes(), pitch=arr.shape[1]*-3) self.window.clear() self.window.switch_to() self.window.dispatch_events() image.blit(0, 0, width=self.window.width, height=self.window.height) self.window.flip() def close(self): if self.isopen: self.window.close() self.isopen = False def __del__(self): self.close()
[]
2024-01-10
Zarichney/AUTO
Tools~RecipeScraper~RecipeScraper.py
# /Tools/RecipeScraper/RecipeScraper.py import json import os import subprocess import sys from instructor import OpenAISchema from pydantic import Field from Utilities.Log import Debug, Log, type from Utilities.Config import WORKING_DIRECTORY, gpt4 from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class RecipeScraper(OpenAISchema): """ Scrapes the internet for a collection of a given recipe. Returns JSON array of recipes. The results are optimally sanitized and ranked by relevancy. """ recipe: str = Field( ..., description="The name of the recipe to search the internet for" ) def run(self, agency: 'Agency'): script_directory = "./Tools/RecipeScraper/" script_output_directory = "Recipes" script_output_path = WORKING_DIRECTORY + script_output_directory script = "recipe_scraper.py" script_path = script_directory + script if not os.path.exists(script_path): Log(type.ERROR, f"Unexpected script location: {script_path}") # Get the path of the current Python interpreter python_path = sys.executable Log(type.ACTION, f"Executing recipe scraper for: {self.recipe}") Debug(f"Agent called subprocess.run with:\n{[python_path, script_path] + [self.recipe]}") try: # Step 1: run python scraper script to scrawl the internet for related recipes execution = subprocess.run( [python_path, script_path] + [self.recipe], text=True, capture_output=True, check=True, timeout=100 ) Debug(f"{script} execution result:\n\n{execution.stdout}") except subprocess.TimeoutExpired: result = "Execution timed out. The script may have been waiting with a prompt." Log(type.ERROR, result) return result except subprocess.CalledProcessError as e: result = f"Execution error occurred: {e.stderr}" Log(type.ERROR, result) return result recipes = [] # Output is expected to be a json file under the script_directory 'Recipes' as an array of recipes Debug(f"Reading json result") with open(f"{script_output_path}/{self.recipe}.json", "r") as f: result = json.load(f) # Make copy of file for archiving reasons with open(f"{script_output_path}/{self.recipe}_scrapped.json", "w") as f: json.dump(result, f, indent=2) recipes = result if not recipes: result = f"No recipes were able to be scraped..." Log(type.RESULT, result) return result # Step 2: refine results instruction = f""" 1. Filter: Eliminate irrelevant recipe data. 2. Sort: Prioritize recipes by relevance to search query. 3. Deduplicate: Remove exact duplicates. 4. Variance: Retain close, non-exact matches selectively. 5. Sanitize: Correct errors, standardize format 6. Ensure top result contains valid image url Context: Top search results from various sites; refine to match original recipe search. Procedure: - Exact matches prioritized. - Use stringent criteria for additional inclusions if exact matches exist. - Sort by relevance: exact matches first. - Eliminate identical recipes; retain similar for variety. - Balance between variety and redundancy: more variety early, less needed later. - Example: 15 recipes, 3 exact, 2 closest non-exact kept, ranked. Task: Rewrite for consistency, error correction; return sanitized JSON array. """.strip() completion = agency.client.chat.completions.create( model=gpt4, response_format={"type": "json_object"}, messages=[ {"role": "system", "content": instruction}, {"role": "user", "content": f"Recipe Name: {self.recipe}"}, # maybe ingest file via new assistant thread (side mission) instead? {"role": "user", "content": f"Recipes: {json.dumps(recipes, indent=2)}"} ] ) Debug(f"Sanitization result:\n{completion.choices[0].message.content}") recipes = json.loads(completion.choices[0].message.content) # Write to file with open(f"{script_output_path}/{self.recipe}.json", "w") as f: json.dump(recipes, f, indent=2) if not recipes: result = f"No valid recipes found..." Log(type.RESULT, result) return result Log(type.RESULT, f"Scrapped {len(recipes)} recipes") return f"{len(recipes)} recipes dumped to file '{script_output_directory}/{self.recipe}.json'"
[]
2024-01-10
Zarichney/AUTO
Tools~FileManagement~MoveFile.py
# /Tools/MoveFile.py import os from instructor import OpenAISchema from pydantic import Field from Utilities.Config import WORKING_DIRECTORY from Utilities.Log import Log, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class MoveFile(OpenAISchema): """ Move a file from one directory to another. This tool can also be used to rename a file when the destination is the same directory but the supplied new file name is different. """ file_name: str = Field( ..., description="Required: The name of the file (including the extension) to be moved" ) new_name: str = Field( default="", description="Optional: The new name of the file (including the extension) to be named at the new destination. If left blank, the file will be moved with the same name.", ) directory: str = Field( default=WORKING_DIRECTORY, description="Optional: The path to the directory where the file is currently stored. Path can be absolute or relative.", ) destination: str = Field( default="", description="Optional: The path to the directory where to file is be moved to. Path can be absolute or relative." ) def run(self, agency: 'Agency'): # If file doesnt exist, return message if not os.path.exists(self.directory + self.file_name): return f"File {self.directory + self.file_name} does not exist." if self.new_name == "": self.new_name = self.file_name if self.destination == "": self.destination = self.directory file_destination_path = os.path.join(self.destination, self.new_name) os.rename(self.directory + self.file_name, file_destination_path) # if destination is the same but file name is different, log rename if self.directory == self.destination and self.new_name != self.file_name: result = f"File renamed to: {file_destination_path}" else: result = f"File moved to: {file_destination_path}" Log(type.RESULT, result) return result
[]
2024-01-10
Zarichney/AUTO
Tools~Programming~DownloadFile.py
# /Tools/DownloadFile.py import os import requests from instructor import OpenAISchema from pydantic import Field from Utilities.Config import WORKING_DIRECTORY from Utilities.Log import Log, Debug, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class DownloadFile(OpenAISchema): """Used to download a file from the internet given a url""" url: str = Field( ..., description="The url to download the file from", ) working_dir: str = Field( default=WORKING_DIRECTORY, description="The path to the directory to be write files to." ) filename: str = Field( default=None, description="Specify a custom name to save the downloaded file as", ) # todo remove and replace with timestamp prefix config overwrite: bool = Field( default=False, description="If true, will overwrite the file if it already exists." ) def run(self, agency: 'Agency'): Log(type.ACTION, f"Downloading file from: {self.url}") # Set file name if agent did not supply one if self.filename is None: self.filename = self.url.split("/")[-1] # If file already exists, return message if os.path.exists(self.working_dir + self.filename): if self.overwrite: Log(type.ACTION, f"Overwriting file: {self.working_dir + self.filename}") else: result = f"File {self.working_dir + self.filename} already exists.\n" result += "Specify to overwrite if you this is intended, or\n" result += "increment the file version for a unique file name" Log(type.ERROR, result) return result try: with requests.get(self.url, stream=True) as r: r.raise_for_status() with open(self.working_dir + self.filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) except requests.exceptions.RequestException as e: result = f"Error downloading file: {e}" Log(type.ERROR, result) return result result = f"{self.url} has been downloaded to '{self.working_dir + self.filename}'" Log(type.ACTION, result) return result
[]
2024-01-10
Zarichney/AUTO
Agency~AgentConfig.py
# /Agency/AgentConfig.py import json import os import openai from Agents import agent_classes from Utilities.Config import agent_config_file_name from Utilities.Log import Debug from typing import TYPE_CHECKING if TYPE_CHECKING: from Agents.BaseAgent import BaseAgent class AgentConfig: def __init__(self, agent_id, agent_name): self.agent_id: str = agent_id self.agent_name: str = agent_name def delete(self, client: openai): try: client.beta.assistants.delete(self.agent_id) Debug(f"OpenAI Assistant '{self.agent_name}' Deleted") except Exception: Debug(f"Failed to delete OpenAI Assistant '{self.agent_name}'") pass def to_dict(self): return { "agent_id": self.agent_id, "agent_name": self.agent_name } class AgentConfigurationManager: def __init__(self, agency, rebuild_agents=False): self.agency = agency self.configurations: [AgentConfig] = [] self.agents: [BaseAgent] = [] # if config_file does not exist or has empty contents, initialize file if ( not os.path.exists(agent_config_file_name) or os.stat(agent_config_file_name).st_size == 0 ): self._write_to_config_file() self._load_from_session_file() if len(self.configurations) > 0 and rebuild_agents: self._reset_config_file() if len(self.configurations) == 0: self._setup_agents() self._load_agents() def _reset_config_file(self): for agentConfig in self.configurations: agentConfig.delete(self.agency.client) self.configurations = [] self._write_to_config_file() def _write_to_config_file(self): with open(agent_config_file_name, "w") as config_file: configurations_dict = [config.to_dict() for config in self.configurations] config_file.write(json.dumps({"agents": configurations_dict}) + "\n") def _load_from_session_file(self): self.configurations = [] with open(agent_config_file_name, "r") as config_file: config = json.load(config_file) for config_dict in config["agents"]: self.configurations.append( AgentConfig( agent_id=config_dict["agent_id"], agent_name=config_dict["agent_name"], ) ) def _load_agents(self): for agent_config in self.configurations: self.agents.append( self._initialize_agent( agent_config.agent_name, agent_config.agent_id ) ) # Agent creation expected. Assert or return error if agent_id cannot be found for agent_config in self.configurations: if agent_config.agent_id not in [agent.id for agent in self.agents]: raise Exception( f"Session Loading Error: Agent {agent_config.agent_name} with id {agent_config.agent_id} could not be added to agency." ) def _initialize_agent(self, agent_name:str, agent_id:str=None): if agent_name not in agent_classes: raise Exception(f"Invalid agent type: {agent_name}") AgentClass = agent_classes[agent_name] agent_instance = AgentClass(self.agency, agent_id) return agent_instance def _setup_agents(self): # Generate new instances of agents for agent_name, AgentClass in agent_classes.items(): agent_instance = self._initialize_agent(agent_name) self.agents.append(agent_instance) self.configurations.append( AgentConfig(agent_instance.id, agent_name) ) self._write_to_config_file() Debug("Generated agents written to file")
[]
2024-01-10
Zarichney/AUTO
Tools~FileManagement~GetDirectoryContents.py
# /Tools/GetDirectoryContents.py import os import time from instructor import OpenAISchema from pydantic import Field from Utilities.Log import Log, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class GetDirectoryContents(OpenAISchema): """ List all files within a given directory """ directory: str = Field( ..., description="The path to the directory to be read" ) def run(self, agency: 'Agency'): if not os.path.exists(self.directory): result = f"Directory does not exist: {self.directory}" Log(type.ERROR, result) return result # Get a list of filenames in the directory filenames = os.listdir(self.directory) # Get the last modification date and size for each file file_info = [(filename, time.ctime(os.path.getmtime(os.path.join(self.directory, filename))), os.path.getsize(os.path.join(self.directory, filename))) for filename in filenames] # Format the output as a Markdown table listing = "| Filename | Size (bytes) Last Modified | |\n" listing += "| --- | --- | --- |\n" for filename, size, last_modified in file_info: listing += f"| {filename} | {size} | {last_modified} |\n" # Get the count of files file_count = len(filenames) listing += f"\nTotal number of files: {file_count}" Log(type.ACTION, f"Listing files in directory: {self.directory}.\nFile Count: {file_count}") return listing
[]
2024-01-10
Zarichney/AUTO
Tools~FileManagement~ReadFile.py
# /Tools/ReadFile.py import os from instructor import OpenAISchema from pydantic import Field from Utilities.Config import WORKING_DIRECTORY from Utilities.Log import Log, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class ReadFile(OpenAISchema): """ Read the contents of a local file. """ file_name: str = Field( ..., description="The name of the file including the extension" ) directory: str = Field( default=WORKING_DIRECTORY, description="The path to the directory where to file is stored. Path can be absolute or relative." ) def run(self, agency: 'Agency'): # If file doesnt exist, return message if not os.path.exists(self.directory + self.file_name): result = f"File {self.directory + self.file_name} does not exist." Log(type.ERROR, result) return result # todos: # 1. upload file to openai # 2. store file id somewhere in agency # 3. attach file id to a new message & drop in queue # 4. create new run with file id # 5. return tool output? # 6. wait for in progress and cancel run? # 7. start the run with the file id # 8. test and review Log(type.ACTION, f"Viewing content of file: {self.directory + self.file_name}") with open(self.directory + self.file_name, "r") as f: file_content = f.read() return f"File contents:\n{file_content}"
[]
2024-01-10
Zarichney/AUTO
Agency~Session.py
# /Agency/Session.py import json import os import openai from Utilities.Config import session_file_name from Utilities.Log import Debug class Session: def __init__(self, client, prompt, thread_id=None): self.prompt: str = prompt self.client:openai = client self.thread = None if thread_id is None: self._setup_thread() else: self._retrieve_thread(thread_id) def _cancel_runs(self): try: # Cancel any runs from a previous session runs = self.client.beta.threads.runs.list(self.thread.id).data for run in runs: if ( run.status != "completed" and run.status != "cancelled" and run.status != "failed" ): self.client.beta.threads.runs.cancel( thread_id=self.thread.id, run_id=run.id ) except Exception: Debug(f"Failed to cancel runs for OpenAI thread_id {self.thread.id}") pass def delete(self): if self.thread is None: return self._cancel_runs() try: self.client.beta.threads.delete(self.thread.id) Debug("Deleted OpenAI thread") except Exception: Debug(f"Failed to delete OpenAI thread_id {self.thread.id}") pass def _setup_thread(self): if self.thread is not None: self._delete_thread() self.thread = self.client.beta.threads.create() def _retrieve_thread(self,thread_id): self.thread = self.client.beta.threads.retrieve(thread_id) self._cancel_runs() def to_dict(self): return { "prompt": self.prompt, "thread_id": self.thread.id } class SessionManager: def __init__(self, client, prompt=None, new_session=False): self.client = client self.sessions: [Session] = [] self.active_session = None # if session_file does not exist or has empty contents, initialize file if ( not os.path.exists(session_file_name) or os.stat(session_file_name).st_size == 0 ): self._write_to_session_file() self._load_from_session_file() # Agency established with the user's prompt # Perform automatic loading of active_session: if prompt is not None: current_session = self._get_session(prompt) if new_session and current_session is not None: self._remove_session(current_session) if new_session or current_session is None: self.active_session = self._create_session(prompt) else: self.active_session = current_session def _remove_session(self, session:Session): session.delete() # filter out current_session from sessions self.sessions = [ session for session in self.sessions if session != session ] self._write_to_session_file() def _load_from_session_file(self): self.sessions = [] with open(session_file_name, "r") as session_file: config = json.load(session_file) config_sessions = config["sessions"] for config_dict in config_sessions: self.sessions.append( Session( client=self.client, prompt=config_dict["prompt"], thread_id=config_dict["thread_id"] ) ) def _write_to_session_file(self): with open(session_file_name, "w") as session_file: configurations_dict = [config.to_dict() for config in self.sessions] session_file.write(json.dumps({"sessions": configurations_dict}) + "\n") def _create_session(self, prompt): session = Session(self.client, prompt) self.sessions.append(session) self._write_to_session_file() return session def _get_session(self, prompt): for session in self.sessions: if session.prompt == prompt: return session return None def get_session(self, prompt): self.active_session = self._get_session(prompt) if self.active_session is None: self.active_session = self._create_session(prompt) return self.active_session.thread
[]
2024-01-10
Zarichney/AUTO
Agents~BaseAgent.py
# /Agents/Agent.py import textwrap import time from openai.types.beta.assistant import Assistant from Utilities.Log import Debug, Log, type from Utilities.Config import current_model from typing import TYPE_CHECKING from Agency.Arsenal import SHARED_TOOLS if TYPE_CHECKING: from Agency.Agency import Agency class BaseAgent: def __init__(self, agency:'Agency', assistant_id=None): if not agency: raise Exception("Agency not supplied") self.agency = agency self.tool_definitions = [] if not hasattr(self, 'custom_tools'): self.custom_tools = [] for tool in self.custom_tools: self.tool_definitions.append({"type": "function", "function": tool.openai_schema}) for tool in SHARED_TOOLS: self.tool_definitions.append({"type": "function", "function": tool.openai_schema}) if not hasattr(self, 'custom_instructions'): Log(type.Error, f"Agent {self.name} does not have a custom_instructions attribute") self.custom_instructions = "" # Create agent if it doesn't exist if assistant_id is None: # Standard template for all agents self.instructions = textwrap.dedent(f""" # Name {self.name} ## Description {self.description} {self.custom_instructions} ## Services You Offer: {self.services} {self.agency.get_team_instruction()} """).strip() assistant = self.agency.client.beta.assistants.create( name = self.name, description = self.description, instructions = self.instructions, model = current_model, metadata = {"services": self.services}, tools = self.tool_definitions ) else: assistant = self.agency.client.beta.assistants.retrieve(assistant_id=assistant_id) self.assistant:Assistant = assistant self.id = self.assistant.id self.instructions = self.assistant.instructions self.waiting_on_response = False self.task_delegated = False self.toolkit = [] self._setup_tools() def add_tool(self, tool): self.toolkit.append(tool) def _setup_tools(self): # Add custom tools for tool in self.custom_tools: self.add_tool(tool) # Add communal tools for tool in SHARED_TOOLS: self.add_tool(tool) def get_completion(self, message=None, useTools=True): client = self.agency.client thread = self.agency.thread if self.agency.running_tool: Debug(f"Agent called for completion while it's currently waiting on tool usage to complete. Falling back to thread-less completion") return client.chat.completions.create( model=current_model, messages=[ {"role": "system", "content": self.instructions}, {"role": "user", "content": message} ] ).choices[0].message.content else: # Messages can't be added while a tool is running so they get queued up # Unload message queue for queued_message in self.agency.message_queue: self.agency.add_message(message=queued_message) self.agency.message_queue = [] self.waiting_on_response = False if message is not None: message = self.agency.add_message(message=message) # run creation if useTools: run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=self.id, ) else: run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=self.id, tools=[] # forces assistant to respond with prompt ) while True: # wait until run completes while run.status in ["queued", "in_progress"]: run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id) time.sleep(1) # Assistant requested to have a tool executed if run.status == "requires_action": tool_calls = run.required_action.submit_tool_outputs.tool_calls tool_outputs = [] if len(tool_calls) > 1: Debug(f"{self.name} is invoking {len(tool_calls)} tool calls") for tool_call in tool_calls: tool_name = tool_call.function.name Debug(f"{self.name} is invoking tool: {tool_name}") # Find the tool to be executed tool_function = next((func for func in self.toolkit if func.__name__ == tool_name), None) if tool_function is None: tool_names = [func.__name__ for func in self.toolkit] Log(type.ERROR, f"No tool found with name {tool_name}. Available tools: {', '.join(tool_names)}") output = f"{tool_name} is not a valid tool name. Available tools: {', '.join(tool_names)}" else: self.agency.running_tool = True try: arguments = tool_call.function.arguments.replace('true', 'True').replace('false', 'False') output = tool_function(**eval(arguments)).run(agency=self.agency) Debug(f"Tool '{tool_name}' Completed.") except Exception as e: Log(type.ERROR, f"Error occurred in function '{tool_name}': {str(e)}") output = f"Tool '{tool_name}' failed. Error: {str(e)}" self.agency.running_tool = False tool_outputs.append({"tool_call_id": tool_call.id, "output": output}) if len(tool_calls) == 1: Debug(f"Submitting tool output") else: Debug(f"Submitting {len(tool_calls)} tool outputs") run = client.beta.threads.runs.submit_tool_outputs( thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs ) # error elif run.status == "failed": Log(type.ERROR, f"Run Failed. Error: {run.last_error}") Debug(f"Run {run.id} has been cancelled") return "An internal server error occurred. Try again" # return assistant response else: completion = client.beta.threads.messages.list(thread_id=thread.id) # todos: # 1. send to agency # 2. store to disk # 3. use for new caching mechanism # 3.1 that can work across different threads (dictionary of hashes?) # 3.2 only store the prompt after response = completion.data[0].content[0].text.value if self.task_delegated: self.waiting_on_response = False else: self.waiting_on_response = True return response
[]
2024-01-10
Zarichney/AUTO
Tools~Organizational~Delegate.py
# /Tools/Delegate.py from instructor import OpenAISchema from pydantic import Field from Utilities.Log import Log, Debug, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agents.BaseAgent import BaseAgent from Agency.Agency import Agency class Delegate(OpenAISchema): """ Used to appoint another agent as the agency's active agent. To hand off the responsibility of tackling the current action item to different specialized agent. """ recipient_name: str = Field( ..., description="The agent's name which is being requested for assistance", ) instruction: str = Field( ..., description="Specify the task required for the recipient agent to complete. Recall the agency's plan and speak to the assistant in terms of the action items you want them to complete.", ) def run(self, agency: 'Agency'): recipient: 'BaseAgent' = agency.get_agent(self.recipient_name) current_agent: 'BaseAgent' = agency.active_agent if recipient.name == current_agent.name: Log(type.ERROR, f"{recipient.name} attempted to delegate to itself") return "You cannot delegate to yourself. Supply a different agent name instead." prompt = f"# User's Prompt\n" prompt += f"{agency.prompt}\n\n" # Every fifth delegation, the agency will remind the agent of the plan if agency.delegation_count % 5 == 0: prompt += f"# Agency's Plan\n" prompt += f"{agency.plan}\n\n" prompt += f"I, {current_agent.name}, am seeking assistance from you, Agent {recipient.name}.\n" prompt += "According to our agency's mission, could you perform the following please:\n" prompt += self.instruction Log(type.COMMUNICATION, f"{current_agent.name} is prompting {recipient.name}:\n{self.instruction}") Debug(f"{current_agent.name} is delegating to {recipient.name} with this prompt:\n{prompt}") agency.add_message(message=prompt) agency.active_agent = recipient agency.delegation_count += 1 current_agent.task_delegated = True return "Delegation complete. The recipient will complete the task. Do not use any tools. Just respond that you've delegated"
[ "# Agency's Plan\n", "According to our agency's mission, could you perform the following please:\n", "# User's Prompt\n" ]
2024-01-10
Zarichney/AUTO
Agency~Agency.py
# /Agents/Agency.py import json import openai from .Team import Team from .Session import SessionManager, Session from .AgentConfig import AgentConfigurationManager from Agents import SprAgent, UserAgent from Utilities.Config import GetClient, current_model from Utilities.Log import Log, Debug, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agents.BaseAgent import BaseAgent class Agency: def __init__( self, prompt=None, new_session: bool = False, rebuild_agents: bool = False ): self.client: openai = GetClient() self.thread = None self.agents: ["BaseAgent"] = [] self.team_instructions = None self.plan = None self.prompt = prompt self.active_agent: 'BaseAgent' = None self.running_tool = False self.message_queue = [] self.delegation_count = 0 self.session_manager:SessionManager = SessionManager(client=self.client,prompt=prompt, new_session=new_session) self.session: Session = self.session_manager.active_session self.agents_manager: AgentConfigurationManager = AgentConfigurationManager(agency=self, rebuild_agents=rebuild_agents) self.agents = self.agents_manager.agents def get_agent(self, name) -> "BaseAgent": for agent in self.agents: if agent.name == name: return agent # An invalid name was supplied, use GPT to find the correct agent name Log(type.ERROR, f"Agent named '{name}' not found in agency. Engaging fall back...") list_of_agent_names = [agent.name for agent in self.agents] Log(type.ERROR, f"Actual agent names: {', '.join(list_of_agent_names)}") completion = self.client.chat.completions.create( model=current_model, response_format={"type": "json_object"}, messages=[ { "role": "system", "content": """ Task: Match closest valid agent name. Input: Valid names, one invalid. Output: JSON with closest match. Method: String similarity analysis. JSON Format: {"Name": "Closest valid agent"}. """.strip(), }, { "role": "user", "content": f"Valid names: {', '.join(list_of_agent_names)}.\nInvalid name:{name}", }, ], ) actualAgentName = json.loads(completion.choices[0].message.content)["Name"] Log(type.ERROR, f"Agent name fallback determined: {actualAgentName}") for agent in self.agents: if agent.name == actualAgentName: return agent Log(type.ERROR, f"Requested Agent could still not be found in agency... Returning user agent") return self.get_agent(UserAgent.NAME) def UpdatePlan(self, plan): # todo self.plan = plan def _queue_message(self, message): self.message_queue.append(message) def add_message(self, message): if self.running_tool: self._queue_message(message) return self.waiting_on_response = False # todo: support seed # appears to currently not be supported: https://github.com/openai/openai-python/blob/790df765d41f27b9a6b88ce7b8af713939f8dc22/src/openai/resources/beta/threads/messages/messages.py#L39 # reported issue: https://community.openai.com/t/seed-param-and-reproducible-output-do-not-work/487245 return self.client.beta.threads.messages.create( thread_id=self.thread.id, role="user", content=message, ) def get_team_instruction(self): if self.team_instructions is None: verbose_team_instructions = Team.build_agents_list_and_arsenal() Debug(f"Verbose Team instructions:\n{verbose_team_instructions}") # Use SPR Writer to compress the instructions completion = self.client.chat.completions.create( model=current_model, messages=[ {"role": "system", "content": SprAgent.INSTRUCTIONS}, {"role": "user", "content": verbose_team_instructions}, ], ) compressed_team_instructions = completion.choices[0].message.content Debug(f"Concise Team instructions:\n{compressed_team_instructions}") self.team_instructions = compressed_team_instructions return self.team_instructions # main method to get the agency to work the prompt def complete( self, mission_prompt, single_agent, stop_word="exit", continue_phrase=None ): if self.prompt is None: self.prompt = mission_prompt if self.thread is None: self.thread = self.session_manager.get_session(self.prompt) if continue_phrase is None: continue_phrase = "" prompt = mission_prompt if self.active_agent is None: self.active_agent = self.get_agent(UserAgent.NAME) while True: if single_agent: agent_name = self.active_agent.name prompt += "\nWork on this alone, do not delegate.\n" response = self.active_agent.get_completion(message=prompt) Debug(f"{agent_name} responded with:\n{response}") Debug(f"Active agent: {self.active_agent.name}") else: response = self._operate(prompt) Log(type.RESULT, f"{self.active_agent.name}:\n{response}") message = f"Waiting for reply from user. Or type '{stop_word}'" if continue_phrase is not None: message += f" to {continue_phrase}" message += ":\n\n" Log(type.PROMPT, message) prompt = input("> ") if prompt.lower() == stop_word.lower(): if continue_phrase is not None: Log(type.ACTION, f"\t{continue_phrase}") break return response # Used to have user agent delegate and auto respond to agency def _operate(self, prompt): # Trigger the initial delegation Debug(f"Starting operation. User provided prompt: {prompt}") response = self.active_agent.get_completion(prompt) Debug(f"Initial response: {response}") user_agent = self.get_agent(UserAgent.NAME) user_agent.task_delegated = False while self.active_agent.waiting_on_response == False: Debug(f"Active agent: {self.active_agent.name}") # Store the name so that we can recognize who the previous agent was after a delegation active_agent_name = self.active_agent.name response = self.active_agent.get_completion() Log(type.COMMUNICATION, f"{self.active_agent.name}:\n{response}") previous_agent = self.get_agent(active_agent_name) if previous_agent.task_delegated == True: # Turn this flag off now that delegation is completed previous_agent.task_delegated == False # Get user agent to handle the response in order to automate the next step if an agent response instead of tool usage elif ( previous_agent.task_delegated == False and active_agent_name != UserAgent.NAME ): prompt = f"{response}\n\n In regards to the overall plan. What do we do now leader?" Debug( f"{active_agent_name} has responded and is addressing user agent:\n{prompt}" ) self.active_agent.waiting_on_response = False self.active_agent = user_agent # Attempt to delegate response = user_agent.get_completion(message=prompt) Debug( f"User agent is expected to have delegated. This was its response:{response}" ) Debug(f"The new active agent is: {self.active_agent.name}") # If the user agent is still active, this will get the response sent back to the user if self.active_agent.name == UserAgent.NAME: self.active_agent.waiting_on_response = True # When successfully delegated, loop will restart, causing the next agent to pick up the delegate instruction message Debug( f"{self.active_agent.name} is returning back to the user with: {response}" ) return response
[ "Task: Match closest valid agent name.\n Input: Valid names, one invalid.\n Output: JSON with closest match.\n Method: String similarity analysis.\n JSON Format: {\"Name\": \"Closest valid agent\"}.", "> ", "\nWork on this alone, do not delegate.\n", "PLACEHOLDER\n\n In regards to the overall plan. What do we do now leader?", ", " ]
2024-01-10
Zarichney/AUTO
Tools~Organizational~Plan.py
# /Tools/Plan.py from instructor import OpenAISchema from pydantic import Field from Utilities.Log import Log, Debug, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agents.BaseAgent import BaseAgent from Agency.Agency import Agency from Agency.Arsenal import SHARED_TOOLS class Plan(OpenAISchema): """ Generates a workflow of actionable steps. """ goal: str = Field( ..., description="The goal that the agent would like to achieve. Will be used as the basis for planning" ) context: str = Field( ..., description="Additional considerations to be taken into account when planning" ) def run(self, agency: 'Agency'): current_agent = agency.active_agent master_plan_creation = agency.plan is None prompt = "You are being engaged to create a plan. Review the following:\n\n" prompt += "User's Prompt: " + agency.prompt + "\n\n" prompt += f"Your goal is to: {self.goal}\n\n" prompt += f"Plan considerations:\n{self.context}\n\n" if master_plan_creation: # Add team details prompt += "# Team Composition: \n" for agent in agency.agents: agent:BaseAgent = agent prompt += f"## Name: \"{agent.name}\"\n" prompt += f"### Description: {agent.description}\n" # prompt += f"### Services: {agent.services}\n" # TODO: fix: empty array [] prompt += "\n" prompt += "\n" else: prompt += f"# Agency's Plan\n\n{agency.plan}\n\n" # Add available tools to prompt: if master_plan_creation: toolkit = SHARED_TOOLS else: toolkit = current_agent.tools # todo i think the issue here is that its being fed the internal function prompt += "# Available Tools: \n" try: for tool in toolkit: schema = tool.openai_schema prompt += "## " + schema['name'] + "\n" prompt += schema['description'] + "\n\n" prompt += "\n" except Exception as e: Log(type.ERROR, f"Error in Plan.py: {e}") Log(type.ERROR, f"Tools: {' | '.join([tool for tool in toolkit])}") Log(type.ERROR, f"master_plan_creation: {master_plan_creation}") # Instruction to review inputs and make a plan prompt += "# Plan Structure\n\n" prompt += "The plan is a workflow of **actionable steps** that will be executed to accomplish the mission.\n" prompt += "An actionable step is specific instruction conducted by a single agent via a tool usage\n" prompt += "The plan format adhere's to the following structure:\n" prompt += "<step_number> + \".\" + <agent_name> + \" using \" + <tool_name> + \": \" + <description of instruction or expected deliverable>\"\n" prompt += "\nExample of a simplified multi step workflow (for the user's prompt \"Create me a script\"):\n" prompt += "\t\"1. Coder using CreateFile: Create the script\"\n" prompt += "\t\"2. Coder using Delegate: Instruct QA to test the generated script, providing them instructions on how to execute\"\n" prompt += "\t\"3. QA using ExecutePyScript: Review execution results and provide appropriate feedback\"\n" prompt += "\t\"4. User Agent: Submit script back to user with execution instructions" prompt += "\tExample of a simple one liner plan (for the user's prompt \"I have a query\"):\n" prompt += "\t\"1. User Agent: I will respond to the user's prompt\"\n\n" # Plan tweaking prompt += "## Additional considerations:\n" prompt += "- Ensure the plan is manageable:\n" prompt += " - Recognize and acknowledge if the mission is too complex.\n" prompt += " - Size complexity will depend on the context so use your judgement.\n" prompt += " - It is acceptable that the user's prompt is as simple as a one step plan\n" prompt += " - Refuse plan generation when:\n" prompt += " - The mission is too general and cannot be executed via actionable steps.\n" prompt += " - The execution to achieve the desired result is deemed infeasible.\n" prompt += " - The request falls outside the agent's capabilities.\n" prompt += " - During refusals, provide detailed explanations:\n" prompt += " - Why the mission cannot be carried out or the plan cannot be generated.\n" prompt += " - Clarify what changes are needed for a successful attempt.\n" if master_plan_creation: prompt += "- Delegation is key:\n" prompt += " - Each agent is equipped with 'Delegate' to perform the handoff of the tasks.\n" prompt += " - The invocation of the tool 'Delegate' is to be it's own step in the plan, ensuring proper delegation.\n" prompt += "\n\n**THE GOAL IN PLAN CREATION IS TO SIMPLY CONSIDER THE MISSION AGAINST " if master_plan_creation: prompt += "THE ENVIRONMENT (AGENTS AND TOOLS AVAILABLE)" else: prompt += "YOUR CAPABILITIES" prompt += " WITH THE LEAST AMOUNT OF ACTIONABLE STEPS NECESSARY**\n\n" prompt += "Think step by step. Good luck, you are great at this!\n" Debug(f"Plan Prompt for {current_agent.name}:\n{prompt}") # todo: use SPR writer to compress this prompt statically (aka update this file to be more concise) if master_plan_creation: Log(type.ACTION, f"Agency is generating a plan\n") # todo: need to test whether its better to have the plan generated here, # or have this prompted returned as tool output for agent to decide what to do next plan = current_agent.get_completion(message=prompt, useTools=False) Log(type.RESULT, f"\nPlan Generated:\n{plan}\n") return plan
[ "## PLACEHOLDER\n", "You are being engaged to create a plan. Review the following:\n\n", "\n", " - Recognize and acknowledge if the mission is too complex.\n", " - Clarify what changes are needed for a successful attempt.\n", " - Why the mission cannot be carried out or the plan cannot be generated.\n", "User's Prompt: ", "\t\"1. User Agent: I will respond to the user's prompt\"\n\n", " - Size complexity will depend on the context so use your judgement.\n", "YOUR CAPABILITIES", "The plan is a workflow of **actionable steps** that will be executed to accomplish the mission.\n", "\nExample of a simplified multi step workflow (for the user's prompt \"Create me a script\"):\n", "- Ensure the plan is manageable:\n", "- Delegation is key:\n", "PLACEHOLDER\n\n", " - It is acceptable that the user's prompt is as simple as a one step plan\n", "\tExample of a simple one liner plan (for the user's prompt \"I have a query\"):\n", " - The request falls outside the agent's capabilities.\n", " - During refusals, provide detailed explanations:\n", "# Available Tools: \n", "# Team Composition: \n", "An actionable step is specific instruction conducted by a single agent via a tool usage\n", " WITH THE LEAST AMOUNT OF ACTIONABLE STEPS NECESSARY**\n\n", "# Plan Structure\n\n", "\n\n**THE GOAL IN PLAN CREATION IS TO SIMPLY CONSIDER THE MISSION AGAINST ", "\t\"1. Coder using CreateFile: Create the script\"\n", "\n\n", " - The mission is too general and cannot be executed via actionable steps.\n", " - Refuse plan generation when:\n", " - The execution to achieve the desired result is deemed infeasible.\n", "The plan format adhere's to the following structure:\n", " - The invocation of the tool 'Delegate' is to be it's own step in the plan, ensuring proper delegation.\n", "Think step by step. Good luck, you are great at this!\n", " - Each agent is equipped with 'Delegate' to perform the handoff of the tasks.\n", "\t\"4. User Agent: Submit script back to user with execution instructions", "THE ENVIRONMENT (AGENTS AND TOOLS AVAILABLE)", "<step_number> + \".\" + <agent_name> + \" using \" + <tool_name> + \": \" + <description of instruction or expected deliverable>\"\n", "## Additional considerations:\n", "\t\"3. QA using ExecutePyScript: Review execution results and provide appropriate feedback\"\n", "\t\"2. Coder using Delegate: Instruct QA to test the generated script, providing them instructions on how to execute\"\n" ]
2024-01-10
Zarichney/AUTO
Tools~Organizational~Inquire.py
# /Tools/Inquire.py from instructor import OpenAISchema from pydantic import Field from Utilities.Log import Log, Debug, type from typing import TYPE_CHECKING if TYPE_CHECKING: from Agents.BaseAgent import BaseAgent from Agency.Agency import Agency class Inquire(OpenAISchema): """ Used to get information from another agent """ recipient_name: str = Field( ..., description="The agent's name which is being queried", ) prompt: str = Field( ..., description="The inquiry to send to the recipient agent", ) chain_of_thought: str = Field( description="Your own chain of thought. Useful for the recipient to understand your thought process." ) # useTools: bool = Field( # default = False, # description="Whether or not you allow the recipient to have access to their tools in order to complete the inquiry. Typically left as default false because this inquiry tool is meant for their own knowledge, however there can be the case where the recipient would require tool access to properly respond to the inquiry." # ) def run(self, agency: 'Agency'): recipient: 'BaseAgent' = agency.get_agent(self.recipient_name) current_agent: 'BaseAgent' = agency.active_agent prompt = f"{recipient.name}, it is I, {current_agent.name}.\n" prompt += "I have an inquiry for you:\n\n" prompt += f"{self.prompt}\n\n" prompt += f"My chain of thought is:\n" prompt += f"{self.chain_of_thought}\n\n" prompt += "Could you share what you think step by step?\n" Log(type.COMMUNICATION, f"{current_agent.name} is inquiring to {recipient.name}:\n{self.prompt}") Debug(f"{current_agent.name} used inquiry tool on {recipient.name}. Full prompt:\n{prompt}") # todo: this needs rework or some design considerations # the problem is that this message does not include the history # and the response also dont get added to the thread # so this tool doesnt really help in providing clarification information to the agencys mission # only helps the current agent with its tool usage # todo: test off this is more effective if the message is dropped and the active agent is switched response = recipient.get_completion(message=prompt, useTools=False) Log(type.COMMUNICATION, f"{recipient.name} response to {current_agent.name}:\n{response}\n_______________________") return f"{recipient.name}:\n{response}"
[ "My chain of thought is:\n", "Could you share what you think step by step?\n", "The inquiry to send to the recipient agent", "I have an inquiry for you:\n\n" ]
2024-01-10
Zarichney/AUTO
Tools~Programming~ExecutePyFile.py
# /Tools/ExecutePyFile.py import os import subprocess import sys from instructor import OpenAISchema from pydantic import Field from Utilities.Config import WORKING_DIRECTORY from Utilities.Log import Debug, Log, type import pkg_resources from typing import TYPE_CHECKING if TYPE_CHECKING: from Agency.Agency import Agency class ExecutePyFile(OpenAISchema): """ Run local python (.py) file. Execution in this environment is safe and has access to all standard Python packages and the internet. Only use this tool if you understand how to troubleshoot with python, if not seek delegation to a more specialized agent. Additional packages can be installed by specifying them in the required_packages field. """ file_name: str = Field( ..., description="The path to the .py file to be executed." ) directory: str = Field( default=WORKING_DIRECTORY, description="The path to the directory where to file is stored. Path can be absolute or relative." ) parameters: str = Field( default="", description="Comma separated list of parameters to be passed to the script call." ) required_packages: str = Field( default="", description="Required packages to be installed. List of comma delimited strings. Will execute ''pip install <package>'' for each package supplied" ) def check_dependencies(self, python_path, required_packages): """Check if the required modules are installed.""" packages = required_packages.split(',') for package in packages: try: dist = pkg_resources.get_distribution(package) Log(type.ACTION,"{} ({}) is installed".format(dist.key, dist.version)) except pkg_resources.DistributionNotFound: Log(type.ACTION,f"The {package} module is not installed. Attempting to install...") try: subprocess.check_call([python_path, "-m", "pip", "install", package]) Log(type.ACTION,f"Successfully installed {package}.") except subprocess.CalledProcessError as e: message = f"Failed to install {package}. Error: {e.output}" Log(type.ERROR, message) return message return "All required modules are installed." def run(self, agency: 'Agency'): """Executes a Python script at the given file path and captures its output and errors.""" # Get the path of the current Python interpreter python_path = sys.executable # Check if the required modules are installed if self.required_packages: Debug(f"Agent called self.required_packages: {self.required_packages}") check_result = self.check_dependencies(python_path, self.required_packages) if check_result != "All required modules are installed.": return check_result # If file doesnt exist, return message if not os.path.exists(self.directory + self.file_name): Log(type.ERROR, f"Cannot execute file, incorrect path invoked: {self.directory + self.file_name}") return f"No file found at '{self.directory + self.file_name}'. Perhaps specify the correct path?" Log(type.ACTION, f"Executing {self.file_name}...") Debug(f"Agent called subprocess.run with:\n{[python_path, self.directory + self.file_name] + self.parameters.split(',')}") try: execution = subprocess.run( [python_path, self.directory + self.file_name] + self.parameters.split(','), text=True, capture_output=True, check=True, timeout=10 ) Debug(f"Agent execution cwd: {execution.cwd}") Debug(f"Agent execution args: {execution.args}") Debug(f"Agent execution results: {execution.stdout}") Debug(f"Agent execution errors: {execution.stderr}") Debug(f"Agent execution return code: {execution.returncode}") result = f"Execution results: {execution.stdout}" Log(type.RESULT, result) return result except subprocess.TimeoutExpired: result = "Execution timed out. The script may have been waiting with a prompt." Log(type.ERROR, result) return result except subprocess.CalledProcessError as e: result = f"Execution error occurred: {e.stderr}.\nPlease attempt to rectify" Log(type.ERROR, result) return result
[]
2024-01-10
Zarichney/AUTO
Utilities~Config.py
# Utilities/Config.py from openai import OpenAI gpt3 = "gpt-3.5-turbo-1106" gpt4 = "gpt-4-1106-preview" current_model = gpt4 USE_VERBOSE_INTRUCTIONS = False WORKING_DIRECTORY = "./ai-working-dir/" session_file_name = "./sessions.json" agent_config_file_name = "./agents.json" def GetClient(): openai_key = GetKey() client = OpenAI( api_key=openai_key, ) return client def GetKey(): with open("openai.key", "r") as file: return file.read().strip()
[]