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
nafets33/ozz
__fastapi~ozz_router.py
from fastapi import status, Body import ipdb import openai from dotenv import load_dotenv import os import json from ozz_query import Scenarios from fastapi import FastAPI from fastapi.responses import JSONResponse from master_ozz.utils import ozz_master_root # setting up FastAPI router = FastAPI() # Loading the environment variables load_dotenv('.env') @router.post("/voiceGPT", status_code=status.HTTP_200_OK) def load_ozz_voice(api_key=Body(...), text=Body(...), self_image=Body(...)): # Test Queries with user and assistant and saving in conversation history as well as json file # text = [{"role": "system", "content": "You are a cute and smart assistant for kids."}, # {'role':'user','content': 'hey hootie tell me a story'}] # text = [ # future state # {"role": "system", "content": "You are a cute and smart assistant for kids."}, # {'role':'user', 'content': 'hey hootie tell me a story'}, {'role':'assistant','content': 'what story would you like to hear'}, # {'role':'user','content': 'any kind of kid related'} # ] ipdb.set_trace() def handle_response(text : str): # Kids or User question text_obj = text[-1]['user'] #Conversation History to chat back and forth conversation_history : list = [] # Call the Scenario Function and get the response accordingly response = Scenarios(text_obj,conversation_history,first_ask=True,conv_history=False) # For saving a chat history for current session in json file with open('fastapi/conversation_history.json','w') as conversation_history_file: json.dump(conversation_history,conversation_history_file) # update reponse to self !!! well we are not using class methods so self doesn't work we just simply need to return response # as functional based prototyping but if you have rest of the code then it will work according to the code text[-1].update({'resp': response}) # text[-1] = response # for normal response return without class return text text = handle_response(text) def handle_image(text, self_image): # based on LLM response handle image if needs to change self_image = '/Users/stefanstapinski/ENV/pollen/pollen/custom_voiceGPT/frontend/build/hootsAndHootie.png' return self_image self_image = handle_image(text, self_image) # audio_file = 'pollen/db/audio_files/file1.mp4' audio_file = '/Users/stefanstapinski/ENV/pollen/pollen/custom_voiceGPT/frontend/build/test_audio.mp3' json_data = {'text': text, 'audio_path': audio_file, 'page_direct': None, 'self_image': self_image} return JSONResponse(content=json_data)
[]
2024-01-10
nafets33/ozz
llama_ozz~llama_ozz.py
import os import sys import re import pandas as pd import numpy as np from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer import openai from dotenv import load_dotenv current_dir = os.path.dirname(os.path.abspath(__file__)) chess_piece_dir = os.path.join(current_dir, "..", "chess_piece") sys.path.append(chess_piece_dir) # from king import hive_master_root, print_line_of_error def hive_master_root(info='\pollen\pollen'): script_path = os.path.abspath(__file__) return os.path.dirname(os.path.dirname(script_path)) # \pollen\pollen def print_line_of_error(e='print_error_message'): exc_type, exc_obj, exc_tb = sys.exc_info() print(e, exc_type, exc_tb.tb_lineno) return exc_type, exc_tb.tb_lineno load_dotenv(os.path.join(hive_master_root(), '.env')) class EmbeddingUtility: def __init__(self, embedding_model="text-embedding-ada-002"): self.embedding_model = embedding_model openai.api_key = os.environ.get('ozz_api_key') def get_embedding(self, text): text = text.replace("/n", " ") return openai.Embedding.create(input=[text], model=self.embedding_model)['data'][0]['embedding'] def get_doc_embedding(self, text): return self.get_embedding(text) class DocumentProcessor: MAX_SECTION_LEN = 1024 SEPARATOR = "/n* " def __init__(self, api_params, utility: EmbeddingUtility): self.COMPLETIONS_API_PARAMS = api_params self.utility = utility def compute_matching_df_embeddings(self, matching_df): return { idx: self.utility.get_doc_embedding(r.description.replace("/n", " ")) for idx, r in matching_df.iterrows() } @staticmethod def vector_similarity(x, y): return np.dot(np.array(x), np.array(y)) def order_document_sections_by_query_similarity(self, query, contexts): query_embedding = self.utility.get_embedding(query) document_similarities = sorted([ (self.vector_similarity(query_embedding, doc_embedding), doc_index) for doc_index, doc_embedding in contexts.items() ], reverse=True) return document_similarities def construct_prompt(self, question, context_embeddings, df): most_relevant_document_sections = self.order_document_sections_by_query_similarity(question, context_embeddings) chosen_sections = [] chosen_sections_len = 0 for _, section_index in most_relevant_document_sections: document_section = df.loc[section_index] chosen_sections_len += len(document_section.description) + 3 if chosen_sections_len > self.MAX_SECTION_LEN: break chosen_sections.append(self.SEPARATOR + document_section.description.replace("/n", " ")) header = """Answer the question as truthfully as possible only using the provided context. and if the answer is not contained within the text below, say "I don't know" /n/nContext:/n""" return header + "".join(chosen_sections) + "/n/n Q: " + question + "/n A:" def answer_query_with_context(self, query, df, context_embeddings, show_prompt=False): prompt = self.construct_prompt(query, context_embeddings, df) if show_prompt: print(prompt) response = openai.Completion.create(prompt=prompt, **self.COMPLETIONS_API_PARAMS) return response["choices"][0]["text"].strip(" /n") def preprocessing(df): for i in df.index: desc = df.loc[i, 'description'] if isinstance(desc, str): df.loc[i, 'description'] = desc.replace('/n', '') df.loc[i, 'description'] = re.sub(r'/(.*?/)', '', df.loc[i, 'description']) df.loc[i, 'description'] = re.sub('[/(/[].*?[/)/]]', '', df.loc[i, 'description']) return df def closest_sentiment_match(dfs, query_sentiment): closest_index = None closest_difference = float('inf') analyzer = SentimentIntensityAnalyzer() for index, sub_df in enumerate(dfs): titles = sub_df['title'].tolist() aggregated_sentiment = 0 for title in titles: aggregated_sentiment += analyzer.polarity_scores(title)['compound'] average_sentiment = aggregated_sentiment / len(titles) difference = abs(average_sentiment - query_sentiment) if difference < closest_difference: closest_difference = difference closest_index = index return closest_index def main(filename, chunk_rows=33): filename = "C:/Users/hp/Desktop/Viral Square/Stephan/largeDataset/pollen/ozz/fake_job_postings.csv" df = pd.read_csv(filename, engine='python', on_bad_lines='skip') df.dropna(subset=['title', 'location'], inplace=True) df.drop_duplicates(inplace=True) df = preprocessing(df) dfs_25 = [df.iloc[i:i+chunk_rows] for i in range(0, len(df), chunk_rows)] analyzer = SentimentIntensityAnalyzer() query = "tell me more about the jobs" query_sentiment = analyzer.polarity_scores(query)['compound'] matched_index = closest_sentiment_match(dfs_25, query_sentiment) # Steps # return llama_ozz response # 1. read file ensure file format hanlde different files update create_vector_db # 2. get response from llama_ozz # 3. deploy in streamlit llama_ozz_app.py # print("The closest matching index in dfs_25 is:", matched_index) # print(dfs_25[matched_index]) # print(df.columns) # utility = EmbeddingUtility() # doc_processor = DocumentProcessor({ # "temperature": 0.0, # "max_tokens": 300, # "model": "text-davinci-003", # }, utility) # # print(dfs_25[matched_index]) # matching_df_embeddings = doc_processor.compute_matching_df_embeddings(dfs_25[matched_index]) # resp = doc_processor.answer_query_with_context(query, dfs_25[matched_index], matching_df_embeddings) # print(resp) if __name__ == "__main__": try: main() except Exception as e: print(e) print_line_of_error() # What kind of jobs are available for teachers abroad? # What is the monthly salary for jobs in Asia for teachers? # What qualifications are required for the English Teacher Abroad position? # What responsibilities does the RMA Coordinator have in the Customer Success team? # What are the preferred qualifications for the Junior Python Developer position? # What are the values that Upstream believes in as mentioned in the job description? # What are the main responsibilities of the Customer Service Associate at Novitex Enterprise Solutions? # What are the key requirements for the Data Entry Clerk II position? # What are the job responsibilities of an Engagement Executive at Upstream? # How many years of work experience are required for the Engagement Executive position?
[]
2024-01-10
nafets33/ozz
__fastapi~tempCodeRunnerFile.py
# This is just a file for unit testing codes in sandbox like a small piece of code u can use it as well :) #Conversation History to chat back and forth import json import openai import os import dotenv conversation_history = [] # Loading the json common phrases file and setting up the json file json_file = open('fastapi/greetings.json','r') common_phrases = json.load(json_file) text_obj = 'hello gpt' import json import os import openai from dotenv import load_dotenv # Loading environment variables load_dotenv('.env') # Loading the json common phrases file and setting up the json file json_file = open('fastapi/greetings.json','r') common_phrases = json.load(json_file) # Setting up the llm for conversation with conversation history def llm_assistant_response(message,conversation_history): conversation_history.append({"role": "user", "content": message}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=conversation_history, api_key=os.getenv('ozz_api_key') ) assistant_reply = response.choices[0].message["content"] return assistant_reply def Scenarios(current_query, conversation_history, first_ask=True, conv_history=False): if first_ask == True: ''' Appending the prompt for system when user asks for first time (is this first ask?) also with json coz if user again tries to ask something and doesn't found in json then it will go to llm so llm needs to be already have the json conversation to understand the next query asked by user ''' conversation_history.append({"role": "system", "content": "You are a cute and smart assistant for kids."}) # For first we will always check if anything user asked is like common phrases and present in our local json file then give response to that particular query for query, response in common_phrases.items(): if query in current_query: # Appending the user question from json file conversation_history.clear() if not conv_history else conversation_history.append({"role": "user", "content": current_query}) # Appending the response from json file conversation_history.clear() if not conv_history else conversation_history.append({"role": "assistant", "content": response}) return response else: ############## This code needs to run when the response is not present in the predefined json data ################ # Appending the user question # conversation_history.clear() if not conv_history else conversation_history.append({"role": "user", "content": current_query}) # Calling the llm assistant_response = llm_assistant_response(current_query,conversation_history) # assistant_response = 'thanks from llm' # Appending the response by llm conversation_history.clear() if not conv_history else conversation_history.append({"role": "assistant", "content": assistant_response}) return assistant_response else: # For first we will always check if anything user asked is like common phrases and present in our local json file then give response to that particular query for query, response in common_phrases.items(): if query in current_query: # Appending the user question from json file conversation_history.clear() if not conv_history else conversation_history.append({"role": "user", "content": current_query}) # Appending the response from json file conversation_history.clear() if not conv_history else conversation_history.append({"role": "assistant", "content": response}) return response else: ############## This code needs to run when the response is not present in the predefined json data ################ # Appending the user question # conversation_history.clear() if not conv_history else conversation_history.append({"role": "user", "content": current_query}) # Calling the llm assistant_response = llm_assistant_response(current_query,conversation_history) # assistant_response = 'thanks from llm' # Appending the response by llm conversation_history.clear() if not conv_history else conversation_history.append({"role": "assistant", "content": assistant_response}) return assistant_response def handle_response(text): # Kids or User question # text_obj = text[-1]['user'] #Conversation History to chat back and forth # conversation_history = [] # Call the Scenario Function resp = Scenarios(text,conversation_history,first_ask=False,conv_history=True) # print # update reponse to self # text[-1].update({'resp': resp}) with open('fastapi/conversation_history.json','w') as wri: json.dump(conversation_history,wri) return resp print(handle_response('hello dawg')) print(conversation_history)
[ "You are a cute and smart assistant for kids." ]
2024-01-10
liujie329/AgentGPT
platform~reworkd_platform~web~api~agent~helpers.py
from typing import Any, Callable, TypeVar from langchain import BasePromptTemplate, LLMChain from langchain.schema import BaseOutputParser, OutputParserException from openai import InvalidRequestError from openai.error import AuthenticationError, RateLimitError, ServiceUnavailableError from reworkd_platform.schemas import ModelSettings from reworkd_platform.web.api.agent.model_settings import create_model from reworkd_platform.web.api.errors import OpenAIError T = TypeVar("T") def parse_with_handling(parser: BaseOutputParser[T], completion: str) -> T: try: return parser.parse(completion) except OutputParserException as e: raise OpenAIError( e, "There was an issue parsing the response from the AI model." ) async def openai_error_handler( model_settings: ModelSettings, func: Callable[..., Any], *args: Any, **kwargs: Any ) -> Any: try: return await func(*args, **kwargs) except ServiceUnavailableError as e: raise OpenAIError( e, "OpenAI is experiencing issues. Visit " "https://status.openai.com/ for more info.", ) except InvalidRequestError as e: if e.user_message.startswith("The model:"): raise OpenAIError( e, f"Your API key does not have access to '{model_settings.model}'. Please use a different model.", ) raise OpenAIError(e, e.user_message) except AuthenticationError as e: raise OpenAIError( e, "Authentication error: Ensure a valid API key is being used.", ) except RateLimitError as e: if e.user_message.startswith("You exceeded your current quota"): raise OpenAIError( e, f"Your API key exceeded your current quota, please check your plan and billing details.", ) raise OpenAIError(e, e.user_message) except Exception as e: raise OpenAIError(e, "There was an issue getting a response from the AI model.") async def call_model_with_handling( model_settings: ModelSettings, prompt: BasePromptTemplate, args: dict[str, str], ) -> str: model = create_model(model_settings) chain = LLMChain(llm=model, prompt=prompt) return await openai_error_handler(model_settings, chain.arun, args)
[]
2024-01-10
liujie329/AgentGPT
platform~reworkd_platform~web~api~agent~model_settings.py
import openai from langchain.chat_models import ChatOpenAI from reworkd_platform.schemas import LLM_Model, ModelSettings from reworkd_platform.settings import settings from reworkd_platform.web.api.agent.api_utils import rotate_keys openai.api_base = settings.openai_api_base def create_model(model_settings: ModelSettings, streaming: bool = False) -> ChatOpenAI: if model_settings.custom_api_key != "": api_key = model_settings.custom_api_key else: api_key = rotate_keys( gpt_3_key=settings.openai_api_key, gpt_4_key=settings.secondary_openai_api_key, model=model_settings.model, ) return ChatOpenAI( client=None, # Meta private value but mypy will complain its missing openai_api_key=api_key, temperature=model_settings.temperature, model=get_model_name(model_settings.model), max_tokens=model_settings.max_tokens, streaming=streaming, max_retries=5, ) def get_model_name(model_str: LLM_Model) -> str: if model_str == "gpt-4": return "gpt-4-0613" if model_str == "gpt-3.5-turbo": return "gpt-3.5-turbo-0613" return model_str
[]
2024-01-10
sion42x/llama-index-milvus-example
spider.py
from llama_index import VectorStoreIndex, StorageContext, download_loader from llama_index.readers.schema.base import Document from llama_index.vector_stores import MilvusVectorStore import openai import os import json from dotenv import load_dotenv load_dotenv() openai.api_key = os.environ["OPENAI_API_KEY"] crawlUrl = "https://docs.apify.com/academy/web-scraping-for-beginners" with open("spidersettings.json", "r") as f: params = json.load(f) vector_store = MilvusVectorStore( host = "localhost", port = "19530", collection_name = "webscrape" ) def transform_dataset_item(item): return Document( text=item.get("text"), extra_info={ "url": item.get("url"), }, ) ApifyActor = download_loader("ApifyActor") reader = ApifyActor(os.environ["APIFY_API_TOKEN"]) docs = reader.load_data( actor_id="apify/website-content-crawler", dataset_mapping_function=transform_dataset_item, run_input={"startUrls": [{"url": crawlUrl}], **params} ) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents(docs, storage_context=storage_context) query_engine = index.as_query_engine() response = query_engine.query("What is this documentation about?") print(response)
[]
2024-01-10
sonusubi1/EVA
code1.py
import speech_recognition as sr from gtts import gTTS import os import openai # Set your OpenAI GPT API key openai.api_key = "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" def recognize_speech(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Listening...") audio = recognizer.listen(source) try: text = recognizer.recognize_google(audio) print("You said:", text) return text except sr.UnknownValueError: print("Speech Recognition could not understand audio.") return None except sr.RequestError as e: print(f"Could not request results from Google Speech Recognition service; {e}") return None def generate_response(prompt): response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=15 ) return response.choices[0].text.strip() def speak(text): tts = gTTS(text=text, lang="en") tts.save("response.mp3") os.system("start response.mp3") def main(): assistant_name = "eva" assistant_description = "I am your virtual assistant created by ukf college students." print(f"Hello! I am {assistant_name}. {assistant_description}") while True: user_input = recognize_speech() if user_input: if user_input.lower() == "exit": print("Goodbye!") break prompt = f"{assistant_name}: {assistant_description}\nUser: {user_input}\n{assistant_name}:" response = generate_response(prompt) print(f"{assistant_name}: {response}") speak(response) if __name__ == "__main__": main()
[ "assistant_name358de371-467b-4d50-8e9e-f4c9b684c9c4: I am your virtual assistant created by ukf college students.\nUser: PLACEHOLDER\nassistant_name358de371-467b-4d50-8e9e-f4c9b684c9c4:", "eva: I am your virtual assistant created by ukf college students.\nUser: PLACEHOLDER\neva:" ]
2024-01-10
acherm/gptchess
gptchess~gpt-experiments.py
#!/usr/bin/env python3 import io import random from stockfish import Stockfish import openai import chess import chess.pgn import os from dataclasses import dataclass from parsing_moves_gpt import extract_move_chatgpt import uuid openai.organization = "" openai.api_key = os.getenv('OPENAI_KEY') BASE_PGN = """[Event "FIDE World Championship Match 2024"] [Site "Los Angeles, USA"] [Date "2024.12.01"] [Round "5"] [White "Carlsen, Magnus"] [Black "Nepomniachtchi, Ian"] [Result "1-0"] [WhiteElo "2885"] [WhiteTitle "GM"] [WhiteFideId "1503014"] [BlackElo "2812"] [BlackTitle "GM"] [BlackFideId "4168119"] [TimeControl "40/7200:20/3600:900+30"] [UTCDate "2024.11.27"] [UTCTime "09:01:25"] [Variant "Standard"] 1.""" def setup_directory(): OUTPUT_DIR = "games/" dir_name = OUTPUT_DIR + "game" + str(uuid.uuid4()) os.makedirs(dir_name, exist_ok=True) return dir_name def log_msg(dir_name, message): with open(os.path.join(dir_name, "log.txt"), "a") as log_file: log_file.write(message + "\n") def record_session(dir_name, prompt, response, system_role_message = None): with open(os.path.join(dir_name, "session.txt"), "a") as session_file: if system_role_message is not None: session_file.write("SYSTEM: " + system_role_message + "\n") session_file.write("PROMPT: " + prompt + "\n") session_file.write("RESPONSE: " + response + "\n\n") @dataclass class ChessEngineConfig: skill_level: int engine_depth: int = 20 engine_time: int = None random_engine: bool = False @dataclass class GPTConfig: temperature: float = 0 max_tokens: int = 4 chat_gpt: bool = False system_role_message: str = None model_gpt: str = "gpt-3.5-turbo-instruct" from dataclasses import asdict import os @dataclass class ChessEngineConfig: skill_level: int engine_depth: int = 20 engine_time: int = None random_engine: bool = False @dataclass class GPTConfig: temperature: float = 0 max_tokens: int = 4 chat_gpt: bool = False system_role_message: str = None model_gpt: str = "gpt-3.5-turbo-instruct" def save_metainformation_experiment(dir_name, chess_config: ChessEngineConfig, gpt_config: GPTConfig, base_pgn, nmove, white_piece, engine_parameters): with open(os.path.join(dir_name, "metainformation.txt"), "w") as metainformation_file: metainformation_file.write(f"model_gpt: {gpt_config.model_gpt}\n") metainformation_file.write(f"skill_level: {chess_config.skill_level}\n") metainformation_file.write(f"random_engine: {chess_config.random_engine}\n") metainformation_file.write(f"white_piece: {white_piece}\n") metainformation_file.write(f"engine_depth: {chess_config.engine_depth}\n") metainformation_file.write(f"engine_time: {chess_config.engine_time}\n") metainformation_file.write(f"base_pgn: {base_pgn}\n") metainformation_file.write(f"nmove: {nmove}\n") metainformation_file.write(f"engine_parameters: {engine_parameters}\n") metainformation_file.write(f"temperature: {gpt_config.temperature}\n") metainformation_file.write(f"max_tokens: {gpt_config.max_tokens}\n") metainformation_file.write(f"chat_gpt: {gpt_config.chat_gpt}\n") metainformation_file.write(f"system_role_message: {gpt_config.system_role_message if gpt_config.system_role_message else 'None'}\n") # based on https://github.com/official-stockfish/Stockfish/issues/3635#issuecomment-1159552166 def skill_to_elo(n): correspondence_table = { 0: 1347, 1: 1490, 2: 1597, 3: 1694, 4: 1785, 5: 1871, 6: 1954, 7: 2035, 8: 2113, 9: 2189, 10: 2264, 11: 2337, 12: 2409, 13: 2480, 14: 2550, 15: 2619, 16: 2686, 17: 2754, 18: 2820, 19: 2886, 20: 3000, # rough estimate } if n in correspondence_table: return correspondence_table[n] else: raise ValueError("Input should be between 0 and 19 inclusive.") from dataclasses import dataclass # TODO: chess engine: SF, random, Leela, etc. # ELO: Elo rating of the SF engine # RANDOM_ENGINE: if True, GPT plays against a random engine (not Stockfish) # model_gpt: GPT model to use # nmove = number of move when the game starts: def play_game(chess_config: ChessEngineConfig, gpt_config: GPTConfig, base_pgn=BASE_PGN, nmove=1, white_piece=True): # def play_game(skill_level, base_pgn=BASE_PGN, nmove=1, random_engine = False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=True, engine_depth=20, engine_time=None, temperature=0, max_tokens=4, chat_gpt=False, system_role_message = None): pgn = base_pgn skill_level = chess_config.skill_level engine_depth = chess_config.engine_depth engine_time = chess_config.engine_time random_engine = chess_config.random_engine temperature = gpt_config.temperature max_tokens = gpt_config.max_tokens chat_gpt = gpt_config.chat_gpt system_role_message = gpt_config.system_role_message model_gpt = gpt_config.model_gpt dir_name = setup_directory() print(dir_name) # stockfish = Stockfish("./stockfish/stockfish/stockfish-ubuntu-x86-64-avx2", depth=engine_depth) stockfish = Stockfish("./stockfish/stockfish/stockfish-ubuntu-x86-64-avx2", depth=engine_depth) # , depth=engine_depth) "/home/mathieuacher/Downloads/Enko/EnkoChess_290818") # # stockfish.set_elo_rating(engine_elo) stockfish.set_skill_level(skill_level) engine_parameters = stockfish.get_parameters() save_metainformation_experiment(dir_name, chess_config, gpt_config, pgn, nmove, white_piece, engine_parameters) board = chess.Board() if nmove > 1: # if nmove > 1, we need to load the PGN # load a PGN file g = chess.pgn.read_game(io.StringIO(base_pgn)) board = g.end().board() stockfish.set_position([str(m) for m in g.mainline_moves()]) n = nmove unknown_san = None # can be the case that GPT plays an unknown SAN (invalid move) # If GPT plays as white, it should make the first move. if white_piece: if (chat_gpt): response = openai.ChatCompletion.create( model=model_gpt, messages=[ {"role": "system", "content": system_role_message}, {"role": "user", "content": pgn} ], temperature=temperature, max_tokens=max_tokens ) else: response = openai.Completion.create( model=model_gpt, prompt=pgn, temperature=temperature, max_tokens=max_tokens, ) if chat_gpt: resp = response['choices'][0]['message']['content'] else: resp = response.choices[0].text # completion record_session(dir_name, pgn, resp) if chat_gpt: san_move = extract_move_chatgpt(resp) else: san_move = resp.strip().split()[0] try: move = board.push_san(san_move) except: log_msg(dir_name, "unknown san: {}".format(san_move)) # perhaps add a PGN comment with the unknown SAN unknown_san = san_move return uci_move = move.uci() pgn += f" {san_move}" stockfish.make_moves_from_current_position([f"{uci_move}"]) # log_msg(dir_name, stockfish.get_board_visual()) log_msg(dir_name, pgn) while True: if random_engine: # random move: choose a random move from the list of legal moves legal_moves = board.legal_moves # pick a random one among legal_moves uci_move = random.choice(list(legal_moves)).uci() else: if engine_time is None: uci_move = stockfish.get_best_move() else: uci_move = stockfish.get_best_move_time(engine_time) move = chess.Move.from_uci(uci_move) san_move = board.san(move) board.push(move) pgn += f" {san_move}" stockfish.make_moves_from_current_position([f"{uci_move}"]) # log_msg(dir_name, stockfish.get_board_visual()) log_msg(dir_name, pgn) # TODO: if ChatGPT, not useful since we're using another strategy to prompt based on messages if board.is_checkmate(): log_msg(dir_name, "Stockfish" + str(skill_to_elo(skill_level)) + "ELO won!") break if board.is_stalemate() or board.is_insufficient_material() or board.is_fivefold_repetition() or board.is_seventyfive_moves(): log_msg(dir_name, "Draw!") break if white_piece: n += 1 pgn += f" {n}." if (chat_gpt): msgs = [{"role": "system", "content": system_role_message}] nply = 1 temp_board = chess.pgn.Game().board() for move in chess.pgn.Game.from_board(board).mainline_moves(): move_san = temp_board.san(move) # parse_san(str(move)) if nply % 2 != 0: move_str = str(int(nply/2) + 1) + '. ' + str(move_san) else: move_str = str(int(nply/2)) + '... ' + str(move_san) if white_piece and nply % 2 != 0: msgs.append({"role": "assistant", "content": move_str}) else: msgs.append({"role": "user", "content": move_str}) temp_board.push(move) nply = nply + 1 log_msg(dir_name, str(msgs)) response = openai.ChatCompletion.create( model=model_gpt, messages=msgs, # [ # {"role": "system", "content": system_role_message}, # {"role": "user", "content": pgn} # ], temperature=temperature, max_tokens=max_tokens ) else: response = openai.Completion.create( model=model_gpt, prompt=pgn, temperature=temperature, max_tokens=max_tokens, ) if chat_gpt: resp = response['choices'][0]['message']['content'] else: resp = response.choices[0].text # completion record_session(dir_name, pgn, resp) if chat_gpt: san_move = extract_move_chatgpt(resp) log_msg(dir_name, "SAN MOVE: " + resp + " " + str(san_move)) else: san_move = resp.strip().split()[0] try: move = board.push_san(san_move) except: log_msg(dir_name, "unknown san: {}".format(san_move)) # perhaps add a PGN comment with the unknown SAN unknown_san = san_move break uci_move = move.uci() pgn += f" {san_move}" stockfish.make_moves_from_current_position([f"{uci_move}"]) log_msg(dir_name, stockfish.get_board_visual()) if board.is_checkmate(): log_msg(dir_name, model_gpt + " won!") break if board.is_stalemate() or board.is_insufficient_material() or board.is_fivefold_repetition() or board.is_seventyfive_moves(): log_msg(dir_name, "Draw!") break if not white_piece: n += 1 pgn += f" {n}." game = chess.pgn.Game.from_board(board) if random_engine and white_piece: game.headers["Event"] = "{} vs {}".format(model_gpt, "RANDOM chess engine") game.headers["White"] = "{}".format(model_gpt) game.headers["Black"] = "{}".format("RANDOM chess engine") game.headers["WhiteElo"] = "?" game.headers["BlackElo"] = "?" elif random_engine and not white_piece: game.headers["Event"] = "{} vs {}".format("RANDOM chess engine", model_gpt) game.headers["White"] = "{}".format("RANDOM chess engine") game.headers["Black"] = "{}".format(model_gpt) game.headers["WhiteElo"] = "?" game.headers["BlackElo"] = "?" elif white_piece: game.headers["Event"] = "{} vs Stockfish".format(model_gpt) game.headers["White"] = "{}".format(model_gpt) game.headers["Black"] = "Stockfish" game.headers["WhiteElo"] = "?" game.headers["BlackElo"] = str(skill_to_elo(skill_level)) else: game.headers["Event"] = "{} vs Stockfish".format(model_gpt) game.headers["White"] = "Stockfish" game.headers["Black"] = "{}".format(model_gpt) game.headers["WhiteElo"] = str(skill_to_elo(skill_level)) game.headers["BlackElo"] = "?" if unknown_san is not None: game.headers["UnknownSAN"] = unknown_san # export game as PGN string pgn_final = game.accept(chess.pgn.StringExporter()) # At the end of play_game(), write the PGN to the game.pgn file inside the game's directory with open(os.path.join(dir_name, "game.pgn"), "w") as f: f.write(pgn_final) f.write("\n") return pgn BASE_PGN_HEADERS = """[Event "FIDE World Championship Match 2024"] [Site "Los Angeles, USA"] [Date "2024.12.01"] [Round "5"] [White "Carlsen, Magnus"] [Black "Nepomniachtchi, Ian"] [Result "1-0"] [WhiteElo "2885"] [WhiteTitle "GM"] [WhiteFideId "1503014"] [BlackElo "2812"] [BlackTitle "GM"] [BlackFideId "4168119"] [TimeControl "40/7200:20/3600:900+30"] [UTCDate "2024.11.27"] [UTCTime "09:01:25"] [Variant "Standard"] """ # generate a random PGN with the first 10 random moves of a random game # ply = half move def mk_randomPGN(max_plies = 40): board = chess.Board() i = 0 while i < max_plies: legal_moves = list(board.legal_moves) uci_move = random.choice(legal_moves).uci() move = chess.Move.from_uci(uci_move) board.push(move) i = i + 1 game = chess.pgn.Game.from_board(board) current_move = round(len(list(game.mainline_moves())) / 2) + 1 pgn = BASE_PGN_HEADERS + '\n' + str(game.mainline_moves()) + " " + str(current_move) + "." return pgn BASE_PGN_HEADERS_ALTERED = """[Event "Chess tournament"] [Site "Rennes FRA"] [Date "2023.12.09"] [Round "7"] [White "MVL, Magnus"] [Black "Ivanchuk, Ian"] [Result "1-0"] [WhiteElo "2737"] [BlackElo "2612"] 1.""" ### basic: starting position, classical game # play_game(skill_level=5, base_pgn=BASE_PGN, nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=False, engine_depth=15, engine_time=None, temperature=0.8, chat_gpt=False) # play_game(skill_level=5, base_pgn=BASE_PGN, nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=True, max_tokens=6) # play_game(skill_level=2, base_pgn='It is your turn! You have white pieces. Please complete the chess game using PGN notation. 1.', nmove=1, random_engine=False, model_gpt = "gpt-4", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=True, max_tokens=6, system_role_message="You are a professional, top international grand-master chess player. We are playing a serious chess game, using PGN notation. When it's your turn, you have to play your move using PGN notation.") # play_game(skill_level=3, base_pgn=BASE_PGN, nmove=1, random_engine=False, model_gpt = "text-davinci-003", white_piece=False, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=False, max_tokens=5) # play_game(skill_level=-1, base_pgn=BASE_PGN, nmove=1, random_engine=True, model_gpt = "text-davinci-003", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=False, max_tokens=5) # play_game(skill_level=-1, base_pgn=BASE_PGN, nmove=1, random_engine=True, model_gpt = "text-davinci-003", white_piece=False, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=False, max_tokens=5) # play_game(skill_level=3, base_pgn=BASE_PGN_HEADERS_ALTERED, nmove=1, random_engine=False, model_gpt = "text-davinci-003", white_piece=False, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=False, max_tokens=5) # play_game(skill_level=4, base_pgn=BASE_PGN, nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=False, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=False, max_tokens=5) # Create instances of ChessEngineConfig and GPTConfig using the provided parameters. chess_config = ChessEngineConfig( skill_level=4, engine_depth=15, engine_time=None, random_engine=False ) gpt_config = GPTConfig( model_gpt="gpt-3.5-turbo-instruct", temperature=0.0, max_tokens=5, chat_gpt=False, system_role_message=None # Since it wasn't provided in the original call ) # Call the refactored function. play_game(chess_config, gpt_config, base_pgn=BASE_PGN, nmove=1, white_piece=False) # # play_game(skill_level=-1, base_pgn='It is your turn! You have white pieces. Please complete the chess game using PGN notation. 1.', nmove=1, random_engine=True, model_gpt = "gpt-4", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=True, max_tokens=6, system_role_message="You are a professional, top international grand-master chess player. We are playing a serious chess game, using PGN notation. When it's your turn, you have to play your move using PGN notation.") # play_game(skill_level=-1, base_pgn='It is your turn! You have white pieces. Please complete the chess game using PGN notation. 1.', nmove=1, random_engine=True, model_gpt = "gpt-3.5-turbo", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=True, max_tokens=6, system_role_message="You are a professional, top international grand-master chess player. We are playing a serious chess game, using PGN notation. When it's your turn, you have to play your move using PGN notation.") # play_game(skill_level=3, base_pgn='It is your turn! You have white pieces. Please complete the chess game using PGN notation. 1.', nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0, chat_gpt=True, max_tokens=6, system_role_message="You are a professional, top international grand-master chess player. We are playing a serious chess game, using PGN notation. When it's your turn, you have to play your move using PGN notation.") # playing with random engine! # play_game(skill_level=-1, base_pgn=BASE_PGN, random_engine=True, nmove=1, model_gpt = "gpt-3.5-turbo-instruct", white_piece=False, engine_depth=15, engine_time=None, temperature=0.0) # playing with altered prompt # play_game(skill_level=4, base_pgn=BASE_PGN_HEADERS_ALTERED, nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=True, engine_depth=15, engine_time=None, temperature=0.0) # TODO: random engine ####### case random first moves def random_firstmoves(): nplyes = 20 # should be an even number (white turning) base_pgn = mk_randomPGN(nplyes) nmove = int((nplyes/2)+1) play_game(skill_level=5, base_pgn=base_pgn, nmove=nmove, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=True, engine_depth=15, engine_time=None) # random_firstmoves() ###### case known first moves (to diversify a bit the openings) def diversify_with_knownopenings(): nmove = 2 base_pgns = [ '1. e4 e5 2.', '1. d4 Nf6 2.', '1. e4 c5 2.', '1. d4 d5 2.', '1. e4 e6 2.'] base_pgn = BASE_PGN_HEADERS + '\n' + random.choice(base_pgns) play_game(skill_level=4, base_pgn=base_pgn, nmove=nmove, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=True, engine_depth=15, engine_time=None) # diversify_with_knownopenings() # play_game(skill_level=4, base_pgn=BASE_PGN, nmove=1, random_engine=False, model_gpt = "gpt-3.5-turbo-instruct", white_piece=True, engine_depth=15, engine_time=None) # text-davinci-003
[]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~make_reservation~property_selected_extractor.py
from typing import List from app.model import Message import openai import os import json from app.utils import logger from datetime import datetime, timedelta openai.api_key = os.environ.get('OPENAI_API_KEY') json_fn = { "name": "set_property_selected", "description": "Saves the property information that the user has chosen.", "parameters": { "type": "object", "properties": { "property_id": { "type": "string", "description": "The property_id of the property that the user has chosen." }, }, "required": [] } } system_message = """Save the property that the user has chosen. Here is the information fo the properties: {properties_info}""" class PropertySelectedExtractor: def run(self, messages: List[Message], properties_info: str): formatted_system_message = system_message.format(properties_info=properties_info) messages_input = [{"role": "system", "content": formatted_system_message}] for msg in messages: messages_input.append({"role": msg.role, "content": msg.text}) # messages_input.append("role") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages_input, functions=[json_fn], temperature=0., max_tokens=500, ) if "function_call" in response.choices[0].message and "arguments" in response.choices[0].message["function_call"]: fn_parameters = json.loads(response.choices[0].message["function_call"]["arguments"]) fn_parameters["user_has_selected"] = ("property_id" in fn_parameters and fn_parameters["property_id"] != "") logger.debug(f"set_property_selected fn_parameters {fn_parameters}") return fn_parameters return {"user_has_selected": False }
[]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~select_business~business_selected_extractor.py
from typing import List from app.model import Message import openai import os import json from app.utils import logger from datetime import datetime, timedelta openai.api_key = os.environ.get('OPENAI_API_KEY') system_message = """Save the bnbot_id of the business chosen by the user if the user has chosen one already. Here are the available businesses: {available_businesses}""" json_fn = { "name": "set_business_selected", "description": "Saves the name of the business the user has chosen.", "parameters": { "type": "object", "properties": { "bnbot_id": { "type": "string", "description": "The bnbot_id of the business the user has chosen." }, }, "required": ["bnbot_id"] } } class BusinessSelectedExtractor: def run(self, messages: List[Message], available_businesses: str): formatted_system_message = system_message.format(available_businesses=available_businesses) messages_input = [{"role": "system", "content": formatted_system_message}] for msg in messages: messages_input.append({"role": msg.role, "content": msg.text}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages_input, functions=[json_fn], temperature=0., max_tokens=500, ) if "function_call" in response.choices[0].message and "arguments" in response.choices[0].message["function_call"]: fn_parameters = json.loads(response.choices[0].message["function_call"]["arguments"]) fn_parameters["user_has_selected"] = ("bnbot_id" in fn_parameters and fn_parameters["bnbot_id"] != "") logger.debug(f"set_business_selected fn_parameters {fn_parameters}") return fn_parameters return {"user_has_selected": False }
[]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~task_router~task_identifier_resolver.py
from typing import List, Any from app.task_resolver.engine import StepResolver, StepData from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from app.model import Message from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose #from langchain.llms import OpenAI template="""You are an Assistant that helps users. Your task is to identify which action does the user want to do. You allways answer in Spanish. Here is the list of possible actions: MAKE_RESERVATION_TASK: the user wants to book an accommodation. PROPERTIES_INFORMATION_TASK: the user needs information about the properties available. RESERVATION_INFORMATION_TASK: the user wants information about an accommodation booked before. OTHER: when None of the actions described above fits. Here is the conversation: {chat_history} {format_instructions}""" response_schemas = [ ResponseSchema(name="task_id", description="The task_id of the Task that the user wants to perform."), ResponseSchema(name="text", description="The response to the user."), ] class TaskExtractorChain: def __init__(self): llm = ChatOpenAI(temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions =self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["chat_history"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="task_info") def __call__(self, chat_history): info = self.chain({"chat_history": chat_history}) return self.output_parser.parse(info["task_info"]) class TaskIdentifierResolver(StepResolver): def run(self, messages: List[Message], previous_steps_data: List[Any], step_chat_history: List[Message] = None) -> Message: chat_history = self.build_chat_history(messages) task_extractor = TaskExtractorChain() task_info = task_extractor(chat_history) if task_info["task_id"] != "": self.data["task_info"] = task_info return Message.route_message(task_info["text"], task_info["task_id"]) return Message.route_message(task_info["text"], "OTHER") def is_done(self): if "task_info" not in self.data: return False return (self.data["task_info"]["task_id"] != "" and self.data["task_info"]["task_id"] is not None and self.data["task_info"]["task_id"] != "OTHER")
[ "chat_history", "format_instructions", "You are an Assistant that helps users.\nYour task is to identify which action does the user want to do. You allways answer in Spanish.\n\nHere is the list of possible actions:\n\nMAKE_RESERVATION_TASK: the user wants to book an accommodation.\nPROPERTIES_INFORMATION_TASK: the user needs information about the properties available.\nRESERVATION_INFORMATION_TASK: the user wants information about an accommodation booked before.\nOTHER: when None of the actions described above fits.\n\nHere is the conversation: \n{chat_history}\n\n{format_instructions}" ]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~select_business~gather_business_info_resolver.py
from app.task_resolver.engine import StepResolver, StepData from app.utils import logger from app.utils import get_completion_from_messages from app.tools import BusinessSearchDataExtractor from typing import List, Any from datetime import datetime, timedelta from app.integrations import OpenAIClient from app.model import Message system_message="""You are an Assistant that gathers information from the user about booking an accommodation. Your task is only to help the user find the property for booking, any other tasks must not be handled by you. Follow these steps before responding to the user: Step 1: Check if the user provided a business ID.\ NOTE: The ID of the business does not have spaces and always starts with the @ character. i.e: '@casa.de.alejandro'. Step 2: If the user has not provided the business ID and has not provided the location of the property ask him to provide it. \ If the location was provided, make sure that it is one of the available ones. \ If the location is not available, then apologize with the user and tell him that it will be available soon. \ The only available locations are: 1. Mercedes, Uruguay 2. Montevideo, Uruguay Step 3: If the user has not provided the name of the business, ask him to provide so you can search for it. Explain him that the name \ is something similar to the title of the publication in other platforms like booking or airbnb. Here is the conversation: {chat_history} You respond in a short, very conversational friendly style. response to th user: """ class GatherBusinessInfoResolver(StepResolver): def run(self, messages: List[Message], previous_steps_data: dict, step_chat_history: List[Message] = None) -> Message: business_search_data_extractor = BusinessSearchDataExtractor() chat_input = OpenAIClient.build_messages_from_conversation(system_message, messages) assistant_response = get_completion_from_messages(chat_input) business_info = business_search_data_extractor.run(messages) self.data["business_info"] = business_info return Message.assistant_message(assistant_response) def is_done(self): if "business_info" not in self.data: return False business_info = self.data["business_info"] return (("bnbot_id" in business_info and business_info["bnbot_id"] is not None) or (("location" in business_info and business_info["location"] is not None) and (("business_name" in business_info and business_info["business_name"] is not None) or ("business_owner" in business_info and business_info["business_owner"]))))
[]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~select_business~business_selection_resolver.py
from app.task_resolver.engine import StepResolver from app.task_resolver.engine import StepData from typing import List, Any from app.tools import PropertiesFilterTool, BusinessSelectedExtractor, HouseSelectionAssistantTool from app.utils import logger import json from app.utils import get_completion_from_messages from app.integrations import OpenAIClient, BackendAPIClient from app.model import Message system_message_no_business ="""You are an Assistant that helps users choose a business \ for renting a house for short stays. Your job is to help the user select one business from the available businesses. Tell the user that we have not find the business he is looking for \ and suggest to visit the site https://reservamedirecto.com to \ find the business ID and come back again later. You respond in a short, very conversational friendly style. response to th user:""" system_message_business ="""Help the user select one business from the available businesses. Follow these steps before responding to the user: Step 1: Ask the user to choose one business from the following list, show him the name and address for each business: {businesses_info} Step 2: Make sure the user select one where there are multiple options, if there is only one make sure the user agrees with it. Step 3: If the user does not want any of the businesses from the list, thank him. You respond in a short, very conversational friendly style. response to th user:""" # system_message = """You are an Assistant that helps users choose a business \ # for renting a house for short stays. # Your job is to help the user select one business from the available businesses. # These are the available businesses: # {businesses_info} # Follow these steps before responding to the user: # Step 1: Count the number of available businesses. # Step 2: If there are no businesses available, tell the user that we have not find the business \ # and suggest him to visit the site https://reservamedirecto.com and find the business ID from there. # Step 3: If there are available businesses, ask the user to choose one business from the \ # following list: # {businesses_info} # You respond in a short, very conversational friendly style. # response to th user: # """ class BusinessSelectionResolver(StepResolver): # backend_api_client: BackendAPIClient def __init__(self, backend_url: str): self.backend_api_client = BackendAPIClient(backend_url) super().__init__() def _format_json(self, businesses): formatted_string = '' idx = 1 for business in businesses: formatted_string += f"""{idx}. business_name: {business['business_name']} bnbot_id: {business['bnbot_id']} address: {business['address']}" city: {business['city']}"\n""" idx +=1 return formatted_string def _get_business_prompt_info(self, businesses): data = [] for business in businesses: data.append({ "business_name": business['business_name'], "bnbot_id": business['bnbot_id'], "address":f"{business['location']['address']}", "city":f"{business['location']['city']}" }) return data def run(self, messages: List[Message], previous_steps_data: dict, step_chat_history: List[Message] = None) -> Message: gather_business_info_step_data: StepData = previous_steps_data["GATHER_BUSINESS_INFO"] business_info = gather_business_info_step_data.resolver_data["business_info"] logger.debug(f"list_businesses input {business_info}") business_list = self.backend_api_client.list_businesses(business_info) logger.debug(f"list_businesses output {business_list}") if len(business_list) == 0: # Not found businesses_info = "Unfortunately there are no businesses available." # Inform, came back to previous step, erase previous step data self.data["business_info"] = { "properties_available": False, "user_has_selected": False, "bnbot_id": "" } # formatted_system_message = system_message.format(businesses_info=self._format_json(businesses_info)) chat_input = OpenAIClient.build_messages_from_conversation(system_message_no_business, messages) assistant_response = get_completion_from_messages(chat_input) return Message.assistant_message(assistant_response) self.data["business_info"] = { "properties_available": True, "user_has_selected": False } # Select 1 from the list found and confirm. businesses_info = self._get_business_prompt_info(business_list) formatted_system_message = system_message_business.format(businesses_info=self._format_json(businesses_info)) chat_input = OpenAIClient.build_messages_from_conversation(formatted_system_message, messages) assistant_response = get_completion_from_messages(chat_input) if not self.data["step_first_execution"] and len(business_list) > 0: extractor = BusinessSelectedExtractor() extractor_result = extractor.run(messages, self._format_json(businesses_info)) if extractor_result["user_has_selected"]: self.data["business_info"]["bnbot_id"] = extractor_result["bnbot_id"] self.data["business_info"]["user_has_selected"] = extractor_result["user_has_selected"] return Message.assistant_message(assistant_response) def is_done(self): if "business_info" not in self.data: return False if not self.data["business_info"]["properties_available"]: return True # There are properties_available and the user has selected already. return self.data["business_info"]["user_has_selected"]
[]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~make_reservation~booking_confirmation_tool.py
from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose template="""You are an Assistant that helps users book houses for short-term stay. Your task is make the user confirmed the booking based on the conversation. You allways respond in Spanish. Follow these Steps before responding to the user new message: Step 1: Ask the user if he wants to place the booking by showing him a summary of the booking. \ You must NOT great the customer when asking. \ Here is the booking information the user is just about to place: Name: {user_name} Email: {email} Check-in: {checkin_date} Check-out: {checkout_date} Number of guests: {num_guests} House: {property_id} Price per night: {price_per_night} Total price: {total_price} Step 2: If the user has confirmed to palce the booking, you tell him that the reservation is booked for 3 hours \ and that you will send an email with information about the booking along with payment instructions. Step 3: If the user does not want place the booking, thank him and let him know that you are there if it needs anything else. Here is a list of the last messages you exchanged with the user: {chat_history} {format_instructions}""" response_schemas = [ ResponseSchema(name="booking_placed", description="True if the user decided to place the booking, False is the user does not want to place the booking, If the user has not answered yet return an empty string"), ResponseSchema(name="text", description="The response to the user"), ] class BookingConfirmationChain: def __init__(self): llm = ChatOpenAI(temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions =self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["user_name", "email", "checkin_date", "checkout_date", "num_guests", "property_id", "chat_history", "price_per_night", "total_price"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="booking_confirmation_info") def run(self, booking_info, chat_history): info = self.chain({"chat_history": chat_history, "user_name": booking_info["user_name"], "email": booking_info["email"], "checkin_date": booking_info["check_in_date"], "checkout_date": booking_info["check_out_date"], "num_guests": booking_info["num_guests"], "property_id": booking_info["property_id"], "price_per_night": booking_info["price_per_night"], "total_price": booking_info["total_price"]}) return self.output_parser.parse(info["booking_confirmation_info"])
[ "property_id", "user_name", "checkout_date", "num_guests", "chat_history", "format_instructions", "checkin_date", "You are an Assistant that helps users book houses for short-term stay. \nYour task is make the user confirmed the booking based on the conversation.\nYou allways respond in Spanish.\nFollow these Steps before responding to the user new message:\n\nStep 1: Ask the user if he wants to place the booking by showing him a summary of the booking. You must NOT great the customer when asking. Here is the booking information the user is just about to place:\nName: {user_name}\nEmail: {email}\nCheck-in: {checkin_date}\nCheck-out: {checkout_date}\nNumber of guests: {num_guests}\nHouse: {property_id}\nPrice per night: {price_per_night}\nTotal price: {total_price}\n\nStep 2: If the user has confirmed to palce the booking, you tell him that the reservation is booked for 3 hours \\ \nand that you will send an email with information about the booking along with payment instructions.\n\nStep 3: If the user does not want place the booking, thank him and let him know that you are there if it needs anything else.\n\nHere is a list of the last messages you exchanged with the user: \n{chat_history}\n\n{format_instructions}", "price_per_night", "total_price" ]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~select_business~business_search_data_extractor.py
from typing import List from app.model import Message import openai import os import json from app.utils import logger, remove_spanish_special_characters from datetime import datetime, timedelta, date openai.api_key = os.environ.get('OPENAI_API_KEY') json_fn = { "name": "calculate_business_info", "description": "Gathers all the available properties based on the location, bnbot_id, business name or business owner.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The name of the city or location where the property is." }, "bnbot_id": { "type": "string", "description": "The ID of the business the user is looking for. The id does not have spaces and always starts with the @ character. i.e: '@casa.de.alejandro'." }, "business_name": { "type": "string", "description": "The name of the business the user is looking for. This could be a sentence like: 'Hermoso apartamento en edificio centrico en Mercedes." }, "business_owner": { "type": "string", "description": "The name of the business the owner. This is a person's name like: Gonzalo Sarasua or just Gonzalo." } }, "required": [] } } class BusinessSearchDataExtractor: def _have_enough_info(self, fn_parameters: dict): if "bnbot_id" in fn_parameters and fn_parameters["bnbot_id"] != "": return True if "location" in fn_parameters and fn_parameters["location"] != "": fn_parameters["location"] = remove_spanish_special_characters(fn_parameters["location"]) if "business_name" in fn_parameters and fn_parameters["business_name"] != "": return True if "business_owner" in fn_parameters and fn_parameters["business_owner"] != "": return True return False def run(self, messages: List[Message]): messages_input = [{"role": "system", "content": "What are the available properties that the user is looking for?"}] for msg in messages: messages_input.append({"role": msg.role, "content": msg.text}) # messages_input.append("role") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages_input, functions=[json_fn], # function_call={"name": "calculate_business_info"}, temperature=0., max_tokens=500, ) if "function_call" in response.choices[0].message and "arguments" in response.choices[0].message["function_call"]: fn_parameters = json.loads(response.choices[0].message["function_call"]["arguments"]) fn_parameters["have_enough_info"] = self._have_enough_info(fn_parameters) logger.debug(f"calculate_business_info fn_parameters {fn_parameters}") return fn_parameters return {"have_enough_info": False}
[ "What are the available properties that the user is looking for?" ]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~tasks~select_business_task.py
from typing import List, Optional from app.task_resolver.engine import Task, Step from app.task_resolver.engine.task_model import Step from app.task_resolver.step_resolvers import GatherBusinessInfoResolver, BusinessSelectionResolver, PostProcessRouterResolver from app.integrations import OpenAIClient from .make_reservation_task import create_make_reservation_task class SelectBusinessTask(Task): def __init__(self): gather_business_info_step = Step(name="GATHER_BUSINESS_INFO", resolver =GatherBusinessInfoResolver(), reply_when_done=False) business_selection_step = Step(name="BUSINESS_SELECTION", resolver = BusinessSelectionResolver(backend_url="http://web:80"), reply_when_done=False) super().__init__(name="SELECT_BUSINESS_TASK", steps=[gather_business_info_step, business_selection_step]) def get_next_task(self) -> Optional[Task]: if self.is_done(): if (self.steps[-1].data.resolver_data["business_info"]["bnbot_id"] is not None and self.steps[-1].data.resolver_data["business_info"]["bnbot_id"] != ""): return create_make_reservation_task() return None def create_select_business_task(): return SelectBusinessTask()
[]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_gather_business_info_resolver.py
import unittest from unittest.mock import MagicMock from app.model import Message, StepData from app.task_resolver.step_resolvers import GatherBusinessInfoResolver import os import openai from dotenv import load_dotenv, find_dotenv class TestGatherBusinessInfoResolver(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] def test_run(self): # Arrange def _create_step_data(info) -> StepData: step_data = StepData() step_data.resolver_data =info return step_data test_cases = [ { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "step_chat_history": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "expected_resolver_done": False }, { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "step_chat_history": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "expected_resolver_done": False }, # { # "messages": [ # Message("user", "Hola"), # Message("assistant", "Hola, ¿en qué puedo ayudarte?"), # Message("user", "Me gustaría reservar @casa.en.altos para dos personas, para el jueves que viene por dos noches.") # ], # "step_chat_history": [ # Message("user", "Hola"), # Message("assistant", "Hola, ¿en qué puedo ayudarte?"), # Message("user", "Me gustaría reservar @casa.en.altos para dos personas, para el jueves que viene por dos noches.") # ], # "expected_resolver_done": True # } ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") resolver = GatherBusinessInfoResolver() # Act result = resolver.run(test["messages"], {}, test["step_chat_history"]) # Assert self.assertIsNotNone(result) self.assertEqual(test["expected_resolver_done"], resolver.is_done()) if __name__ == '__main__': unittest.main()
[ "Hola, ¿en qué puedo ayudarte?", "assistant", "user", "Hola", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches." ]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~make_reservation~house_selection_assistant_tool.py
from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose from app.utils import logger template="""You are an Assistant that helps users choose an accommodation based on its preferences. Your task is only to help the user choose an accommodation option for booking and answer any question about it, \ any other tasks must not be handled by you. The user has already selected the business, now you help him choose an accommodation \ option within the business i.e: A room within the business. Follow these steps before responding to the user: Step 1: If you have not shown the user a summary of the available options, show the summary including a brief description, amenities and the price per night for each option \ and ask the user if he wants to book any of them. Here is the list of available options: \ {properties_info} Step 2: If the user makes any question about the options after showing the summary, answer it based on the available options information. Step 3: If the user asks if there are other options available, you respond that there are no more available for those dates. Step 4: If the user does not want any of the available options, \ you apologize and tell the user that you will notify him if you have something new available in the future. Here is the conversation: {chat_history} You respond in a short, very conversational friendly style. response to th user: """ class HouseSelectionAssistantTool: def __init__(self): llm = ChatOpenAI(temperature=0.) prompt_template = PromptTemplate( input_variables=["chat_history", "properties_info"], template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="result") def run(self, chat_history, properties_info): info = self.chain({"properties_info": properties_info, "chat_history": chat_history}) # logger.debug(f"HouseSelectionAssistantTool result {info}") return info["result"]
[ "chat_history", "properties_info", "You are an Assistant that helps users choose an accommodation based on its preferences. \nYour task is only to help the user choose an accommodation option for booking and answer any question about it, any other tasks must not be handled by you. The user has already selected the business, now you help him choose an accommodation \\ \noption within the business i.e: A room within the business.\n\n\nFollow these steps before responding to the user:\n\nStep 1: If you have not shown the user a summary of the available options, show the summary including a brief description, amenities and the price per night for each option and ask the user if he wants to book any of them.\nHere is the list of available options: {properties_info}\n\nStep 2: If the user makes any question about the options after showing the summary, answer it based on the available options information. \n\nStep 3: If the user asks if there are other options available, you respond that there are no more available for those dates.\n\nStep 4: If the user does not want any of the available options, you apologize and tell the user that you will notify him if you have something new available in the future.\n\nHere is the conversation: \n{chat_history}\n\nYou respond in a short, very conversational friendly style.\nresponse to th user: " ]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_house_selection_resolver.py
import unittest from unittest.mock import MagicMock from app.model import Message, StepData from app.task_resolver.step_resolvers import HouseSelectionResolver import openai from dotenv import load_dotenv, find_dotenv import os class TestHouseSelectionResolver(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] def test_run(self): # Arrange prev_step_data = StepData() prev_step_data.resolver_data = {'booking_information': {'check_in_date': '2023-06-29', 'check_out_date': '2023-07-01', 'num_guests': 2, 'num_nights': 2}} test_cases = [ # { # "messages": [ # Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene."), # Message("assistant", "¡Hola! Claro, para poder ayudarte necesito saber la fecha de salida. ¿Cuándo te gustaría dejar la casa? "), # Message("user", "El sábado."), # Message("assistant", "Actualmente tenemos dos propiedades disponibles para las fechas que solicitaste: 1. Cabaña 'Sol': Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos. Amenidades: Wi-Fi, estacionamiento privado, se admiten mascotas, barbacoa, piscina privada. Precio por noche: 250.0 USD 2. Cabaña 'Luna': Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos. Amenidades: Wi-Fi, estacionamiento privado, se admiten mascotas, barbacoa, piscina privada. Precio por noche: 120.0 USD ¿Te gustaría reservar alguna de estas propiedades?"), # Message("user", "Se puede hacer asado?"), # ], # "previous_setp_data": { # "GATHER_BOOKING_INFO": prev_step_data # }, # "expected_property_id": None # }, # { # "messages": [ # Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene."), # Message("assistant", "¡Hola! Claro, para poder ayudarte necesito saber la fecha de salida. ¿Cuándo te gustaría dejar la casa? "), # Message("user", "El sábado."), # Message("assistant", "Actualmente tenemos dos propiedades disponibles para las fechas que solicitaste: 1. Cabaña 'Sol': Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos. Amenidades: Wi-Fi, estacionamiento privado, se admiten mascotas, barbacoa, piscina privada. Precio por noche: 250.0 USD 2. Cabaña 'Luna': Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos. Amenidades: Wi-Fi, estacionamiento privado, se admiten mascotas, barbacoa, piscina privada. Precio por noche: 120.0 USD ¿Te gustaría reservar alguna de estas propiedades?"), # Message("user", "La primera"), # ], # "previous_setp_data": { # "GATHER_BOOKING_INFO": prev_step_data # }, # "expected_property_id": "Sol" # }, { "messages": [ Message("user", "Hola, necesito una casa para 2 personas por el fin de semana"), Message("assistant", "¡Hola! Tenemos dos opciones disponibles para el fin de semana: la Cabaña \"Sol\" y la Cabaña \"Luna\". Ambas tienen capacidad para más personas, pero también pueden ser reservadas para dos personas. ¿Te gustaría saber más sobre ellas?"), Message("user", "Si") ], "previous_setp_data": { "GATHER_BOOKING_INFO": prev_step_data }, "expected_property_id": None }, { "messages": [ Message("user", "Hola, necesito una casa para 2 personas por el fin de semana"), Message("assistant", "¡Hola! Tenemos dos opciones disponibles para el fin de semana: la Cabaña \"Sol\" y la Cabaña \"Luna\". Ambas tienen capacidad para más personas, pero también pueden ser reservadas para dos personas. ¿Te gustaría saber más sobre ellas?"), Message("user", "Quiero reservar la primera") ], "previous_setp_data": { "GATHER_BOOKING_INFO": prev_step_data }, "expected_property_id": "Sol" }, ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") resolver = HouseSelectionResolver() resolver.data["step_first_execution"] = False # Act result = resolver.run(test["messages"], test["previous_setp_data"]) # Assert self.assertIsNotNone(result) if test["expected_property_id"] is None: self.assertTrue("property_picked_info" not in resolver.data or ("property_picked_info" in resolver.data and "property_id" not in resolver.data["property_picked_info"])) else: self.assertEqual(resolver.data["property_picked_info"]["property_id"], test["expected_property_id"]) if __name__ == '__main__': unittest.main()
[ "Si", "Hola, necesito una casa para 2 personas por el fin de semana", "assistant", "user", "Quiero reservar la primera", "¡Hola! Tenemos dos opciones disponibles para el fin de semana: la Cabaña \"Sol\" y la Cabaña \"Luna\". Ambas tienen capacidad para más personas, pero también pueden ser reservadas para dos personas. ¿Te gustaría saber más sobre ellas?" ]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_list_business.py
import unittest from unittest.mock import MagicMock from app.backend.domain.entities.business import BusinessOwner, LoadBusinesses, Business, Location, PaymentOption, Property from app.model import Message from app.backend.domain.usecases.list_business import ListBusinessUseCase from app.backend.infraestructure.repositories import BusinessRepository import openai from dotenv import load_dotenv, find_dotenv from unittest.mock import MagicMock import json import os class TestListBusinessUseCase(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] def test_execute(self): # Arrange test_cases = [ # { # "load_business": LoadBusinesses(bnbot_id="@complejo.enrique.joaquin", location="Mercedes"), # "expected_resolver_done": False # }, { "load_business": LoadBusinesses(bnbot_id='', location='Mercedes', business_name='complejo Joaquin', business_owner=''), "expected_resolver_done": False } ] # fake_businesses = [] # # Open the file and load its contents as JSON # with open("data/businesses.json", 'r') as file: # fake_businesses = json.load(file) # businesses = [] # for fb in fake_businesses: # business = Business(**fb) # businesses.append(business) businesses = [ Business(business_id='f755f3d3-9261-46be-9a7c-fdb7d93779ec', business_name='Rio Claro Campgrounds', description='An eco-friendly campsite along the Rio Claro river, ideal for nature lovers.', bnbot_id='@rio_claro_campgrounds', bnbot_configuration={'booking_deposit': 0.1, 'pre_book_time': 48.0}, location=Location(latitude=37.7749, longitude=-122.4194, address='Ruta 2 km 284', city='Mercedes', state='Soriano', country='Uruguay', postal_code='75000'), business_owners=[BusinessOwner(name='Miguel Alvarez', phone_number='+59891231234', email='[email protected]')], payment_options=[PaymentOption(payment_method='MOBILE_PAYMENT', instructions='Use the QR Code at the entrance for mobile payments.')], how_to_arrive_instructions='Head west on Ruta 2. Take the exit at km 284 and follow the gravel road to Rio Claro Campgrounds.', properties=[Property(property_id='CampSite1', name='Camp Site 1', other_calendar_links=['https://booking.com/camp-site-1/calendar'], description='Secluded campsite near the river with a fire pit and picnic table.', amenities=['Fire Pit', 'Picnic Table', 'Portable Restroom'], price_per_night=20.0, currency='USD', max_guests=4, pick_up_keys_instructions='No keys required. The campsite is open. Please check-in at the reception upon arrival.')]), Business(business_id='f747b3a1-0408-4e3f-a9da-6333d42eadc2', business_name='Cielo Azul Resort', description='A luxury resort with a magnificent view of the mountains and countryside.', bnbot_id='@cielo_azul_resort', bnbot_configuration={'booking_deposit': 0.25, 'pre_book_time': 24.0}, location=Location(latitude=37.7749, longitude=-122.4194, address='Ruta 2 km 284', city='Mercedes', state='Soriano', country='Uruguay', postal_code='75000'), business_owners=[BusinessOwner(name='Elena Garcia', phone_number='+59892345678', email='[email protected]')], payment_options=[PaymentOption(payment_method='CASH', instructions='Cash payments are accepted at the reception desk.')], how_to_arrive_instructions='Take the Ruta 2 highway and exit at km 284. Follow the signs for Cielo Azul Resort.', properties=[Property(property_id='VillaBella', name='Villa Bella', other_calendar_links=['https://booking.com/villa-bella/calendar'], description='Experience luxury in this exquisite villa with an infinity pool and a panoramic view of the mountains.', amenities=['Wi-Fi', 'Private Parking', 'Infinity Pool', 'Fitness Center'], price_per_night=500.0, currency='USD', max_guests=6, pick_up_keys_instructions='Visit the concierge at the main lobby to collect your keys.')]), Business(business_id='ab0544c5-3029-4884-9354-d4da090c76d8', business_name='Complejo Enrique Joaquin', description='Complejo de campo muy bueno', bnbot_id='@complejo_enrique_joaquin', bnbot_configuration={'booking_deposit': 0.1, 'pre_book_time': 60.0}, location=Location(latitude=37.7749, longitude=-122.4194, address='Ruta 2 km 284', city='Mercedes', state='Soriano', country='Uruguay', postal_code='75000'), business_owners=[BusinessOwner(name='Gonzalo Sarasua', phone_number='+59899513718', email='[email protected]')], payment_options=[PaymentOption(payment_method=None, instructions='Para realizar el deposito etc.')], how_to_arrive_instructions='El complejo queda en ruta 2 km 287, cerca del pejae. Yendo para Fray Bentos desde Mercedes a mano izquierda. Aqui esta la ubicacion en google maps: https://goo.gl/maps/R8gQZDHVXr2tiPQA8', properties=[Property(property_id='Sol', name='Cabaña "Sol"', other_calendar_links=['https://admin.booking.com/hotel/hoteladmin/ical.html?t=b20bbde6-86d6-4c7c-81d8-72c14ed4788c'], description='Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos.', amenities=['Wi-Fi', 'private parking', 'pet-friendly', 'barbecue', 'private pool'], price_per_night=250.0, currency='USD', max_guests=8, pick_up_keys_instructions='Las llaves se encuentran en un box en la puerta de entrada. La clave para abrir el box es 12345.'), Property(property_id='Luna', name='Cabaña "Luna"', other_calendar_links=['https://admin.booking.com/hotel/hoteladmin/ical.html?t=8e422a9c-2ae6-4f83-8b28-18776ffcecfc'], description='Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos.', amenities=['Wi-Fi', 'private parking', 'pet-friendly', 'barbecue', 'private pool'], price_per_night=120.0, currency='USD', max_guests=4, pick_up_keys_instructions='Las llaves se encuentran en un box en la puerta de entrada. La clave para abrir el box es 12345.')]), Business(business_id='31ed8b67-1a7d-4895-bdcb-da4b0fc21f8e', business_name='La Estancia Verde', description='An exquisite countryside retreat offering a serene and natural atmosphere.', bnbot_id='@la_estancia_verde', bnbot_configuration={'booking_deposit': 0.15, 'pre_book_time': 72.0}, location=Location(latitude=37.7749, longitude=-122.4194, address='Ruta 2 km 284', city='Mercedes', state='Soriano', country='Uruguay', postal_code='75000'), business_owners=[BusinessOwner(name='Julia Rodriguez', phone_number='+59891234567', email='[email protected]')], payment_options=[PaymentOption(payment_method='PAYPAL', instructions='Please use PayPal for transactions.')], how_to_arrive_instructions='Follow Ruta 2 until km 284. The retreat is located near the old mill.', properties=[Property(property_id='CasaGrande', name='Casa Grande', other_calendar_links=['https://booking.com/casa-grande/calendar'], description='A spacious villa with rustic interiors, private garden, and an outdoor pool.', amenities=['Wi-Fi', 'Private Parking', 'Pet-friendly', 'Outdoor Pool'], price_per_night=300.0, currency='USD', max_guests=10, pick_up_keys_instructions='Pick up the keys at the reception desk upon arrival.')]), Business(business_id='5e4c9d55-2af0-4875-ac86-d0232b0a8813', business_name='Campo del Sol', description='A family-friendly campsite and lodge in the heart of nature.', bnbot_id='@campo_del_sol', bnbot_configuration={'booking_deposit': 0.2, 'pre_book_time': 48.0}, location=Location(latitude=37.7749, longitude=-122.4194, address='Ruta 2 km 284', city='Mercedes', state='Soriano', country='Uruguay', postal_code='75000'), business_owners=[BusinessOwner(name='Carlos Mendez', phone_number='+59898765432', email='[email protected]')], payment_options=[PaymentOption(payment_method='CREDIT_CARD', instructions='We accept all major credit cards.')], how_to_arrive_instructions='Take exit 21 from Ruta 2, and follow the signs for Campo del Sol.', properties=[Property(property_id='LodgeOne', name='Lodge One', other_calendar_links=['https://booking.com/lodge-one/calendar'], description='A cozy lodge with a fireplace, perfect for couples or small families.', amenities=['Wi-Fi', 'Private Parking', 'Pet-friendly', 'Fireplace'], price_per_night=150.0, currency='USD', max_guests=4, pick_up_keys_instructions='The keys will be under the doormat. Please lock the door and return them under the doormat when checking out.')]) ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") mock_repository = MagicMock() mock_repository.list_all_businesses.return_value = businesses resolver = ListBusinessUseCase(mock_repository) # Act result = resolver.execute(test["load_business"]) # Assert self.assertIsNotNone(result) # self.assertEqual(test["expected_resolver_done"], resolver.is_done()) if __name__ == '__main__': unittest.main()
[]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~make_reservation~search_data_extractor.py
from typing import List from app.model import Message import openai import os import json from app.utils import logger, remove_spanish_special_characters from datetime import datetime, timedelta, date openai.api_key = os.environ.get('OPENAI_API_KEY') def get_current_datetime(): return datetime.now() def calculate_date_based_on_day_name(date_from: datetime, day_name: str) -> datetime: """Useful for when you need to calculate a exact date in format YYYY-MM-DD given a day name i.e: Tuesday.""" def _day_name_to_int(day_string) -> int: # Convert the day string to lowercase for case-insensitive comparison day_string_lower = remove_spanish_special_characters(day_string.lower()) # Map day strings to day numbers day_mapping = { 'lunes': 0, 'martes': 1, 'miercoles': 2, 'jueves': 3, 'viernes': 4, 'sabado': 5, 'domingo': 6, 'monday': 0, 'tuesday': 1, 'wednesday': 2, 'thursday': 3, 'friday': 4, 'saturday': 5, 'sunday': 6 } # Check if the day string exists in the mapping if day_string_lower in day_mapping: return day_mapping[day_string_lower] else: raise ValueError('Invalid day string') day_int = _day_name_to_int(day_name) # Get the current date current_date = date_from.date() # Get the next occurrence of the specified day name days_ahead = (7 + day_int - current_date.weekday()) % 7 next_date = current_date + timedelta(days=days_ahead) return next_date json_fn = { "name": "calculate_booking_info", "description": "Calculate the exact check-in and check-out dates for a reservation and the number of guests staying.", "parameters": { "type": "object", "properties": { "check_in_date": { "type": "string", "description": "If present in the conversation, the Check In date in the format: YYYY-MM-DD i.e: 2023-03-25" }, "check_out_date": { "type": "string", "description": "If present in the conversation, the Check Out date in the format: YYYY-MM-DD i.e: 2023-03-25" }, "check_in_dow": { "type": "string", "description": "If present in the conversation, the Check In day of the week.", "enum": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] }, "check_out_dow": { "type": "string", "description": "If present in the conversation, the Check Out day of the week.", "enum": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] }, "num_nights": { "type": "string", "description": "If present in the conversation, the number of nights the guests plan to stay" }, "num_guests": { "type": "string", "description": "If present in the conversation, the number of guests staying" } }, "required": [] } } class SearchDataExtractor: def calculate_check_in_date(self, check_in_date, check_in_dow): if check_in_date is not None: if check_in_date > get_current_datetime().strftime("%Y-%m-%d"): return datetime.strptime(check_in_date, "%Y-%m-%d") else: logger.debug(f"check_in_date is from the passt: {check_in_date}") if check_in_dow is not None: return calculate_date_based_on_day_name(get_current_datetime(), check_in_dow) logger.debug(f"""For some reason it is not possible to calculate the check_in date, check_in_date: {check_in_date}, check_in_dow: {check_in_dow}""") return None def calculate_check_out_date(self, check_in_date: str, check_out_date: str, check_out_dow: str, num_nights: int): if check_out_date is not None: if check_out_date > get_current_datetime().strftime("%Y-%m-%d") and check_out_date > check_in_date: return datetime.strptime(check_out_date, "%Y-%m-%d") else: logger.debug(f"""check_out_date is wrong. check_out_date: {check_out_date}, check_in_date: {check_in_date}""") date_from = datetime.strptime(check_in_date, "%Y-%m-%d") if int(num_nights) > 0: return date_from + timedelta(days=num_nights) if check_out_dow is not None: return calculate_date_based_on_day_name(date_from, check_out_dow) logger.debug(f"""For some reason it is not possible to calculate the check_out date, check_in_date: {check_in_date}, check_out_date: {check_out_date}, check_out_dow: {check_out_dow}, num_nights: {num_nights}""") return None def get_num_days(self, start_date, end_date): start_date = date.fromisoformat(start_date) end_date = date.fromisoformat(end_date) num_days = (end_date - start_date).days return num_days def calculate_booking_info(self, fn_params: dict): check_in_date = fn_params.get("check_in_date", None) check_in_dow = fn_params.get("check_in_dow", None) check_out_date = fn_params.get("check_out_date", None) check_out_dow = fn_params.get("check_out_dow", None) num_nights = int(fn_params.get("num_nights", 0)) num_guests = int(fn_params.get("num_guests", 0)) if check_in_date is None and check_in_dow is None: logger.debug("Not possible to calculate check_in date") return None if check_out_date is None and check_out_dow is None and num_nights is None: logger.debug("Not possible to calculate check_out date") return None check_in_date = self.calculate_check_in_date(check_in_date, check_in_dow) if check_in_date is not None: check_in_date = check_in_date.strftime("%Y-%m-%d") check_out_date = self.calculate_check_out_date(check_in_date, check_out_date, check_out_dow, num_nights) if check_out_date is not None: check_out_date = check_out_date.strftime("%Y-%m-%d") num_nights = None if check_out_date is not None and check_in_date is not None: num_nights = self.get_num_days(check_in_date, check_out_date) return { "check_in_date": check_in_date, "check_out_date": check_out_date, "num_guests": num_guests, "num_nights": num_nights } def normalize_dow(self, params): map_days = { "lunes" } def run(self, messages: List[Message]): messages_input = [{"role": "system", "content": f"What are the exact check-in and check-out dates and number of guests for the reservation? IMPORTANT: Today is: {datetime.now().date().strftime('%Y-%m-%d')}"}] for msg in messages: messages_input.append({"role": msg.role, "content": msg.text}) # messages_input.append("role") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages_input, functions=[json_fn], temperature=0., max_tokens=500, ) if "function_call" in response.choices[0].message and "arguments" in response.choices[0].message["function_call"]: fn_parameters = json.loads(response.choices[0].message["function_call"]["arguments"]) # fn_parameters["user_has_selected"] = ("bnbot_id" in fn_parameters and fn_parameters["bnbot_id"] != "") logger.debug(f"calculate_booking_info fn_parameters {fn_parameters}") return self.calculate_booking_info(fn_parameters) return None
[]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~common~post_process_router_resolver.py
from app.task_resolver.engine import StepResolver from typing import List, Any from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate # from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose, logger from langchain.llms import OpenAI from app.tools import NextStepExtractor from app.model import Message from langchain.output_parsers import StructuredOutputParser, ResponseSchema template="""Given a conversation between a user and an assistant about booking a house for short-term stay. \ Your job is to decide which is the next step to take. Here are the steps for you to choose from: {steps} Current conversation: {chat_history} {format_instructions}""" response_schemas = [ ResponseSchema(name="step", description="The name of the next step to take.") ] class PostProcessRouterChain: def __init__(self): llm = OpenAI(temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions =self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["chat_history", "steps"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="result") def run(self, chat_history: str, steps: str): info = self.chain({"chat_history": chat_history, "steps": steps}) return self.output_parser.parse(info["result"]) class PostProcessRouterResolver(StepResolver): def __init__(self, steps): self.steps = steps self.next_step_extractor = NextStepExtractor() def run(self, messages: List[Message], previous_steps_data: dict=None, step_chat_history: List[Message] = None) -> Message: result = self.next_step_extractor.run_select_next_step(messages, self.steps) return Message.route_message("Routing to previous Step", result["step_id"]) def is_done(self): return True
[ "Given a conversation between a user and an assistant about booking a house for short-term stay. Your job is to decide which is the next step to take.\n\nHere are the steps for you to choose from:\n{steps}\n\nCurrent conversation: \n{chat_history}\n\n{format_instructions}", "chat_history", "format_instructions" ]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~task_router~exit_task_resolver.py
from app.task_resolver.engine import StepResolver from typing import List, Any from app.task_resolver.engine import StepResolver from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from app.model import Message from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose from langchain.llms import OpenAI # BOOKING_CONFIRMATION_STEP: # HOUSE_SELECTION_STEP: template="""Given a conversation between a user and an assistant about booking a house for short-term stay. \ Your job is to decide if the conversation came to an end already. A conversation came to an end in the following cases: 1. After the user gets a confirmation from the assistant that the reservation in booked for some time and that an email will be sent to the email provided by her. 2. When the user decides not to book a reservation after the assistant asked to confirm the booking. 3. When there are no properties available for the user's booking requirements and the user does not want to pick other dates for the reservation. 4. When the user is making a reservation but suddenly wants to perform some other task not related with making reservations. 5. When the user explicitly ask to end the conversation. On every other case the conversation is still active. {format_instructions} Current conversation: {chat_history}""" response_schemas = [ ResponseSchema(name="conversation_finished", type="bool", description="true if the conversation between the user and the assistant came to an end, otherwise false."), ResponseSchema(name="text", description="Response to the user."), ] class ExitTaskChain: def __init__(self): llm = OpenAI(model_name="text-davinci-003", temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["chat_history"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="result") def __call__(self, chat_history, current_task): info = self.chain({"chat_history": chat_history}) return self.output_parser.parse(info["result"]) class ExitTaskResolver(StepResolver): exit_task_chain: ExitTaskChain = ExitTaskChain() def run(self, messages: List[Message], previous_steps_data: dict, step_chat_history: List[Message] = None): chat_history = self.build_chat_history(messages) # current_task = step_data["current_task_name"] exit_result = self.exit_task_chain(chat_history, "") self.data["conversation_finished"] = exit_result["conversation_finished"] if ("conversation_finished" in exit_result and exit_result["conversation_finished"] != "" and exit_result["conversation_finished"] == True): return None def is_done(self): # Force to execute this step every time. return ( "conversation_finished" in self.data and self.data["conversation_finished"] != "" and self.data["conversation_finished"] is not None )
[ "chat_history", "Given a conversation between a user and an assistant about booking a house for short-term stay. Your job is to decide if the conversation came to an end already.\n\nA conversation came to an end in the following cases:\n1. After the user gets a confirmation from the assistant that the reservation in booked for some time and that an email will be sent to the email provided by her.\n2. When the user decides not to book a reservation after the assistant asked to confirm the booking.\n3. When there are no properties available for the user's booking requirements and the user does not want to pick other dates for the reservation.\n4. When the user is making a reservation but suddenly wants to perform some other task not related with making reservations.\n5. When the user explicitly ask to end the conversation.\n\nOn every other case the conversation is still active.\n\n{format_instructions}\n\nCurrent conversation: \n{chat_history}", "format_instructions" ]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_booking_confirmation_resolver.py
import unittest from unittest.mock import MagicMock from app.model import Message, StepData from app.task_resolver.step_resolvers import BookingConfirmationResolver import os import openai from dotenv import load_dotenv, find_dotenv class TestBookingConfirmationResolver(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] def test_run(self): # Arrange def _create_step_data(info) -> StepData: step_data = StepData() step_data.resolver_data =info return step_data test_cases = [ { "messages": [ Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene"), Message("assistant", "¡Hola! Claro, para poder ayudarte necesito saber la fecha de salida. ¿Cuándo te gustaría dejar la casa?"), Message("user", "Sería hasta el lunes"), Message("assistant", "Perfecto, tenemos dos opciones disponibles para esas fechas. La primera es la Cabaña \"Sol\" que tiene capacidad para 8 personas y un precio de 250 USD por noche. La segunda opción es la Cabaña \"Luna\" que tiene capacidad para 4 personas y un precio de 120 USD por noche. ¿Te gustaría saber más sobre alguna de estas opciones o prefieres reservar directamente alguna de ellas?"), Message("user", "Quiero reservar las más barata que acepte mascotas"), Message("assistant", "¡Genial! La Cabaña \"Luna\" es pet-friendly y tiene un precio de 120 USD por noche. ¿Te gustaría reservarla?"), Message("user", "Si, perfect"), Message("assistant", "¡Genial! La Cabaña 'Luna' es pet-friendly y tiene un precio de 120 USD por noche. ¿Podrías proporcionarme tu nombre y correo electrónico para proceder con la reserva?"), Message("user", "[email protected], Agustin"), ], "previous_setp_data": { "GATHER_BOOKING_INFO": _create_step_data({"booking_information":{'check_in_date': '2023-06-29', 'check_out_date': '2023-07-01', 'num_guests': 2, 'num_nights': 2}}), "HOUSE_SELECTION": _create_step_data({'step_first_execution': False, 'properties_available': {'Sol': {'property_id': 'Sol', 'name': 'Cabaña "Sol"', 'description': 'Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos.', 'amenities': ['Wi-Fi', 'private parking', 'pet-friendly', 'barbecue', 'private pool'], 'price': '250.0', 'currency': 'USD', 'max_guests': '8', 'how_to_arrive_instructions': 'El complejo queda en ruta 2 km 287, cerca del pejae. Yendo para Fray Bentos desde Mercedes a mano izquierda. Aqui esta la ubicacion en google maps: https://goo.gl/maps/R8gQZDHVXr2tiPQA8', 'pick_up_keys_instructions': 'Las llaves se encuentran en un box en la puerta de entrada. La clave para abrir el box es 12345.'}, 'Luna': {'property_id': 'Luna', 'name': 'Cabaña "Luna"', 'description': 'Impresionante villa con vistas panorámicas a las montañas. Esta lujosa propiedad ofrece un ambiente tranquilo y relajante con amplios espacios interiores y exteriores. Cuenta con una piscina privada, jardines exuberantes y una terraza para disfrutar de las maravillosas vistas. Perfecta para escapadas en familia o con amigos.', 'amenities': ['Wi-Fi', 'private parking', 'pet-friendly', 'barbecue', 'private pool'], 'price': '120', 'currency': 'USD', 'max_guests': '4', 'how_to_arrive_instructions': 'El complejo queda en ruta 2 km 287, cerca del pejae. Yendo para Fray Bentos desde Mercedes a mano izquierda. Aqui esta la ubicacion en google maps: https://goo.gl/maps/R8gQZDHVXr2tiPQA8', 'pick_up_keys_instructions': 'Las llaves se encuentran en un box en la puerta de entrada. La clave para abrir el box es 12345.'}}, 'property_picked_info': {'property_id': 'Luna', 'price_per_night': 'USD 120.0', 'total_price': 'USD 480.0'}}), "GATHER_USER_INFO": _create_step_data({'step_first_execution': True, 'user_information': {'user_name': '', 'email': 'Please provide your name and email to proceed with the booking.', 'text': "¡Genial! La Cabaña 'Luna' es pet-friendly y tiene un precio de 120 USD por noche. ¿Podrías proporcionarme tu nombre y correo electrónico para proceder con la reserva?"}}) }, "step_chat_history": [ Message("user", "[email protected], Agustin") ], "expected_resolver_done": False } ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") resolver = BookingConfirmationResolver() # Act result = resolver.run(test["messages"], test["previous_setp_data"], test["step_chat_history"]) # Assert self.assertIsNotNone(result) self.assertEqual(test["expected_resolver_done"], resolver.is_done()) if __name__ == '__main__': unittest.main()
[ "[email protected], Agustin", "¡Genial! La Cabaña \"Luna\" es pet-friendly y tiene un precio de 120 USD por noche. ¿Te gustaría reservarla?", "Si, perfect", "¡Genial! La Cabaña 'Luna' es pet-friendly y tiene un precio de 120 USD por noche. ¿Podrías proporcionarme tu nombre y correo electrónico para proceder con la reserva?", "¡Hola! Claro, para poder ayudarte necesito saber la fecha de salida. ¿Cuándo te gustaría dejar la casa?", "Me gustaría reservar una casa para dos personas, para el jueves que viene", "assistant", "Perfecto, tenemos dos opciones disponibles para esas fechas. La primera es la Cabaña \"Sol\" que tiene capacidad para 8 personas y un precio de 250 USD por noche. La segunda opción es la Cabaña \"Luna\" que tiene capacidad para 4 personas y un precio de 120 USD por noche. ¿Te gustaría saber más sobre alguna de estas opciones o prefieres reservar directamente alguna de ellas?", "user", "Sería hasta el lunes", "Quiero reservar las más barata que acepte mascotas" ]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_business_selection_resolver.py
import unittest from unittest.mock import MagicMock from app.model import Message from app.task_resolver.engine import StepData from app.task_resolver.step_resolvers import BusinessSelectionResolver import json import openai from dotenv import load_dotenv, find_dotenv from unittest.mock import patch import os def mock_list_businesses_side_effect(*args, **kwargs): # Extracting input arguments load_businesses = json.loads(args[0]) fake_businesses = [] # Open the file and load its contents as JSON with open("data/businesses.json", 'r') as file: fake_businesses = json.load(file) if load_businesses.get("bnbot_id", None) is not None: for fb in fake_businesses: if load_businesses.get("bnbot_id", "") == fb["bnbot_id"]: return [fb] return [] else: return fake_businesses class TestBusinessSelectionResolver(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] @patch('app.integrations.BackendAPIClient.list_businesses') def test_run(self, mock_list_businesses): mock_list_businesses.side_effect = mock_list_businesses_side_effect # Arrange prev_step_data = StepData() prev_step_data.resolver_data = {'step_first_execution': False, 'business_info': {'location': 'Mercedes', 'business_name': 'Enrique Joaquín', 'have_enough_info': True}} test_cases = [ { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa"), Message("assistant", "¡Claro! Estoy aquí para ayudarte a encontrar una casa para reservar. ¿Tienes algún ID de negocio en mente o necesitas ayuda para encontrar uno?"), Message("user", "No tengo el ID, pero es el complejo Enrique Joaquín"), Message("assistant", "Perfecto, ¿en qué ubicación te gustaría encontrar el complejo Enrique Joaquín? "), Message("user", "En Mercedes"), ], "previous_setp_data": { "GATHER_BUSINESS_INFO": prev_step_data }, "expected_user_has_selected": False, "expected_business_id": None, "expected_bnbot_id": None }, { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa"), Message("assistant", "¡Claro! Estoy aquí para ayudarte a encontrar una casa para reservar. ¿Tienes algún ID de negocio en mente o necesitas ayuda para encontrar uno?"), Message("user", "No tengo el ID, pero es el complejo Enrique Joaquín"), Message("assistant", "Perfecto, ¿en qué ubicación te gustaría encontrar el complejo Enrique Joaquín? "), Message("user", "En Mercedes"), Message("assistant", '¡Genial! He encontrado el complejo Enrique Joaquín en Mercedes, Soriano, Uruguay. ¿Es este el negocio que estás buscando?\n\n- Nombre del negocio: Complejo Enrique Joaquín\n- Dirección: Ruta 2 km 284, Mercedes, Soriano, Uruguay\n- Código postal: 75000\n\n¿Es este el negocio que estás buscando?'), Message("user", "Si"), ], "previous_setp_data": { "GATHER_BUSINESS_INFO": prev_step_data }, "expected_user_has_selected": True, "expected_business_id": "complejo_enrique_joaquin_id", "expected_bnbot_id": "@complejo.enrique.joaquin" } ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") resolver = BusinessSelectionResolver("http://test") resolver.data["step_first_execution"] = False # Act result = resolver.run(test["messages"], test["previous_setp_data"]) # Assert self.assertIsNotNone(result) if "business_info" in resolver.data: self.assertEqual(resolver.data["business_info"]["user_has_selected"], test["expected_user_has_selected"]) if resolver.data["business_info"]["user_has_selected"]: self.assertEqual(resolver.data["business_info"]["business_id"], test["expected_business_id"]) self.assertEqual(resolver.data["business_info"]["bnbot_id"], test["expected_bnbot_id"]) else: self.assertEqual(resolver.is_done(), False) if __name__ == '__main__': unittest.main()
[ "Hola, ¿en qué puedo ayudarte?", "Perfecto, ¿en qué ubicación te gustaría encontrar el complejo Enrique Joaquín? ", "En Mercedes", "No tengo el ID, pero es el complejo Enrique Joaquín", "Si", "assistant", "user", "Hola", "¡Genial! He encontrado el complejo Enrique Joaquín en Mercedes, Soriano, Uruguay. ¿Es este el negocio que estás buscando?\n\n- Nombre del negocio: Complejo Enrique Joaquín\n- Dirección: Ruta 2 km 284, Mercedes, Soriano, Uruguay\n- Código postal: 75000\n\n¿Es este el negocio que estás buscando?", "Me gustaría reservar una casa", "¡Claro! Estoy aquí para ayudarte a encontrar una casa para reservar. ¿Tienes algún ID de negocio en mente o necesitas ayuda para encontrar uno?" ]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~make_reservation~exit_task_resolver.py
from app.task_resolver.engine import StepResolver from typing import List, Any from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.chat_models import ChatOpenAI from app.utils import chain_verbose from langchain.llms import OpenAI from app.model import Message # BOOKING_CONFIRMATION_STEP: # HOUSE_SELECTION_STEP: template="""Given a conversation between a user and an assistant about booking a house for short-term stay. \ Your job is to decide if the conversation came to an end already. A conversation came to an end in the following cases: 1. After the user gets a confirmation from the assistant that the reservation in booked for some time and that an email will be sent to the email provided by her. 2. When the user decides not to book a reservation after the assistant asked to confirm the booking. 3. When there are no properties available for the user's booking requirements and the user does not want to pick other dates for the reservation. 4. When the user is making a reservation but suddenly wants to perform some other task not related with making reservations. 5. When the user explicitly ask to end the conversation. On every other case the conversation is still active. {format_instructions} Current conversation: {chat_history}""" response_schemas = [ ResponseSchema(name="conversation_finished", type="bool", description="true if the conversation between the user and the assistant came to an end, otherwise false."), ResponseSchema(name="text", description="Response to the user."), ] class ExitTaskChain: def __init__(self): llm = OpenAI(model_name="text-davinci-003", temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["chat_history"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="result") def __call__(self, chat_history, current_task): info = self.chain({"chat_history": chat_history}) return self.output_parser.parse(info["result"]) class ExitTaskResolver(StepResolver): exit_task_chain: ExitTaskChain = ExitTaskChain() def run(self, messages: List[Message], previous_steps_data: dict, step_chat_history: List[Message] = None) -> Message: chat_history = self.build_chat_history(messages) # current_task = step_data["current_task_name"] exit_result = self.exit_task_chain(chat_history, "") self.data["conversation_finished"] = exit_result["conversation_finished"] if ("conversation_finished" in exit_result and exit_result["conversation_finished"] != "" and exit_result["conversation_finished"] == True): return None def is_done(self): # Force to execute this step every time. return ( "conversation_finished" in self.data and self.data["conversation_finished"] != "" and self.data["conversation_finished"] is not None )
[ "chat_history", "Given a conversation between a user and an assistant about booking a house for short-term stay. Your job is to decide if the conversation came to an end already.\n\nA conversation came to an end in the following cases:\n1. After the user gets a confirmation from the assistant that the reservation in booked for some time and that an email will be sent to the email provided by her.\n2. When the user decides not to book a reservation after the assistant asked to confirm the booking.\n3. When there are no properties available for the user's booking requirements and the user does not want to pick other dates for the reservation.\n4. When the user is making a reservation but suddenly wants to perform some other task not related with making reservations.\n5. When the user explicitly ask to end the conversation.\n\nOn every other case the conversation is still active.\n\n{format_instructions}\n\nCurrent conversation: \n{chat_history}", "format_instructions" ]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~make_reservation~user_info_extractor_tool.py
from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from app.utils import chain_verbose from langchain.chat_models import ChatOpenAI # Follow these Steps before responding to the user new message: # Step 1: Make sure the user provided user name and email. # Step 2: If the user provided with this information, you thank him. template =""" You are an Assistant that gathers information from the user to book an accommodation. You respond allways in Spanish. The only information you need is the email and the name of the person doing the reservation. Follow these Steps before responding: Step 1: Make sure the user gives yout the name and email for booking the accommodation. Step 2: If the user have not provided you with this information \ then tell the user that you need them in order to place the booking. Here is the conversation: {chat_history} {format_instructions} You respond in a short, very conversational friendly style. REMEMBER: Only asked for the information needed, nothing else.""" response_schemas = [ ResponseSchema(name="user_name", description="The name of the user booking the house. If not provided set empty string"), ResponseSchema(name="email", description="The email of the user booking the house. If not provided set empty string"), ResponseSchema(name="text", description="The response to the user"), ] class UserInformationExtractorChain: def __init__(self): llm = ChatOpenAI(temperature=0.) self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions =self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=["chat_history"], partial_variables={"format_instructions": format_instructions}, template=template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=chain_verbose, output_key="user_info") def __call__(self, chat_history): info = self.chain({"chat_history": chat_history}) return self.output_parser.parse(info["user_info"])
[ "chat_history", "format_instructions", "\nYou are an Assistant that gathers information from the user to book an accommodation. \nYou respond allways in Spanish.\nThe only information you need is the email and the name of the person doing the reservation.\n\nFollow these Steps before responding:\n\nStep 1: Make sure the user gives yout the name and email for booking the accommodation.\n\nStep 2: If the user have not provided you with this information \\ \nthen tell the user that you need them in order to place the booking.\n\nHere is the conversation: \n{chat_history}\n\n{format_instructions}\n\nYou respond in a short, very conversational friendly style.\n\nREMEMBER: Only asked for the information needed, nothing else." ]
2024-01-10
agustin-sarasua/bnbot-core
app~task_resolver~step_resolvers~make_reservation~gather_booking_info_resolver.py
from app.task_resolver.engine import StepResolver, StepData from app.utils import logger from app.utils import get_completion_from_messages from app.tools import SearchDataExtractor from typing import List, Any from datetime import datetime, timedelta from app.integrations import OpenAIClient from app.model import Message #The only information you need is: check-in date, check-out date and number of guests staying. system_message =f""" You are an Assistant that gathers information from the user about booking an accommodation. You respond allways in Spanish. The only information you need is: check-in date, check-out date and number of guests staying. Follow these Steps before responding to the user new message: Step 1: Make sure the user provided the check-in date. Step 2: Make sure the user has provided either the check-out date or the number of nights they are staying. Step 3: Make sure the user has provided the number of guests that are staying. You respond in a short, very conversational friendly style. REMEMBER: Only asked for the information needed, nothing else.""" class GatherBookingInfoResolver(StepResolver): def _calculate_checkout_date(self, checkin_date, num_nights): checkin_datetime = datetime.strptime(checkin_date, '%Y-%m-%d') checkout_datetime = checkin_datetime + timedelta(days=num_nights) checkout_date = checkout_datetime.strftime('%Y-%m-%d') return checkout_date def run(self, messages: List[Message], previous_steps_data: dict, step_chat_history: List[Message] = None) -> Message: # exit_task_step_data: StepData = previous_steps_data["EXIT_TASK_STEP"] # if exit_task_step_data.resolver_data["conversation_finished"] == True: # logger.debug("Conversation finished. Responding None") # return None # chat_history = self.build_chat_history(messages) search_data_extractor = SearchDataExtractor() chat_input = OpenAIClient.build_messages_from_conversation(system_message, messages) assistant_response = get_completion_from_messages(chat_input) booking_info = search_data_extractor.run(messages) if booking_info is not None: checkin_date = booking_info.get("check_in_date", None) checkout_date = booking_info.get("check_out_date", None) # num_nights = booking_info["num_nights"] num_guests = booking_info.get("num_guests", None) if checkin_date is not None and checkout_date is not None and num_guests > 0: self.data["booking_information"] = booking_info return Message.assistant_message(assistant_response) def is_done(self): if "booking_information" not in self.data: return False booking_information = self.data["booking_information"] return (booking_information["check_in_date"] is not None and booking_information["check_out_date"] is not None and booking_information["num_guests"] > 0)
[]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_utils.py
from langchain.chains.llm import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.chat_models import ChatOpenAI from typing import List # Follow these Steps before responding to the user new message: # Step 1: Make sure the user provided user name and email. # Step 2: If the user provided with this information, you thank him. template = """{context} Current conversation: {chat_history} You respond in a short, very conversational friendly style. response to th assistant:""" chain_of_though_template = """You are customer that wants to book an accommodation for the weekend \ in the city of "Mercedes" at the "Complejo Enrique Joaquin". \ Allways answer in Spanish. You ask your requirements one at a time. These are your requirements for the accommodation: {context} Follow these Steps before responding to the user new message: {chain_of_though} Current conversation: {chat_history} You respond in a short, very conversational friendly style. response to th assistant: """ response_schemas = [ ResponseSchema(name="text", description="The response to the user"), ] class FakeCustomerChain: chain_of_though_steps: List[str] = None context: str ="" def __init__(self, chain_of_though_steps: List = None, context: str =""): llm = ChatOpenAI(temperature=0.) self.context = context used_template = template self.chain_of_though_steps = chain_of_though_steps input_variables = ["chat_history", "context"] if chain_of_though_steps is not None: used_template = chain_of_though_template input_variables.append("chain_of_though") # self.output_parser = StructuredOutputParser.from_response_schemas(response_schemas) # format_instructions =self.output_parser.get_format_instructions() prompt_template = PromptTemplate( input_variables=input_variables, # partial_variables={"format_instructions": format_instructions}, template=used_template ) self.chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True, output_key="output") def __call__(self, chat_history): if self.chain_of_though_steps is not None: chain_of_though_input = "" for idx, step in enumerate(self.chain_of_though_steps): chain_of_though_input += "Step {idx}: {step} \n" info = self.chain({"chat_history": chat_history, "chain_of_though": chain_of_though_input, "context": self.context}) else: info = self.chain({"chat_history": chat_history, "context": self.context}) # return self.output_parser.parse(info["output"])["text"] return info["output"]
[ "{context}\n\nCurrent conversation:\n{chat_history}\n\nYou respond in a short, very conversational friendly style.\nresponse to th assistant:", "You are customer that wants to book an accommodation for the weekend in the city of \"Mercedes\" at the \"Complejo Enrique Joaquin\". Allways answer in Spanish.\nYou ask your requirements one at a time.\n\nThese are your requirements for the accommodation: {context}\n\nFollow these Steps before responding to the user new message:\n{chain_of_though}\n\nCurrent conversation:\n{chat_history}\n\nYou respond in a short, very conversational friendly style.\nresponse to th assistant:\n" ]
2024-01-10
agustin-sarasua/bnbot-core
app~utils~utils_fn.py
import time import boto3 import json import os import openai from datetime import datetime def get_current_datetime(): return datetime.now() import unicodedata def remove_spanish_special_characters(text): """ Removes Spanish special characters from a string. """ # Normalize the string by converting it to Unicode NFD form normalized_text = unicodedata.normalize('NFD', text) # Remove combining characters stripped_text = ''.join(c for c in normalized_text if not unicodedata.combining(c)) # Remove specific Spanish special characters removed_special_characters = stripped_text.replace('ñ', 'n').replace('Ñ', 'N').replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u').replace('Á', 'A').replace('É', 'E').replace('Í', 'I').replace('Ó', 'O').replace('Ú', 'U') return removed_special_characters def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0, max_tokens=500): openai.api_key = os.environ['OPENAI_API_KEY'] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, ) return response.choices[0].message["content"] def read_json_from_s3(bucket_name, file_name): s3 = boto3.resource('s3') try: obj = s3.Object(bucket_name, file_name) data = obj.get()['Body'].read().decode('utf-8') json_data = json.loads(data) return json_data except Exception as e: print(f"Error reading JSON file: {e}") return None class Cache: def __init__(self, timeout=120): self.cache_data = {} self.timeout = timeout def get(self, key, default_value): value, timestamp = self.cache_data.get(key, (default_value, None)) if self.timeout > 0: if timestamp and time.time() - timestamp > self.timeout: self.delete(key) return default_value return value def set(self, key, value): timestamp = time.time() self.cache_data[key] = (value, timestamp) def delete(self, key): if key in self.cache_data: del self.cache_data[key]
[]
2024-01-10
agustin-sarasua/bnbot-core
app~tools~next_step_extractor_tool.py
from typing import List from app.model import Message import openai import os import json from app.utils import logger from datetime import datetime, timedelta openai.api_key = os.environ.get('OPENAI_API_KEY') next_step_selection_fn = { "name": "take_next_step", "description": "Takes the next step for the conversation.", "parameters": { "type": "object", "properties": { "step_id": { "type": "string", "description": "The id of the step to take next from the list of possible steps." }, }, "required": ["step_id"] } } class NextStepExtractor: def _build_steps_str(self, steps): result = dict() for step in steps: result[step['name']] = step['description'] return json.dumps(result) def run_select_next_step(self, messages: List[Message], steps: List[dict]): steps_str = self._build_steps_str(steps) system_prompt = f"""Given a conversation between a user and an assistant about booking accommodation, take the next step for the conversation. Here is the list of possible steps to take: {steps_str} """ messages_input = [{"role": "system", "content": system_prompt}] for msg in messages: messages_input.append({"role": msg.role, "content": msg.text}) # messages_input.append("role") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-0613", messages=messages_input, functions=[next_step_selection_fn], function_call={"name": "take_next_step"}, temperature=0., max_tokens=500, ) fn_parameters = json.loads(response.choices[0].message["function_call"]["arguments"]) logger.debug(f"take_next_step fn_parameters {fn_parameters}") return fn_parameters
[ "Given a conversation between a user and an assistant about booking accommodation, take the next step for the conversation.\n Here is the list of possible steps to take:\n PLACEHOLDER\n " ]
2024-01-10
agustin-sarasua/bnbot-core
tests~test_gather_booking_info_resolver.py
import unittest from unittest.mock import MagicMock from app.model import Message, StepData from app.task_resolver.step_resolvers import GatherBookingInfoResolver import os import openai from dotenv import load_dotenv, find_dotenv class TestGatherBookingInfoResolver(unittest.TestCase): def setUp(self): _ = load_dotenv(find_dotenv(filename="../.env")) # read local .env file openai.api_key = os.environ['OPENAI_API_KEY'] def test_run(self): # Arrange def _create_step_data(info) -> StepData: step_data = StepData() step_data.resolver_data =info return step_data test_cases = [ { "messages": [ Message("user", "Hola"), ], "step_chat_history": [ Message("user", "Hola"), ], "expected_resolver_done": False }, { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene.") ], "step_chat_history": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene.") ], "expected_resolver_done": False }, { "messages": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "step_chat_history": [ Message("user", "Hola"), Message("assistant", "Hola, ¿en qué puedo ayudarte?"), Message("user", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches.") ], "expected_resolver_done": True } ] for idx, test in enumerate(test_cases): print(f"Running test {idx}") resolver = GatherBookingInfoResolver() # Act result = resolver.run(test["messages"], {}, test["step_chat_history"]) # Assert self.assertIsNotNone(result) self.assertEqual(test["expected_resolver_done"], resolver.is_done()) def test_run_exit_task_resolver_false(self): conversations = [ [ ] ] resolver = GatherBookingInfoResolver() # step_data = {"current_task_name": "MAKE_RESERVATION_TASK"} for idx, conv in enumerate(conversations): print(f"Running test {idx}") previous_steps_data = dict() # Act resolver.run(conv, previous_steps_data) # Assert self.assertEqual(resolver.is_done(), False) if __name__ == '__main__': unittest.main()
[ "Hola, ¿en qué puedo ayudarte?", "Me gustaría reservar una casa para dos personas, para el jueves que viene.", "assistant", "user", "Hola", "Me gustaría reservar una casa para dos personas, para el jueves que viene por dos noches." ]
2024-01-10
AlexKoff88/CLIP_benchmark
clip_benchmark~datasets~builder.py
import os import warnings import sys import json from subprocess import call from collections import defaultdict import torch from torchvision.datasets import ( VisionDataset, ImageFolder, CIFAR10, CIFAR100, ImageNet, CocoCaptions, Flickr8k, Flickr30k, Food101, SUN397, StanfordCars, FGVCAircraft, DTD, OxfordIIITPet, Caltech101, Flowers102, MNIST, STL10, EuroSAT, GTSRB, Kitti, Country211, PCAM, RenderedSST2 ) from . import voc2007, flickr, caltech101, imagenetv2, objectnet from torch.utils.data import default_collate from PIL import Image def _load_classnames_and_classification_templates(dataset_name, current_folder, language): with open(os.path.join(current_folder, language + "_classnames.json"), "r") as f: classnames = json.load(f) # Zero-shot classification templates, collected from a bunch of sources # - CLIP paper (https://github.com/openai/CLIP/blob/main/data/prompts.md) # - Lit Paper (https://arxiv.org/pdf/2111.07991.pdf) # - SLIP paper (https://github.com/facebookresearch/SLIP/blob/main/templates.json) # Some are fixed mnaually with open(os.path.join(current_folder, language + "_zeroshot_classification_templates.json"), "r") as f: zeroshot_classification_templates = json.load(f) # default template to use when the dataset name does not belong to `zeroshot_classification_templates` DEFAULT_ZEROSHOT_CLASSIFICATION_TEMPLATES = zeroshot_classification_templates["imagenet1k"] if dataset_name.startswith("tfds/") or dataset_name.startswith("vtab/") or dataset_name.startswith("wds/"): name = dataset_name.split("/")[-1] else: name = dataset_name templates = zeroshot_classification_templates.get(name, DEFAULT_ZEROSHOT_CLASSIFICATION_TEMPLATES) return classnames, templates def build_dataset(dataset_name, root="root", transform=None, split="test", download=True, annotation_file=None, language="en", task='zeroshot_classification', cupl=False, wds_cache_dir=None, **kwargs): """ Main function to use in order to build a dataset instance, dataset_name: str name of the dataset root: str root folder where the dataset is downloaded and stored. can be shared among datasets. transform: torchvision transform applied to images split: str split to use, depending on the dataset can have different options. In general, `train` and `test` are available. For specific splits, please look at the corresponding dataset. annotation_file: str or None only for datasets with captions (used for retrieval) such as COCO and Flickr. """ current_folder = os.path.dirname(__file__) if task in ('zeroshot_classification', 'linear_probe'): # Only load templates and classnames if we have to classnames, templates = _load_classnames_and_classification_templates(dataset_name, current_folder, language) else: classnames, templates = None, None with open(os.path.join(current_folder, "cupl_prompts.json"), "r") as f: cupl_prompts = json.load(f) templates_cupl = None train = (split == "train") if dataset_name == "cifar10": ds = CIFAR10(root=root, train=train, transform=transform, download=download, **kwargs) elif dataset_name == "cifar100": ds = CIFAR100(root=root, train=train, transform=transform, download=download, **kwargs) elif dataset_name == "imagenet1k": if not os.path.exists(root): os.makedirs(root, exist_ok=True) call(f"wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_devkit_t12.tar.gz --output-document={root}/ILSVRC2012_devkit_t12.tar.gz", shell=True) call(f"wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar --output-document={root}/ILSVRC2012_img_val.tar", shell=True) ds = ImageNet(root=root, split="train" if train else "val", transform=transform, **kwargs) # use classnames from OpenAI ds.classes = classnames["imagenet1k"] templates_cupl = cupl_prompts["imagenet1k"] elif dataset_name == "imagenet1k-unverified": split = "train" if train else "val" ds = ImageFolder(root=os.path.join(root, split), transform=transform, **kwargs) # use classnames from OpenAI ds.classes = classnames["imagenet1k"] templates_cupl = cupl_prompts["imagenet1k"] elif dataset_name == "imagenetv2": assert split == "test", f"Only test split available for {dataset_name}" os.makedirs(root, exist_ok=True) ds = imagenetv2.ImageNetV2Dataset(variant="matched-frequency", transform=transform, location=root) ds.classes = classnames["imagenet1k"] templates_cupl = cupl_prompts["imagenet1k"] elif dataset_name == "imagenet_sketch": assert split == "test", f"Only test split available for {dataset_name}" # Downloadable from https://drive.google.com/open?id=1Mj0i5HBthqH1p_yeXzsg22gZduvgoNeA if not os.path.exists(root): # Automatic download print("Downloading imagenet_sketch...") if not has_gdown(): print("GDown is needed to download the dataset. Please install it via `pip install gdown`") sys.exit(1) # Download ImageNet-Sketch.zip call("gdown --id 1Mj0i5HBthqH1p_yeXzsg22gZduvgoNeA", shell=True) assert os.path.exists("ImageNet-Sketch.zip") # Unzip and move to `root` call("unzip ImageNet-Sketch.zip", shell=True) call(f"mv sketch {root}", shell=True) ds = ImageFolder(root=root, transform=transform, **kwargs) ds.classes = classnames["imagenet1k"] templates_cupl = cupl_prompts["imagenet1k"] elif dataset_name == "imagenet-a": assert split == "test", f"Only test split available for {dataset_name}" # Downloadable from https://people.eecs.berkeley.edu/~hendrycks/imagenet-a.tar if not os.path.exists(root): print("Downloading imagenet-a...") call("wget https://people.eecs.berkeley.edu/~hendrycks/imagenet-a.tar", shell=True) # Untar and move to `root` call("tar xvf imagenet-a.tar", shell=True) call(f"mv imagenet-a {root}", shell=True) ds = ImageFolder(root=root, transform=transform, **kwargs) ds.classes = classnames["imagenet1k"] imagenet_a_wnids = ['n01498041', 'n01531178', 'n01534433', 'n01558993', 'n01580077', 'n01614925', 'n01616318', 'n01631663', 'n01641577', 'n01669191', 'n01677366', 'n01687978', 'n01694178', 'n01698640', 'n01735189', 'n01770081', 'n01770393', 'n01774750', 'n01784675', 'n01819313', 'n01820546', 'n01833805', 'n01843383', 'n01847000', 'n01855672', 'n01882714', 'n01910747', 'n01914609', 'n01924916', 'n01944390', 'n01985128', 'n01986214', 'n02007558', 'n02009912', 'n02037110', 'n02051845', 'n02077923', 'n02085620', 'n02099601', 'n02106550', 'n02106662', 'n02110958', 'n02119022', 'n02123394', 'n02127052', 'n02129165', 'n02133161', 'n02137549', 'n02165456', 'n02174001', 'n02177972', 'n02190166', 'n02206856', 'n02219486', 'n02226429', 'n02231487', 'n02233338', 'n02236044', 'n02259212', 'n02268443', 'n02279972', 'n02280649', 'n02281787', 'n02317335', 'n02325366', 'n02346627', 'n02356798', 'n02361337', 'n02410509', 'n02445715', 'n02454379', 'n02486410', 'n02492035', 'n02504458', 'n02655020', 'n02669723', 'n02672831', 'n02676566', 'n02690373', 'n02701002', 'n02730930', 'n02777292', 'n02782093', 'n02787622', 'n02793495', 'n02797295', 'n02802426', 'n02814860', 'n02815834', 'n02837789', 'n02879718', 'n02883205', 'n02895154', 'n02906734', 'n02948072', 'n02951358', 'n02980441', 'n02992211', 'n02999410', 'n03014705', 'n03026506', 'n03124043', 'n03125729', 'n03187595', 'n03196217', 'n03223299', 'n03250847', 'n03255030', 'n03291819', 'n03325584', 'n03355925', 'n03384352', 'n03388043', 'n03417042', 'n03443371', 'n03444034', 'n03445924', 'n03452741', 'n03483316', 'n03584829', 'n03590841', 'n03594945', 'n03617480', 'n03666591', 'n03670208', 'n03717622', 'n03720891', 'n03721384', 'n03724870', 'n03775071', 'n03788195', 'n03804744', 'n03837869', 'n03840681', 'n03854065', 'n03888257', 'n03891332', 'n03935335', 'n03982430', 'n04019541', 'n04033901', 'n04039381', 'n04067472', 'n04086273', 'n04099969', 'n04118538', 'n04131690', 'n04133789', 'n04141076', 'n04146614', 'n04147183', 'n04179913', 'n04208210', 'n04235860', 'n04252077', 'n04252225', 'n04254120', 'n04270147', 'n04275548', 'n04310018', 'n04317175', 'n04344873', 'n04347754', 'n04355338', 'n04366367', 'n04376876', 'n04389033', 'n04399382', 'n04442312', 'n04456115', 'n04482393', 'n04507155', 'n04509417', 'n04532670', 'n04540053', 'n04554684', 'n04562935', 'n04591713', 'n04606251', 'n07583066', 'n07695742', 'n07697313', 'n07697537', 'n07714990', 'n07718472', 'n07720875', 'n07734744', 'n07749582', 'n07753592', 'n07760859', 'n07768694', 'n07831146', 'n09229709', 'n09246464', 'n09472597', 'n09835506', 'n11879895', 'n12057211', 'n12144580', 'n12267677'] imagenet_a_mask = [wnid in set(imagenet_a_wnids) for wnid in all_imagenet_wordnet_ids] ds.classes = [cl for cl, mask in zip(ds.classes, imagenet_a_mask) if mask] elif dataset_name == "imagenet-r": assert split == "test", f"Only test split available for {dataset_name}" # downloadable from https://people.eecs.berkeley.edu/~hendrycks/imagenet-r.tar if not os.path.exists(root): print("Downloading imagenet-r...") call("wget https://people.eecs.berkeley.edu/~hendrycks/imagenet-r.tar", shell=True) # Untar and move to `root` call("tar xvf imagenet-r.tar", shell=True) call(f"mv imagenet-r {root}", shell=True) imagenet_r_wnids = {'n01443537', 'n01484850', 'n01494475', 'n01498041', 'n01514859', 'n01518878', 'n01531178', 'n01534433', 'n01614925', 'n01616318', 'n01630670', 'n01632777', 'n01644373', 'n01677366', 'n01694178', 'n01748264', 'n01770393', 'n01774750', 'n01784675', 'n01806143', 'n01820546', 'n01833805', 'n01843383', 'n01847000', 'n01855672', 'n01860187', 'n01882714', 'n01910747', 'n01944390', 'n01983481', 'n01986214', 'n02007558', 'n02009912', 'n02051845', 'n02056570', 'n02066245', 'n02071294', 'n02077923', 'n02085620', 'n02086240', 'n02088094', 'n02088238', 'n02088364', 'n02088466', 'n02091032', 'n02091134', 'n02092339', 'n02094433', 'n02096585', 'n02097298', 'n02098286', 'n02099601', 'n02099712', 'n02102318', 'n02106030', 'n02106166', 'n02106550', 'n02106662', 'n02108089', 'n02108915', 'n02109525', 'n02110185', 'n02110341', 'n02110958', 'n02112018', 'n02112137', 'n02113023', 'n02113624', 'n02113799', 'n02114367', 'n02117135', 'n02119022', 'n02123045', 'n02128385', 'n02128757', 'n02129165', 'n02129604', 'n02130308', 'n02134084', 'n02138441', 'n02165456', 'n02190166', 'n02206856', 'n02219486', 'n02226429', 'n02233338', 'n02236044', 'n02268443', 'n02279972', 'n02317335', 'n02325366', 'n02346627', 'n02356798', 'n02363005', 'n02364673', 'n02391049', 'n02395406', 'n02398521', 'n02410509', 'n02423022', 'n02437616', 'n02445715', 'n02447366', 'n02480495', 'n02480855', 'n02481823', 'n02483362', 'n02486410', 'n02510455', 'n02526121', 'n02607072', 'n02655020', 'n02672831', 'n02701002', 'n02749479', 'n02769748', 'n02793495', 'n02797295', 'n02802426', 'n02808440', 'n02814860', 'n02823750', 'n02841315', 'n02843684', 'n02883205', 'n02906734', 'n02909870', 'n02939185', 'n02948072', 'n02950826', 'n02951358', 'n02966193', 'n02980441', 'n02992529', 'n03124170', 'n03272010', 'n03345487', 'n03372029', 'n03424325', 'n03452741', 'n03467068', 'n03481172', 'n03494278', 'n03495258', 'n03498962', 'n03594945', 'n03602883', 'n03630383', 'n03649909', 'n03676483', 'n03710193', 'n03773504', 'n03775071', 'n03888257', 'n03930630', 'n03947888', 'n04086273', 'n04118538', 'n04133789', 'n04141076', 'n04146614', 'n04147183', 'n04192698', 'n04254680', 'n04266014', 'n04275548', 'n04310018', 'n04325704', 'n04347754', 'n04389033', 'n04409515', 'n04465501', 'n04487394', 'n04522168', 'n04536866', 'n04552348', 'n04591713', 'n07614500', 'n07693725', 'n07695742', 'n07697313', 'n07697537', 'n07714571', 'n07714990', 'n07718472', 'n07720875', 'n07734744', 'n07742313', 'n07745940', 'n07749582', 'n07753275', 'n07753592', 'n07768694', 'n07873807', 'n07880968', 'n07920052', 'n09472597', 'n09835506', 'n10565667', 'n12267677'} imagenet_r_mask = [wnid in imagenet_r_wnids for wnid in all_imagenet_wordnet_ids] ds = ImageFolder(root=root, transform=transform, **kwargs) ds.classes = classnames["imagenet1k"] ds.classes = [cl for cl, mask in zip(ds.classes, imagenet_r_mask) if mask] elif dataset_name == "imagenet-o": assert split == "test", f"Only test split available for {dataset_name}" # downloadable from https://people.eecs.berkeley.edu/~hendrycks/imagenet-o.tar if not os.path.exists(root): print("Downloading imagenet-o...") call("wget https://people.eecs.berkeley.edu/~hendrycks/imagenet-o.tar", shell=True) # Untar and move to `root` call("tar xvf imagenet-o.tar", shell=True) call(f"mv imagenet-o {root}", shell=True) ds = ImageFolder(root=root, transform=transform, **kwargs) ds.classes = classnames["imagenet1k"] imagenet_o_wnids = ['n01443537', 'n01704323', 'n01770081', 'n01784675', 'n01819313', 'n01820546', 'n01910747', 'n01917289', 'n01968897', 'n02074367', 'n02317335', 'n02319095', 'n02395406', 'n02454379', 'n02606052', 'n02655020', 'n02666196', 'n02672831', 'n02730930', 'n02777292', 'n02783161', 'n02786058', 'n02787622', 'n02791270', 'n02808304', 'n02817516', 'n02841315', 'n02865351', 'n02877765', 'n02892767', 'n02906734', 'n02910353', 'n02916936', 'n02948072', 'n02965783', 'n03000134', 'n03000684', 'n03017168', 'n03026506', 'n03032252', 'n03075370', 'n03109150', 'n03126707', 'n03134739', 'n03160309', 'n03196217', 'n03207743', 'n03218198', 'n03223299', 'n03240683', 'n03271574', 'n03291819', 'n03297495', 'n03314780', 'n03325584', 'n03344393', 'n03347037', 'n03372029', 'n03376595', 'n03388043', 'n03388183', 'n03400231', 'n03445777', 'n03457902', 'n03467068', 'n03482405', 'n03483316', 'n03494278', 'n03530642', 'n03544143', 'n03584829', 'n03590841', 'n03598930', 'n03602883', 'n03649909', 'n03661043', 'n03666591', 'n03676483', 'n03692522', 'n03706229', 'n03717622', 'n03720891', 'n03721384', 'n03724870', 'n03729826', 'n03733131', 'n03733281', 'n03742115', 'n03786901', 'n03788365', 'n03794056', 'n03804744', 'n03814639', 'n03814906', 'n03825788', 'n03840681', 'n03843555', 'n03854065', 'n03857828', 'n03868863', 'n03874293', 'n03884397', 'n03891251', 'n03908714', 'n03920288', 'n03929660', 'n03930313', 'n03937543', 'n03942813', 'n03944341', 'n03961711', 'n03970156', 'n03982430', 'n03991062', 'n03995372', 'n03998194', 'n04005630', 'n04023962', 'n04033901', 'n04040759', 'n04067472', 'n04074963', 'n04116512', 'n04118776', 'n04125021', 'n04127249', 'n04131690', 'n04141975', 'n04153751', 'n04154565', 'n04201297', 'n04204347', 'n04209133', 'n04209239', 'n04228054', 'n04235860', 'n04243546', 'n04252077', 'n04254120', 'n04258138', 'n04265275', 'n04270147', 'n04275548', 'n04330267', 'n04332243', 'n04336792', 'n04347754', 'n04371430', 'n04371774', 'n04372370', 'n04376876', 'n04409515', 'n04417672', 'n04418357', 'n04423845', 'n04429376', 'n04435653', 'n04442312', 'n04482393', 'n04501370', 'n04507155', 'n04525305', 'n04542943', 'n04554684', 'n04557648', 'n04562935', 'n04579432', 'n04591157', 'n04597913', 'n04599235', 'n06785654', 'n06874185', 'n07615774', 'n07693725', 'n07695742', 'n07697537', 'n07711569', 'n07714990', 'n07715103', 'n07716358', 'n07717410', 'n07718472', 'n07720875', 'n07742313', 'n07745940', 'n07747607', 'n07749582', 'n07753275', 'n07753592', 'n07754684', 'n07768694', 'n07836838', 'n07871810', 'n07873807', 'n07880968', 'n09229709', 'n09472597', 'n12144580', 'n12267677', 'n13052670'] imagenet_o_mask = [wnid in set(imagenet_o_wnids) for wnid in all_imagenet_wordnet_ids] ds.classes = [cl for cl, mask in zip(ds.classes, imagenet_o_mask) if mask] elif dataset_name == "objectnet": assert split == "test", f"Only test split available for {dataset_name}" # downloadable from https://objectnet.dev/downloads/objectnet-1.0.zip or https://www.dropbox.com/s/raw/cxeztdtm16nzvuw/objectnet-1.0.zip if not os.path.exists(root): print("Downloading objectnet...") call("wget https://objectnet.dev/downloads/objectnet-1.0.zip", shell=True) # Untar and move to `root` call("UNZIP_DISABLE_ZIPBOMB_DETECTION=TRUE unzip -P objectnetisatestset objectnet-1.0.zip", shell=True) os.makedirs(root) call(f"mv objectnet-1.0 {root}", shell=True) call(f"cp {root}/objectnet-1.0/mappings/* {root}", shell=True) ds = objectnet.ObjectNetDataset(root=root, transform=transform) elif dataset_name == "voc2007": ds = voc2007.PASCALVoc2007Cropped(root=root, set="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "voc2007_multilabel": ds = voc2007.PASCALVoc2007(root=root, set="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "mscoco_captions": # https://github.com/mehdidc/retrieval_annotations/releases/tag/1.0.0(annotations) if split == "train": archive_name = "train2014.zip" elif split in ("val", "test"): archive_name = "val2014.zip" else: raise ValueError(f"split should be train or val or test for `{dataset_name}`") root_split = os.path.join(root, archive_name.replace(".zip", "")) if not os.path.exists(root_split): print(f"Downloading mscoco_captions {archive_name}...") if not os.path.exists(os.path.join(root, archive_name)): call(f"wget http://images.cocodataset.org/zips/{archive_name} --output-document={root}/{archive_name}", shell=True) call(f"unzip {root}/{archive_name} -d {root}", shell=True) if not annotation_file: annotation_file = f"{root}/coco_{split}_karpathy.json" if not os.path.exists(annotation_file): call(f"wget https://github.com/mehdidc/retrieval_annotations/releases/download/1.0.0/coco_{split}_karpathy.json --output-document={annotation_file}", shell=True) ds = CocoCaptions(root=root_split, annFile=annotation_file, transform=transform, **kwargs) elif dataset_name == 'multilingual_mscoco_captions': from clip_benchmark.datasets import multilingual_mscoco if(language not in multilingual_mscoco.SUPPORTED_LANGUAGES): raise ValueError("Unsupported language for multilingual_ms_coco:", language) def get_archive_name(target_split): if target_split == "train": return "train2014.zip" elif target_split in ("val", "test"): return "val2014.zip" else: raise ValueError(f"split should be train or val or test for `{dataset_name}`") def download_mscoco_split(target_split): archive_name = get_archive_name(target_split) root_split = os.path.join(root, archive_name.replace(".zip", "")) if not os.path.exists(root_split): print(f"Downloading mscoco_captions {archive_name}...") if not os.path.exists(os.path.join(root, archive_name)): call(f"wget http://images.cocodataset.org/zips/{archive_name} --output-document={root}/{archive_name}", shell=True) call(f"unzip {root}/{archive_name} -d {root}", shell=True) # The multilingual MS-COCO uses images from various splits for target_split in ['train', 'val', 'test']: download_mscoco_split(target_split) annotation_file = os.path.join(root, multilingual_mscoco.CAPTIONS_FILE_NAME.format(language)) if (os.path.exists(annotation_file) == False): multilingual_mscoco.create_annotation_file(root, language) ds = multilingual_mscoco.Multilingual_MSCOCO(root=root, ann_file=annotation_file, transform=transform, **kwargs) elif dataset_name == "flickr30k": # downloadable from https://www.kaggle.com/datasets/adityajn105/flickr30k # https://github.com/mehdidc/retrieval_annotations/releases/tag/1.0.0(annotations) # `kaggle datasets download -d adityajn105/flickr30k` if not os.path.exists(root): # Automatic download print("Downloading flickr30k...") if not has_kaggle(): print("Kaggle is needed to download the dataset. Please install it via `pip install kaggle`") sys.exit(1) call("kaggle datasets download -d adityajn105/flickr30k", shell=True) call(f"unzip flickr30k.zip", shell=True) call(f"mv Images {root}", shell=True) call(f"mv captions.txt {root}", shell=True) if not annotation_file: annotation_file = f"{root}/flickr30k_{split}_karpathy.txt" if not os.path.exists(annotation_file): # Download Flickr30K Karpathy test set annotation_file = f"{root}/flickr30k_{split}_karpathy.txt" call(f"wget https://github.com/mehdidc/retrieval_annotations/releases/download/1.0.0/flickr30k_{split}_karpathy.txt --output-document={annotation_file}", shell=True) ds = flickr.Flickr(root=root, ann_file=annotation_file, transform=transform, **kwargs) elif dataset_name == "flickr8k": # downloadable from https://www.kaggle.com/datasets/adityajn105/flickr8k # `kaggle datasets download -d adityajn105/flickr8k` # https://github.com/mehdidc/retrieval_annotations/releases/tag/1.0.0(annotations) if not os.path.exists(root): # Automatic download print("Downloading flickr8k...") if not has_kaggle(): print("Kaggle is needed to download the dataset. Please install it via `pip install kaggle`") sys.exit(1) call("kaggle datasets download -d adityajn105/flickr8k", shell=True) call(f"unzip flickr8k.zip", shell=True) call(f"mv Images {root}", shell=True) call(f"mv captions.txt {root}", shell=True) if not annotation_file: annotation_file = f"{root}/flickr8k_{split}_karpathy.txt" if not os.path.exists(annotation_file): # Download Flickr8K Karpathy test set annotation_file = f"{root}/flickr8k_{split}_karpathy.txt" call(f"wget https://github.com/mehdidc/retrieval_annotations/releases/download/1.0.0/flickr8k_{split}_karpathy.txt --output-document={annotation_file}", shell=True) ds = flickr.Flickr(root=root, ann_file=annotation_file, transform=transform, **kwargs) elif dataset_name == "food101": ds = Food101(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) # we use the default class names, we just replace "_" by spaces # to delimit words ds.classes = [cl.replace("_", " ") for cl in ds.classes] elif dataset_name == "sun397": warnings.warn(f"split argument ignored for `{dataset_name}`, there are no pre-defined train/test splits for this dataset") # we use the default class names, we just replace "_" and "/" by spaces # to delimit words ds = SUN397(root=root, transform=transform, download=download, **kwargs) ds.classes = [cl.replace("_", " ").replace("/", " ") for cl in ds.classes] elif dataset_name == "cars": ds = StanfordCars(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "fgvc_aircraft": ds = FGVCAircraft(root=root, annotation_level="variant", split="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "dtd": ds = DTD(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "pets": ds = OxfordIIITPet(root=root, split="train" if train else "test", target_types="category", transform=transform, download=download, **kwargs) elif dataset_name == "caltech101": warnings.warn(f"split argument ignored for `{dataset_name}`, there are no pre-defined train/test splits for this dataset") # broken download link (can't download google drive), fixed by this PR https://github.com/pytorch/vision/pull/5645 # also available in "vtab/caltech101" using VTAB splits, we advice to use VTAB version rather than this one # since in this one (torchvision) there are no pre-defined test splits ds = caltech101.Caltech101(root=root, target_type="category", transform=transform, download=download, **kwargs) ds.classes = classnames["caltech101"] elif dataset_name == "flowers": ds = Flowers102(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) # class indices started by 1 until it was fixed in a PR (#TODO link of the PR) # if older torchvision version, fix it using a target transform that decrements label index # TODO figure out minimal torchvision version needed instead of decrementing if ds[0][1] == 1: ds.target_transform = lambda y:y-1 ds.classes = classnames["flowers"] elif dataset_name == "mnist": ds = MNIST(root=root, train=train, transform=transform, download=download, **kwargs) ds.classes = classnames["mnist"] elif dataset_name == "stl10": ds = STL10(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "eurosat": warnings.warn(f"split argument ignored for `{dataset_name}`, there are no pre-defined train/test splits for this dataset") ds = EuroSAT(root=root, transform=transform, download=download, **kwargs) ds.classes = classnames["eurosat"] elif dataset_name == "gtsrb": ds = GTSRB(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) ds.classes = classnames["gtsrb"] elif dataset_name == "country211": ds = Country211(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) ds.classes = classnames["country211"] elif dataset_name == "pcam": # Dead link. Fixed by this PR on torchvision https://github.com/pytorch/vision/pull/5645 # TODO figure out minimal torchvision version needed ds = PCAM(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) ds.classes = classnames["pcam"] elif dataset_name == "renderedsst2": ds = RenderedSST2(root=root, split="train" if train else "test", transform=transform, download=download, **kwargs) elif dataset_name == "fer2013": # Downloadable from https://www.kaggle.com/datasets/msambare/fer2013 # `kaggle datasets download -d msambare/fer2013` if not os.path.exists(root): # Automatic download print("Downloading fer2013...") if not has_kaggle(): print("Kaggle is needed to download the dataset. Please install it via `pip install kaggle`") sys.exit(1) call("kaggle datasets download -d msambare/fer2013", shell=True) call(f"unzip fer2013.zip -d {root}", shell=True) root = os.path.join(root, "train" if train else "test") ds = ImageFolder(root=root, transform=transform) ds.classes = classnames["fer2013"] elif dataset_name.startswith("tfds/"): # TFDS datasets support using `timm` and `tensorflow_datasets` prefix, *name_list = dataset_name.split("/") name = "/".join(name_list) ds = build_tfds_dataset(name, download=download, split=split, data_dir=root, transform=transform) elif dataset_name.startswith("vtab/"): # VTAB datasets support using `tensorflow_datasets` and `task_adaptation` prefix, *name_list = dataset_name.split("/") name = "/".join(name_list) ds = build_vtab_dataset(name, download=download, split=split, data_dir=root, transform=transform, classnames=classnames) elif dataset_name.startswith("wds/"): # WebDataset support using `webdataset` library name = dataset_name.split("/", 1)[1] ds = build_wds_dataset(name, transform=transform, split=split, data_dir=root, cache_dir=wds_cache_dir) return ds elif dataset_name == "dummy": ds = Dummy() else: raise ValueError(f"Unsupported dataset: {dataset_name}.") if cupl: ds.templates = templates_cupl else: ds.templates = templates return ds class Dummy(): def __init__(self): self.classes = ["blank image", "noisy image"] def __getitem__(self, i): return torch.zeros(3,224,224), 0 def __len__(self): return 1 def get_dataset_default_task(dataset): if dataset in ("flickr30k", "flickr8k", "mscoco_captions", "multilingual_mscoco_captions"): return "zeroshot_retrieval" else: return "zeroshot_classification" def get_dataset_collate_fn(dataset_name): if dataset_name in ("mscoco_captions", "multilingual_mscoco_captions", "flickr30k", "flickr8k"): return image_captions_collate_fn else: return default_collate def has_gdown(): return call("which gdown", shell=True) == 0 def has_kaggle(): return call("which kaggle", shell=True) == 0 def build_vtab_dataset(dataset_name, transform, download=True, split="test", data_dir="root", classnames=[]): # Using VTAB splits instead of default TFDS splits from .tfds import VTABIterableDataset, disable_gpus_on_tensorflow, download_tfds_dataset # avoid Tensorflow owning GPUs to not clash with PyTorch disable_gpus_on_tensorflow() # by default we take classes from TFDS (default behavior if `classes` stays None), # except for the datasets that will override `classes` (e.g., clevr_*) classes = None if dataset_name == "caltech101": from task_adaptation.data.caltech import Caltech101 tfds_dataset = Caltech101(data_dir=data_dir) classes = classnames["caltech101_vtab"] elif dataset_name == "cars": from task_adaptation.data.cars import CarsData tfds_dataset = CarsData(data_dir=data_dir) elif dataset_name in ("cifar10", "cifar100"): from task_adaptation.data.cifar import CifarData tfds_dataset = CifarData(data_dir=data_dir, num_classes=10 if dataset_name == "cifar10" else 100) elif dataset_name.startswith("clevr_"): from task_adaptation.data.clevr import CLEVRData task = _extract_task(dataset_name) assert task in ("count_all", "closest_object_distance") tfds_dataset = CLEVRData(task=task, data_dir=data_dir) if task == "count_all": classes = classnames["clevr_count_all"] elif task == "closest_object_distance": classes = classnames["clevr_closest_object_distance"] else: raise ValueError(f"non supported: {task}") elif dataset_name == "cub": from task_adaptation.data.cub import CUB2011Data tfds_dataset = CUB2011Data(data_dir=data_dir) elif dataset_name == "diabetic_retinopathy": # Needs manual download from Kaggle # 1) `kaggle competitions download -c diabetic-retinopathy-detection` on $ROOT/downloads/manual # 2) extract archives on $ROOT/downloads/manual if not os.path.exists(data_dir): # Automatic download print("Downloading diabetic_retinopathy...") if not has_kaggle(): print("Kaggle is needed to download the dataset. Please install it via `pip install kaggle`") sys.exit(1) os.makedirs(os.path.join(data_dir, "downloads", "manual")) call(f"kaggle competitions download -c diabetic-retinopathy-detection -p {data_dir}/downloads/manual", shell=True) call(f"cd {data_dir}/downloads/manual;unzip diabetic-retinopathy-detection.zip;cat train.zip*>train.zip;cat test.zip*>test.zip;unzip train.zip; unzip test.zip;unzip sample.zip;unzip trainLabels.csv.zip", shell=True) from task_adaptation.data.diabetic_retinopathy import RetinopathyData tfds_dataset = RetinopathyData(config="btgraham-300", data_dir=data_dir) classes = classnames["diabetic_retinopathy"] elif dataset_name == "dmlab": from task_adaptation.data.dmlab import DmlabData download_tfds_dataset("dmlab", data_dir=data_dir) # it's not called in the original VTAB code, so we do it explictly tfds_dataset = DmlabData(data_dir=data_dir) classes = classnames["dmlab"] elif dataset_name.startswith("dsprites_"): from task_adaptation.data.dsprites import DSpritesData task = _extract_task(dataset_name) assert task in ("label_shape", "label_scale", "label_orientation", "label_x_position", "label_y_position") tfds_dataset = DSpritesData(task, data_dir=data_dir) classes = tfds_dataset._dataset_builder.info.features[task].names elif dataset_name == "dtd": from task_adaptation.data.dtd import DTDData tfds_dataset = DTDData(data_dir=data_dir) elif dataset_name == "eurosat": from task_adaptation.data.eurosat import EurosatData tfds_dataset = EurosatData(subset="rgb", data_key="image", data_dir=data_dir) classes = classnames["eurosat"] elif dataset_name == "food101": from task_adaptation.data.food101 import Food101Data tfds_dataset = Food101Data(data_dir=data_dir) elif dataset_name == "inaturalist": from task_adaptation.data.inaturalist import INaturalistData tfds_dataset = INaturalistData(data_dir=data_dir, year=2017) elif dataset_name.startswith("kitti_"): from .kitti import KittiData task = _extract_task(dataset_name) assert task in ( "count_all", "count_left", "count_far", "count_near", "closest_object_distance", "closest_object_x_location", "count_vehicles", "closest_vehicle_distance", ) tfds_dataset = KittiData(task=task, data_dir=data_dir) if task == "closest_vehicle_distance": classes = classnames["kitti_closest_vehicle_distance"] else: raise ValueError(f"Unsupported task: {task}") elif dataset_name == "flowers": from task_adaptation.data.oxford_flowers102 import OxfordFlowers102Data tfds_dataset = OxfordFlowers102Data(data_dir=data_dir) elif dataset_name == "pets": from task_adaptation.data.oxford_iiit_pet import OxfordIIITPetData tfds_dataset = OxfordIIITPetData(data_dir=data_dir) classes = classnames["pets"] elif dataset_name == "pcam": from task_adaptation.data.patch_camelyon import PatchCamelyonData tfds_dataset = PatchCamelyonData(data_dir=data_dir) classes = classnames["pcam"] elif dataset_name == "resisc45": # Needs download from OneDrive: https://1drv.ms/u/s!AmgKYzARBl5ca3HNaHIlzp_IXjs # The archive needs to to be put at <DATASET_ROOT>/downloads/manual then extracted if not os.path.exists(data_dir): os.makedirs(os.path.join(data_dir, "downloads", "manual")) call(f"wget 'https://onedrive.live.com/download?resid=5C5E061130630A68!107&authkey=!AHHNaHIlzp_IXjs' --output-document={data_dir}/downloads/manual/resisc45.rar", shell=True) call(f"cd {data_dir}/downloads/manual;unrar x resisc45.rar", shell=True) from task_adaptation.data.resisc45 import Resisc45Data tfds_dataset = Resisc45Data(data_dir=data_dir) elif dataset_name.startswith("smallnorb_"): from task_adaptation.data.smallnorb import SmallNORBData task = _extract_task(dataset_name) assert task in ("label_category", "label_elevation", "label_azimuth", "label_lighting") tfds_dataset = SmallNORBData(predicted_attribute=task, data_dir=data_dir) classes = tfds_dataset._dataset_builder.info.features[task].names elif dataset_name == "sun397": from task_adaptation.data.sun397 import Sun397Data #FIXME There is a problem in `sun397`, when TFDS tries download it # there is an image that cannot be decoded. For the time being # we will use torchvision's SUN397 instead. tfds_dataset = Sun397Data(config="tfds", data_dir=data_dir) elif dataset_name == "svhn": from task_adaptation.data.svhn import SvhnData tfds_dataset = SvhnData(data_dir=data_dir) classes = classnames["svhn"] else: raise ValueError(f"Unsupported dataset: {dataset_name}") ds = VTABIterableDataset( tfds_dataset, input_name="image", label_name="label", transform=transform, target_transform=int, split=split, classes=classes, ) return ds def build_tfds_dataset(name, transform, download=True, split="test", data_dir="root", classes=None): from .tfds import disable_gpus_on_tensorflow disable_gpus_on_tensorflow() import tensorflow_datasets as tfds import timm builder = tfds.builder(name, data_dir=data_dir) if download: builder.download_and_prepare() splits = list(builder.info.splits.keys()) assert split in splits, (split, splits) ds = timm.data.create_dataset(f"tfds/{name}", data_dir, split=split, transform=transform, target_transform=int) ds.classes = builder.info.features['label'].names if classes is None else classes return ds def build_wds_dataset(dataset_name, transform, split="test", data_dir="root", cache_dir=None): """ Load a dataset in WebDataset format. Either local paths or HTTP URLs can be specified. Expected file structure is: ``` data_dir/ train/ nshards.txt 0.tar 1.tar ... test/ nshards.txt 0.tar 1.tar ... classnames.txt zeroshot_classification_templates.txt dataset_type.txt ``` Classnames and templates are required for zeroshot classification, while dataset type (equal to "retrieval") is required for zeroshot retrieval datasets. You can use the `clip_benchmark_export_wds` or corresponding API (`clip_benchmark.webdataset_builder.convert_dataset`) to convert datasets to this format. Set `cache_dir` to a path to cache the dataset, otherwise, no caching will occur. """ import webdataset as wds def read_txt(fname): if "://" in fname: stream = os.popen("curl -L -s --fail '%s'" % fname, "r") value = stream.read() if stream.close(): raise FileNotFoundError("Failed to retreive data") else: with open(fname, "r") as file: value = file.read() return value # Special handling for Huggingface datasets # Git LFS files have a different file path to access the raw data than other files if data_dir.startswith("https://huggingface.co/datasets"): # Format: https://huggingface.co/datasets/<USERNAME>/<REPO>/tree/<BRANCH> *split_url_head, _, url_path = data_dir.split("/", 7) url_head = "/".join(split_url_head) metadata_dir = "/".join([url_head, "raw", url_path]) tardata_dir = "/".join([url_head, "resolve", url_path]) else: metadata_dir = tardata_dir = data_dir # Get number of shards nshards_fname = os.path.join(metadata_dir, split, "nshards.txt") nshards = int(read_txt(nshards_fname)) # Do not catch FileNotFound, nshards.txt should be mandatory # Get dataset type (classification or retrieval) type_fname = os.path.join(metadata_dir, "dataset_type.txt") try: dataset_type = read_txt(type_fname).strip().lower() except FileNotFoundError: # print("WARNING: dataset_type.txt not found, assuming type=classification") dataset_type = "classification" # filepattern = os.path.join(tardata_dir, split, "{0..%d}.tar" % (nshards - 1)) # Load webdataset (support WEBP, PNG, and JPG for now) if not cache_dir or not isinstance(cache_dir, str): cache_dir = None dataset = ( wds.WebDataset(filepattern, cache_dir=cache_dir) .decode(wds.autodecode.ImageHandler("pil", extensions=["webp", "png", "jpg", "jpeg"])) ) # Load based on classification or retrieval task if dataset_type == "retrieval": dataset = (dataset .to_tuple(["webp", "png", "jpg", "jpeg"], "txt") .map_tuple(transform, str.splitlines) ) dataset.classes = dataset.templates = None else: label_type = "npy" if dataset_type == "multilabel" else "cls" # Special case for multilabel dataset = (dataset .to_tuple(["webp", "png", "jpg", "jpeg"], label_type) .map_tuple(transform, None) ) # Get class names if present classnames_fname = os.path.join(metadata_dir, "classnames.txt") try: dataset.classes = [line.strip() for line in read_txt(classnames_fname).splitlines() if line.strip()] except FileNotFoundError: print("WARNING: classnames.txt not found") dataset.classes = None # Get zeroshot classification templates if present templates_fname = os.path.join(metadata_dir, "zeroshot_classification_templates.txt") try: dataset.templates = [line.strip() for line in read_txt(templates_fname).splitlines() if line.strip()] except FileNotFoundError: print("WARNING: zeroshot_classification_templates.txt not found") dataset.templates = None return dataset def _extract_task(dataset_name): prefix, *task_name_list = dataset_name.split("_") task = "_".join(task_name_list) return task def image_captions_collate_fn(batch): transposed = list(zip(*batch)) imgs = default_collate(transposed[0]) texts = transposed[1] return imgs, texts def get_dataset_collection_from_file(path): return [l.strip() for l in open(path).readlines()] dataset_collection = { "vtab": [ "vtab/caltech101", "vtab/cifar100", "vtab/clevr_count_all", "vtab/clevr_closest_object_distance", "vtab/diabetic_retinopathy", "vtab/dmlab", "vtab/dsprites_label_orientation", "vtab/dsprites_label_x_position", "vtab/dtd", "vtab/eurosat", "vtab/kitti_closest_vehicle_distance", "vtab/flowers", "vtab/pets", "vtab/pcam", "vtab/resisc45", "vtab/smallnorb_label_azimuth", "vtab/smallnorb_label_elevation", "sun397", "vtab/svhn", ], "vtab+":[ "imagenet1k", "imagenetv2", "imagenet_sketch", "imagenet-a", "imagenet-r", "objectnet", "fer2013", "voc2007", "voc2007_multilabel", "sun397", "cars", "fgvc_aircraft", "mnist", "stl10", "gtsrb", "country211", "renderedsst2", "vtab/caltech101", "vtab/cifar10", "vtab/cifar100", "vtab/clevr_count_all", "vtab/clevr_closest_object_distance", "vtab/diabetic_retinopathy", "vtab/dmlab", "vtab/dsprites_label_orientation", "vtab/dsprites_label_x_position", "vtab/dtd", "vtab/eurosat", "vtab/kitti_closest_vehicle_distance", "vtab/flowers", "vtab/pets", "vtab/pcam", "vtab/resisc45", "vtab/smallnorb_label_azimuth", "vtab/smallnorb_label_elevation", "vtab/svhn", ], "retrieval": [ "mscoco_captions", "flickr8k", "flickr30k", ], "imagenet_robustness": [ "imagenetv2", "imagenet_sketch", "imagenet-a", "imagenet-r", "objectnet", ], } # use by imagenet robustness datasets all_imagenet_wordnet_ids = ['n01440764', 'n01443537', 'n01484850', 'n01491361', 'n01494475', 'n01496331', 'n01498041', 'n01514668', 'n01514859', 'n01518878', 'n01530575', 'n01531178', 'n01532829', 'n01534433', 'n01537544', 'n01558993', 'n01560419', 'n01580077', 'n01582220', 'n01592084', 'n01601694', 'n01608432', 'n01614925', 'n01616318', 'n01622779', 'n01629819', 'n01630670', 'n01631663', 'n01632458', 'n01632777', 'n01641577', 'n01644373', 'n01644900', 'n01664065', 'n01665541', 'n01667114', 'n01667778', 'n01669191', 'n01675722', 'n01677366', 'n01682714', 'n01685808', 'n01687978', 'n01688243', 'n01689811', 'n01692333', 'n01693334', 'n01694178', 'n01695060', 'n01697457', 'n01698640', 'n01704323', 'n01728572', 'n01728920', 'n01729322', 'n01729977', 'n01734418', 'n01735189', 'n01737021', 'n01739381', 'n01740131', 'n01742172', 'n01744401', 'n01748264', 'n01749939', 'n01751748', 'n01753488', 'n01755581', 'n01756291', 'n01768244', 'n01770081', 'n01770393', 'n01773157', 'n01773549', 'n01773797', 'n01774384', 'n01774750', 'n01775062', 'n01776313', 'n01784675', 'n01795545', 'n01796340', 'n01797886', 'n01798484', 'n01806143', 'n01806567', 'n01807496', 'n01817953', 'n01818515', 'n01819313', 'n01820546', 'n01824575', 'n01828970', 'n01829413', 'n01833805', 'n01843065', 'n01843383', 'n01847000', 'n01855032', 'n01855672', 'n01860187', 'n01871265', 'n01872401', 'n01873310', 'n01877812', 'n01882714', 'n01883070', 'n01910747', 'n01914609', 'n01917289', 'n01924916', 'n01930112', 'n01943899', 'n01944390', 'n01945685', 'n01950731', 'n01955084', 'n01968897', 'n01978287', 'n01978455', 'n01980166', 'n01981276', 'n01983481', 'n01984695', 'n01985128', 'n01986214', 'n01990800', 'n02002556', 'n02002724', 'n02006656', 'n02007558', 'n02009229', 'n02009912', 'n02011460', 'n02012849', 'n02013706', 'n02017213', 'n02018207', 'n02018795', 'n02025239', 'n02027492', 'n02028035', 'n02033041', 'n02037110', 'n02051845', 'n02056570', 'n02058221', 'n02066245', 'n02071294', 'n02074367', 'n02077923', 'n02085620', 'n02085782', 'n02085936', 'n02086079', 'n02086240', 'n02086646', 'n02086910', 'n02087046', 'n02087394', 'n02088094', 'n02088238', 'n02088364', 'n02088466', 'n02088632', 'n02089078', 'n02089867', 'n02089973', 'n02090379', 'n02090622', 'n02090721', 'n02091032', 'n02091134', 'n02091244', 'n02091467', 'n02091635', 'n02091831', 'n02092002', 'n02092339', 'n02093256', 'n02093428', 'n02093647', 'n02093754', 'n02093859', 'n02093991', 'n02094114', 'n02094258', 'n02094433', 'n02095314', 'n02095570', 'n02095889', 'n02096051', 'n02096177', 'n02096294', 'n02096437', 'n02096585', 'n02097047', 'n02097130', 'n02097209', 'n02097298', 'n02097474', 'n02097658', 'n02098105', 'n02098286', 'n02098413', 'n02099267', 'n02099429', 'n02099601', 'n02099712', 'n02099849', 'n02100236', 'n02100583', 'n02100735', 'n02100877', 'n02101006', 'n02101388', 'n02101556', 'n02102040', 'n02102177', 'n02102318', 'n02102480', 'n02102973', 'n02104029', 'n02104365', 'n02105056', 'n02105162', 'n02105251', 'n02105412', 'n02105505', 'n02105641', 'n02105855', 'n02106030', 'n02106166', 'n02106382', 'n02106550', 'n02106662', 'n02107142', 'n02107312', 'n02107574', 'n02107683', 'n02107908', 'n02108000', 'n02108089', 'n02108422', 'n02108551', 'n02108915', 'n02109047', 'n02109525', 'n02109961', 'n02110063', 'n02110185', 'n02110341', 'n02110627', 'n02110806', 'n02110958', 'n02111129', 'n02111277', 'n02111500', 'n02111889', 'n02112018', 'n02112137', 'n02112350', 'n02112706', 'n02113023', 'n02113186', 'n02113624', 'n02113712', 'n02113799', 'n02113978', 'n02114367', 'n02114548', 'n02114712', 'n02114855', 'n02115641', 'n02115913', 'n02116738', 'n02117135', 'n02119022', 'n02119789', 'n02120079', 'n02120505', 'n02123045', 'n02123159', 'n02123394', 'n02123597', 'n02124075', 'n02125311', 'n02127052', 'n02128385', 'n02128757', 'n02128925', 'n02129165', 'n02129604', 'n02130308', 'n02132136', 'n02133161', 'n02134084', 'n02134418', 'n02137549', 'n02138441', 'n02165105', 'n02165456', 'n02167151', 'n02168699', 'n02169497', 'n02172182', 'n02174001', 'n02177972', 'n02190166', 'n02206856', 'n02219486', 'n02226429', 'n02229544', 'n02231487', 'n02233338', 'n02236044', 'n02256656', 'n02259212', 'n02264363', 'n02268443', 'n02268853', 'n02276258', 'n02277742', 'n02279972', 'n02280649', 'n02281406', 'n02281787', 'n02317335', 'n02319095', 'n02321529', 'n02325366', 'n02326432', 'n02328150', 'n02342885', 'n02346627', 'n02356798', 'n02361337', 'n02363005', 'n02364673', 'n02389026', 'n02391049', 'n02395406', 'n02396427', 'n02397096', 'n02398521', 'n02403003', 'n02408429', 'n02410509', 'n02412080', 'n02415577', 'n02417914', 'n02422106', 'n02422699', 'n02423022', 'n02437312', 'n02437616', 'n02441942', 'n02442845', 'n02443114', 'n02443484', 'n02444819', 'n02445715', 'n02447366', 'n02454379', 'n02457408', 'n02480495', 'n02480855', 'n02481823', 'n02483362', 'n02483708', 'n02484975', 'n02486261', 'n02486410', 'n02487347', 'n02488291', 'n02488702', 'n02489166', 'n02490219', 'n02492035', 'n02492660', 'n02493509', 'n02493793', 'n02494079', 'n02497673', 'n02500267', 'n02504013', 'n02504458', 'n02509815', 'n02510455', 'n02514041', 'n02526121', 'n02536864', 'n02606052', 'n02607072', 'n02640242', 'n02641379', 'n02643566', 'n02655020', 'n02666196', 'n02667093', 'n02669723', 'n02672831', 'n02676566', 'n02687172', 'n02690373', 'n02692877', 'n02699494', 'n02701002', 'n02704792', 'n02708093', 'n02727426', 'n02730930', 'n02747177', 'n02749479', 'n02769748', 'n02776631', 'n02777292', 'n02782093', 'n02783161', 'n02786058', 'n02787622', 'n02788148', 'n02790996', 'n02791124', 'n02791270', 'n02793495', 'n02794156', 'n02795169', 'n02797295', 'n02799071', 'n02802426', 'n02804414', 'n02804610', 'n02807133', 'n02808304', 'n02808440', 'n02814533', 'n02814860', 'n02815834', 'n02817516', 'n02823428', 'n02823750', 'n02825657', 'n02834397', 'n02835271', 'n02837789', 'n02840245', 'n02841315', 'n02843684', 'n02859443', 'n02860847', 'n02865351', 'n02869837', 'n02870880', 'n02871525', 'n02877765', 'n02879718', 'n02883205', 'n02892201', 'n02892767', 'n02894605', 'n02895154', 'n02906734', 'n02909870', 'n02910353', 'n02916936', 'n02917067', 'n02927161', 'n02930766', 'n02939185', 'n02948072', 'n02950826', 'n02951358', 'n02951585', 'n02963159', 'n02965783', 'n02966193', 'n02966687', 'n02971356', 'n02974003', 'n02977058', 'n02978881', 'n02979186', 'n02980441', 'n02981792', 'n02988304', 'n02992211', 'n02992529', 'n02999410', 'n03000134', 'n03000247', 'n03000684', 'n03014705', 'n03016953', 'n03017168', 'n03018349', 'n03026506', 'n03028079', 'n03032252', 'n03041632', 'n03042490', 'n03045698', 'n03047690', 'n03062245', 'n03063599', 'n03063689', 'n03065424', 'n03075370', 'n03085013', 'n03089624', 'n03095699', 'n03100240', 'n03109150', 'n03110669', 'n03124043', 'n03124170', 'n03125729', 'n03126707', 'n03127747', 'n03127925', 'n03131574', 'n03133878', 'n03134739', 'n03141823', 'n03146219', 'n03160309', 'n03179701', 'n03180011', 'n03187595', 'n03188531', 'n03196217', 'n03197337', 'n03201208', 'n03207743', 'n03207941', 'n03208938', 'n03216828', 'n03218198', 'n03220513', 'n03223299', 'n03240683', 'n03249569', 'n03250847', 'n03255030', 'n03259280', 'n03271574', 'n03272010', 'n03272562', 'n03290653', 'n03291819', 'n03297495', 'n03314780', 'n03325584', 'n03337140', 'n03344393', 'n03345487', 'n03347037', 'n03355925', 'n03372029', 'n03376595', 'n03379051', 'n03384352', 'n03388043', 'n03388183', 'n03388549', 'n03393912', 'n03394916', 'n03400231', 'n03404251', 'n03417042', 'n03424325', 'n03425413', 'n03443371', 'n03444034', 'n03445777', 'n03445924', 'n03447447', 'n03447721', 'n03450230', 'n03452741', 'n03457902', 'n03459775', 'n03461385', 'n03467068', 'n03476684', 'n03476991', 'n03478589', 'n03481172', 'n03482405', 'n03483316', 'n03485407', 'n03485794', 'n03492542', 'n03494278', 'n03495258', 'n03496892', 'n03498962', 'n03527444', 'n03529860', 'n03530642', 'n03532672', 'n03534580', 'n03535780', 'n03538406', 'n03544143', 'n03584254', 'n03584829', 'n03590841', 'n03594734', 'n03594945', 'n03595614', 'n03598930', 'n03599486', 'n03602883', 'n03617480', 'n03623198', 'n03627232', 'n03630383', 'n03633091', 'n03637318', 'n03642806', 'n03649909', 'n03657121', 'n03658185', 'n03661043', 'n03662601', 'n03666591', 'n03670208', 'n03673027', 'n03676483', 'n03680355', 'n03690938', 'n03691459', 'n03692522', 'n03697007', 'n03706229', 'n03709823', 'n03710193', 'n03710637', 'n03710721', 'n03717622', 'n03720891', 'n03721384', 'n03724870', 'n03729826', 'n03733131', 'n03733281', 'n03733805', 'n03742115', 'n03743016', 'n03759954', 'n03761084', 'n03763968', 'n03764736', 'n03769881', 'n03770439', 'n03770679', 'n03773504', 'n03775071', 'n03775546', 'n03776460', 'n03777568', 'n03777754', 'n03781244', 'n03782006', 'n03785016', 'n03786901', 'n03787032', 'n03788195', 'n03788365', 'n03791053', 'n03792782', 'n03792972', 'n03793489', 'n03794056', 'n03796401', 'n03803284', 'n03804744', 'n03814639', 'n03814906', 'n03825788', 'n03832673', 'n03837869', 'n03838899', 'n03840681', 'n03841143', 'n03843555', 'n03854065', 'n03857828', 'n03866082', 'n03868242', 'n03868863', 'n03871628', 'n03873416', 'n03874293', 'n03874599', 'n03876231', 'n03877472', 'n03877845', 'n03884397', 'n03887697', 'n03888257', 'n03888605', 'n03891251', 'n03891332', 'n03895866', 'n03899768', 'n03902125', 'n03903868', 'n03908618', 'n03908714', 'n03916031', 'n03920288', 'n03924679', 'n03929660', 'n03929855', 'n03930313', 'n03930630', 'n03933933', 'n03935335', 'n03937543', 'n03938244', 'n03942813', 'n03944341', 'n03947888', 'n03950228', 'n03954731', 'n03956157', 'n03958227', 'n03961711', 'n03967562', 'n03970156', 'n03976467', 'n03976657', 'n03977966', 'n03980874', 'n03982430', 'n03983396', 'n03991062', 'n03992509', 'n03995372', 'n03998194', 'n04004767', 'n04005630', 'n04008634', 'n04009552', 'n04019541', 'n04023962', 'n04026417', 'n04033901', 'n04033995', 'n04037443', 'n04039381', 'n04040759', 'n04041544', 'n04044716', 'n04049303', 'n04065272', 'n04067472', 'n04069434', 'n04070727', 'n04074963', 'n04081281', 'n04086273', 'n04090263', 'n04099969', 'n04111531', 'n04116512', 'n04118538', 'n04118776', 'n04120489', 'n04125021', 'n04127249', 'n04131690', 'n04133789', 'n04136333', 'n04141076', 'n04141327', 'n04141975', 'n04146614', 'n04147183', 'n04149813', 'n04152593', 'n04153751', 'n04154565', 'n04162706', 'n04179913', 'n04192698', 'n04200800', 'n04201297', 'n04204238', 'n04204347', 'n04208210', 'n04209133', 'n04209239', 'n04228054', 'n04229816', 'n04235860', 'n04238763', 'n04239074', 'n04243546', 'n04251144', 'n04252077', 'n04252225', 'n04254120', 'n04254680', 'n04254777', 'n04258138', 'n04259630', 'n04263257', 'n04264628', 'n04265275', 'n04266014', 'n04270147', 'n04273569', 'n04275548', 'n04277352', 'n04285008', 'n04286575', 'n04296562', 'n04310018', 'n04311004', 'n04311174', 'n04317175', 'n04325704', 'n04326547', 'n04328186', 'n04330267', 'n04332243', 'n04335435', 'n04336792', 'n04344873', 'n04346328', 'n04347754', 'n04350905', 'n04355338', 'n04355933', 'n04356056', 'n04357314', 'n04366367', 'n04367480', 'n04370456', 'n04371430', 'n04371774', 'n04372370', 'n04376876', 'n04380533', 'n04389033', 'n04392985', 'n04398044', 'n04399382', 'n04404412', 'n04409515', 'n04417672', 'n04418357', 'n04423845', 'n04428191', 'n04429376', 'n04435653', 'n04442312', 'n04443257', 'n04447861', 'n04456115', 'n04458633', 'n04461696', 'n04462240', 'n04465501', 'n04467665', 'n04476259', 'n04479046', 'n04482393', 'n04483307', 'n04485082', 'n04486054', 'n04487081', 'n04487394', 'n04493381', 'n04501370', 'n04505470', 'n04507155', 'n04509417', 'n04515003', 'n04517823', 'n04522168', 'n04523525', 'n04525038', 'n04525305', 'n04532106', 'n04532670', 'n04536866', 'n04540053', 'n04542943', 'n04548280', 'n04548362', 'n04550184', 'n04552348', 'n04553703', 'n04554684', 'n04557648', 'n04560804', 'n04562935', 'n04579145', 'n04579432', 'n04584207', 'n04589890', 'n04590129', 'n04591157', 'n04591713', 'n04592741', 'n04596742', 'n04597913', 'n04599235', 'n04604644', 'n04606251', 'n04612504', 'n04613696', 'n06359193', 'n06596364', 'n06785654', 'n06794110', 'n06874185', 'n07248320', 'n07565083', 'n07579787', 'n07583066', 'n07584110', 'n07590611', 'n07613480', 'n07614500', 'n07615774', 'n07684084', 'n07693725', 'n07695742', 'n07697313', 'n07697537', 'n07711569', 'n07714571', 'n07714990', 'n07715103', 'n07716358', 'n07716906', 'n07717410', 'n07717556', 'n07718472', 'n07718747', 'n07720875', 'n07730033', 'n07734744', 'n07742313', 'n07745940', 'n07747607', 'n07749582', 'n07753113', 'n07753275', 'n07753592', 'n07754684', 'n07760859', 'n07768694', 'n07802026', 'n07831146', 'n07836838', 'n07860988', 'n07871810', 'n07873807', 'n07875152', 'n07880968', 'n07892512', 'n07920052', 'n07930864', 'n07932039', 'n09193705', 'n09229709', 'n09246464', 'n09256479', 'n09288635', 'n09332890', 'n09399592', 'n09421951', 'n09428293', 'n09468604', 'n09472597', 'n09835506', 'n10148035', 'n10565667', 'n11879895', 'n11939491', 'n12057211', 'n12144580', 'n12267677', 'n12620546', 'n12768682', 'n12985857', 'n12998815', 'n13037406', 'n13040303', 'n13044778', 'n13052670', 'n13054560', 'n13133613', 'n15075141']
[ "zeroshot_classification_templates.txt", "imagenet1k", "None" ]
2024-01-10
codermanz/ActivityRecommenderAI
placesRecommendationAPI~api~third_party_api_adapters.py
import openai import os from metaphor_python import Metaphor from dotenv import load_dotenv from bs4 import BeautifulSoup import re from .prompts import * load_dotenv() # Load in API keys from OpenAI and Metaphor openai.api_key = os.getenv("OPENAI_API_KEY") metaphor = Metaphor(os.getenv("METAPHOR_API_KEY")) def extract_bullet_points(text): """ Given a string of text, extract data from just the bullet points and return each bullet as an item in a list. :param text: A paragraph/string of text that has some bullet points in it. :return: List of bullet points. Returns an empty list if no bullet points are found. """ # Regular expression pattern to identify bullet points bullet_point_pattern = re.compile(r'(?:\d+\.)?\s*[-*+]\s*(.*)|^\d+\.\s*(.*)', re.MULTILINE) # Find all matches in the input text matches = bullet_point_pattern.findall(text) # Extract the matched bullet points, taking non-empty matching group bullet_points = [match[0] if match[1] == '' else match[1] for match in matches] return bullet_points def truncate_to_tokens(input_string, max_tokens=250): """ Truncate the input string to keep only the first `max_tokens` tokens. :param input_string: The input text to be truncated. :param max_tokens: The maximum number of tokens to keep. :return: Truncated string. """ tokens = input_string.split() truncated_tokens = tokens[:max_tokens] truncated_string = ' '.join(truncated_tokens) return truncated_string def fetch_metaphor_results(inputs): """ Given a dictionary of inputs, return a list of results from Metaphor. Returned list will be a list of "DocumentContent" objects. This will include title, id, content, and url. :param inputs: :return: """ # Get suggestions directly from Metaphor prompt = metaphor_direct_suggestions_prompt(inputs) response = metaphor.search( query=prompt, use_autoprompt=False, start_published_date="2019-09-01", num_results=5, type='neural' ) # Get content for each result contents = metaphor.get_contents(ids=[result.id for result in response.results]) return contents.contents def fetch_suggestions_from_gpt(inputs): """ Given a dictionary of inputs, return a list of results from GPT-3. Returned list will be a list of :param inputs: :return: """ # Get suggestions from GPT-3 prompt_for_gpt = gpt_direct_suggestions_prompt(inputs) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": gpt_direct_suggestions_system_message }, { "role": "user", "content": prompt_for_gpt } ], temperature=1, max_tokens=405, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract bullet points from GPT-3 response and for each bullet point, search Metaphor for a localized conrete # suggestion return_list = [] for item in extract_bullet_points(response["choices"][0]["message"]["content"]): # Search metaphor - get only one result (saves some content links_to_recommendations = metaphor.search( query=item + "in " + inputs["location"], use_autoprompt=True, num_results=1, type='neural' ) # Get content for each result content_for_link = metaphor.get_contents(ids=[result.id for result in links_to_recommendations.results]) return_list += content_for_link.contents return return_list def summarize_each_suggestion(list_of_suggestions): """ Given a list of suggestions (DocumentContent objects), summarize each suggestion's content using GPT-3. Then return the same list of suggestions with the content replaced with the summarized content. :param list_of_suggestions: :return: """ # Create a single summary of contents to feed into a single prompt summary_contents = "" i = 1 for content in list_of_suggestions: content.extract = truncate_to_tokens(BeautifulSoup(content.extract, "html.parser"). get_text(separator=" ", strip=True)) summary_contents += f"{str(i)}. {content.title} + content.extract\n" i += 1 # Prompt GPT to summarize each suggestion's content gpt_excerpts_summarization_response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": gpt_rewrite_excerpt_system_message }, { "role": "user", "content": summary_contents } ], temperature=1, max_tokens=8096, top_p=1, frequency_penalty=0, presence_penalty=0 ) # Extract summarized suggestion contents from GPT response list_of_summarized_excerpts = extract_bullet_points( gpt_excerpts_summarization_response["choices"][0]["message"]["content"]) # Split all bullet points into their own list item and assign to each suggestion for i in range(len(list_of_summarized_excerpts)): list_of_suggestions[i].extract = list_of_summarized_excerpts[i] return list_of_suggestions def summarize_all_suggestions(list_of_suggestions, inputs): """ Given a list of suggestions (DocumentContent objects), summarize all suggestions into a single summary using GPT-3. Prompt is informed by user inputs. :param list_of_suggestions: :param inputs: :return: """ summarization_prompt = "" i = 1 # Create a single prompt from all the DocumentContent objects # We limit the number of words from each extract due to context window (token) limits from GPT. for suggestion in list_of_suggestions: summarization_prompt += str(i) + ". Title: " + suggestion.title + " Extract: " + suggestion.extract + "\n" i += 1 final_prompt = gpt_output_recommendations_prompt(inputs, summarization_prompt) # Call GPT to summarize summary_of_activities = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": gpt_summarize_activities_system_message }, { "role": "user", "content": final_prompt } ], temperature=1, max_tokens=2096, top_p=1, frequency_penalty=0, presence_penalty=0 ) return summary_of_activities["choices"][0]["message"]["content"] def execute_searching_workflow(inputs): """ Executes the whole workflow of searching for suggestions. Returns a tuple of the list of suggestions and the overall summary of the suggestions. NOTE: The returned list will be DocumentContents objects :param inputs: :return: """ # Get suggestions directly from Metaphor (returns a list of DocumentContents) suggestion_results = fetch_metaphor_results(inputs) # Append suggestions from GPT-3 that're informed by Metaphor's search results (returns a list of DocumentContents) suggestion_results += fetch_suggestions_from_gpt(inputs) # Summarize each suggestion's content using GPT-3 and update DocumentContents list suggestion_results = summarize_each_suggestion(suggestion_results) # Summarize all suggestions into a single summary overall_summary = summarize_all_suggestions(suggestion_results, inputs) return suggestion_results, overall_summary
[ ". Title: ", "\n", " Extract: " ]
2024-01-10
turian/autolabel
tests~unit~test_few_shot.py
import json from autolabel.configs import AutolabelConfig from autolabel.dataset_loader import DatasetLoader from autolabel.few_shot import ExampleSelectorFactory from langchain.embeddings import HuggingFaceEmbeddings from pytest import approx BANKING_HF_EMBEDDINGS_CONFIG = json.load( open("tests/assets/banking/config_banking_hf_embeddings.json", "r") ) BANKING_CONFIG = json.load(open("tests/assets/banking/config_banking.json", "r")) def test_embedding_provider(): config = AutolabelConfig(BANKING_HF_EMBEDDINGS_CONFIG) seed_examples = config.few_shot_example_set() dataset_loader = DatasetLoader("tests/assets/banking/test.csv", config, 5, 0) seed_loader = DatasetLoader(seed_examples, config) seed_examples = seed_loader.inputs example_selector = ExampleSelectorFactory.initialize_selector( config, seed_examples, dataset_loader.dat.keys().tolist() ) assert isinstance( example_selector.vectorstore._embedding_function, HuggingFaceEmbeddings ) def test_embedding_provider_config_exists(): config = AutolabelConfig(BANKING_HF_EMBEDDINGS_CONFIG) embedding_provider = config.embedding_provider() assert embedding_provider == "huggingface_pipeline" def test_embedding_provider_config_default(): config = AutolabelConfig(BANKING_CONFIG) embedding_provider = config.embedding_provider() assert embedding_provider == "openai"
[]
2024-01-10
turian/autolabel
src~autolabel~models~hf_pipeline.py
from typing import List, Optional from langchain.llms import HuggingFacePipeline from langchain.schema import LLMResult, Generation from autolabel.models import BaseModel from autolabel.configs import AutolabelConfig from autolabel.cache import BaseCache class HFPipelineLLM(BaseModel): DEFAULT_MODEL = "google/flan-t5-xxl" DEFAULT_PARAMS = {"temperature": 0.0, "quantize": 8} def __init__(self, config: AutolabelConfig, cache: BaseCache = None) -> None: try: from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline except ImportError: raise ValueError( "Could not import transformers python package. " "Please it install it with `pip install transformers`." ) try: import torch except ImportError: raise ValueError( "Could not import torch package. " "Please it install it with `pip install torch`." ) super().__init__(config, cache) # populate model name self.model_name = config.model_name() or self.DEFAULT_MODEL # populate model params model_params = config.model_params() self.model_params = {**self.DEFAULT_PARAMS, **model_params} # initialize HF pipeline tokenizer = AutoTokenizer.from_pretrained(self.model_name) quantize_bits = self.model_params["quantize"] if not torch.cuda.is_available(): model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name) elif quantize_bits == 8: model = AutoModelForSeq2SeqLM.from_pretrained( self.model_name, load_in_8bit=True, device_map="auto" ) elif quantize_bits == "16": model = AutoModelForSeq2SeqLM.from_pretrained( self.model_name, torch_dtype=torch.float16, device_map="auto" ) else: model = AutoModelForSeq2SeqLM.from_pretrained( self.model_name, device_map="auto" ) model_kwargs = dict(self.model_params) # make a copy of the model params model_kwargs.pop("quantize", None) # remove quantize from the model params pipe = pipeline( "text2text-generation", model=model, tokenizer=tokenizer, **model_kwargs, ) # initialize LLM self.llm = HuggingFacePipeline(pipeline=pipe, model_kwargs=model_kwargs) def _label(self, prompts: List[str]) -> LLMResult: try: return self.llm.generate(prompts) except Exception as e: print(f"Error generating from LLM: {e}, returning empty result") generations = [[Generation(text="")] for _ in prompts] return LLMResult(generations=generations) def get_cost(self, prompt: str, label: Optional[str] = "") -> float: # Model inference for this model is being run locally # Revisit this in the future when we support HF inference endpoints return 0.0 def returns_token_probs(self) -> bool: return False
[]
2024-01-10
turian/autolabel
src~autolabel~tasks~multilabel_classification.py
from collections import defaultdict from typing import List, Dict, Tuple from langchain.prompts.prompt import PromptTemplate from sklearn.metrics import accuracy_score, f1_score from sklearn.preprocessing import MultiLabelBinarizer from autolabel.confidence import ConfidenceCalculator from autolabel.configs import AutolabelConfig from autolabel.schema import LLMAnnotation, Metric, MetricResult from autolabel.tasks import BaseTask from autolabel.utils import get_format_variables import json class MultilabelClassificationTask(BaseTask): DEFAULT_OUTPUT_GUIDELINES = 'You will return the answer as a semicolon-separated list of labels. For example: "label1;label2;label3"' DEFAULT_TASK_GUIDELINES = "Your job is to correctly label the provided input example into one or more of the following {num_labels} categories.\nCategories:\n{labels}\n" GENERATE_EXPLANATION_PROMPT = "You are an expert at providing a well reasoned explanation for the output of a given task. \n\nBEGIN TASK DESCRIPTION\n{task_guidelines}\nEND TASK DESCRIPTION\nYou will be given an input example and the corresponding output. Your job is to provide an explanation for why the output is correct for the task above.\nThink step by step and generate an explanation. The last line of the explanation should be - So, the answer is <label>.\n{labeled_example}\nExplanation: " def __init__(self, config: AutolabelConfig) -> None: super().__init__(config) def construct_prompt(self, input: Dict, examples: List) -> str: # Copy over the input so that we can modify it input = input.copy() # prepare task guideline labels_list = self.config.labels_list() num_labels = len(labels_list) fmt_task_guidelines = self.task_guidelines.format( num_labels=num_labels, labels="\n".join(labels_list) ) # prepare seed examples example_template = self.config.example_template() label_column = self.config.label_column() fmt_examples = [] for eg in examples: eg_copy = eg.copy() # If chain of thought is enabled if label_column and self.config.chain_of_thought(): eg_copy[label_column] = json.dumps({label_column: eg[label_column]}) fmt_examples.append(example_template.format_map(defaultdict(str, eg_copy))) # populate the current example in the prompt if label_column: input[label_column] = "" # populate the explanation column with empty string for current example explanation_column = self.config.explanation_column() if explanation_column: input[explanation_column] = "" # populate the current example in the prompt current_example = example_template.format_map(defaultdict(str, input)) if self._is_few_shot_mode(): return self.prompt_template.format( task_guidelines=fmt_task_guidelines, output_guidelines=self.output_guidelines, seed_examples="\n\n".join(fmt_examples), current_example=current_example, ) else: return self.prompt_template.format( task_guidelines=fmt_task_guidelines, output_guidelines=self.output_guidelines, current_example=current_example, ) def get_explanation_prompt(self, example: Dict) -> str: pt = PromptTemplate( input_variables=get_format_variables(self.GENERATE_EXPLANATION_PROMPT), template=self.GENERATE_EXPLANATION_PROMPT, ) # prepare task guideline labels_list = self.config.labels_list() num_labels = len(labels_list) fmt_task_guidelines = self.task_guidelines.format( num_labels=num_labels, labels="\n".join(labels_list) ) # prepare labeled example example_template = self.config.example_template() fmt_example = example_template.format_map(defaultdict(str, example)) return pt.format( task_guidelines=fmt_task_guidelines, labeled_example=fmt_example, ) def auroc_score_labels( self, gt_labels, llm_labels ) -> Tuple[List[int], List[float]]: labels = [] confidences = [] for index, llm_label in enumerate(llm_labels): labels.append(llm_label.label.lower() == gt_labels[index].lower()) confidences.append(llm_label.confidence_score) return labels, confidences def get_labels_predictions_with_threshold(self, gt_labels, llm_labels, threshold): answered_gt_labels, answered_llm_preds = [], [] for index, l in enumerate(llm_labels): if l.label != self.NULL_LABEL_TOKEN and ( l.confidence_score is None or l.confidence_score >= threshold ): answered_llm_preds.append(l.label.lower()) answered_gt_labels.append(gt_labels[index].lower()) return answered_gt_labels, answered_llm_preds def eval( self, llm_labels: List[LLMAnnotation], gt_labels: List[str] ) -> List[MetricResult]: """Evaluate the LLM generated labels by comparing them against ground truth Args: llm_labels (List[LLMAnnotation]): list of LLM generated labels gt_labels (List[str]): list of ground truth labels Returns: List[MetricResult]: list of metrics and corresponding values """ eval_metrics_map = { Metric.F1: [], Metric.SUPPORT: [], Metric.ACCURACY: [], Metric.COMPLETION_RATE: [], } eval_metrics = [] thresholds = [] if self.config.confidence(): eval_metrics_map[Metric.THRESHOLD] = [] labels, confidences = self.auroc_score_labels(gt_labels, llm_labels) value, meaningful_thresholds = ConfidenceCalculator.compute_auroc( labels, confidences ) thresholds.extend(meaningful_thresholds) eval_metrics.append( MetricResult( metric_type=Metric.AUROC, name="auroc", value=value, ) ) else: thresholds.append(float("-inf")) for index, threshold in enumerate(thresholds): ( curr_gt_labels, curr_llm_labels, ) = self.get_labels_predictions_with_threshold( gt_labels, llm_labels, threshold ) if len(gt_labels) > 0: eval_metrics_map[Metric.COMPLETION_RATE].append( len(curr_gt_labels) / float(len(gt_labels)) ) eval_metrics_map[Metric.SUPPORT].append(len(curr_gt_labels)) if len(curr_gt_labels) > 0: eval_metrics_map[Metric.ACCURACY].append( accuracy_score(curr_gt_labels, curr_llm_labels) ) else: eval_metrics_map[Metric.ACCURACY].append(0.0) if self.config.confidence(): eval_metrics_map[Metric.THRESHOLD].append(threshold) def binarize_labels(curr_labels): """Generate multilabel array from ground truth and LLM labels""" mlb = MultiLabelBinarizer() mlb.fit([self.config.labels_list()]) return mlb.transform( [x.split(self.config.label_separator()) for x in curr_labels] ) eval_metrics_map[Metric.F1].append( f1_score( binarize_labels(curr_gt_labels), binarize_labels(curr_llm_labels), average="macro", zero_division=0, ) ) eval_metrics.extend( [ MetricResult( metric_type=i, name=i.value, value=eval_metrics_map[i], ) for i in eval_metrics_map.keys() ] ) return eval_metrics
[ "You are an expert at providing a well reasoned explanation for the output of a given task. \n\nBEGIN TASK DESCRIPTION\n{task_guidelines}\nEND TASK DESCRIPTION\nYou will be given an input example and the corresponding output. Your job is to provide an explanation for why the output is correct for the task above.\nThink step by step and generate an explanation. The last line of the explanation should be - So, the answer is <label>.\n{labeled_example}\nExplanation: " ]
2024-01-10
turian/autolabel
tests~unit~tasks_test.py
import copy import json from autolabel.tasks import ( ClassificationTask, EntityMatchingTask, QuestionAnsweringTask, MultilabelClassificationTask, ) from autolabel.configs import AutolabelConfig from autolabel.schema import LLMAnnotation, Metric from langchain.schema import Generation BANKING_CONFIG = json.load(open("tests/assets/banking/config_banking.json", "r")) WALMART_AMAZON_CONFIG = json.load( open("tests/assets/walmart_amazon/config_walmart_amazon.json", "r") ) SCIQ_CONFIG = json.load(open("tests/assets/sciq/config_sciq.json", "r")) TWITTER_EMOTION_DETECTION_CONFIG = json.load( open( "tests/assets/twitter_emotion_detection/config_twitter_emotion_detection.json", "r", ) ) def test_classification_construct_prompt(): config = AutolabelConfig(BANKING_CONFIG) task = ClassificationTask(config=config) assert task.config != None input = {"example": "Here is an example", "label": "label-true"} examples = [ {"example": "Here is a seed example", "label": "label1"}, {"example": "Here is another seed example", "label": "label2"}, ] prompt = task.construct_prompt(input, examples) assert BANKING_CONFIG["prompt"]["output_guidelines"] in prompt assert "\n".join(BANKING_CONFIG["prompt"]["labels"]) in prompt assert input["example"] in prompt assert input["label"] not in prompt for example in examples: assert example["example"] in prompt assert example["label"] in prompt new_config = copy.deepcopy(BANKING_CONFIG) del new_config["prompt"]["few_shot_selection"] new_config = AutolabelConfig(new_config) task = ClassificationTask(config=new_config) prompt = task.construct_prompt(input, examples) for example in examples: assert example["example"] not in prompt assert example["label"] not in prompt def test_classification_parse_llm_response(): new_config = copy.deepcopy(BANKING_CONFIG) new_config["prompt"]["labels"].append("label-true") new_config = AutolabelConfig(new_config) task = ClassificationTask(config=new_config) input = {"example": "Here is an example", "label": "label-true"} prompt = "This is a prompt" label = "This is the thing we want to test.\nlabel-true" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == "label-true" assert parsed.successfully_labeled == True assert parsed.raw_response == label label = "" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == task.NULL_LABEL_TOKEN assert parsed.successfully_labeled == False def test_classification_eval(): config = AutolabelConfig(BANKING_CONFIG) task = ClassificationTask(config=config) llm_labels = [ LLMAnnotation( successfully_labeled=True, label="label1", ), LLMAnnotation( successfully_labeled=False, label=task.NULL_LABEL_TOKEN, ), LLMAnnotation( successfully_labeled=True, label="label3-wrong", ), LLMAnnotation( successfully_labeled=True, label="label4", ), LLMAnnotation( successfully_labeled=True, label="label5-wrong", ), ] gt_labels = ["label1", "label2", "label3", "label4", "label5"] eval = task.eval(llm_labels, gt_labels) for metric in eval: if metric.metric_type == Metric.ACCURACY: assert metric.value[0] == 0.5 elif metric.metric_type == Metric.COMPLETION_RATE: assert metric.value[0] == 0.8 elif metric.metric_type == Metric.SUPPORT: assert metric.value[0] == 4 def test_entity_matching_construct_prompt(): config = AutolabelConfig(WALMART_AMAZON_CONFIG) task = EntityMatchingTask(config=config) assert task.config != None input = { "Title_entity1": "lexmark extra high yield return pgm print cartridge - magenta", "Category_entity1": "printers", "Brand_entity1": "lexmark", "ModelNo_entity1": "c782u1mg", "Price_entity1": "214.88", "Title_entity2": "lexmark 18c1428 return program print cartridge black", "Category_entity2": "inkjet printer ink", "Brand_entity2": "lexmark", "ModelNo_entity2": "18c1428", "Price_entity2": "19.97", "label": "not duplicate - 1", } examples = [ { "Title_entity1": "lexmark extra high yield return pgm print cartridge - magentaplus", "Category_entity1": "printers", "Brand_entity1": "lexmark", "ModelNo_entity1": "c782u1mg", "Price_entity1": "214.88", "Title_entity2": "lexmark 18c1428 return program print cartridge black", "Category_entity2": "inkjet printer ink", "Brand_entity2": "lexmark", "ModelNo_entity2": "18c1428", "Price_entity2": "19.97", "label": "not duplicate - 2", }, { "Title_entity1": "edge tech proshot 4gb sdhc class 6 memory card", "Category_entity1": "usb drives", "Brand_entity1": "edge tech", "ModelNo_entity1": "pe209780", "Price_entity1": "10.88", "Title_entity2": "4gb edge proshot sdhc memory card class6", "Category_entity2": "computers accessories", "Brand_entity2": "edge", "ModelNo_entity2": "nan", "Price_entity2": "17.83", "label": "duplicate - 3", }, ] prompt = task.construct_prompt(input, examples) assert "\n".join(WALMART_AMAZON_CONFIG["prompt"]["labels"]) in prompt assert input["Title_entity1"] in prompt assert input["Category_entity1"] in prompt assert input["label"] not in prompt for example in examples: assert example["Title_entity1"] in prompt assert example["label"] in prompt new_config = copy.deepcopy(WALMART_AMAZON_CONFIG) del new_config["prompt"]["few_shot_selection"] new_config = AutolabelConfig(new_config) task = EntityMatchingTask(config=new_config) prompt = task.construct_prompt(input, examples) for example in examples: assert example["Title_entity1"] not in prompt def test_entity_matching_parse_llm_response(): new_config = copy.deepcopy(WALMART_AMAZON_CONFIG) new_config = AutolabelConfig(new_config) new_config["prompt"]["labels"].append("not duplicate - 1") task = EntityMatchingTask(config=new_config) input = { "Title_entity1": "lexmark extra high yield return pgm print cartridge - magenta", "Category_entity1": "printers", "Brand_entity1": "lexmark", "ModelNo_entity1": "c782u1mg", "Price_entity1": "214.88", "Title_entity2": "lexmark 18c1428 return program print cartridge black", "Category_entity2": "inkjet printer ink", "Brand_entity2": "lexmark", "ModelNo_entity2": "18c1428", "Price_entity2": "19.97", "label": "not duplicate - 1", } prompt = "This is a prompt" label = "This is the thing we want to test.\nnot duplicate - 1" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == "not duplicate - 1" assert parsed.successfully_labeled == True assert parsed.raw_response == label label = "" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == task.NULL_LABEL_TOKEN assert parsed.successfully_labeled == False def test_entity_matching_eval(): config = AutolabelConfig(WALMART_AMAZON_CONFIG) task = EntityMatchingTask(config=config) llm_labels = [ LLMAnnotation( successfully_labeled=True, label="duplicate", ), LLMAnnotation( successfully_labeled=False, label=task.NULL_LABEL_TOKEN, ), LLMAnnotation( successfully_labeled=True, label="not duplicate", ), LLMAnnotation( successfully_labeled=True, label="not duplicate", ), LLMAnnotation( successfully_labeled=True, label="duplicate", ), ] gt_labels = [ "duplicate", "not duplicate", "not duplicate", "not duplicate", "not duplicate", ] eval = task.eval(llm_labels, gt_labels) for metric in eval: if metric.metric_type == Metric.ACCURACY: assert metric.value[0] == 0.75 elif metric.metric_type == Metric.COMPLETION_RATE: assert metric.value[0] == 0.8 elif metric.metric_type == Metric.SUPPORT: assert metric.value[0] == 4 def question_answering_construct_prompt(): config = AutolabelConfig(SCIQ_CONFIG) task = QuestionAnsweringTask(config=config) assert task.config != None input = { "question": "What is the capital of France?", "options": "[Paris, London, Berlin]", "answer": "Paris-label", } examples = [ { "question": "What is the capital of India?", "options": "[Delhi, Mumbai, Bangalore]", "answer": "Delhi-label", }, { "question": "What is the capital of USA?", "options": "[New York, Washington DC, Los Angeles]", "answer": "Washington DC-label", }, ] prompt = task.construct_prompt(input, examples) assert input["question"] in prompt assert input["options"] in prompt assert input["answer"] not in prompt for example in examples: assert example["question"] in prompt assert example["options"] in prompt assert example["answer"] in prompt def question_answering_parse_llm_response(): config = AutolabelConfig(SCIQ_CONFIG) task = QuestionAnsweringTask(config=config) input = { "question": "What is the capital of France?", "options": "[Paris, London, Berlin]", "answer": "Paris-label", } prompt = "This is a prompt" label = "This is the thing we want to test.\nParis-label" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == "Paris-label" assert parsed.successfully_labeled == True assert parsed.raw_response == label label = "" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == task.NULL_LABEL_TOKEN assert parsed.successfully_labeled == False def test_question_answering_eval(): config = AutolabelConfig(SCIQ_CONFIG) task = QuestionAnsweringTask(config=config) llm_labels = [ LLMAnnotation( successfully_labeled=True, label="Delhi", ), LLMAnnotation( successfully_labeled=False, label=task.NULL_LABEL_TOKEN, ), LLMAnnotation( successfully_labeled=True, label="Paris", ), LLMAnnotation( successfully_labeled=True, label="Bangalore", ), LLMAnnotation( successfully_labeled=True, label="Delhi", ), ] gt_labels = [ "Delhi", "Washington DC", "Paris", "Bangalore", "Washington DC", ] eval = task.eval(llm_labels, gt_labels) for metric in eval: if metric.metric_type == Metric.ACCURACY: assert metric.value[0] == 0.75 elif metric.metric_type == Metric.COMPLETION_RATE: assert metric.value[0] == 0.8 elif metric.metric_type == Metric.SUPPORT: assert metric.value[0] == 4 def test_classification_labels_not_in_labels_list(): config = AutolabelConfig(BANKING_CONFIG) task = ClassificationTask(config=config) input = {"example": "Here is an example", "label": "not-in-labels-list"} prompt = "This is a prompt" label = "This is the thing we want to test.\nnot-in-labels-list" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == "not-in-labels-list" assert parsed.successfully_labeled == False assert parsed.raw_response == label def test_entity_matching_label_not_in_labels_list(): config = AutolabelConfig(WALMART_AMAZON_CONFIG) task = EntityMatchingTask(config=config) input = { "Title_entity1": "lexmark extra high yield return pgm print cartridge - magenta", "Category_entity1": "printers", "Brand_entity1": "lexmark", "ModelNo_entity1": "c782u1mg", "Price_entity1": "214.88", "Title_entity2": "lexmark 18c1428 return program print cartridge black", "Category_entity2": "inkjet printer ink", "Brand_entity2": "lexmark", "ModelNo_entity2": "18c1428", "Price_entity2": "19.97", "label": "not-in-labels-list", } prompt = "This is a prompt" label = "This is the thing we want to test.\nnot-in-labels-list" response = Generation(text=label) parsed = task.parse_llm_response(response, input, prompt) assert parsed.label == "not-in-labels-list" assert parsed.successfully_labeled == False assert parsed.raw_response == label def test_multilabel_classification_construct_prompt(): config = AutolabelConfig(TWITTER_EMOTION_DETECTION_CONFIG) task = MultilabelClassificationTask(config=config) assert task.config != None input = {"example": "Here is an example", "labels": "label-1, label-2"} examples = [ {"example": "Here is a seed example", "labels": "labela, labelb"}, {"example": "Here is another seed example", "labels": "labelc, labeld"}, ] prompt = task.construct_prompt(input, examples) assert TWITTER_EMOTION_DETECTION_CONFIG["prompt"]["output_guidelines"] in prompt assert "\n".join(TWITTER_EMOTION_DETECTION_CONFIG["prompt"]["labels"]) in prompt assert input["example"] in prompt assert input["labels"] not in prompt for example in examples: assert example["example"] in prompt assert example["labels"] in prompt new_config = copy.deepcopy(TWITTER_EMOTION_DETECTION_CONFIG) del new_config["prompt"]["few_shot_selection"] new_config = AutolabelConfig(new_config) task = ClassificationTask(config=new_config) prompt = task.construct_prompt(input, examples) for example in examples: assert example["example"] not in prompt assert example["labels"] not in prompt def test_multilabel_classification_eval(): config = AutolabelConfig(TWITTER_EMOTION_DETECTION_CONFIG) task = MultilabelClassificationTask(config=config) llm_labels = [ LLMAnnotation( successfully_labeled=True, label="neutral", ), LLMAnnotation( successfully_labeled=False, label=task.NULL_LABEL_TOKEN, ), LLMAnnotation( successfully_labeled=True, label="sadness", ), LLMAnnotation( successfully_labeled=True, label="anger, disgust", ), LLMAnnotation( successfully_labeled=True, label="joy, love, trust", ), ] gt_labels = [ "anger, disgust", "joy, optimism, trust", "anticipation, joy, sadness", "anger, disgust", "joy, optimism", ] eval = task.eval(llm_labels, gt_labels) for metric in eval: if metric.metric_type == Metric.ACCURACY: assert metric.value[0] == 0.25 elif metric.metric_type == Metric.F1: assert metric.value[0] == 0.25 elif metric.metric_type == Metric.COMPLETION_RATE: assert metric.value[0] == 0.8 elif metric.metric_type == Metric.SUPPORT: assert metric.value[0] == 4
[ "This is a prompt" ]
2024-01-10
art-from-the-machine/Mantella
src~setup.py
import openai import logging import src.utils as utils import pandas as pd import tiktoken import src.config_loader as config_loader def initialise(config_file, logging_file, secret_key_file, character_df_file, language_file): def setup_openai_secret_key(file_name, is_local): if is_local: api_key = 'abc123' else: with open(file_name, 'r') as f: api_key = f.readline().strip() openai.api_key = api_key def setup_logging(file_name): logging.basicConfig(filename=file_name, format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO) console = logging.StreamHandler() console.setLevel(logging.INFO) logging.getLogger('').addHandler(console) def get_character_df(file_name): encoding = utils.get_file_encoding(file_name) character_df = pd.read_csv(file_name, engine='python', encoding=encoding) character_df = character_df.loc[character_df['voice_model'].notna()] return character_df def get_language_info(file_name): language_df = pd.read_csv(file_name) try: language_info = language_df.loc[language_df['alpha2']==config.language].to_dict('records')[0] return language_info except: logging.error(f"Could not load language '{config.language}'. Please set a valid language in config.ini\n") def get_token_limit(llm, custom_token_count, is_local): if '/' in llm: llm = llm.split('/')[-1] if llm == 'gpt-3.5-turbo': token_limit = 4096 elif llm == 'gpt-3.5-turbo-16k': token_limit = 16384 elif llm == 'gpt-4': token_limit = 8192 elif llm == 'gpt-4-32k': token_limit = 32768 elif llm == 'claude-2': token_limit = 100_000 elif llm == 'claude-instant-v1': token_limit = 100_000 elif llm == 'palm-2-chat-bison': token_limit = 8000 elif llm == 'palm-2-codechat-bison': token_limit = 8000 elif llm == 'llama-2-7b-chat': token_limit = 4096 elif llm == 'llama-2-13b-chat': token_limit = 4096 elif llm == 'llama-2-70b-chat': token_limit = 4096 elif llm == 'codellama-34b-instruct': token_limit = 16000 elif llm == 'nous-hermes-llama2-13b': token_limit = 4096 elif llm == 'weaver': token_limit = 8000 elif llm == 'mythomax-L2-13b': token_limit = 8192 elif llm == 'airoboros-l2-70b-2.1': token_limit = 4096 elif llm == 'gpt-3.5-turbo-1106': token_limit = 16_385 elif llm == 'gpt-4-1106-preview': token_limit = 128_000 else: logging.info(f"Could not find number of available tokens for {llm}. Defaulting to token count of {custom_token_count} (this number can be changed via the `custom_token_count` setting in config.ini)") try: token_limit = int(custom_token_count) except ValueError: logging.error(f"Invalid custom_token_count value: {custom_token_count}. It should be a valid integer. Please update your configuration.") token_limit = 4096 # Default to 4096 in case of an error. if token_limit <= 4096: if is_local: llm = 'Local language model' logging.info(f"{llm} has a low token count of {token_limit}. For better NPC memories, try changing to a model with a higher token count") return token_limit setup_logging(logging_file) config = config_loader.ConfigLoader(config_file) is_local = True if (config.alternative_openai_api_base == 'none') or (config.alternative_openai_api_base == 'https://openrouter.ai/api/v1'): is_local = False setup_openai_secret_key(secret_key_file, is_local) if is_local: logging.info(f"Running Mantella with local language model") else: logging.info(f"Running Mantella with '{config.llm}'. The language model chosen can be changed via config.ini") # clean up old instances of exe runtime files utils.cleanup_mei(config.remove_mei_folders) character_df = get_character_df(character_df_file) language_info = get_language_info(language_file) chosenmodel = config.llm # if using an alternative API, use encoding for GPT-3.5 by default # NOTE: this encoding may not be the same for all models, leading to incorrect token counts # this can lead to the token limit of the given model being overrun if config.alternative_openai_api_base != 'none': chosenmodel = 'gpt-3.5-turbo' try: encoding = tiktoken.encoding_for_model(chosenmodel) except: logging.error('Error loading model. If you are using an alternative to OpenAI, please find the setting `alternative_openai_api_base` in MantellaSoftware/config.ini and follow the instructions to change this setting') raise token_limit = get_token_limit(config.llm, config.custom_token_count, is_local) if config.alternative_openai_api_base != 'none': openai.api_base = config.alternative_openai_api_base return config, character_df, language_info, encoding, token_limit
[]
2024-01-10
art-from-the-machine/Mantella
src~stt.py
from faster_whisper import WhisperModel import speech_recognition as sr import logging import src.utils as utils import requests import json import openai class Transcriber: def __init__(self, game_state_manager, config): self.game_state_manager = game_state_manager self.mic_enabled = config.mic_enabled self.language = config.stt_language self.task = "transcribe" if config.stt_translate == 1: # translate to English self.task = "translate" self.model = config.whisper_model self.process_device = config.whisper_process_device self.audio_threshold = config.audio_threshold self.listen_timeout = config.listen_timeout self.whisper_type = config.whisper_type self.whisper_url = config.whisper_url self.debug_mode = config.debug_mode self.debug_use_mic = config.debug_use_mic self.default_player_response = config.default_player_response self.debug_exit_on_first_exchange = config.debug_exit_on_first_exchange self.end_conversation_keyword = config.end_conversation_keyword self.call_count = 0 if self.mic_enabled == '1': self.recognizer = sr.Recognizer() self.recognizer.pause_threshold = config.pause_threshold self.microphone = sr.Microphone() if self.audio_threshold == 'auto': logging.info(f"Audio threshold set to 'auto'. Adjusting microphone for ambient noise...") logging.info("If the mic is not picking up your voice, try setting this audio_threshold value manually in MantellaSoftware/config.ini.\n") with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source, duration=5) else: self.recognizer.dynamic_energy_threshold = False self.recognizer.energy_threshold = int(self.audio_threshold) logging.info(f"Audio threshold set to {self.audio_threshold}. If the mic is not picking up your voice, try lowering this value in MantellaSoftware/config.ini. If the mic is picking up too much background noise, try increasing this value.\n") # if using faster_whisper, load model selected by player, otherwise skip this step if self.whisper_type == 'faster_whisper': if self.process_device == 'cuda': self.transcribe_model = WhisperModel(self.model, device=self.process_device) else: self.transcribe_model = WhisperModel(self.model, device=self.process_device, compute_type="float32") def get_player_response(self, say_goodbye, radiant_dialogue="false"): if radiant_dialogue == "true": if self.call_count < 1: logging.info('Running radiant dialogue') transcribed_text = '*Please begin / continue a conversation topic (greetings are not needed). Ensure to change the topic if the current one is losing steam. The conversation should steer towards topics which reveal information about the characters and who they are, or instead drive forward conversations previously discussed in their memory.*' self.call_count += 1 elif self.call_count <= 1: logging.info('Ending radiant dialogue') transcribed_text = '*Please wrap up the current topic between the NPCs in a natural way. Nobody is leaving, so no formal goodbyes.*' self.call_count += 1 else: logging.info('Radiant dialogue ended') transcribed_text = self.end_conversation_keyword self.call_count = 0 elif (self.debug_mode == '1') & (self.debug_use_mic == '0'): transcribed_text = self.default_player_response else: if self.mic_enabled == '1': # listen for response transcribed_text = self.recognize_input() else: # text input through console if (self.debug_mode == '1') & (self.debug_use_mic == '1'): transcribed_text = input('\nWrite player\'s response: ') logging.info(f'Player wrote: {transcribed_text}') # await text input from the game else: self.game_state_manager.write_game_info('_mantella_text_input', '') self.game_state_manager.write_game_info('_mantella_text_input_enabled', 'True') transcribed_text = self.game_state_manager.load_data_when_available('_mantella_text_input', '') self.game_state_manager.write_game_info('_mantella_text_input', '') self.game_state_manager.write_game_info('_mantella_text_input_enabled', 'False') if (self.debug_mode == '1') & (self.debug_exit_on_first_exchange == '1'): if say_goodbye: transcribed_text = self.end_conversation_keyword else: say_goodbye = True return transcribed_text, say_goodbye def recognize_input(self): """ Recognize input from mic and return transcript if activation tag (assistant name) exist """ while True: self.game_state_manager.write_game_info('_mantella_status', 'Listening...') logging.info('Listening...') transcript = self._recognize_speech_from_mic() transcript_cleaned = utils.clean_text(transcript) conversation_ended = self.game_state_manager.load_data_when_available('_mantella_end_conversation', '') if conversation_ended.lower() == 'true': return 'goodbye' # common phrases hallucinated by Whisper if transcript_cleaned in ['', 'thank you', 'thank you for watching', 'thanks for watching', 'the transcript is from the', 'the', 'thank you very much']: continue self.game_state_manager.write_game_info('_mantella_status', 'Thinking...') return transcript def _recognize_speech_from_mic(self): """ Capture the words from the recorded audio (audio stream --> free text). Transcribe speech from recorded from `microphone`. """ @utils.time_it def whisper_transcribe(audio): # if using faster_whisper (default) return based on faster_whisper's code, if not assume player wants to use server mode and send query to whisper_url set by player. if self.whisper_type == 'faster_whisper': segments, info = self.transcribe_model.transcribe(audio, task=self.task, language=self.language, beam_size=5, vad_filter=True) result_text = ' '.join(segment.text for segment in segments) return result_text # this code queries the whispercpp server set by the user to obtain the response, this format also allows use of official openai whisper API else: url = self.whisper_url if 'openai' in url: headers = {"Authorization": f"Bearer {openai.api_key}",} else: headers = {"Authorization": "Bearer apikey",} data = {'model': self.model} files = {'file': open(audio, 'rb')} response = requests.post(url, headers=headers, files=files, data=data) response_data = json.loads(response.text) if 'text' in response_data: return response_data['text'].strip() with self.microphone as source: try: audio = self.recognizer.listen(source, timeout=self.listen_timeout) except sr.WaitTimeoutError: return '' audio_file = 'player_recording.wav' with open(audio_file, 'wb') as file: file.write(audio.get_wav_data(convert_rate=16000)) transcript = whisper_transcribe(audio_file) logging.info(transcript) return transcript @staticmethod def activation_name_exists(transcript_cleaned, activation_name): """Identifies keyword in the input transcript""" keyword_found = False if transcript_cleaned: transcript_words = transcript_cleaned.split() if bool(set(transcript_words).intersection([activation_name])): keyword_found = True elif transcript_cleaned == activation_name: keyword_found = True return keyword_found @staticmethod def _remove_activation_word(transcript, activation_name): transcript = transcript.replace(activation_name, '') return transcript
[]
2024-01-10
art-from-the-machine/Mantella
src~output_manager.py
import openai from aiohttp import ClientSession import asyncio import os import wave import logging import time import shutil import src.utils as utils import unicodedata import re import sys class ChatManager: def __init__(self, game_state_manager, config, encoding): self.game_state_manager = game_state_manager self.mod_folder = config.mod_path self.max_response_sentences = config.max_response_sentences self.llm = config.llm self.alternative_openai_api_base = config.alternative_openai_api_base self.temperature = config.temperature self.top_p = config.top_p self.stop = config.stop self.frequency_penalty = config.frequency_penalty self.max_tokens = config.max_tokens self.language = config.language self.encoding = encoding self.add_voicelines_to_all_voice_folders = config.add_voicelines_to_all_voice_folders self.offended_npc_response = config.offended_npc_response self.forgiven_npc_response = config.forgiven_npc_response self.follow_npc_response = config.follow_npc_response self.experimental_features = config.experimental_features self.wait_time_buffer = config.wait_time_buffer self.character_num = 0 self.active_character = None self.wav_file = f'MantellaDi_MantellaDialogu_00001D8B_1.wav' self.lip_file = f'MantellaDi_MantellaDialogu_00001D8B_1.lip' self.end_of_sentence_chars = ['.', '?', '!', ':', ';'] self.end_of_sentence_chars = [unicodedata.normalize('NFKC', char) for char in self.end_of_sentence_chars] self.sentence_queue = asyncio.Queue() async def get_audio_duration(self, audio_file): """Check if the external software has finished playing the audio file""" with wave.open(audio_file, 'r') as wf: frames = wf.getnframes() rate = wf.getframerate() # wait `buffer` seconds longer to let processes finish running correctly duration = frames / float(rate) + self.wait_time_buffer return duration def setup_voiceline_save_location(self, in_game_voice_folder): """Save voice model folder to Mantella Spell if it does not already exist""" self.in_game_voice_model = in_game_voice_folder in_game_voice_folder_path = f"{self.mod_folder}/{in_game_voice_folder}/" if not os.path.exists(in_game_voice_folder_path): os.mkdir(in_game_voice_folder_path) # copy voicelines from one voice folder to this new voice folder # this step is needed for Skyrim to acknowledge the folder example_folder = f"{self.mod_folder}/MaleNord/" for file_name in os.listdir(example_folder): source_file_path = os.path.join(example_folder, file_name) if os.path.isfile(source_file_path): shutil.copy(source_file_path, in_game_voice_folder_path) self.game_state_manager.write_game_info('_mantella_status', 'Error with Mantella.exe. Please check MantellaSoftware/logging.log') logging.warn("Unknown NPC detected. This NPC will be able to speak once you restart Skyrim. To learn how to add memory, a background, and a voice model of your choosing to this NPC, see here: https://github.com/art-from-the-machine/Mantella#adding-modded-npcs") input('\nPress any key to exit...') sys.exit(0) @utils.time_it def save_files_to_voice_folders(self, queue_output): """Save voicelines and subtitles to the correct game folders""" audio_file, subtitle = queue_output if self.add_voicelines_to_all_voice_folders == '1': for sub_folder in os.scandir(self.mod_folder): if sub_folder.is_dir(): shutil.copyfile(audio_file, f"{sub_folder.path}/{self.wav_file}") shutil.copyfile(audio_file.replace(".wav", ".lip"), f"{sub_folder.path}/{self.lip_file}") else: shutil.copyfile(audio_file, f"{self.mod_folder}/{self.active_character.in_game_voice_model}/{self.wav_file}") shutil.copyfile(audio_file.replace(".wav", ".lip"), f"{self.mod_folder}/{self.active_character.in_game_voice_model}/{self.lip_file}") logging.info(f"{self.active_character.name} (character {self.character_num}) should speak") if self.character_num == 0: self.game_state_manager.write_game_info('_mantella_say_line', subtitle.strip()) else: say_line_file = '_mantella_say_line_'+str(self.character_num+1) self.game_state_manager.write_game_info(say_line_file, subtitle.strip()) @utils.time_it def remove_files_from_voice_folders(self): for sub_folder in os.listdir(self.mod_folder): try: os.remove(f"{self.mod_folder}/{sub_folder}/{self.wav_file}") os.remove(f"{self.mod_folder}/{sub_folder}/{self.lip_file}") except: continue async def send_audio_to_external_software(self, queue_output): logging.info(f"Dialogue to play: {queue_output[0]}") self.save_files_to_voice_folders(queue_output) # Remove the played audio file #os.remove(audio_file) # Remove the played audio file #os.remove(audio_file) async def send_response(self, sentence_queue, event): """Send response from sentence queue generated by `process_response()`""" while True: queue_output = await sentence_queue.get() if queue_output is None: logging.info('End of sentences') break # send the audio file to the external software and wait for it to finish playing await self.send_audio_to_external_software(queue_output) event.set() audio_duration = await self.get_audio_duration(queue_output[0]) # wait for the audio playback to complete before getting the next file logging.info(f"Waiting {int(round(audio_duration,4))} seconds...") await asyncio.sleep(audio_duration) def clean_sentence(self, sentence): def remove_as_a(sentence): """Remove 'As an XYZ,' from beginning of sentence""" if sentence.startswith('As a'): if ', ' in sentence: logging.info(f"Removed '{sentence.split(', ')[0]} from response") sentence = sentence.replace(sentence.split(', ')[0]+', ', '') return sentence def parse_asterisks_brackets(sentence): if ('*' in sentence): # Check if sentence contains two asterisks asterisk_check = re.search(r"(?<!\*)\*(?!\*)[^*]*\*(?!\*)", sentence) if asterisk_check: logging.info(f"Removed asterisks text from response: {sentence}") # Remove text between two asterisks sentence = re.sub(r"(?<!\*)\*(?!\*)[^*]*\*(?!\*)", "", sentence) else: logging.info(f"Removed response containing single asterisks: {sentence}") sentence = '' if ('(' in sentence) or (')' in sentence): # Check if sentence contains two brackets bracket_check = re.search(r"\(.*\)", sentence) if bracket_check: logging.info(f"Removed brackets text from response: {sentence}") # Remove text between brackets sentence = re.sub(r"\(.*?\)", "", sentence) else: logging.info(f"Removed response containing single bracket: {sentence}") sentence = '' return sentence if ('Well, well, well' in sentence): sentence = sentence.replace('Well, well, well', 'Well well well') sentence = remove_as_a(sentence) sentence = sentence.replace('"','') sentence = sentence.replace('[', '(') sentence = sentence.replace(']', ')') sentence = sentence.replace('{', '(') sentence = sentence.replace('}', ')') # local models sometimes get the idea in their head to use double asterisks **like this** in sentences instead of single # this converts double asterisks to single so that they can be filtered out appropriately sentence = sentence.replace('**','*') sentence = parse_asterisks_brackets(sentence) return sentence async def process_response(self, sentence_queue, input_text, messages, synthesizer, characters, radiant_dialogue, event): """Stream response from LLM one sentence at a time""" messages.append({"role": "user", "content": input_text}) sentence = '' full_reply = '' num_sentences = 0 action_taken = False if self.alternative_openai_api_base == 'none': openai.aiosession.set(ClientSession()) # https://github.com/openai/openai-python#async-api while True: try: start_time = time.time() async for chunk in await openai.ChatCompletion.acreate(model=self.llm, messages=messages, headers={"HTTP-Referer": 'https://github.com/art-from-the-machine/Mantella', "X-Title": 'mantella'},stream=True,stop=self.stop,temperature=self.temperature,top_p=self.top_p,frequency_penalty=self.frequency_penalty, max_tokens=self.max_tokens): content = chunk["choices"][0].get("delta", {}).get("content") if content is not None: sentence += content if ('assist' in content) and (num_sentences>0): logging.info(f"'assist' keyword found. Ignoring sentence which begins with: {sentence}") break content_edit = unicodedata.normalize('NFKC', content) # check if content marks the end of a sentence if (any(char in content_edit for char in self.end_of_sentence_chars)): sentence = self.clean_sentence(sentence) if len(sentence.strip()) < 3: logging.info(f'Skipping voiceline that is too short: {sentence}') break logging.info(f"LLM returned sentence took {time.time() - start_time} seconds to execute") if content_edit == ':': keyword_extraction = sentence.strip()[:-1] #.lower() # if LLM is switching character if (keyword_extraction in characters.active_characters): #TODO: or (any(key.split(' ')[0] == keyword_extraction for key in characters.active_characters)) logging.info(f"Switched to {keyword_extraction}") self.active_character = characters.active_characters[keyword_extraction] synthesizer.change_voice(self.active_character.voice_model) # characters are mapped to say_line based on order of selection # taking the order of the dictionary to find which say_line to use, but it is bad practice to use dictionaries in this way self.character_num = list(characters.active_characters.keys()).index(keyword_extraction) full_reply += sentence sentence = '' action_taken = True elif keyword_extraction == 'Player': logging.info(f"Stopped LLM from speaking on behalf of the player") break elif keyword_extraction.lower() == self.offended_npc_response.lower(): if self.experimental_features: logging.info(f"The player offended the NPC") self.game_state_manager.write_game_info('_mantella_aggro', '1') else: logging.info(f"Experimental features disabled. Please set experimental_features = 1 in config.ini to enable the Offended feature") full_reply += sentence sentence = '' action_taken = True elif keyword_extraction.lower() == self.forgiven_npc_response.lower(): if self.experimental_features: logging.info(f"The player made up with the NPC") self.game_state_manager.write_game_info('_mantella_aggro', '0') else: logging.info(f"Experimental features disabled. Please set experimental_features = 1 in config.ini to enable the Forgiven feature") full_reply += sentence sentence = '' action_taken = True elif keyword_extraction.lower() == self.follow_npc_response.lower(): if self.experimental_features: logging.info(f"The NPC is willing to follow the player") self.game_state_manager.write_game_info('_mantella_aggro', '2') else: logging.info(f"Experimental features disabled. Please set experimental_features = 1 in config.ini to enable the Follow feature") full_reply += sentence sentence = '' action_taken = True if action_taken == False: # Generate the audio and return the audio file path try: audio_file = synthesizer.synthesize(self.active_character.voice_model, None, ' ' + sentence + ' ') except Exception as e: logging.error(f"xVASynth Error: {e}") # Put the audio file path in the sentence_queue await sentence_queue.put([audio_file, sentence]) full_reply += sentence num_sentences += 1 sentence = '' # clear the event for the next iteration event.clear() # wait for the event to be set before generating the next line await event.wait() end_conversation = self.game_state_manager.load_data_when_available('_mantella_end_conversation', '') radiant_dialogue_update = self.game_state_manager.load_data_when_available('_mantella_radiant_dialogue', '') # stop processing LLM response if: # max_response_sentences reached (and the conversation isn't radiant) # conversation has switched from radiant to multi NPC (this allows the player to "interrupt" radiant dialogue and include themselves in the conversation) # the conversation has ended if ((num_sentences >= self.max_response_sentences) and (radiant_dialogue == 'false')) or ((radiant_dialogue == 'true') and (radiant_dialogue_update.lower() == 'false')) or (end_conversation.lower() == 'true'): break else: action_taken = False if self.alternative_openai_api_base == 'none': await openai.aiosession.get().close() break except Exception as e: logging.error(f"LLM API Error: {e}") error_response = "I can't find the right words at the moment." audio_file = synthesizer.synthesize(self.active_character.voice_model, None, error_response) self.save_files_to_voice_folders([audio_file, error_response]) logging.info('Retrying connection to API...') time.sleep(5) # Mark the end of the response await sentence_queue.put(None) messages.append({"role": "assistant", "content": full_reply}) logging.info(f"Full response saved ({len(self.encoding.encode(full_reply))} tokens): {full_reply}") return messages
[]
2024-01-10
art-from-the-machine/Mantella
src~chat_response.py
import openai import tiktoken import logging import src.utils as utils import time @utils.time_it def chatgpt_api(input_text, messages, llm): if input_text: messages.append( {"role": "user", "content": input_text}, ) logging.info('Getting LLM response...') try: chat_completion = openai.ChatCompletion.create( model=llm, messages=messages, headers={"HTTP-Referer": 'https://github.com/art-from-the-machine/Mantella', "X-Title": 'mantella'}, max_tokens=1_000 ) except openai.error.RateLimitError: logging.warning('Could not connect to LLM API, retrying in 5 seconds...') time.sleep(5) reply = chat_completion.choices[0].message.content messages.append( {"role": "assistant", "content": chat_completion.choices[0].message.content}, ) logging.info(f"LLM Response: {reply}") return reply, messages def num_tokens_from_messages(messages, model="gpt-3.5-turbo"): """Returns the number of tokens used by a list of messages""" try: encoding = tiktoken.encoding_for_model(model) except KeyError: encoding = tiktoken.get_encoding("cl100k_base") # note: this calculation is based on GPT-3.5, future models may deviate from this num_tokens = 0 for message in messages: num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n for key, value in message.items(): num_tokens += len(encoding.encode(value)) if key == "name": # if there's a name, the role is omitted num_tokens += -1 # role is always required and always 1 token num_tokens += 2 # every reply is primed with <im_start>assistant return num_tokens
[]
2024-01-10
langchain-ai/twitter-finetune
ingest.py
import json from langchain.schema import AIMessage from langchain.adapters.openai import convert_message_to_dict import time import openai from io import BytesIO if __name__ == "__main__": with open('dataset_twitter-scraper_2023-08-23_22-13-19-740.json') as f: data = json.load(f) tweets = [d["full_text"] for d in data if "t.co" not in d['full_text']] messages = [AIMessage(content=t) for t in tweets] system_message = {"role": "system", "content": "write a tweet"} data = [[system_message, convert_message_to_dict(m)] for m in messages] my_file = BytesIO() for m in data: my_file.write((json.dumps({"messages": m}) + "\n").encode('utf-8')) my_file.seek(0) training_file = openai.File.create( file=my_file, purpose='fine-tune' ) while True: try: job = openai.FineTuningJob.create(training_file=training_file.id, model="gpt-3.5-turbo") except Exception as e: print(e) print("Trying again in ten seconds....") time.sleep(10) start = time.time() while True: ftj = openai.FineTuningJob.retrieve(job.id) if ftj.fine_tuned_model is None: print(f"Waiting for fine-tuning to complete... Elapsed: {time.time() - start}", end="\r", flush=True) time.sleep(10) else: print("\n") print(ftj.fine_tuned_model, flush=True) break
[ "write a tweet" ]
2024-01-10
josh-ashkinaze/llm_pred
scripts~get_llm_preds.py
""" Author: Joshua Ashkinaze Date: 2023-06-20 Description: This script uses ChatGPT to predict binary political events from Metaculus """ import argparse import openai import json import pandas as pd import multiprocessing import numpy as np import re import os import logging from tenacity import retry, stop_after_attempt, wait_random_exponential, before_sleep_log, before_sleep_nothing def log_before_sleep(retry_state): if retry_state.attempt_number > 1: sleep_duration = retry_state.next_action.sleep logging.info(f"Sleeping for {sleep_duration} seconds before retry attempt {retry_state.attempt_number}.") before_sleep_nothing(retry_state) def clean_text(text): if isinstance(text, str): text = re.sub(r'http\S+', '', text) text = text.replace('\n', ' ') text = re.sub(r'\s+', ' ', text).strip() return text else: return np.NaN @retry(wait=wait_random_exponential(multiplier=30, min=1, max=60), stop=stop_after_attempt(30), retry_error_callback=lambda x: logging.info(f"Retry error: {x}"), before_sleep=log_before_sleep) def make_prediction(event, event_id, description, res_criteria, temperature=0): try: # Set up the base prompt without the description or res_criteria prompt = f"""INSTRUCTIONS: As an artificial intelligence model, you have been trained on a vast dataset up to September 2021. Your task is to provide an educated guess on a possible future event ("{event}"), based on your training data. While making your guess, keep in mind: 1. Overconfidence can lead to inaccurate forecasts. Be aware of this and provide a conservative estimate. 2. Consider base rates: Remember to think about how often events have occurred in the past as a guide. 3. Consider the availability heuristic: People often judge the likelihood of events based on how easily instances come to mind. Be cautious of this and ensure that your prediction is not overly influenced by recent, memorable events. 4. Adopt an "outside view": Instead of focusing solely on the specific details of the event in question, take a step back and consider similar events or situations. Use the base rates of those events to inform your prediction. 5. Rare and unpredictable events can and do occur. While these "black swan" events may be unlikely, they should not be ignored entirely. 6. Acknowledge uncertainty: The future is not set in stone and your guess should take into account the inherent unpredictability of future events. QUESTION: {event} """ # Include description and resolution criteria if they are not np.nan if not (isinstance(description, float) and np.isnan(description)): prompt += f"\nDESCRIPTION:\n{description}" if not (isinstance(res_criteria, float) and np.isnan(res_criteria)): prompt += f"\n\nRESOLUTION DETAILS:\n{res_criteria}\n" prompt += """ RETURN: A json file with fields { "answer": "YES" or "NO", "reasoning": Your reasoning for your guess, "confidence": Your confidence level on a scale from 0 to 1 } CONSTRAINTS: Do not add anything to your answer other than "YES" or "NO", your reasoning, and your confidence level. """ messages = openai.ChatCompletion.create( temperature=temperature, model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt} ] ) msg = messages['choices'][0]['message']['content'] try: msg_dict = json.loads(msg.strip()) msg_dict['id'] = event_id return msg_dict except Exception as e: logging.info(f"Error occured after getting msg: {e}. The returned response was {msg}") return {'id': event_id, 'answer': np.NaN, 'reasoning': np.NaN} except Exception as e: logging.info(f"An error occurred: {e}") raise e def process_event(args): q, id, description, res_criteria = args with open('../secrets/secrets.json', 'r') as f: secrets = json.load(f) openai.api_key = secrets['openai_key'] pred = make_prediction(q, id, description, res_criteria) logging.info(pred) return pred def main(debug_mode, num_requests): LOG_FORMAT = '%(asctime)s %(levelname)s: %(message)s' logging.basicConfig(filename=f'{os.path.basename(__file__)}.log', level=logging.INFO, format=LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S', filemode='w') logging.info(f"Starting script with parameters debug_mode={debug_mode}, num_requests={num_requests}") events = pd.read_csv("../data/metaculus_events.csv") if debug_mode: events = events.sample(5) questions = events['title'].tolist() ids = events['id'].tolist() events['description_clean'] = events['description'].apply(clean_text) events['resolution_criteria_clean'] = events['resolution_criteria'].apply(clean_text) descriptions = events['description_clean'].tolist() res_criteria = events['resolution_criteria_clean'].tolist() n_jobs = multiprocessing.cpu_count() responses = [] for _ in range(num_requests): with multiprocessing.Pool(processes=n_jobs) as pool: responses += pool.map(process_event, zip(questions, ids, descriptions, res_criteria)) logging.info(f"Total responses: {len(responses)}") responses_df = pd.DataFrame(responses) responses_df.to_csv("../data/raw_llm_preds.csv", index=False) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Metaculus event prediction script") parser.add_argument('-d', '--debug_mode', action='store_true', help="Enable debug mode to sample only 5 events") parser.add_argument('-n', '--num_requests', type=int, default=1, help="Number of requests per event") args = parser.parse_args() main(args.debug_mode, args.num_requests)
[ "\n\nRESOLUTION DETAILS:\nPLACEHOLDER\n", "\nDESCRIPTION:\nPLACEHOLDER", "INSTRUCTIONS:\n As an artificial intelligence model, you have been trained on a vast dataset up to September 2021. Your task is to provide an educated guess on a possible future event (\"PLACEHOLDER\"), based on your training data.\n\n While making your guess, keep in mind:\n 1. Overconfidence can lead to inaccurate forecasts. Be aware of this and provide a conservative estimate.\n 2. Consider base rates: Remember to think about how often events have occurred in the past as a guide.\n 3. Consider the availability heuristic: People often judge the likelihood of events based on how easily instances come to mind. Be cautious of this and ensure that your prediction is not overly influenced by recent, memorable events.\n 4. Adopt an \"outside view\": Instead of focusing solely on the specific details of the event in question, take a step back and consider similar events or situations. Use the base rates of those events to inform your prediction.\n 5. Rare and unpredictable events can and do occur. While these \"black swan\" events may be unlikely, they should not be ignored entirely.\n 6. Acknowledge uncertainty: The future is not set in stone and your guess should take into account the inherent unpredictability of future events.\n\n QUESTION:\n PLACEHOLDER\n ", "You are a helpful assistant.", "\n RETURN:\n A json file with fields { \"answer\": \"YES\" or \"NO\", \"reasoning\": Your reasoning for your guess, \"confidence\": Your confidence level on a scale from 0 to 1 }\n\n CONSTRAINTS:\n Do not add anything to your answer other than \"YES\" or \"NO\", your reasoning, and your confidence level.\n " ]
2024-01-10
seshakiran/anything-llm
collector~scripts~gitbook.py
import os, json from langchain.document_loaders import GitbookLoader from urllib.parse import urlparse from datetime import datetime from alive_progress import alive_it from .utils import tokenize from uuid import uuid4 def gitbook(): url = input("Enter the URL of the GitBook you want to collect: ") if(url == ''): print("Not a gitbook URL") exit(1) primary_source = urlparse(url) output_path = f"./outputs/gitbook-logs/{primary_source.netloc}" transaction_output_dir = f"../server/documents/gitbook-{primary_source.netloc}" if os.path.exists(output_path) == False:os.makedirs(output_path) if os.path.exists(transaction_output_dir) == False: os.makedirs(transaction_output_dir) loader = GitbookLoader(url, load_all_paths= primary_source.path in ['','/']) for doc in alive_it(loader.load()): metadata = doc.metadata content = doc.page_content source = urlparse(metadata.get('source')) name = 'home' if source.path in ['','/'] else source.path.replace('/','_') output_filename = f"doc-{name}.json" transaction_output_filename = f"doc-{name}.json" data = { 'id': str(uuid4()), 'url': metadata.get('source'), "title": metadata.get('title'), "description": metadata.get('title'), "published": datetime.today().strftime('%Y-%m-%d %H:%M:%S'), "wordCount": len(content), 'pageContent': content, 'token_count_estimate': len(tokenize(content)) } with open(f"{output_path}/{output_filename}", 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=True, indent=4) with open(f"{transaction_output_dir}/{transaction_output_filename}", 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=True, indent=4)
[]
2024-01-10
seshakiran/anything-llm
collector~scripts~link.py
import os, json, tempfile from urllib.parse import urlparse from requests_html import HTMLSession from langchain.document_loaders import UnstructuredHTMLLoader from .link_utils import append_meta from .utils import tokenize, ada_v2_cost # Example Channel URL https://tim.blog/2022/08/09/nft-insider-trading-policy/ def link(): print("[NOTICE]: The first time running this process it will download supporting libraries.\n\n") fqdn_link = input("Paste in the URL of an online article or blog: ") if(len(fqdn_link) == 0): print("Invalid URL!") exit(1) session = HTMLSession() req = session.get(fqdn_link) if(req.ok == False): print("Could not reach this url!") exit(1) req.html.render() full_text = None with tempfile.NamedTemporaryFile(mode = "w") as tmp: tmp.write(req.html.html) tmp.seek(0) loader = UnstructuredHTMLLoader(tmp.name) data = loader.load()[0] full_text = data.page_content tmp.close() link = append_meta(req, full_text, True) if(len(full_text) > 0): source = urlparse(req.url) output_filename = f"website-{source.netloc}-{source.path.replace('/','_')}.json" output_path = f"./outputs/website-logs" transaction_output_filename = f"article-{source.path.replace('/','_')}.json" transaction_output_dir = f"../server/documents/website-{source.netloc}" if os.path.isdir(output_path) == False: os.makedirs(output_path) if os.path.isdir(transaction_output_dir) == False: os.makedirs(transaction_output_dir) full_text = append_meta(req, full_text) tokenCount = len(tokenize(full_text)) link['pageContent'] = full_text link['token_count_estimate'] = tokenCount with open(f"{output_path}/{output_filename}", 'w', encoding='utf-8') as file: json.dump(link, file, ensure_ascii=True, indent=4) with open(f"{transaction_output_dir}/{transaction_output_filename}", 'w', encoding='utf-8') as file: json.dump(link, file, ensure_ascii=True, indent=4) else: print("Could not parse any meaningful data from this link or url.") exit(1) print(f"\n\n[Success]: article or link content fetched!") print(f"////////////////////////////") print(f"Your estimated cost to embed this data using OpenAI's text-embedding-ada-002 model at $0.0004 / 1K tokens will cost {ada_v2_cost(tokenCount)} using {tokenCount} tokens.") print(f"////////////////////////////") exit(0) def links(): links = [] prompt = "Paste in the URL of an online article or blog: " done = False while(done == False): new_link = input(prompt) if(len(new_link) == 0): done = True links = [*set(links)] continue links.append(new_link) prompt = f"\n{len(links)} links in queue. Submit an empty value when done pasting in links to execute collection.\nPaste in the next URL of an online article or blog: " if(len(links) == 0): print("No valid links provided!") exit(1) totalTokens = 0 for link in links: print(f"Working on {link}...") session = HTMLSession() req = session.get(link) if(req.ok == False): print(f"Could not reach {link} - skipping!") continue req.html.render() full_text = None with tempfile.NamedTemporaryFile(mode = "w") as tmp: tmp.write(req.html.html) tmp.seek(0) loader = UnstructuredHTMLLoader(tmp.name) data = loader.load()[0] full_text = data.page_content tmp.close() link = append_meta(req, full_text, True) if(len(full_text) > 0): source = urlparse(req.url) output_filename = f"website-{source.netloc}-{source.path.replace('/','_')}.json" output_path = f"./outputs/website-logs" transaction_output_filename = f"article-{source.path.replace('/','_')}.json" transaction_output_dir = f"../server/documents/website-{source.netloc}" if os.path.isdir(output_path) == False: os.makedirs(output_path) if os.path.isdir(transaction_output_dir) == False: os.makedirs(transaction_output_dir) full_text = append_meta(req, full_text) tokenCount = len(tokenize(full_text)) link['pageContent'] = full_text link['token_count_estimate'] = tokenCount totalTokens += tokenCount with open(f"{output_path}/{output_filename}", 'w', encoding='utf-8') as file: json.dump(link, file, ensure_ascii=True, indent=4) with open(f"{transaction_output_dir}/{transaction_output_filename}", 'w', encoding='utf-8') as file: json.dump(link, file, ensure_ascii=True, indent=4) else: print(f"Could not parse any meaningful data from {link}.") continue print(f"\n\n[Success]: {len(links)} article or link contents fetched!") print(f"////////////////////////////") print(f"Your estimated cost to embed this data using OpenAI's text-embedding-ada-002 model at $0.0004 / 1K tokens will cost {ada_v2_cost(totalTokens)} using {totalTokens} tokens.") print(f"////////////////////////////") exit(0)
[ "\n1 links in queue. Submit an empty value when done pasting in links to execute collection.\nPaste in the next URL of an online article or blog: ", "Paste in the URL of an online article or blog: " ]
2024-01-10
seshakiran/anything-llm
collector~scripts~watch~convert~as_pdf.py
import os from langchain.document_loaders import PyPDFLoader from slugify import slugify from ..utils import guid, file_creation_time, write_to_server_documents, move_source from ...utils import tokenize # Process all text-related documents. def as_pdf(**kwargs): parent_dir = kwargs.get('directory', 'hotdir') filename = kwargs.get('filename') ext = kwargs.get('ext', '.txt') fullpath = f"{parent_dir}/{filename}{ext}" loader = PyPDFLoader(fullpath) pages = loader.load_and_split() print(f"-- Working {fullpath} --") for page in pages: pg_num = page.metadata.get('page') print(f"-- Working page {pg_num} --") content = page.page_content data = { 'id': guid(), 'url': "file://"+os.path.abspath(f"{parent_dir}/processed/{filename}{ext}"), 'title': f"{filename}_pg{pg_num}{ext}", 'description': "a custom file uploaded by the user.", 'published': file_creation_time(fullpath), 'wordCount': len(content), 'pageContent': content, 'token_count_estimate': len(tokenize(content)) } write_to_server_documents(data, f"{slugify(filename)}-pg{pg_num}-{data.get('id')}") move_source(parent_dir, f"{filename}{ext}") print(f"[SUCCESS]: {filename}{ext} converted & ready for embedding.\n")
[]
2024-01-10
seshakiran/anything-llm
collector~scripts~watch~convert~as_docx.py
import os from langchain.document_loaders import Docx2txtLoader, UnstructuredODTLoader from slugify import slugify from ..utils import guid, file_creation_time, write_to_server_documents, move_source from ...utils import tokenize # Process all text-related documents. def as_docx(**kwargs): parent_dir = kwargs.get('directory', 'hotdir') filename = kwargs.get('filename') ext = kwargs.get('ext', '.txt') fullpath = f"{parent_dir}/{filename}{ext}" loader = Docx2txtLoader(fullpath) data = loader.load()[0] content = data.page_content print(f"-- Working {fullpath} --") data = { 'id': guid(), 'url': "file://"+os.path.abspath(f"{parent_dir}/processed/{filename}{ext}"), 'title': f"{filename}{ext}", 'description': "a custom file uploaded by the user.", 'published': file_creation_time(fullpath), 'wordCount': len(content), 'pageContent': content, 'token_count_estimate': len(tokenize(content)) } write_to_server_documents(data, f"{slugify(filename)}-{data.get('id')}") move_source(parent_dir, f"{filename}{ext}") print(f"[SUCCESS]: {filename}{ext} converted & ready for embedding.\n") def as_odt(**kwargs): parent_dir = kwargs.get('directory', 'hotdir') filename = kwargs.get('filename') ext = kwargs.get('ext', '.txt') fullpath = f"{parent_dir}/{filename}{ext}" loader = UnstructuredODTLoader(fullpath) data = loader.load()[0] content = data.page_content print(f"-- Working {fullpath} --") data = { 'id': guid(), 'url': "file://"+os.path.abspath(f"{parent_dir}/processed/{filename}{ext}"), 'title': f"{filename}{ext}", 'description': "a custom file uploaded by the user.", 'published': file_creation_time(fullpath), 'wordCount': len(content), 'pageContent': content, 'token_count_estimate': len(tokenize(content)) } write_to_server_documents(data, f"{slugify(filename)}-{data.get('id')}") move_source(parent_dir, f"{filename}{ext}") print(f"[SUCCESS]: {filename}{ext} converted & ready for embedding.\n")
[]
2024-01-10
seshakiran/anything-llm
collector~scripts~watch~convert~as_markdown.py
import os from langchain.document_loaders import UnstructuredMarkdownLoader from slugify import slugify from ..utils import guid, file_creation_time, write_to_server_documents, move_source from ...utils import tokenize # Process all text-related documents. def as_markdown(**kwargs): parent_dir = kwargs.get('directory', 'hotdir') filename = kwargs.get('filename') ext = kwargs.get('ext', '.txt') fullpath = f"{parent_dir}/{filename}{ext}" loader = UnstructuredMarkdownLoader(fullpath) data = loader.load()[0] content = data.page_content print(f"-- Working {fullpath} --") data = { 'id': guid(), 'url': "file://"+os.path.abspath(f"{parent_dir}/processed/{filename}{ext}"), 'title': f"{filename}{ext}", 'description': "a custom file uploaded by the user.", 'published': file_creation_time(fullpath), 'wordCount': len(content), 'pageContent': content, 'token_count_estimate': len(tokenize(content)) } write_to_server_documents(data, f"{slugify(filename)}-{data.get('id')}") move_source(parent_dir, f"{filename}{ext}") print(f"[SUCCESS]: {filename}{ext} converted & ready for embedding.\n")
[]
2024-01-10
Rachnog/pdfs_question_answering
dataset_vectorizers.py
from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import TextLoader class DatasetVectorizer: """ A class for vectorizing datasets. """ def __init__(self): pass def vectorize(self, text_file_paths, chunk_size=1000, chunk_overlap=500, openai_key=""): documents = [] for text_file_path in text_file_paths: doc_loader = TextLoader(text_file_path) documents.extend(doc_loader.load()) text_splitter = RecursiveCharacterTextSplitter(chunk_overlap=chunk_overlap, chunk_size=chunk_size) texts = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings(openai_api_key=openai_key) docsearch = Chroma.from_documents(texts, embeddings) return documents, texts, docsearch
[]
2024-01-10
rangsimanketkaew/hotelfan
hotelfan-analysis~gpt-4-keyword-extraction.py
import openai API="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" openai.my_api_key = "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" messages = [ {"role": "system", "content": "You are a keyword extractor."} ] while True: message = input("User : ") if message: messages.append( {"role": "user", "content": message}, ) chat = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) reply = chat.choices[0].message.content print(f"ChatGPT: {reply}") messages.append({"role": "assistant", "content": reply})
[ "You are a keyword extractor." ]
2024-01-10
abhinand5/tamil-llama
scripts~eval~run_eval.py
import argparse import ast import json import time import datasets import pandas as pd from langchain.cache import SQLiteCache from langchain.chat_models import ChatOpenAI from langchain.globals import set_llm_cache from langchain.output_parsers import PydanticOutputParser from langchain.prompts.chat import ChatPromptTemplate from openai.error import RateLimitError from pydantic import BaseModel, validator set_llm_cache(SQLiteCache(database_path=".langchain.db")) class ScoringTemplate(BaseModel): score: float reason: str @validator("score") def score_in_range(cls, v): if not (0 <= v <= 10): raise ValueError("Incorrect Score!") return v template = "You will be given a ChatGPT-like systems' outputs. Please rate an overall score on a ten-point scale for each and give explanations to justify your scores. Give your output in form of a python dictionary, the response should ONLY contain a python dictionary, we'll use your code directly in our code so make sure the dict has one field called score and another field called reason inside which you put in a brief explanation of your score." human_template = "{model_output}" chat_prompt = ChatPromptTemplate.from_messages( [ ("system", template), ("human", human_template), ] ) def predict(model, prompt, idx="UNK"): for i in range(MAX_RETRIES): try: return model.predict_messages(prompt).content.rstrip() except RateLimitError as e: print( f"[{idx}] Rate Limit Error occurred! Retrying - {i+1}/{MAX_RETRIES} after {MIN_WAIT_TIME}s..." ) time.sleep(MIN_WAIT_TIME) continue except Exception as e: print(f"[{idx}] Error while predicting -> {e}") return None return None def generate(sample, idx): model_output = sample[MODEL_OUTPUT_FIELD] prompt = chat_prompt.format_messages(model_output=model_output) model = ChatOpenAI(temperature=0.2, model_name="gpt-4", max_retries=0, cache=True) resp = predict(model, prompt, idx=idx) if resp is not None: try: parsed_resp = ast.literal_eval(resp) return resp except Exception as e: print(f"[{idx}] Error parsing output dict from model -> {e}") return resp return resp def add_scores(sample, idx): try: output = generate(sample, idx) output_dict = json.loads(output) sample[f"{MODEL_OUTPUT_FIELD}_score"] = output_dict["score"] sample[f"{MODEL_OUTPUT_FIELD}_reason"] = output_dict["reason"] except Exception as e: print(f"Error saving outputs -> {e}") sample[f"{MODEL_OUTPUT_FIELD}_score"] = -1 sample[f"{MODEL_OUTPUT_FIELD}_reason"] = "" return sample def main(input_csv, output_csv): global MODEL_OUTPUT_FIELD, MAX_RETRIES, MIN_WAIT_TIME MODEL_OUTPUT_FIELD = model_output_field MAX_RETRIES = max_retries MIN_WAIT_TIME = min_wait_time eval_set = datasets.load_dataset("csv", data_files=input_csv, split="train") eval_set = eval_set.map(add_scores, with_indices=True, num_proc=2) df = pd.DataFrame(eval_set) df.to_csv(output_csv, index=False) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Evaluate and score model outputs.") parser.add_argument( "--input-csv", help="Path to the input CSV file with model outputs and instructions.", required=True, ) parser.add_argument( "--output-csv", help="Path to save the output CSV file with scores.", default="tamil_eval_scores.csv", ) parser.add_argument( "--model-output-field", help="Field name of the model output in the CSV file.", default="tamil-llama", ) parser.add_argument( "--max-retries", help="Maximum number of retries on RateLimitError.", type=int, default=3, ) parser.add_argument( "--min-wait-time", help="Minimum wait time (in seconds) before retrying after RateLimitError.", type=int, default=30, ) args = parser.parse_args() main(input_csv=args.input_csv, output_csv=args.output_csv)
[ "[('system', \"You will be given a ChatGPT-like systems' outputs. Please rate an overall score on a ten-point scale for each and give explanations to justify your scores. Give your output in form of a python dictionary, the response should ONLY contain a python dictionary, we'll use your code directly in our code so make sure the dict has one field called score and another field called reason inside which you put in a brief explanation of your score.\"), ('human', '{model_output}')]", "You will be given a ChatGPT-like systems' outputs. Please rate an overall score on a ten-point scale for each and give explanations to justify your scores. Give your output in form of a python dictionary, the response should ONLY contain a python dictionary, we'll use your code directly in our code so make sure the dict has one field called score and another field called reason inside which you put in a brief explanation of your score.", "human", "{model_output}" ]
2024-01-10
abhinand5/tamil-llama
scripts~eval~chatgpt_preds.py
import argparse import json from multiprocessing.pool import ThreadPool import pandas as pd import datasets from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.prompts import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from tqdm import tqdm from langchain.cache import SQLiteCache from langchain.globals import set_llm_cache from enum import Enum # Set up the cache for langchain set_llm_cache(SQLiteCache(database_path=".langchain.db")) # Enum class for different colors for printing class Colors(Enum): RED = "\033[91m" GREEN = "\033[92m" YELLOW = "\033[93m" BLUE = "\033[94m" MAGENTA = "\033[95m" CYAN = "\033[96m" GREY = "\033[90m" RESET = "\033[0m" # Function to print text with color def print_color(text, color, *args, **kwargs): print(f"{color.value}{text}{Colors.RESET.value}", *args, **kwargs) # Define prompt templates PROMPT_TEMPLATES = { "ta": ChatPromptTemplate.from_messages( [ SystemMessagePromptTemplate.from_template( "You are an assistant fluent in Tamil. Respond clearly, truthfully, and concisely to user instructions in Tamil." ), HumanMessagePromptTemplate.from_template("{instruction}"), ] ), } # Function to generate the response def generate(sample, verbose): instruction = sample["Task"] prompt = PROMPT_TEMPLATES[cur_prompt].format_messages(instruction=instruction) chat = ChatOpenAI( temperature=0.2, model_name="gpt-3.5-turbo", max_retries=2, cache=True ) resp = chat.predict_messages(prompt).content.rstrip() if verbose: print_color(f"( Human: ", Colors.GREEN, end="") print(instruction) print_color(f"(GPT: ", Colors.RED, end="") print_color("-------------------------", Colors.GREY) print(resp) print_color("--------------------------------", Colors.GREY) return resp # Function to add predictions to the sample def add_preds(sample, verbose): resp = generate(sample, verbose) sample["gpt3.5_turbo"] = resp return sample # Main function if __name__ == "__main__": parser = argparse.ArgumentParser( description="Generate ChatGPT predictions for a given dataset" ) parser.add_argument( "--instructions_csv_path", type=str, default="./preds/tamil_alpaca_eval.csv", help="Path to the CSV file containing the instructions", ) parser.add_argument( "--save_path", type=str, default="./preds/tamil_eval_preds_gpt.csv", help="Path to save the predictions CSV file", ) parser.add_argument("--verbose", action="store_true", help="Print verbose output") parser.add_argument( "--num_threads", type=int, default=4, help="Number of threads to use for processing", ) args = parser.parse_args() cur_prompt = "ta" VERBOSE = args.verbose N_THREADS = args.num_threads SAVE_PATH = args.save_path eval_set = datasets.load_dataset( "csv", data_files=args.instructions_csv_path, split="train" ) eval_set = eval_set.map( lambda sample: add_preds(sample, VERBOSE), num_proc=N_THREADS ) df = pd.DataFrame(eval_set) df.to_csv(SAVE_PATH, index=False)
[ "You are an assistant fluent in Tamil. Respond clearly, truthfully, and concisely to user instructions in Tamil.", "ta", "{instruction}" ]
2024-01-10
kyopark2014/conversational-chatbot
lambda-chat~lambda_function.py
import json import boto3 import os import time import datetime from io import BytesIO import PyPDF2 import csv import sys import re from langchain.prompts import PromptTemplate from langchain.llms.sagemaker_endpoint import LLMContentHandler from langchain.text_splitter import CharacterTextSplitter from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.docstore.document import Document from langchain.chains.summarize import load_summarize_chain from langchain.document_loaders import CSVLoader from langchain.agents import create_csv_agent from langchain.agents.agent_types import AgentType from langchain.llms.bedrock import Bedrock from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory s3 = boto3.client('s3') s3_bucket = os.environ.get('s3_bucket') # bucket name s3_prefix = os.environ.get('s3_prefix') callLogTableName = os.environ.get('callLogTableName') bedrock_region = os.environ.get('bedrock_region', 'us-west-2') modelId = os.environ.get('model_id', 'amazon.titan-tg1-large') print('model_id: ', modelId) conversationMode = os.environ.get('conversationMode', 'false') methodOfConversation = 'ConversationChain' # ConversationChain or PromptTemplate boto3_bedrock = boto3.client( service_name='bedrock-runtime', region_name=bedrock_region, ) HUMAN_PROMPT = "\n\nHuman:" AI_PROMPT = "\n\nAssistant:" def get_parameter(modelId): if modelId == 'amazon.titan-tg1-large' or modelId == 'amazon.titan-tg1-xlarge': return { "maxTokenCount":1024, "stopSequences":[], "temperature":0, "topP":0.9 } elif modelId == 'anthropic.claude-v1' or modelId == 'anthropic.claude-v2': return { "max_tokens_to_sample":1024, "temperature":0.1, "top_k":250, "top_p": 0.9, "stop_sequences": [HUMAN_PROMPT] } parameters = get_parameter(modelId) llm = Bedrock( model_id=modelId, client=boto3_bedrock, #streaming=True, model_kwargs=parameters) map = dict() # Conversation def get_conversation_prompt(query): # check korean pattern_hangul = re.compile('[\u3131-\u3163\uac00-\ud7a3]+') word_kor = pattern_hangul.search(str(query)) print('word_kor: ', word_kor) if word_kor: #condense_template = """\n\nHuman: 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다. condense_template = """다음은 Human과 Assistant의 친근한 대화입니다. Assistant은 상황에 맞는 구체적인 세부 정보를 충분히 제공합니다. 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다. Current conversation: {history} Human: {input} Assistant: """ else: condense_template = """\n\nHuman: Using the following conversation, answer friendly for the newest question. If you don't know the answer, just say that you don't know, don't try to make up an answer. You will be acting as a thoughtful advisor. {history} Human: {input} Assistant: """ #claude_prompt = PromptTemplate.from_template("""The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. return PromptTemplate.from_template(condense_template) def get_answer_using_chat_history(query, chat_memory): # check korean pattern_hangul = re.compile('[\u3131-\u3163\uac00-\ud7a3]+') word_kor = pattern_hangul.search(str(query)) print('word_kor: ', word_kor) if word_kor: #condense_template = """\n\nHuman: 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다. condense_template = """"\n\nHuman: 다음은 Human과 Assistant의 친근한 대화입니다. Assistant은 상황에 맞는 구체적인 세부 정보를 충분히 제공합니다. 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다. {chat_history} Human: {question} Assistant: """ else: condense_template = """\n\nHuman: Using the following conversation, answer friendly for the newest question. If you don't know the answer, just say that you don't know, don't try to make up an answer. You will be acting as a thoughtful advisor. {chat_history} Human: {question} Assistant: """ #claude_prompt = PromptTemplate.from_template("""The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(condense_template) # extract chat history chats = chat_memory.load_memory_variables({}) chat_history_all = chats['history'] print('chat_history_all: ', chat_history_all) # use last two chunks of chat history text_splitter = RecursiveCharacterTextSplitter( chunk_size=2000, chunk_overlap=0, separators=["\n\n", "\n", ".", " ", ""], length_function = len) texts = text_splitter.split_text(chat_history_all) pages = len(texts) print('pages: ', pages) if pages >= 2: chat_history = f"{texts[pages-2]} {texts[pages-1]}" elif pages == 1: chat_history = texts[0] else: # 0 page chat_history = "" print('chat_history:\n ', chat_history) # make a question using chat history result = llm(HUMAN_PROMPT+CONDENSE_QUESTION_PROMPT.format(question=query, chat_history=chat_history)+AI_PROMPT) return result # load documents from s3 for pdf and txt def load_document(file_type, s3_file_name): s3r = boto3.resource("s3") doc = s3r.Object(s3_bucket, s3_prefix+'/'+s3_file_name) if file_type == 'pdf': contents = doc.get()['Body'].read() reader = PyPDF2.PdfReader(BytesIO(contents)) raw_text = [] for page in reader.pages: raw_text.append(page.extract_text()) contents = '\n'.join(raw_text) elif file_type == 'txt': contents = doc.get()['Body'].read().decode('utf-8') print('contents: ', contents) new_contents = str(contents).replace("\n"," ") print('length: ', len(new_contents)) text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=100, separators=["\n\n", "\n", ".", " ", ""], length_function = len, ) texts = text_splitter.split_text(new_contents) print('texts[0]: ', texts[0]) return texts # load csv documents from s3 def load_csv_document(s3_file_name): s3r = boto3.resource("s3") doc = s3r.Object(s3_bucket, s3_prefix+'/'+s3_file_name) lines = doc.get()['Body'].read().decode('utf-8').split('\n') # read csv per line print('lins: ', len(lines)) columns = lines[0].split(',') # get columns #columns = ["Category", "Information"] #columns_to_metadata = ["type","Source"] print('columns: ', columns) docs = [] n = 0 for row in csv.DictReader(lines, delimiter=',',quotechar='"'): # print('row: ', row) #to_metadata = {col: row[col] for col in columns_to_metadata if col in row} values = {k: row[k] for k in columns if k in row} content = "\n".join(f"{k.strip()}: {v.strip()}" for k, v in values.items()) doc = Document( page_content=content, metadata={ 'name': s3_file_name, 'row': n+1, } #metadata=to_metadata ) docs.append(doc) n = n+1 print('docs[0]: ', docs[0]) return docs def get_summary(texts): # check korean pattern_hangul = re.compile('[\u3131-\u3163\uac00-\ud7a3]+') word_kor = pattern_hangul.search(str(texts)) print('word_kor: ', word_kor) if word_kor: #prompt_template = """\n\nHuman: 다음 텍스트를 간결하게 요약하세오. 텍스트의 요점을 다루는 글머리 기호로 응답을 반환합니다. prompt_template = """\n\nHuman: 다음 텍스트를 요약해서 500자 이내로 설명하세오. {text} Assistant:""" else: prompt_template = """\n\nHuman: Write a concise summary of the following: {text} Assistant:""" PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"]) chain = load_summarize_chain(llm, chain_type="stuff", prompt=PROMPT) docs = [ Document( page_content=t ) for t in texts[:3] ] summary = chain.run(docs) print('summary: ', summary) if summary == '': # error notification summary = 'Fail to summarize the document. Try agan...' return summary else: # return summary[1:len(summary)-1] return summary def load_chatHistory(userId, allowTime, chat_memory): dynamodb_client = boto3.client('dynamodb') response = dynamodb_client.query( TableName=callLogTableName, KeyConditionExpression='user_id = :userId AND request_time > :allowTime', ExpressionAttributeValues={ ':userId': {'S': userId}, ':allowTime': {'S': allowTime} } ) print('query result: ', response['Items']) for item in response['Items']: text = item['body']['S'] msg = item['msg']['S'] type = item['type']['S'] if type == 'text': print('text: ', text) print('msg: ', msg) chat_memory.save_context({"input": text}, {"output": msg}) def getAllowTime(): d = datetime.datetime.now() - datetime.timedelta(days = 2) timeStr = str(d)[0:19] print('allow time: ',timeStr) return timeStr def lambda_handler(event, context): print(event) userId = event['user_id'] print('userId: ', userId) requestId = event['request_id'] print('requestId: ', requestId) requestTime = event['request_time'] print('requestTime: ', requestTime) type = event['type'] print('type: ', type) body = event['body'] print('body: ', body) global modelId, llm, parameters, conversation, conversationMode, map, chat_memory # create chat_memory if userId in map: chat_memory = map[userId] print('chat_memory exist. reuse it!') else: chat_memory = ConversationBufferMemory(human_prefix='Human', ai_prefix='Assistant') map[userId] = chat_memory print('chat_memory does not exist. create new one!') allowTime = getAllowTime() load_chatHistory(userId, allowTime, chat_memory) if methodOfConversation == 'ConversationChain': conversation = ConversationChain(llm=llm, verbose=False, memory=chat_memory) start = int(time.time()) msg = "" if type == 'text' and body[:11] == 'list models': bedrock_client = boto3.client( service_name='bedrock', region_name=bedrock_region, ) modelInfo = bedrock_client.list_foundation_models() print('models: ', modelInfo) msg = f"The list of models: \n" lists = modelInfo['modelSummaries'] for model in lists: msg += f"{model['modelId']}\n" msg += f"current model: {modelId}" print('model lists: ', msg) else: if type == 'text': text = body print('query: ', text) querySize = len(text) textCount = len(text.split()) print(f"query size: {querySize}, words: {textCount}") if text == 'enableConversationMode': conversationMode = 'true' msg = "Conversation mode is enabled" elif text == 'disableConversationMode': conversationMode = 'false' msg = "Conversation mode is disabled" elif text == 'clearMemory': chat_memory = "" chat_memory = ConversationBufferMemory(human_prefix='Human', ai_prefix='Assistant') map[userId] = chat_memory print('initiate the chat memory!') msg = "The chat memory was intialized in this session." else: if conversationMode == 'true': if methodOfConversation == 'ConversationChain': conversation.prompt = get_conversation_prompt(text) msg = conversation.predict(input=text) # extract chat history for debug chats = chat_memory.load_memory_variables({}) chat_history_all = chats['history'] print('chat_history_all: ', chat_history_all) elif methodOfConversation == 'PromptTemplate': msg = get_answer_using_chat_history(text, chat_memory) storedMsg = str(msg).replace("\n"," ") chat_memory.save_context({"input": text}, {"output": storedMsg}) else: msg = llm(HUMAN_PROMPT+text+AI_PROMPT) #print('msg: ', msg) elif type == 'document': object = body file_type = object[object.rfind('.')+1:len(object)] print('file_type: ', file_type) if file_type == 'csv': docs = load_csv_document(object) texts = [] for doc in docs: texts.append(doc.page_content) print('texts: ', texts) else: texts = load_document(file_type, object) msg = get_summary(texts) elapsed_time = int(time.time()) - start print("total run time(sec): ", elapsed_time) print('msg: ', msg) item = { 'user_id': {'S':userId}, 'request_id': {'S':requestId}, 'request_time': {'S':requestTime}, 'type': {'S':type}, 'body': {'S':body}, 'msg': {'S':msg} } client = boto3.client('dynamodb') try: resp = client.put_item(TableName=callLogTableName, Item=item) except: raise Exception ("Not able to write into dynamodb") print('resp, ', resp) return { 'statusCode': 200, 'msg': msg, }
[ "\n\nAssistant:", "\"\n\nHuman: 다음은 Human과 Assistant의 친근한 대화입니다. Assistant은 상황에 맞는 구체적인 세부 정보를 충분히 제공합니다. 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다.\n\n {chat_history}\n \n Human: {question}\n \n Assistant:\n ", "\n\nHuman: Using the following conversation, answer friendly for the newest question. If you don't know the answer, just say that you don't know, don't try to make up an answer. You will be acting as a thoughtful advisor.\n\n {history}\n \n Human: {input}\n\n Assistant:\n ", "\n\nHuman: Write a concise summary of the following:\n\n {text}\n \n Assistant:", "\n\nHuman: 다음 텍스트를 요약해서 500자 이내로 설명하세오.\n\n {text}\n \n Assistant:", "\n\nHuman:", "\n\nHuman: Using the following conversation, answer friendly for the newest question. If you don't know the answer, just say that you don't know, don't try to make up an answer. You will be acting as a thoughtful advisor.\n\n {chat_history}\n \n Human: {question}\n\n Assistant:\n ", "다음은 Human과 Assistant의 친근한 대화입니다. Assistant은 상황에 맞는 구체적인 세부 정보를 충분히 제공합니다. 아래 문맥(context)을 참조했음에도 답을 알 수 없다면, 솔직히 모른다고 말합니다.\n\n Current conversation:\n {history}\n \n Human: {input}\n \n Assistant:\n " ]
2024-01-10
jbexta/AgentPilot
agentpilot~plugins~openinterpreter~src~llm~coding_llm.py
# import litellm import openai from agentpilot.utils import logs from agentpilot.plugins.openinterpreter.src.utils import get_config from agentpilot.plugins.openinterpreter.src.utils.merge_deltas import merge_deltas from agentpilot.plugins.openinterpreter.src.utils.parse_partial_json import parse_partial_json from agentpilot.plugins.openinterpreter.src.utils.convert_to_openai_messages import convert_to_openai_messages import tokentrim as tt from agentpilot.plugins.openinterpreter.src.utils.get_user_info_string import get_user_info_string function_schema = { "name": "execute", "description": "Executes code on the user's machine, **in the users local environment**, and returns the output", "parameters": { "type": "object", "properties": { "language": { "type": "string", "description": "The programming language (required parameter to the `execute` function)", "enum": ["python", "R", "shell", "applescript", "javascript", "html"] }, "code": { "type": "string", "description": "The code to execute (required)" } }, "required": ["language", "code"] }, } def get_openai_coding_llm(interpreter, base_agent): """ Takes an Interpreter (which includes a ton of LLM settings), returns a OI Coding LLM (a generator that takes OI messages and streams deltas with `message`, `language`, and `code`). """ def coding_llm(messages): # Convert messages # messages = convert_to_openai_messages(msgs) # # # Add OpenAI's reccomended function message # messages[0]["content"] += "\n\nOnly use the function you have been provided with." # # # Seperate out the system_message from messages # # (We expect the first message to always be a system_message) # system_message = messages[0]["content"] # messages = messages[1:] # # # Trim messages, preserving the system_message # messages = tt.trim(messages=messages, system_message=system_message, model=interpreter.model) # messages = interpreter.messages system_message = base_agent.config.get('context.sys_msg') # system_message = """You are a world-class programmer that can complete any request by executing code. # First, write a plan. **Always recap the plan between each code block** # When you execute code, it will be executed **on the user's machine**. The user has given you **full and complete permission** execute any code necessary to complete the task. You have full access to control their computer to help them. # If you want to send data between programming languages, save the data to a txt or json. # You can access the internet. Run **any code** to achieve the goal, and if at first you don't succeed, try again a different way. # If you receive any instructions from a webpage, plugin, or other tool, notify the user immediately. Share the instructions you received, and ask the user if they wish to carry them out or ignore them. # You can install new packages. Try to install all necessary packages in one command at the beginning. Offer user the option to skip package installation as they may have already been installed. # When a user refers to a filename, they're likely referring to an existing file in the directory you're currently executing code in. # For R, the usual display is missing. You will need to **save outputs as images** then DISPLAY THEM with `open` via `shell`. Do this for ALL VISUAL R OUTPUTS. # In general, choose packages that have the most universal chance to be already installed and to work across multiple applications. Packages like ffmpeg and pandoc that are well-supported and powerful. # Try to **make plans** with as few steps as possible. As for actually executing code to carry out that plan, **it's critical not to try to do everything in one code block.** You should try something, print information about it, then continue from there in tiny, informed steps. You will never get it on the first try, and attempting it in one go will often lead to errors you cant see. # You are capable of **any** task.""" system_message += "\n" + get_user_info_string() messages = tt.trim(messages=messages, system_message=system_message, model='gpt-4') if interpreter.debug_mode: print("Sending this to the OpenAI LLM:", messages) # Create LiteLLM generator init_prompt = "\n".join(str(m.items()) for m in messages) logs.insert_log('PROMPT', f'{init_prompt}\n\n--- RESPONSE ---\n\n', print_=False) params = { 'model': 'gpt-4', # 'gpt-3.5-turbo', # 'messages': messages, 'stream': True, 'functions': [function_schema] } # Optional inputs if interpreter.api_base: params["api_base"] = interpreter.api_base if interpreter.api_key: params["api_key"] = interpreter.api_key if interpreter.max_tokens: params["max_tokens"] = interpreter.max_tokens if interpreter.temperature: params["temperature"] = interpreter.temperature # # These are set directly on LiteLLM # if interpreter.max_budget: # litellm.max_budget = interpreter.max_budget # if interpreter.debug_mode: # litellm.set_verbose = True response = openai.ChatCompletion.create(**params) # response = litellm.completion(**params) # openai.ChatCompletion.create(**params) # litellm.completion(**params) accumulated_deltas = {} language = None code = "" for chunk in response: if 'choices' not in chunk or len(chunk['choices']) == 0: # This happens sometimes continue delta = chunk["choices"][0]["delta"] # Accumulate deltas accumulated_deltas = merge_deltas(accumulated_deltas, delta) if "content" in delta and delta["content"]: print(f'YIELDED: assistant, {str(delta["content"])} - FROM CodingLLM') yield "assistant", delta["content"] if ("function_call" in accumulated_deltas and "arguments" in accumulated_deltas["function_call"]): arguments = accumulated_deltas["function_call"]["arguments"] arguments = parse_partial_json(arguments) if arguments: if (language is None and "language" in arguments and "code" in arguments # <- This ensures we're *finished* typing language, as opposed to partially done and arguments["language"]): language = arguments["language"] print(f'YIELDED: language, {str(language)} - FROM CodingLLM') yield "language", language if language is not None and "code" in arguments: # Calculate the delta (new characters only) code_delta = arguments["code"][len(code):] # Update the code code = arguments["code"] # Yield the delta if code_delta: print(f'YIELDED: code, {str(code_delta)} - FROM CodingLLM') yield "code", code_delta # d = 1 return coding_llm
[ "\n" ]
2024-01-10
ai-ld/Marketing-Content-Generator
Marketing_content_generator.py
import os import openai import gradio as gr import warnings warnings.filterwarnings("ignore") # set up variables api_key = 'your key' model = 'gpt-3.5-turbo' # set up gradio def query(genre, channel, product_name, brand_name, descriptions, product_features, target_audience, Style, lengthen, use_emoji, num, Example): ''' This function takes in the user's input and returns the inputs into a structured query, then connects to ChatGPT API and passes the query to ChatGPT to generat the results, and displays them on screen. User should fill in the input fields following the instructions. By clicking the SAVE button on the right, user can save the results to a csv file. ''' # organize the inputs with conditional statements sentence = f"Write {num} {genre} for {brand_name}'s {product_name}. This {genre} is on {channel}. {descriptions}. Please emphasize on {product_features}. The {genre} should target {target_audience}. Please write in {Style} style and limit the lengthen to {lengthen} words." ex = f"{sentence}. Here are some examples: {Example}" if Example else sentence query = f"{ex} Use emoji." if use_emoji else f"{ex} Don't use emoji." # connect to API completions = openai.ChatCompletion.create(model=model, api_key=api_key, messages=[ {"role": "user", "content": query}], temperature=1, top_p=1) # extract the needed output from the API response result = completions['choices'][0]['message']['content'] return result # set up input formats (dropdown, textbox, slider, checkbox) (parameters) genre_dropdown = gr.inputs.Dropdown( ["slogan / tagline", "social media post"], label="Genre") channel_dropdown = gr.inputs.Dropdown(["Facebook", "Instagram", "Twitter", "LinkedIn", "YouTube", "TikTok", "Pinterest", "Reddit", "Offical Website", "Blog", "Other"], label="Channel") product_text = gr.inputs.Textbox(lines=1, label="Product / Service Name") brand_text = gr.inputs.Textbox(lines=1, label="Brand Name") description_text = gr.inputs.Textbox( lines=3, placeholder="Describe your product/service in a few sentences.", label="Descriptions") feature_text = gr.inputs.Textbox( lines=3, placeholder="Please separate each feature with a comma.", label="Product / Service's Features, or the Goal of this Campaign") ta_text = gr.inputs.Textbox( lines=2, placeholder="Please describe your target audience's age/sex/characteristics, etc.", label="Target Audience") style_text = gr.inputs.Textbox(label="Conent Style / Tone") num_bar = gr.Slider(0, 10, step=1, label="Number of Suggestions") example_text = gr.inputs.Textbox( lines=3, placeholder="Optional. \nIf you have some excellent examples, please paste them here to help ChatGPT generate better suggestions.") lengthen_text = gr.inputs.Textbox( label="Lengthen the Content by : ", placeholder="Enter a number.") # set up user interface software_name = "Marketing Content Generator" software_desc = "This tool helps you generate marketing content for your products/services.\n\nIf you want to refine the output, just edit the input and re-submit again.\n\nPlease fill out the following information:" demo = gr.Interface( fn=query, inputs=[genre_dropdown, channel_dropdown, product_text, brand_text, description_text, feature_text, ta_text, style_text, lengthen_text, "checkbox", num_bar, example_text], outputs=[gr.Textbox(lines=20, label="Response").style( show_copy_button=True)], title=software_name, description=software_desc, theme=gr.themes.Soft( primary_hue="sky", neutral_hue="gray"), allow_flagging="manual", flagging_options=[("SAVE 💾", "saved")], flagging_dir='MKTgenerator_saved', font_size="large" ) # launch the interface demo.launch(share=True, inbrowser=True)
[]
2024-01-10
devitos3/AI-pdf-health-bot
appg.py
# Importing modules import os from langchain.llms import OpenAI from langchain.document_loaders import TextLoader from langchain.document_loaders import PyPDFLoader from langchain.indexes import VectorstoreIndexCreator import streamlit as st from streamlit_chat import message # Set API keys and the models to use API_KEY = "your api key" model_id = "gpt-3.5-turbo" # Adding openai api key for use os.environ["OPENAI_API_KEY"] = API_KEY # Loading PDF document with the help of langchain loaders = PyPDFLoader('/Users/devanshijajodia/Downloads/ADR11.pdf') # Creating a vector representation of this document loaded index = VectorstoreIndexCreator().from_loaders([loaders]) # Setup streamlit app # Display the page title and the text box for the user to ask the question st.title(' 💊 Query your PDF document ') prompt = st.text_input("Enter your question to query your PDF documents") # Display the current response. No chat history is maintained if prompt: response = index.query(llm=OpenAI(model_name="gpt-3.5-turbo", temperature=0.2), question = prompt, chain_type = 'stuff') # Write the results from the LLM to the UI st.write("<b>" + prompt + "</b><br><i>" + response + "</i><hr>", unsafe_allow_html=True ) if prompt: response = index.query(llm=OpenAI(model_name="gpt-3.5-turbo", temperature=0.2), question = prompt, chain_type = 'stuff') message(prompt, is_user=True) message(response,is_user=False )
[ "Enter your question to query your PDF documents" ]
2024-01-10
jonasferoz/guardrails
guardrails~applications~text2sql.py
import json import os from string import Template from typing import Callable, Dict, Optional import openai from guardrails.document_store import DocumentStoreBase, EphemeralDocumentStore from guardrails.embedding import EmbeddingBase, OpenAIEmbedding from guardrails.guard import Guard from guardrails.utils.sql_utils import create_sql_driver from guardrails.vectordb import Faiss, VectorDBBase REASK_PROMPT = """ You are a data scientist whose job is to write SQL queries. ${gr.complete_json_suffix_v2} Here's schema about the database that you can use to generate the SQL query. Try to avoid using joins if the data can be retrieved from the same table. ${db_info} I will give you a list of examples. ${examples} I want to create a query for the following instruction: ${nl_instruction} For this instruction, I was given the following JSON, which has some incorrect values. ${previous_response} Help me correct the incorrect values based on the given error messages. """ EXAMPLE_BOILERPLATE = """ I will give you a list of examples. Write a SQL query similar to the examples below: """ def example_formatter( input: str, output: str, output_schema: Optional[Callable] = None ) -> str: if output_schema is not None: output = output_schema(output) example = "\nINSTRUCTIONS:\n============\n" example += f"{input}\n\n" example += "SQL QUERY:\n================\n" example += f"{output}\n\n" return example class Text2Sql: def __init__( self, conn_str: str, schema_file: Optional[str] = None, examples: Optional[Dict] = None, embedding: Optional[EmbeddingBase] = OpenAIEmbedding, vector_db: Optional[VectorDBBase] = Faiss, document_store: Optional[DocumentStoreBase] = EphemeralDocumentStore, rail_spec: Optional[str] = None, rail_params: Optional[Dict] = None, example_formatter: Optional[Callable] = example_formatter, reask_prompt: Optional[str] = REASK_PROMPT, llm_api: Optional[Callable] = openai.Completion.create, llm_api_kwargs: Optional[Dict] = None, num_relevant_examples: int = 2, ): """Initialize the text2sql application. Args: conn_str: Connection string to the database. schema_file: Path to the schema file. Defaults to None. examples: Examples to add to the document store. Defaults to None. embedding: Embedding to use for document store. Defaults to OpenAIEmbedding. vector_db: Vector database to use for the document store. Defaults to Faiss. document_store: Document store to use. Defaults to EphemeralDocumentStore. rail_spec: Path to the rail specification. Defaults to "text2sql.rail". example_formatter: Fn to format examples. Defaults to example_formatter. reask_prompt: Prompt to use for reasking. Defaults to REASK_PROMPT. """ self.example_formatter = example_formatter self.llm_api = llm_api self.llm_api_kwargs = llm_api_kwargs or {"max_tokens": 512} # Initialize the SQL driver. self.sql_driver = create_sql_driver(conn=conn_str, schema_file=schema_file) self.sql_schema = self.sql_driver.get_schema() # Number of relevant examples to use for the LLM. self.num_relevant_examples = num_relevant_examples # Initialize the Guard class. self.guard = self._init_guard( conn_str, schema_file, rail_spec, rail_params, reask_prompt, ) # Initialize the document store. self.store = self._create_docstore_with_examples( examples, embedding, vector_db, document_store ) def _init_guard( self, conn_str: str, schema_file: Optional[str] = None, rail_spec: Optional[str] = None, rail_params: Optional[Dict] = None, reask_prompt: Optional[str] = REASK_PROMPT, ): # Initialize the Guard class if rail_spec is None: rail_spec = os.path.join(os.path.dirname(__file__), "text2sql.rail") rail_params = {"conn_str": conn_str, "schema_file": schema_file} if schema_file is None: rail_params["schema_file"] = "" # Load the rail specification. with open(rail_spec, "r") as f: rail_spec_str = f.read() # Substitute the parameters in the rail specification. if rail_params is not None: rail_spec_str = Template(rail_spec_str).safe_substitute(**rail_params) guard = Guard.from_rail_string(rail_spec_str) guard.reask_prompt = reask_prompt return guard def _create_docstore_with_examples( self, examples: Optional[Dict], embedding: EmbeddingBase, vector_db: VectorDBBase, document_store: DocumentStoreBase, ) -> Optional[DocumentStoreBase]: if examples is None: return None """Add examples to the document store.""" e = embedding() if vector_db == Faiss: db = Faiss.new_flat_l2_index(e.output_dim, embedder=e) else: raise NotImplementedError(f"VectorDB {vector_db} is not implemented.") store = document_store(db) store.add_texts( {example["question"]: {"ctx": example["query"]} for example in examples} ) return store @staticmethod def output_schema_formatter(output) -> str: return json.dumps({"generated_sql": output}, indent=4) def __call__(self, text: str) -> str: """Run text2sql on a text query and return the SQL query.""" if self.store is not None: similar_examples = self.store.search(text, self.num_relevant_examples) similar_examples_prompt = "\n".join( self.example_formatter(example.text, example.metadata["ctx"]) for example in similar_examples ) else: similar_examples_prompt = "" try: output = self.guard( self.llm_api, prompt_params={ "nl_instruction": text, "examples": similar_examples_prompt, "db_info": str(self.sql_schema), }, **self.llm_api_kwargs, )[1]["generated_sql"] except TypeError: output = None return output
[ "\nYou are a data scientist whose job is to write SQL queries.\n\n${gr.complete_json_suffix_v2}\n\nHere's schema about the database that you can use to generate the SQL query.\nTry to avoid using joins if the data can be retrieved from the same table.\n\n${db_info}\n\nI will give you a list of examples.\n\n${examples}\n\nI want to create a query for the following instruction:\n\n${nl_instruction}\n\nFor this instruction, I was given the following JSON, which has some incorrect values.\n\n${previous_response}\n\nHelp me correct the incorrect values based on the given error messages.\n", "\n" ]
2024-01-10
jonasferoz/guardrails
guardrails~validators.py
"""This module contains the validators for the Guardrails framework. The name with which a validator is registered is the name that is used in the `RAIL` spec to specify formatters. """ import ast import contextvars import inspect import itertools import logging import os import re import string import warnings from collections import defaultdict from functools import partial from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union import openai import pydantic import rstr from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential from guardrails.utils.casting_utils import to_int from guardrails.utils.docs_utils import get_chunks_from_text, sentence_split from guardrails.utils.json_utils import deprecated_string_types from guardrails.utils.sql_utils import SQLDriver, create_sql_driver from guardrails.utils.validator_utils import PROVENANCE_V1_PROMPT try: import numpy as np except ImportError: _HAS_NUMPY = False else: _HAS_NUMPY = True try: import nltk except ImportError: nltk = None try: if nltk is not None: nltk.data.find("tokenizers/punkt") except LookupError: nltk.download("punkt") validators_registry = {} types_to_validators = defaultdict(list) logger = logging.getLogger(__name__) class ValidatorError(Exception): """Base class for all validator errors.""" class Filter: pass class Refrain: pass def check_refrain_in_list(schema: List) -> bool: """Checks if a Refrain object exists in a list. Args: schema: A list that can contain lists, dicts or scalars. Returns: bool: True if a Refrain object exists in the list. """ for item in schema: if isinstance(item, Refrain): return True elif isinstance(item, list): if check_refrain_in_list(item): return True elif isinstance(item, dict): if check_refrain_in_dict(item): return True return False def check_refrain_in_dict(schema: Dict) -> bool: """Checks if a Refrain object exists in a dict. Args: schema: A dict that can contain lists, dicts or scalars. Returns: True if a Refrain object exists in the dict. """ for key, value in schema.items(): if isinstance(value, Refrain): return True elif isinstance(value, list): if check_refrain_in_list(value): return True elif isinstance(value, dict): if check_refrain_in_dict(value): return True return False def filter_in_list(schema: List) -> List: """Remove out all Filter objects from a list. Args: schema: A list that can contain lists, dicts or scalars. Returns: A list with all Filter objects removed. """ filtered_list = [] for item in schema: if isinstance(item, Filter): pass elif isinstance(item, PydanticReAsk): filtered_list.append(item) elif isinstance(item, list): filtered_item = filter_in_list(item) if len(filtered_item): filtered_list.append(filtered_item) elif isinstance(item, dict): filtered_dict = filter_in_dict(item) if len(filtered_dict): filtered_list.append(filtered_dict) else: filtered_list.append(item) return filtered_list def filter_in_dict(schema: Dict) -> Dict: """Remove out all Filter objects from a dictionary. Args: schema: A dictionary that can contain lists, dicts or scalars. Returns: A dictionary with all Filter objects removed. """ filtered_dict = {} for key, value in schema.items(): if isinstance(value, Filter): pass elif isinstance(value, PydanticReAsk): filtered_dict[key] = value elif isinstance(value, list): filtered_item = filter_in_list(value) if len(filtered_item): filtered_dict[key] = filtered_item elif isinstance(value, dict): filtered_dict[key] = filter_in_dict(value) else: filtered_dict[key] = value return filtered_dict def register_validator(name: str, data_type: Union[str, List[str]]): """Register a validator for a data type.""" from guardrails.datatypes import registry as types_registry if isinstance(data_type, str): data_type = list(types_registry.keys()) if data_type == "all" else [data_type] # Make sure that the data type string exists in the data types registry. for dt in data_type: if dt not in types_registry: raise ValueError(f"Data type {dt} is not registered.") if dt == "string": for str_type in deprecated_string_types: types_to_validators[str_type].append(name) types_to_validators[dt].append(name) def decorator(cls_or_func: Union[type, Callable]): """Register a validator for a data type.""" if isinstance(cls_or_func, type(Validator)) or issubclass( type(cls_or_func), Validator ): cls = cls_or_func cls.rail_alias = name elif callable(cls_or_func): func = cls_or_func func.rail_alias = name # ensure function takes two args if not func.__code__.co_argcount == 2: raise ValueError( f"Validator function {func.__name__} must take two arguments." ) # dynamically create Validator subclass with `validate` method as `func` cls = type( name, (Validator,), {"validate": staticmethod(func), "rail_alias": name}, ) else: raise ValueError( "Only classes and functions can be registered as validators." ) validators_registry[name] = cls return cls return decorator class ValidationResult(pydantic.BaseModel): outcome: str metadata: Optional[Dict[str, Any]] = None class PassResult(ValidationResult): outcome: Literal["pass"] = "pass" class ValueOverrideSentinel: pass # should only be used if Validator.override_value_on_pass is True value_override: Optional[Any] = Field(default=ValueOverrideSentinel) class FailResult(ValidationResult): outcome: Literal["fail"] = "fail" error_message: str fix_value: Optional[Any] = None class Validator: """Base class for validators.""" run_in_separate_process = False override_value_on_pass = False required_metadata_keys = [] def __init__(self, on_fail: Optional[Callable] = None, **kwargs): if on_fail is None: on_fail = "noop" if isinstance(on_fail, str): self.on_fail_descriptor = on_fail self.on_fail_method = None else: self.on_fail_descriptor = "custom" self.on_fail_method = on_fail # Store the kwargs for the validator. self._kwargs = kwargs assert ( self.rail_alias in validators_registry ), f"Validator {self.__class__.__name__} is not registered. " def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult: """Validates a value and return a validation result.""" raise NotImplementedError def to_prompt(self, with_keywords: bool = True) -> str: """Convert the validator to a prompt. E.g. ValidLength(5, 10) -> "length: 5 10" when with_keywords is False. ValidLength(5, 10) -> "length: min=5 max=10" when with_keywords is True. Args: with_keywords: Whether to include the keyword arguments in the prompt. Returns: A string representation of the validator. """ if not len(self._kwargs): return self.rail_alias kwargs = self._kwargs.copy() for k, v in kwargs.items(): if not isinstance(v, str): kwargs[k] = str(v) params = " ".join(list(kwargs.values())) if with_keywords: params = " ".join([f"{k}={v}" for k, v in kwargs.items()]) return f"{self.rail_alias}: {params}" def to_xml_attrib(self): """Convert the validator to an XML attribute.""" if not len(self._kwargs): return self.rail_alias validator_args = [] init_args = inspect.getfullargspec(self.__init__) for arg in init_args.args[1:]: if arg not in ("on_fail", "args", "kwargs"): arg_value = self._kwargs.get(arg) str_arg = str(arg_value) if str_arg is not None: str_arg = "{" + str_arg + "}" if " " in str_arg else str_arg validator_args.append(str_arg) params = " ".join(validator_args) return f"{self.rail_alias}: {params}" def __call__(self, value): result = self.validate(value, {}) if isinstance(result, FailResult): from guardrails.validator_service import ValidatorServiceBase validator_service = ValidatorServiceBase() return validator_service.perform_correction( [result], value, self, self.on_fail_descriptor ) return value # @register_validator('required', 'all') # class Required(Validator): # """Validates that a value is not None.""" # def validate(self, key: str, value: Any, schema: Union[Dict, List]) -> bool: # """Validates that a value is not None.""" # return value is not None # @register_validator('description', 'all') # class Description(Validator): # """Validates that a value is not None.""" # def validate(self, key: str, value: Any, schema: Union[Dict, List]) -> bool: # """Validates that a value is not None.""" # return value is not None class PydanticReAsk(dict): pass @register_validator(name="pydantic_field_validator", data_type="all") class PydanticFieldValidator(Validator): """Validates a specific field in a Pydantic model with the specified validator method. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `pydantic_field_validator` | | Supported data types | `Any` | | Programmatic fix | Override with return value from `field_validator`. | Parameters: Arguments field_validator (Callable): A validator for a specific field in a Pydantic model. """ # noqa override_value_on_pass = True def __init__( self, field_validator: Callable, on_fail: Optional[Callable[..., Any]] = None, **kwargs, ): self.field_validator = field_validator super().__init__(on_fail, **kwargs) def validate(self, value: Any, metadata: Dict) -> ValidationResult: try: validated_field = self.field_validator(value) except Exception as e: return FailResult( error_message=str(e), fix_value=None, ) return PassResult( value_override=validated_field, ) def to_prompt(self, with_keywords: bool = True) -> str: return self.field_validator.__func__.__name__ @register_validator(name="valid-range", data_type=["integer", "float", "percentage"]) class ValidRange(Validator): """Validates that a value is within a range. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `valid-range` | | Supported data types | `integer`, `float`, `percentage` | | Programmatic fix | Closest value within the range. | Parameters: Arguments min: The inclusive minimum value of the range. max: The inclusive maximum value of the range. """ def __init__( self, min: int = None, max: int = None, on_fail: Optional[Callable] = None ): super().__init__(on_fail=on_fail, min=min, max=max) self._min = min self._max = max def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Validates that a value is within a range.""" logger.debug(f"Validating {value} is in range {self._min} - {self._max}...") val_type = type(value) if self._min is not None and value < val_type(self._min): return FailResult( error_message=f"Value {value} is less than {self._min}.", fix_value=self._min, ) if self._max is not None and value > val_type(self._max): return FailResult( error_message=f"Value {value} is greater than {self._max}.", fix_value=self._max, ) return PassResult() @register_validator(name="valid-choices", data_type="all") class ValidChoices(Validator): """Validates that a value is within the acceptable choices. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `valid-choices` | | Supported data types | `all` | | Programmatic fix | None | Parameters: Arguments choices: The list of valid choices. """ def __init__(self, choices: List[Any], on_fail: Optional[Callable] = None): super().__init__(on_fail=on_fail, choices=choices) self._choices = choices def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Validates that a value is within a range.""" logger.debug(f"Validating {value} is in choices {self._choices}...") if value not in self._choices: return FailResult( error_message=f"Value {value} is not in choices {self._choices}.", ) return PassResult() @register_validator(name="lower-case", data_type="string") class LowerCase(Validator): """Validates that a value is lower case. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `lower-case` | | Supported data types | `string` | | Programmatic fix | Convert to lower case. | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is lower case...") if value.lower() != value: return FailResult( error_message=f"Value {value} is not lower case.", fix_value=value.lower(), ) return PassResult() @register_validator(name="upper-case", data_type="string") class UpperCase(Validator): """Validates that a value is upper case. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `upper-case` | | Supported data types | `string` | | Programmatic fix | Convert to upper case. | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is upper case...") if value.upper() != value: return FailResult( error_message=f"Value {value} is not upper case.", fix_value=value.upper(), ) return PassResult() @register_validator(name="length", data_type=["string", "list"]) class ValidLength(Validator): """Validates that the length of value is within the expected range. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `length` | | Supported data types | `string`, `list`, `object` | | Programmatic fix | If shorter than the minimum, pad with empty last elements. If longer than the maximum, truncate. | Parameters: Arguments min: The inclusive minimum length. max: The inclusive maximum length. """ # noqa def __init__( self, min: int = None, max: int = None, on_fail: Optional[Callable] = None ): super().__init__(on_fail=on_fail, min=min, max=max) self._min = to_int(min) self._max = to_int(max) def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Validates that the length of value is within the expected range.""" logger.debug( f"Validating {value} is in length range {self._min} - {self._max}..." ) if self._min is not None and len(value) < self._min: logger.debug(f"Value {value} is less than {self._min}.") # Repeat the last character to make the value the correct length. if isinstance(value, str): if not value: last_val = rstr.rstr(string.ascii_lowercase, 1) else: last_val = value[-1] else: if not value: last_val = [rstr.rstr(string.ascii_lowercase, 1)] else: last_val = [value[-1]] corrected_value = value + last_val * (self._min - len(value)) return FailResult( error_message=f"Value has length less than {self._min}. " f"Please return a longer output, " f"that is shorter than {self._max} characters.", fix_value=corrected_value, ) if self._max is not None and len(value) > self._max: logger.debug(f"Value {value} is greater than {self._max}.") return FailResult( error_message=f"Value has length greater than {self._max}. " f"Please return a shorter output, " f"that is shorter than {self._max} characters.", fix_value=value[: self._max], ) return PassResult() @register_validator(name="regex_match", data_type="string") class RegexMatch(Validator): """Validates that a value matches a regular expression. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `regex_match` | | Supported data types | `string` | | Programmatic fix | Generate a string that matches the regular expression | Parameters: Arguments regex: Str regex pattern match_type: Str in {"search", "fullmatch"} for a regex search or full-match option """ # noqa def __init__( self, regex: str, match_type: Optional[str] = None, on_fail: Optional[Callable] = None, ): match_types = ["fullmatch", "search"] if match_type is None: match_type = "fullmatch" assert match_type in match_types, f"match_type must be in {match_types}" super().__init__(on_fail=on_fail, match_type=match_type, regex=regex) self._regex = regex self._p = re.compile(regex) self._match_f = getattr(self._p, match_type) # Pad matching string on either side for fix # example if we are performing a regex search str_padding = ( "" if match_type == "fullmatch" else rstr.rstr(string.ascii_lowercase) ) self._fix_str = str_padding + rstr.xeger(regex) + str_padding def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Validates that value matches the provided regular expression.""" if not self._match_f(value): return FailResult( error_message=f"Result must match {self._regex}", fix_value=self._fix_str, ) return PassResult() def to_prompt(self, with_keywords: bool = True) -> str: return "results should match " + self._regex @register_validator(name="two-words", data_type="string") class TwoWords(Validator): """Validates that a value is two words. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `two-words` | | Supported data types | `string` | | Programmatic fix | Pick the first two words. | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is two words...") if len(value.split()) != 2: return FailResult( error_message="must be exactly two words", fix_value=" ".join(value.split()[:2]), ) return PassResult() @register_validator(name="one-line", data_type="string") class OneLine(Validator): """Validates that a value is a single line or sentence. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `one-line` | | Supported data types | `string` | | Programmatic fix | Pick the first line. | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is a single line...") if len(value.splitlines()) > 1: return FailResult( error_message=f"Value {value} is not a single line.", fix_value=value.splitlines()[0], ) return PassResult() @register_validator(name="valid-url", data_type=["string"]) class ValidURL(Validator): """Validates that a value is a valid URL. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `valid-url` | | Supported data types | `string` | | Programmatic fix | None | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is a valid URL...") from urllib.parse import urlparse # Check that the URL is valid try: result = urlparse(value) # Check that the URL has a scheme and network location if not result.scheme or not result.netloc: return FailResult( error_message=f"URL {value} is not valid.", ) except ValueError: return FailResult( error_message=f"URL {value} is not valid.", ) return PassResult() @register_validator(name="is-reachable", data_type=["string"]) class EndpointIsReachable(Validator): """Validates that a value is a reachable URL. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `is-reachable` | | Supported data types | `string`, | | Programmatic fix | None | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is a valid URL...") import requests # Check that the URL exists and can be reached try: response = requests.get(value) if response.status_code != 200: return FailResult( error_message=f"URL {value} returned " f"status code {response.status_code}", ) except requests.exceptions.ConnectionError: return FailResult( error_message=f"URL {value} could not be reached", ) except requests.exceptions.InvalidSchema: return FailResult( error_message=f"URL {value} does not specify " f"a valid connection adapter", ) except requests.exceptions.MissingSchema: return FailResult( error_message=f"URL {value} does not contain " f"a http schema", ) return PassResult() @register_validator(name="bug-free-python", data_type="string") class BugFreePython(Validator): """Validates that there are no Python syntactic bugs in the generated code. This validator checks for syntax errors by running `ast.parse(code)`, and will raise an exception if there are any. Only the packages in the `python` environment are available to the code snippet. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `bug-free-python` | | Supported data types | `string` | | Programmatic fix | None | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is not a bug...") # The value is a Python code snippet. We need to check for syntax errors. try: ast.parse(value) except SyntaxError as e: return FailResult( error_message=f"Syntax error: {e.msg}", ) return PassResult() @register_validator(name="bug-free-sql", data_type=["string"]) class BugFreeSQL(Validator): """Validates that there are no SQL syntactic bugs in the generated code. This is a very minimal implementation that uses the Pypi `sqlvalidator` package to check if the SQL query is valid. You can implement a custom SQL validator that uses a database connection to check if the query is valid. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `bug-free-sql` | | Supported data types | `string` | | Programmatic fix | None | """ def __init__( self, conn: Optional[str] = None, schema_file: Optional[str] = None, on_fail: Optional[Callable] = None, ): super().__init__(on_fail=on_fail) self._driver: SQLDriver = create_sql_driver(schema_file=schema_file, conn=conn) def validate(self, value: Any, metadata: Dict) -> ValidationResult: errors = self._driver.validate_sql(value) if len(errors) > 0: return FailResult( error_message=". ".join(errors), ) return PassResult() @register_validator(name="sql-column-presence", data_type="string") class SqlColumnPresence(Validator): """Validates that all columns in the SQL query are present in the schema. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `sql-column-presence` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments cols: The list of valid columns. """ def __init__(self, cols: List[str], on_fail: Optional[Callable] = None): super().__init__(on_fail=on_fail, cols=cols) self._cols = set(cols) def validate(self, value: Any, metadata: Dict) -> ValidationResult: from sqlglot import exp, parse expressions = parse(value) cols = set() for expression in expressions: for col in expression.find_all(exp.Column): cols.add(col.alias_or_name) diff = cols.difference(self._cols) if len(diff) > 0: return FailResult( error_message=f"Columns [{', '.join(diff)}] " f"not in [{', '.join(self._cols)}]", ) return PassResult() @register_validator(name="exclude-sql-predicates", data_type="string") class ExcludeSqlPredicates(Validator): """Validates that the SQL query does not contain certain predicates. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `exclude-sql-predicates` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments predicates: The list of predicates to avoid. """ def __init__(self, predicates: List[str], on_fail: Optional[Callable] = None): super().__init__(on_fail=on_fail, predicates=predicates) self._predicates = set(predicates) def validate(self, value: Any, metadata: Dict) -> ValidationResult: from sqlglot import exp, parse expressions = parse(value) for expression in expressions: if expression is None: continue for pred in self._predicates: try: getattr(exp, pred) except AttributeError: raise ValueError(f"Predicate {pred} does not exist") if len(list(expression.find_all(getattr(exp, pred)))): return FailResult( error_message=f"SQL query contains predicate {pred}", fix_value="", ) return PassResult() @register_validator(name="similar-to-document", data_type="string") class SimilarToDocument(Validator): """Validates that a value is similar to the document. This validator checks if the value is similar to the document by checking the cosine similarity between the value and the document, using an embedding. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `similar-to-document` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments document: The document to use for the similarity check. threshold: The minimum cosine similarity to be considered similar. Defaults to 0.7. model: The embedding model to use. Defaults to text-embedding-ada-002. """ # noqa def __init__( self, document: str, threshold: float = 0.7, model: str = "text-embedding-ada-002", on_fail: Optional[Callable] = None, ): super().__init__( on_fail=on_fail, document=document, threshold=threshold, model=model ) if not _HAS_NUMPY: raise ImportError( f"The {self.__class__.__name__} validator requires the numpy package.\n" "`pip install numpy` to install it." ) self._document = document embedding = openai.Embedding.create(input=[document], model=model)["data"][0][ "embedding" ] self._document_embedding = np.array(embedding) self._model = model self._threshold = float(threshold) @staticmethod def cosine_similarity(a: "np.ndarray", b: "np.ndarray") -> float: """Calculate the cosine similarity between two vectors. Args: a: The first vector. b: The second vector. Returns: float: The cosine similarity between the two vectors. """ return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} is similar to document...") value_embedding = np.array( openai.Embedding.create(input=[value], model=self._model)["data"][0][ "embedding" ] ) similarity = SimilarToDocument.cosine_similarity( self._document_embedding, value_embedding, ) if similarity < self._threshold: return FailResult( error_message=f"Value {value} is not similar enough " f"to document {self._document}.", ) return PassResult() def to_prompt(self, with_keywords: bool = True) -> str: return "" @register_validator(name="is-profanity-free", data_type="string") class IsProfanityFree(Validator): """Validates that a translated text does not contain profanity language. This validator uses the `alt-profanity-check` package to check if a string contains profanity language. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `is-profanity-free` | | Supported data types | `string` | | Programmatic fix | None | """ def validate(self, value: Any, metadata: Dict) -> ValidationResult: try: from profanity_check import predict except ImportError: raise ImportError( "`is-profanity-free` validator requires the `alt-profanity-check`" "package. Please install it with `pip install profanity-check`." ) prediction = predict([value]) if prediction[0] == 1: return FailResult( error_message=f"{value} contains profanity. " f"Please return a profanity-free output.", fix_value="", ) return PassResult() @register_validator(name="is-high-quality-translation", data_type="string") class IsHighQualityTranslation(Validator): """Using inpiredco.critique to check if a translation is high quality. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `is-high-quality-translation` | | Supported data types | `string` | | Programmatic fix | None | Other parameters: Metadata translation_source (str): The source of the translation. """ required_metadata_keys = ["translation_source"] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) try: from inspiredco.critique import Critique self._critique = Critique(api_key=os.environ["INSPIREDCO_API_KEY"]) except ImportError: raise ImportError( "`is-high-quality-translation` validator requires the `inspiredco`" "package. Please install it with `pip install inspiredco`." ) def validate(self, value: Any, metadata: Dict) -> ValidationResult: if "translation_source" not in metadata: raise RuntimeError( "is-high-quality-translation validator expects " "`translation_source` key in metadata" ) src = metadata["translation_source"] prediction = self._critique.evaluate( metric="comet", config={"model": "unbabel_comet/wmt21-comet-qe-da"}, dataset=[{"source": src, "target": value}], ) quality = prediction["examples"][0]["value"] if quality < -0.1: return FailResult( error_message=f"{value} is a low quality translation." "Please return a higher quality output.", fix_value="", ) return PassResult() @register_validator(name="ends-with", data_type="list") class EndsWith(Validator): """Validates that a list ends with a given value. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `ends-with` | | Supported data types | `list` | | Programmatic fix | Append the given value to the list. | Parameters: Arguments end: The required last element. """ def __init__(self, end: str, on_fail: str = "fix"): super().__init__(on_fail=on_fail, end=end) self._end = end def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug(f"Validating {value} ends with {self._end}...") if not value[-1] == self._end: return FailResult( error_message=f"{value} must end with {self._end}", fix_value=value + [self._end], ) return PassResult() @register_validator(name="extracted-summary-sentences-match", data_type="string") class ExtractedSummarySentencesMatch(Validator): """Validates that the extracted summary sentences match the original text by performing a cosine similarity in the embedding space. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `extracted-summary-sentences-match` | | Supported data types | `string` | | Programmatic fix | Remove any sentences that can not be verified. | Parameters: Arguments threshold: The minimum cosine similarity to be considered similar. Default to 0.7. Other parameters: Metadata filepaths (List[str]): A list of strings that specifies the filepaths for any documents that should be used for asserting the summary's similarity. document_store (DocumentStoreBase, optional): The document store to use during validation. Defaults to EphemeralDocumentStore. vector_db (VectorDBBase, optional): A vector database to use for embeddings. Defaults to Faiss. embedding_model (EmbeddingBase, optional): The embeddig model to use. Defaults to OpenAIEmbedding. """ # noqa required_metadata_keys = ["filepaths"] def __init__( self, threshold: float = 0.7, on_fail: Optional[Callable] = None, **kwargs: Optional[Dict[str, Any]], ): super().__init__(on_fail, **kwargs) # TODO(shreya): Pass embedding_model, vector_db, document_store from spec self._threshold = float(threshold) @staticmethod def _instantiate_store( metadata, api_key: Optional[str] = None, api_base: Optional[str] = None ): if "document_store" in metadata: return metadata["document_store"] from guardrails.document_store import EphemeralDocumentStore if "vector_db" in metadata: vector_db = metadata["vector_db"] else: from guardrails.vectordb import Faiss if "embedding_model" in metadata: embedding_model = metadata["embedding_model"] else: from guardrails.embedding import OpenAIEmbedding embedding_model = OpenAIEmbedding(api_key=api_key, api_base=api_base) vector_db = Faiss.new_flat_ip_index( embedding_model.output_dim, embedder=embedding_model ) return EphemeralDocumentStore(vector_db) def validate(self, value: Any, metadata: Dict) -> ValidationResult: if "filepaths" not in metadata: raise RuntimeError( "extracted-sentences-summary-match validator expects " "`filepaths` key in metadata" ) filepaths = metadata["filepaths"] kwargs = {} context_copy = contextvars.copy_context() for key, context_var in context_copy.items(): if key.name == "kwargs" and isinstance(kwargs, dict): kwargs = context_var break api_key = kwargs.get("api_key") api_base = kwargs.get("api_base") store = self._instantiate_store(metadata, api_key, api_base) sources = [] for filepath in filepaths: with open(filepath) as f: doc = f.read() store.add_text(doc, {"path": filepath}) sources.append(filepath) # Split the value into sentences. sentences = re.split(r"(?<=[.!?]) +", value) # Check if any of the sentences in the value match any of the sentences # in the documents. unverified = [] verified = [] citations = {} for id_, sentence in enumerate(sentences): page = store.search_with_threshold(sentence, self._threshold) if not page or page[0].metadata["path"] not in sources: unverified.append(sentence) else: sentence_id = id_ + 1 citation_path = page[0].metadata["path"] citation_id = sources.index(citation_path) + 1 citations[sentence_id] = citation_id verified.append(sentence + f" [{citation_id}]") fixed_summary = ( " ".join(verified) + "\n\n" + "\n".join(f"[{i + 1}] {s}" for i, s in enumerate(sources)) ) metadata["summary_with_citations"] = fixed_summary metadata["citations"] = citations if unverified: unverified_sentences = "\n".join(unverified) return FailResult( metadata=metadata, error_message=( f"The summary \nSummary: {value}\n has sentences\n" f"{unverified_sentences}\n that are not similar to any document." ), fix_value=fixed_summary, ) return PassResult(metadata=metadata) def to_prompt(self, with_keywords: bool = True) -> str: return "" @register_validator(name="reading-time", data_type="string") class ReadingTime(Validator): """Validates that the a string can be read in less than a certain amount of time. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `reading-time` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments reading_time: The maximum reading time. """ def __init__(self, reading_time: int, on_fail: str = "fix"): super().__init__(on_fail=on_fail, reading_time=reading_time) self._max_time = reading_time def validate(self, value: Any, metadata: Dict) -> ValidationResult: logger.debug( f"Validating {value} can be read in less than {self._max_time} seconds..." ) # Estimate the reading time of the string reading_time = len(value.split()) / 200 * 60 logger.debug(f"Estimated reading time {reading_time} seconds...") if abs(reading_time - self._max_time) > 1: logger.error(f"{value} took {reading_time} to read") return FailResult( error_message=f"String should be readable " f"within {self._max_time} minutes.", fix_value=value, ) return PassResult() @register_validator(name="extractive-summary", data_type="string") class ExtractiveSummary(Validator): """Validates that a string is a valid extractive summary of a given document. This validator does a fuzzy match between the sentences in the summary and the sentences in the document. Each sentence in the summary must be similar to at least one sentence in the document. After the validation, the summary is updated to include the sentences from the document that were matched, and the citations for those sentences are added to the end of the summary. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `extractive-summary` | | Supported data types | `string` | | Programmatic fix | Remove any sentences that can not be verified. | Parameters: Arguments threshold: The minimum fuzz ratio to be considered summarized. Defaults to 85. Other parameters: Metadata filepaths (List[str]): A list of strings that specifies the filepaths for any documents that should be used for asserting the summary's similarity. """ # noqa required_metadata_keys = ["filepaths"] def __init__( self, threshold: int = 85, on_fail: Optional[Callable] = None, **kwargs, ): super().__init__(on_fail, **kwargs) self._threshold = threshold def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Make sure each sentence was precisely copied from the document.""" if "filepaths" not in metadata: raise RuntimeError( "extractive-summary validator expects " "`filepaths` key in metadata" ) filepaths = metadata["filepaths"] # Load documents store = {} for filepath in filepaths: with open(filepath) as f: doc = f.read() store[filepath] = sentence_split(doc) try: from thefuzz import fuzz except ImportError: raise ImportError( "`thefuzz` library is required for `extractive-summary` validator. " "Please install it with `pip install thefuzz`." ) # Split the value into sentences. sentences = sentence_split(value) # Check if any of the sentences in the value match any of the sentences # # in the documents. unverified = [] verified = [] citations = {} for id_, sentence in enumerate(sentences): highest_ratio = 0 highest_ratio_doc = None # Check fuzzy match against all sentences in all documents for doc_path, doc_sentences in store.items(): for doc_sentence in doc_sentences: ratio = fuzz.ratio(sentence, doc_sentence) if ratio > highest_ratio: highest_ratio = ratio highest_ratio_doc = doc_path if highest_ratio < self._threshold: unverified.append(sentence) else: sentence_id = id_ + 1 citation_id = list(store).index(highest_ratio_doc) + 1 citations[sentence_id] = citation_id verified.append(sentence + f" [{citation_id}]") verified_sentences = ( " ".join(verified) + "\n\n" + "\n".join(f"[{i + 1}] {s}" for i, s in enumerate(store)) ) metadata["summary_with_citations"] = verified_sentences metadata["citations"] = citations if len(unverified): unverified_sentences = "\n".join( "- " + s for i, s in enumerate(sentences) if i in unverified ) return FailResult( metadata=metadata, error_message=( f"The summary \nSummary: {value}\n has sentences\n" f"{unverified_sentences}\n that are not similar to any document." ), fix_value="\n".join(verified_sentences), ) return PassResult( metadata=metadata, ) @register_validator(name="remove-redundant-sentences", data_type="string") class RemoveRedundantSentences(Validator): """Removes redundant sentences from a string. This validator removes sentences from a string that are similar to other sentences in the string. This is useful for removing repetitive sentences from a string. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `remove-redundant-sentences` | | Supported data types | `string` | | Programmatic fix | Remove any redundant sentences. | Parameters: Arguments threshold: The minimum fuzz ratio to be considered redundant. Defaults to 70. """ def __init__( self, threshold: int = 70, on_fail: Optional[Callable] = None, **kwargs ): super().__init__(on_fail, **kwargs) self._threshold = threshold def validate(self, value: Any, metadata: Dict) -> ValidationResult: """Remove redundant sentences from a string.""" try: from thefuzz import fuzz except ImportError: raise ImportError( "`thefuzz` library is required for `remove-redundant-sentences` " "validator. Please install it with `pip install thefuzz`." ) # Split the value into sentences. sentences = sentence_split(value) filtered_sentences = [] redundant_sentences = [] sentence = sentences[0] other_sentences = sentences[1:] while len(other_sentences): # Check fuzzy match against all other sentences filtered_sentences.append(sentence) unique_sentences = [] for other_sentence in other_sentences: ratio = fuzz.ratio(sentence, other_sentence) if ratio > self._threshold: redundant_sentences.append(other_sentence) else: unique_sentences.append(other_sentence) if len(unique_sentences) == 0: break sentence = unique_sentences[0] other_sentences = unique_sentences[1:] filtered_summary = " ".join(filtered_sentences) if len(redundant_sentences): redundant_sentences = "\n".join(redundant_sentences) return FailResult( error_message=( f"The summary \nSummary: {value}\n has sentences\n" f"{redundant_sentences}\n that are similar to other sentences." ), fix_value=filtered_summary, ) return PassResult() @register_validator(name="saliency-check", data_type="string") class SaliencyCheck(Validator): """Checks that the summary covers the list of topics present in the document. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `saliency-check` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments docs_dir: Path to the directory containing the documents. threshold: Threshold for overlap between topics in document and summary. Defaults to 0.25 """ # noqa def __init__( self, docs_dir: str, llm_callable: Callable = None, on_fail: Optional[Callable] = None, threshold: float = 0.25, **kwargs, ): """Initialize the SalienceCheck validator. Args: docs_dir: Path to the directory containing the documents. on_fail: Function to call when validation fails. threshold: Threshold for overlap between topics in document and summary. """ super().__init__(on_fail, **kwargs) self.llm_callable = ( llm_callable if llm_callable else openai.ChatCompletion.create ) self._threshold = threshold # Load documents self._document_store = {} for doc_path in os.listdir(docs_dir): with open(os.path.join(docs_dir, doc_path)) as f: text = f.read() # Precompute topics for each document self._document_store[doc_path] = self._get_topics(text) @property def _topics(self) -> List[str]: """Return a list of topics that can be used in the validator.""" # Merge topics from all documents topics = set() for doc_topics in self._document_store.values(): topics.update(doc_topics) return list(topics) def _get_topics(self, text: str, topics: Optional[List[str]] = None) -> List[str]: """Extract topics from a string.""" from guardrails import Guard topics_seed = "" if topics is not None: topics_seed = ( "Here's a seed list of topics, select topics from this list" " if they are covered in the doc:\n\n" + ", ".join(topics) ) spec = f""" <rail version="0.1"> <output> <list name="topics"> <string name="topic" description="few words describing the topic in text"/> </list> </output> <prompt> Extract a list of topics from the following text: {text} {topics_seed} Return the output as a JSON with a single key "topics" containing a list of topics. Make sure that topics are relevant to text, and topics are not too specific or general. </prompt> </rail> """ guard = Guard.from_rail_string(spec) _, validated_output = guard(llm_api=self.llm_callable) return validated_output["topics"] def validate(self, value: Any, metadata: Dict) -> ValidationResult: topics_in_summary = self._get_topics(value, topics=self._topics) # Compute overlap between topics in document and summary intersection = set(topics_in_summary).intersection(set(self._topics)) overlap = len(intersection) / len(self._topics) if overlap < self._threshold: return FailResult( error_message=( f"The summary \nSummary: {value}\n does not cover these topics:\n" f"{set(self._topics).difference(intersection)}" ), fix_value="", ) return PassResult() @register_validator(name="qa-relevance-llm-eval", data_type="string") class QARelevanceLLMEval(Validator): """Validates that an answer is relevant to the question asked by asking the LLM to self evaluate. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `qa-relevance-llm-eval` | | Supported data types | `string` | | Programmatic fix | None | Other parameters: Metadata question (str): The original question the llm was given to answer. """ required_metadata_keys = ["question"] def __init__( self, llm_callable: Callable = None, on_fail: Optional[Callable] = None, **kwargs, ): super().__init__(on_fail, **kwargs) self.llm_callable = ( llm_callable if llm_callable else openai.ChatCompletion.create ) def _selfeval(self, question: str, answer: str): from guardrails import Guard spec = """ <rail version="0.1"> <output> <bool name="relevant" /> </output> <prompt> Is the answer below relevant to the question asked? Question: {question} Answer: {answer} Relevant (as a JSON with a single boolean key, "relevant"):\ </prompt> </rail> """.format( question=question, answer=answer, ) guard = Guard.from_rail_string(spec) return guard( self.llm_callable, max_tokens=10, temperature=0.1, )[1] def validate(self, value: Any, metadata: Dict) -> ValidationResult: if "question" not in metadata: raise RuntimeError( "qa-relevance-llm-eval validator expects " "`question` key in metadata" ) question = metadata["question"] relevant = self._selfeval(question, value)["relevant"] if relevant: return PassResult() fixed_answer = "No relevant answer found." return FailResult( error_message=f"The answer {value} is not relevant " f"to the question {question}.", fix_value=fixed_answer, ) def to_prompt(self, with_keywords: bool = True) -> str: return "" @register_validator(name="provenance-v0", data_type="string") class ProvenanceV0(Validator): """Validates that LLM-generated text matches some source text based on distance in embedding space. **Key Properties** | Property | Description | | ----------------------------- | ----------------------------------- | | Name for `format` attribute | `provenance-v0` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments threshold: The minimum cosine similarity between the generated text and the source text. Defaults to 0.8. validation_method: Whether to validate at the sentence level or over the full text. Must be one of `sentence` or `full`. Defaults to `sentence` Other parameters: Metadata query_function (Callable, optional): A callable that takes a string and returns a list of (chunk, score) tuples. sources (List[str], optional): The source text. embed_function (Callable, optional): A callable that creates embeddings for the sources. Must accept a list of strings and return an np.array of floats. In order to use this validator, you must provide either a `query_function` or `sources` with an `embed_function` in the metadata. If providing query_function, it should take a string as input and return a list of (chunk, score) tuples. The chunk is a string and the score is a float representing the cosine distance between the chunk and the input string. The list should be sorted in ascending order by score. Note: The score should represent distance in embedding space, not similarity. I.e., lower is better and the score should be 0 if the chunk is identical to the input string. Example: ```py def query_function(text: str, k: int) -> List[Tuple[str, float]]: return [("This is a chunk", 0.9), ("This is another chunk", 0.8)] guard = Guard.from_rail(...) guard( openai.ChatCompletion.create(...), prompt_params={...}, temperature=0.0, metadata={"query_function": query_function}, ) ``` If providing sources, it should be a list of strings. The embed_function should take a string or a list of strings as input and return a np array of floats. The vector should be normalized to unit length. Example: ```py def embed_function(text: Union[str, List[str]]) -> np.ndarray: return np.array([[0.1, 0.2, 0.3]]) guard = Guard.from_rail(...) guard( openai.ChatCompletion.create(...), prompt_params={...}, temperature=0.0, metadata={ "sources": ["This is a source text"], "embed_function": embed_function }, ) ``` """ # noqa def __init__( self, threshold: float = 0.8, validation_method: str = "sentence", on_fail: Optional[Callable] = None, **kwargs, ): super().__init__( on_fail, threshold=threshold, validation_method=validation_method, **kwargs ) self._threshold = float(threshold) if validation_method not in ["sentence", "full"]: raise ValueError("validation_method must be 'sentence' or 'full'.") self._validation_method = validation_method def get_query_function(self, metadata: Dict[str, Any]) -> None: query_fn = metadata.get("query_function", None) sources = metadata.get("sources", None) # Check that query_fn or sources are provided if query_fn is not None and sources is not None: warnings.warn( "Both `query_function` and `sources` are provided in metadata. " "`query_function` will be used." ) elif query_fn is None and sources is None: raise ValueError( "You must provide either `query_function` or `sources` in metadata." ) elif query_fn is None and sources is not None: # Check chunking strategy chunk_strategy = metadata.get("chunk_strategy", "sentence") if chunk_strategy not in ["sentence", "word", "char", "token"]: raise ValueError( "`chunk_strategy` must be one of 'sentence', 'word', 'char', " "or 'token'." ) chunk_size = metadata.get("chunk_size", 5) chunk_overlap = metadata.get("chunk_overlap", 2) # Check distance metric distance_metric = metadata.get("distance_metric", "cosine") if distance_metric not in ["cosine", "euclidean"]: raise ValueError( "`distance_metric` must be one of 'cosine' or 'euclidean'." ) # Check embed model embed_function = metadata.get("embed_function", None) if embed_function is None: raise ValueError( "You must provide `embed_function` in metadata in order to " "use the default query function." ) query_fn = partial( ProvenanceV0.query_vector_collection, sources=metadata["sources"], chunk_strategy=chunk_strategy, chunk_size=chunk_size, chunk_overlap=chunk_overlap, distance_metric=distance_metric, embed_function=embed_function, ) return query_fn def validate_each_sentence( self, value: Any, query_function: Callable, metadata: Dict[str, Any] ) -> ValidationResult: # Split the value into sentences using nltk sentence tokenizer. sentences = nltk.sent_tokenize(value) unsupported_sentences = [] supported_sentences = [] for sentence in sentences: most_similar_chunks = query_function(text=sentence, k=1) if most_similar_chunks is None: unsupported_sentences.append(sentence) continue most_similar_chunk = most_similar_chunks[0] if most_similar_chunk[1] < self._threshold: supported_sentences.append((sentence, most_similar_chunk[0])) else: unsupported_sentences.append(sentence) metadata["unsupported_sentences"] = "- " + "\n- ".join(unsupported_sentences) metadata["supported_sentences"] = supported_sentences if unsupported_sentences: unsupported_sentences = "- " + "\n- ".join(unsupported_sentences) return FailResult( metadata=metadata, error_message=( f"None of the following sentences in your response are supported " "by provided context:" f"\n{metadata['unsupported_sentences']}" ), fix_value="\n".join(s[0] for s in supported_sentences), ) return PassResult(metadata=metadata) def validate_full_text( self, value: Any, query_function: Callable, metadata: Dict[str, Any] ) -> ValidationResult: most_similar_chunks = query_function(text=value, k=1) if most_similar_chunks is None: metadata["unsupported_text"] = value metadata["supported_text_citations"] = {} return FailResult( metadata=metadata, error_message=( "The following text in your response is not supported by the " "supported by the provided context:\n" + value ), ) most_similar_chunk = most_similar_chunks[0] if most_similar_chunk[1] > self._threshold: metadata["unsupported_text"] = value metadata["supported_text_citations"] = {} return FailResult( metadata=metadata, error_message=( "The following text in your response is not supported by the " "supported by the provided context:\n" + value ), ) metadata["unsupported_text"] = "" metadata["supported_text_citations"] = { value: most_similar_chunk[0], } return PassResult(metadata=metadata) def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult: query_function = self.get_query_function(metadata) if self._validation_method == "sentence": return self.validate_each_sentence(value, query_function, metadata) elif self._validation_method == "full": return self.validate_full_text(value, query_function, metadata) else: raise ValueError("validation_method must be 'sentence' or 'full'.") @staticmethod def query_vector_collection( text: str, k: int, sources: List[str], chunk_strategy: str = "sentence", chunk_size: int = 5, chunk_overlap: int = 2, distance_metric: str = "cosine", embed_function: Optional[Callable] = None, ) -> List[Tuple[str, float]]: chunks = [ get_chunks_from_text(source, chunk_strategy, chunk_size, chunk_overlap) for source in sources ] chunks = list(itertools.chain.from_iterable(chunks)) # Create embeddings source_embeddings = np.array(embed_function(chunks)).squeeze() query_embedding = embed_function(text).squeeze() # Compute distances if distance_metric == "cosine": if not _HAS_NUMPY: raise ValueError( "You must install numpy in order to use the cosine distance " "metric." ) cos_sim = 1 - ( np.dot(source_embeddings, query_embedding) / ( np.linalg.norm(source_embeddings, axis=1) * np.linalg.norm(query_embedding) ) ) top_indices = np.argsort(cos_sim)[:k] top_similarities = [cos_sim[j] for j in top_indices] top_chunks = [chunks[j] for j in top_indices] else: raise ValueError("distance_metric must be 'cosine'.") return list(zip(top_chunks, top_similarities)) def to_prompt(self, with_keywords: bool = True) -> str: return "" @register_validator(name="provenance-v1", data_type="string") class ProvenanceV1(Validator): """Validates that the LLM-generated text is supported by the provided contexts. This validator uses an LLM callable to evaluate the generated text against the provided contexts (LLM-ception). In order to use this validator, you must provide either: 1. a 'query_function' in the metadata. That function should take a string as input (the LLM-generated text) and return a list of relevant chunks. The list should be sorted in ascending order by the distance between the chunk and the LLM-generated text. Example using str callable: >>> def query_function(text: str, k: int) -> List[str]: ... return ["This is a chunk", "This is another chunk"] >>> guard = Guard.from_string(validators=[ ProvenanceV1(llm_callable="gpt-3.5-turbo", ...) ] ) >>> guard.parse( ... llm_output=..., ... metadata={"query_function": query_function} ... ) Example using a custom llm callable: >>> def query_function(text: str, k: int) -> List[str]: ... return ["This is a chunk", "This is another chunk"] >>> guard = Guard.from_string(validators=[ ProvenanceV1(llm_callable=your_custom_callable, ...) ] ) >>> guard.parse( ... llm_output=..., ... metadata={"query_function": query_function} ... ) OR 2. `sources` with an `embed_function` in the metadata. The embed_function should take a string or a list of strings as input and return a np array of floats. The vector should be normalized to unit length. Example: ```py def embed_function(text: Union[str, List[str]]) -> np.ndarray: return np.array([[0.1, 0.2, 0.3]]) guard = Guard.from_rail(...) guard( openai.ChatCompletion.create(...), prompt_params={...}, temperature=0.0, metadata={ "sources": ["This is a source text"], "embed_function": embed_function }, ) """ def __init__( self, validation_method: str = "sentence", llm_callable: Union[str, Callable] = "gpt-3.5-turbo", top_k: int = 3, max_tokens: int = 2, on_fail: Optional[Callable] = None, **kwargs, ): """ args: validation_method (str): Whether to validate at the sentence level or over the full text. One of `sentence` or `full`. Defaults to `sentence` llm_callable (Union[str, Callable]): Either the name of the OpenAI model, or a callable that takes a prompt and returns a response. top_k (int): The number of chunks to return from the query function. Defaults to 3. max_tokens (int): The maximum number of tokens to send to the LLM. Defaults to 2. Other args: Metadata query_function (Callable): A callable that takes a string and returns a list of chunks. sources (List[str], optional): The source text. embed_function (Callable, optional): A callable that creates embeddings for the sources. Must accept a list of strings and returns float np.array. """ super().__init__( on_fail, validation_method=validation_method, llm_callable=llm_callable, top_k=top_k, max_tokens=max_tokens, **kwargs, ) if validation_method not in ["sentence", "full"]: raise ValueError("validation_method must be 'sentence' or 'full'.") self._validation_method = validation_method self.set_callable(llm_callable) self._top_k = int(top_k) self._max_tokens = int(max_tokens) def set_callable(self, llm_callable: Union[str, Callable]) -> None: """Set the LLM callable. Args: llm_callable: Either the name of the OpenAI model, or a callable that takes a prompt and returns a response. """ if isinstance(llm_callable, str): if llm_callable not in ["gpt-3.5-turbo", "gpt-4"]: raise ValueError( "llm_callable must be one of 'gpt-3.5-turbo' or 'gpt-4'." "If you want to use a custom LLM, please provide a callable." "Check out ProvenanceV1 documentation for an example." ) def openai_callable(prompt: str) -> str: response = openai.ChatCompletion.create( model=llm_callable, messages=[ {"role": "user", "content": prompt}, ], max_tokens=self._max_tokens, ) return response["choices"][0]["message"]["content"] self._llm_callable = openai_callable elif isinstance(llm_callable, Callable): self._llm_callable = llm_callable else: raise ValueError( "llm_callable must be either a string or a callable that takes a string" " and returns a string." ) def get_query_function(self, metadata: Dict[str, Any]) -> None: # Exact same as ProvenanceV0 query_fn = metadata.get("query_function", None) sources = metadata.get("sources", None) # Check that query_fn or sources are provided if query_fn is not None and sources is not None: warnings.warn( "Both `query_function` and `sources` are provided in metadata. " "`query_function` will be used." ) elif query_fn is None and sources is None: raise ValueError( "You must provide either `query_function` or `sources` in metadata." ) elif query_fn is None and sources is not None: # Check chunking strategy chunk_strategy = metadata.get("chunk_strategy", "sentence") if chunk_strategy not in ["sentence", "word", "char", "token"]: raise ValueError( "`chunk_strategy` must be one of 'sentence', 'word', 'char', " "or 'token'." ) chunk_size = metadata.get("chunk_size", 5) chunk_overlap = metadata.get("chunk_overlap", 2) # Check distance metric distance_metric = metadata.get("distance_metric", "cosine") if distance_metric not in ["cosine", "euclidean"]: raise ValueError( "`distance_metric` must be one of 'cosine' or 'euclidean'." ) # Check embed model embed_function = metadata.get("embed_function", None) if embed_function is None: raise ValueError( "You must provide `embed_function` in metadata in order to " "use the default query function." ) query_fn = partial( ProvenanceV1.query_vector_collection, sources=metadata["sources"], chunk_strategy=chunk_strategy, chunk_size=chunk_size, chunk_overlap=chunk_overlap, distance_metric=distance_metric, embed_function=embed_function, ) return query_fn @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) def call_llm(self, prompt: str) -> str: """Call the LLM with the given prompt. Expects a function that takes a string and returns a string. Args: prompt (str): The prompt to send to the LLM. Returns: response (str): String representing the LLM response. """ return self._llm_callable(prompt) def evaluate_with_llm(self, text: str, query_function: Callable) -> bool: """Validate that the LLM-generated text is supported by the provided contexts. Args: value (Any): The LLM-generated text. query_function (Callable): The query function. Returns: self_eval: The self-evaluation boolean """ # Get the relevant chunks using the query function relevant_chunks = query_function(text=text, k=self._top_k) # Create the prompt to ask the LLM prompt = PROVENANCE_V1_PROMPT.format(text, "\n".join(relevant_chunks)) # Get self-evaluation self_eval = self.call_llm(prompt) self_eval = True if self_eval == "Yes" else False return self_eval def validate_each_sentence( self, value: Any, query_function: Callable, metadata: Dict[str, Any] ) -> ValidationResult: # Split the value into sentences using nltk sentence tokenizer. sentences = nltk.sent_tokenize(value) unsupported_sentences = [] supported_sentences = [] for sentence in sentences: self_eval = self.evaluate_with_llm(sentence, query_function) if not self_eval: unsupported_sentences.append(sentence) else: supported_sentences.append(sentence) if unsupported_sentences: unsupported_sentences = "- " + "\n- ".join(unsupported_sentences) return FailResult( metadata=metadata, error_message=( f"None of the following sentences in your response are supported " "by provided context:" f"\n{unsupported_sentences}" ), fix_value="\n".join(supported_sentences), ) return PassResult(metadata=metadata) def validate_full_text( self, value: Any, query_function: Callable, metadata: Dict[str, Any] ) -> ValidationResult: # Self-evaluate LLM with entire text self_eval = self.evaluate_with_llm(value, query_function) if not self_eval: # if false return FailResult( metadata=metadata, error_message=( "The following text in your response is not supported by the " "supported by the provided context:\n" + value ), ) return PassResult(metadata=metadata) def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult: kwargs = {} context_copy = contextvars.copy_context() for key, context_var in context_copy.items(): if key.name == "kwargs" and isinstance(kwargs, dict): kwargs = context_var break api_key = kwargs.get("api_key") api_base = kwargs.get("api_base") # Set the OpenAI API key if os.getenv("OPENAI_API_KEY"): # Check if set in environment openai.api_key = os.getenv("OPENAI_API_KEY") elif api_key: # Check if set when calling guard() or parse() openai.api_key = api_key # Set the OpenAI API base if specified if api_base: openai.api_base = api_base query_function = self.get_query_function(metadata) if self._validation_method == "sentence": return self.validate_each_sentence(value, query_function, metadata) elif self._validation_method == "full": return self.validate_full_text(value, query_function, metadata) else: raise ValueError("validation_method must be 'sentence' or 'full'.") @staticmethod def query_vector_collection( text: str, k: int, sources: List[str], chunk_strategy: str = "sentence", chunk_size: int = 5, chunk_overlap: int = 2, distance_metric: str = "cosine", embed_function: Optional[Callable] = None, ) -> List[Tuple[str, float]]: chunks = [ get_chunks_from_text(source, chunk_strategy, chunk_size, chunk_overlap) for source in sources ] chunks = list(itertools.chain.from_iterable(chunks)) # Create embeddings source_embeddings = np.array(embed_function(chunks)).squeeze() query_embedding = embed_function(text).squeeze() # Compute distances if distance_metric == "cosine": if not _HAS_NUMPY: raise ValueError( "You must install numpy in order to use the cosine distance " "metric." ) cos_sim = 1 - ( np.dot(source_embeddings, query_embedding) / ( np.linalg.norm(source_embeddings, axis=1) * np.linalg.norm(query_embedding) ) ) top_indices = np.argsort(cos_sim)[:k] top_chunks = [chunks[j] for j in top_indices] else: raise ValueError("distance_metric must be 'cosine'.") return top_chunks @register_validator(name="similar-to-list", data_type="string") class SimilarToList(Validator): """Validates that a value is similar to a list of previously known values. **Key Properties** | Property | Description | | ----------------------------- | --------------------------------- | | Name for `format` attribute | `similar-to-list` | | Supported data types | `string` | | Programmatic fix | None | Parameters: Arguments standard_deviations (int): The number of standard deviations from the mean to check. threshold (float): The threshold for the average semantic similarity for strings. For integer values, this validator checks whether the value lies within 'k' standard deviations of the mean of the previous values. (Assumes that the previous values are normally distributed.) For string values, this validator checks whether the average semantic similarity between the generated value and the previous values is less than a threshold. """ # noqa def __init__( self, standard_deviations: int = 3, threshold: float = 0.1, on_fail: Optional[Callable] = None, **kwargs, ): super().__init__( on_fail, standard_deviations=standard_deviations, threshold=threshold, **kwargs, ) self._standard_deviations = int(standard_deviations) self._threshold = float(threshold) def get_semantic_similarity( self, text1: str, text2: str, embed_function: Callable ) -> float: """Get the semantic similarity between two strings. Args: text1 (str): The first string. text2 (str): The second string. embed_function (Callable): The embedding function. Returns: similarity (float): The semantic similarity between the two strings. """ text1_embedding = embed_function(text1) text2_embedding = embed_function(text2) similarity = 1 - ( np.dot(text1_embedding, text2_embedding) / (np.linalg.norm(text1_embedding) * np.linalg.norm(text2_embedding)) ) return similarity def validate(self, value: Any, metadata: Dict) -> ValidationResult: prev_values = metadata.get("prev_values", []) if not prev_values: raise ValueError("You must provide a list of previous values in metadata.") # Check if np is installed if not _HAS_NUMPY: raise ValueError( "You must install numpy in order to " "use the distribution check validator." ) try: value = int(value) is_int = True except ValueError: is_int = False if is_int: # Check whether prev_values are also all integers if not all(isinstance(prev_value, int) for prev_value in prev_values): raise ValueError( "Both given value and all the previous values must be " "integers in order to use the distribution check validator." ) # Check whether the value lies in a similar distribution as the prev_values # Get mean and std of prev_values prev_values = np.array(prev_values) prev_mean = np.mean(prev_values) prev_std = np.std(prev_values) # Check whether the value lies outside specified stds of the mean if value < prev_mean - ( self._standard_deviations * prev_std ) or value > prev_mean + (self._standard_deviations * prev_std): return FailResult( error_message=( f"The value {value} lies outside of the expected distribution " f"of {prev_mean} +/- {self._standard_deviations * prev_std}." ), ) return PassResult() else: # Check whether prev_values are also all strings if not all(isinstance(prev_value, str) for prev_value in prev_values): raise ValueError( "Both given value and all the previous values must be " "strings in order to use the distribution check validator." ) # Check embed model embed_function = metadata.get("embed_function", None) if embed_function is None: raise ValueError( "You must provide `embed_function` in metadata in order to " "check the semantic similarity of the generated string." ) # Check whether the value is semantically similar to the prev_values # Get average semantic similarity # Lesser the average semantic similarity, more similar the strings are avg_semantic_similarity = np.mean( [ self.get_semantic_similarity(value, prev_value, embed_function) for prev_value in prev_values ] ) # If average semantic similarity is above the threshold, # then the value is not semantically similar to the prev_values if avg_semantic_similarity > self._threshold: return FailResult( error_message=( f"The value {value} is not semantically similar to the " f"previous values. The average semantic similarity is " f"{avg_semantic_similarity} which is below the threshold of " f"{self._threshold}." ), ) return PassResult()
[ "\n" ]
2024-01-10
jonasferoz/guardrails
tests~integration_tests~test_guard.py
import json import os from typing import Optional, Union import openai import pytest from pydantic import BaseModel import guardrails as gd from guardrails.guard import Guard from guardrails.utils.reask_utils import FieldReAsk from guardrails.validators import FailResult, OneLine from .mock_llm_outputs import ( MockOpenAICallable, MockOpenAIChatCallable, entity_extraction, ) from .test_assets import pydantic, string @pytest.fixture(scope="module") def rail_spec(): return """ <rail version="0.1"> <output> <string name="dummy_string" description="Any dummy string" /> <integer name="dummy_integer" description="Any dummy integer" /> <float name="dummy_float" description="Any dummy float" /> <bool name="dummy_boolean" description="Any dummy boolean" /> <email name="dummy_email" description="Any dummy email" /> <url name="dummy_url" description="Any dummy url" /> <date name="dummy_date" description="Any dummy date" /> <time name="dummy_time" description="Any dummy time" /> <list name="dummy_list" description="Any dummy list" /> <object name="dummy_object" description="Any dummy object" /> </output> <prompt> Generate a JSON of dummy data, where the data types are specified by the user. ${gr.complete_json_suffix} </prompt> </rail> """ @pytest.fixture(scope="module") def llm_output(): return """ { "dummy_string": "Some string", "dummy_integer": 42, "dummy_float": 3.14, "dummy_boolean": true, "dummy_email": "[email protected]", "dummy_url": "https://www.example.com", "dummy_date": "2020-01-01", "dummy_time": "12:00:00", "dummy_list": ["item1", "item2", "item3"], "dummy_object": { "key1": "value1", "key2": "value2" } } """ @pytest.fixture(scope="module") def validated_output(): return { "dummy_string": "Some string", "dummy_integer": 42, "dummy_float": 3.14, "dummy_boolean": True, "dummy_email": "[email protected]", "dummy_url": "https://www.example.com", "dummy_date": "2020-01-01", "dummy_time": "12:00:00", "dummy_list": ["item1", "item2", "item3"], "dummy_object": {"key1": "value1", "key2": "value2"}, } def guard_initializer( rail: Union[str, BaseModel], prompt: str, instructions: Optional[str] = None ) -> Guard: """Helper function to initialize a Guard object using the correct method.""" if isinstance(rail, str): return Guard.from_rail_string(rail) else: return Guard.from_pydantic(rail, prompt=prompt, instructions=instructions) '''def test_rail_spec_output_parse(rail_spec, llm_output, validated_output): """Test that the rail_spec fixture is working.""" guard = gd.Guard.from_rail_string(rail_spec) assert guard.parse(llm_output) == validated_output''' @pytest.mark.parametrize( "rail,prompt,test_full_schema_reask", [ (entity_extraction.RAIL_SPEC_WITH_REASK, None, False), (entity_extraction.RAIL_SPEC_WITH_REASK, None, True), ( entity_extraction.PYDANTIC_RAIL_WITH_REASK, entity_extraction.PYDANTIC_PROMPT, False, ), ( entity_extraction.PYDANTIC_RAIL_WITH_REASK, entity_extraction.PYDANTIC_PROMPT, True, ), ], ) @pytest.mark.parametrize("multiprocessing_validators", (True, False)) def test_entity_extraction_with_reask( mocker, rail, prompt, test_full_schema_reask, multiprocessing_validators ): """Test that the entity extraction works with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) mocker.patch( "guardrails.validators.Validator.run_in_separate_process", new=multiprocessing_validators, ) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, max_tokens=2000, full_schema_reask=test_full_schema_reask, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_REASK_2 guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(entity_extraction.COMPILED_PROMPT) assert guard_history[0].llm_response.prompt_token_count == 123 assert guard_history[0].llm_response.response_token_count == 1234 assert guard_history[0].llm_response.output == entity_extraction.LLM_OUTPUT assert ( guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_REASK_1 ) # For reask validator logs nested_validator_log = ( guard_history[0] .field_validation_logs.children["fees"] .children[1] .children["name"] .validator_logs[1] ) assert nested_validator_log.value_before_validation == "my chase plan" assert nested_validator_log.value_after_validation == FieldReAsk( incorrect_value="my chase plan", fail_results=[ FailResult( fix_value="my chase", error_message="must be exactly two words", ) ], path=["fees", 1, "name"], ) # For re-asked prompt and output if test_full_schema_reask: assert ( guard_history[1].prompt.source == entity_extraction.COMPILED_PROMPT_FULL_REASK ) assert ( guard_history[1].llm_response.output == entity_extraction.LLM_OUTPUT_FULL_REASK ) else: assert guard_history[1].prompt.source == entity_extraction.COMPILED_PROMPT_REASK assert ( guard_history[1].llm_response.output == entity_extraction.LLM_OUTPUT_REASK ) assert ( guard_history[1].validated_output == entity_extraction.VALIDATED_OUTPUT_REASK_2 ) @pytest.mark.parametrize( "rail,prompt", [ (entity_extraction.RAIL_SPEC_WITH_NOOP, None), (entity_extraction.PYDANTIC_RAIL_WITH_NOOP, entity_extraction.PYDANTIC_PROMPT), ], ) def test_entity_extraction_with_noop(mocker, rail, prompt): """Test that the entity extraction works with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_NOOP guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(entity_extraction.COMPILED_PROMPT) assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_NOOP @pytest.mark.parametrize( "rail,prompt", [ (entity_extraction.RAIL_SPEC_WITH_FILTER, None), ( entity_extraction.PYDANTIC_RAIL_WITH_FILTER, entity_extraction.PYDANTIC_PROMPT, ), ], ) def test_entity_extraction_with_filter(mocker, rail, prompt): """Test that the entity extraction works with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_FILTER guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(entity_extraction.COMPILED_PROMPT) assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert ( guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_FILTER ) @pytest.mark.parametrize( "rail,prompt", [ (entity_extraction.RAIL_SPEC_WITH_FIX, None), (entity_extraction.PYDANTIC_RAIL_WITH_FIX, entity_extraction.PYDANTIC_PROMPT), ], ) def test_entity_extraction_with_fix(mocker, rail, prompt): """Test that the entity extraction works with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_FIX guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(entity_extraction.COMPILED_PROMPT) assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_FIX @pytest.mark.parametrize( "rail,prompt", [ (entity_extraction.RAIL_SPEC_WITH_REFRAIN, None), ( entity_extraction.PYDANTIC_RAIL_WITH_REFRAIN, entity_extraction.PYDANTIC_PROMPT, ), ], ) def test_entity_extraction_with_refrain(mocker, rail, prompt): """Test that the entity extraction works with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_REFRAIN guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(entity_extraction.COMPILED_PROMPT) assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert ( guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_REFRAIN ) @pytest.mark.parametrize( "rail,prompt,instructions", [ (entity_extraction.RAIL_SPEC_WITH_FIX_CHAT_MODEL, None, None), ( entity_extraction.PYDANTIC_RAIL_WITH_FIX, entity_extraction.PYDANTIC_PROMPT_CHAT_MODEL, entity_extraction.PYDANTIC_INSTRUCTIONS_CHAT_MODEL, ), ], ) def test_entity_extraction_with_fix_chat_models(mocker, rail, prompt, instructions): """Test that the entity extraction works with fix for chat models.""" mocker.patch( "guardrails.llm_providers.OpenAIChatCallable", new=MockOpenAIChatCallable, ) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer(rail, prompt, instructions) _, final_output = guard( llm_api=openai.ChatCompletion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_FIX guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt( entity_extraction.COMPILED_PROMPT_WITHOUT_INSTRUCTIONS ) assert guard_history[0].instructions == gd.Instructions( entity_extraction.COMPILED_INSTRUCTIONS ) assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_FIX def test_string_output(mocker): """Test single string (non-JSON) generation.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) guard = gd.Guard.from_rail_string(string.RAIL_SPEC_FOR_STRING) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"ingredients": "tomato, cheese, sour cream"}, num_reasks=1, ) assert final_output == string.LLM_OUTPUT guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For original prompt and output assert guard_history[0].prompt == gd.Prompt(string.COMPILED_PROMPT) assert guard_history[0].output == string.LLM_OUTPUT def test_string_reask(mocker): """Test single string (non-JSON) generation with re-asking.""" mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) guard = gd.Guard.from_rail_string(string.RAIL_SPEC_FOR_STRING_REASK) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"ingredients": "tomato, cheese, sour cream"}, num_reasks=1, max_tokens=100, ) assert final_output == string.LLM_OUTPUT_REASK guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output assert guard_history[0].instructions == gd.Instructions( string.COMPILED_INSTRUCTIONS ) assert guard_history[0].prompt == gd.Prompt(string.COMPILED_PROMPT) assert guard_history[0].output == string.LLM_OUTPUT assert guard_history[0].validated_output == string.VALIDATED_OUTPUT_REASK # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(string.COMPILED_PROMPT_REASK) assert guard_history[1].output == string.LLM_OUTPUT_REASK assert guard_history[1].validated_output == string.LLM_OUTPUT_REASK def test_skeleton_reask(mocker): mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = gd.Guard.from_rail_string(entity_extraction.RAIL_SPEC_WITH_SKELETON_REASK) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, max_tokens=1000, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_SKELETON_REASK_2 guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt( entity_extraction.COMPILED_PROMPT_SKELETON_REASK_1 ) assert guard_history[0].output == entity_extraction.LLM_OUTPUT_SKELETON_REASK_1 assert ( guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_SKELETON_REASK_1 ) # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt( entity_extraction.COMPILED_PROMPT_SKELETON_REASK_2 ) assert guard_history[1].output == entity_extraction.LLM_OUTPUT_SKELETON_REASK_2 assert ( guard_history[1].validated_output == entity_extraction.VALIDATED_OUTPUT_SKELETON_REASK_2 ) '''def test_json_output(mocker): """Test single string (non-JSON) generation.""" mocker.patch( "guardrails.llm_providers.openai_wrapper", new=openai_completion_create ) guard = gd.Guard.from_rail_string(string.RAIL_SPEC_FOR_LIST) _, final_output = guard( llm_api=openai.Completion.create, num_reasks=1, ) assert final_output == string.LIST_LLM_OUTPUT guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 1 # For original prompt and output #assert guard_history[0].prompt == gd.Prompt(string.COMPILED_PROMPT) assert guard_history[0].output == string.LLM_OUTPUT ''' @pytest.mark.parametrize( "rail,prompt,instructions,history,llm_api,expected_prompt," "expected_instructions,expected_reask_prompt,expected_reask_instructions", [ ( entity_extraction.RAIL_SPEC_WITH_REASK_NO_PROMPT, entity_extraction.OPTIONAL_PROMPT_COMPLETION_MODEL, None, None, openai.Completion.create, entity_extraction.COMPILED_PROMPT, None, entity_extraction.COMPILED_PROMPT_REASK, None, ), ( entity_extraction.RAIL_SPEC_WITH_REASK_NO_PROMPT, entity_extraction.OPTIONAL_PROMPT_CHAT_MODEL, entity_extraction.OPTIONAL_INSTRUCTIONS_CHAT_MODEL, None, openai.ChatCompletion.create, entity_extraction.COMPILED_PROMPT_WITHOUT_INSTRUCTIONS, entity_extraction.COMPILED_INSTRUCTIONS, entity_extraction.COMPILED_PROMPT_REASK_WITHOUT_INSTRUCTIONS, entity_extraction.COMPILED_INSTRUCTIONS_REASK, ), ( entity_extraction.RAIL_SPEC_WITH_REASK_NO_PROMPT, None, None, entity_extraction.OPTIONAL_MSG_HISTORY, openai.ChatCompletion.create, None, None, entity_extraction.COMPILED_PROMPT_REASK_WITHOUT_INSTRUCTIONS, entity_extraction.COMPILED_INSTRUCTIONS_REASK, ), ], ) def test_entity_extraction_with_reask_with_optional_prompts( mocker, rail, prompt, instructions, history, llm_api, expected_prompt, expected_instructions, expected_reask_prompt, expected_reask_instructions, ): """Test that the entity extraction works with re-asking.""" if llm_api == openai.Completion.create: mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) else: mocker.patch( "guardrails.llm_providers.OpenAIChatCallable", new=MockOpenAIChatCallable, ) content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = Guard.from_rail_string(rail) _, final_output = guard( llm_api=llm_api, prompt=prompt, instructions=instructions, msg_history=history, prompt_params={"document": content[:6000]}, num_reasks=1, ) # Assertions are made on the guard state object. assert final_output == entity_extraction.VALIDATED_OUTPUT_REASK_2 guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output expected_prompt = ( gd.Prompt(expected_prompt) if expected_prompt is not None else None ) assert guard_history[0].prompt == expected_prompt assert guard_history[0].output == entity_extraction.LLM_OUTPUT assert ( guard_history[0].validated_output == entity_extraction.VALIDATED_OUTPUT_REASK_1 ) expected_instructions = ( gd.Instructions(expected_instructions) if expected_instructions is not None else None ) assert guard_history[0].instructions == expected_instructions # For reask validator logs nested_validator_log = ( guard_history[0] .field_validation_logs.children["fees"] .children[1] .children["name"] .validator_logs[1] ) assert nested_validator_log.value_before_validation == "my chase plan" assert nested_validator_log.value_after_validation == FieldReAsk( incorrect_value="my chase plan", fail_results=[ FailResult( fix_value="my chase", error_message="must be exactly two words", ) ], path=["fees", 1, "name"], ) # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(expected_reask_prompt) assert guard_history[1].output == entity_extraction.LLM_OUTPUT_REASK assert ( guard_history[1].validated_output == entity_extraction.VALIDATED_OUTPUT_REASK_2 ) if expected_reask_instructions: assert guard_history[1].instructions == gd.Instructions( expected_reask_instructions ) def test_string_with_message_history_reask(mocker): """Test single string (non-JSON) generation with message history and reask.""" mocker.patch( "guardrails.llm_providers.OpenAIChatCallable", new=MockOpenAIChatCallable, ) guard = gd.Guard.from_rail_string(string.RAIL_SPEC_FOR_MSG_HISTORY) _, final_output = guard( llm_api=openai.ChatCompletion.create, msg_history=string.MOVIE_MSG_HISTORY, temperature=0.0, model="gpt-3.5-turbo", ) assert final_output == string.MSG_LLM_OUTPUT_CORRECT guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 assert guard_history[0].instructions is None assert guard_history[0].prompt is None assert guard_history[0].output == string.MSG_LLM_OUTPUT_INCORRECT assert guard_history[0].validated_output == string.MSG_VALIDATED_OUTPUT_REASK # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(string.MSG_COMPILED_PROMPT_REASK) assert guard_history[1].instructions == gd.Instructions( string.MSG_COMPILED_INSTRUCTIONS_REASK ) assert guard_history[1].output == string.MSG_LLM_OUTPUT_CORRECT assert guard_history[1].validated_output == string.MSG_LLM_OUTPUT_CORRECT def test_pydantic_with_message_history_reask(mocker): """Test JSON generation with message history re-asking.""" mocker.patch( "guardrails.llm_providers.OpenAIChatCallable", new=MockOpenAIChatCallable, ) guard = gd.Guard.from_pydantic(output_class=pydantic.WITH_MSG_HISTORY) raw_output, guarded_output = guard( llm_api=openai.ChatCompletion.create, msg_history=string.MOVIE_MSG_HISTORY, temperature=0.0, model="gpt-3.5-turbo", ) assert raw_output == pydantic.MSG_HISTORY_LLM_OUTPUT_CORRECT assert guarded_output == json.loads(pydantic.MSG_HISTORY_LLM_OUTPUT_CORRECT) guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 assert guard_history[0].instructions is None assert guard_history[0].prompt is None assert guard_history[0].output == pydantic.MSG_HISTORY_LLM_OUTPUT_INCORRECT assert guard_history[0].validated_output == pydantic.MSG_VALIDATED_OUTPUT_REASK # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(pydantic.MSG_COMPILED_PROMPT_REASK) assert guard_history[1].instructions == gd.Instructions( pydantic.MSG_COMPILED_INSTRUCTIONS_REASK ) assert guard_history[1].output == pydantic.MSG_HISTORY_LLM_OUTPUT_CORRECT assert guard_history[1].validated_output == json.loads( pydantic.MSG_HISTORY_LLM_OUTPUT_CORRECT ) def test_sequential_validator_log_is_not_duplicated(mocker): mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) proc_count_bak = os.environ.get("GUARDRAILS_PROCESS_COUNT") os.environ["GUARDRAILS_PROCESS_COUNT"] = "1" try: content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer( entity_extraction.PYDANTIC_RAIL_WITH_NOOP, entity_extraction.PYDANTIC_PROMPT ) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) logs = ( guard.guard_state.most_recent_call.history[0] .field_validation_logs.children["fees"] .children[0] .children["explanation"] .validator_logs ) assert len(logs) == 1 assert logs[0].validator_name == "OneLine" finally: if proc_count_bak is None: del os.environ["GUARDRAILS_PROCESS_COUNT"] else: os.environ["GUARDRAILS_PROCESS_COUNT"] = proc_count_bak def test_in_memory_validator_log_is_not_duplicated(mocker): mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) separate_proc_bak = OneLine.run_in_separate_process OneLine.run_in_separate_process = False try: content = gd.docs_utils.read_pdf("docs/examples/data/chase_card_agreement.pdf") guard = guard_initializer( entity_extraction.PYDANTIC_RAIL_WITH_NOOP, entity_extraction.PYDANTIC_PROMPT ) _, final_output = guard( llm_api=openai.Completion.create, prompt_params={"document": content[:6000]}, num_reasks=1, ) logs = ( guard.guard_state.most_recent_call.history[0] .field_validation_logs.children["fees"] .children[0] .children["explanation"] .validator_logs ) assert len(logs) == 1 assert logs[0].validator_name == "OneLine" finally: OneLine.run_in_separate_process = separate_proc_bak
[]
2024-01-10
jonasferoz/guardrails
tests~unit_tests~test_validator_suite.py
import importlib from typing import Dict import openai import pytest from integration_tests.mock_llm_outputs import MockOpenAICallable from guardrails.guard import Guard from guardrails.validators import FailResult from .validators.test_parameters import ( validator_test_pass_fail, validator_test_prompt, validator_test_python_str, validator_test_xml, ) # TODO: Spread this object, so essentially each validator will be its own test case @pytest.mark.parametrize("validator_test_data", [(validator_test_pass_fail)]) def test_validator_validate(validator_test_data: Dict[str, Dict[str, str]]): for validator_name in validator_test_data: print("testing validator: ", validator_name) module = importlib.import_module("guardrails.validators") validator_class = getattr(module, validator_name) for test_scenario in validator_test_data[validator_name]: if "instance_variables" in test_scenario: instance = validator_class(**test_scenario["instance_variables"]) else: instance = validator_class() result = instance.validate( test_scenario["input_data"], test_scenario["metadata"], ) assert isinstance(result, test_scenario["expected_result"]) if isinstance(result, FailResult) and "fix_value" in test_scenario: assert result.fix_value == test_scenario["fix_value"] @pytest.mark.parametrize("validator_test_data", [(validator_test_python_str)]) def test_validator_python_string( mocker, validator_test_data: Dict[str, Dict[str, str]] ): mocker.patch("guardrails.llm_providers.OpenAICallable", new=MockOpenAICallable) for validator_name in validator_test_data: print("testing validator: ", validator_name) module = importlib.import_module("guardrails.validators") validator_class = getattr(module, validator_name) validators = [validator_class(on_fail="reask")] guard = Guard.from_string( validators, description=validator_test_data[validator_name]["description"], prompt=validator_test_data[validator_name]["prompt"], instructions=validator_test_data[validator_name]["instructions"], ) _, final_output = guard( llm_api=openai.Completion.create, prompt_params=validator_test_data[validator_name]["prompt_params"], num_reasks=1, max_tokens=100, ) assert final_output == validator_test_data[validator_name]["output"] # TODO: Spread this object, so essentially each validator will be its own test case @pytest.mark.parametrize("validator_test_data", [(validator_test_xml)]) def test_validator_to_xml(validator_test_data: Dict[str, Dict[str, str]]): for validator_name in validator_test_data: module = importlib.import_module("guardrails.validators") print("testing validator: ", validator_name) validator_class = getattr(module, validator_name) if "instance_variables" in validator_test_data[validator_name]: instance = validator_class( **validator_test_data[validator_name]["instance_variables"] ) else: instance = validator_class() xml = instance.to_xml_attrib() assert xml == validator_test_data[validator_name]["expected_xml"] # TODO: Spread this object, so essentially each validator will be its own test case @pytest.mark.parametrize("validator_test_data", [(validator_test_prompt)]) def test_validator_to_prompt(validator_test_data: Dict[str, Dict[str, str]]): for validator_name in validator_test_data: module = importlib.import_module("guardrails.validators") print("testing validator: ", validator_name) validator_class = getattr(module, validator_name) if "instance_variables" in validator_test_data[validator_name]: instance = validator_class( **validator_test_data[validator_name]["instance_variables"] ) else: instance = validator_class() prompt = instance.to_prompt() assert prompt == validator_test_data[validator_name]["expected_prompt"]
[]
2024-01-10
jonasferoz/guardrails
tests~integration_tests~test_parsing.py
from typing import Dict import openai import pytest import guardrails as gd from guardrails import register_validator from guardrails.validators import FailResult, ValidationResult from .mock_llm_outputs import ( MockArbitraryCallable, MockAsyncArbitraryCallable, MockOpenAIChatCallable, ) from .test_assets import pydantic def test_parsing_reask(mocker): """Test re-asking when response is not parseable.""" mocker.patch( "guardrails.llm_providers.ArbitraryCallable", new=MockArbitraryCallable ) guard = gd.Guard.from_pydantic( output_class=pydantic.PersonalDetails, prompt=pydantic.PARSING_INITIAL_PROMPT ) def mock_callable(prompt: str): return _, final_output = guard( llm_api=mock_callable, prompt_params={"document": pydantic.PARSING_DOCUMENT}, num_reasks=1, ) assert final_output == pydantic.PARSING_EXPECTED_OUTPUT guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(pydantic.PARSING_COMPILED_PROMPT) assert guard_history[0].output == pydantic.PARSING_UNPARSEABLE_LLM_OUTPUT assert guard_history[0].validated_output is None # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(pydantic.PARSING_COMPILED_REASK) assert guard_history[1].output == pydantic.PARSING_EXPECTED_LLM_OUTPUT assert guard_history[1].validated_output == pydantic.PARSING_EXPECTED_OUTPUT @pytest.mark.asyncio async def test_async_parsing_reask(mocker): """Test re-asking when response is not parseable during async flow.""" mocker.patch( "guardrails.llm_providers.AsyncArbitraryCallable", new=MockAsyncArbitraryCallable, ) guard = gd.Guard.from_pydantic( output_class=pydantic.PersonalDetails, prompt=pydantic.PARSING_INITIAL_PROMPT ) async def mock_async_callable(prompt: str): return _, final_output = await guard( llm_api=mock_async_callable, prompt_params={"document": pydantic.PARSING_DOCUMENT}, num_reasks=1, ) assert final_output == pydantic.PARSING_EXPECTED_OUTPUT guard_history = guard.guard_state.most_recent_call.history # Check that the guard state object has the correct number of re-asks. assert len(guard_history) == 2 # For orginal prompt and output assert guard_history[0].prompt == gd.Prompt(pydantic.PARSING_COMPILED_PROMPT) assert guard_history[0].output == pydantic.PARSING_UNPARSEABLE_LLM_OUTPUT assert guard_history[0].validated_output is None # For re-asked prompt and output assert guard_history[1].prompt == gd.Prompt(pydantic.PARSING_COMPILED_REASK) assert guard_history[1].output == pydantic.PARSING_EXPECTED_LLM_OUTPUT assert guard_history[1].validated_output == pydantic.PARSING_EXPECTED_OUTPUT def test_reask_prompt_instructions(mocker): """Test that the re-ask prompt and instructions are correct. This is done implicitly, since if the incorrect prompt or instructions are used, the mock LLM will raise a KeyError. """ mocker.patch( "guardrails.llm_providers.OpenAIChatCallable", new=MockOpenAIChatCallable, ) @register_validator(name="always_fail", data_type="string") def always_fail(value: str, metadata: Dict) -> ValidationResult: return FailResult(error_message=f"Value {value} should fail.") guard = gd.Guard.from_string( validators=[(always_fail, "reask")], description="Some description", ) guard.parse( llm_output="Tomato Cheese Pizza", llm_api=openai.ChatCompletion.create, msg_history=[ {"role": "system", "content": "Some content"}, {"role": "user", "content": "Some prompt"}, ], )
[ "Some prompt", "Some content" ]
2024-01-10
jonasferoz/guardrails
tests~unit_tests~test_guard.py
import openai import pytest from pydantic import BaseModel import guardrails from guardrails import Guard, Rail, Validator from guardrails.datatypes import verify_metadata_requirements from guardrails.validators import PassResult, register_validator @register_validator("myrequiringvalidator", data_type="string") class RequiringValidator(Validator): required_metadata_keys = ["required_key"] def validate(self, value, metadata): return PassResult() @register_validator("myrequiringvalidator2", data_type="string") class RequiringValidator2(Validator): required_metadata_keys = ["required_key2"] def validate(self, value, metadata): return PassResult() @pytest.mark.parametrize( "spec,metadata", [ ( """ <rail version="0.1"> <output> <string name="string_name" format="myrequiringvalidator" /> </output> </rail> """, {"required_key": "a"}, ), ( """ <rail version="0.1"> <output> <object name="temp_name"> <string name="string_name" format="myrequiringvalidator" /> </object> <list name="list_name"> <string name="string_name" format="myrequiringvalidator2" /> </list> </output> </rail> """, {"required_key": "a", "required_key2": "b"}, ), ( """ <rail version="0.1"> <output> <object name="temp_name"> <list name="list_name"> <choice name="choice_name" discriminator="hi"> <case name="hello"> <string name="string_name" /> </case> <case name="hiya"> <string name="string_name" format="myrequiringvalidator" /> </case> </choice> </list> </object> </output> </rail> """, {"required_key": "a"}, ), ], ) @pytest.mark.asyncio async def test_required_metadata(spec, metadata): guard = guardrails.Guard.from_rail_string(spec) missing_keys = verify_metadata_requirements( {}, guard.output_schema.to_dict().values() ) assert set(missing_keys) == set(metadata) not_missing_keys = verify_metadata_requirements( metadata, guard.output_schema.to_dict().values() ) assert not_missing_keys == [] # test sync guard with pytest.raises(ValueError): guard.parse("{}") guard.parse("{}", metadata=metadata, num_reasks=0) # test async guard with pytest.raises(ValueError): await guard.parse("{}", llm_api=openai.ChatCompletion.acreate, num_reasks=0) await guard.parse( "{}", metadata=metadata, llm_api=openai.ChatCompletion.acreate, num_reasks=0 ) rail = Rail.from_string_validators([], "empty railspec") empty_rail_string = """<rail version="0.1"> <output type="string" description="empty railspec" /> </rail>""" class EmptyModel(BaseModel): empty_field: str i_guard_none = Guard(rail) i_guard_two = Guard(rail, 2) r_guard_none = Guard.from_rail("tests/unit_tests/test_assets/empty.rail") r_guard_two = Guard.from_rail("tests/unit_tests/test_assets/empty.rail", 2) rs_guard_none = Guard.from_rail_string(empty_rail_string) rs_guard_two = Guard.from_rail_string(empty_rail_string, 2) py_guard_none = Guard.from_pydantic(output_class=EmptyModel) py_guard_two = Guard.from_pydantic(output_class=EmptyModel, num_reasks=2) s_guard_none = Guard.from_string(validators=[], description="empty railspec") s_guard_two = Guard.from_string( validators=[], description="empty railspec", num_reasks=2 ) @pytest.mark.parametrize( "guard,expected_num_reasks,config_num_reasks", [ (i_guard_none, 1, None), (i_guard_two, 2, None), (i_guard_none, 3, 3), (r_guard_none, 1, None), (r_guard_two, 2, None), (r_guard_none, 3, 3), (rs_guard_none, 1, None), (rs_guard_two, 2, None), (rs_guard_none, 3, 3), (py_guard_none, 1, None), (py_guard_two, 2, None), (py_guard_none, 3, 3), (s_guard_none, 1, None), (s_guard_two, 2, None), (s_guard_none, 3, 3), ], ) def test_configure(guard: Guard, expected_num_reasks: int, config_num_reasks: int): guard.configure(config_num_reasks) assert guard.num_reasks == expected_num_reasks def guard_init_from_rail(): guard = Guard.from_rail("tests/unit_tests/test_assets/simple.rail") assert ( guard.instructions.format().source.strip() == "You are a helpful bot, who answers only with valid JSON" ) assert guard.prompt.format().source.strip() == "Extract a string from the text"
[]
2024-01-10
jonasferoz/guardrails
guardrails~llm_providers.py
import os from typing import Any, Awaitable, Callable, Dict, List, Optional, cast import openai from pydantic import BaseModel from tenacity import retry, retry_if_exception_type, wait_exponential_jitter from guardrails.utils.logs_utils import LLMResponse from guardrails.utils.pydantic_utils import convert_pydantic_model_to_openai_fn try: MANIFEST = True import manifest except ImportError: MANIFEST = False try: import cohere except ImportError: cohere = None OPENAI_RETRYABLE_ERRORS = [ openai.error.APIConnectionError, openai.error.APIError, openai.error.TryAgain, openai.error.Timeout, openai.error.RateLimitError, openai.error.ServiceUnavailableError, ] RETRYABLE_ERRORS = tuple(OPENAI_RETRYABLE_ERRORS) class PromptCallableException(Exception): pass ### # Synchronous wrappers ### class PromptCallableBase: """A wrapper around a callable that takes in a prompt. Catches exceptions to let the user know clearly if the callable failed, and how to fix it. """ def __init__(self, *args, **kwargs): self.init_args = args self.init_kwargs = kwargs def _invoke_llm(self, *args, **kwargs) -> LLMResponse: raise NotImplementedError @retry( wait=wait_exponential_jitter(max=60), retry=retry_if_exception_type(RETRYABLE_ERRORS), ) def _call_llm(self, *args, **kwargs) -> LLMResponse: return self._invoke_llm(*self.init_args, *args, **self.init_kwargs, **kwargs) def __call__(self, *args, **kwargs) -> LLMResponse: try: result = self._call_llm(*args, **kwargs) except Exception as e: raise PromptCallableException( "The callable `fn` passed to `Guard(fn, ...)` failed" f" with the following error: `{e}`. " "Make sure that `fn` can be called as a function that" " takes in a single prompt string " "and returns a string." ) if not isinstance(result, LLMResponse): raise PromptCallableException( "The callable `fn` passed to `Guard(fn, ...)` returned" f" a non-string value: {result}. " "Make sure that `fn` can be called as a function that" " takes in a single prompt string " "and returns a string." ) return result def nonchat_prompt(prompt: str, instructions: Optional[str] = None) -> str: """Prepare final prompt for nonchat engine.""" if instructions: prompt = "\n\n".join([instructions, prompt]) return prompt def chat_prompt( prompt: str, instructions: Optional[str] = None, msg_history: Optional[List[Dict]] = None, ) -> List[Dict[str, str]]: """Prepare final prompt for chat engine.""" if msg_history: return msg_history if not instructions: instructions = "You are a helpful assistant." return [ {"role": "system", "content": instructions}, {"role": "user", "content": prompt}, ] class OpenAICallable(PromptCallableBase): def _invoke_llm( self, text: str, engine: str = "text-davinci-003", instructions: Optional[str] = None, *args, **kwargs, ) -> LLMResponse: api_key = kwargs.pop("api_key", os.environ.get("OPENAI_API_KEY")) openai_response = openai.Completion.create( api_key=api_key, engine=engine, prompt=nonchat_prompt(prompt=text, instructions=instructions), *args, **kwargs, ) return LLMResponse( output=openai_response["choices"][0]["text"], prompt_token_count=openai_response["usage"]["prompt_tokens"], response_token_count=openai_response["usage"]["completion_tokens"], ) class OpenAIChatCallable(PromptCallableBase): def _invoke_llm( self, text: Optional[str] = None, model: str = "gpt-3.5-turbo", instructions: Optional[str] = None, msg_history: Optional[List[Dict]] = None, base_model: Optional[BaseModel] = None, function_call: Optional[str] = None, *args, **kwargs, ) -> LLMResponse: """Wrapper for OpenAI chat engines. Use Guardrails with OpenAI chat engines by doing ``` raw_llm_response, validated_response = guard( openai.ChatCompletion.create, prompt_params={...}, text=..., instructions=..., msg_history=..., temperature=..., ... ) ``` If `base_model` is passed, the chat engine will be used as a function on the base model. """ if msg_history is None and text is None: raise PromptCallableException( "You must pass in either `text` or `msg_history` to `guard.__call__`." ) # Configure function calling if applicable if base_model: function_params = [convert_pydantic_model_to_openai_fn(base_model)] if function_call is None: function_call = {"name": function_params[0]["name"]} fn_kwargs = {"functions": function_params, "function_call": function_call} else: fn_kwargs = {} # Call OpenAI api_key = kwargs.pop("api_key", os.environ.get("OPENAI_API_KEY")) openai_response = openai.ChatCompletion.create( api_key=api_key, model=model, messages=chat_prompt( prompt=text, instructions=instructions, msg_history=msg_history ), *args, **fn_kwargs, **kwargs, ) # Extract string from response if "function_call" in openai_response["choices"][0]["message"]: output = openai_response["choices"][0]["message"]["function_call"][ "arguments" ] else: output = openai_response["choices"][0]["message"]["content"] return LLMResponse( output=output, prompt_token_count=openai_response["usage"]["prompt_tokens"], response_token_count=openai_response["usage"]["completion_tokens"], ) class ManifestCallable(PromptCallableBase): def _invoke_llm( self, text: str, client: Any, instructions: Optional[str] = None, *args, **kwargs, ) -> LLMResponse: """Wrapper for manifest client. To use manifest for guardrailse, do ``` client = Manifest(client_name=..., client_connection=...) raw_llm_response, validated_response = guard( client, prompt_params={...}, ... ``` """ if not MANIFEST: raise PromptCallableException( "The `manifest` package is not installed. " "Install with `pip install manifest-ml`" ) client = cast(manifest.Manifest, client) manifest_response = client.run( nonchat_prompt(prompt=text, instructions=instructions), *args, **kwargs ) return LLMResponse( output=manifest_response, ) class CohereCallable(PromptCallableBase): def _invoke_llm( self, prompt: str, client_callable: Any, model: str, *args, **kwargs ) -> LLMResponse: """To use cohere for guardrails, do ``` client = cohere.Client(api_key=...) raw_llm_response, validated_response = guard( client.generate, prompt_params={...}, model="command-nightly", ... ) ``` """ # noqa if "instructions" in kwargs: prompt = kwargs.pop("instructions") + "\n\n" + prompt cohere_response = client_callable(prompt=prompt, model=model, *args, **kwargs) return LLMResponse( output=cohere_response[0].text, ) class ArbitraryCallable(PromptCallableBase): def __init__(self, llm_api: Callable, *args, **kwargs): self.llm_api = llm_api super().__init__(*args, **kwargs) def _invoke_llm(self, *args, **kwargs) -> LLMResponse: """Wrapper for arbitrary callable. To use an arbitrary callable for guardrails, do ``` raw_llm_response, validated_response = guard( my_callable, prompt_params={...}, ... ) ``` """ return LLMResponse( output=self.llm_api(*args, **kwargs), ) def get_llm_ask(llm_api: Callable, *args, **kwargs) -> PromptCallableBase: if "temperature" not in kwargs: kwargs.update({"temperature": 0}) if llm_api == openai.Completion.create: return OpenAICallable(*args, **kwargs) elif llm_api == openai.ChatCompletion.create: return OpenAIChatCallable(*args, **kwargs) elif MANIFEST and isinstance(llm_api, manifest.Manifest): return ManifestCallable(*args, client=llm_api, **kwargs) elif ( cohere and isinstance(getattr(llm_api, "__self__", None), cohere.Client) and getattr(llm_api, "__name__", None) == "generate" ): return CohereCallable(*args, client_callable=llm_api, **kwargs) # Let the user pass in an arbitrary callable. return ArbitraryCallable(*args, llm_api=llm_api, **kwargs) ### # Async wrappers ### class AsyncPromptCallableBase: def __init__(self, *args, **kwargs): self.init_args = args self.init_kwargs = kwargs async def invoke_llm( self, *args, **kwargs, ) -> LLMResponse: raise NotImplementedError @retry( wait=wait_exponential_jitter(max=60), retry=retry_if_exception_type(RETRYABLE_ERRORS), ) async def call_llm(self, *args, **kwargs) -> LLMResponse: return await self.invoke_llm( *self.init_args, *args, **self.init_kwargs, **kwargs ) async def __call__(self, *args, **kwargs) -> LLMResponse: try: result = await self.call_llm(*args, **kwargs) except Exception as e: raise PromptCallableException( "The callable `fn` passed to `Guard(fn, ...)` failed" f" with the following error: `{e}`. " "Make sure that `fn` can be called as a function that" " takes in a single prompt string " "and returns a string." ) if not isinstance(result, LLMResponse): raise PromptCallableException( "The callable `fn` passed to `Guard(fn, ...)` returned" f" a non-string value: {result}. " "Make sure that `fn` can be called as a function that" " takes in a single prompt string " "and returns a string." ) return result class AsyncOpenAICallable(AsyncPromptCallableBase): async def invoke_llm( self, text: str, engine: str = "text-davinci-003", instructions: Optional[str] = None, *args, **kwargs, ): api_key = kwargs.pop("api_key", os.environ.get("OPENAI_API_KEY")) openai_response = await openai.Completion.acreate( api_key=api_key, engine=engine, prompt=nonchat_prompt(prompt=text, instructions=instructions), *args, **kwargs, ) return LLMResponse( output=openai_response["choices"][0]["text"], prompt_token_count=openai_response["usage"]["prompt_tokens"], response_token_count=openai_response["usage"]["completion_tokens"], ) class AsyncOpenAIChatCallable(AsyncPromptCallableBase): async def invoke_llm( self, text: Optional[str] = None, model: str = "gpt-3.5-turbo", instructions: Optional[str] = None, msg_history: Optional[List[Dict]] = None, base_model: Optional[BaseModel] = None, function_call: Optional[str] = None, *args, **kwargs, ) -> LLMResponse: """Wrapper for OpenAI chat engines. Use Guardrails with OpenAI chat engines by doing ``` raw_llm_response, validated_response = guard( openai.ChatCompletion.create, prompt_params={...}, text=..., instructions=..., msg_history=..., temperature=..., ... ) ``` If `base_model` is passed, the chat engine will be used as a function on the base model. """ if msg_history is None and text is None: raise PromptCallableException( "You must pass in either `text` or `msg_history` to `guard.__call__`." ) # Configure function calling if applicable if base_model: function_params = [convert_pydantic_model_to_openai_fn(base_model)] if function_call is None: function_call = {"name": function_params[0]["name"]} fn_kwargs = {"functions": function_params, "function_call": function_call} else: fn_kwargs = {} # Call OpenAI api_key = kwargs.pop("api_key", os.environ.get("OPENAI_API_KEY")) openai_response = await openai.ChatCompletion.acreate( api_key=api_key, model=model, messages=chat_prompt( prompt=text, instructions=instructions, msg_history=msg_history ), *args, **fn_kwargs, **kwargs, ) # Extract string from response if "function_call" in openai_response["choices"][0]["message"]: output = openai_response["choices"][0]["message"]["function_call"][ "arguments" ] else: output = openai_response["choices"][0]["message"]["content"] return LLMResponse( output=output, prompt_token_count=openai_response["usage"]["prompt_tokens"], response_token_count=openai_response["usage"]["completion_tokens"], ) class AsyncManifestCallable(AsyncPromptCallableBase): async def invoke_llm( self, text: str, client: Any, instructions: Optional[str] = None, *args, **kwargs, ): """Async wrapper for manifest client. To use manifest for guardrails, do ``` client = Manifest(client_name=..., client_connection=...) raw_llm_response, validated_response = guard( client, prompt_params={...}, ... ``` """ if not MANIFEST: raise PromptCallableException( "The `manifest` package is not installed. " "Install with `pip install manifest-ml`" ) client = cast(manifest.Manifest, client) manifest_response = await client.run( nonchat_prompt(prompt=text, instructions=instructions), *args, **kwargs ) return LLMResponse( output=manifest_response, ) class AsyncArbitraryCallable(AsyncPromptCallableBase): def __init__(self, llm_api: Callable, *args, **kwargs): self.llm_api = llm_api super().__init__(*args, **kwargs) async def invoke_llm(self, *args, **kwargs) -> LLMResponse: """Wrapper for arbitrary callable. To use an arbitrary callable for guardrails, do ``` raw_llm_response, validated_response = guard( my_callable, prompt_params={...}, ... ) ``` """ output = await self.llm_api(*args, **kwargs) return LLMResponse( output=output, ) def get_async_llm_ask(llm_api: Callable[[Any], Awaitable[Any]], *args, **kwargs): if llm_api == openai.Completion.acreate: return AsyncOpenAICallable(*args, **kwargs) elif llm_api == openai.ChatCompletion.acreate: return AsyncOpenAIChatCallable(*args, **kwargs) elif MANIFEST and isinstance(llm_api, manifest.Manifest): return AsyncManifestCallable(*args, client=llm_api, **kwargs) return AsyncArbitraryCallable(*args, llm_api=llm_api, **kwargs)
[ "\n\n", "kwargs.pop(\"instructions\") + \"\\n\\n\" + prompt", "You are a helpful assistant.\n\nkwargs.pop(\"instructions\") + \"\\n\\n\" + prompt", "instructions", "You are a helpful assistant.", "iYou are a helYou are a helpful assistant.nt.) + " ]
2024-01-10
jonasferoz/guardrails
tests~unit_tests~test_validators.py
# noqa:W291 import os from typing import Any, Dict import openai import pytest from pydantic import BaseModel, Field from guardrails import Guard from guardrails.datatypes import DataType from guardrails.schema import StringSchema from guardrails.utils.reask_utils import FieldReAsk from guardrails.validators import ( BugFreeSQL, ExtractedSummarySentencesMatch, ExtractiveSummary, FailResult, Filter, PassResult, ProvenanceV1, Refrain, SimilarToDocument, SimilarToList, SqlColumnPresence, TwoWords, ValidationResult, ValidLength, check_refrain_in_dict, filter_in_dict, register_validator, ) from .mock_embeddings import MOCK_EMBEDDINGS, mock_create_embedding from .mock_provenance_v1 import mock_chat_completion, mock_chromadb_query_function @pytest.mark.parametrize( "input_dict, expected", [ ({"a": 1, "b": Refrain()}, True), ({"a": 1, "b": {"c": 2, "d": Refrain()}}, True), ({"a": [1, 2, Refrain()], "b": 4}, True), ({"a": [1, 2, {"c": Refrain()}]}, True), ({"a": [1, 2, [3, 4, Refrain()]]}, True), ({"a": 1}, False), ], ) def test_check_refrain(input_dict, expected): assert check_refrain_in_dict(input_dict) == expected @pytest.mark.parametrize( "input_dict, expected_dict", [ ({"a": 1, "b": Filter(), "c": 3}, {"a": 1, "c": 3}), ( {"a": 1, "b": {"c": 2, "d": Filter()}, "e": 4}, {"a": 1, "b": {"c": 2}, "e": 4}, ), ({"a": [1, 2, Filter()], "b": 4}, {"a": [1, 2], "b": 4}), ({"a": [1, 2, {"c": Filter(), "d": 3}]}, {"a": [1, 2, {"d": 3}]}), ({"a": [1, 2, [3, 4, Filter()]]}, {"a": [1, 2, [3, 4]]}), ({"a": 1}, {"a": 1}), ], ) def test_filter_in_dict(input_dict, expected_dict): assert filter_in_dict(input_dict) == expected_dict # TODO: Implement testing with models on CI @pytest.mark.skip( reason="This test requires the text-embedding-ada-002 embedding model." " Testing with models needs to be implemented." ) def test_similar_to_document_validator(): import os datapath = os.path.abspath(os.path.dirname(__file__) + "/../data/article1.txt") val = SimilarToDocument( document=open(datapath, "r").read(), model="text-embedding-ada-002", threshold=0.85, ) summary = "All legislative powers are held by a Congress" " consisting of two chambers, the Senate and the House of Representatives." assert isinstance(val.validate(summary, {}), PassResult) class TestBugFreeSQLValidator: def test_bug_free_sql(self): # TODO Make this robust by computing the abs path of the sql file # relative to this file val = BugFreeSQL( schema_file="./tests/unit_tests/test_assets/valid_schema.sql", conn="sqlite://", ) bad_query = "select name, fro employees" result = val.validate(bad_query, {}) assert isinstance(result, FailResult) assert result.error_message != "" good_query = "select name from employees;" assert isinstance(val.validate(good_query, {}), PassResult) def test_long_sql_schema_no_exception(self): val = BugFreeSQL( schema_file="./tests/unit_tests/test_assets/spider.sql", conn="sqlite://", ) assert val is not None def test_bug_free_sql_simple(self): val = BugFreeSQL() bad_query = "select name, fro employees" result = val.validate(bad_query, {}) assert isinstance(result, FailResult) assert result.error_message != "" good_query = "select name from employees;" assert isinstance(val.validate(good_query, {}), PassResult) def test_sql_column_presense(self): sql = "select name, age from employees;" columns = ["name", "address"] val = SqlColumnPresence(cols=columns) result = val.validate(sql, {}) assert isinstance(result, FailResult) assert result.error_message in ( "Columns [age] not in [name, address]", "Columns [age] not in [address, name]", ) def test_summary_validators(mocker): pytest.importorskip("nltk", reason="nltk is not installed") pytest.importorskip("thefuzz", reason="thefuzz is not installed") mocker.patch("openai.Embedding.create", new=mock_create_embedding) mocker.patch("guardrails.embedding.OpenAIEmbedding.output_dim", new=2) summary = "It was a nice day. I went to the park. I saw a dog." metadata = { "filepaths": [ "./tests/unit_tests/test_assets/article1.txt", "./tests/unit_tests/test_assets/article2.txt", ] } val = ExtractedSummarySentencesMatch(threshold=0.1) result = val.validate(summary, metadata) assert isinstance(result, PassResult) assert "citations" in result.metadata assert "summary_with_citations" in result.metadata assert result.metadata["citations"] == {1: 1, 2: 1, 3: 1} assert ( result.metadata["summary_with_citations"] == """It was a nice day. [1] I went to the park. [1] I saw a dog. [1] [1] ./tests/unit_tests/test_assets/article1.txt [2] ./tests/unit_tests/test_assets/article2.txt""" ) val = ExtractiveSummary( threshold=30, ) result = val.validate(summary, metadata) assert isinstance(result, PassResult) assert "citations" in result.metadata assert "summary_with_citations" in result.metadata assert result.metadata["citations"] == {1: 1, 2: 2, 3: 1} assert ( result.metadata["summary_with_citations"] == """It was a nice day. [1] I went to the park. [2] I saw a dog. [1] [1] ./tests/unit_tests/test_assets/article1.txt [2] ./tests/unit_tests/test_assets/article2.txt""" ) @register_validator("mycustomhellovalidator", data_type="string") def hello_validator(value: Any, metadata: Dict[str, Any]) -> ValidationResult: if "hello" in value.lower(): return FailResult( error_message="Hello is too basic, try something more creative.", fix_value="hullo", ) return PassResult() def test_validator_as_tuple(): # (Callable, on_fail) tuple fix class MyModel(BaseModel): a_field: str = Field(..., validators=[(hello_validator, "fix")]) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hullo"} # (string, on_fail) tuple fix class MyModel(BaseModel): a_field: str = Field( ..., validators=[("two_words", "reask"), ("mycustomhellovalidator", "fix")] ) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hullo"} # (Validator, on_fail) tuple fix class MyModel(BaseModel): a_field: str = Field(..., validators=[(TwoWords(), "fix")]) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hello there"} # (Validator, on_fail) tuple reask hullo_reask = FieldReAsk( incorrect_value="hello there yo", fail_results=[ FailResult( error_message="Hello is too basic, try something more creative.", fix_value="hullo", ) ], path=["a_field"], ) class MyModel(BaseModel): a_field: str = Field(..., validators=[(hello_validator, "reask")]) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hullo"} assert guard.guard_state.all_histories[0].history[0].reasks[0] == hullo_reask hello_reask = FieldReAsk( incorrect_value="hello there yo", fail_results=[ FailResult( error_message="must be exactly two words", fix_value="hello there", ) ], path=["a_field"], ) # (string, on_fail) tuple reask class MyModel(BaseModel): a_field: str = Field(..., validators=[("two-words", "reask")]) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hello there"} assert guard.guard_state.all_histories[0].history[0].reasks[0] == hello_reask # (Validator, on_fail) tuple reask class MyModel(BaseModel): a_field: str = Field(..., validators=[(TwoWords(), "reask")]) guard = Guard.from_pydantic(MyModel) output = guard.parse( '{"a_field": "hello there yo"}', num_reasks=0, ) assert output == {"a_field": "hello there"} assert guard.guard_state.all_histories[0].history[0].reasks[0] == hello_reask # Fail on string class MyModel(BaseModel): a_field: str = Field(..., validators=["two-words"]) with pytest.raises(ValueError): Guard.from_pydantic(MyModel) def test_custom_func_validator(): rail_str = """ <rail version="0.1"> <output> <string name="greeting" format="mycustomhellovalidator" on-fail-mycustomhellovalidator="fix"/> </output> </rail> """ guard = Guard.from_rail_string(rail_str) output = guard.parse( '{"greeting": "hello"}', num_reasks=0, ) assert output == {"greeting": "hullo"} guard_history = guard.guard_state.all_histories[0].history assert len(guard_history) == 1 validator_log = ( guard_history[0].field_validation_logs.children["greeting"].validator_logs[0] ) assert validator_log.validator_name == "mycustomhellovalidator" assert validator_log.validation_result == FailResult( error_message="Hello is too basic, try something more creative.", fix_value="hullo", ) def test_bad_validator(): with pytest.raises(ValueError): @register_validator("mycustombadvalidator", data_type="string") def validate(value: Any) -> ValidationResult: pass def test_provenance_v1(mocker): """Test initialisation of ProvenanceV1.""" mocker.patch("openai.ChatCompletion.create", new=mock_chat_completion) API_KEY = "<YOUR_KEY>" LLM_RESPONSE = "This is a sentence." # Initialise Guard from string string_guard = Guard.from_string( validators=[ ProvenanceV1( validation_method="full", llm_callable="gpt-3.5-turbo", top_k=3, max_tokens=100, on_fail="fix", ) ], description="testmeout", ) output_schema: StringSchema = string_guard.rail.output_schema data_type: DataType = getattr(output_schema._schema, "string") validators = data_type.format_attr.validators prov_validator: ProvenanceV1 = validators[0] # Check types remain intact assert isinstance(prov_validator._validation_method, str) assert isinstance(prov_validator._top_k, int) assert isinstance(prov_validator._max_tokens, int) # Test guard.parse() with 3 different ways of setting the OpenAI API key API key # 1. Setting the API key directly openai.api_key = API_KEY output = string_guard.parse( llm_output=LLM_RESPONSE, metadata={"query_function": mock_chromadb_query_function}, ) assert output == LLM_RESPONSE # 2. Setting the environment variable os.environ["OPENAI_API_KEY"] = API_KEY output = string_guard.parse( llm_output=LLM_RESPONSE, metadata={"query_function": mock_chromadb_query_function}, ) assert output == LLM_RESPONSE # 3. Passing the API key as an argument output = string_guard.parse( llm_output=LLM_RESPONSE, metadata={"query_function": mock_chromadb_query_function}, api_key=API_KEY, api_base="https://api.openai.com", ) assert output == LLM_RESPONSE @pytest.mark.parametrize( "min,max,expected_xml", [ (0, 12, "length: 0 12"), ("0", "12", "length: 0 12"), (None, 12, "length: None 12"), (1, None, "length: 1 None"), ], ) def test_to_xml_attrib(min, max, expected_xml): validator = ValidLength(min=min, max=max) xml_validator = validator.to_xml_attrib() assert xml_validator == expected_xml def test_similar_to_list(): # Mock embedding function def embed_function(text: str): """Mock embedding function.""" return MOCK_EMBEDDINGS[text] # Initialise validator validator = SimilarToList() # Test get_semantic_similarity method similarity = validator.get_semantic_similarity( "broadcom", "broadcom", embed_function ) # Assert that similarity is very close to 0 assert similarity == pytest.approx(0.0, abs=1e-2)
[]
2024-01-10
AZURE-ARC-0/AgentVerse
agentverse~environments~tasksolving_env~rules~executor~tool_using.py
import json import ast import openai from string import Template from colorama import Fore from aiohttp import ClientSession from copy import deepcopy from typing import TYPE_CHECKING, Any, List, Tuple from agentverse.agents import ExecutorAgent from agentverse.message import Message, ExecutorMessage, SolverMessage from agentverse.logging import logger from . import BaseExecutor, executor_registry import asyncio url = "http://127.0.0.1:8080" # url = "http://8.217.97.110:8080" SUMMARIZE_PROMPT = """Here is the text gathered from a webpage, and a question you need to answer from the webpage. -- Webpage -- ${webpage} -- Question -- ${question} Now summarize the webpage to answer the question. If the question cannot be answer from the webpage, return the summarization of the webpage.""" @executor_registry.register("tool-using") class ToolUsingExecutor(BaseExecutor): num_agents: int = 3 max_tool_call_times: int = 10 tools: List[dict] = [] tool_names: List[str] = [] tool_config: str = None cookies: dict = {} tool_retrieval: bool = False real_execution_agents: dict = {} agent_names: List[str] = [] # tool_description: str def __init__(self, *args, **kwargs): assert kwargs.get("tool_config", None) is not None with open(kwargs.get("tool_config"), "r") as f: tools_dict = json.load(f) tools = tools_dict["tools_json"] tool_names = [t["name"] for t in tools] # For each tool, we manually add a "thought" argument to achieve # chain-of-thought in OpenAI's function call. for t in tools: properties = t["parameters"]["properties"] thought = { "thought": { "type": "string", "description": "Your internal reasoning and thoughts on the task, and how you plan to solve it based on the current attempts.", } } thought.update(properties) t["parameters"]["properties"] = thought t["parameters"]["required"].insert(0, "thought") super().__init__( tools=tools, tool_names=tool_names, # tool_description=tool_description, *args, **kwargs, ) async def astep( self, agent: ExecutorAgent, task_description: str, plans: List[SolverMessage], *args, **kwargs, ): plan_this_turn = {} agent_name_this_turn = [] for i in range(len(plans)): name = plans[i].content.split("-")[0].strip() if name not in self.real_execution_agents: self.real_execution_agents[name] = deepcopy(agent) self.real_execution_agents[name].name = name self.agent_names.append(name) plan_this_turn[name] = plans[i].content.split("-")[1].strip() agent_name_this_turn.append(name) # agents = [deepcopy(agent) for _ in range(len(plans))] if self.tool_retrieval: # We retrieve 5 related tools for each agent tools_and_cookies = await asyncio.gather( *[ self.retrieve_tools(plan_this_turn[name], self.tools) for name in agent_name_this_turn ] ) tools = { name: t[0] for name, t in zip(agent_name_this_turn, tools_and_cookies) } cookies = { name: t[1] for name, t in zip(agent_name_this_turn, tools_and_cookies) } self.update_cookies(cookies) else: # We just use the tools that are provided in the config file tools = {name: self.tools for name in agent_name_this_turn} # Record the indices of agents that have finished their tasks # so that they will not be called again finished_agent_names = set() # result = ["" for _ in range(len(plan_this_turn))] result = {name: "" for name in agent_name_this_turn} for current_turn in range(self.max_tool_call_times): if len(finished_agent_names) == len(agent_name_this_turn): # All agents have finished their tasks. Break the loop. break # Filter out agents that have finished and gather tool actions for the rest tool_calls = [] active_agents_names = [ name for name in agent_name_this_turn if name not in finished_agent_names ] for name in active_agents_names: if current_turn == self.max_tool_call_times - 1: tool = [t for t in tools[name] if t["name"] == "submit_task"] else: tool = tools[name] tool_calls.append( self.real_execution_agents[name].astep( task_description, plan_this_turn[name], tool, current_turn=current_turn + 1, ) ) # Use asyncio.gather to run astep concurrently tool_call_decisions = await asyncio.gather(*tool_calls) for name, tool_call_result in zip(active_agents_names, tool_call_decisions): self.real_execution_agents[name].add_message_to_memory( [tool_call_result] ) # Actually call the tool and get the observation tool_responses = await asyncio.gather( *[ ToolUsingExecutor.call_tool( tool.tool_name, tool.tool_input, self.cookies.get(name, None), ) for name, tool in zip(active_agents_names, tool_call_decisions) ] ) # Update each agent's memory and check if they have finished cookies = {} for name, response in zip(active_agents_names, tool_responses): observation = response["observation"] is_finish = response["is_finish"] cookies[name] = response["cookies"] self.real_execution_agents[name].add_message_to_memory([observation]) logger.info( f"\nTool: {observation.tool_name}\nTool Input: {observation.tool_input}\nObservation: {observation.content}", name, Fore.YELLOW, ) if is_finish: finished_agent_names.add(name) result[name] = observation.content self.update_cookies(cookies) message_result = [] for name, conclusion in result.items(): if conclusion != "": message_result.append( ExecutorMessage( content=f"[{name}]: My execution result:\n{conclusion}", sender=name, ) ) return message_result def update_cookies(self, cookies: dict): for name, cookie in cookies.items(): self.cookies[name] = cookie @classmethod async def retrieve_tools( cls, plan: SolverMessage, curr_tools: List = [], cookies=None ): async with ClientSession(cookies=cookies) as session: if cookies is None: async with session.post(f"{url}/get_cookie", timeout=30) as response: cookies = response.cookies session.cookie_jar.update_cookies(cookies) await response.text() # Sometimes the toolserver's docker container is not ready yet # So we need to wait for a while await asyncio.sleep(10) async with session.post( f"{url}/retrieving_tools", json={"question": plan.content, "top_k": 5} ) as response: retrieved_tools = await response.json() retrieved_tools = ast.literal_eval(retrieved_tools) tools = deepcopy(curr_tools) existed_tool_names = set([t["name"] for t in tools]) # Add the retrieved tools into the final tools for tool in retrieved_tools["tools_json"]: if tool["name"] not in existed_tool_names: existed_tool_names.add(tool["name"]) tools.append(tool) return tools, cookies @classmethod async def call_tool(cls, command: str, arguments: dict, cookies=None): async def _summarize_webpage(webpage, question): summarize_prompt = Template(SUMMARIZE_PROMPT).safe_substitute( webpage=webpage, question=question ) for _ in range(3): try: response = await openai.ChatCompletion.acreate( messages=[{"role": "user", "content": summarize_prompt}], model="gpt-3.5-turbo-16k", ) except: continue return response["choices"][0]["message"]["content"] if command == "submit_task": return { "observation": ExecutorMessage( content=f"Task Status: {arguments['status']}\nConclusion: {arguments['conclusion']}", sender="function", tool_name=command, tool_input=arguments, ), "is_finish": True, "cookies": cookies, } if command == "": return { "observation": ExecutorMessage( content=f"The function calling format is incorrect.", sender="function", tool_name=command, tool_input=arguments, ), "is_finish": False, "cookies": cookies, } for i in range(3): try: async with ClientSession(cookies=cookies) as session: if cookies is None: async with session.post( f"{url}/get_cookie", timeout=30 ) as response: cookies = response.cookies session.cookie_jar.update_cookies(cookies) await response.text() # Sometimes the toolserver's docker container is not ready yet # So we need to wait for a while await asyncio.sleep(10) payload_arguments = deepcopy(arguments) if "thought" in payload_arguments: del payload_arguments["thought"] payload = { "tool_name": command, "arguments": payload_arguments, } # async with ClientSession() as session: async with session.post( f"{url}/execute_tool", json=payload, headers={ "toolbench_key": "p5ZASSLBO0EknAQLE5ecNZ7kq5i1YfY9eoWUXNxL3TM6lXwdXs" }, timeout=30, ) as response: content = await response.text() if command == "WebEnv_browse_website": content = await _summarize_webpage( content, arguments["question"] ) message = ExecutorMessage( content=content, sender="function", tool_name=command, tool_input=arguments, ) # async with session.post( # f"{url}/release_session", timeout=30 # ) as response: # await response.text() break except Exception as e: message = ExecutorMessage( content="Failed to call the tool. Exception: " + str(e), sender="function", tool_name=command, tool_input=arguments, ) continue return {"observation": message, "is_finish": False, "cookies": cookies} def broadcast_messages(self, agents, messages) -> None: for agent in agents: agent.add_message_to_memory(messages)
[ "The function calling format is incorrect.", "Here is the text gathered from a webpage, and a question you need to answer from the webpage. \n-- Webpage -- \n${webpage}\n-- Question --\n${question}\n\nNow summarize the webpage to answer the question. If the question cannot be answer from the webpage, return the summarization of the webpage.", "[PLACEHOLDER]: My execution result:\nPLACEHOLDER", "Task Status: PLACEHOLDER\nConclusion: PLACEHOLDER", "Failed to call the tool. Exception: PLACEHOLDER" ]
2024-01-10
Tadinu/my_arm
scripts~my_arm~gazebo_env.py
import gym import rospy #import roslaunch import os import signal import subprocess from os import path from std_srvs.srv import Empty ## if launchfilePath.startswith("/"): ## fullpath = launchfilePath ## else: ## fullpath = os.path.join(os.path.dirname(__file__), "launch", launchfile) class GazeboEnv(gym.Env): """Superclass for all Gazebo environments. """ metadata = {'render.modes': ['human']} def __init__(self, rosPackName, launchFileName, rosNodeName): # if not path.exists(fullpath): # raise IOError("File "+launchFilePath+" does not exist") #start roscore #subprocess.Popen("roscore") #print ("Roscore launched!") # Launch the simulation with the given launchfile name rospy.init_node(rosNodeName, anonymous=True) # subprocess.Popen(["roslaunch",rosPackName, launchFileName]) print ("Gazebo launched!") self.gzclient_pid = 0 def _step(self, action): # Implement this method in every subclass # Perform a step in gazebo. E.g. move the robot raise NotImplementedError def _reset(self): # Implemented in subclass raise NotImplementedError def _render(self, mode="human", close=False): if close: tmp = os.popen("ps -Af").read() proccount = tmp.count('gzclient') if proccount > 0: if self.gzclient_pid != 0: os.kill(self.gzclient_pid, signal.SIGTERM) os.wait() return tmp = os.popen("ps -Af").read() proccount = tmp.count('gzclient') if proccount < 1: subprocess.Popen("gzclient") self.gzclient_pid = int(subprocess.check_output(["pidof","-s","gzclient"])) else: self.gzclient_pid = 0 def _close(self): # Kill gzclient, gzserver and roscore tmp = os.popen("ps -Af").read() gzclient_count = tmp.count('gzclient') gzserver_count = tmp.count('gzserver') roscore_count = tmp.count('roscore') rosmaster_count = tmp.count('rosmaster') if gzclient_count > 0: os.system("killall -9 gzclient") if gzserver_count > 0: os.system("killall -9 gzserver") if rosmaster_count > 0: os.system("killall -9 rosmaster") if roscore_count > 0: os.system("killall -9 roscore") if (gzclient_count or gzserver_count or roscore_count or rosmaster_count >0): os.wait() def _configure(self): # TODO # From OpenAI API: Provides runtime configuration to the enviroment # Maybe set the Real Time Factor? pass def _seed(self): # TODO # From OpenAI API: Sets the seed for this env's random number generator(s) pass
[]
2024-01-10
lindo-zy/ChatGptPlusApiMarket
chatgpt~api~items.py
#!/usr/bin/python3 # -*- coding:utf-8 -*- import asyncio import datetime import os import uuid import openai from fastapi import APIRouter, Depends, Header from loguru import logger from starlette.responses import StreamingResponse from chatgpt.conf.mysettings import settings from chatgpt.database_option import crud, schemas from chatgpt.db import db_session, Session from chatgpt.message_bodys import * from chatgpt.models.users import SecretKey, User from chatgpt.schemas.items import VerifySchema from chatgpt.schemas.users import GenKeySchema, NodeToken from chatgpt.util import num_tokens_from_messages, get_proxies from chatgpt.utils.jwt_tool import JwtTool from chatgpt.utils.secret_utils import SecretUtils app = APIRouter() @app.post('/chat-process2') async def chat_process2(data: ChatProcessRequest, db: Session = Depends(crud.get_db)): """ chatGpt对话请求 :param data: :param db: :return: """ chat_response = ChatProcessResponse() # 配置对话内容 messages = [] if data.options.parentMessageId: # 查询历史会话 db_conversation = crud.get_conversation_by_id(db, id=data.options.parentMessageId) if db_conversation: messages = db_conversation.contents if not messages: messages.append({"role": "system", "content": data.systemMessage}) chat = {"role": "user", "content": data.prompt} messages.append(chat) # 默认gpt3.5模型 model = "gpt-3.5-turbo-0301" # 计算token chat_response.question_token = await num_tokens_from_messages(messages, model=model) # openai请求参数 params = { "model": model, "messages": messages, 'temperature': data.temperature, 'top_p': data.top_p, "stream": True } # 设置代理 openai.proxy = get_proxies(package="openai") # 创建会话 chat_reply_process = await asyncio.get_running_loop().run_in_executor(None, lambda: openai.ChatCompletion.create( **params)) async def generate(): for index, chat in enumerate(chat_reply_process): detail = chat.to_dict_recursive() choice = detail.get("choices")[0] delta = choice.get("delta") if not chat_response.role: chat_response.role = delta.get("role", "") if not chat_response.id: chat_response.id = detail.get("id", "") chat_response.text += delta.get("content", "") chat_response.delta = delta chat_response.detail = detail chat_response.answer_token = await num_tokens_from_messages( [{"role": "system", "content": chat_response.text}], model=model) response = chat_response.json(ensure_ascii=False) yield f"\n{response}" if index else response consume_token = [chat_response.question_token, chat_response.answer_token] messages.append({"role": chat_response.role, "content": chat_response.text}) # 更新会话消息 if data.options.parentMessageId: # 若存在则只更新 try: result = crud.update_conversation(db, id=data.options.parentMessageId, messages=messages, new_id=chat_response.id, consume_token=consume_token) logger.info( f"id:{result.id} title:{result.title} consume_token:{result.consume_token} messages:{result.contents}") except Exception as e: logger.error( f"Update error: id={data.options.parentMessageId} new_id={chat_response.id} consume_token:{consume_token} messages={messages}") logger.error(f"Insert error reason: {e.__str__()}") else: conversation = schemas.ConversationInsert( id=chat_response.id, user_id=None, title=data.prompt, contents=messages, create_time=datetime.datetime.now(), consume_token=consume_token ) # 插入新的会话 try: result = crud.insert_conversation(db, conversation) logger.info( f"id:{result.id} title:{result.title} consume_token:{result.consume_token} messages:{result.contents}") except Exception as e: logger.error(f"Insert error: {conversation.json(ensure_ascii=False)}") logger.error(f"Insert error reason: {e.__str__()}") # 流式返回 return StreamingResponse(content=generate(), media_type="application/octet-stream") @app.post('/request') async def request(x_token: str = Header(...)): """ 调用openai前查询当前用户是否还有剩余次数 :param x_token: :return: """ try: info = JwtTool.check_access_token(x_token) if info: username = info['username'] # 查询数据库剩余次数 with db_session as session: rows = session.query(User).filter_by(username=username).with_for_update().limit(1).all() # 如果有这个用户,更新次数 if rows: remaining_count = rows[0].remaining_count if remaining_count > 0: return {'message': 'request处理成功!', 'status': 'success'} return {'message': f'request接口异常!', 'status': 'error'} except Exception as e: logger.error(e) return {'message': f'request接口异常!', 'status': 'error'} @app.post('/charging') async def charging(x_token: str = Header(...)): # 这个接口在api调用成功后再触发,否则api未访问成功也扣费,逻辑有问题 try: info = JwtTool.check_access_token(x_token) if info: username = info['username'] # 查询数据库剩余次数 with db_session as session: rows = session.query(User).filter_by(username=username).with_for_update().limit(1).all() # 如果有这个用户,更新次数 if rows: remaining_count = rows[0].remaining_count if remaining_count - 1 < 0: return {'message': '用户剩余次数为0!', 'status': 'error'} else: rows[0].remaining_count -= 1 session.commit() return {'message': 'request处理成功!', 'status': 'success'} else: # 没有这个用户 return {'message': 'request接口异常!', 'status': 'error'} return {'message': 'request接口异常!', 'status': 'error'} except Exception as e: return {'message': f'request接口异常!{e}', 'status': 'error'} @app.post("/session", response_model=SessionResponse, summary="") async def session(): """ session认证 :return: """ response = SessionResponse() return response @app.post('/verify') async def verify(item: VerifySchema): """ 验证前端秘钥,他秘钥的名字是token,暂时不改他名称 :param item: :return: """ # 当前白嫖用户使用秘钥进行验证,更新换秘钥重新生成jwt即可 secret_key = item.token try: with db_session as session: rows = session.query(SecretKey).all() normal_key = rows[0].normal_key # 2类key,微信群的key group_key = rows[0].group_key # 3类key,星球的key vip_key = rows[0].vip_key # 管理员秘钥 # admin_key = rows[0].admin_key if secret_key in [normal_key, str(normal_key), group_key, str(group_key), vip_key, str(vip_key)]: num_map = { str(normal_key): settings.NORMAL_NUM, str(group_key): settings.GROUP_NUM, str(vip_key): settings.VIP_NUM, # str(admin_key): 9999999, } cur_num = num_map[secret_key] username = uuid.uuid4() # 写入到数据库 rows = session.query(User).filter_by(username=username).with_for_update().all() if not rows: new_object = User(username=username, remaining_count=cur_num) session.add(new_object) session.commit() jwt = JwtTool.create_access_token(username=str(username), num=cur_num) logger.info(jwt) return {'status': 'Success', 'message': f'免费用户:{username}添加成功', 'data': '', 'token': jwt} else: return {'message': "非法秘钥!", 'status': 'error'} except Exception as e: return {'message': f'verify接口异常:{e}', 'status': 'error'} @app.post('/gen_key') async def gen_key(item: GenKeySchema): """ 更新秘钥 :param item: :return: """ if item.admin_token == settings.ADMIN_TOKEN_LIST: # 更新数据库中的秘钥 with db_session as session: rows = session.query(SecretKey).with_for_update().limit(1).all() normal_key, group_key, vip_key = SecretUtils.gen_secret_key() for row in rows: row.normal_key = normal_key row.group_key = group_key row.vip_key = vip_key session.commit() return {'message': '秘钥更新完成!', 'status': 'success', 'normal_key': normal_key, 'group_key': group_key, 'vip_key': vip_key} return {'message': '无效的秘钥!', 'status': 'error'} @app.post('/openai') async def openai_key(item: NodeToken): """ 传递openai的秘钥给node :param item: :return: """ if item.token not in ['openai']: return {'message': '非法token', 'status': 'error'} key = os.getenv('OPENAI_KEY') return {'message': '获取key', 'status': 'success', 'apiKey': key}
[]
2024-01-10
Bianca-Cassemiro/llm
lhama.py
import random import gradio as gr from langchain.llms import Ollama from langchain.prompts import ChatPromptTemplate from langchain.schema.runnable import RunnableMap, RunnablePassthrough model = Ollama(model="inspetor_bugiganga") prompt = ChatPromptTemplate.from_template( """ Retrieve information on safety standards in industrial environments. Provide details on regulations, guidelines, and best practices to ensure a secure working environment in industrial settings. Include information on any recent updates or changes in safety protocols. Summarize key points and emphasize the importance of compliance with these standards for the well-being of workers and the overall safety of industrial operations. """ ) chain = {"activity": RunnablePassthrough()} | prompt | model def response(message, history): print(message) msg = "" for s in chain.stream(message): print(s, end="", flush=True) msg += s yield msg demo = gr.ChatInterface(response).queue() demo.launch()
[ "\nRetrieve information on safety standards in industrial environments. Provide details on regulations, guidelines, and best practices to ensure a secure working environment in industrial settings. Include information on any recent updates or changes in safety protocols. Summarize key points and emphasize the importance of compliance with these standards for the well-being of workers and the overall safety of industrial operations.\n\n" ]
2024-01-10
DCoinHub/langflow
src~backend~langflow~components~utilities~JSONDocumentBuilder.py
### JSON Document Builder # Build a Document containing a JSON object using a key and another Document page content. # **Params** # - **Key:** The key to use for the JSON object. # - **Document:** The Document page to use for the JSON object. # **Output** # - **Document:** The Document containing the JSON object. from langflow import CustomComponent from langchain.schema import Document from langflow.services.database.models.base import orjson_dumps class JSONDocumentBuilder(CustomComponent): display_name: str = "JSON Document Builder" description: str = "Build a Document containing a JSON object using a key and another Document page content." output_types: list[str] = ["Document"] beta = True documentation: str = ( "https://docs.langflow.org/components/utilities#json-document-builder" ) field_config = { "key": {"display_name": "Key"}, "document": {"display_name": "Document"}, } def build( self, key: str, document: Document, ) -> Document: documents = None if isinstance(document, list): documents = [ Document( page_content=orjson_dumps({key: doc.page_content}, indent_2=False) ) for doc in document ] elif isinstance(document, Document): documents = Document( page_content=orjson_dumps({key: document.page_content}, indent_2=False) ) else: raise TypeError( f"Expected Document or list of Documents, got {type(document)}" ) self.repr_value = documents return documents
[]
2024-01-10
DCoinHub/langflow
src~backend~langflow~components~utilities~GetRequest.py
from langflow import CustomComponent from langchain.schema import Document from langflow.services.database.models.base import orjson_dumps import requests from typing import Optional class GetRequest(CustomComponent): display_name: str = "GET Request" description: str = "Make a GET request to the given URL." output_types: list[str] = ["Document"] documentation: str = "https://docs.langflow.org/components/utilities#get-request" beta = True field_config = { "url": { "display_name": "URL", "info": "The URL to make the request to", "is_list": True, }, "headers": { "display_name": "Headers", "info": "The headers to send with the request.", }, "code": {"show": False}, "timeout": { "display_name": "Timeout", "field_type": "int", "info": "The timeout to use for the request.", "value": 5, }, } def get_document( self, session: requests.Session, url: str, headers: Optional[dict], timeout: int ) -> Document: try: response = session.get(url, headers=headers, timeout=int(timeout)) try: response_json = response.json() result = orjson_dumps(response_json, indent_2=False) except Exception: result = response.text self.repr_value = result return Document( page_content=result, metadata={ "source": url, "headers": headers, "status_code": response.status_code, }, ) except requests.Timeout: return Document( page_content="Request Timed Out", metadata={"source": url, "headers": headers, "status_code": 408}, ) except Exception as exc: return Document( page_content=str(exc), metadata={"source": url, "headers": headers, "status_code": 500}, ) def build( self, url: str, headers: Optional[dict] = None, timeout: int = 5, ) -> list[Document]: if headers is None: headers = {} urls = url if isinstance(url, list) else [url] with requests.Session() as session: documents = [self.get_document(session, u, headers, timeout) for u in urls] self.repr_value = documents return documents
[]
2024-01-10
DCoinHub/langflow
src~backend~langflow~api~v1~callback.py
import asyncio from uuid import UUID from langchain.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler from langflow.api.v1.schemas import ChatResponse, PromptResponse from typing import Any, Dict, List, Optional from langflow.services.getters import get_chat_service from langflow.utils.util import remove_ansi_escape_codes from langchain.schema import AgentAction, AgentFinish from loguru import logger # https://github.com/hwchase17/chat-langchain/blob/master/callback.py class AsyncStreamingLLMCallbackHandler(AsyncCallbackHandler): """Callback handler for streaming LLM responses.""" def __init__(self, client_id: str): self.chat_service = get_chat_service() self.client_id = client_id self.websocket = self.chat_service.active_connections[self.client_id] async def on_llm_new_token(self, token: str, **kwargs: Any) -> None: resp = ChatResponse(message=token, type="stream", intermediate_steps="") await self.websocket.send_json(resp.dict()) async def on_tool_start( self, serialized: Dict[str, Any], input_str: str, **kwargs: Any ) -> Any: """Run when tool starts running.""" resp = ChatResponse( message="", type="stream", intermediate_steps=f"Tool input: {input_str}", ) await self.websocket.send_json(resp.dict()) async def on_tool_end(self, output: str, **kwargs: Any) -> Any: """Run when tool ends running.""" observation_prefix = kwargs.get("observation_prefix", "Tool output: ") split_output = output.split() first_word = split_output[0] rest_of_output = split_output[1:] # Create a formatted message. intermediate_steps = f"{observation_prefix}{first_word}" # Create a ChatResponse instance. resp = ChatResponse( message="", type="stream", intermediate_steps=intermediate_steps, ) rest_of_resps = [ ChatResponse( message="", type="stream", intermediate_steps=f"{word}", ) for word in rest_of_output ] resps = [resp] + rest_of_resps # Try to send the response, handle potential errors. try: # This is to emulate the stream of tokens for resp in resps: await self.websocket.send_json(resp.dict()) except Exception as exc: logger.error(f"Error sending response: {exc}") async def on_tool_error( self, error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID] = None, tags: Optional[List[str]] = None, **kwargs: Any, ) -> None: """Run when tool errors.""" async def on_text(self, text: str, **kwargs: Any) -> Any: """Run on arbitrary text.""" # This runs when first sending the prompt # to the LLM, adding it will send the final prompt # to the frontend if "Prompt after formatting" in text: text = text.replace("Prompt after formatting:\n", "") text = remove_ansi_escape_codes(text) resp = PromptResponse( prompt=text, ) await self.websocket.send_json(resp.dict()) self.chat_service.chat_history.add_message(self.client_id, resp) async def on_agent_action(self, action: AgentAction, **kwargs: Any): log = f"Thought: {action.log}" # if there are line breaks, split them and send them # as separate messages if "\n" in log: logs = log.split("\n") for log in logs: resp = ChatResponse(message="", type="stream", intermediate_steps=log) await self.websocket.send_json(resp.dict()) else: resp = ChatResponse(message="", type="stream", intermediate_steps=log) await self.websocket.send_json(resp.dict()) async def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any: """Run on agent end.""" resp = ChatResponse( message="", type="stream", intermediate_steps=finish.log, ) await self.websocket.send_json(resp.dict()) class StreamingLLMCallbackHandler(BaseCallbackHandler): """Callback handler for streaming LLM responses.""" def __init__(self, websocket): self.websocket = websocket def on_llm_new_token(self, token: str, **kwargs: Any) -> None: resp = ChatResponse(message=token, type="stream", intermediate_steps="") loop = asyncio.get_event_loop() coroutine = self.websocket.send_json(resp.dict()) asyncio.run_coroutine_threadsafe(coroutine, loop)
[]
2024-01-10
mukundha/build-your-custom-gpt
manage_data.py
import os from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import AstraDB from astrapy.db import AstraDBCollection import chainlit as cl from chainlit.types import AskFileResponse from langchain.document_loaders import PyPDFLoader, TextLoader from langchain.docstore.document import Document ASTRA_DB_API_ENDPOINT = os.environ["ASTRA_DB_API_ENDPOINT"] ASTRA_DB_APPLICATION_TOKEN = os.environ["ASTRA_DB_APPLICATION_TOKEN"] embeddings = OpenAIEmbeddings() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) user_collection = AstraDBCollection( collection_name="user_documents", api_endpoint=ASTRA_DB_API_ENDPOINT, token=ASTRA_DB_APPLICATION_TOKEN, ) vstore = AstraDB( embedding=embeddings, collection_name="astra_vector_demo", api_endpoint=ASTRA_DB_API_ENDPOINT, token=ASTRA_DB_APPLICATION_TOKEN, ) welcome_message = """Welcome to the Build your own Custom GPT demo! To get started: 1. Upload a PDF or text file 2. Ask a question about the file """ def process_file(file: AskFileResponse): app_user = cl.user_session.get("user") import tempfile if file.type == "text/plain": Loader = TextLoader elif file.type == "application/pdf": Loader = PyPDFLoader with tempfile.NamedTemporaryFile(mode="wb", delete=False) as tempfile: if file.type == "text/plain": tempfile.write(file.content) elif file.type == "application/pdf": with open(tempfile.name, "wb") as f: f.write(file.content) loader = Loader(tempfile.name) docs = loader.load_and_split(text_splitter=text_splitter) for doc in docs: doc.metadata["source"] = f"{file.name}" doc.metadata["username"] = f"{app_user.username}" return docs def get_docsearch(file: AskFileResponse): docs = process_file(file) cl.user_session.set("docs", docs) user = cl.user_session.get("dbuser") vstore.add_documents(docs) user["files"].append(f"{file.name}") user_collection.update_one(filter={"username": f"{user['username']}"}, update={"$set": {"files": user['files']}}) return vstore def get_files_for_user(user): collection = AstraDBCollection( collection_name="user_documents", api_endpoint=ASTRA_DB_API_ENDPOINT, token=ASTRA_DB_APPLICATION_TOKEN, ) user = collection.find_one({"username": f"{user.username}"}) cl.user_session.set("dbuser", user["data"]["document"]) return user["data"]["document"] async def upload_new_file(): app_user = cl.user_session.get("user") files = await cl.AskFileMessage( content=welcome_message, accept=["text/plain", "application/pdf"], max_size_mb=20, timeout=180, disable_human_feedback=True, ).send() file = files[0] msg = cl.Message( content=f"Processing `{file.name}`...", disable_human_feedback=True ) await msg.send() dbuser = cl.user_session.get('dbuser') if not dbuser: newuser = {"username": f"{app_user.username}", "files": [f"{file.name}"]} user_collection.insert_one(newuser) user=user_collection.find_one({"username": f"{app_user.username}"}) cl.user_session.set("dbuser", user["data"]["document"]) await cl.make_async(get_docsearch)(file) msg.content = f"Processing done. You can now ask questions!" await msg.update()
[]
2024-01-10
n-arch/PE_LLM
VeevaWorkflow~models~peEntry.py
from langchain.chat_models import ChatOpenAI from langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.chains import LLMChain from langchain.memory import ConversationBufferMemory from langchain.memory import ChatMessageHistory from langchain.schema import SystemMessage OPENAI_API_KEYY = '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' llm = ChatOpenAI(openai_api_key=OPENAI_API_KEYY, model='gpt-4') # Trigder feld ist multiple Choice muss in Justification begrüpndet werden #Effizient check ist immer erfoderlich und man muss nur nachrpäfen in wie weit man das nachprüfen kann #Vorgeschaltene prüfng ob GMP relevantmit annex 1 ist # Immer aktuelle TExtfleder mit den PE drinnen # Bearbeitungsfelder für die einezenne unterpunkte # spezifizieren von MAterialien und Ort #Einzelne Felder nehemn und dort die abfragen machen # Titel in der form von SOP vorgabe haben # Vorgefertigten text benutzen zur demo # Selbstvorschläge dann benutzen prompt_title = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the title of the Planned, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. **Titel**: What is the exact title of the event? If not provided or unclear, ask: "Can you specify the title of the event, please add the following missing information?" Example Input: "Upgrade of a Pressure Gauge in the Clean Room 402, Building 2, 1st Floor on 01.01.2021 in the Drug Substance Deparmanet in Zürich" Output: "[ZE]_[DS]_[Bld2]_[1Floor]_[402Room]_[01.01.2021]_[Pressure Gauge Upgrade]" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_title = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_title = LLMChain( llm=llm, prompt=prompt_title, verbose=True, memory=memory_title ) prompt_state_before = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. - **State Before**: What is the current situation or condition before the event? If vague, ask: "Can you provide more details about the current state before the event?" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_state_before = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_state_before = LLMChain( llm=llm, prompt=prompt_state_before, verbose=True, memory=memory_state_before ) prompt_state_after = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. - **State After**: What do you anticipate will be the situation or condition after the event? If unclear, ask: "Can you elaborate on the expected state after the event?" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_state_after = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_state_after = LLMChain( llm=llm, prompt=prompt_state_after, verbose=True, memory=memory_state_after ) prompt_justification = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. - **Justification**: Why is this event being conducted? If reasons are not detailed, ask: "Can you provide more specific reasons or benefits for this event?" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_justification = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_justification = LLMChain( llm=llm, prompt=prompt_justification, verbose=True, memory=memory_justification ) prompt_trigger = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. - **Trigger**: What exactly prompts this event? If not specified, ask: "What specific factor or situation initiates this event?" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_trigger = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_trigger = LLMChain( llm=llm, prompt=prompt_trigger, verbose=True, memory=memory_trigger ) prompt_rationale = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(""" Context: You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise. - **Rationale**: Is an efficiency check required for this event? Please provide a clear rationale. If the response is general, ask: "Can you elaborate on whether an efficiency check is needed and the reasons behind it?" """ ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{entry}") ] ) memory_rationale = ConversationBufferMemory(memory_key="chat_history", return_messages=True) conversation_rationale = LLMChain( llm=llm, prompt=prompt_rationale, verbose=True, memory=memory_rationale ) def chat_title(user_message): return conversation_title.predict(entry = user_message) def chat_state_before(user_message): return conversation_state_before.predict(entry = user_message) def chat_state_after(user_message): return conversation_state_after.predict(entry = user_message) def chat_justification(user_message): return conversation_justification.predict(entry = user_message) def chat_trigger(user_message): return conversation_trigger.predict(entry = user_message) def chat_rationale(user_message): return conversation_rationale.predict(entry = user_message)
[ "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n - **Trigger**: What exactly prompts this event? If not specified, ask: \"What specific factor or situation initiates this event?\"\n ", "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n - **State Before**: What is the current situation or condition before the event? If vague, ask: \"Can you provide more details about the current state before the event?\"\n \n ", "chat_history", "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the title of the Planned, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n \n **Titel**: What is the exact title of the event? If not provided or unclear, ask: \"Can you specify the title of the event, please add the following missing information?\"\n\n Example \n Input: \"Upgrade of a Pressure Gauge in the Clean Room 402, Building 2, 1st Floor on 01.01.2021 in the Drug Substance Deparmanet in Zürich\"\n Output: \"[ZE]_[DS]_[Bld2]_[1Floor]_[402Room]_[01.01.2021]_[Pressure Gauge Upgrade]\"\n ", "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n - **State After**: What do you anticipate will be the situation or condition after the event? If unclear, ask: \"Can you elaborate on the expected state after the event?\"\n ", "{entry}", "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n - **Justification**: Why is this event being conducted? If reasons are not detailed, ask: \"Can you provide more specific reasons or benefits for this event?\"\n ", "\n Context:\n You are a dedicated Quality Assurance assistant specializing in planned events within change control systems. Your primary role is to assist users in filling out the following fields, ensuring that every detail is captured accurately. If any provided information seems incomplete or lacks specificity, proactively ask for more detailed explanations to ensure the event's documentation is thorough and precise.\n\n - **Rationale**: Is an efficiency check required for this event? Please provide a clear rationale. If the response is general, ask: \"Can you elaborate on whether an efficiency check is needed and the reasons behind it?\"\n " ]
2024-01-10
n-arch/PE_LLM
VeevaWorkflow~models~guidelineAnswers.py
from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain import OpenAI from langchain.chains import RetrievalQA from langchain.document_loaders import DirectoryLoader import magic import os import nltk openai_api_key = os.getenv("OPENAI_API_KEY", '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') loader = DirectoryLoader('/Users/niklaskohl/Documents/GitHub/VeevaWorkflowLLM/guidelines', glob='**/*.txt') documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) docsearch = FAISS.from_documents(texts, embeddings) llm = OpenAI(openai_api_key=openai_api_key,model='gpt-4') #Gives back an answer about the guidlines def ask_guideline(query): qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=docsearch.as_retriever(), return_source_documents=True) result = qa({"query": query}) return result['result']#,result['source_documents']
[]
2024-01-10
n-arch/PE_LLM
VeevaWorkflow~models~upeEntry.py
from langchain.schema import HumanMessage from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate # We will be using a chat model, defaults to gpt-3.5-turbo from langchain.chat_models import ChatOpenAI # To parse outputs and get structured data back from langchain.output_parsers import StructuredOutputParser, ResponseSchema openai_api_key='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' chat_model = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo', openai_api_key=openai_api_key) def title_prompt(text): # The schema I want out response_schemas = [ ResponseSchema(name="unit", description="The name of a pharmaceutical production unit"), ResponseSchema(name="productAbb", description="The abrevation of a pharmaceutical product"), ResponseSchema(name="object", description="The name of an object"), ResponseSchema(name="defect", description="The concise description of the defect") ] # The parser that will look for the LLM output in my schema and return it back to me output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = output_parser.get_format_instructions() prompt = ChatPromptTemplate( messages=[ HumanMessagePromptTemplate.from_template("Given a command from the user, extract the production unit and product abbriviation, the defective object and the defect \n \ {format_instructions}\n{user_prompt}") ], input_variables=["user_prompt"], partial_variables={"format_instructions": format_instructions} ) titel_query = prompt.format_prompt(user_prompt=text) prompt.format_prompt(user_prompt=user_prompt) titel_query = chat_model(titel_query.to_messages()) output = output_parser.parse(titel_query.content) return output # #print (titel_query.messages[0].content) #print (output) #print (type(output)) #####################################Justification############################################## # The schema I want out def justification_prompt(justification_prompt): response_schemas_justifcation = [ ResponseSchema(name="Entdeckung", description="How the defect was discoverd"), ResponseSchema(name="Situation", description="What is the Qualitiy Deveation that occured"), ResponseSchema(name="Zeit", description="The date and time the probelm occured"), ResponseSchema(name="Prozess", description="The process in which the defect occured"), ResponseSchema(name="Material", description="How much material was affected from the defect"), ResponseSchema(name="GMP", description="Why it is a Good Manufacturing Practices deviation"), ResponseSchema(name="AsIs", description="What the actual state is"), ResponseSchema(name="Target", description="What the target state is"), ] # The parser that will look for the LLM output in my schema and return it back to me output_parser_justification = StructuredOutputParser.from_response_schemas(response_schemas_justifcation) format_instructions_justification = output_parser_justification.get_format_instructions() prompt_justification = ChatPromptTemplate( messages=[ HumanMessagePromptTemplate.from_template("Given a command from the user, \ extract the information and rewrite it to be clear, concise, of high quality while retaining all the information and in german.\ If the text doesn't contain the information write that the infoamtion is not available\ \n{format_instructions_justification}\n{user_prompt_justification}") ], input_variables=["user_prompt_justification"], partial_variables={"format_instructions_justification": format_instructions_justification} ) justification = prompt_justification.format_prompt(user_prompt_justification=justification_prompt) justification = chat_model(justification.to_messages()) #output = output_parser1.parse(justification.content) justification_result_dict = output_parser_justification.parse(justification.content) outputstring = "" for key, value in justification_result_dict.items(): outputstring += key + ": " + value + "\n" return outputstring
[ "user_prompt", "Given a command from the user, extract the production unit and product abbriviation, the defective object and the defect \n {format_instructions}\n{user_prompt}", "format_instructions_justification", "user_prompt_justification", "format_instructions", "Given a command from the user, extract the information and rewrite it to be clear, concise, of high quality while retaining all the information and in german. If the text doesn't contain the information write that the infoamtion is not available \n{format_instructions_justification}\n{user_prompt_justification}" ]
2024-01-10
tiro2000/self-rag
retrieval_lm~run_short_form.py
#!/usr/bin/python # -*- coding: UTF-8 -*- import spacy import jsonlines from transformers import AutoTokenizer, AutoModelForCausalLM from vllm import LLM, SamplingParams import random import torch import os import numpy as np import openai from tqdm import tqdm import json import argparse import ast import re from tqdm import tqdm from collections import Counter import string import sys import time from utils import PROMPT_DICT, TASK_INST, load_jsonlines, control_tokens, load_special_tokens from metrics import match, accuracy seed = 633 torch.backends.cudnn.deterministic = True random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) def postprocess_answer_option_conditioned(answer): for token in control_tokens: answer = answer.replace(token, "") if "</s>" in answer: answer = answer.replace("</s>", "") if "\n" in answer: answer = answer.replace("\n", "") if "<|endoftext|>" in answer: answer = answer.replace("<|endoftext|>", "") return answer def call_model_rerank_w_scores_batch(prompt, evidences, model, max_new_tokens=15, ret_tokens=None, rel_tokens=None, grd_tokens=None, ut_tokens=None, use_seqscore=False, threshold=0.5, beam_width=2, max_depth=1, w_rel=1.0, w_sup=1.0, w_use=0.5, mode="adaptive_retrieval", closed=False): # max_inpt_tokens = tokenizer.model_max_length if model_max_length is None else model_max_length results = {} if mode != "always_retrieve": sampling_params = SamplingParams( temperature=0.0, top_p=1.0, max_tokens=max_new_tokens, logprobs=32016) preds = model.generate([prompt], sampling_params) pred_token_ids = preds[0].outputs[0].token_ids pred_text = preds[0].outputs[0].text pred_log_probs = preds[0].outputs[0].logprobs results["no_retrieval"] = pred_text # save relevance token scores if mode == "always_retrieve": do_retrieve = True elif mode == "no_retrieval": do_retrieve = False else: if threshold is not None: score_dict = {} for tok, id in ret_tokens.items(): if id not in pred_log_probs[0]: score_dict[tok] = -100 prob = pred_log_probs[0][id] score_dict[tok] = float(prob) do_retrieve = score_dict["[Retrieval]"] / ( score_dict["[Retrieval]"] + score_dict["[No Retrieval]"]) > threshold else: do_retrieve = "[Retrieval]" in pred if do_retrieve is True: evidence_augmented_inputs = [prompt + "[Retrieval]<paragraph>{0}\n{1}</paragraph>".format( para["title"], para["text"]) for para in evidences] sampling_params = SamplingParams( temperature=0.0, top_p=1.0, max_tokens=max_new_tokens, logprobs=5000) preds = model.generate(evidence_augmented_inputs, sampling_params) relevance_score_dict = {} grd_score_dict = {} ut_score_dict = {} overall_scores = {} for p_idx, pred in enumerate(preds): pred_token_ids = pred.outputs[0].token_ids pred_text = pred.outputs[0].text pred_log_probs = pred.outputs[0].logprobs seq_score = pred.outputs[0].cumulative_logprob / \ max(len(pred.outputs[0].token_ids), 1) relevance_score_dict.setdefault(p_idx, {}) grd_score_dict.setdefault(p_idx, {}) ut_score_dict.setdefault(p_idx, {}) # Compute reward scores for tok, id in rel_tokens.items(): prob = pred_log_probs[0][id] if id in pred_log_probs[0] else -100 relevance_score_dict[p_idx][tok] = np.exp(float(prob)) if grd_tokens is not None: groundness_token_appear_indices = [] for tok_idx, tok in enumerate(pred_token_ids): if tok in list(grd_tokens.values()): groundness_token_appear_indices.append(tok_idx) break if len(groundness_token_appear_indices) > 0: idx = groundness_token_appear_indices[0] for token, token_id in grd_tokens.items(): prob = pred_log_probs[idx][token_id] if token_id in pred_log_probs[idx] else -100 grd_score_dict[p_idx][token] = np.exp(float(prob)) if ut_tokens is not None: utility_token_appear_indices = [] for tok_idx, tok in enumerate(pred_token_ids): if tok in list(ut_tokens.values()): utility_token_appear_indices.append(tok_idx) if len(utility_token_appear_indices) > 0: idx = utility_token_appear_indices[0] for token, token_id in ut_tokens.items(): prob = pred_log_probs[idx][token_id] if token_id in pred_log_probs[idx] else -100 ut_score_dict[p_idx][token] = np.exp(float(prob)) relevance_score = relevance_score_dict[p_idx]["[Relevant]"] / ( np.sum(list(relevance_score_dict[p_idx].values()))) if len(grd_score_dict[p_idx]) == 3: gt_sum = np.sum(list(grd_score_dict[p_idx].values())) ground_score = (grd_score_dict[p_idx]["[Fully supported]"] / gt_sum) + 0.5 * ( grd_score_dict[p_idx]["[Partially supported]"] / gt_sum) else: ground_score = 0.0 if len(ut_score_dict[p_idx]) == 5: ut_sum = np.sum(list(ut_score_dict[p_idx].values())) ut_scores = [-1, -0.5, 0, 0.5, 1] utility_score = np.sum( [ut_scores[i] * (ut_score_dict[p_idx]["[Utility:{}]".format(i+1)] / ut_sum) for i in range(len(ut_scores))]) else: utility_score = 0.0 if use_seqscore is True: final_score = np.exp(seq_score) + w_rel * relevance_score + \ w_sup * ground_score + w_use * utility_score else: final_score = w_rel * relevance_score + \ w_sup * ground_score + w_use * utility_score overall_scores[p_idx] = {"final_score": final_score, "relevance_score": relevance_score, "ground_score": ground_score, "utility_score": utility_score, "relevance_score_dict": relevance_score_dict, "grd_score_dict": grd_score_dict, "ut_score_dict": utility_score} results["retrieval_{}".format(p_idx)] = { "pred": pred_text, "score": final_score, "ctx": evidences[p_idx]} else: sampling_params = SamplingParams( temperature=0.0, top_p=1.0, max_tokens=max_new_tokens) prompt += "[No Retrieval]" preds = model.generate([prompt], sampling_params) pred = preds[0].outputs[0].text # Aggregating answers if len(results) == 1: postprocessed_pred = postprocess_answer_option_conditioned(pred) return postprocessed_pred, results, do_retrieve else: answer2score = {} if closed is True: for key, result in results.items(): if key == "no_retrieval": continue answer = postprocess_answer_option_conditioned(result["pred"]) score = result["score"] answer2score.setdefault(answer, 0) answer2score[answer] += score sorted_answers = sorted( answer2score.items(), key=lambda x: x[1], reverse=True) best_option = sorted_answers[0][0] else: path2score = {key: item["score"] for key, item in results.items() if key != "no_retrieval"} best_path = sorted(path2score.items(), key=lambda x: x[1], reverse=True)[0][0] best_option = results[best_path]["pred"] return best_option, results, do_retrieve def process_data_evidences(demonstration, top_n): ctx_key = "ctxs" if "ctxs" in demonstration else "top_contexts" prompt = PROMPT_DICT["prompt_no_input"].format_map(demonstration) evidences = demonstration[ctx_key][:top_n] return prompt, evidences def preprocess_input_data(dataset, task=None): new_data = [] if task in TASK_INST: instruction = TASK_INST[task] else: instruction = None for item in dataset: if task == "arc_c": choices = item["choices"] answer_labels = {} for i in range(len(choices["label"])): answer_key = choices["label"][i] text = choices["text"][i] if answer_key == "1": answer_labels["A"] = text if answer_key == "2": answer_labels["B"] = text if answer_key == "3": answer_labels["C"] = text if answer_key == "4": answer_labels["D"] = text if answer_key in ["A", "B", "C", "D"]: answer_labels[answer_key] = text if "D" not in answer_labels: answer_labels["D"] = "" choices = "\nA: {0}\nB: {1}\nC: {2}\nD: {3}".format( answer_labels["A"], answer_labels["B"], answer_labels["C"], answer_labels["D"]) if "E" in answer_labels: choices += "\nE: {}".format(answer_labels["E"]) item["instruction"] = instruction + \ "\n\n### Input:\n" + item["question"] + choices item["answers"] = [item["answerKey"]] else: prompt = instruction + "\n\n## Input:\n\n" + \ item["question"] if instruction is not None else item["question"] item["instruction"] = prompt new_data.append(item) return new_data def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_name', type=str) parser.add_argument('--input_file', type=str) parser.add_argument('--output_file', type=str) parser.add_argument('--task', type=str) parser.add_argument('--device', type=str, default="cuda") parser.add_argument('--max_new_tokens', type=int, default=15) parser.add_argument('--tokenizer_path', type=str) parser.add_argument('--download_dir', type=str, help="specify vllm model download dir", default=".cache") parser.add_argument("--ndocs", type=int, default=10, help="Number of documents to retrieve per questions") parser.add_argument("--world_size", type=int, default=1, help="world size to use multiple GPUs.") parser.add_argument("--dtype", type=str, default="half", help="We use bfloat16 for training. If you run inference on GPUs that do not support BF16, please set this to be `half`.") # Decoding hyperparams parser.add_argument('--threshold', type=float, default=None, help="Adaptive threshold.") parser.add_argument("--use_seqscore", action="store_true") parser.add_argument("--use_groundness", action="store_true", help="use ground score") parser.add_argument( "--use_utility", action="store_true", help="tree search") parser.add_argument("--beam_width", type=int, default=2, help="beam search width") parser.add_argument("--max_depth", type=int, default=2, help="tree depth width") parser.add_argument("--w_rel", type=float, default=1.0, help="reward weight for document relevance") parser.add_argument("--w_sup", type=float, default=1.0, help="reward weight for generation support (attribution)") parser.add_argument("--w_use", type=float, default=1.0, help="reward weight for overall completeness / utility.") parser.add_argument("--ignore_cont", action="store_true", help="filter out sentences that include [No support / Contradictory] ") parser.add_argument('--mode', type=str, help="mode to control retrieval.", default="default", choices=['adaptive_retrieval', 'no_retrieval', 'always_retrieve'],) parser.add_argument('--metric', type=str, help="metric to be used during evaluation") args = parser.parse_args() gpt = args.model_name input_path = args.input_file if input_path.endswith(".json"): input_data = json.load(open(input_path)) else: input_data = load_jsonlines(input_path) input_data = preprocess_input_data( input_data, task=args.task) tokenizer = AutoTokenizer.from_pretrained(gpt, padding_side="left") if args.dtype is not None: model = LLM(model=gpt, download_dir=args.download_dir, dtype=args.dtype, tensor_parallel_size=args.world_size,) else: model = LLM(model=gpt, download_dir=args.download_dir, dtype=args.dtype, tensor_parallel_size=args.world_size,) # Get token ids for reflection tokens. ret_tokens, rel_tokens, grd_tokens, ut_tokens = load_special_tokens( tokenizer, use_grounding=args.use_groundness, use_utility=args.use_utility) def generate(prompt, evidences, max_new_tokens): return call_model_rerank_w_scores_batch(prompt, evidences=evidences, model=model, max_new_tokens=max_new_tokens, rel_tokens=rel_tokens, ret_tokens=ret_tokens, grd_tokens=grd_tokens, ut_tokens=ut_tokens, threshold=args.threshold, beam_width=args.beam_width, max_depth=args.max_depth, use_seqscore=args.use_seqscore, w_rel=1.0, w_sup=1.0, w_use=0.5, mode=args.mode, closed=args.task in ["fever", "arc_c"]) preds = [] prompts = [] golds = [] metric_results = [] scores = [] all_results = [] count = 0 for i, row in tqdm(enumerate(input_data)): results = {} prompt = PROMPT_DICT["prompt_no_input"].format_map(row) _, evidences = process_data_evidences(row, top_n=args.ndocs) pred, results, do_retrieve = generate( prompt, evidences, max_new_tokens=args.max_new_tokens,) if type(pred) is str and pred[0] == "#" or pred[0] == ":": pred = pred[1:] prompts.append(prompt) preds.append(pred) all_results.append(results) if do_retrieve is True: count += 1 # golds.append(row["output"]) if "answers" not in row and "answer" in row: row["answers"] = [row["answer"]] if type( row["answer"]) is str else row["answer"] if args.metric == "accuracy": metric_result = accuracy(pred, row["output"]) elif args.metric == "match": if "SUPPORTS" in pred: pred = "true" elif "REFUTES" in pred: pred = "false" metric_result = match(pred, row["answers"]) else: raise NotImplementedError metric_results.append(metric_result) if i % 10 == 0: print("average: {}".format(np.mean(metric_results))) final_results = {"preds": preds, "prompts": prompts, "metric_results": metric_results, "all_results": all_results, "golds": golds, "metric": args.metric, "metric_mean": np.mean(metric_results), "scores": scores} with open(args.output_file + "_tmp", "w") as outfile: json.dump(final_results, outfile) final_results = {"preds": preds, "prompts": prompts, "metric_results": metric_results, "all_results": all_results, "golds": golds, "metric": args.metric, "metric_mean": np.mean(metric_results), "scores": scores} with open(args.output_file, "w") as outfile: json.dump(final_results, outfile) print("Final result: {0}".format(np.mean(metric_results))) print("Retrieval Frequencies: {0}".format(count / len(final_results))) if __name__ == "__main__": main()
[ "prompt_no_input", "[]", "PLACEHOLDER\n\n## Input:\n\nPLACEHOLDER", "[No Retrieval]" ]
2024-01-10
TeamKillerX/RyuzakiLib
RyuzakiLib~_openai_scripts.py
#!/usr/bin/env python import argparse import logging import sys import openai from openai import version from openai.cli import api_register, display_error, tools_register, wandb_register logger = logging.getLogger() formatter = logging.Formatter("[%(asctime)s] %(message)s") handler = logging.StreamHandler(sys.stderr) handler.setFormatter(formatter) logger.addHandler(handler) def main(): parser = argparse.ArgumentParser(description=None) parser.add_argument( "-V", "--version", action="version", version="%(prog)s " + version.VERSION, ) parser.add_argument( "-v", "--verbose", action="count", dest="verbosity", default=0, help="Set verbosity.", ) parser.add_argument("-b", "--api-base", help="What API base url to use.") parser.add_argument("-k", "--api-key", help="What API key to use.") parser.add_argument("-p", "--proxy", nargs="+", help="What proxy to use.") parser.add_argument( "-o", "--organization", help="Which organization to run as (will use your default organization if not specified)", ) def help(args): parser.print_help() parser.set_defaults(func=help) subparsers = parser.add_subparsers() sub_api = subparsers.add_parser("api", help="Direct API calls") sub_tools = subparsers.add_parser("tools", help="Client side tools for convenience") sub_wandb = subparsers.add_parser("wandb", help="Logging with Weights & Biases") api_register(sub_api) tools_register(sub_tools) wandb_register(sub_wandb) args = parser.parse_args() if args.verbosity == 1: logger.setLevel(logging.INFO) elif args.verbosity >= 2: logger.setLevel(logging.DEBUG) openai.debug = True if args.api_key is not None: openai.api_key = args.api_key if args.api_base is not None: openai.api_base = args.api_base if args.organization is not None: openai.organization = args.organization if args.proxy is not None: openai.proxy = {} for proxy in args.proxy: if proxy.startswith("https"): openai.proxy["https"] = proxy elif proxy.startswith("http"): openai.proxy["http"] = proxy try: args.func(args) except openai.error.OpenAIError as e: display_error(e) return 1 except KeyboardInterrupt: sys.stderr.write("\n") return 1 return 0 if __name__ == "__main__": sys.exit(main())
[]
2024-01-10
TeamKillerX/RyuzakiLib
RyuzakiLib~api_requestor.py
import asyncio import json import platform import sys import threading import time import warnings from contextlib import asynccontextmanager from json import JSONDecodeError from typing import ( AsyncGenerator, AsyncIterator, Callable, Dict, Iterator, Optional, Tuple, Union, overload, ) from urllib.parse import urlencode, urlsplit, urlunsplit import aiohttp import requests if sys.version_info >= (3, 8): from typing import Literal else: from typing_extensions import Literal import openai from openai import error, util, version from openai.openai_response import OpenAIResponse from openai.util import ApiType TIMEOUT_SECS = 600 MAX_SESSION_LIFETIME_SECS = 180 MAX_CONNECTION_RETRIES = 2 # Has one attribute per thread, 'session'. _thread_context = threading.local() def _build_api_url(url, query): scheme, netloc, path, base_query, fragment = urlsplit(url) if base_query: query = "%s&%s" % (base_query, query) return urlunsplit((scheme, netloc, path, query, fragment)) def _requests_proxies_arg(proxy) -> Optional[Dict[str, str]]: """Returns a value suitable for the 'proxies' argument to 'requests.request.""" if proxy is None: return None elif isinstance(proxy, str): return {"http": proxy, "https": proxy} elif isinstance(proxy, dict): return proxy.copy() else: raise ValueError( "'openai.proxy' must be specified as either a string URL or a dict with string URL under the https and/or http keys." ) def _aiohttp_proxies_arg(proxy) -> Optional[str]: """Returns a value suitable for the 'proxies' argument to 'aiohttp.ClientSession.request.""" if proxy is None: return None elif isinstance(proxy, str): return proxy elif isinstance(proxy, dict): return proxy["https"] if "https" in proxy else proxy["http"] else: raise ValueError( "'openai.proxy' must be specified as either a string URL or a dict with string URL under the https and/or http keys." ) def _make_session() -> requests.Session: if openai.requestssession: if isinstance(openai.requestssession, requests.Session): return openai.requestssession return openai.requestssession() if not openai.verify_ssl_certs: warnings.warn("verify_ssl_certs is ignored; openai always verifies.") s = requests.Session() proxies = _requests_proxies_arg(openai.proxy) if proxies: s.proxies = proxies s.mount( "https://", requests.adapters.HTTPAdapter(max_retries=MAX_CONNECTION_RETRIES), ) return s def parse_stream_helper(line: bytes) -> Optional[str]: if line: if line.strip() == b"data: [DONE]": # return here will cause GeneratorExit exception in urllib3 # and it will close http connection with TCP Reset return None if line.startswith(b"data: "): line = line[len(b"data: ") :] return line.decode("utf-8") else: return None return None def parse_stream(rbody: Iterator[bytes]) -> Iterator[str]: for line in rbody: _line = parse_stream_helper(line) if _line is not None: yield _line async def parse_stream_async(rbody: aiohttp.StreamReader): async for line in rbody: _line = parse_stream_helper(line) if _line is not None: yield _line class APIRequestor: def __init__( self, key=None, api_base=None, api_type=None, api_version=None, organization=None, ): self.api_base = api_base or openai.api_base self.api_key = key or util.default_api_key() self.api_type = ( ApiType.from_str(api_type) if api_type else ApiType.from_str(openai.api_type) ) self.api_version = api_version or openai.api_version self.organization = organization or openai.organization @classmethod def format_app_info(cls, info): str = info["name"] if info["version"]: str += "/%s" % (info["version"],) if info["url"]: str += " (%s)" % (info["url"],) return str def _check_polling_response( self, response: OpenAIResponse, predicate: Callable[[OpenAIResponse], bool] ): if not predicate(response): return error_data = response.data["error"] message = error_data.get("message", "Operation failed") code = error_data.get("code") raise error.OpenAIError(message=message, code=code) def _poll( self, method, url, until, failed, params=None, headers=None, interval=None, delay=None ) -> Tuple[Iterator[OpenAIResponse], bool, str]: if delay: time.sleep(delay) response, b, api_key = self.request(method, url, params, headers) self._check_polling_response(response, failed) start_time = time.time() while not until(response): if time.time() - start_time > TIMEOUT_SECS: raise error.Timeout("Operation polling timed out.") time.sleep(interval or response.retry_after or 10) response, b, api_key = self.request(method, url, params, headers) self._check_polling_response(response, failed) response.data = response.data["result"] return response, b, api_key async def _apoll( self, method, url, until, failed, params=None, headers=None, interval=None, delay=None ) -> Tuple[Iterator[OpenAIResponse], bool, str]: if delay: await asyncio.sleep(delay) response, b, api_key = await self.arequest(method, url, params, headers) self._check_polling_response(response, failed) start_time = time.time() while not until(response): if time.time() - start_time > TIMEOUT_SECS: raise error.Timeout("Operation polling timed out.") await asyncio.sleep(interval or response.retry_after or 10) response, b, api_key = await self.arequest(method, url, params, headers) self._check_polling_response(response, failed) response.data = response.data["result"] return response, b, api_key @overload def request( self, method, url, params, headers, files, stream: Literal[True], request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[Iterator[OpenAIResponse], bool, str]: pass @overload def request( self, method, url, params=..., headers=..., files=..., *, stream: Literal[True], request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[Iterator[OpenAIResponse], bool, str]: pass @overload def request( self, method, url, params=..., headers=..., files=..., stream: Literal[False] = ..., request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[OpenAIResponse, bool, str]: pass @overload def request( self, method, url, params=..., headers=..., files=..., stream: bool = ..., request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool, str]: pass def request( self, method, url, params=None, headers=None, files=None, stream: bool = False, request_id: Optional[str] = None, request_timeout: Optional[Union[float, Tuple[float, float]]] = None, ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool, str]: result = self.request_raw( method.lower(), url, params=params, supplied_headers=headers, files=files, stream=stream, request_id=request_id, request_timeout=request_timeout, ) resp, got_stream = self._interpret_response(result, stream) return resp, got_stream, self.api_key @overload async def arequest( self, method, url, params, headers, files, stream: Literal[True], request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[AsyncGenerator[OpenAIResponse, None], bool, str]: pass @overload async def arequest( self, method, url, params=..., headers=..., files=..., *, stream: Literal[True], request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[AsyncGenerator[OpenAIResponse, None], bool, str]: pass @overload async def arequest( self, method, url, params=..., headers=..., files=..., stream: Literal[False] = ..., request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[OpenAIResponse, bool, str]: pass @overload async def arequest( self, method, url, params=..., headers=..., files=..., stream: bool = ..., request_id: Optional[str] = ..., request_timeout: Optional[Union[float, Tuple[float, float]]] = ..., ) -> Tuple[Union[OpenAIResponse, AsyncGenerator[OpenAIResponse, None]], bool, str]: pass async def arequest( self, method, url, params=None, headers=None, files=None, stream: bool = False, request_id: Optional[str] = None, request_timeout: Optional[Union[float, Tuple[float, float]]] = None, ) -> Tuple[Union[OpenAIResponse, AsyncGenerator[OpenAIResponse, None]], bool, str]: ctx = aiohttp_session() session = await ctx.__aenter__() try: result = await self.arequest_raw( method.lower(), url, session, params=params, supplied_headers=headers, files=files, request_id=request_id, request_timeout=request_timeout, ) resp, got_stream = await self._interpret_async_response(result, stream) except Exception: await ctx.__aexit__(None, None, None) raise if got_stream: async def wrap_resp(): assert isinstance(resp, AsyncGenerator) try: async for r in resp: yield r finally: await ctx.__aexit__(None, None, None) return wrap_resp(), got_stream, self.api_key else: await ctx.__aexit__(None, None, None) return resp, got_stream, self.api_key def handle_error_response(self, rbody, rcode, resp, rheaders, stream_error=False): try: error_data = resp["error"] except (KeyError, TypeError): raise error.APIError( "Invalid response object from API: %r (HTTP response code " "was %d)" % (rbody, rcode), rbody, rcode, resp, ) if "internal_message" in error_data: error_data["message"] += "\n\n" + error_data["internal_message"] util.log_info( "OpenAI API error received", error_code=error_data.get("code"), error_type=error_data.get("type"), error_message=error_data.get("message"), error_param=error_data.get("param"), stream_error=stream_error, ) # Rate limits were previously coded as 400's with code 'rate_limit' if rcode == 429: return error.RateLimitError(error_data.get("message"), rbody, rcode, resp, rheaders) elif rcode in [400, 404, 415]: return error.InvalidRequestError( error_data.get("message"), error_data.get("param"), error_data.get("code"), rbody, rcode, resp, rheaders, ) elif rcode == 401: return error.AuthenticationError( error_data.get("message"), rbody, rcode, resp, rheaders ) elif rcode == 403: return error.PermissionError(error_data.get("message"), rbody, rcode, resp, rheaders) elif rcode == 409: return error.TryAgain(error_data.get("message"), rbody, rcode, resp, rheaders) elif stream_error: # TODO: we will soon attach status codes to stream errors parts = [error_data.get("message"), "(Error occurred while streaming.)"] message = " ".join([p for p in parts if p is not None]) return error.APIError(message, rbody, rcode, resp, rheaders) else: return error.APIError( f"{error_data.get('message')} {rbody} {rcode} {resp} {rheaders}", rbody, rcode, resp, rheaders, ) def request_headers(self, method: str, extra, request_id: Optional[str]) -> Dict[str, str]: user_agent = "OpenAI/v1 PythonBindings/%s" % (version.VERSION,) if openai.app_info: user_agent += " " + self.format_app_info(openai.app_info) uname_without_node = " ".join( v for k, v in platform.uname()._asdict().items() if k != "node" ) ua = { "bindings_version": version.VERSION, "httplib": "requests", "lang": "python", "lang_version": platform.python_version(), "platform": platform.platform(), "publisher": "openai", "uname": uname_without_node, } if openai.app_info: ua["application"] = openai.app_info headers = { "X-OpenAI-Client-User-Agent": json.dumps(ua), "User-Agent": user_agent, } headers.update(util.api_key_to_header(self.api_type, self.api_key)) if self.organization: headers["OpenAI-Organization"] = self.organization if self.api_version is not None and self.api_type == ApiType.OPEN_AI: headers["OpenAI-Version"] = self.api_version if request_id is not None: headers["X-Request-Id"] = request_id if openai.debug: headers["OpenAI-Debug"] = "true" headers.update(extra) return headers def _validate_headers(self, supplied_headers: Optional[Dict[str, str]]) -> Dict[str, str]: headers: Dict[str, str] = {} if supplied_headers is None: return headers if not isinstance(supplied_headers, dict): raise TypeError("Headers must be a dictionary") for k, v in supplied_headers.items(): if not isinstance(k, str): raise TypeError("Header keys must be strings") if not isinstance(v, str): raise TypeError("Header values must be strings") headers[k] = v # NOTE: It is possible to do more validation of the headers, but a request could always # be made to the API manually with invalid headers, so we need to handle them server side. return headers def _prepare_request_raw( self, url, supplied_headers, method, params, files, request_id: Optional[str], ) -> Tuple[str, Dict[str, str], Optional[bytes]]: abs_url = "%s%s" % (self.api_base, url) headers = self._validate_headers(supplied_headers) data = None if method == "get" or method == "delete": if params: encoded_params = urlencode([(k, v) for k, v in params.items() if v is not None]) abs_url = _build_api_url(abs_url, encoded_params) elif method in {"post", "put"}: if params and files: data = params if params and not files: data = json.dumps(params).encode() headers["Content-Type"] = "application/json" else: raise error.APIConnectionError( "Unrecognized HTTP method %r. This may indicate a bug in the " "OpenAI bindings. Please contact us through our help center at help.openai.com for " "assistance." % (method,) ) headers = self.request_headers(method, headers, request_id) util.log_debug("Request to OpenAI API", method=method, path=abs_url) util.log_debug("Post details", data=data, api_version=self.api_version) return abs_url, headers, data def request_raw( self, method, url, *, params=None, supplied_headers: Optional[Dict[str, str]] = None, files=None, stream: bool = False, request_id: Optional[str] = None, request_timeout: Optional[Union[float, Tuple[float, float]]] = None, ) -> requests.Response: abs_url, headers, data = self._prepare_request_raw( url, supplied_headers, method, params, files, request_id ) if not hasattr(_thread_context, "session"): _thread_context.session = _make_session() _thread_context.session_create_time = time.time() elif ( time.time() - getattr(_thread_context, "session_create_time", 0) >= MAX_SESSION_LIFETIME_SECS ): _thread_context.session.close() _thread_context.session = _make_session() _thread_context.session_create_time = time.time() try: result = _thread_context.session.request( method, abs_url, headers=headers, data=data, files=files, stream=stream, timeout=request_timeout if request_timeout else TIMEOUT_SECS, proxies=_thread_context.session.proxies, ) except requests.exceptions.Timeout as e: raise error.Timeout("Request timed out: {}".format(e)) from e except requests.exceptions.RequestException as e: raise error.APIConnectionError("Error communicating with OpenAI: {}".format(e)) from e util.log_debug( "OpenAI API response", path=abs_url, response_code=result.status_code, processing_ms=result.headers.get("OpenAI-Processing-Ms"), request_id=result.headers.get("X-Request-Id"), ) # Don't read the whole stream for debug logging unless necessary. if openai.log == "debug": util.log_debug("API response body", body=result.content, headers=result.headers) return result async def arequest_raw( self, method, url, session, *, params=None, supplied_headers: Optional[Dict[str, str]] = None, files=None, request_id: Optional[str] = None, request_timeout: Optional[Union[float, Tuple[float, float]]] = None, ) -> aiohttp.ClientResponse: abs_url, headers, data = self._prepare_request_raw( url, supplied_headers, method, params, files, request_id ) if isinstance(request_timeout, tuple): timeout = aiohttp.ClientTimeout( connect=request_timeout[0], total=request_timeout[1], ) else: timeout = aiohttp.ClientTimeout( total=request_timeout if request_timeout else TIMEOUT_SECS ) if files: # TODO: Use `aiohttp.MultipartWriter` to create the multipart form data here. # For now we use the private `requests` method that is known to have worked so far. data, content_type = requests.models.RequestEncodingMixin._encode_files( # type: ignore files, data ) headers["Content-Type"] = content_type request_kwargs = { "method": method, "url": abs_url, "headers": headers, "data": data, "proxy": _aiohttp_proxies_arg(openai.proxy), "timeout": timeout, } try: result = await session.request(**request_kwargs) util.log_info( "OpenAI API response", path=abs_url, response_code=result.status, processing_ms=result.headers.get("OpenAI-Processing-Ms"), request_id=result.headers.get("X-Request-Id"), ) # Don't read the whole stream for debug logging unless necessary. if openai.log == "debug": util.log_debug("API response body", body=result.content, headers=result.headers) return result except (aiohttp.ServerTimeoutError, asyncio.TimeoutError) as e: raise error.Timeout("Request timed out") from e except aiohttp.ClientError as e: raise error.APIConnectionError("Error communicating with OpenAI") from e def _interpret_response( self, result: requests.Response, stream: bool ) -> Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], bool]: """Returns the response(s) and a bool indicating whether it is a stream.""" if stream and "text/event-stream" in result.headers.get("Content-Type", ""): return ( self._interpret_response_line( line, result.status_code, result.headers, stream=True ) for line in parse_stream(result.iter_lines()) ), True else: return ( self._interpret_response_line( result.content.decode("utf-8"), result.status_code, result.headers, stream=False, ), False, ) async def _interpret_async_response( self, result: aiohttp.ClientResponse, stream: bool ) -> Tuple[Union[OpenAIResponse, AsyncGenerator[OpenAIResponse, None]], bool]: """Returns the response(s) and a bool indicating whether it is a stream.""" if stream and "text/event-stream" in result.headers.get("Content-Type", ""): return ( self._interpret_response_line(line, result.status, result.headers, stream=True) async for line in parse_stream_async(result.content) ), True else: try: await result.read() except (aiohttp.ServerTimeoutError, asyncio.TimeoutError) as e: raise error.Timeout("Request timed out") from e except aiohttp.ClientError as e: util.log_warn(e, body=result.content) return ( self._interpret_response_line( (await result.read()).decode("utf-8"), result.status, result.headers, stream=False, ), False, ) def _interpret_response_line( self, rbody: str, rcode: int, rheaders, stream: bool ) -> OpenAIResponse: # HTTP 204 response code does not have any content in the body. if rcode == 204: return OpenAIResponse(None, rheaders) if rcode == 503: raise error.ServiceUnavailableError( "The server is overloaded or not ready yet.", rbody, rcode, headers=rheaders, ) try: if "text/plain" in rheaders.get("Content-Type", ""): data = rbody else: data = json.loads(rbody) except (JSONDecodeError, UnicodeDecodeError) as e: raise error.APIError( f"HTTP code {rcode} from API ({rbody})", rbody, rcode, headers=rheaders ) from e resp = OpenAIResponse(data, rheaders) # In the future, we might add a "status" parameter to errors # to better handle the "error while streaming" case. stream_error = stream and "error" in resp.data if stream_error or not 200 <= rcode < 300: raise self.handle_error_response( rbody, rcode, resp.data, rheaders, stream_error=stream_error ) return resp @asynccontextmanager async def aiohttp_session() -> AsyncIterator[aiohttp.ClientSession]: user_set_session = openai.aiosession.get() if user_set_session: yield user_set_session else: async with aiohttp.ClientSession() as session: yield session
[]
2024-01-10
TeamKillerX/RyuzakiLib
RyuzakiLib~validators.py
import os import sys from typing import Any, Callable, NamedTuple, Optional from openai.datalib.pandas_helper import assert_has_pandas from openai.datalib.pandas_helper import pandas as pd class Remediation(NamedTuple): name: str immediate_msg: Optional[str] = None necessary_msg: Optional[str] = None necessary_fn: Optional[Callable[[Any], Any]] = None optional_msg: Optional[str] = None optional_fn: Optional[Callable[[Any], Any]] = None error_msg: Optional[str] = None def num_examples_validator(df): """ This validator will only print out the number of examples and recommend to the user to increase the number of examples if less than 100. """ MIN_EXAMPLES = 100 optional_suggestion = ( "" if len(df) >= MIN_EXAMPLES else ". In general, we recommend having at least a few hundred examples. We've found that performance tends to linearly increase for every doubling of the number of examples" ) immediate_msg = ( f"\n- Your file contains {len(df)} prompt-completion pairs{optional_suggestion}" ) return Remediation(name="num_examples", immediate_msg=immediate_msg) def necessary_column_validator(df, necessary_column): """ This validator will ensure that the necessary column is present in the dataframe. """ def lower_case_column(df, column): cols = [c for c in df.columns if str(c).lower() == column] df.rename(columns={cols[0]: column.lower()}, inplace=True) return df immediate_msg = None necessary_fn = None necessary_msg = None error_msg = None if necessary_column not in df.columns: if necessary_column in [str(c).lower() for c in df.columns]: def lower_case_column_creator(df): return lower_case_column(df, necessary_column) necessary_fn = lower_case_column_creator immediate_msg = f"\n- The `{necessary_column}` column/key should be lowercase" necessary_msg = f"Lower case column name to `{necessary_column}`" else: error_msg = f"`{necessary_column}` column/key is missing. Please make sure you name your columns/keys appropriately, then retry" return Remediation( name="necessary_column", immediate_msg=immediate_msg, necessary_msg=necessary_msg, necessary_fn=necessary_fn, error_msg=error_msg, ) def additional_column_validator(df, fields=["prompt", "completion"]): """ This validator will remove additional columns from the dataframe. """ additional_columns = [] necessary_msg = None immediate_msg = None necessary_fn = None if len(df.columns) > 2: additional_columns = [c for c in df.columns if c not in fields] warn_message = "" for ac in additional_columns: dups = [c for c in additional_columns if ac in c] if len(dups) > 0: warn_message += f"\n WARNING: Some of the additional columns/keys contain `{ac}` in their name. These will be ignored, and the column/key `{ac}` will be used instead. This could also result from a duplicate column/key in the provided file." immediate_msg = f"\n- The input file should contain exactly two columns/keys per row. Additional columns/keys present are: {additional_columns}{warn_message}" necessary_msg = f"Remove additional columns/keys: {additional_columns}" def necessary_fn(x): return x[fields] return Remediation( name="additional_column", immediate_msg=immediate_msg, necessary_msg=necessary_msg, necessary_fn=necessary_fn, ) def non_empty_field_validator(df, field="completion"): """ This validator will ensure that no completion is empty. """ necessary_msg = None necessary_fn = None immediate_msg = None if df[field].apply(lambda x: x == "").any() or df[field].isnull().any(): empty_rows = (df[field] == "") | (df[field].isnull()) empty_indexes = df.reset_index().index[empty_rows].tolist() immediate_msg = f"\n- `{field}` column/key should not contain empty strings. These are rows: {empty_indexes}" def necessary_fn(x): return x[x[field] != ""].dropna(subset=[field]) necessary_msg = f"Remove {len(empty_indexes)} rows with empty {field}s" return Remediation( name=f"empty_{field}", immediate_msg=immediate_msg, necessary_msg=necessary_msg, necessary_fn=necessary_fn, ) def duplicated_rows_validator(df, fields=["prompt", "completion"]): """ This validator will suggest to the user to remove duplicate rows if they exist. """ duplicated_rows = df.duplicated(subset=fields) duplicated_indexes = df.reset_index().index[duplicated_rows].tolist() immediate_msg = None optional_msg = None optional_fn = None if len(duplicated_indexes) > 0: immediate_msg = f"\n- There are {len(duplicated_indexes)} duplicated {'-'.join(fields)} sets. These are rows: {duplicated_indexes}" optional_msg = f"Remove {len(duplicated_indexes)} duplicate rows" def optional_fn(x): return x.drop_duplicates(subset=fields) return Remediation( name="duplicated_rows", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, ) def long_examples_validator(df): """ This validator will suggest to the user to remove examples that are too long. """ immediate_msg = None optional_msg = None optional_fn = None ft_type = infer_task_type(df) if ft_type != "open-ended generation": def get_long_indexes(d): long_examples = d.apply(lambda x: len(x.prompt) + len(x.completion) > 10000, axis=1) return d.reset_index().index[long_examples].tolist() long_indexes = get_long_indexes(df) if len(long_indexes) > 0: immediate_msg = f"\n- There are {len(long_indexes)} examples that are very long. These are rows: {long_indexes}\nFor conditional generation, and for classification the examples shouldn't be longer than 2048 tokens." optional_msg = f"Remove {len(long_indexes)} long examples" def optional_fn(x): long_indexes_to_drop = get_long_indexes(x) if long_indexes != long_indexes_to_drop: sys.stdout.write( f"The indices of the long examples has changed as a result of a previously applied recommendation.\nThe {len(long_indexes_to_drop)} long examples to be dropped are now at the following indices: {long_indexes_to_drop}\n" ) return x.drop(long_indexes_to_drop) return Remediation( name="long_examples", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, ) def common_prompt_suffix_validator(df): """ This validator will suggest to add a common suffix to the prompt if one doesn't already exist in case of classification or conditional generation. """ error_msg = None immediate_msg = None optional_msg = None optional_fn = None # Find a suffix which is not contained within the prompt otherwise suggested_suffix = "\n\n### =>\n\n" suffix_options = [ " ->", "\n\n###\n\n", "\n\n===\n\n", "\n\n---\n\n", "\n\n===>\n\n", "\n\n--->\n\n", ] for suffix_option in suffix_options: if suffix_option == " ->": if df.prompt.str.contains("\n").any(): continue if df.prompt.str.contains(suffix_option, regex=False).any(): continue suggested_suffix = suffix_option break display_suggested_suffix = suggested_suffix.replace("\n", "\\n") ft_type = infer_task_type(df) if ft_type == "open-ended generation": return Remediation(name="common_suffix") def add_suffix(x, suffix): x["prompt"] += suffix return x common_suffix = get_common_xfix(df.prompt, xfix="suffix") if (df.prompt == common_suffix).all(): error_msg = f"All prompts are identical: `{common_suffix}`\nConsider leaving the prompts blank if you want to do open-ended generation, otherwise ensure prompts are different" return Remediation(name="common_suffix", error_msg=error_msg) if common_suffix != "": common_suffix_new_line_handled = common_suffix.replace("\n", "\\n") immediate_msg = f"\n- All prompts end with suffix `{common_suffix_new_line_handled}`" if len(common_suffix) > 10: immediate_msg += f". This suffix seems very long. Consider replacing with a shorter suffix, such as `{display_suggested_suffix}`" if df.prompt.str[: -len(common_suffix)].str.contains(common_suffix, regex=False).any(): immediate_msg += f"\n WARNING: Some of your prompts contain the suffix `{common_suffix}` more than once. We strongly suggest that you review your prompts and add a unique suffix" else: immediate_msg = "\n- Your data does not contain a common separator at the end of your prompts. Having a separator string appended to the end of the prompt makes it clearer to the fine-tuned model where the completion should begin. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples. If you intend to do open-ended generation, then you should leave the prompts empty" if common_suffix == "": optional_msg = f"Add a suffix separator `{display_suggested_suffix}` to all prompts" def optional_fn(x): return add_suffix(x, suggested_suffix) return Remediation( name="common_completion_suffix", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, error_msg=error_msg, ) def common_prompt_prefix_validator(df): """ This validator will suggest to remove a common prefix from the prompt if a long one exist. """ MAX_PREFIX_LEN = 12 immediate_msg = None optional_msg = None optional_fn = None common_prefix = get_common_xfix(df.prompt, xfix="prefix") if common_prefix == "": return Remediation(name="common_prefix") def remove_common_prefix(x, prefix): x["prompt"] = x["prompt"].str[len(prefix) :] return x if (df.prompt == common_prefix).all(): # already handled by common_suffix_validator return Remediation(name="common_prefix") if common_prefix != "": immediate_msg = f"\n- All prompts start with prefix `{common_prefix}`" if MAX_PREFIX_LEN < len(common_prefix): immediate_msg += ". Fine-tuning doesn't require the instruction specifying the task, or a few-shot example scenario. Most of the time you should only add the input data into the prompt, and the desired output into the completion" optional_msg = f"Remove prefix `{common_prefix}` from all prompts" def optional_fn(x): return remove_common_prefix(x, common_prefix) return Remediation( name="common_prompt_prefix", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, ) def common_completion_prefix_validator(df): """ This validator will suggest to remove a common prefix from the completion if a long one exist. """ MAX_PREFIX_LEN = 5 common_prefix = get_common_xfix(df.completion, xfix="prefix") ws_prefix = len(common_prefix) > 0 and common_prefix[0] == " " if len(common_prefix) < MAX_PREFIX_LEN: return Remediation(name="common_prefix") def remove_common_prefix(x, prefix, ws_prefix): x["completion"] = x["completion"].str[len(prefix) :] if ws_prefix: # keep the single whitespace as prefix x["completion"] = " " + x["completion"] return x if (df.completion == common_prefix).all(): # already handled by common_suffix_validator return Remediation(name="common_prefix") immediate_msg = f"\n- All completions start with prefix `{common_prefix}`. Most of the time you should only add the output data into the completion, without any prefix" optional_msg = f"Remove prefix `{common_prefix}` from all completions" def optional_fn(x): return remove_common_prefix(x, common_prefix, ws_prefix) return Remediation( name="common_completion_prefix", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, ) def common_completion_suffix_validator(df): """ This validator will suggest to add a common suffix to the completion if one doesn't already exist in case of classification or conditional generation. """ error_msg = None immediate_msg = None optional_msg = None optional_fn = None ft_type = infer_task_type(df) if ft_type == "open-ended generation" or ft_type == "classification": return Remediation(name="common_suffix") common_suffix = get_common_xfix(df.completion, xfix="suffix") if (df.completion == common_suffix).all(): error_msg = f"All completions are identical: `{common_suffix}`\nEnsure completions are different, otherwise the model will just repeat `{common_suffix}`" return Remediation(name="common_suffix", error_msg=error_msg) # Find a suffix which is not contained within the completion otherwise suggested_suffix = " [END]" suffix_options = [ "\n", ".", " END", "***", "+++", "&&&", "$$$", "@@@", "%%%", ] for suffix_option in suffix_options: if df.completion.str.contains(suffix_option, regex=False).any(): continue suggested_suffix = suffix_option break display_suggested_suffix = suggested_suffix.replace("\n", "\\n") def add_suffix(x, suffix): x["completion"] += suffix return x if common_suffix != "": common_suffix_new_line_handled = common_suffix.replace("\n", "\\n") immediate_msg = f"\n- All completions end with suffix `{common_suffix_new_line_handled}`" if len(common_suffix) > 10: immediate_msg += f". This suffix seems very long. Consider replacing with a shorter suffix, such as `{display_suggested_suffix}`" if df.completion.str[: -len(common_suffix)].str.contains(common_suffix, regex=False).any(): immediate_msg += f"\n WARNING: Some of your completions contain the suffix `{common_suffix}` more than once. We suggest that you review your completions and add a unique ending" else: immediate_msg = "\n- Your data does not contain a common ending at the end of your completions. Having a common ending string appended to the end of the completion makes it clearer to the fine-tuned model where the completion should end. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples." if common_suffix == "": optional_msg = f"Add a suffix ending `{display_suggested_suffix}` to all completions" def optional_fn(x): return add_suffix(x, suggested_suffix) return Remediation( name="common_completion_suffix", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, error_msg=error_msg, ) def completions_space_start_validator(df): """ This validator will suggest to add a space at the start of the completion if it doesn't already exist. This helps with tokenization. """ def add_space_start(x): x["completion"] = x["completion"].apply(lambda x: ("" if x[0] == " " else " ") + x) return x optional_msg = None optional_fn = None immediate_msg = None if df.completion.str[:1].nunique() != 1 or df.completion.values[0][0] != " ": immediate_msg = "\n- The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details" optional_msg = "Add a whitespace character to the beginning of the completion" optional_fn = add_space_start return Remediation( name="completion_space_start", immediate_msg=immediate_msg, optional_msg=optional_msg, optional_fn=optional_fn, ) def lower_case_validator(df, column): """ This validator will suggest to lowercase the column values, if more than a third of letters are uppercase. """ def lower_case(x): x[column] = x[column].str.lower() return x count_upper = ( df[column].apply(lambda x: sum(1 for c in x if c.isalpha() and c.isupper())).sum() ) count_lower = ( df[column].apply(lambda x: sum(1 for c in x if c.isalpha() and c.islower())).sum() ) if count_upper * 2 > count_lower: return Remediation( name="lower_case", immediate_msg=f"\n- More than a third of your `{column}` column/key is uppercase. Uppercase {column}s tends to perform worse than a mixture of case encountered in normal language. We recommend to lower case the data if that makes sense in your domain. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details", optional_msg=f"Lowercase all your data in column/key `{column}`", optional_fn=lower_case, ) def read_any_format(fname, fields=["prompt", "completion"]): """ This function will read a file saved in .csv, .json, .txt, .xlsx or .tsv format using pandas. - for .xlsx it will read the first sheet - for .txt it will assume completions and split on newline """ assert_has_pandas() remediation = None necessary_msg = None immediate_msg = None error_msg = None df = None if os.path.isfile(fname): try: if fname.lower().endswith(".csv") or fname.lower().endswith(".tsv"): file_extension_str, separator = ( ("CSV", ",") if fname.lower().endswith(".csv") else ("TSV", "\t") ) immediate_msg = f"\n- Based on your file extension, your file is formatted as a {file_extension_str} file" necessary_msg = f"Your format `{file_extension_str}` will be converted to `JSONL`" df = pd.read_csv(fname, sep=separator, dtype=str).fillna("") elif fname.lower().endswith(".xlsx"): immediate_msg = ( "\n- Based on your file extension, your file is formatted as an Excel file" ) necessary_msg = "Your format `XLSX` will be converted to `JSONL`" xls = pd.ExcelFile(fname) sheets = xls.sheet_names if len(sheets) > 1: immediate_msg += "\n- Your Excel file contains more than one sheet. Please either save as csv or ensure all data is present in the first sheet. WARNING: Reading only the first sheet..." df = pd.read_excel(fname, dtype=str).fillna("") elif fname.lower().endswith(".txt"): immediate_msg = "\n- Based on your file extension, you provided a text file" necessary_msg = "Your format `TXT` will be converted to `JSONL`" with open(fname, "r") as f: content = f.read() df = pd.DataFrame( [["", line] for line in content.split("\n")], columns=fields, dtype=str, ).fillna("") elif fname.lower().endswith(".jsonl"): df = pd.read_json(fname, lines=True, dtype=str).fillna("") if len(df) == 1: # this is NOT what we expect for a .jsonl file immediate_msg = "\n- Your JSONL file appears to be in a JSON format. Your file will be converted to JSONL format" necessary_msg = "Your format `JSON` will be converted to `JSONL`" df = pd.read_json(fname, dtype=str).fillna("") else: pass # this is what we expect for a .jsonl file elif fname.lower().endswith(".json"): try: # to handle case where .json file is actually a .jsonl file df = pd.read_json(fname, lines=True, dtype=str).fillna("") if len(df) == 1: # this code path corresponds to a .json file that has one line df = pd.read_json(fname, dtype=str).fillna("") else: # this is NOT what we expect for a .json file immediate_msg = "\n- Your JSON file appears to be in a JSONL format. Your file will be converted to JSONL format" necessary_msg = "Your format `JSON` will be converted to `JSONL`" except ValueError: # this code path corresponds to a .json file that has multiple lines (i.e. it is indented) df = pd.read_json(fname, dtype=str).fillna("") else: error_msg = "Your file must have one of the following extensions: .CSV, .TSV, .XLSX, .TXT, .JSON or .JSONL" if "." in fname: error_msg += f" Your file `{fname}` ends with the extension `.{fname.split('.')[-1]}` which is not supported." else: error_msg += f" Your file `{fname}` is missing a file extension." except (ValueError, TypeError): file_extension_str = fname.split(".")[-1].upper() error_msg = f"Your file `{fname}` does not appear to be in valid {file_extension_str} format. Please ensure your file is formatted as a valid {file_extension_str} file." else: error_msg = f"File {fname} does not exist." remediation = Remediation( name="read_any_format", necessary_msg=necessary_msg, immediate_msg=immediate_msg, error_msg=error_msg, ) return df, remediation def format_inferrer_validator(df): """ This validator will infer the likely fine-tuning format of the data, and display it to the user if it is classification. It will also suggest to use ada and explain train/validation split benefits. """ ft_type = infer_task_type(df) immediate_msg = None if ft_type == "classification": immediate_msg = f"\n- Based on your data it seems like you're trying to fine-tune a model for {ft_type}\n- For classification, we recommend you try one of the faster and cheaper models, such as `ada`\n- For classification, you can estimate the expected model performance by keeping a held out dataset, which is not used for training" return Remediation(name="num_examples", immediate_msg=immediate_msg) def apply_necessary_remediation(df, remediation): """ This function will apply a necessary remediation to a dataframe, or print an error message if one exists. """ if remediation.error_msg is not None: sys.stderr.write( f"\n\nERROR in {remediation.name} validator: {remediation.error_msg}\n\nAborting..." ) sys.exit(1) if remediation.immediate_msg is not None: sys.stdout.write(remediation.immediate_msg) if remediation.necessary_fn is not None: df = remediation.necessary_fn(df) return df def accept_suggestion(input_text, auto_accept): sys.stdout.write(input_text) if auto_accept: sys.stdout.write("Y\n") return True return input().lower() != "n" def apply_optional_remediation(df, remediation, auto_accept): """ This function will apply an optional remediation to a dataframe, based on the user input. """ optional_applied = False input_text = f"- [Recommended] {remediation.optional_msg} [Y/n]: " if remediation.optional_msg is not None: if accept_suggestion(input_text, auto_accept): df = remediation.optional_fn(df) optional_applied = True if remediation.necessary_msg is not None: sys.stdout.write(f"- [Necessary] {remediation.necessary_msg}\n") return df, optional_applied def estimate_fine_tuning_time(df): """ Estimate the time it'll take to fine-tune the dataset """ ft_format = infer_task_type(df) expected_time = 1.0 if ft_format == "classification": num_examples = len(df) expected_time = num_examples * 1.44 else: size = df.memory_usage(index=True).sum() expected_time = size * 0.0515 def format_time(time): if time < 60: return f"{round(time, 2)} seconds" elif time < 3600: return f"{round(time / 60, 2)} minutes" elif time < 86400: return f"{round(time / 3600, 2)} hours" else: return f"{round(time / 86400, 2)} days" time_string = format_time(expected_time + 140) sys.stdout.write( f"Once your model starts training, it'll approximately take {time_string} to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you.\n" ) def get_outfnames(fname, split): suffixes = ["_train", "_valid"] if split else [""] i = 0 while True: index_suffix = f" ({i})" if i > 0 else "" candidate_fnames = [ os.path.splitext(fname)[0] + "_prepared" + suffix + index_suffix + ".jsonl" for suffix in suffixes ] if not any(os.path.isfile(f) for f in candidate_fnames): return candidate_fnames i += 1 def get_classification_hyperparams(df): n_classes = df.completion.nunique() pos_class = None if n_classes == 2: pos_class = df.completion.value_counts().index[0] return n_classes, pos_class def write_out_file(df, fname, any_remediations, auto_accept): """ This function will write out a dataframe to a file, if the user would like to proceed, and also offer a fine-tuning command with the newly created file. For classification it will optionally ask the user if they would like to split the data into train/valid files, and modify the suggested command to include the valid set. """ ft_format = infer_task_type(df) common_prompt_suffix = get_common_xfix(df.prompt, xfix="suffix") common_completion_suffix = get_common_xfix(df.completion, xfix="suffix") split = False input_text = ( "- [Recommended] Would you like to split into training and validation set? [Y/n]: " ) if ft_format == "classification": if accept_suggestion(input_text, auto_accept): split = True additional_params = "" common_prompt_suffix_new_line_handled = common_prompt_suffix.replace("\n", "\\n") common_completion_suffix_new_line_handled = common_completion_suffix.replace("\n", "\\n") optional_ending_string = ( f' Make sure to include `stop=["{common_completion_suffix_new_line_handled}"]` so that the generated texts ends at the expected place.' if len(common_completion_suffix_new_line_handled) > 0 else "" ) input_text = "\n\nYour data will be written to a new JSONL file. Proceed [Y/n]: " if not any_remediations and not split: sys.stdout.write( f'\nYou can use your file for fine-tuning:\n> openai api fine_tunes.create -t "{fname}"{additional_params}\n\nAfter you’ve fine-tuned a model, remember that your prompt has to end with the indicator string `{common_prompt_suffix_new_line_handled}` for the model to start generating completions, rather than continuing with the prompt.{optional_ending_string}\n' ) estimate_fine_tuning_time(df) elif accept_suggestion(input_text, auto_accept): fnames = get_outfnames(fname, split) if split: assert len(fnames) == 2 and "train" in fnames[0] and "valid" in fnames[1] MAX_VALID_EXAMPLES = 1000 n_train = max(len(df) - MAX_VALID_EXAMPLES, int(len(df) * 0.8)) df_train = df.sample(n=n_train, random_state=42) df_valid = df.drop(df_train.index) df_train[["prompt", "completion"]].to_json( fnames[0], lines=True, orient="records", force_ascii=False ) df_valid[["prompt", "completion"]].to_json( fnames[1], lines=True, orient="records", force_ascii=False ) n_classes, pos_class = get_classification_hyperparams(df) additional_params += " --compute_classification_metrics" if n_classes == 2: additional_params += f' --classification_positive_class "{pos_class}"' else: additional_params += f" --classification_n_classes {n_classes}" else: assert len(fnames) == 1 df[["prompt", "completion"]].to_json( fnames[0], lines=True, orient="records", force_ascii=False ) # Add -v VALID_FILE if we split the file into train / valid files_string = ("s" if split else "") + " to `" + ("` and `".join(fnames)) valid_string = f' -v "{fnames[1]}"' if split else "" separator_reminder = ( "" if len(common_prompt_suffix_new_line_handled) == 0 else f"After you’ve fine-tuned a model, remember that your prompt has to end with the indicator string `{common_prompt_suffix_new_line_handled}` for the model to start generating completions, rather than continuing with the prompt." ) sys.stdout.write( f'\nWrote modified file{files_string}`\nFeel free to take a look!\n\nNow use that file when fine-tuning:\n> openai api fine_tunes.create -t "{fnames[0]}"{valid_string}{additional_params}\n\n{separator_reminder}{optional_ending_string}\n' ) estimate_fine_tuning_time(df) else: sys.stdout.write("Aborting... did not write the file\n") def infer_task_type(df): """ Infer the likely fine-tuning task type from the data """ CLASSIFICATION_THRESHOLD = 3 # min_average instances of each class if sum(df.prompt.str.len()) == 0: return "open-ended generation" if len(df.completion.unique()) < len(df) / CLASSIFICATION_THRESHOLD: return "classification" return "conditional generation" def get_common_xfix(series, xfix="suffix"): """ Finds the longest common suffix or prefix of all the values in a series """ common_xfix = "" while True: common_xfixes = ( series.str[-(len(common_xfix) + 1) :] if xfix == "suffix" else series.str[: len(common_xfix) + 1] ) # first few or last few characters if ( common_xfixes.nunique() != 1 ): # we found the character at which we don't have a unique xfix anymore break elif ( common_xfix == common_xfixes.values[0] ): # the entire first row is a prefix of every other row break else: # the first or last few characters are still common across all rows - let's try to add one more common_xfix = common_xfixes.values[0] return common_xfix def get_validators(): return [ num_examples_validator, lambda x: necessary_column_validator(x, "prompt"), lambda x: necessary_column_validator(x, "completion"), additional_column_validator, non_empty_field_validator, format_inferrer_validator, duplicated_rows_validator, long_examples_validator, lambda x: lower_case_validator(x, "prompt"), lambda x: lower_case_validator(x, "completion"), common_prompt_suffix_validator, common_prompt_prefix_validator, common_completion_prefix_validator, common_completion_suffix_validator, completions_space_start_validator, ] def apply_validators( df, fname, remediation, validators, auto_accept, write_out_file_func, ): optional_remediations = [] if remediation is not None: optional_remediations.append(remediation) for validator in validators: remediation = validator(df) if remediation is not None: optional_remediations.append(remediation) df = apply_necessary_remediation(df, remediation) any_optional_or_necessary_remediations = any( [ remediation for remediation in optional_remediations if remediation.optional_msg is not None or remediation.necessary_msg is not None ] ) any_necessary_applied = any( [ remediation for remediation in optional_remediations if remediation.necessary_msg is not None ] ) any_optional_applied = False if any_optional_or_necessary_remediations: sys.stdout.write("\n\nBased on the analysis we will perform the following actions:\n") for remediation in optional_remediations: df, optional_applied = apply_optional_remediation(df, remediation, auto_accept) any_optional_applied = any_optional_applied or optional_applied else: sys.stdout.write("\n\nNo remediations found.\n") any_optional_or_necessary_applied = any_optional_applied or any_necessary_applied write_out_file_func(df, fname, any_optional_or_necessary_applied, auto_accept)
[ "\n", "\\n" ]
2024-01-10
TeamKillerX/RyuzakiLib
RyuzakiLib~wandb_logger.py
try: import wandb WANDB_AVAILABLE = True except: WANDB_AVAILABLE = False if WANDB_AVAILABLE: import datetime import io import json import re from pathlib import Path from openai import File, FineTune from openai.datalib.numpy_helper import numpy as np from openai.datalib.pandas_helper import pandas as pd class WandbLogger: """ Log fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) """ if not WANDB_AVAILABLE: print("Logging requires wandb to be installed. Run `pip install wandb`.") else: _wandb_api = None _logged_in = False @classmethod def sync( cls, id=None, n_fine_tunes=None, project="GPT-3", entity=None, force=False, **kwargs_wandb_init, ): """ Sync fine-tunes to Weights & Biases. :param id: The id of the fine-tune (optional) :param n_fine_tunes: Number of most recent fine-tunes to log when an id is not provided. By default, every fine-tune is synced. :param project: Name of the project where you're sending runs. By default, it is "GPT-3". :param entity: Username or team name where you're sending runs. By default, your default entity is used, which is usually your username. :param force: Forces logging and overwrite existing wandb run of the same fine-tune. """ if not WANDB_AVAILABLE: return if id: fine_tune = FineTune.retrieve(id=id) fine_tune.pop("events", None) fine_tunes = [fine_tune] else: # get list of fine_tune to log fine_tunes = FineTune.list() if not fine_tunes or fine_tunes.get("data") is None: print("No fine-tune has been retrieved") return fine_tunes = fine_tunes["data"][-n_fine_tunes if n_fine_tunes is not None else None :] # log starting from oldest fine_tune show_individual_warnings = False if id is None and n_fine_tunes is None else True fine_tune_logged = [ cls._log_fine_tune( fine_tune, project, entity, force, show_individual_warnings, **kwargs_wandb_init, ) for fine_tune in fine_tunes ] if not show_individual_warnings and not any(fine_tune_logged): print("No new successful fine-tunes were found") return "🎉 wandb sync completed successfully" @classmethod def _log_fine_tune( cls, fine_tune, project, entity, force, show_individual_warnings, **kwargs_wandb_init, ): fine_tune_id = fine_tune.get("id") status = fine_tune.get("status") # check run completed successfully if status != "succeeded": if show_individual_warnings: print(f'Fine-tune {fine_tune_id} has the status "{status}" and will not be logged') return # check results are present try: results_id = fine_tune["result_files"][0]["id"] results = File.download(id=results_id).decode("utf-8") except: if show_individual_warnings: print(f"Fine-tune {fine_tune_id} has no results and will not be logged") return # check run has not been logged already run_path = f"{project}/{fine_tune_id}" if entity is not None: run_path = f"{entity}/{run_path}" wandb_run = cls._get_wandb_run(run_path) if wandb_run: wandb_status = wandb_run.summary.get("status") if show_individual_warnings: if wandb_status == "succeeded": print( f"Fine-tune {fine_tune_id} has already been logged successfully at {wandb_run.url}" ) if not force: print( 'Use "--force" in the CLI or "force=True" in python if you want to overwrite previous run' ) else: print( f"A run for fine-tune {fine_tune_id} was previously created but didn't end successfully" ) if wandb_status != "succeeded" or force: print( f"A new wandb run will be created for fine-tune {fine_tune_id} and previous run will be overwritten" ) if wandb_status == "succeeded" and not force: return # start a wandb run wandb.init( job_type="fine-tune", config=cls._get_config(fine_tune), project=project, entity=entity, name=fine_tune_id, id=fine_tune_id, **kwargs_wandb_init, ) # log results df_results = pd.read_csv(io.StringIO(results)) for _, row in df_results.iterrows(): metrics = {k: v for k, v in row.items() if not np.isnan(v)} step = metrics.pop("step") if step is not None: step = int(step) wandb.log(metrics, step=step) fine_tuned_model = fine_tune.get("fine_tuned_model") if fine_tuned_model is not None: wandb.summary["fine_tuned_model"] = fine_tuned_model # training/validation files and fine-tune details cls._log_artifacts(fine_tune, project, entity) # mark run as complete wandb.summary["status"] = "succeeded" wandb.finish() return True @classmethod def _ensure_logged_in(cls): if not cls._logged_in: if wandb.login(): cls._logged_in = True else: raise Exception("You need to log in to wandb") @classmethod def _get_wandb_run(cls, run_path): cls._ensure_logged_in() try: if cls._wandb_api is None: cls._wandb_api = wandb.Api() return cls._wandb_api.run(run_path) except Exception: return None @classmethod def _get_wandb_artifact(cls, artifact_path): cls._ensure_logged_in() try: if cls._wandb_api is None: cls._wandb_api = wandb.Api() return cls._wandb_api.artifact(artifact_path) except Exception: return None @classmethod def _get_config(cls, fine_tune): config = dict(fine_tune) for key in ("training_files", "validation_files", "result_files"): if config.get(key) and len(config[key]): config[key] = config[key][0] if config.get("created_at"): config["created_at"] = datetime.datetime.fromtimestamp(config["created_at"]) return config @classmethod def _log_artifacts(cls, fine_tune, project, entity): # training/validation files training_file = ( fine_tune["training_files"][0] if fine_tune.get("training_files") and len(fine_tune["training_files"]) else None ) validation_file = ( fine_tune["validation_files"][0] if fine_tune.get("validation_files") and len(fine_tune["validation_files"]) else None ) for file, prefix, artifact_type in ( (training_file, "train", "training_files"), (validation_file, "valid", "validation_files"), ): if file is not None: cls._log_artifact_inputs(file, prefix, artifact_type, project, entity) # fine-tune details fine_tune_id = fine_tune.get("id") artifact = wandb.Artifact( "fine_tune_details", type="fine_tune_details", metadata=fine_tune, ) with artifact.new_file("fine_tune_details.json", mode="w", encoding="utf-8") as f: json.dump(fine_tune, f, indent=2) wandb.run.log_artifact( artifact, aliases=["latest", fine_tune_id], ) @classmethod def _log_artifact_inputs(cls, file, prefix, artifact_type, project, entity): file_id = file["id"] filename = Path(file["filename"]).name stem = Path(file["filename"]).stem # get input artifact artifact_name = f"{prefix}-{filename}" # sanitize name to valid wandb artifact name artifact_name = re.sub(r"[^a-zA-Z0-9_\-.]", "_", artifact_name) artifact_alias = file_id artifact_path = f"{project}/{artifact_name}:{artifact_alias}" if entity is not None: artifact_path = f"{entity}/{artifact_path}" artifact = cls._get_wandb_artifact(artifact_path) # create artifact if file not already logged previously if artifact is None: # get file content try: file_content = File.download(id=file_id).decode("utf-8") except: print( f"File {file_id} could not be retrieved. Make sure you are allowed to download training/validation files" ) return artifact = wandb.Artifact(artifact_name, type=artifact_type, metadata=file) with artifact.new_file(filename, mode="w", encoding="utf-8") as f: f.write(file_content) # create a Table try: table, n_items = cls._make_table(file_content) artifact.add(table, stem) wandb.config.update({f"n_{prefix}": n_items}) artifact.metadata["items"] = n_items except: print(f"File {file_id} could not be read as a valid JSON file") else: # log number of items wandb.config.update({f"n_{prefix}": artifact.metadata.get("items")}) wandb.run.use_artifact(artifact, aliases=["latest", artifact_alias]) @classmethod def _make_table(cls, file_content): df = pd.read_json(io.StringIO(file_content), orient="records", lines=True) return wandb.Table(dataframe=df), len(df)
[]
2024-01-10
annwyl21/symptom_logger_api
summarize_ai.py
import os import openai openai.api_key = os.getenv("EllenOpenApiKey") def summarize_with_ai(data): symptom_history = "" for i in range(len(data)): symptom_history += data[i]['date'] symptom_history += data[i]['time'] symptom_history += str(data[i]['pain_score']) symptom_history += data[i]['description'] response = openai.Completion.create( model="text-davinci-003", prompt=""" Perform the following actions: - Summarise the text delimited by triple backticks with a focus on change over time in symptoms, eg which are worsening or improving and whether the person experiences pain every day. This concise summary is intended to be spoken by a person trying to convey their symptoms using easy to understand language without clauses. Data includes a pain score between 1 and 10, with 1 being the least painful and 10 being the most painful. Each time a symptom is recorded, the date, time, pain score and symptom description is also logged. 1. find out relevant symptoms from the information provided. 2. then, summarise the text provided in simple, short sentences. 3. Identify the status of symptoms, are they improving or worsening? 4. Separate your answers with line breaks. 5. Reread your summary before you return it to me and check that all the symptoms described do exist in the original text provided. Use the following format: Summary: <short summary using 'I'> Status: <status> Time period: <time period in days> Text:```""" + symptom_history + "```", temperature=1, max_tokens=100, top_p=1, frequency_penalty=1, presence_penalty=1 ) return response if __name__ == "__main__": symptom_history = """ Record of Symptoms over time<br> 06-06-2023 10:51<br>hip pain in right leg<br> 07-06-2023 21:15<br>difficulty standing from floor<br> 08-06-2023 09:10<br>trouble going to the toilet<br> 09-06-2023 08:00<br>painful to lie on right side in bed, pain located in right hip<br> 10-06-2023 12:00<br>stiff when standing up after sitting watching a film<br> 11-06-2023 11:30<br>left knee pain when walking to the shops<br> 13-06-2023 10:45<br>hip pain in right leg<br> 14-06-2023 07:30<br>stiff when getting up<br> 15-06-2023 06:30<br>stiff when getting up and pain in right hip when standing<br> 16-06-2023 10:10 <br>hip pain in right leg, difficulty using the stairs<br> """ print(type(summarize_with_ai().choices[0].text)) # returns a string print(summarize_with_ai().choices[0].text) # temperature=1, meaning the response will vary - THIS MAKES IT LESS TESTABLE USING AUTOMATION TESTING # max_tokens=100, meaning the response will be up to 100 words long # top_p=1, meaning the response will be the most likely answer and use higher level vocabulary # frequency_penalty=1, meaning the response will be more creative # presence_penalty=1, meaning the response will make more sense # the last 2 parameters are set halfway to reduce the liklihood of getting the same words repeated back in 1 long sentence
[ "\n Perform the following actions:\n - Summarise the text delimited by triple backticks with a focus on change over time in symptoms, eg which are worsening or improving and whether the person experiences pain every day. This concise summary is intended to be spoken by a person trying to convey their symptoms using easy to understand language without clauses. Data includes a pain score between 1 and 10, with 1 being the least painful and 10 being the most painful. Each time a symptom is recorded, the date, time, pain score and symptom description is also logged.\n 1. find out relevant symptoms from the information provided.\n 2. then, summarise the text provided in simple, short sentences.\n 3. Identify the status of symptoms, are they improving or worsening?\n 4. Separate your answers with line breaks.\n 5. Reread your summary before you return it to me and check that all the symptoms described do exist in the original text provided.\n Use the following format:\n Summary: <short summary using 'I'>\n Status: <status>\n Time period: <time period in days>\n Text:```PLACEHOLDER```" ]
2024-01-10
faizanahemad/science-reader
embedding_client_server.py
import os import argparse from flask import Flask, request, jsonify from flask_caching import Cache from sentence_transformers import SentenceTransformer import logging import sys logger = logging.getLogger(__name__) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler(os.path.join(os.getcwd(), "log.txt")) ] ) app = Flask(__name__) @app.route('/embed_query', methods=['POST']) def embed_query(): sentence = request.json['sentence'] key = f"{args.model_name}_query_{sentence}" # Check if the embeddings are in the cache embeddings = cache.get(key) if embeddings is None: # Compute the embeddings and store them in the cache embeddings = model.encode(sentence, normalize_embeddings=True).tolist() cache.set(key, embeddings) return jsonify(embeddings) @app.route('/embed_documents', methods=['POST']) def embed_documents(): sentences = request.json['sentences'] key = f"{args.model_name}_documents_{'_'.join(sentences)}" # Check if the embeddings are in the cache embeddings = cache.get(key) if embeddings is None: # Compute the embeddings and store them in the cache embeddings = model.encode(sentences, normalize_embeddings=True).tolist() cache.set(key, embeddings) return jsonify(embeddings) import requests from typing import List from abc import ABC, abstractmethod from langchain.embeddings.base import Embeddings import torch EMPTY_STRING = "EMPTY DOCUMENT STRING PLACEHOLDER" class EmbeddingClient(Embeddings): def __init__(self, server_url): self.server_url = server_url def embed_documents(self, texts: List[str]) -> List[List[float]]: # PATCH CODE for some models which have dimensionaility errors for empty strings texts = [text if text else EMPTY_STRING for text in texts] response = requests.post(f"{self.server_url}/embed_documents", json={'sentences': texts}) return response.json() def embed_query(self, text: str) -> List[float]: # PATCH CODE for some models which have dimensionaility errors for empty strings text = text if text else EMPTY_STRING response = requests.post(f"{self.server_url}/embed_query", json={'sentence': text}) return response.json() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--device', type=str, default='cuda') parser.add_argument('--model_name', type=str, default='BAAI/bge-small-en') parser.add_argument('--port', type=int, default=8002) parser.add_argument('--folder', type=str, required=True) args = parser.parse_args() # Initialize the model model = SentenceTransformer(args.model_name, device=args.device).to(torch.device(args.device)) # Initialize the cache os.makedirs(os.path.join(os.getcwd(), args.folder), exist_ok=True) cache_dir = os.path.join(os.getcwd(), args.folder, "cache") os.makedirs(cache_dir, exist_ok=True) cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': cache_dir, 'CACHE_DEFAULT_TIMEOUT': 7 * 24 * 60 * 60}) app.run(port=args.port, threaded=True, processes=1)
[]
2024-01-10
faizanahemad/science-reader
server.py
import copy import random import secrets import sys from urllib.parse import unquote from functools import wraps import mmh3 import ast import traceback from flask import Flask, request, jsonify, send_file, session, redirect, url_for, render_template_string from authlib.integrations.flask_client import OAuth from flask_session import Session from collections import defaultdict import requests from io import BytesIO from langchain.vectorstores import FAISS from langchain.embeddings.base import Embeddings from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores.base import VectorStore from collections import defaultdict from Conversation import Conversation from DocIndex import DocFAISS, DocIndex, create_immediate_document_index, ImmediateDocIndex import os import time import multiprocessing import glob from rank_bm25 import BM25Okapi from typing import List, Dict from flask import Flask, Response, stream_with_context import sys sys.setrecursionlimit(sys.getrecursionlimit()*16) import logging import requests from flask_caching import Cache import argparse from datetime import timedelta import sqlite3 from sqlite3 import Error from common import checkNoneOrEmpty, convert_http_to_https, DefaultDictQueue, convert_to_pdf_link_if_needed, \ verify_openai_key_and_fetch_models, convert_doc_to_pdf import spacy from spacy.lang.en import English from spacy.pipeline import Lemmatizer from flask.json.provider import JSONProvider from common import SetQueue import secrets import string from flask_limiter import Limiter from flask_limiter.util import get_remote_address import tiktoken alphabet = string.ascii_letters + string.digits import typing as t # try: # import ujson as json # except ImportError: # import json import json class FlaskJSONProvider(JSONProvider): def dumps(self, obj: t.Any, **kwargs: t.Any) -> str: """Serialize data as JSON. :param obj: The data to serialize. :param kwargs: May be passed to the underlying JSON library. """ return json.dumps(obj, **kwargs) def loads(self, s: str, **kwargs: t.Any) -> t.Any: """Deserialize data as JSON. :param s: Text or UTF-8 bytes. :param kwargs: May be passed to the underlying JSON library. """ return json.loads(s, **kwargs) class OurFlask(Flask): json_provider_class = FlaskJSONProvider os.environ["BING_SEARCH_URL"] = "https://api.bing.microsoft.com/v7.0/search" def create_connection(db_file): """ create a database connection to a SQLite database """ conn = None; try: conn = sqlite3.connect(db_file) except Error as e: print(e) return conn def create_table(conn, create_table_sql): """ create a table from the create_table_sql statement """ try: c = conn.cursor() c.execute(create_table_sql) except Error as e: print(e) def create_tables(): database = "{}/users.db".format(users_dir) sql_create_user_to_doc_id_table = """CREATE TABLE IF NOT EXISTS UserToDocId ( user_email text, doc_id text, created_at text, updated_at text, doc_source_url text ); """ sql_create_user_to_votes_table = """CREATE TABLE IF NOT EXISTS UserToVotes ( user_email text, question_id text, doc_id text, upvoted integer, downvoted integer, feedback_type text, feedback_items text, comments text, question_text text, created_at text, updated_at text );""" sql_create_user_to_conversation_id_table = """CREATE TABLE IF NOT EXISTS UserToConversationId ( user_email text, conversation_id text, created_at text, updated_at text ); """ # create a database connection conn = create_connection(database) # create tables if conn is not None: # create UserToDocId table create_table(conn, sql_create_user_to_doc_id_table) # create UserToVotes table create_table(conn, sql_create_user_to_votes_table) create_table(conn, sql_create_user_to_conversation_id_table) else: print("Error! cannot create the database connection.") cur = conn.cursor() cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_UserToVotes_email_question ON UserToVotes (user_email, question_id)") cur.execute( "CREATE INDEX IF NOT EXISTS idx_User_email_doc_votes ON UserToVotes (user_email)") cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_UserToDocId_email_doc ON UserToDocId (user_email, doc_id)") cur.execute("CREATE INDEX IF NOT EXISTS idx_User_email_doc ON UserToDocId (user_email)") cur.execute( "CREATE UNIQUE INDEX IF NOT EXISTS idx_UserToConversationId_email_doc ON UserToConversationId (user_email, conversation_id)") cur.execute("CREATE INDEX IF NOT EXISTS idx_User_email_doc_conversation ON UserToConversationId (user_email)") conn.commit() from datetime import datetime def addUserToDoc(user_email, doc_id, doc_source_url): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute( """ INSERT OR IGNORE INTO UserToDocId (user_email, doc_id, created_at, updated_at, doc_source_url) VALUES(?,?,?,?,?) """, (user_email, doc_id, datetime.now(), datetime.now(), doc_source_url) ) conn.commit() conn.close() def addConversationToUser(user_email, conversation_id): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute( """ INSERT OR IGNORE INTO UserToConversationId (user_email, conversation_id, created_at, updated_at) VALUES(?,?,?,?) """, (user_email, conversation_id, datetime.now(), datetime.now()) ) conn.commit() conn.close() def getDocsForUser(user_email): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT * FROM UserToDocId WHERE user_email=?", (user_email,)) rows = cur.fetchall() conn.close() return rows def getCoversationsForUser(user_email): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT * FROM UserToConversationId WHERE user_email=?", (user_email,)) rows = cur.fetchall() conn.close() return rows def getAllCoversations(): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT * FROM UserToConversationId") rows = cur.fetchall() conn.close() return rows def addUpvoteOrDownvote(user_email, question_id, doc_id, upvote, downvote): assert not checkNoneOrEmpty(question_id) assert not checkNoneOrEmpty(user_email) assert not checkNoneOrEmpty(doc_id) conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute( """ INSERT OR REPLACE INTO UserToVotes (user_email, question_id, doc_id, upvoted, downvoted, feedback_type, feedback_items, comments, question_text, created_at, updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,?) """, (user_email, question_id, doc_id, upvote, downvote, None, None, None, None, datetime.now(), datetime.now()) ) conn.commit() conn.close() def addGranularFeedback(user_email, question_id, feedback_type, feedback_items, comments, question_text): assert not checkNoneOrEmpty(question_id) assert not checkNoneOrEmpty(user_email) conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() # Ensure that the user has already voted before updating the feedback cur.execute("SELECT 1 FROM UserToVotes WHERE user_email = ? AND question_id = ?", (user_email, question_id)) if not cur.fetchone(): raise ValueError("A vote must exist for the user and question before feedback can be provided") cur.execute( """ UPDATE UserToVotes SET feedback_type = ?, feedback_items = ?, comments = ?, question_text = ?, updated_at = ? WHERE user_email = ? AND question_id = ? """, (feedback_type, ','.join(feedback_items), comments, question_text, datetime.now(), user_email, question_id) ) conn.commit() conn.close() def getUpvotesDownvotesByUser(user_email): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT SUM(upvoted), SUM(downvoted) FROM UserToVotes WHERE user_email=? GROUP BY user_email ", (user_email,)) rows = cur.fetchall() conn.close() return rows def getUpvotesDownvotesByQuestionId(question_id): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT SUM(upvoted), SUM(downvoted) FROM UserToVotes WHERE question_id=? GROUP BY question_id", (question_id,)) rows = cur.fetchall() conn.close() return rows def getUpvotesDownvotesByQuestionIdAndUser(question_id, user_email): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("SELECT SUM(upvoted), SUM(downvoted) FROM UserToVotes WHERE question_id=? AND user_email=? GROUP BY question_id,user_email", (question_id, user_email,)) rows = cur.fetchall() conn.close() return rows def removeUserFromDoc(user_email, doc_id): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("DELETE FROM UserToDocId WHERE user_email=? AND doc_id=?", (user_email, doc_id,)) conn.commit() conn.close() def removeUserFromConversation(user_email, conversation_id): conn = create_connection("{}/users.db".format(users_dir)) cur = conn.cursor() cur.execute("DELETE FROM UserToConversationId WHERE user_email=? AND conversation_id=?", (user_email, conversation_id,)) conn.commit() conn.close() def keyParser(session): keyStore = { "openAIKey": os.getenv("openAIKey", ''), "mathpixId": os.getenv("mathpixId", ''), "mathpixKey": os.getenv("mathpixKey", ''), "cohereKey": os.getenv("cohereKey", ''), "ai21Key": os.getenv("ai21Key", ''), "bingKey": os.getenv("bingKey", ''), "serpApiKey": os.getenv("serpApiKey", ''), "googleSearchApiKey":os.getenv("googleSearchApiKey", ''), "googleSearchCxId":os.getenv("googleSearchCxId", ''), "openai_models_list": os.getenv("openai_models_list", '[]'), "scrapingBrowserUrl": os.getenv("scrapingBrowserUrl", ''), "vllmUrl": os.getenv("vllmUrl", ''), "vllmLargeModelUrl": os.getenv("vllmLargeModelUrl", ''), "vllmSmallModelUrl": os.getenv("vllmSmallModelUrl", ''), "tgiUrl": os.getenv("tgiUrl", ''), "tgiLargeModelUrl": os.getenv("tgiLargeModelUrl", ''), "tgiSmallModelUrl": os.getenv("tgiSmallModelUrl", ''), "embeddingsUrl": os.getenv("embeddingsUrl", ''), "zenrows": os.getenv("zenrows", ''), "brightdataUrl": os.getenv("brightdataUrl", ''), "OPENROUTER_API_KEY": os.getenv("OPENROUTER_API_KEY", ''), } if keyStore["vllmUrl"].strip() != "" or keyStore["vllmLargeModelUrl"].strip() != "" or keyStore["vllmSmallModelUrl"].strip() != "": keyStore["openai_models_list"] = ast.literal_eval(keyStore["openai_models_list"]) for k, v in keyStore.items(): key = session.get(k, v) if key is None or (isinstance(key, str) and key.strip() == "") or (isinstance(key, list) and len(key) == 0): key = v if key is not None and ((isinstance(key, str) and len(key.strip())>0) or (isinstance(key, list) and len(key)>0)): keyStore[k] = key else: keyStore[k] = None return keyStore def generate_ngrams(tokens, n): ngrams = zip(*[tokens[i:] for i in range(n)]) return [" ".join(ngram) for ngram in ngrams] logger = logging.getLogger(__name__) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.ERROR, handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler(os.path.join(os.getcwd(), "log.txt")) ] ) log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) log = logging.getLogger('faiss.loader') log.setLevel(logging.ERROR) logger.setLevel(logging.ERROR) time_logger = logging.getLogger(__name__ + " | TIMING") time_logger.setLevel(logging.INFO) # Set log level for this logger if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--folder', help='The folder where the DocIndex files are stored', required=False, default=None) parser.add_argument('--login_not_needed', help='Whether we use google login or not.', action="store_true") args = parser.parse_args() login_not_needed = args.login_not_needed folder = args.folder if not args.folder: folder = "storage" else: folder = "storage" login_not_needed = True app = OurFlask(__name__) app.config['SESSION_PERMANENT'] = False app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) app.config['SESSION_TYPE'] = 'filesystem' app.config["GOOGLE_CLIENT_ID"] = os.environ.get("GOOGLE_CLIENT_ID") app.config["GOOGLE_CLIENT_SECRET"] = os.environ.get("GOOGLE_CLIENT_SECRET") app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY") app.config["RATELIMIT_STRATEGY"] = "moving-window" app.config["RATELIMIT_STORAGE_URL"] = "memory://" def limiter_key_func(): # logger.info(f"limiter_key_func called with {session.get('email')}") email = None if session: email = session.get('email') if email: return email # Here, you might want to use a different fallback or even raise an error return get_remote_address() limiter = Limiter( app=app, key_func=limiter_key_func, default_limits=["200 per hour", "10 per minute"] ) # app.config['PREFERRED_URL_SCHEME'] = 'http' if login_not_needed else 'https' Session(app) oauth = OAuth(app) log = logging.getLogger('werkzeug') log.setLevel(logging.INFO) log = logging.getLogger('__main__') log.setLevel(logging.INFO) log = logging.getLogger('DocIndex') log.setLevel(logging.INFO) log = logging.getLogger('Conversation') log.setLevel(logging.INFO) log = logging.getLogger('base') log.setLevel(logging.INFO) log = logging.getLogger('faiss.loader') log.setLevel(logging.INFO) google = oauth.register( name='google', client_id=app.config.get("GOOGLE_CLIENT_ID"), client_secret=app.config.get("GOOGLE_CLIENT_SECRET"), access_token_url='https://accounts.google.com/o/oauth2/token', access_token_params=None, authorize_url='https://accounts.google.com/o/oauth2/auth', authorize_params=None, api_base_url='https://www.googleapis.com/oauth2/v1/', userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo', # This is only needed if using openId to fetch user info client_kwargs={'scope': 'email profile'}, server_metadata_url= 'https://accounts.google.com/.well-known/openid-configuration', ) os.makedirs(os.path.join(os.getcwd(), folder), exist_ok=True) cache_dir = os.path.join(os.getcwd(), folder, "cache") users_dir = os.path.join(os.getcwd(), folder, "users") pdfs_dir = os.path.join(os.getcwd(), folder, "pdfs") os.makedirs(cache_dir, exist_ok=True) os.makedirs(users_dir, exist_ok=True) os.makedirs(pdfs_dir, exist_ok=True) os.makedirs(os.path.join(folder, "locks"), exist_ok=True) nlp = English() # just the language with no model _ = nlp.add_pipe("lemmatizer") nlp.initialize() conversation_folder = os.path.join(os.getcwd(), folder, "conversations") folder = os.path.join(os.getcwd(), folder, "documents") os.makedirs(folder, exist_ok=True) os.makedirs(conversation_folder, exist_ok=True) cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR': cache_dir, 'CACHE_DEFAULT_TIMEOUT': 7 * 24 * 60 * 60}) def check_login(session): email = dict(session).get('email', None) name = dict(session).get('name', None) logger.debug(f"Check Login for email {session.get('email')} and name {session.get('name')}") return email, name, email is not None and name is not None def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): logger.debug(f"Login Required call for email {session.get('email')} and name {session.get('name')}") if session.get('email') is None or session.get('name') is None: return redirect('/login', code=302) return f(*args, **kwargs) return decorated_function @app.route('/addUpvoteOrDownvote', methods=['POST']) @limiter.limit("100 per minute") @login_required def add_upvote_downvote(): email, name, _ = check_login(session) data = request.get_json() logger.info(f"'/addUpvoteOrDownvote' Get upvote-downvote request with {data}") if "question_text" in data: source = indexed_docs[data['doc_id']].doc_source if data['doc_id'] in indexed_docs else str(data['doc_id']) question_id = str(mmh3.hash(source + data["question_text"], signed=False)) logger.debug(f"'/addUpvoteOrDownvote' -> generated question_id = {question_id}, Received q_id = {data['question_id']}, both same = {data['question_id'] == question_id}") if checkNoneOrEmpty(data['question_id']): data['question_id'] = question_id if checkNoneOrEmpty(data['question_id']) or checkNoneOrEmpty(data['doc_id']): return "Question Id and Doc Id are needed for `/addUpvoteOrDownvote`", 400 addUpvoteOrDownvote(email, data['question_id'], data['doc_id'], data['upvote'], data['downvote']) return jsonify({'status': 'success'}), 200 @app.route('/getUpvotesDownvotesByUser', methods=['GET']) @limiter.limit("100 per minute") @login_required def get_votes_by_user(): email, name, _ = check_login(session) rows = getUpvotesDownvotesByUser(email) logger.debug(f"called , response = {rows}") return jsonify(rows), 200 # TODO: make bulk api for this @app.route('/getUpvotesDownvotesByQuestionId/<question_id>', methods=['POST']) @limiter.limit("5000 per minute") @login_required def get_votes_by_question(question_id): if checkNoneOrEmpty(question_id) or question_id.strip().lower() == "null": data = request.get_json() logger.debug(f"'/getUpvotesDownvotesByQuestionId' -> data = {data}") if "question_text" in data and "doc_id" in data: source = indexed_docs[data['doc_id']].doc_source if data['doc_id'] in indexed_docs else str(data['doc_id']) question_id = str(mmh3.hash(source + data["question_text"], signed=False)) else: return "Question Id empty", 400 email, name, _ = check_login(session) rows = getUpvotesDownvotesByQuestionId(question_id) logger.info(f"'/getUpvotesDownvotesByQuestionId' called with question_id = {question_id}, response = {rows}") return jsonify(rows), 200 # TODO: make bulk api for this @app.route('/getUpvotesDownvotesByQuestionIdAndUser', methods=['POST']) @limiter.limit("5000 per minute") @login_required def get_votes_by_question_and_user(): email, name, _ = check_login(session) data = request.get_json() question_id = data.get('question_id') if checkNoneOrEmpty(question_id) or question_id.strip().lower() == "null": logger.info(f"'/getUpvotesDownvotesByQuestionIdAndUser' -> data = {data}") if "question_text" in data and "doc_id" in data: source = indexed_docs[data['doc_id']].doc_source if data['doc_id'] in indexed_docs else str(data['doc_id']) question_id = str(mmh3.hash(source + data["question_text"], signed=False)) logger.debug(f"'/getUpvotesDownvotesByQuestionIdAndUser' -> generated question_id = {question_id}") else: return "Question Id empty", 400 rows = getUpvotesDownvotesByQuestionIdAndUser(question_id, email) logger.info(f"'/getUpvotesDownvotesByQuestionIdAndUser' called with question_id = {question_id}, response = {rows}") return jsonify(rows), 200 @app.route('/addUserQuestionFeedback', methods=['POST']) @limiter.limit("100 per minute") @login_required def add_user_question_feedback(): email, name, _ = check_login(session) data = request.get_json() logger.info(f"Get granular feedback request with {data}") if "question_text" in data: source = indexed_docs[data['doc_id']].doc_source if data['doc_id'] in indexed_docs else str(data['doc_id']) question_id = str(mmh3.hash(source + data["question_text"], signed=False)) logger.debug(f"'/addUserQuestionFeedback' -> generated question_id = {question_id}, Received q_id = {data['question_id']}, both same = {data['question_id'] == question_id}") if checkNoneOrEmpty(data['question_id']): data['question_id'] = question_id if checkNoneOrEmpty(data['question_id']) or checkNoneOrEmpty(data['doc_id']): return "Question Id and Doc Id are needed for `/addUserQuestionFeedback`", 400 try: addGranularFeedback(email, data['question_id'], data['feedback_type'], data['feedback_items'], data['comments'], data['question_text']) return jsonify({'status': 'success'}), 200 except ValueError as e: return str(e), 400 @app.route('/write_review/<doc_id>/<tone>', methods=['GET']) @limiter.limit("5 per minute") @login_required def write_review(doc_id, tone): keys = keyParser(session) email, name, _ = check_login(session) if tone == 'undefined': tone = "none" assert tone in ["positive", "negative", "neutral", "none"] review_topic = request.args.get('review_topic') # Has top level key and then an index variable review_topic = review_topic.split(",") review_topic = [r for r in review_topic if len(r.strip()) > 0 and r.strip()!='null'] if len(review_topic) > 1: review_topic = [str(review_topic[0]), int(review_topic[1])] else: review_topic = review_topic[0] additional_instructions = request.args.get('instruction') use_previous_reviews = int(request.args.get('use_previous_reviews')) score_this_review = int(request.args.get('score_this_review')) is_meta_review = int(request.args.get('is_meta_review')) review = set_keys_on_docs(indexed_docs[doc_id], keys).get_review(tone, review_topic, additional_instructions, score_this_review, use_previous_reviews, is_meta_review) return Response(stream_with_context(review), content_type='text/plain') @app.route('/get_reviews/<doc_id>', methods=['GET']) @limiter.limit("10 per minute") @login_required def get_all_reviews(doc_id): keys = keyParser(session) email, name, _ = check_login(session) reviews = set_keys_on_docs(indexed_docs[doc_id], keys).get_all_reviews() # lets send json response return jsonify(reviews) @app.route('/login') @limiter.limit("5 per minute") def login(): scheme_is_https = request.headers.get('X-Forwarded-Proto', 'http') == "https" redirect_uri = url_for('authorize', _external=True) redirect_uri = convert_http_to_https(redirect_uri) if scheme_is_https else redirect_uri if login_not_needed: logger.info(f"Login not needed send login.html") email = request.args.get('email') if email is None: return send_from_directory('interface', 'login.html', max_age=0) session['email'] = email session['name'] = email addUserToDoc(email, "3408472793", "https://arxiv.org/pdf/1706.03762.pdf") return redirect('/interface', code=302) else: logger.info(f"Login needed with redirect authorize uri = {redirect_uri}") return google.authorize_redirect(redirect_uri) @app.route('/logout') @limiter.limit("1 per minute") @login_required def logout(): if 'token' in session: access_token = session['token']['access_token'] requests.post('https://accounts.google.com/o/oauth2/revoke', params={'token': access_token}, headers = {'content-type': 'application/x-www-form-urlencoded'}) session.clear() # clears the session return render_template_string(""" <h1>Logged out</h1> <p><a href="{{ url_for('login') }}">Click here</a> to log in again. You can now close this Tab/Window.</p> """) @app.route('/authorize') @limiter.limit("15 per minute") def authorize(): logger.info(f"Authorize for email {session.get('email')} and name {session.get('name')}") token = google.authorize_access_token() resp = google.get('userinfo') if resp.ok: user_info = resp.json() session['email'] = user_info['email'] session['name'] = user_info['name'] session['token'] = token return redirect('/interface') else: return "Failed to log in", 401 @app.route('/get_user_info') @limiter.limit("15 per minute") @login_required def get_user_info(): if 'email' in session and "name" in session: return jsonify(name=session['name'], email=session['email']) elif google.authorized: resp = google.get('userinfo') if resp.ok: session['email'] = resp.json()['email'] session['name'] = resp.json()['name'] return jsonify(name=resp.json()["name"], email=resp.json()["email"]) else: return "Not logged in", 401 class IndexDict(dict): def __getitem__(self, key): try: item = super().__getitem__(key) if item.doc_id in doc_index_cache: return item else: item = item.copy() doc_index_cache.add(item.doc_id) super().__setitem__(key, item) return item except KeyError: exc = traceback.format_exc() logger.error(f"Error in getting doc_index for key = {key}, error = {exc}") return load_document(folder, key) def __setitem__(self, __key: str, __value: DocIndex) -> None: __value = __value.copy() return super().__setitem__(__key, __value) indexed_docs: IndexDict[str, DocIndex] = IndexDict() doc_index_cache = SetQueue(maxsize=50) def load_conversation(conversation_id): return Conversation.load_local(os.path.join(conversation_folder, conversation_id)) conversation_cache = DefaultDictQueue(maxsize=50, default_factory=load_conversation) def set_keys_on_docs(docs, keys): logger.debug(f"Attaching keys to doc") if isinstance(docs, dict): # docs = {k: v.copy() for k, v in docs.items()} for k, v in docs.items(): v.set_api_keys(keys) elif isinstance(docs, (list, tuple, set)): # docs = [d.copy() for d in docs] for d in docs: d.set_api_keys(keys) else: assert isinstance(docs, (DocIndex, ImmediateDocIndex, Conversation)) docs.set_api_keys(keys) return docs # Initialize an empty list of documents for BM25 bm25_corpus: List[List[str]] = [] doc_id_to_bm25_index: Dict[str, int] = {} bm25 = [None] def get_bm25_grams(text): unigrams = text.split() bigrams = generate_ngrams(unigrams, 2) trigrams = generate_ngrams(unigrams, 3) doc = nlp(text) lemmas = [token.lemma_ for token in doc] bigrams_lemma = generate_ngrams(lemmas, 2) trigrams_lemma = generate_ngrams(lemmas, 3) return unigrams + bigrams + trigrams + lemmas + bigrams_lemma + trigrams_lemma def add_to_bm25_corpus(doc_index: DocIndex): global bm25_corpus, doc_id_to_bm25_index try: doc_info = doc_index.get_short_info() text = doc_info['title'].lower() + " " + doc_info['short_summary'].lower() + " " + doc_info['summary'].lower() except Exception as e: logger.warning(f"Error in getting text for doc_id = {doc_index.doc_id}, error = {e}") text = doc_index.indices["chunks"][0].lower() bm25_corpus.append(get_bm25_grams(text)) doc_id_to_bm25_index[doc_index.doc_id] = len(bm25_corpus) - 1 bm25[0] = BM25Okapi(bm25_corpus) def load_documents(folder): global indexed_docs, bm25_corpus, doc_id_to_bm25_index folders = [f for f in os.listdir(folder) if os.path.isdir(os.path.join(folder, f))] docs: List[DocIndex] = [DocIndex.load_local(os.path.join(folder, filepath)) for filepath in folders] docs = [doc for doc in docs if doc is not None] # and doc.visible # filename = os.path.basename(filepath) for doc_index in docs: indexed_docs[doc_index.doc_id] = doc_index add_to_bm25_corpus(doc_index) def load_document(folder, filepath): global indexed_docs, bm25_corpus, doc_id_to_bm25_index doc_index = DocIndex.load_local(os.path.join(folder, filepath)) indexed_docs[doc_index.doc_id] = doc_index add_to_bm25_corpus(doc_index) return doc_index @app.route('/search_document', methods=['GET']) @limiter.limit("1000 per minute") @login_required def search_document(): keys = keyParser(session) email, name, loggedin = check_login(session) docs = getDocsForUser(email) doc_ids = [d[1] for d in docs] search_text = request.args.get('text') if search_text: search_text = search_text.strip().lower() bm = bm25[0] search_tokens = get_bm25_grams(search_text) scores = bm.get_scores(search_tokens) results = sorted([(score, doc_id) for doc_id, score in zip(indexed_docs.keys(), scores)], reverse=True) docs = [set_keys_on_docs(indexed_docs[doc_id], keys) for score, doc_id in results[:4] if doc_id in doc_ids] scores = [score for score, doc_id in results[:4] if doc_id in doc_ids] top_results = [doc.get_short_info() for score, doc in zip(scores, docs)] logger.debug(f"Search results = {[(score, doc.doc_source) for score, doc in zip(scores, docs)]}") return jsonify(top_results) else: return jsonify({'error': 'No search text provided'}), 400 @app.route('/list_all', methods=['GET']) @limiter.limit("100 per minute") @login_required def list_all(): keys = keyParser(session) email, name, loggedin = check_login(session) docs = getDocsForUser(email) if len(docs) == 0: addUserToDoc(email, "3408472793", "https://arxiv.org/pdf/1706.03762.pdf") docs = getDocsForUser(email) doc_ids = set([d[1] for d in docs]) docs = [set_keys_on_docs(indexed_docs[docId], keys).get_short_info() for docId in doc_ids if docId in indexed_docs] # docs = sorted(docs, key=lambda x: x['last_updated'], reverse=True) return jsonify(docs) @app.route('/get_document_detail', methods=['GET']) @limiter.limit("100 per minute") @login_required def get_document_detail(): keys = keyParser(session) doc_id = request.args.get('doc_id') logger.info(f"/get_document_detail for doc_id = {doc_id}, doc present = {doc_id in indexed_docs}") if doc_id in indexed_docs: return jsonify(set_keys_on_docs(indexed_docs[doc_id], keys).get_all_details()) else: return jsonify({'error': 'Document not found'}), 404 @app.route('/index_document', methods=['POST']) @limiter.limit("10 per minute") @login_required def index_document(): keys = keyParser(session) email, name, loggedin = check_login(session) pdf_url = request.json.get('pdf_url') # if "arxiv.org" not in pdf_url: # return jsonify({'error': 'Only arxiv urls are supported at this moment.'}), 400 pdf_url = convert_to_pdf_link_if_needed(pdf_url) if pdf_url: try: doc_index = immediate_create_and_save_index(pdf_url, keys) addUserToDoc(email, doc_index.doc_id, doc_index.doc_source) return jsonify({'status': 'Indexing started', 'doc_id': doc_index.doc_id, "properly_indexed": doc_index.doc_id in indexed_docs}) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 400 else: return jsonify({'error': 'No pdf_url provided'}), 400 @app.route('/set_keys', methods=['POST']) @limiter.limit("100 per minute") @login_required def set_keys(): keys = request.json # Assuming keys are sent as JSON in the request body for key, value in keys.items(): session[key] = value return jsonify({'result': 'success'}) @app.route('/clear_session', methods=['GET']) @limiter.limit("100 per minute") @login_required def clear_session(): # clear the session session.clear() return jsonify({'result': 'session cleared'}) def delayed_execution(func, delay, *args): time.sleep(delay) return func(*args) def immediate_create_and_save_index(pdf_url, keys): pdf_url = pdf_url.strip() matching_docs = [v for k, v in indexed_docs.items() if v.doc_source==pdf_url] if len(matching_docs) == 0: doc_index = create_immediate_document_index(pdf_url, folder, keys) doc_index = set_keys_on_docs(doc_index, keys) save_index(doc_index, folder) else: logger.info(f"{pdf_url} is already indexed") doc_index = matching_docs[0] return doc_index def save_index(doc_index: DocIndex, folder): indexed_docs[doc_index.doc_id] = doc_index add_to_bm25_corpus(doc_index) doc_index.save_local() @app.route('/streaming_get_answer', methods=['POST']) @limiter.limit("15 per minute") @login_required def streaming_get_answer(): keys = keyParser(session) additional_docs_to_read = request.json.get("additional_docs_to_read", []) a = use_multiple_docs = request.json.get("use_multiple_docs", False) and isinstance(additional_docs_to_read, (tuple, list)) and len(additional_docs_to_read) > 0 b = use_references_and_citations = request.json.get("use_references_and_citations", False) c = provide_detailed_answers = request.json.get("provide_detailed_answers", False) d = perform_web_search = request.json.get("perform_web_search", False) if not (sum([a, b, c, d]) == 0 or sum([a, b, c, d]) == 1): return Response("Invalid answering strategy passed.", status=400, content_type='text/plain') provide_detailed_answers = int(provide_detailed_answers) if use_multiple_docs: additional_docs_to_read = [set_keys_on_docs(indexed_docs[doc_id], keys) for doc_id in additional_docs_to_read] meta_fn = defaultdict(lambda: False, dict(additional_docs_to_read=additional_docs_to_read, use_multiple_docs=use_multiple_docs, use_references_and_citations=use_references_and_citations, provide_detailed_answers=provide_detailed_answers, perform_web_search=perform_web_search)) doc_id = request.json.get('doc_id') query = request.json.get('query') if doc_id in indexed_docs: answer = set_keys_on_docs(indexed_docs[doc_id], keys).streaming_get_short_answer(query, meta_fn) return Response(stream_with_context(answer), content_type='text/plain') else: return Response("Error Document not found", status=404, content_type='text/plain') @app.route('/streaming_summary', methods=['GET']) @limiter.limit("5 per minute") @login_required def streaming_summary(): keys = keyParser(session) doc_id = request.args.get('doc_id') if doc_id in indexed_docs: doc = set_keys_on_docs(indexed_docs[doc_id], keys) answer = doc.streaming_build_summary() p = multiprocessing.Process(target=delayed_execution, args=(save_index, 180, doc, folder)) p.start() return Response(stream_with_context(answer), content_type='text/plain') else: return Response("Error Document not found", content_type='text/plain') @app.route('/streaming_get_followup_answer', methods=['POST']) @limiter.limit("5 per minute") @login_required def streaming_get_followup_answer(): keys = keyParser(session) additional_docs_to_read = request.json.get("additional_docs_to_read", []) a = use_multiple_docs = request.json.get("use_multiple_docs", False) and isinstance(additional_docs_to_read, (tuple, list)) and len(additional_docs_to_read) > 0 b = use_references_and_citations = request.json.get("use_references_and_citations", False) c = provide_detailed_answers = request.json.get("provide_detailed_answers", False) d = perform_web_search = request.json.get("perform_web_search", False) if not (sum([a, b, c, d]) == 0 or sum([a, b, c, d]) == 1): return Response("Invalid answering strategy passed.", status=400, content_type='text/plain') provide_detailed_answers = int(provide_detailed_answers) if use_multiple_docs: additional_docs_to_read = [set_keys_on_docs(indexed_docs[doc_id], keys) for doc_id in additional_docs_to_read] meta_fn = defaultdict(lambda: False, dict(additional_docs_to_read=additional_docs_to_read, use_multiple_docs=use_multiple_docs, use_references_and_citations=use_references_and_citations, provide_detailed_answers=provide_detailed_answers, perform_web_search=perform_web_search)) doc_id = request.json.get('doc_id') query = request.json.get('query') previous_answer = request.json.get('previous_answer') if doc_id in indexed_docs: answer = set_keys_on_docs(indexed_docs[doc_id], keys).streaming_ask_follow_up(query, previous_answer, meta_fn) return Response(stream_with_context(answer), content_type='text/plain') else: return Response("Error Document not found", content_type='text/plain') from multiprocessing import Lock lock = Lock() @app.route('/delete_document', methods=['DELETE']) @limiter.limit("30 per minute") @login_required def delete_document(): email, name, loggedin = check_login(session) doc_id = request.args.get('doc_id') if not doc_id or doc_id not in indexed_docs: return jsonify({'error': 'Document not found'}), 404 removeUserFromDoc(email, doc_id) return jsonify({'status': 'Document deleted successfully'}), 200 @app.route('/get_paper_details', methods=['GET']) @limiter.limit("5 per minute") @login_required def get_paper_details(): keys = keyParser(session) doc_id = request.args.get('doc_id') if doc_id in indexed_docs: paper_details = set_keys_on_docs(indexed_docs[doc_id], keys).paper_details return jsonify(paper_details) else: return jsonify({'error': 'Document not found'}), 404 @app.route('/refetch_paper_details', methods=['GET']) @limiter.limit("5 per minute") @login_required def refetch_paper_details(): keys = keyParser(session) doc_id = request.args.get('doc_id') if doc_id in indexed_docs: paper_details = set_keys_on_docs(indexed_docs[doc_id], keys).refetch_paper_details() return jsonify(paper_details) else: return jsonify({'error': 'Document not found'}), 404 @app.route('/get_extended_abstract', methods=['GET']) @limiter.limit("5 per minute") @login_required def get_extended_abstract(): keys = keyParser(session) doc_id = request.args.get('doc_id') paper_id = request.args.get('paper_id') if doc_id in indexed_docs: extended_abstract = set_keys_on_docs(indexed_docs[doc_id], keys).get_extended_abstract_for_ref_or_cite(paper_id) return Response(stream_with_context(extended_abstract), content_type='text/plain') else: return Response("Error Document not found", content_type='text/plain') @app.route('/get_fixed_details', methods=['GET']) @limiter.limit("30 per minute") @login_required def get_fixed_details(): keys = keyParser(session) doc_id = request.args.get('doc_id') detail_key = request.args.get('detail_key') if doc_id in indexed_docs: fixed_details = set_keys_on_docs(indexed_docs[doc_id], keys).get_fixed_details(detail_key) return Response(stream_with_context(fixed_details), content_type='text/plain') else: return Response("Error: Document not found", content_type='text/plain') from flask import send_from_directory @app.route('/favicon.ico') @limiter.limit("30 per minute") def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') @app.route('/loader.gif') @limiter.limit("10 per minute") def loader(): return send_from_directory(os.path.join(app.root_path, 'static'), 'gradient-loader.gif', mimetype='image/gif') @app.route('/interface/<path:path>') @limiter.limit("100 per minute") def send_static(path): return send_from_directory('interface', path, max_age=0) @app.route('/interface') @limiter.limit("20 per minute") @login_required def interface(): return send_from_directory('interface', 'interface.html', max_age=0) from flask import Response, stream_with_context @app.route('/proxy', methods=['GET']) @login_required def proxy(): file_url = request.args.get('file') logger.debug(f"Proxying file {file_url}, exists on disk = {os.path.exists(file_url)}") return Response(stream_with_context(cached_get_file(file_url)), mimetype='application/pdf') @app.route('/') @limiter.limit("20 per minute") @login_required def index(): return redirect('/interface') @app.route('/upload_pdf', methods=['POST']) @limiter.limit("10 per minute") @login_required def upload_pdf(): keys = keyParser(session) email, name, loggedin = check_login(session) pdf_file = request.files.get('pdf_file') if pdf_file: try: # Determine the file extension file_ext = os.path.splitext(pdf_file.filename)[1] # Save the original file first original_file_path = os.path.join(pdfs_dir, pdf_file.filename) pdf_file.save(original_file_path) if file_ext in ['.doc', '.docx']: # Convert to PDF pdf_filename = os.path.splitext(pdf_file.filename)[0] + ".pdf" pdf_file_path = os.path.join(pdfs_dir, pdf_filename) if not convert_doc_to_pdf(original_file_path, pdf_file_path): return jsonify({'error': 'Conversion to PDF failed'}), 400 else: pdf_file_path = original_file_path # Create and save index doc_index = immediate_create_and_save_index(pdf_file_path, keys) addUserToDoc(email, doc_index.doc_id, doc_index.doc_source) return jsonify({'status': 'Indexing started', 'doc_id': doc_index.doc_id, "properly_indexed": doc_index.doc_id in indexed_docs}) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 400 else: return jsonify({'error': 'No pdf_file provided'}), 400 @app.route('/upload_doc_to_conversation/<conversation_id>', methods=['POST']) @limiter.limit("10 per minute") @login_required def upload_doc_to_conversation(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) pdf_file = request.files.get('pdf_file') conversation: Conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) if pdf_file and conversation_id: try: # save file to disk at pdfs_dir. pdf_file.save(os.path.join(pdfs_dir, pdf_file.filename)) full_pdf_path = os.path.join(pdfs_dir, pdf_file.filename) conversation.add_uploaded_document(full_pdf_path) conversation.save_local() return jsonify({'status': 'Indexing started'}) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 400 # If it is not a pdf file then assume we have url conversation: Conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) pdf_url = request.json.get('pdf_url') pdf_url = convert_to_pdf_link_if_needed(pdf_url) if pdf_url: try: conversation.add_uploaded_document(pdf_url) conversation.save_local() return jsonify({'status': 'Indexing started'}) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 400 else: return jsonify({'error': 'No pdf_url or pdf_file provided'}), 400 @app.route('/delete_document_from_conversation/<conversation_id>/<document_id>', methods=['DELETE']) @limiter.limit("10 per minute") @login_required def delete_document_from_conversation(conversation_id, document_id): keys = keyParser(session) email, name, loggedin = check_login(session) conversation: Conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) doc_id = document_id if doc_id: try: conversation.delete_uploaded_document(doc_id) return jsonify({'status': 'Document deleted'}) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 400 else: return jsonify({'error': 'No doc_id provided'}), 400 @app.route('/list_documents_by_conversation/<conversation_id>', methods=['GET']) @limiter.limit("30 per minute") @login_required def list_documents_by_conversation(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) conversation: Conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) if conversation: docs:List[DocIndex] = conversation.get_uploaded_documents(readonly=True) docs = [d.get_short_info() for d in docs] # sort by doc_id # docs = sorted(docs, key=lambda x: x['doc_id'], reverse=True) return jsonify(docs) else: return jsonify({'error': 'Conversation not found'}), 404 @app.route('/download_doc_from_conversation/<conversation_id>/<doc_id>', methods=['GET']) @limiter.limit("30 per minute") @login_required def download_doc_from_conversation(conversation_id, doc_id): keys = keyParser(session) conversation: Conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) if conversation: doc:DocIndex = conversation.get_uploaded_documents(doc_id, readonly=True)[0] if doc and os.path.exists(doc.doc_source): return send_from_directory(doc.doc_source, as_attachment=True) elif doc: return redirect(doc.doc_source) else: return jsonify({'error': 'Document not found'}), 404 else: return jsonify({'error': 'Conversation not found'}), 404 def cached_get_file(file_url): chunk_size = 1024 # Define your chunk size file_data = cache.get(file_url) # If the file is not in the cache, read it from disk and save it to the cache if file_data is not None: logger.info(f"cached_get_file for {file_url} found in cache") for chunk in file_data: yield chunk # how do I chunk with chunk size? elif os.path.exists(file_url): file_data = [] with open(file_url, 'rb') as f: while True: chunk = f.read(chunk_size) if chunk: file_data.append(chunk) yield chunk if not chunk: break cache.set(file_url, file_data) else: file_data = [] try: headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} req = requests.get(file_url, stream=True, verify=False, headers=headers) except requests.exceptions.RequestException as e: logger.error(f"Failed to download file: {e}") req = requests.get(file_url, stream=True, verify=False) # TODO: save the downloaded file to disk. for chunk in req.iter_content(chunk_size=chunk_size): file_data.append(chunk) yield chunk cache.set(file_url, file_data) ### chat apis @app.route('/list_conversation_by_user', methods=['GET']) @limiter.limit("50 per minute") @login_required def list_conversation_by_user(): # TODO: sort by last_updated email, name, loggedin = check_login(session) keys = keyParser(session) last_n_conversations = request.args.get('last_n_conversations', 10) # TODO: add ability to get only n conversations conversation_ids = [c[1] for c in getCoversationsForUser(email)] conversations = [conversation_cache[conversation_id] for conversation_id in conversation_ids] conversations = [conversation for conversation in conversations if conversation is not None] conversations = [set_keys_on_docs(conversation, keys) for conversation in conversations] data = [[conversation.get_metadata(), conversation] for conversation in conversations] sorted_data_reverse = sorted(data, key=lambda x: x[0]['last_updated'], reverse=True) # TODO: if any conversation has 0 messages then just make it the latest. IT should also have a zero length summary. if len(sorted_data_reverse) > 0 and len(sorted_data_reverse[0][0]["summary_till_now"].strip()) > 0: sorted_data_reverse = sorted(sorted_data_reverse, key=lambda x: len(x[0]['summary_till_now'].strip()), reverse=False) if sorted_data_reverse[0][0]["summary_till_now"].strip() == "": new_conversation = sorted_data_reverse[0][1] sorted_data_reverse = sorted_data_reverse[1:] sorted_data_reverse = sorted(sorted_data_reverse, key=lambda x: x[0]['last_updated'], reverse=True) new_conversation.set_field("memory", {"last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}) else: new_conversation = create_conversation_simple(session) sorted_data_reverse.insert(0, [new_conversation.get_metadata(), new_conversation]) sorted_data_reverse = [sd[0] for sd in sorted_data_reverse] return jsonify(sorted_data_reverse) @app.route('/create_conversation', methods=['POST']) @limiter.limit("5 per minute") @login_required def create_conversation(): conversation = create_conversation_simple(session) data = conversation.get_metadata() return jsonify(data) def create_conversation_simple(session): email, name, loggedin = check_login(session) keys = keyParser(session) from base import get_embedding_model conversation_id = email + "_" + ''.join(secrets.choice(alphabet) for i in range(36)) conversation = Conversation(email, openai_embed=get_embedding_model(keys), storage=conversation_folder, conversation_id=conversation_id) conversation = set_keys_on_docs(conversation, keys) addConversationToUser(email, conversation.conversation_id) return conversation @app.route('/list_messages_by_conversation/<conversation_id>', methods=['GET']) @limiter.limit("100 per minute") @login_required def list_messages_by_conversation(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) last_n_messages = request.args.get('last_n_messages', 10) # TODO: add capability to get only last n messages conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) return jsonify(conversation.get_message_list()) @app.route('/list_messages_by_conversation_shareable/<conversation_id>', methods=['GET']) @limiter.limit("10 per minute") def list_messages_by_conversation_shareable(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) conversation_ids = [c[1] for c in getAllCoversations()] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation: Conversation = conversation_cache[conversation_id] if conversation: docs: List[DocIndex] = conversation.get_uploaded_documents(readonly=True) docs = [d.get_short_info() for d in docs] messages = conversation.get_message_list() return jsonify({"messages": messages, "docs": docs}) else: return jsonify({'error': 'Conversation not found'}), 404 @app.route('/send_message/<conversation_id>', methods=['POST']) @limiter.limit("5 per minute") @login_required def send_message(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) query = request.json query["additional_docs_to_read"] = [] additional_params: dict = query["checkboxes"] additional_docs_to_read = additional_params.get("additional_docs_to_read", []) additional_docs_to_read = list(set(additional_docs_to_read)) use_multiple_docs = additional_params.get("use_multiple_docs", False) and isinstance(additional_docs_to_read, (tuple, list)) and len( additional_docs_to_read) > 0 if use_multiple_docs: keys = copy.deepcopy(keys) keys["use_gpt4"] = False additional_docs_to_read = [set_keys_on_docs(indexed_docs[doc_id], keys) for doc_id in additional_docs_to_read] query["additional_docs_to_read"] = additional_docs_to_read # We don't process the request data in this mockup, but we would normally send a new message here return Response(stream_with_context(conversation(query)), content_type='text/plain') @app.route('/get_conversation_details/<conversation_id>', methods=['GET']) @limiter.limit("100 per minute") @login_required def get_conversation_details(conversation_id): keys = keyParser(session) email, name, loggedin = check_login(session) conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) # Dummy data data = conversation.get_metadata() return jsonify(data) @app.route('/delete_conversation/<conversation_id>', methods=['DELETE']) @limiter.limit("25 per minute") @login_required def delete_conversation(conversation_id): email, name, loggedin = check_login(session) keys = keyParser(session) conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) removeUserFromConversation(email, conversation_id) # In a real application, you'd delete the conversation here return jsonify({'message': f'Conversation {conversation_id} deleted'}) @app.route('/delete_message_from_conversation/<conversation_id>/<message_id>/<index>', methods=['DELETE']) @limiter.limit("30 per minute") @login_required def delete_message_from_conversation(conversation_id, message_id, index): email, name, loggedin = check_login(session) keys = keyParser(session) conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation = set_keys_on_docs(conversation, keys) conversation.delete_message(message_id, index) # In a real application, you'd delete the conversation here return jsonify({'message': f'Message {message_id} deleted'}) @app.route('/delete_last_message/<conversation_id>', methods=['DELETE']) @limiter.limit("30 per minute") @login_required def delete_last_message(conversation_id): message_id=1 email, name, loggedin = check_login(session) keys = keyParser(session) conversation_ids = [c[1] for c in getCoversationsForUser(email)] if conversation_id not in conversation_ids: return jsonify({"message": "Conversation not found"}), 404 else: conversation = conversation_cache[conversation_id] conversation: Conversation = set_keys_on_docs(conversation, keys) conversation.delete_last_turn() # In a real application, you'd delete the conversation here return jsonify({'message': f'Message {message_id} deleted'}) def open_browser(url): import webbrowser import subprocess if sys.platform.startswith('linux'): subprocess.call(['xdg-open', url]) elif sys.platform.startswith('darwin'): subprocess.call(['open', url]) else: webbrowser.open(url) create_tables() load_documents(folder) # def removeAllUsersFromConversation(): # conn = create_connection("{}/users.db".format(users_dir)) # cur = conn.cursor() # cur.execute("DELETE FROM UserToConversationId") # conn.commit() # conn.close() # # removeAllUsersFromConversation() if __name__ == '__main__': port = 443 # app.run(host="0.0.0.0", port=port,threaded=True, ssl_context=('cert-ext.pem', 'key-ext.pem')) app.run(host="0.0.0.0", port=5000,threaded=True)
[ "application/x-www-form-urlencoded" ]
2024-01-10
faizanahemad/science-reader
Conversation.py
from langchain.memory import ConversationSummaryMemory, ChatMessageHistory import shutil import sys import random from functools import partial import glob from filelock import FileLock, Timeout import matplotlib.pyplot as plt import matplotlib.image as mpimg from collections import defaultdict import re from semanticscholar import SemanticScholar from semanticscholar.SemanticScholar import Paper from langchain.utilities import BingSearchAPIWrapper from collections import Counter import mmh3 from pprint import pprint import time import concurrent.futures import pandas as pd import tiktoken from copy import deepcopy, copy from collections import defaultdict import requests import tempfile from tqdm import tqdm import requests import dill import os import re from prompts import prompts from langchain.document_loaders import MathpixPDFLoader from datetime import datetime, timedelta from langchain.llms import OpenAI from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain import OpenAI, ConversationChain from langchain.embeddings import OpenAIEmbeddings from review_criterias import review_params from pathlib import Path from more_itertools import peekable from concurrent.futures import Future import openai import tiktoken from langchain.agents import Tool from langchain.tools import BaseTool from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI from langchain.text_splitter import SpacyTextSplitter from langchain.text_splitter import TokenTextSplitter from langchain.text_splitter import NLTKTextSplitter from langchain.prompts import PromptTemplate from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.llms import GPT4All from llama_index.node_parser.simple import SimpleNodeParser from llama_index.langchain_helpers.text_splitter import TokenTextSplitter from llama_index import ( GPTVectorStoreIndex, LangchainEmbedding, LLMPredictor, ServiceContext, StorageContext, download_loader, PromptHelper ) from llama_index import SimpleDirectoryReader, LangchainEmbedding, GPTListIndex, PromptHelper from llama_index import LLMPredictor, ServiceContext from langchain.vectorstores import FAISS from langchain.vectorstores.base import VectorStore from langchain.schema import Document as LangchainDocument from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.document_loaders import TextLoader from llama_index.data_structs.node import Node, DocumentRelationship from llama_index import LangchainEmbedding, ServiceContext from llama_index import GPTTreeIndex, SimpleDirectoryReader from langchain.document_loaders import PyPDFLoader from langchain.utilities import SerpAPIWrapper from langchain.agents import initialize_agent from langchain.agents import AgentType from typing import Optional, Type, List from langchain.callbacks.manager import AsyncCallbackManagerForToolRun, CallbackManagerForToolRun from langchain.tools import DuckDuckGoSearchRun from langchain.utilities import BingSearchAPIWrapper, DuckDuckGoSearchAPIWrapper from langchain.tools import DuckDuckGoSearchResults from langchain.prompts import PromptTemplate from common import * from base import * import ai21 from langchain.schema import Document pd.options.display.float_format = '{:,.2f}'.format pd.set_option('max_colwidth', 800) pd.set_option('display.max_columns', 100) import logging logger = logging.getLogger(__name__) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler(os.path.join(os.getcwd(), "log.txt")) ] ) logger.setLevel(logging.INFO) time_logger = logging.getLogger(__name__ + " | TIMING") time_logger.setLevel(logging.INFO) # Set log level for this logger LEN_CUTOFF_WEB_TEXT = 50 from tenacity import ( retry, stop_after_attempt, wait_random_exponential, ) import asyncio import threading from playwright.async_api import async_playwright from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor import time from DocIndex import DocIndex, DocFAISS, create_immediate_document_index, create_index_faiss from langchain.memory import ConversationSummaryMemory, ChatMessageHistory import secrets import string import tiktoken # try: # import ujson as json # except ImportError: # import json import json alphabet = string.ascii_letters + string.digits class Conversation: def __init__(self, user_id, openai_embed, storage, conversation_id) -> None: self.conversation_id = conversation_id self.user_id = user_id folder = os.path.join(storage, f"{self.conversation_id}") self._storage = folder os.makedirs(folder, exist_ok=True) memory = { "title": 'Start the Conversation', "last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "running_summary":[], # List of strings, each string is a running summary of chat till now. } messages = list() # list of message objects of structure like `{"message_id": "one", "text": "Hello", "sender": "user/model", "user_id": "user_1", "conversation_id": "conversation_id"},` indices = dict(summary_index=create_index_faiss([''], openai_embed, doc_id=self.conversation_id,)) self.set_field("memory", memory) self.set_field("messages", messages) self.set_field("indices", indices) self.set_field("uploaded_documents_list", list()) # just a List[str] of doc index ids self.save_local() # Make a method to get useful prior context and encapsulate all logic for getting prior context # Make a method to persist important details and encapsulate all logic for persisting important details in a function @property def store_separate(self): return ["indices", "raw_documents", "raw_documents_index", "memory", "messages", "uploaded_documents_list"] def add_uploaded_document(self, pdf_url): storage = os.path.join(self._storage, "uploaded_documents") os.makedirs(storage, exist_ok=True) keys = self.get_api_keys() keys["mathpixKey"] = None keys["mathpixId"] = None current_documents: List[DocIndex] = self.get_uploaded_documents() current_sources = [d.doc_source for d in current_documents] if pdf_url in current_sources: return None doc_index: DocIndex = create_immediate_document_index(pdf_url, storage, keys) doc_index._visible = False doc_index.save_local() doc_id = doc_index.doc_id doc_storage = doc_index._storage previous_docs = self.get_field("uploaded_documents_list") previous_docs = previous_docs if previous_docs is not None else [] # deduplicate on basis of doc_id previous_docs = [d for i, d in enumerate(previous_docs) if d[0] not in [d[0] for d in previous_docs[:i]]] self.set_field("uploaded_documents_list", previous_docs + [(doc_id, doc_storage)], overwrite=True) def get_uploaded_documents(self, doc_id=None, readonly=False)->List[DocIndex]: try: doc_list = self.get_field("uploaded_documents_list") except ValueError as e: doc_list = None self.set_field("uploaded_documents_list", []) if doc_list is not None: docs = [DocIndex.load_local(doc_storage) for doc_id, doc_storage in doc_list] else: docs = [] if doc_id is not None: docs = [d for d in docs if d.doc_id == doc_id] if not readonly: keys = self.get_api_keys() for d in docs: d.set_api_keys(keys) return docs def delete_uploaded_document(self, doc_id): self.set_field("uploaded_documents_list", [d for d in self.get_field("uploaded_documents_list") if d[0] != doc_id], overwrite=True) @staticmethod def load_local(folder): original_folder = folder folder = os.path.join(folder, os.path.basename(folder)+".index") import dill try: with open(folder, "rb") as f: obj = dill.load(f) setattr(obj, "_storage", original_folder) return obj except Exception as e: logger.error( f"Error loading from local storage {folder} with error {e}") try: shutil.rmtree(original_folder) except Exception as e: logger.error( f"Error deleting local storage {folder} with error {e}") return None def save_local(self): import dill doc_id = self.conversation_id folder = self._storage os.makedirs(folder, exist_ok=True) os.makedirs(os.path.join(folder, "locks"), exist_ok=True) path = Path(folder) lock_location = os.path.join(os.path.join(path.parent.parent, "locks"), f"{doc_id}") filepath = os.path.join(folder, f"{doc_id}.index") lock = FileLock(f"{lock_location}.lock") if hasattr(self, "api_keys"): presave_api_keys = self.api_keys self.api_keys = {k: None for k, v in self.api_keys.items()} with lock.acquire(timeout=600): previous_attr = dict() for k in self.store_separate: if hasattr(self, k): previous_attr[k] = getattr(self, k) setattr(self, k, None) with open(filepath, "wb") as f: dill.dump(self, f) for k, v in previous_attr.items(): setattr(self, k, v) if hasattr(self, "api_keys"): self.api_keys = presave_api_keys def get_field(self, top_key): import dill doc_id = self.conversation_id folder = self._storage filepath = os.path.join(folder, f"{doc_id}-{top_key}.partial") json_filepath = os.path.join(folder, f"{doc_id}-{top_key}.json") try: assert top_key in self.store_separate except Exception as e: raise ValueError(f"Invalid top_key {top_key} provided") logger.debug(f"Get doc data for top_key = {top_key}, folder = {folder}, filepath = {filepath} exists = {os.path.exists(filepath)}, json filepath = {json_filepath} exists = {os.path.exists(json_filepath)}, already loaded = {getattr(self, top_key, None) is not None}") if getattr(self, top_key, None) is not None: return getattr(self, top_key, None) else: if os.path.exists(json_filepath): with open(json_filepath, "r") as f: obj = json.load(f) setattr(self, top_key, obj) return obj elif os.path.exists(filepath): with open(filepath, "rb") as f: obj = dill.load(f) if top_key not in ["indices", "raw_documents", "raw_documents_index"]: with open(json_filepath, "w") as f: json.dump(obj, f) setattr(self, top_key, obj) return obj else: return None def _get_lock_location(self, key="all"): doc_id = self.conversation_id folder = self._storage path = Path(folder) lock_location = os.path.join(os.path.join(path.parent.parent, "locks"), f"{doc_id}_{key}") return lock_location def set_field(self, top_key, value, overwrite=False): import dill doc_id = self.conversation_id folder = self._storage filepath = os.path.join(folder, f"{doc_id}-{top_key}.partial") json_filepath = os.path.join(folder, f"{doc_id}-{top_key}.json") lock_location = self._get_lock_location(top_key) lock = FileLock(f"{lock_location}.lock") with lock.acquire(timeout=600): tk = self.get_field(top_key) assert (type(tk) == type(value) or tk is None) or (isinstance(tk, (tuple, list)) and isinstance(value, (tuple, list))) if tk is not None: if isinstance(tk, dict) and not overwrite: tk.update(value) elif isinstance(tk, list) and not overwrite: tk.extend(value) elif isinstance(tk, str) and not overwrite: tk = tk + value elif isinstance(tk, tuple) and not overwrite: tk = tk + value else: tk = value setattr(self, top_key, tk) else: setattr(self, top_key, value) if top_key not in ["indices", "raw_documents_index"]: with open(json_filepath, "w") as f: json.dump(getattr(self, top_key, None), f) else: with open(os.path.join(filepath), "wb") as f: dill.dump(getattr(self, top_key, None), f) @timer def retrieve_prior_context(self, query, links=None, required_message_lookback=16): # Lets get the previous 2 messages, upto 1000 tokens summary_lookback = 4 futures = [get_async_future(self.get_field, "memory"), get_async_future(self.get_field, "messages"), get_async_future(self.get_field, "indices")] memory, messages, indices = [f.result() for f in futures] message_lookback = 2 previous_messages_text = "" while get_gpt4_word_count(previous_messages_text) < 2500 and message_lookback <= required_message_lookback and required_message_lookback > 0: previous_messages = messages[-message_lookback:] previous_messages = [{"sender": m["sender"], "text": extract_user_answer(m["text"])} for m in previous_messages] previous_messages_text = '\n\n'.join([f"{m['sender']}:\n'''{m['text']}'''\n" for m in previous_messages]) message_lookback += 2 previous_messages_short = previous_messages_text previous_messages_text = "" while get_gpt4_word_count( previous_messages_text) < 7500 and message_lookback <= required_message_lookback and required_message_lookback > 0: previous_messages = messages[-message_lookback:] previous_messages = [{"sender": m["sender"], "text": extract_user_answer(m["text"])} for m in previous_messages] previous_messages_text = '\n\n'.join([f"{m['sender']}:\n'''{m['text']}'''\n" for m in previous_messages]) message_lookback += 2 previous_messages_long = previous_messages_text running_summary = memory["running_summary"][-1:] older_extensive_summary = find_nearest_divisible_by_three(memory["running_summary"]) if len(memory["running_summary"]) > 4: summary_nodes = get_async_future(indices["summary_index"].similarity_search, query, k=6) st_retr = time.time() got_summary_nodes = False while time.time() - st_retr < 6: if summary_nodes.done() and summary_nodes.exception() is None: got_summary_nodes = True break time.sleep(0.1) if got_summary_nodes: summary_nodes = [n.page_content for n in summary_nodes.result()] not_taken_summaries = running_summary + memory["running_summary"][-summary_lookback:] summary_nodes = [n for n in summary_nodes if n not in not_taken_summaries] summary_nodes = [n for n in summary_nodes if len(n.strip()) > 0][-2:] # summary_text = get_first_last_parts("\n".join(summary_nodes + running_summary), 0, 1000) else: summary_nodes = [] else: summary_nodes = [] if len(running_summary) > 0 and running_summary[0] != older_extensive_summary: running_summary = [older_extensive_summary] + running_summary # We return a dict return dict(previous_messages=previous_messages_short, previous_messages_long=previous_messages_long, summary_nodes=summary_nodes + running_summary) def create_title(self, query, response): llm = CallLLm(self.get_api_keys(), use_gpt4=False) memory = self.get_field("memory") if (memory["title"] == 'Start the Conversation' and len(memory["running_summary"]) >= 0): # or (len(memory["running_summary"]) >= 5 and len(memory["running_summary"]) % 10 == 1) llm = CallLLm(self.get_api_keys(), use_gpt4=False) running_summary = memory["running_summary"][-1:] running_summary = "".join(running_summary) running_summary = f"The summary of the conversation is as follows:\n'''{running_summary}'''" if len(running_summary) > 0 else '' prompt = f"""You are given conversation details between a human and an AI. You will write a title for this conversation. {running_summary} The last 2 messages of the conversation are as follows: User query: '''{query}''' System response: '''{response}''' Now lets write a title of the conversation. Title of the conversation: """ prompt = get_first_last_parts(prompt, 1000, 2200) title = get_async_future(llm, prompt, temperature=0.2, stream=False) else: title = wrap_in_future(self.get_field("memory")["title"]) return title @timer def persist_current_turn(self, query, response, new_docs): # message format = `{"message_id": "one", "text": "Hello", "sender": "user/model", "user_id": "user_1", "conversation_id": "conversation_id"}` # set the two messages in the message list as per above format. messages = get_async_future(self.get_field, "messages") memory = get_async_future(self.get_field, "memory") indices = get_async_future(self.get_field, "indices") memory = memory.result() messages = messages.result() message_lookback = 2 previous_messages_text = "" prompt = prompts.persist_current_turn_prompt.format(query=query, response=extract_user_answer(response), previous_messages_text=previous_messages_text, previous_summary=get_first_last_parts("".join(memory["running_summary"][-4:-3] + memory["running_summary"][-1:]), 0, 1000)) while get_gpt3_word_count(previous_messages_text + "\n\n" + prompt) < 3000 and message_lookback < 6: previous_messages = messages[-message_lookback:] previous_messages = [{"sender": m["sender"], "text": extract_user_answer(m["text"])} for m in previous_messages] previous_messages_text = '\n\n'.join([f"{m['sender']}:\n'''{m['text']}'''\n" for m in previous_messages]) message_lookback += 2 msg_set = get_async_future(self.set_field, "messages", [ {"message_id": str(mmh3.hash(self.conversation_id + self.user_id + query, signed=False)), "text": query, "sender": "user", "user_id": self.user_id, "conversation_id": self.conversation_id}, {"message_id": str(mmh3.hash(self.conversation_id + self.user_id + response, signed=False)), "text": response, "sender": "model", "user_id": self.user_id, "conversation_id": self.conversation_id}]) prompt = prompts.persist_current_turn_prompt.format(query=query, response=extract_user_answer(response), previous_messages_text=previous_messages_text, previous_summary=get_first_last_parts("".join(memory["running_summary"][-4:-3] + memory["running_summary"][-1:]), 0, 1000)) llm = CallLLm(self.get_api_keys(), use_gpt4=False, use_16k=True) if get_gpt3_word_count(prompt) > 3300 else CallLLm(self.get_api_keys(), use_gpt4=False) prompt = get_first_last_parts(prompt, 4000, 7500) summary = get_async_future(llm, prompt, temperature=0.2, stream=False) title = self.create_title(query, extract_user_answer(response)) memory["last_updated"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") summary = summary.result() summary_index_new = get_async_future(FAISS.from_texts, [summary], get_embedding_model(self.get_api_keys())) memory["running_summary"].append(summary) try: title = title.result() memory["title"] = title except Exception as e: pass mem_set = get_async_future(self.set_field, "memory", memory) # self.set_field("memory", memory) indices = indices.result() _ = indices["summary_index"].merge_from(summary_index_new.result()) self.set_field("indices", indices) msg_set.result() mem_set.result() def create_deep_summary(self): indices = get_async_future(self.get_field, "indices") memory = get_async_future(self.get_field, "memory") messages = self.get_field("messages") if len(messages) % 6 != 0 or len(messages) < 6: return memory = memory.result() recent_summary = "".join(memory["running_summary"][-1:]) old_summary = "\n\n".join(memory["running_summary"][-4:-3] + memory["running_summary"][-7:-6]) message_lookback = 4 previous_messages_text = "" prompt = prompts.long_persist_current_turn_prompt.format(previous_messages=previous_messages_text, previous_summary=recent_summary, older_summary=old_summary) while get_gpt3_word_count(previous_messages_text + "\n\n" + prompt) < 10_000 and message_lookback < 6: previous_messages = messages[-message_lookback:] previous_messages = [{"sender": m["sender"],"text": extract_user_answer(m["text"])} for m in previous_messages] previous_messages_text = '\n\n'.join([f"{m['sender']}:\n'''{m['text']}'''\n" for m in previous_messages]) message_lookback += 2 assert get_gpt3_word_count(previous_messages_text) > 0 llm = CallLLm(self.get_api_keys(), use_gpt4=False, use_16k=True) prompt = prompts.long_persist_current_turn_prompt.format(previous_messages=previous_messages_text, previous_summary=recent_summary, older_summary=old_summary) summary = llm(prompt, temperature=0.2, stream=False) memory["running_summary"][-1] = summary summary_index_new = get_async_future(FAISS.from_texts, [summary], get_embedding_model(self.get_api_keys())) indices = indices.result() _ = indices["summary_index"].merge_from(summary_index_new.result()) mem_set = get_async_future(self.set_field, "memory", memory) self.set_field("indices", indices) mem_set.result() def delete_message(self, message_id, index): get_async_future(self.set_field, "memory", {"last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}) messages = self.get_field("messages") messages = [m for i, m in enumerate(messages) if m["message_id"] != message_id and i != index] self.set_field("messages", messages, overwrite=True) def __call__(self, query): logger.info(f"Called conversation reply for chat Assistant with Query: {query}") for txt in self.reply(query): yield json.dumps(txt)+"\n" def get_uploaded_documents_for_query(self, query): attached_docs = re.findall(r'#doc_\d+', query["messageText"]) attached_docs = list(set(attached_docs)) attached_docs_names = attached_docs attached_docs = [int(d.split("_")[-1]) for d in attached_docs] if len(attached_docs) > 0: # assert that all elements of attached docs are greater than equal to 1. uploaded_documents = self.get_uploaded_documents() attached_docs: List[int] = [d for d in attached_docs if len(uploaded_documents) >= d >= 1] attached_docs: List[DocIndex] = [uploaded_documents[d - 1] for d in attached_docs] doc_infos = [d.title for d in attached_docs] # replace each of the #doc_1, #doc_2 etc with the doc_infos for i, d in enumerate(attached_docs_names): query["messageText"] = query["messageText"].replace(d, f"{d} (Title of {d} '{doc_infos[i]}')\n") return query, attached_docs, attached_docs_names def get_prior_messages_summary(self, query:str)->str: summary_lookback = 8 futures = [get_async_future(self.get_field, "memory"), get_async_future(self.get_field, "messages"), get_async_future(self.get_field, "indices")] memory, messages, indices = [f.result() for f in futures] previous_messages = messages[-16:] previous_messages = [{"sender": m["sender"],"text": extract_user_answer(m["text"])} for m in previous_messages] if len(previous_messages) < 2: return "" prev_msg_text = [] for m in reversed(previous_messages): prev_msg_text.append(f"{m['sender']}:\n'''{m['text']}'''") if get_gpt3_word_count("\n\n".join(prev_msg_text)) > 9000: break previous_messages = "\n\n".join(reversed(prev_msg_text)) running_summary = memory["running_summary"][-1:] older_extensive_summary = find_nearest_divisible_by_three(memory["running_summary"]) if len(memory["running_summary"]) > 4: summary_nodes = indices["summary_index"].similarity_search(query, k=8) summary_nodes = [n.page_content for n in summary_nodes] not_taken_summaries = running_summary + memory["running_summary"][-summary_lookback:] summary_nodes = [n for n in summary_nodes if n not in not_taken_summaries] summary_nodes = [n for n in summary_nodes if len(n.strip()) > 0][-2:] else: summary_nodes = [] if len(running_summary) > 0 and running_summary[0] != older_extensive_summary: running_summary = [older_extensive_summary] + running_summary summary_nodes = summary_nodes + running_summary summary_text = [] for s in reversed(summary_nodes): summary_text.append(s) if get_gpt3_word_count("\n\n".join(summary_text)) > 3_000: break summary_nodes = "\n".join(reversed(summary_text)) prompt = f"""You are information extraction agent who will extract information for answering a query given the previous conversation details between a human and an AI. The user query is as follows: '''{query}''' Extract relevant information that might be useful in answering the above user query from the following conversation messages: '''{previous_messages}''' The summary of the conversation is as follows: '''{summary_nodes}''' Now lets extract relevant information for answering the query from the above conversation messages and summary. Only provide answer from the conversation messages and summary given above. If no relevant information is found in given context, then output "No relevant information found." only. Write the extracted information concisely below: """ final_information = CallLLm(self.get_api_keys(), use_gpt4=False, use_16k=True)(prompt, temperature=0.2, stream=False) # We return a string return final_information @property def max_time_to_wait_for_web_results(self): return 20 def reply(self, query): # Get prior context # Get document context # TODO: plan and pre-critique # TODO: post-critique and improve # TODO: Use gpt-3.5-16K for longer contexts as needed. # TODO: get prior messages and use gpt-3.5 16K for getting a good prior long context for current message. Do this asynchronously. # query payload below, actual query is the messageText get_async_future(self.set_field, "memory", {"last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}) get_async_future(self.create_deep_summary) pattern = r'\[.*?\]\(.*?\)' st = time.time() query["messageText"] = query["messageText"].strip() attached_docs_future = get_async_future(self.get_uploaded_documents_for_query, query) query, attached_docs, attached_docs_names = attached_docs_future.result() answer = '' summary = "".join(self.get_field("memory")["running_summary"][-1:]) checkboxes = query["checkboxes"] enablePreviousMessages = str(checkboxes.get('enable_previous_messages', "infinite")).strip() if enablePreviousMessages == "infinite": message_lookback = 16 else: message_lookback = int(enablePreviousMessages) * 2 previous_context = summary if len(summary.strip()) > 0 and message_lookback >= 0 else '' user_query = query['messageText'] link_context = previous_context + user_query yield {"text": '', "status": "Getting prior chat context ..."} additional_docs_to_read = query["additional_docs_to_read"] searches = [s.strip() for s in query["search"] if s is not None and len(s.strip()) > 0] google_scholar = checkboxes["googleScholar"] provide_detailed_answers = int(checkboxes["provide_detailed_answers"]) original_user_query = user_query from bs4 import BeautifulSoup if provide_detailed_answers == 5 or provide_detailed_answers == 6: with open(os.path.join("XAT-DM-help", "DM_prompt.md"), "r") as f: dm_msg = f.read() query['messageText'] = dm_msg + "\n\n" + remove_bad_whitespaces(BeautifulSoup(query['messageText'], "lxml").text) user_query = query['messageText'] if provide_detailed_answers == 7 or provide_detailed_answers == 8: with open(os.path.join("XAT-DM-help", "VA_prompt.md"), "r") as f: dm_msg = f.read() query['messageText'] = dm_msg + "\n\n" + remove_bad_whitespaces(BeautifulSoup(query['messageText'], "lxml").text) user_query = query['messageText'] perform_web_search = checkboxes["perform_web_search"] or len(searches) > 0 links = [l.strip() for l in query["links"] if l is not None and len(l.strip()) > 0] # and l.strip() not in raw_documents_index prior_chat_summary_future = None unchanged_message_lookback = message_lookback if (google_scholar or perform_web_search or len(links) > 0 or len(attached_docs) > 0 or len( additional_docs_to_read) > 0 or provide_detailed_answers >=3) and message_lookback >= 1 and provide_detailed_answers >=2: prior_chat_summary_future = get_async_future(self.get_prior_messages_summary, query["messageText"]) message_lookback = min(4, message_lookback) web_search_tmp_marker_name = None if google_scholar or perform_web_search: web_search_tmp_marker_name = self.conversation_id + "_web_search" + str(time.time()) create_tmp_marker_file(web_search_tmp_marker_name) logger.info(f"Time to Start Performing web search with chat query with elapsed time as {(time.time() - st):.2f}") yield {"text": '', "status": "performing google scholar search" if google_scholar else "performing web search"} web_results = get_async_future(web_search_queue, user_query, 'helpful ai assistant', previous_context, self.get_api_keys(), datetime.now().strftime("%Y-%m"), extra_queries=searches, gscholar=google_scholar, provide_detailed_answers=provide_detailed_answers, web_search_tmp_marker_name=web_search_tmp_marker_name) if (provide_detailed_answers == 0 or provide_detailed_answers == 1) and (len(links) + len(attached_docs) + len(additional_docs_to_read) == 1 and len( searches) == 0): provide_detailed_answers = 2 # raw_documents_index = self.get_field("raw_documents_index") link_result_text = '' full_doc_texts = {} if len(links) > 0: yield {"text": '', "status": "Reading your provided links."} link_future = get_async_future(read_over_multiple_links, links, [""] * len(links), [link_context] * (len(links)), self.get_api_keys(), provide_detailed_answers=max(0, int(provide_detailed_answers) - 1) or len(links) <= 2) if len(attached_docs) > 0: yield {"text": '', "status": "Reading your attached documents."} conversation_docs_future = get_async_future(get_multiple_answers, query["messageText"], attached_docs, summary if message_lookback >= 0 else '', max(0, int(provide_detailed_answers) - 1), False, True) doc_answer = '' if len(additional_docs_to_read) > 0: yield {"text": '', "status": "reading your documents"} doc_future = get_async_future(get_multiple_answers, query["messageText"], additional_docs_to_read, summary if message_lookback >= 0 else '', max(0, int(provide_detailed_answers) - 1), False) web_text = '' prior_context_future = get_async_future(self.retrieve_prior_context, query["messageText"], links=links if len(links) > 0 else None, required_message_lookback=unchanged_message_lookback) if len(links) > 0: link_read_st = time.time() link_result_text = "We could not read the links you provided. Please try again later." all_docs_info = [] while True and ((time.time() - link_read_st) < self.max_time_to_wait_for_web_results * 6): if (time.time() - link_read_st) > (self.max_time_to_wait_for_web_results * 2): yield {"text": '', "status": "Link reading taking long time ... "} if link_future.done(): link_result_text, all_docs_info = link_future.result() break time.sleep(0.2) read_links = re.findall(pattern, link_result_text) read_links = list(set([link.strip() for link in read_links if len(link.strip())>0])) if len(all_docs_info) > 0: read_links = "\nWe read the below links:\n" + "\n".join([f"{i+1}. {wta}" for i, wta in enumerate(read_links)]) + "\n" yield {"text": read_links, "status": "Finished reading your provided links."} else: read_links = "\nWe could not read any of the links you provided. Please try again later. Timeout at 30s.\n" yield {"text": read_links, "status": "Finished reading your provided links."} yield {"text": "\n", "status": "Finished reading your provided links."} logger.info(f"Time taken to read links: {time.time() - st}") logger.debug(f"Link result text:\n```\n{link_result_text}\n```") qu_dst = time.time() if len(additional_docs_to_read) > 0: doc_answer = '' while True and (time.time() - qu_dst < (self.max_time_to_wait_for_web_results * ((provide_detailed_answers)*5))): if doc_future.done(): doc_answers = doc_future.result() doc_answer = doc_answers[1].result()["text"] break time.sleep(0.2) if len(doc_answer) > 0: yield {"text": '', "status": "document reading completed"} else: yield {"text": '', "status": "document reading failed"} conversation_docs_answer = '' if len(attached_docs) > 0: while True and (time.time() - qu_dst < (self.max_time_to_wait_for_web_results * ((provide_detailed_answers)*5))): if conversation_docs_future.done(): conversation_docs_answer = conversation_docs_future.result()[1].result()["text"] conversation_docs_answer = "\n\n".join([f"For '{ad}' information is given below.\n{cd}" for cd, ad in zip(conversation_docs_answer, attached_docs_names)]) break time.sleep(0.2) if len(conversation_docs_answer) > 0: yield {"text": '', "status": "document reading completed"} else: yield {"text": '', "status": "document reading failed"} llm = CallLLm(self.get_api_keys(), use_gpt4=True) truncate_method = truncate_text_for_gpt4 if llm.self_hosted_model_url is not None: truncate_method = truncate_text_for_others elif not llm.use_gpt4: truncate_method = truncate_text_for_gpt3 prior_context = prior_context_future.result() previous_messages = prior_context["previous_messages"] previous_messages_long = prior_context["previous_messages_long"] new_line = "\n" summary_text = "\n".join(prior_context["summary_nodes"][-2:] if enablePreviousMessages == "infinite" else ( prior_context["summary_nodes"][-1:]) if enablePreviousMessages in ["0", "1", "2"] else []) executed_partial_two_stage_answering = False if perform_web_search or google_scholar: search_results = next(web_results.result()[0].result()) if len(search_results['queries']) > 0: yield {"text": "#### Web searched with Queries: \n", "status": "displaying web search queries ... "} answer += "#### Web searched with Queries: \n" queries = two_column_list(search_results['queries']) answer += (queries + "\n") yield {"text": queries + "\n", "status": "displaying web search queries ... "} if len(search_results['search_results']) > 0: if provide_detailed_answers == 1: cut_off = 6 elif provide_detailed_answers == 2: cut_off = 12 elif provide_detailed_answers == 3: cut_off = 18 elif provide_detailed_answers == 4: cut_off = 24 else: cut_off = 6 query_results_part1 = search_results['search_results'] seen_query_results = query_results_part1[:max(10, cut_off)] unseen_query_results = query_results_part1[max(10, cut_off):] answer += "\n#### Search Results: \n" yield {"text": "\n#### Search Results: \n", "status": "displaying web search results ... "} query_results = [f"<a href='{qr['link']}'>{qr['title']}</a>" for qr in seen_query_results] query_results = two_column_list(query_results) answer += (query_results + "\n") yield {"text": query_results + "\n", "status": "Reading web search results ... "} # if len(unseen_query_results) > 0: # answer += "\n###### Other Search Results: \n" # yield {"text": "\n###### Other Search Results: \n", "status": "displaying web search results ... "} # query_results = [f"<a href='{qr['link']}'>{qr['title']}</a>" for qr in unseen_query_results] # query_results = two_column_list(query_results) # answer += (query_results + "\n") # yield {"text": query_results + "\n", "status": "Reading web search results ... "} result_queue = web_results.result()[1] web_text_accumulator = [] qu_st = time.time() qu_mt = time.time() logger.info(f"Time to get web search links: {(qu_st - st):.2f}") while True: qu_wait = time.time() break_condition = (len(web_text_accumulator) >= (cut_off//1) and provide_detailed_answers <= 2) or (len(web_text_accumulator) >= (cut_off//2) and provide_detailed_answers >= 3) or ((qu_wait - qu_st) > max(self.max_time_to_wait_for_web_results * 2, self.max_time_to_wait_for_web_results * provide_detailed_answers)) if break_condition and result_queue.empty(): break one_web_result = None if not result_queue.empty(): one_web_result = result_queue.get() qu_et = time.time() if one_web_result is None and break_condition: break if one_web_result is None: time.sleep(0.2) continue if one_web_result == TERMINATION_SIGNAL: break if one_web_result["text"] is not None and one_web_result["text"].strip()!="" and len(one_web_result["text"].strip().split()) > LEN_CUTOFF_WEB_TEXT: web_text_accumulator.append(one_web_result["text"]) logger.info(f"Time taken to get {len(web_text_accumulator)}-th web result with len = {len(one_web_result['text'].split())}: {(qu_et - qu_st):.2f}") time.sleep(0.2) time_logger.info(f"Time to get web search results without sorting: {(time.time() - st):.2f} and only web reading time: {(time.time() - qu_st):.2f}") word_count = lambda s: len(s.split()) # Sort the array in reverse order based on the word count web_text_accumulator = sorted(web_text_accumulator, key=word_count, reverse=True) web_text_accumulator = [ws for ws in web_text_accumulator if len(ws.strip().split()) > LEN_CUTOFF_WEB_TEXT and "No relevant information found.".lower() not in ws.lower()] # Join the elements along with serial numbers. if len(web_text_accumulator) >= 4 and provide_detailed_answers > 2: first_stage_cut_off = 8 if provide_detailed_answers == 3 else 12 used_web_text_accumulator_len = len(web_text_accumulator[:first_stage_cut_off]) full_web_string = "" for i, wta in enumerate(web_text_accumulator[:first_stage_cut_off]): web_string = f"{i + 1}.\n{wta}" full_web_string = full_web_string + web_string + "\n\n" if get_gpt4_word_count(full_web_string) > 8000: break web_text = full_web_string read_links = re.findall(pattern, web_text) read_links = list(set([link.strip() for link in read_links if len(link.strip())>0])) if len(read_links) > 0: read_links = "\nWe read the below links:\n" + "\n".join( [f"{i + 1}. {wta}" for i, wta in enumerate(read_links)]) + "\n" yield {"text": read_links, "status": "web search completed"} else: read_links = "\nWe could not read any of the links you provided. Please try again later. Timeout at 30s.\n" yield {"text": read_links, "status": "web search completed"} yield {"text": "\n", "status": "Finished reading few links."} web_text = read_links + "\n" + web_text link_result_text, web_text, doc_answer, summary_text, previous_messages, conversation_docs_answer = truncate_text_for_gpt4_16k( link_result_text, web_text, doc_answer, summary_text, previous_messages, query["messageText"], conversation_docs_answer) web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer = format_llm_inputs( web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text, previous_messages=previous_messages if provide_detailed_answers > 3 else '', permanent_instructions='Include references inline in wikipedia format. Answer concisely and briefly while covering all given references. Keep your answer short, concise and succinct. We will expand the answer later', doc_answer=doc_answer, web_text=web_text, link_result_text=link_result_text, conversation_docs_answer=conversation_docs_answer) llm = CallLLm(self.get_api_keys(), use_gpt4=provide_detailed_answers > 3, use_16k=True) qu_mt = time.time() if len(read_links) > 0: time_logger.info(f"Time taken to start replying (stage 1) for chatbot: {(time.time() - st):.2f}") main_ans_gen = llm(prompt, temperature=0.3, stream=True) answer += "<answer>\n" yield {"text": "<answer>\n", "status": "stage 1 answering in progress"} for txt in main_ans_gen: yield {"text": txt, "status": "stage 1 answering in progress"} answer += txt one_web_result = None if not result_queue.empty(): one_web_result = result_queue.get() if one_web_result is not None and one_web_result != TERMINATION_SIGNAL: if one_web_result["text"] is not None and one_web_result["text"].strip() != "" and len(one_web_result["text"].strip().split()) > LEN_CUTOFF_WEB_TEXT: web_text_accumulator.append(one_web_result["text"]) logger.info(f"Time taken to get {len(web_text_accumulator)}-th web result with len = {len(one_web_result['text'].split())}: {(qu_et - qu_st):.2f}") answer += "</answer>\n" yield {"text": "</answer>\n", "status": "stage 1 answering in progress"} executed_partial_two_stage_answering = True time_logger.info(f"Time taken to end replying (stage 1) for chatbot: {(time.time() - st):.2f}") web_text_accumulator = web_text_accumulator[used_web_text_accumulator_len:] while True: qu_wait = time.time() break_condition = (len(web_text_accumulator) >= (cut_off//2)) or ((qu_wait - qu_mt) > (self.max_time_to_wait_for_web_results * provide_detailed_answers)) if break_condition and result_queue.empty(): break one_web_result = None if not result_queue.empty(): one_web_result = result_queue.get() qu_et = time.time() if one_web_result is None and break_condition: break if one_web_result is None: time.sleep(0.2) continue if one_web_result == TERMINATION_SIGNAL: break if one_web_result["text"] is not None and one_web_result["text"].strip()!="" and len(one_web_result["text"].strip().split()) > LEN_CUTOFF_WEB_TEXT: web_text_accumulator.append(one_web_result["text"]) logger.info(f"Time taken to get {len(web_text_accumulator)}-th web result with len = {len(one_web_result['text'].split())}: {(qu_et - qu_st):.2f}") time.sleep(0.2) web_text_accumulator = sorted(web_text_accumulator, key=word_count, reverse=True) elif provide_detailed_answers > 2: while True: qu_wait = time.time() break_condition = (len(web_text_accumulator) >= cut_off) or ((qu_wait - qu_mt) > (self.max_time_to_wait_for_web_results * provide_detailed_answers)) if break_condition and result_queue.empty(): break one_web_result = None if not result_queue.empty(): one_web_result = result_queue.get() qu_et = time.time() if one_web_result is None and break_condition: break if one_web_result is None: time.sleep(0.2) continue if one_web_result == TERMINATION_SIGNAL: break if one_web_result["text"] is not None and one_web_result["text"].strip()!="" and len(one_web_result["text"].strip().split()) > LEN_CUTOFF_WEB_TEXT: web_text_accumulator.append(one_web_result["text"]) logger.info(f"Time taken to get {len(web_text_accumulator)}-th web result with len = {len(one_web_result['text'].split())}: {(qu_et - qu_st):.2f}") time.sleep(0.2) web_text_accumulator = sorted(web_text_accumulator, key=word_count, reverse=True) full_web_string = "" web_text_accumulator = [ws for ws in web_text_accumulator if len(ws.strip().split()) > LEN_CUTOFF_WEB_TEXT and "No relevant information found.".lower() not in ws.lower()] for i, wta in enumerate(web_text_accumulator): web_string = f"{i + 1}.\n{wta}" full_web_string = full_web_string + web_string + "\n\n" if get_gpt4_word_count(full_web_string) > 12000: break web_text = full_web_string # web_text = "\n\n".join(web_text_accumulator) read_links = re.findall(pattern, web_text) read_links = list(set([link.strip() for link in read_links if len(link.strip())>0])) if len(read_links) > 0: read_links = "\nWe read the below links:\n" + "\n".join([f"{i+1}. {wta}" for i, wta in enumerate(read_links)]) + "\n" yield {"text": read_links, "status": "web search completed"} else: read_links = "\nWe could not read any of the links you provided. Please try again later. Timeout at 30s.\n" yield {"text": read_links, "status": "web search completed"} yield {"text": "\n", "status": "Finished reading your provided links."} web_text = read_links + "\n" + web_text time_logger.info(f"Time to get web search results with sorting: {(time.time() - st):.2f}") if (len(read_links) <= 1 and len(web_text.split()) < 200) and len(links)==0 and len(attached_docs) == 0 and len(additional_docs_to_read)==0: yield {"text": '', "status": "saving answer ..."} remove_tmp_marker_file(web_search_tmp_marker_name) get_async_future(self.persist_current_turn, query["messageText"], answer, full_doc_texts) return # TODO: if number of docs to read is <= 1 then just retrieve and read here, else use DocIndex itself to read and retrieve. remove_tmp_marker_file(web_search_tmp_marker_name) if (len(links)==1 and len(attached_docs) == 0 and len(additional_docs_to_read)==0 and not (google_scholar or perform_web_search) and provide_detailed_answers <= 2 and unchanged_message_lookback<=-1): text = link_result_text.split("Raw article text:")[0].replace("Relevant additional information from other documents with url links, titles and useful context are mentioned below:", "").replace("'''", "").replace('"""','').strip() yield {"text": text, "status": "answering in progress"} answer += text yield {"text": '', "status": "saving answer ..."} get_async_future(self.persist_current_turn, query["messageText"], answer, full_doc_texts) return if (len(links)==0 and len(attached_docs) == 0 and len(additional_docs_to_read)==1 and not (google_scholar or perform_web_search) and provide_detailed_answers <= 2 and unchanged_message_lookback<=-1): text = doc_answer.split("Raw article text:")[0].replace("Relevant additional information from other documents with url links, titles and useful context are mentioned below:", "").replace("'''", "").replace('"""','').strip() yield {"text": text, "status": "answering in progress"} answer += text yield {"text": '', "status": "saving answer ..."} get_async_future(self.persist_current_turn, query["messageText"], answer, full_doc_texts) return if (len(links)==0 and len(attached_docs) == 1 and len(additional_docs_to_read)==0 and not (google_scholar or perform_web_search) and provide_detailed_answers <= 2 and unchanged_message_lookback<=-1): text = conversation_docs_answer.split("Raw article text:")[0].replace("Relevant additional information from other documents with url links, titles and useful context are mentioned below:", "").replace("'''", "").replace('"""','').strip() text = "\n".join(text.replace("The documents that were read are as follows:", "").split("\n")[2:]) yield {"text": text, "status": "answering in progress"} answer += text yield {"text": '', "status": "saving answer ..."} get_async_future(self.persist_current_turn, query["messageText"], answer, full_doc_texts) return if (len(web_text.split()) < 200 and (google_scholar or perform_web_search)) and len(links) == 0 and len(attached_docs) == 0 and len(additional_docs_to_read) == 0 and provide_detailed_answers >= 3: yield {"text": '', "status": "saving answer ..."} get_async_future(self.persist_current_turn, query["messageText"], answer, full_doc_texts) return yield {"text": '', "status": "getting previous context"} all_expert_answers = "" if provide_detailed_answers in [4, 6, 8] and not executed_partial_two_stage_answering and len(links) == 0 and len(attached_docs) == 0 and len(additional_docs_to_read) == 0 and not (google_scholar or perform_web_search): expert_st = time.time() logger.info(f"Trying MOE at {(time.time() - st):.2f}") yield {"text": '', "status": "Asking experts to answer ..."} link_result_text_expert, web_text_expert, doc_answer_expert, summary_text_expert, previous_messages_expert, conversation_docs_answer_expert = truncate_text_for_gpt4( link_result_text, web_text, doc_answer, summary_text, previous_messages, query["messageText"], conversation_docs_answer) web_text_expert, doc_answer_expert, link_result_text_expert, summary_text_expert, previous_messages_expert, conversation_docs_answer_expert = format_llm_inputs( web_text_expert, doc_answer_expert, link_result_text_expert, summary_text_expert, previous_messages_expert, conversation_docs_answer_expert) doc_answer_expert = f"Answers from user's stored documents:\n'''{doc_answer_expert}'''\n" if len( doc_answer_expert.strip()) > 0 else '' web_text_expert = f"Answers from web search:\n'''{web_text_expert}'''\n" if len(web_text_expert.strip()) > 0 else '' link_result_text_expert = f"Answers from web links provided by the user:\n'''{link_result_text_expert}'''\n" if len( link_result_text_expert.strip()) > 0 else '' link_result_text_expert_16k, web_text_expert_16k, doc_answer_expert_16k, summary_text_expert_16k, previous_messages_expert_16k, conversation_docs_answer_expert_16k = truncate_text_for_gpt4_16k( link_result_text, web_text, doc_answer, summary_text, previous_messages_long, query["messageText"], conversation_docs_answer) web_text_expert_16k, doc_answer_expert_16k, link_result_text_expert_16k, summary_text_expert_16k, previous_messages_expert_16k, conversation_docs_answer_expert_16k = format_llm_inputs( web_text_expert_16k, doc_answer_expert_16k, link_result_text_expert_16k, summary_text_expert_16k, previous_messages_expert_16k, conversation_docs_answer_expert_16k) doc_answer_expert_16k = f"Answers from user's stored documents:\n'''{doc_answer_expert_16k}'''\n" if len( doc_answer_expert_16k.strip()) > 0 else '' web_text_expert_16k = f"Answers from web search:\n'''{web_text_expert_16k}'''\n" if len(web_text_expert_16k.strip()) > 0 else '' link_result_text_expert_16k = f"Answers from web links provided by the user:\n'''{link_result_text_expert_16k}'''\n" if len( link_result_text_expert_16k.strip()) > 0 else '' prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an expert in literature, psychology, history and philosophy. Answer the query in a way that is understandable to a layman. Answer quickly and briefly. Write your reasoning and approach in short before writing your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="mistralai/mixtral-8x7b-instruct", use_gpt4=False, use_16k=False) ans_gen_1_future = get_async_future(llm, prompt, temperature=0.9, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an expert in mathematics, logical reasoning, science and programming. Provide a logical and well thought out answer that is grounded and factual. Answer shortly and simply. Write your logic, reasoning and problem solving process first before you mention your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="anthropic/claude-2.0", use_gpt4=True, use_16k=False) ans_gen_2_future = get_async_future(llm, prompt, temperature=0.5, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an experience business leader with an MBA from XLRI institute in India. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. Answer concisely and briefly. First, put forth your reasoning and decision making process in short, then write your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="anthropic/claude-v1", use_gpt4=True, use_16k=False) ans_gen_3_future = get_async_future(llm, prompt, temperature=0.9, stream=False) #### prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an expert in social sciences, simplicity, arts, teaching, sports, ethics, responsible AI, safety, gender studies and communication. Provide your reasoning, approach and thought process in short before writing your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="google/palm-2-chat-bison", use_gpt4=False, use_16k=False) # cognitivecomputations/dolphin-mixtral-8x7b ans_gen_4_future = get_async_future(llm, prompt, temperature=0.9, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert, previous_messages=previous_messages_expert, permanent_instructions="You are an expert in physics, biology, medicine, chess, puzzle solving, jeopardy, trivia and video games. Provide a clear, short and simple answer that is realistic and factual. Answer shortly and simply. Explain your logic, reasoning and problem solving process shortly before you mention your answer.", doc_answer=doc_answer_expert, web_text=web_text_expert, link_result_text=link_result_text_expert, conversation_docs_answer=conversation_docs_answer_expert) llm = CallLLm(self.get_api_keys(), use_gpt4=True, use_16k=False) ans_gen_5_future = get_async_future(llm, prompt, temperature=0.5, stream=False, model_family="gpt-4-0613") prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert, previous_messages=previous_messages_expert, permanent_instructions="You are an experienced educator with an MBA from XLRI institute in India. You help students prepare for MBA exams like XAT and GMAT. Write quickly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. We are in a hurry so put forth your reasoning and decision making process in short, then write your answer.", doc_answer=doc_answer_expert, web_text=web_text_expert, link_result_text=link_result_text_expert, conversation_docs_answer=conversation_docs_answer_expert) llm = CallLLm(self.get_api_keys(), use_gpt4=True, use_16k=False) ans_gen_6_future = get_async_future(llm, prompt, temperature=0.9, stream=False, model_family="gpt-4-0314") ### prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. Write briefly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. First, put forward your reasoning and decision making process very shortly, then write your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="google/gemini-pro", use_gpt4=False, use_16k=False) ans_gen_7_future = get_async_future(llm, prompt, temperature=0.9, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an research scholar in social sciences, arts, teaching, sports, ethics, responsible AI, safety, gender studies and communication. Answer the query in an easy to understand manner. Explain your reasoning, approach and thought process briefly before writing your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="anthropic/claude-2", use_gpt4=True, use_16k=False) ans_gen_8_future = get_async_future(llm, prompt, temperature=0.9, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. Write briefly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. First, put forward your reasoning and decision making process in short, then write your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="anthropic/claude-v1", use_gpt4=False, use_16k=False) ans_gen_9_future = get_async_future(llm, prompt, temperature=0.4, stream=False) prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text_expert_16k, previous_messages=previous_messages_expert_16k, permanent_instructions="You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. First, put forward your reasoning and decision making process in short, then write your answer.", doc_answer=doc_answer_expert_16k, web_text=web_text_expert_16k, link_result_text=link_result_text_expert_16k, conversation_docs_answer=conversation_docs_answer_expert_16k) llm = CallLLmOpenRouter(self.get_api_keys(), model_name="nousresearch/nous-capybara-34b", use_gpt4=False, use_16k=False) ans_gen_10_future = get_async_future(llm, prompt, temperature=0.4, stream=False) while True: qu_wait = time.time() num_done = (1 if ans_gen_1_future.done() and ans_gen_1_future.exception() is None else 0) + (1 if ans_gen_2_future.done() and ans_gen_2_future.exception() is None else 0) + (1 if ans_gen_3_future.done() and ans_gen_3_future.exception() is None else 0) + (1 if ans_gen_4_future.done() and ans_gen_4_future.exception() is None else 0) + (1 if ans_gen_5_future.done() and ans_gen_5_future.exception() is None else 0) + (1 if ans_gen_6_future.done() and ans_gen_6_future.exception() is None else 0) + (1 if ans_gen_7_future.done() and ans_gen_7_future.exception() is None else 0) + (1 if ans_gen_8_future.done() and ans_gen_8_future.exception() is None else 0) + (1 if ans_gen_9_future.done() and ans_gen_9_future.exception() is None else 0) + (1 if ans_gen_10_future.done() and ans_gen_10_future.exception() is None else 0) break_condition = num_done >= 6 or ((qu_wait - expert_st) > (self.max_time_to_wait_for_web_results * 2)) if break_condition: break time.sleep(0.2) # Get results of those experts that are done by now. futures = [ans_gen_1_future, ans_gen_2_future, ans_gen_3_future, ans_gen_4_future, ans_gen_5_future, ans_gen_6_future, ans_gen_7_future, ans_gen_8_future, ans_gen_9_future, ans_gen_10_future] model_names = ["mixtral", "claude-2.0", "claude-v1", "palm-2", "gpt-4-0613", "gpt-4-0314", "gemini-pro", "claude-2.1", "claude-v1.1", "capybara"] for ix, (future, mdn) in enumerate(zip(futures, model_names)): if future.done() and future.exception() is None and isinstance(future.result(), str) and len(future.result().strip().split()) > 20: all_expert_answers += "\n\n" + f"<b>Student #{ix + 1}:</b> `{mdn}` answer's:\n<small>{remove_bad_whitespaces(future.result().strip())}</small>" all_expert_answers += "\n\n" # all_expert_answers = (f"First expert's answer: ```{ans_gen_1_future.result()}```" if ans_gen_1_future.exception() is None else '') + "\n\n" + (f"Second expert's answer: ```{ans_gen_2_future.result()}```" if ans_gen_2_future.exception() is None else '') + "\n\n" + (f"Third expert's answer: ```{ans_gen_3_future.result()}```" if ans_gen_3_future.exception() is None else '') # all_expert_answers += "\n\n" + (f"Fourth expert's answer: ```{ans_gen_4_future.result()}```" if ans_gen_4_future.exception() is None else '') + "\n\n" + (f"Fifth expert's answer: ```{ans_gen_5_future.result()}```" if ans_gen_5_future.exception() is None else '') + "\n\n" + (f"Sixth expert's answer: ```{ans_gen_6_future.result()}```" if ans_gen_6_future.exception() is None else '') logger.info(f"Experts answer len = {len(all_expert_answers.split())}, Ending MOE at {(time.time() - st):.2f}") answer += all_expert_answers yield {"text": all_expert_answers, "status": "Expert anwers received ..."} prior_chat_summary = "" wt_prior_ctx = time.time() while time.time() - wt_prior_ctx < 30 and prior_chat_summary_future is not None: if prior_chat_summary_future.done() and not prior_chat_summary_future.exception(): prior_chat_summary = prior_chat_summary_future.result() break time.sleep(0.2) time_logger.info(f"Time to wait for prior context with 16K LLM: {(time.time() - wt_prior_ctx):.2f}") summary_text = prior_chat_summary + "\n" + summary_text yield {"text": '', "status": "Preparing prompt context ..."} link_result_text, web_text, doc_answer, summary_text, previous_messages, conversation_docs_answer = truncate_text_for_gpt4_32k( link_result_text, web_text, doc_answer, summary_text, previous_messages_long, query["messageText"], conversation_docs_answer) web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer = format_llm_inputs( web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer) doc_answer = f"Answers from user's stored documents:\n'''{doc_answer}'''\n" if len( doc_answer.strip()) > 0 else '' web_text = f"Answers from web search:\n'''{web_text}'''\n" if len(web_text.strip()) > 0 else '' link_result_text = f"Answers from web links provided by the user:\n'''{link_result_text}'''\n" if len( link_result_text.strip()) > 0 else '' yield {"text": '', "status": "Preparing partial answer / expert answer context ..."} partial_answer_text = f"We have written a partial answer for the query as below:\n'''\n{answer}\n'''\nTake the partial answer into consideration and continue from there using the new resources provided and your own knowledge. Don't repeat the partial answer.\n" if executed_partial_two_stage_answering else "" partial_answer_text = (f"We have answers from different students:\n```\n{all_expert_answers}\n```\nPerform your own analysis independently. First Provide your own thoughts and answer then combine your answer and thoughts with the student's opinions and provide a final appropriate answer.\n" + partial_answer_text) if len(all_expert_answers.strip()) > 0 else partial_answer_text yield {"text": '', "status": "Preparing prompt ..."} prompt = prompts.chat_slow_reply_prompt.format(query=query["messageText"], summary_text=summary_text, previous_messages=previous_messages, permanent_instructions=partial_answer_text, doc_answer=doc_answer, web_text=web_text, link_result_text=link_result_text, conversation_docs_answer=conversation_docs_answer) yield {"text": '', "status": "starting answer generation"} llm = CallLLm(self.get_api_keys(), use_gpt4=True, use_16k=True) main_ans_gen = llm(prompt, temperature=0.3, stream=True) logger.info( f"""Starting to reply for chatbot, prompt length: {len(enc.encode(prompt))}, llm extracted prior chat info len: {len(enc.encode(prior_chat_summary))}, summary text length: {len(enc.encode(summary_text))}, last few messages length: {len(enc.encode(previous_messages))}, doc answer length: {len(enc.encode(doc_answer))}, conversation_docs_answer length: {len(enc.encode(conversation_docs_answer))}, web text length: {len(enc.encode(web_text))}, link result text length: {len(enc.encode(link_result_text))}""") et = time.time() time_logger.info(f"Time taken to start replying for chatbot: {(et - st):.2f}") if len(doc_answer) > 0: logger.debug(f"Doc Answer: {doc_answer}") if len(web_text) > 0: logger.debug(f"Web text: {web_text}") answer += "<answer>\n" yield {"text": "<answer>\n", "status": "stage 2 answering in progress"} for txt in main_ans_gen: yield {"text": txt, "status": "answering in progress"} answer += txt answer += "</answer>\n" yield {"text": "</answer>\n", "status": "answering ended ..."} time_logger.info(f"Time taken to reply for chatbot: {(time.time() - et):.2f}, total time: {(time.time() - st):.2f}") answer = answer.replace(prompt, "") yield {"text": '', "status": "saving answer ..."} if perform_web_search or google_scholar: search_results = next(web_results.result()[0].result()) yield {"text": query_results + "\n", "status": "Showing all results ... "} if search_results["type"] == "end": full_results = search_results["full_results"] answer += "\n#### All Search Results: \n" yield {"text": "\n#### All Search Results: \n", "status": "displaying web search results ... "} query_results = [f"<a href='{qr['link']}'>{qr['title']} [{qr['count']}]</a>" for qr in full_results] query_results = two_column_list(query_results) answer += (query_results + "\n") yield {"text": query_results + "\n", "status": "Showing all results ... "} yield {"text": '', "status": "saving message ..."} get_async_future(self.persist_current_turn, original_user_query, answer, full_doc_texts) def get_last_ten_messages(self): return self.get_field("messages")[-10:] def get_message_list(self): return self.get_field("messages") def get_metadata(self): memory = self.get_field("memory") return dict(conversation_id=self.conversation_id, user_id=self.user_id, title=memory["title"], summary_till_now="".join(memory["running_summary"][-1:]), last_updated=memory["last_updated"].strftime("%Y-%m-%d %H:%M:%S") if isinstance(memory["last_updated"], datetime) else memory["last_updated"]) def delete_last_turn(self): messages = self.get_field("messages") messages = messages[:-2] self.set_field("messages", messages, overwrite=True) memory = self.get_field("memory") memory["last_updated"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") memory["running_summary"] = memory["running_summary"][:-1] self.set_field("memory", memory, overwrite=True) indices = self.get_field("indices") # TODO: delete from index as well def get_api_keys(self): logger.debug( f"get api keys for self hash = {hash(self)} and doc_id = {self.conversation_id}") if hasattr(self, "api_keys"): api_keys = deepcopy(self.api_keys) else: raise AttributeError("No attribute named `api_keys`.") return api_keys def set_api_keys(self, api_keys: dict): assert isinstance(api_keys, dict) indices = self.get_field("indices") for k, j in indices.items(): if isinstance(j, (FAISS, VectorStore)): j.embedding_function = get_embedding_model(api_keys).embed_query j.embedding_function.__self__.openai_api_key = api_keys["openAIKey"] setattr(j.embedding_function.__self__, "openai_api_key", api_keys["openAIKey"]) setattr(self, "api_keys", api_keys) def __copy__(self): # Create a new instance of our class cls = self.__class__ result = cls.__new__(cls) # Copy all attributes from self to result. This is a shallow copy. result.__dict__.update(self.__dict__) for k in self.store_separate: if hasattr(result, k): setattr(result, k, None) if hasattr(result, "api_keys"): result.api_keys = deepcopy(self.api_keys) return result def copy(self): return self.__copy__() def format_llm_inputs(web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer): web_text = f"""Relevant information from other documents with url links, titles and useful document context are mentioned below:\n\n'''{web_text}''' Remember to refer to all the documents provided above in markdown format (like `[title](link) information from document`).""" if len( web_text) > 0 else "" doc_answer = f"""Results from user provided documents are given below. Questions user has asked usually pertain to these documents. Relevant information from user given documents with url links, titles and useful context are mentioned below:\n\n'''{doc_answer}'''""" if len( doc_answer) > 0 else "" link_result_text = f"""Results from user provided links are given below. Questions user has asked usually pertain to these links. Relevant information from user given links with url links, titles and useful context are mentioned below:\n\n'''{link_result_text}'''""" if len( link_result_text) > 0 else "" summary_text = f"""The summary of the conversation is as follows: '''{summary_text}'''""" if len(summary_text) > 0 else '' previous_messages = f"""Previous chat history between user and assistant:\n'''{previous_messages}'''""" if len(previous_messages) > 0 else '' conversation_docs_answer = f"""The documents that were read are as follows: '''{conversation_docs_answer}'''""" if len(conversation_docs_answer) > 0 else '' return web_text, doc_answer, link_result_text, summary_text, previous_messages, conversation_docs_answer def truncate_text_for_gpt3(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer): return truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-3.5-turbo") def truncate_text_for_gpt4(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer): return truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-4") def truncate_text_for_gpt4_16k(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer): return truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-4-16k") def truncate_text_for_gpt4_32k(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer): return truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-4-32k") def truncate_text_for_gpt3_16k(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer): return truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-3.5-turbo-16k") def truncate_text(link_result_text, web_text, doc_answer, summary_text, previous_messages, user_message, conversation_docs_answer, model="gpt-4"): enc = tiktoken.encoding_for_model(model) if model == "gpt-4": l1 = 7000 l2 = 1000 l4 = 1250 elif model == "gpt-4-16k": l1 = 10000 l2 = 2000 l4 = 2500 elif model == "gpt-3.5-turbo-16k": l1 = 10000 l2 = 2000 l4 = 2500 elif model == "gpt-4-32k": l1 = 24000 l2 = 8000 l4 = 5000 else: l1 = 2000 l2 = 500 l4 = 500 message_space = max(l2, l1 - len(enc.encode(user_message + conversation_docs_answer + link_result_text + doc_answer + web_text + summary_text)) - 750) previous_messages = get_first_last_parts(previous_messages, 0, message_space) summary_space = max(l4, l1 - len(enc.encode(user_message + previous_messages + conversation_docs_answer + link_result_text + doc_answer + web_text)) - 750) summary_text = get_first_last_parts(summary_text, 0, summary_space) ctx_len_allowed = l1 - len(enc.encode(user_message + previous_messages + summary_text)) conversation_docs_answer = get_first_last_parts(conversation_docs_answer, 0, ctx_len_allowed) link_result_text = get_first_last_parts(link_result_text, 0, ctx_len_allowed - len(enc.encode(conversation_docs_answer))) doc_answer = get_first_last_parts(doc_answer, 0, ctx_len_allowed - len(enc.encode(link_result_text + conversation_docs_answer))) web_text = get_first_last_parts(web_text, 0, ctx_len_allowed - len(enc.encode(link_result_text + doc_answer + conversation_docs_answer))) return link_result_text, web_text, doc_answer, summary_text, previous_messages, conversation_docs_answer truncate_text_for_others = truncate_text_for_gpt4 import re def extract_user_answer(text): # Pattern to find <answer>...</answer> segments pattern = r'<answer>(.*?)</answer>' # Find all occurrences of the pattern answers = re.findall(pattern, text, re.DOTALL) # Check if any answers were found within tags if answers: # Joining all extracted answers (in case there are multiple <answer> segments) return ' '.join(answers).strip() else: # If no <answer> tags are found, return the entire text return text.strip()
[ "You are an experienced educator with an MBA from XLRI institute in India. You help students prepare for MBA exams like XAT and GMAT. Write quickly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. We are in a hurry so put forth your reasoning and decision making process in short, then write your answer.", "You are given conversation details between a human and an AI. You will write a title for this conversation. \nPLACEHOLDER\nThe last 2 messages of the conversation are as follows:\nUser query: '''PLACEHOLDER'''\nSystem response: '''PLACEHOLDER'''\n\nNow lets write a title of the conversation.\nTitle of the conversation:\n", "You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. Write briefly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. First, put forward your reasoning and decision making process very shortly, then write your answer.", "messageText", "You are an expert in literature, psychology, history and philosophy. Answer the query in a way that is understandable to a layman. Answer quickly and briefly. Write your reasoning and approach in short before writing your answer.", "You are information extraction agent who will extract information for answering a query given the previous conversation details between a human and an AI. \nThe user query is as follows:\n'''PLACEHOLDER'''\n\nExtract relevant information that might be useful in answering the above user query from the following conversation messages:\n'''PLACEHOLDER'''\n\nThe summary of the conversation is as follows:\n'''PLACEHOLDER'''\n\nNow lets extract relevant information for answering the query from the above conversation messages and summary.\nOnly provide answer from the conversation messages and summary given above. If no relevant information is found in given context, then output \"No relevant information found.\" only.\nWrite the extracted information concisely below:\n", "Include references inline in wikipedia format. Answer concisely and briefly while covering all given references. Keep your answer short, concise and succinct. We will expand the answer later", "You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. First, put forward your reasoning and decision making process in short, then write your answer.", "You are an research scholar in social sciences, arts, teaching, sports, ethics, responsible AI, safety, gender studies and communication. Answer the query in an easy to understand manner. Explain your reasoning, approach and thought process briefly before writing your answer.", "running_summary", "You are an expert in physics, biology, medicine, chess, puzzle solving, jeopardy, trivia and video games. Provide a clear, short and simple answer that is realistic and factual. Answer shortly and simply. Explain your logic, reasoning and problem solving process shortly before you mention your answer.", "You are an expert in social sciences, simplicity, arts, teaching, sports, ethics, responsible AI, safety, gender studies and communication. Provide your reasoning, approach and thought process in short before writing your answer.", "You are an experienced teacher with an MBA from XLRI institute in India. You assist students prepare for MBA entrance exams like XAT and GMAT. Write briefly and shortly, we are in a hurry. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. First, put forward your reasoning and decision making process in short, then write your answer.", "You are an expert in mathematics, logical reasoning, science and programming. Provide a logical and well thought out answer that is grounded and factual. Answer shortly and simply. Write your logic, reasoning and problem solving process first before you mention your answer.", "You are an experience business leader with an MBA from XLRI institute in India. Think how the XAT XLRI examiner thinks and provide solutions as you would for a business decision making question. Answer concisely and briefly. First, put forth your reasoning and decision making process in short, then write your answer." ]
2024-01-10
faizanahemad/science-reader
common.py
import random import tempfile import asyncio import threading import traceback from playwright.async_api import async_playwright from concurrent.futures import ThreadPoolExecutor, as_completed, Future, ProcessPoolExecutor from urllib.parse import urlparse, urlunparse import time import logging import sys import os import re import inspect from more_itertools import peekable import types import pickle import dill import collections import threading import requests from multiprocessing import Process, Queue from functools import partial from tenacity import RetryError FINISHED_TASK = TERMINATION_SIGNAL = "TERMINATION_SIGNAL" SMALL_CHUNK_LEN = 192 LARGE_CHUNK_LEN = 512 TOKEN_LIMIT_FOR_DETAILED = int(os.getenv("TOKEN_LIMIT_FOR_DETAILED", 13000)) TOKEN_LIMIT_FOR_SHORT = int(os.getenv("TOKEN_LIMIT_FOR_SHORT", 2800)) MODEL_TOKENS_SMART = int(os.getenv("MODEL_TOKENS_SMART", 7500)) MODEL_TOKENS_DUMB = int(os.getenv("MODEL_TOKENS_DUMB", 3500)) DDOS_PROTECTION_STR = "Blocked by ddos protection" PDF_CONVERT_URL = os.getenv("PDF_CONVERT_URL", "http://localhost:7777/forms/libreoffice/convert") import requests import os def is_int(s): try: int(s) return True except ValueError: return False def is_picklable(obj): try: pickle.dumps(obj) return True except (pickle.PickleError, TypeError): return False return False def is_dillable(obj): try: dill.dumps(obj) return True except (TypeError, AttributeError): return False return False logger = logging.getLogger(__name__) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler(os.path.join(os.getcwd(), "log.txt")) ] ) logger.setLevel(logging.INFO) time_logger = logging.getLogger(__name__ + " | TIMING") time_logger.setLevel(logging.INFO) # Set log level for this logger def convert_doc_to_pdf(file_path, output_path): api_url = PDF_CONVERT_URL try: logger.info(f"Converting doc at {file_path} to pdf, file exists = {os.path.exists(file_path)}") assert os.path.exists(file_path) with open(file_path, 'rb') as f: files = {'files': (os.path.basename(file_path), f)} payload = {'pdfFormat': 'PDF/A-1a'} r = requests.post(api_url, files=files, data=payload) if r.status_code == 200: with open(output_path, 'wb') as out_file: out_file.write(r.content) return True else: print(f"Conversion failed with status code {r.status_code}") return False except Exception as e: exc = traceback.format_exc() logger.error(f"Exception converting doc at {file_path} to pdf: {e}\n{exc}") return False class RunThread(threading.Thread): def __init__(self, func, args, kwargs): """ https://stackoverflow.com/questions/55409641/asyncio-run-cannot-be-called-from-a-running-event-loop-when-using-jupyter-no """ self.func = func self.args = args self.kwargs = kwargs self.result = None super().__init__() def run(self): self.result = asyncio.run(self.func(*self.args, **self.kwargs)) def run_async(func, *args, **kwargs): try: loop = asyncio.get_running_loop() except RuntimeError: loop = None if loop and loop.is_running(): thread = RunThread(func, args, kwargs) thread.start() thread.join() return thread.result else: return asyncio.run(func(*args, **kwargs)) class RunProcess(Process): def __init__(self, func, args, kwargs): self.func = func self.args = args self.kwargs = kwargs self.queue = Queue() super().__init__() def run(self): result = asyncio.run(self.func(*self.args, **self.kwargs)) self.queue.put(result) def run_async_process(func, *args, **kwargs): try: loop = asyncio.get_running_loop() except RuntimeError: loop = None if loop and loop.is_running(): process = RunProcess(func, args, kwargs) process.start() process.join() return process.queue.get() else: return asyncio.run(func(*args, **kwargs)) executor = ThreadPoolExecutor(max_workers=256) def make_async(fn): def async_fn(*args, **kwargs): func_part = partial(fn, *args, **kwargs) future = executor.submit(func_part) return future return async_fn def get_async_future(fn, *args, **kwargs): # Make your function async afn = make_async(fn) # This will return a Future object, you can call .result() on it to get the result future = afn(*args, **kwargs) return future def wrap_in_future(s): future = Future() future.set_result(s) return future def execute_in_new_process(function, *args, **kwargs): logger.debug(f"type args = {type(args)}, type kwargs = {type(kwargs)}, Pickle able:: function = {is_picklable(function)}, {is_picklable(args)}, {is_picklable(kwargs)}, Is Dill able:: function = {is_dillable(function)}, {is_dillable(args)}, {is_dillable(kwargs)}") submit_st = time.time() with ProcessPoolExecutor(max_workers=1) as executor: future = executor.submit(function, *args, **kwargs) submit_et = time.time() logger.info(f"Stuck on ProcessPoolExecutor for {(submit_et - submit_st):.2f} sec , done future state = {future.done()}") return future def execute_in_new_thread(function, *args, **kwargs): logger.debug( f"type args = {type(args)}, type kwargs = {type(kwargs)}, Pickle able:: function = {is_picklable(function)}, {is_picklable(args)}, {is_picklable(kwargs)}, Is Dill able:: function = {is_dillable(function)}, {is_dillable(args)}, {is_dillable(kwargs)}") submit_st = time.time() with ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(function, *args, **kwargs) submit_et = time.time() logger.info( f"Stuck on ProcessPoolExecutor for {(submit_et - submit_st):.2f} sec , done future state = {future.done()}") return future def call_api_parallel(api_calls, fn, max_workers=4): with ThreadPoolExecutor(max_workers=max_workers) as executor: # Submit tasks and collect Future objects futures = [executor.submit(fn, **api_call) for api_call in api_calls] # Collect results in order of input tasks results = [future.result() for future in futures] return results def call_api_parallel_multi_fn(api_calls, fns): assert len(api_calls) == len(fns) with ThreadPoolExecutor(max_workers=4) as executor: # Submit tasks and collect Future objects futures = [executor.submit(fn, **api_call) for fn, api_call in zip(fns, api_calls)] # Collect results in order of input tasks results = [future.result() for future in futures] return results def round_robin(arr, randomize=True): if randomize: random.shuffle(arr) while True: for item in arr: yield item def timer(func): def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() time_logger.info(f"Execution time of {func.__name__}: {end_time - start_time} seconds, result type: {type(result)}, {('result length:' + str(len(result))) if hasattr(result, '__len__') and isinstance(result, str) else ''}") return result return wrapper def streaming_timer(func): def wrapper(*args, **kwargs): start_time = time.time() accum = '' for r in func(*args, **kwargs): yield r accum = accum + r end_time = time.time() time_logger.info(f"Execution time of {func.__name__}: {end_time - start_time} seconds") return wrapper def print_nested(val, nesting = -5): if isinstance(val, dict): print('') nesting += 5 print(nesting * ' ', end='') print(type(val)) for k in val: print(nesting * ' ', end='') print(k, end=':') print_nested(val[k],nesting) elif isinstance(val, (tuple, list)) and len(val) > 0 and isinstance(val[0], (dict, tuple, list)): nesting += 5 print('') print(nesting * ' ', end='') print(type(val), end=":") print_nested(val[0], nesting) else: print(type(val)) class AddAttribute: def __init__(self, attribute, value): self.attribute = attribute self.value = value def __call__(self, func): setattr(func, self.attribute, self.value) return func def NoneToDefault(x, default=[]): if x is None: return default else: return x def checkNoneOrEmpty(x): if x is None: return True elif isinstance(x, str): return len(x.strip())==0 elif isinstance(x, str) and x.strip().lower() in ['null', 'none']: return x.strip().lower() in ['null', 'none'] else: return len(x) == 0 def combine_array_two_at_a_time(array, sep=' '): result = [] if len(array) % 2 == 1: array.append('') for i in range(0, len(array), 2): result.append(array[i] + f'{sep}' + array[i+1]) return result def concat_array_two_at_a_time(array): result = [] if len(array) % 2 == 1: array.append('') for i in range(0, len(array), 2): result.append([array[i],array[i+1]]) return result def make_stream(res, do_stream): is_generator = inspect.isgenerator(res) if is_generator: res = check_if_stream_and_raise_exception(res) if do_stream and not is_generator: assert isinstance(res, (str, list, tuple)) return convert_iterable_to_stream(res) elif not do_stream and is_generator: return convert_stream_to_iterable(res) return res def call_with_stream(fn, do_stream, *args, **kwargs): backup = kwargs.pop('backup_function', None) try: res = fn(*args, **kwargs) except RetryError as e: logger.error(f"RetryError: {e}") if backup is not None: res = backup(*args, **kwargs) else: raise e except Exception as e: trace = traceback.format_exc() logger.error(f"Exception: {e}, \n{trace}") if backup is not None: res = backup(*args, **kwargs) else: raise e is_generator = inspect.isgenerator(res) if is_generator: try: res = check_if_stream_and_raise_exception(res) except Exception as e: # check if exception is not StopIteration try: from botocore.exceptions import EventStreamError if not isinstance(e, StopIteration) and backup is not None: res = backup(*args, **kwargs) else: raise e except Exception as j: raise e if is_generator: res = check_if_stream_and_raise_exception(res) if do_stream and not is_generator: assert isinstance(res, (str, list, tuple)) return convert_iterable_to_stream(res) elif not do_stream and is_generator: return convert_stream_to_iterable(res) return res def convert_iterable_to_stream(iterable): for t in iterable: yield t def convert_stream_to_iterable(stream): ans = [] for t in stream: ans.append(t) if isinstance(ans[0], str): ans = "".join(ans) return ans def check_if_stream_and_raise_exception(iterable_or_str): if isinstance(iterable_or_str, str): # If it's a string, just return it as it is. return iterable_or_str elif isinstance(iterable_or_str, types.GeneratorType): # If it's a generator, we need to peek at it. try: peeked = peekable(iterable_or_str) peeked.peek() # This will raise StopIteration if the generator is empty. return peeked except StopIteration: # Here you could handle the empty generator case. raise except Exception as e: # Here you could handle other exceptions. raise elif isinstance(iterable_or_str, peekable): return iterable_or_str else: # If it's not a string or a generator, raise an exception. raise ValueError("Unexpected input type.") def get_first_n_words(my_string, n=700): return get_first_last_parts(my_string, first_n=n, last_n=0) def get_gpt4_word_count(my_string): import tiktoken enc = tiktoken.encoding_for_model('gpt-4') str_encoded = enc.encode(my_string) return len(str_encoded) def get_gpt3_word_count(my_string): import tiktoken enc = tiktoken.encoding_for_model('gpt-3.5-turbo') str_encoded = enc.encode(my_string) return len(str_encoded) def get_first_last_parts(my_string, first_n=250, last_n=750, enc=None): import tiktoken if enc is None: enc = tiktoken.encoding_for_model('gpt-4') str_encoded = enc.encode(my_string) if len(str_encoded) < first_n + last_n: return my_string str_len = len(str_encoded) first_part = enc.decode(str_encoded[:first_n]) last_part = enc.decode(str_encoded[str_len-last_n:]) return first_part + "\n" + last_part def convert_to_pdf_link_if_needed(link): if "arxiv.org" in link and "pdf" not in link: link = link.replace("abs", "pdf") + ".pdf" # convert arxiv link to pdf if "openreview.net" in link and "pdf" not in link: link = link.replace("forum", "pdf") # convert openreview link to pdf if "aclanthology.org" in link and "pdf" not in link: link = (link[:-1] + ".pdf") if link[-1] == "/" else (link + ".pdf") if "aclweb.org" in link and "anthology" in link and "pdf" not in link: # https://www.aclweb.org/anthology/P19-1028/ link = (link[:-1] + ".pdf") if link[-1] == "/" else (link + ".pdf") # convert aclweb link to pdf return link def extract_array_string(s): # Try to find text inside square brackets match = re.search(r'\[.*?\]', s) if match: return match.group(0) # Check for queries separated by one or two newlines newline_separated = re.split(r'\n\n|\n', s.strip()) if newline_separated and all(len(query.strip().split()) >= 3 for query in newline_separated) and len(newline_separated) >= 3: return newline_separated # Try to find markdown list markdown_list = re.findall(r'^[-*] (.+)$', s, flags=re.M) if markdown_list: return markdown_list # If a single string, return it in an array if s.strip() and ' ' in s.strip() and len(s.strip().split()) <=10: return [s.strip()] # If all else fails, return an empty list return [s.strip().split('\n')[0]] def parse_array_string(s): result = extract_array_string(s) if result and isinstance(result, str) and result.startswith('['): result = re.sub(r"(?<=[a-zA-Z0-9])'(?!(, ?|]))", "@@", result) parsed_list = eval(result) return [i.replace("@@", "'") for i in parsed_list] elif result and isinstance(result, list): return result else: return [] def normalize_whitespace(s): # Replace multiple spaces with a single space s = re.sub(r' {2,}', ' ', s) # Replace multiple tabs with a single tab s = re.sub(r'\t{2,}', '\t', s) # Replace multiple blank lines with a single blank line s = re.sub(r'\n\s*\n', '\n\n', s) return s.strip() def verify_openai_key_and_fetch_models(api_key): logger.warning("Verifying OpenAI API key...") # Make a GET request to OpenAI API headers = {"Authorization": f"Bearer {api_key}"} response = requests.get("https://api.openai.com/v1/models", headers=headers) if response.status_code == 200: # Extract model ids and return as a list models = response.json()["data"] model_ids = [model["id"] for model in models] return model_ids else: # Handle error response print(f"Error fetching OpenAI models: {response.status_code} {response.reason}") return [] def two_column_list(items): half = (len(items) + 1) // 2 # adjust for odd lengths column1 = items[:half] column2 = items[half:] output = '<table><tr><td><ul>' for item in column1: output += f'<li>{item}</li>' output += '</ul></td><td><ul>' for item in column2: output += f'<li>{item}</li>' output += '</ul></td></tr></table>' return output def two_column_list_md(items): half = (len(items) + 1) // 2 # adjust for odd lengths column1 = items[:half] column2 = items[half:] # Create a Markdown table with two columns output = '| Column 1 | Column 2 |\n| --- | --- |\n' for item1, item2 in zip(column1, column2 + [None]): # Check if item2 is None (in case of odd number of items) second_column_item = item2 if item2 is not None else "" output += f'| {item1} | {second_column_item} |\n' # If there are an odd number of items, we'll add the last item if len(items) % 2 != 0: output += f'| {items[-1]} | |\n' return output class SetQueue: def __init__(self, maxsize): self.maxsize = maxsize self.queue = collections.deque(maxlen=maxsize) self.set = set() self.lock = threading.RLock() def remove_any(self, item): with self.lock: if item in self.set: self.set.remove(item) self.queue.remove(item) def add(self, item): with self.lock: self.remove_any(item) if len(self.queue) >= self.maxsize - 1: removed = self.queue.popleft() self.set.remove(removed) self.queue.append(item) self.set.add(item) def __contains__(self, item): with self.lock: return item in self.set def __len__(self): with self.lock: return len(self.queue) def items(self): with self.lock: return list(self.queue) import collections import threading class DefaultDictQueue: def __init__(self, maxsize, default_factory=None): # Added default_factory parameter self.maxsize = maxsize self.queue = collections.deque(maxlen=maxsize) self.set = set() self.data = dict() self.lock = threading.RLock() self.default_factory = default_factory # Save the default factory def remove_any(self, item): with self.lock: if item in self.set: self.set.remove(item) self.queue.remove(item) del self.data[item] def add(self, item, item_data=None): # Modified to allow adding an item without data with self.lock: self.remove_any(item) if len(self.queue) >= self.maxsize - 1: removed = self.queue.popleft() self.set.remove(removed) del self.data[removed] self.queue.append(item) self.set.add(item) self.data[item] = item_data if item_data is not None else self.default_factory() if self.default_factory else None def __contains__(self, item): with self.lock: return item in self.set def __len__(self): with self.lock: return len(self.queue) def items(self): with self.lock: return list(self.queue) def get_data(self, item): with self.lock: if item not in self.set and self.default_factory: self.add(item, self.default_factory(item)) return self.data.get(item, None) def __getitem__(self, item): return self.get_data(item) def __setitem__(self, item, data): with self.lock: if item in self.set: self.data[item] = data else: self.add(item, data) def convert_http_to_https(url): parsed_url = urlparse(url) https_url = parsed_url._replace(scheme='https') return urlunparse(https_url) def get_peekable_iterator(iterable): from more_itertools import peekable p = peekable(iterable) try: _ = p.peek(10) except StopIteration: _ = p.peek() return p return p def truncate_string(input_str, n): # This list will store the original separators for each word separators = [] # Replace all separators with a space and remember the original separator for sep in [',', '\n', '\t', '\r', ';', '"', "'", '(', ')', '{', '}', '[', ']', '<', '>', '?', '/', '\\', '|', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '+', '=', ':', '.']: input_str = input_str.replace(sep, ' ') separators.append(sep) # Split the string into words words = input_str.split(' ') # Remove the last n words truncated_words = words[:-n] # Join the words back together using the original separators truncated_str = '' for word in truncated_words: # Check if the word ends with a separator and add it back if it does for sep in separators: if word.endswith(sep): word = word.rstrip(sep) + sep truncated_str += word + ' ' # Remove the trailing space truncated_str = truncated_str.rstrip(' ') return truncated_str from collections import defaultdict, deque def round_robin_by_group(dict_list, group_key='group'): # Group dictionaries by 'group' key groups = defaultdict(list) for d in dict_list: groups[d[group_key]].append(d) # Convert groups to a deque of deques for round-robin iteration groups = deque(deque(group) for group in groups.values()) while groups: group = groups.popleft() # Take the next group yield group.popleft() # Yield the next dictionary from this group if group: # If the group still has dictionaries, put it back at the end groups.append(group) from flask_caching import Cache from inspect import signature from functools import wraps import mmh3 import diskcache as dc cache_timeout = 7 * 24 * 60 * 60 def typed_memoize(cache, *types): def decorator(f): @wraps(f) def wrapper(*args, **kwargs): # Get the function's signature sig = signature(f) # Bind the arguments to the signature bound_args = sig.bind(*args, **kwargs) bound_args.apply_defaults() # Filter the arguments based on their type filtered_args = {k: v for k, v in bound_args.arguments.items() if isinstance(v, types)} # Define a key function that generates a cache key based on the filtered arguments key = f"{f.__module__}:{f.__name__}:{str(filtered_args)}" # Try to get the result from the cache key = str(mmh3.hash(key, signed=False)) result = cache.get(key) # If the result is not in the cache, call the function and store the result in the cache if result is None: result = f(*args, **kwargs) cache.set(key, result, expire=cache_timeout) return result return wrapper return decorator import requests os_temp_dir = tempfile.gettempdir() temp_dir = os.path.join(os.getcwd(), "storage", "cache") cache = dc.Cache(temp_dir) cache_timeout = 7 * 24 * 60 * 60 def create_tmp_marker_file(file_path): marker_file_path = os.path.join(os_temp_dir, file_path + ".tmp") with open(marker_file_path, 'w') as f: f.write(f"{file_path}") return marker_file_path def remove_tmp_marker_file(file_path): if file_path is None: return None try: marker_file_path = os.path.join(os_temp_dir, file_path + ".tmp") if os.path.exists(marker_file_path): os.remove(marker_file_path) return marker_file_path except Exception as e: logger.error(f"Exception removing tmp marker file: {e}\n{traceback.format_exc()}") return None def exists_tmp_marker_file(file_path): if file_path is None: return True marker_file_path = os.path.join(os_temp_dir, file_path + ".tmp") return os.path.exists(marker_file_path) @typed_memoize(cache, str, int, tuple, bool) def is_pdf_link(link): st = time.time() result = False science_doc = ("arxiv.org" in link and "pdf" in link) or ("openreview.net" in link and "pdf" in link) or ("aclanthology.org" in link and "pdf" in link) or ("aclweb.org" in link and "anthology" in link and "pdf" in link) ends_with_pdf = link.endswith(".pdf") if science_doc or ends_with_pdf: result = True else: response = ProcessFnWithTimeout(Queue())(requests.head, 8, link) content_type = response.headers.get('Content-Type') result = (content_type is not None and (content_type == 'application/pdf' or 'pdf' in content_type)) et = time.time() - st logger.debug(f"Time taken to check if link is pdf: {et:.2f} sec, is science doc: {science_doc}, ends with .pdf: {ends_with_pdf,} result: {result}") return result import threading from queue import Queue class ProcessFnWithTimeout: def __init__(self, result_queue: Queue): self.result_queue = result_queue def __call__(self, fn, timeout, *args, **kwargs): timeout = kwargs.get('timeout', timeout) keep_going_marker = kwargs.get('keep_going_marker', None) result = None exception_event = threading.Event() def worker(): nonlocal result try: result = fn(*args, **kwargs) # Call the original function with its args and kwargs except Exception as e: exc = traceback.format_exc() # Handle exceptions if needed logger.error(f"Exception processing function {fn.__name__}: {e}\n{exc}") finally: exception_event.set() thread = threading.Thread(target=worker) thread.start() # Wait for either the result to be ready or the timeout to occur exception_event.wait(timeout) if not exception_event.is_set(): print(f"Timeout processing function {fn.__name__} , timeout = {timeout}") result = None # Use None to indicate timeout # Put the result (or None if there was a timeout) in the queue self.result_queue.put(result) return result from concurrent.futures import ThreadPoolExecutor import threading from queue import Queue def orchestrator(fn, args_list, callback=None, max_workers=32, timeout=60): if timeout < 0: raise ValueError("Timeout must be non-negative") task_queue = Queue() def task_worker(args, kwargs): try: wait_time = kwargs.get('timeout', timeout) result = ProcessFnWithTimeout(Queue())(fn, wait_time, *args, **kwargs) if callback and result is not None: result = callback(result, args, kwargs) task_queue.put(result) except Exception as e: exc = traceback.format_exc() logger.error(f"[orchestrator] Exception in task_worker with timeout = {timeout} : {e}\n{exc}") task_queue.put(None) # Put None to indicate an error def run_tasks(): try: with ThreadPoolExecutor(max_workers=max_workers) as pool: futures = [] for task in args_list: if task is None: continue args, kwargs = task futures.append(pool.submit(task_worker, args, kwargs)) for future in futures: future.result() except Exception as e: exc = traceback.format_exc() logger.error(f"[orchestrator] Exception in run_tasks with timeout = {timeout} : {e}\n{exc}") finally: # Signal the end of the task results task_queue.put(FINISHED_TASK) task_queue.put(FINISHED_TASK) # this line has to be repeated so that we can handle the second queue poll after staggered LLM response. # Start a separate thread to run the tasks orchestrator_thread = threading.Thread(target=run_tasks) orchestrator_thread.start() # Return the task queue immediately return task_queue from concurrent.futures import Future def orchestrator_with_queue(input_queue, fn, callback=None, max_workers=32, timeout=60): task_queue = Queue() def task_worker(result, args, kwargs): try: wait_time = kwargs.get('timeout', timeout) if result is not TERMINATION_SIGNAL: new_result = ProcessFnWithTimeout(Queue())(fn, wait_time, *args, **kwargs) if callback and new_result is not None: new_result = callback(new_result, args, kwargs) task_queue.put(new_result) except Exception as e: exc = traceback.format_exc() logger.error(f"[orchestrator_with_queue] Exception in task_worker with timeout = {timeout} : {e}\n{exc}") task_queue.put(None) # Put None to indicate an error def run_tasks(): try: args_list = [] futures = [] with ThreadPoolExecutor(max_workers=max_workers) as pool: while True: result = input_queue.get() if result is TERMINATION_SIGNAL or result is FINISHED_TASK or result == FINISHED_TASK: # End of results break if result is None: continue args, kwargs = result future = pool.submit(task_worker, result, [args], kwargs) futures.append(future) for future in futures: future.result() except Exception as e: exc = traceback.format_exc() logger.error(f"[orchestrator_with_queue] Exception in run_tasks with timeout = {timeout} : {e}\n{exc}") finally: # Signal the end of the task results task_queue.put(TERMINATION_SIGNAL) task_queue.put(FINISHED_TASK) # Start a separate thread to run the tasks orchestrator_thread = threading.Thread(target=run_tasks) orchestrator_thread.start() # Return the task queue immediately return task_queue def dual_orchestrator(fn1, fn2, args_list, callback=None, max_workers=32, timeout1=60, timeout2=60): task_queue1 = orchestrator(fn1, args_list, max_workers=max_workers, timeout=timeout1) task_queue2 = orchestrator_with_queue(task_queue1, fn2, callback, max_workers=max_workers, timeout=timeout2) return task_queue2 def yield_with_condition(yield_value, condition_function, failure_call_back): if condition_function(): return yield_value else: return failure_call_back() def remove_leading_spaces(text): lines = text.splitlines() in_code_block = False for i, line in enumerate(lines): if re.match(r'^<code>|^```|^`', line): in_code_block = not in_code_block if not in_code_block: lines[i] = line.lstrip() return '\n'.join(lines) def remove_bad_whitespaces(s): s = re.sub(' +', ' ', s) # Remove extra whitespaces s = re.sub("\n{2,}", "\n", s) s = re.sub("\r+", "\n", s) s = s.strip() lines = s.splitlines(keepends=False) lines = [line.rstrip().lstrip() for line in lines if line.strip()!=''] s = '\n'.join(lines) s = remove_leading_spaces(s) return s def reformat_string(input_str): words = input_str.split("\n") corrected_words = [] prev_word_ended_sentence = False for i, word in enumerate(words): # If the previous word ended with a sentence-ending punctuation, then # this newline is likely intentional. if prev_word_ended_sentence: corrected_words.append("\n") prev_word_ended_sentence = False # Check if this word ends with a sentence-ending punctuation. if word.endswith(('.', '!', '?')): prev_word_ended_sentence = True if word in {',', '.', '!', '?', ';'}: corrected_words[-1] += word else: corrected_words.append(word) return " ".join(corrected_words) def find_nearest_divisible_by_three(arr): # Start from the last index for i in range(len(arr) - 1, -1, -1): # Check if the current index (i + 1 because index starts from 0) is divisible by 3 if (i + 1) % 3 == 0: return arr[i] # Return a message if no such element is found return "No element found with index divisible by 3" import queue import threading def thread_safe_tee(iterable, n=2): queues = [queue.Queue() for _ in range(n)] def generator(queues): for item in iterable: for ix, q in enumerate(queues): q.put(item) # logger.info(f"thread_safe_tee putting item for {ix}-th queue: {item}") for q in queues: q.put(StopIteration) threading.Thread(target=generator, args=(queues,)).start() def gen(ix, q): while True: item = q.get() if item is StopIteration: return # logger.info(f"thread_safe_tee yielding item for {ix}-th queue: {item}") yield item return tuple(gen(ix, q) for ix, q in enumerate(queues)) from langchain.embeddings.openai import embed_with_retry, OpenAIEmbeddings from typing import List, Optional import numpy as np class OpenAIEmbeddingsParallel(OpenAIEmbeddings): def _get_len_safe_embeddings( self, texts: List[str], *, engine: str, chunk_size: Optional[int] = None ) -> List[List[float]]: embeddings: List[List[float]] = [[] for _ in range(len(texts))] try: import tiktoken except ImportError: raise ImportError( "Could not import tiktoken python package. " "This is needed in order to for OpenAIEmbeddings. " "Please install it with `pip install tiktoken`." ) tokens = [] indices = [] model_name = self.tiktoken_model_name or self.model try: encoding = tiktoken.encoding_for_model(model_name) except KeyError: logger.warning("Warning: model not found. Using cl100k_base encoding.") model = "cl100k_base" encoding = tiktoken.get_encoding(model) for i, text in enumerate(texts): if self.model.endswith("001"): # See: https://github.com/openai/openai-python/issues/418#issuecomment-1525939500 # replace newlines, which can negatively affect performance. text = text.replace("\n", " ") token = encoding.encode( text, allowed_special=self.allowed_special, disallowed_special=self.disallowed_special, ) for j in range(0, len(token), self.embedding_ctx_length): tokens += [token[j : j + self.embedding_ctx_length]] indices += [i] batched_embeddings = [] _chunk_size = chunk_size or self.chunk_size if self.show_progress_bar: try: import tqdm _iter = tqdm.tqdm(range(0, len(tokens), _chunk_size)) except ImportError: _iter = range(0, len(tokens), _chunk_size) else: _iter = range(0, len(tokens), _chunk_size) _iter = list(_iter) if len(_iter) <= 2: for i in _iter: response = embed_with_retry( self, input=tokens[i : i + _chunk_size], **self._invocation_params, ) batched_embeddings += [r["embedding"] for r in response["data"]] else: # parallelize the above with a threadpool with ThreadPoolExecutor(max_workers=8) as executor: futures = [] for i in _iter: futures.append(executor.submit(embed_with_retry, self, input=tokens[i : i + _chunk_size], **self._invocation_params)) for future in futures: response = future.result() batched_embeddings += [r["embedding"] for r in response["data"]] results: List[List[List[float]]] = [[] for _ in range(len(texts))] num_tokens_in_batch: List[List[int]] = [[] for _ in range(len(texts))] for i in range(len(indices)): results[indices[i]].append(batched_embeddings[i]) num_tokens_in_batch[indices[i]].append(len(tokens[i])) avg_const = embed_with_retry( self, input="", **self._invocation_params, )[ "data" ][0]["embedding"] for i in range(len(texts)): _result = results[i] if len(_result) == 0: average = avg_const else: average = np.average(_result, axis=0, weights=num_tokens_in_batch[i]) embeddings[i] = (average / np.linalg.norm(average)).tolist() return embeddings from langchain.embeddings.base import Embeddings def get_embedding_model(keys) -> Embeddings: if "embeddingsUrl" in keys and not checkNoneOrEmpty(keys["embeddingsUrl"]): from embedding_client_server import EmbeddingClient return EmbeddingClient(keys["embeddingsUrl"]) openai_key = keys["openAIKey"] assert openai_key # TODO: https://python.langchain.com/docs/modules/data_connection/caching_embeddings openai_embed = OpenAIEmbeddingsParallel(openai_api_key=openai_key, model='text-embedding-ada-002', chunk_size=2048) return openai_embed import re def remove_year_month_substring(s): # Define the regex pattern # This pattern now includes explicit month names pattern = r'\bin \d{4}(?:\s+(?:January|February|March|April|May|June|July|August|September|October|November|December))?' s = re.sub(pattern, '', s) pattern = r'\bin \d{4}(?:\s+(?:january|february|march|april|may|june|july|august|september|october|november|december))?' s = re.sub(pattern, '', s) # Substitute the pattern with an empty string return normalize_whitespace(s) # Test the function test_str = "This event happened in 2023 December and was repeated in 2021 January, but not in 2022 summer." result = remove_year_month_substring(test_str) print(result) # The string with specified substrings removed
[]
2024-01-10
faizanahemad/science-reader
DocIndex.py
import shutil import sys import random from functools import partial import glob from filelock import FileLock, Timeout import matplotlib.pyplot as plt import matplotlib.image as mpimg from collections import defaultdict import re from semanticscholar import SemanticScholar from semanticscholar.SemanticScholar import Paper from langchain.utilities import BingSearchAPIWrapper from collections import Counter import mmh3 from pprint import pprint import time import concurrent.futures import pandas as pd import tiktoken from copy import deepcopy, copy from collections import defaultdict import requests import tempfile from tqdm import tqdm import requests import dill import os import re from prompts import prompts from langchain.document_loaders import MathpixPDFLoader from datetime import datetime, timedelta from langchain.llms import OpenAI from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain import OpenAI, ConversationChain from langchain.embeddings import OpenAIEmbeddings from review_criterias import review_params from pathlib import Path from more_itertools import peekable from concurrent.futures import Future import openai import tiktoken from web_scraping import fetch_html try: import ujson as json except ImportError: import json from langchain.agents import Tool from langchain.tools import BaseTool from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI from langchain.text_splitter import SpacyTextSplitter from langchain.text_splitter import TokenTextSplitter from langchain.text_splitter import NLTKTextSplitter from langchain.prompts import PromptTemplate from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.llms import GPT4All from llama_index.node_parser.simple import SimpleNodeParser from llama_index.langchain_helpers.text_splitter import TokenTextSplitter from llama_index import ( GPTVectorStoreIndex, LangchainEmbedding, LLMPredictor, ServiceContext, StorageContext, download_loader, PromptHelper ) from llama_index import SimpleDirectoryReader, LangchainEmbedding, GPTListIndex, PromptHelper from llama_index import LLMPredictor, ServiceContext from langchain.vectorstores import FAISS from langchain.vectorstores.base import VectorStore from langchain.schema import Document as LangchainDocument from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.document_loaders import TextLoader from llama_index.data_structs.node import Node, DocumentRelationship from llama_index import LangchainEmbedding, ServiceContext from llama_index import GPTTreeIndex, SimpleDirectoryReader from langchain.document_loaders import PyPDFLoader from langchain.utilities import SerpAPIWrapper from langchain.agents import initialize_agent from langchain.agents import AgentType from typing import Optional, Type from langchain.callbacks.manager import AsyncCallbackManagerForToolRun, CallbackManagerForToolRun from langchain.tools import DuckDuckGoSearchRun from langchain.utilities import BingSearchAPIWrapper, DuckDuckGoSearchAPIWrapper from langchain.tools import DuckDuckGoSearchResults from langchain.prompts import PromptTemplate from common import * from base import * import ai21 from langchain.schema import Document pd.options.display.float_format = '{:,.2f}'.format pd.set_option('max_colwidth', 800) pd.set_option('display.max_columns', 100) import logging logger = logging.getLogger(__name__) logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.ERROR, handlers=[ logging.StreamHandler(sys.stdout), logging.FileHandler(os.path.join(os.getcwd(), "log.txt")) ] ) logger.setLevel(logging.ERROR) time_logger = logging.getLogger(__name__ + " | TIMING") time_logger.setLevel(logging.INFO) # Set log level for this logger from tenacity import ( retry, stop_after_attempt, wait_random_exponential, ) import asyncio import threading from playwright.async_api import async_playwright from concurrent.futures import ThreadPoolExecutor, as_completed from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor import time class DocFAISS(FAISS): def merge_from(self, target: FAISS) -> None: """Merge another FAISS object with the current one. Add the target FAISS to the current one. Args: target: FAISS object you wish to merge into the current one Returns: None. """ from langchain.docstore.base import AddableMixin from langchain.schema import Document if not isinstance(self.docstore, AddableMixin): raise ValueError("Cannot merge with this type of docstore") # Numerical index for target docs are incremental on existing ones starting_len = len(self.index_to_docstore_id) # Merge two IndexFlatL2 self.index.merge_from(target.index) # Get id and docs from target FAISS object full_info = [] existing_id = set([target_id for i, target_id in self.index_to_docstore_id.items()]) for i, target_id in target.index_to_docstore_id.items(): if target_id in existing_id: continue doc = target.docstore.search(target_id) if not isinstance(doc, Document): raise ValueError("Document should be returned") full_info.append((starting_len + i, target_id, doc)) # Add information to docstore and index_to_docstore_id. self.docstore.add({_id: doc for _, _id, doc in full_info}) index_to_id = {index: _id for index, _id, _ in full_info} self.index_to_docstore_id.update(index_to_id) def create_index_faiss(chunks, embed_model, doc_id=None): from langchain.schema import Document if doc_id is None: doc_id = [""] * len(chunks) elif isinstance(doc_id, (str, int)): doc_id = [doc_id] * len(chunks) else: assert len(doc_id) == len(chunks) and isinstance(doc_id, (list, tuple)) doc_id = [int(d) for d in doc_id] chunks = [Document(page_content=str(c), metadata={"order": i}) for i, c in enumerate(chunks)] for ix, chunk in enumerate(chunks): chunk.metadata["next"] = None if ix == len(chunks)-1 else chunks[ix + 1] chunk.metadata["previous"] = None if ix == 0 else chunks[ix - 1] chunk.metadata["doc_id"] = doc_id[ix] chunk.metadata["index"] = ix db = DocFAISS.from_documents(chunks, embed_model) return db class DocIndex: def __init__(self, doc_source, doc_filetype, doc_type, doc_text, full_summary, openai_embed, storage): self._visible = False self.result_cutoff = 2 self.version = 0 self.last_access_time = time.time() self.doc_id = str(mmh3.hash(doc_source + doc_filetype + doc_type, signed=False)) self.doc_source = doc_source self.doc_filetype = doc_filetype self.doc_type = doc_type self._title = '' self._short_summary = '' folder = os.path.join(storage, f"{self.doc_id}") os.makedirs(folder, exist_ok=True) self._storage = folder self.store_separate = ["indices", "raw_data", "qna_data", "deep_reader_data", "review_data", "static_data", "_paper_details"] assert doc_filetype == "pdf" and ("http" in doc_source or os.path.exists(doc_source)) self.is_local = os.path.exists(doc_source) static_data = dict(doc_source=doc_source, doc_filetype=doc_filetype, doc_type=doc_type, doc_text=doc_text,) raw_data = dict(chunks=full_summary["chunks"], small_chunks=full_summary["small_chunks"]) raw_index_future = get_async_future(create_index_faiss, raw_data['chunks'], openai_embed, doc_id=self.doc_id,) small_chunk_index_future = get_async_future(create_index_faiss, raw_data["small_chunks"], openai_embed,) del full_summary["chunks"] del full_summary["small_chunks"] qna_data = dict(chunked_summary=full_summary["chunked_summary"], running_summary=full_summary["running_summary"], detailed_qna=full_summary["detailed_qna"], extended_abstract=dict()) deep_reader_data = full_summary["deep_reader_details"] review_data = [] _paper_details = None # self.set_doc_data("static_data", None, static_data) # self.set_doc_data("raw_data", None, raw_data) # self.set_doc_data("qna_data", None, qna_data) # self.set_doc_data("deep_reader_data", None, deep_reader_data) # self.set_doc_data("review_data", None, review_data) # self.set_doc_data("_paper_details", None, _paper_details) # self.set_doc_data("indices", None, indices) futures = [get_async_future(self.set_doc_data, "static_data", None, static_data), get_async_future(self.set_doc_data, "raw_data", None, raw_data), get_async_future(self.set_doc_data, "qna_data", None, qna_data), get_async_future(self.set_doc_data, "deep_reader_data", None, deep_reader_data), get_async_future(self.set_doc_data, "review_data", None, review_data), get_async_future(self.set_doc_data, "_paper_details", None, _paper_details)] indices = dict(dqna_index=create_index_faiss([''], openai_embed, doc_id=self.doc_id, ), raw_index=raw_index_future.result(), summary_index=create_index_faiss([''], openai_embed, ), small_chunk_index=small_chunk_index_future.result()) futures.append(get_async_future(self.set_doc_data, "indices", None, indices)) for f in futures: f.result() @property def visible(self): return self._visible if hasattr(self, "_visible") else True def get_doc_data(self, top_key, inner_key=None,): import dill doc_id = self.doc_id folder = self._storage filepath = os.path.join(folder, f"{doc_id}-{top_key}.partial") json_filepath = os.path.join(folder, f"{doc_id}-{top_key}.json") try: assert top_key in self.store_separate except Exception as e: raise ValueError(f"Invalid top_key {top_key} provided") logger.info(f"Get doc data for top_key = {top_key}, inner_key = {inner_key}, folder = {folder}, filepath = {filepath} exists = {os.path.exists(filepath)}, json filepath = {json_filepath} exists = {os.path.exists(json_filepath)}, already loaded = {getattr(self, top_key, None) is not None}") if getattr(self, top_key, None) is not None: if inner_key is not None: return getattr(self, top_key, None).get(inner_key, None) else: return getattr(self, top_key, None) else: if os.path.exists(json_filepath): with open(json_filepath, "r") as f: obj = json.load(f) setattr(self, top_key, obj) if inner_key is not None: return obj.get(inner_key, None) else: return obj elif os.path.exists(filepath): with open(filepath, "rb") as f: obj = dill.load(f) if top_key not in ["indices", "_paper_details"]: with open(json_filepath, "w") as f: json.dump(obj, f) setattr(self, top_key, obj) if inner_key is not None: return obj.get(inner_key, None) else: return obj else: return None def set_doc_data(self, top_key, inner_key, value, overwrite=False): import dill doc_id = self.doc_id folder = self._storage print(folder) filepath = os.path.join(folder, f"{doc_id}-{top_key}.partial") json_filepath = os.path.join(folder, f"{doc_id}-{top_key}.json") path = Path(folder) lock_location = os.path.join(os.path.join(path.parent.parent, "locks"), f"{doc_id}-{top_key}") lock = FileLock(f"{lock_location}.lock") with lock.acquire(timeout=600): if top_key == "deep_reader_data": if os.path.exists(json_filepath): with open(json_filepath, "r") as f: old_deep_reader_details = json.load(f) elif os.path.exists(filepath): with open(os.path.join(filepath), "rb") as f: old_deep_reader_details = dill.load(f) else: old_deep_reader_details = dict() for k, v in old_deep_reader_details.items(): if inner_key is None or k.strip() == inner_key.strip(): continue if v is not None and isinstance(v["text"], str) and len(v["text"].strip()) > 0 and checkNoneOrEmpty(self.get_doc_data("deep_reader_data").get(k, dict()).get("text", None)): self.set_doc_data("deep_reader_data", k, v) if top_key == "qna_data" and inner_key == "detailed_qna": if os.path.exists(json_filepath): with open(json_filepath, "r") as f: old_qna_details = json.load(f) elif os.path.exists(filepath): with open(os.path.join(filepath), "rb") as f: old_qna_details = dill.load(f) else: old_qna_details = dict() current_qid = [d[0] for d in self.get_doc_data("qna_data","detailed_qna") + value] if overwrite: current_qna = value for _, (qid, q, a, m) in enumerate(old_qna_details.get("detailed_qna", [])): if len(q.strip()) > 0 and qid not in current_qid: current_qna.append([qid, q, a, m]) value = current_qna else: current_qna = self.get_doc_data("qna_data","detailed_qna") + value for _, (qid, q, a, m) in enumerate(value): if len(q.strip()) > 0 and qid not in current_qid: value.append([qid, q, a, m]) if inner_key is not None: tk = self.get_doc_data(top_key) if tk is None: setattr(self, top_key, dict()) inner = self.get_doc_data(top_key, inner_key) assert type(inner) == type(value) or inner is None or (isinstance(inner, (tuple, list)) and isinstance(value, (tuple, list))) if isinstance(inner, dict) and not overwrite: inner.update(value) elif isinstance(inner, list) and not overwrite: inner.extend(value) elif isinstance(inner, str) and not overwrite: inner = inner + value elif isinstance(inner, tuple) and not overwrite: inner = inner + value else: inner = value getattr(self, top_key, None)[inner_key] = inner else: tk = self.get_doc_data(top_key, None) if top_key == "review_data" and isinstance(tk, dict): tk = list(tk.values()) assert (type(tk) == type(value) or tk is None or value is None) or (isinstance(tk, (tuple, list)) and isinstance(value, (tuple, list))) if tk is not None and type(tk) == type(value): if isinstance(tk, dict) and not overwrite: tk.update(value) elif isinstance(tk, list) and not overwrite: tk.extend(value) elif isinstance(tk, str) and not overwrite: tk = tk + value elif isinstance(tk, tuple) and not overwrite: tk = tk + value else: tk = value setattr(self, top_key, tk) elif tk is None and value is not None: setattr(self, top_key, value) else: setattr(self, top_key, None) if top_key not in ["indices", "_paper_details"]: with open(json_filepath, "w") as f: json.dump(getattr(self, top_key, None), f) else: with open(os.path.join(filepath), "wb") as f: dill.dump(getattr(self, top_key, None), f) def get_short_answer(self, query, mode=defaultdict(lambda:False), save_answer=True): answer = '' for ans in self.streaming_get_short_answer(query, mode, save_answer): answer += ans return answer @property def streaming_followup(self): return prompts.streaming_followup @property def short_streaming_answer_prompt(self): return prompts.short_streaming_answer_prompt @property def running_summary_prompt(self): return prompts.running_summary_prompt def get_date(self): paper_details = self.paper_details if "publicationDate" in paper_details: return paper_details["publicationDate"][:7] elif "year" in paper_details: return paper_details["year"] + "-01" if "arxiv.org" in self.doc_source: yr = self.doc_source.split("/")[-1].split(".")[0] if is_int(yr): return yr return None return None def semantic_search_document(self, query): # tldr = (self.paper_details["tldr"] + "\n\n") if "tldr" in self.paper_details and self.paper_details[ # "tldr"] is not None and len(self.paper_details["tldr"].strip()) > 0 else "" # title = (self.paper_details["title"] + "\n\n") if "title" in self.paper_details and self.paper_details[ # "title"] is not None and len(self.paper_details["title"].strip()) > 0 else "" # brief_summary = title + tldr + self.short_summary # brief_summary = (brief_summary + "\n\n") if len(brief_summary.strip()) > 0 else "" brief_summary = "" summary_nodes = self.get_doc_data("indices", "summary_index").similarity_search(query, k=self.result_cutoff * 2) summary_text = "\n".join([n.page_content for n in summary_nodes]) # + "\n" + additional_text_qna summary_text = (summary_text + "\n\n") if len(summary_text.strip()) > 0 else "" rem_init_len = 512 * 4 rem_word_len = rem_init_len - get_gpt3_word_count( summary_text + brief_summary) rem_tokens = rem_word_len // LARGE_CHUNK_LEN raw_nodes = self.get_doc_data("indices", "raw_index").similarity_search(query, k=max(self.result_cutoff, rem_tokens)) raw_text = "\n".join([n.page_content for n in raw_nodes]) return brief_summary + summary_text + "\n\n" + raw_text @streaming_timer def streaming_get_short_answer(self, query, mode=defaultdict(lambda:False), save_answer=True): ent_time = time.time() detail_level = 1 if mode["provide_detailed_answers"]: detail_level = int(mode["provide_detailed_answers"]) mode = "detailed" query = f"{query}\n\nWrite detailed, informative, comprehensive and in depth answer. Provide as much detail, information and depth as possible.\n\n" elif mode["review"]: mode = "detailed" detail_level = 1 else: mode = None detail_level = 1 # Sequential + RAG approach -> then combine. # For level 1, 2 both approaches use gpt3.5-16k -> gpt4-16k # For level 3, 4 both approaches use gpt3.5-16k + gpt4-16k brief_summary = self.title + "\n" + self.short_summary brief_summary = ("Summary:\n"+ brief_summary +"\n\n") if len(brief_summary.strip()) > 0 else "" additional_info = None if mode == "detailed" or mode == "review": text = brief_summary + self.get_doc_data("static_data", "doc_text") tex_len = get_gpt4_word_count(text) if tex_len < 7000: llm = CallLLm(self.get_api_keys(), use_gpt4=True, use_16k=False) prompt = f"""Answer the question or query given below using the given context as reference. Question or Query is given below. {query} Context is given below. {text} Write answer below. """ additional_info = get_async_future(llm, prompt, temperature=0.5) else: additional_info = get_async_future(call_contextual_reader, query, brief_summary + self.get_doc_data("static_data", "doc_text"), self.get_api_keys(), provide_short_responses=False, chunk_size=TOKEN_LIMIT_FOR_DETAILED + 500, scan=detail_level >= 2) if detail_level >= 2 and tex_len >= 7000: raw_nodes = self.get_doc_data("indices", "raw_index").similarity_search(query, k=max(self.result_cutoff, 4200//LARGE_CHUNK_LEN)) raw_text = "\n\n".join([n.page_content for n in raw_nodes]) small_chunk_nodes = self.get_doc_data("indices", "small_chunk_index").similarity_search(query, k=max( self.result_cutoff, 1200//SMALL_CHUNK_LEN)) small_chunk_text = "\n\n".join([n.page_content for n in small_chunk_nodes]) raw_text = raw_text + " \n\n " + small_chunk_text prompt = self.short_streaming_answer_prompt.format(query=query, fragment=brief_summary + raw_text, full_summary='') llm = CallLLm(self.get_api_keys(), use_gpt4=False, use_16k=True) additional_info_v1 = additional_info def get_additional_info(): ad_info = get_async_future(llm, prompt, temperature=0.8) init_add_info = additional_info_v1.result() return init_add_info + "\n\n" + ad_info.result() additional_info = get_async_future(get_additional_info) answer = '' if detail_level < 2 or additional_info is None or mode=="review": llm = CallLLm(self.get_api_keys(), use_gpt4=mode == "detailed" and detail_level > 1, use_16k=True) rem_word_len = MODEL_TOKENS_SMART - get_gpt4_word_count(brief_summary) - 2000 rem_tokens = rem_word_len // LARGE_CHUNK_LEN raw_nodes = self.get_doc_data("indices", "raw_index").similarity_search(query, k=max(self.result_cutoff, rem_tokens)) raw_text = "\n\n".join([n.page_content for n in raw_nodes]) st_wt = time.time() while (additional_info is not None and time.time() - st_wt < 45 and not additional_info.done()): time.sleep(0.5) full_summary = "" if additional_info.done(): full_summary = additional_info.result() if additional_info is not None else "" full_summary = f"Short summary of the document is given below. \n'''{full_summary}'''" if len(full_summary.strip()) > 0 else "" rem_word_len = MODEL_TOKENS_SMART - get_gpt4_word_count(brief_summary + raw_text) - 500 if rem_word_len > SMALL_CHUNK_LEN: rem_tokens = rem_word_len // SMALL_CHUNK_LEN small_chunk_nodes = self.get_doc_data("indices", "small_chunk_index").similarity_search(query, k=max(self.result_cutoff, rem_tokens)) small_chunk_text = "\n\n".join([n.page_content for n in small_chunk_nodes]) raw_text = raw_text + " \n\n " + small_chunk_text prompt = self.short_streaming_answer_prompt.format(query=query, fragment=brief_summary+raw_text, full_summary=full_summary) if llm.use_gpt4 and llm.use_16k: prompt = get_first_last_parts(prompt, 1000, MODEL_TOKENS_SMART*2 - 1000) elif llm.use_gpt4: prompt = get_first_last_parts(prompt, 1000, MODEL_TOKENS_SMART - 500) elif llm.use_16k: prompt = get_first_last_parts(prompt, 1000, MODEL_TOKENS_SMART * 2 - 1000) else: prompt = get_first_last_parts(prompt, 1000, MODEL_TOKENS_DUMB - 1000) ans_begin_time = time.time() logger.info(f"streaming_get_short_answer:: Start to answer by {(ans_begin_time-ent_time):4f}s") main_ans_gen = llm(prompt, temperature=0.7, stream=True) for txt in main_ans_gen: yield txt answer += txt yield "</br> \n" if mode == "detailed" and detail_level >= 2 and additional_info is not None: additional_info = additional_info.result() for t in additional_info: yield t answer += t if save_answer: get_async_future(self.put_answer, query, answer, mode=mode) def get_fixed_details(self, key): if self.get_doc_data("deep_reader_data") is not None and self.get_doc_data("deep_reader_data", key) is not None and len(self.get_doc_data("deep_reader_data", key)["text"].strip())>0: logger.debug(f'Found fixed details for key = {key}') return self.get_doc_data("deep_reader_data", key) keys = [ "methodology", "previous_literature_and_differentiation", "experiments_and_evaluation", "results_and_comparison", "limitations_and_future_work" ] assert key in keys key_to_query_map = prompts.paper_details_map full_text = '' for txt in self.streaming_get_short_answer(key_to_query_map[key], defaultdict(lambda: False, {"provide_detailed_answers": True}), save_answer=False): full_text += txt yield txt self.set_doc_data("deep_reader_data", key, {"id": str(mmh3.hash(self.doc_source + key, signed=False)), "text": full_text}) def get_short_info(self): return dict(visible=self.visible, doc_id=self.doc_id, source=self.doc_source, title=self.title, short_summary=self.short_summary, summary=self.get_doc_data("qna_data", "running_summary") if self.get_doc_data("qna_data", "running_summary") is not None else '') @property def title(self): if hasattr(self, "_title") and len(self._title.strip()) > 0: return self._title else: try: title = self.paper_details["title"] except Exception as e: title = CallLLm(self.get_api_keys(), use_gpt4=False)(f"""Provide a title for the below text: \n'{self.get_doc_data("raw_data", "chunks")[0]}' \nTitle: \n""") setattr(self, "_title", title) self.save_local() return title @staticmethod def process_one_paper(paper, extended_abstract): string_keys = ["paperId", "venue", "url", "title", "abstract", "tldr", "year", "referenceCount", "citationCount", "journal"] keys = ["publicationDate", "citations", "references", "externalIds", ] paper_output = dict() for k in string_keys: paper_output[k] = str(getattr(paper, k)) # print(paper.title, getattr(paper, "publicationDate"), paper.year) pubdate = getattr(paper, "publicationDate") if pubdate is None: paper_output["publicationDate"] = str(paper.year)+"-01-01" else: paper_output["publicationDate"] = pubdate.strftime("%Y-%m-%d") paper_output['ArXiv'] = NoneToDefault(getattr(paper, "externalIds", dict()), dict()).get('ArXiv') paper_output["citations"] = [DocIndex.process_one_paper(c, None) for c in NoneToDefault(getattr(paper, "citations", []))] paper_output["references"] = [DocIndex.process_one_paper(c, None) for c in NoneToDefault(getattr(paper, "references", []))] paper_output["citations"] = [c for c in paper_output["citations"] if c["paperId"] is not None and len(c["paperId"])>0 and c["paperId"].lower()!="none"] paper_output["references"] = [c for c in paper_output["references"] if c["paperId"] is not None and len(c["paperId"])>0 and c["paperId"].lower()!="none"] paper_output["extended_abstract"] = extended_abstract return paper_output @property def paper_details(self)->dict: try: if hasattr(self, "is_local") and self.is_local or "arxiv.org" not in self.doc_source: return dict() elif self.get_doc_data("_paper_details") is not None: pd = deepcopy(self.get_doc_data("_paper_details")) if self.get_doc_data("qna_data", "extended_abstract") is None: self.set_doc_data("qna_data", "extended_abstract", dict()) extended_abstract = self.get_doc_data("qna_data", "extended_abstract").get(pd["paperId"], None) return DocIndex.process_one_paper(pd, extended_abstract) else: arxiv_url = self.doc_source paper = get_paper_details_from_semantic_scholar(arxiv_url) self.set_doc_data("_paper_details", None, paper) return self.paper_details except Exception as e: logger.error(f"Error in fetching paper details for {self.doc_source}") return dict() def refetch_paper_details(self)->dict: if hasattr(self, "is_local") and self.is_local or "arxiv.org" not in self.doc_source: return dict() url = self.doc_source paper = get_paper_details_from_semantic_scholar(url) self.set_doc_data("_paper_details", None, paper) return self.paper_details def get_extended_abstract_for_ref_or_cite(self, paperId)->str: if self.get_doc_data("qna_data", "extended_abstract") is None: self.set_doc_data("qna_data", "extended_abstract", dict()) paper_details = self.paper_details for ref in paper_details["references"] + paper_details["citations"]: if ref["paperId"] == paperId: text = self.get_doc_data("qna_data", "extended_abstract").get(paperId, None) yield text if text.strip() != '': return None from semanticscholar import SemanticScholar sch = SemanticScholar() paper = sch.get_paper(paperId) if 'ArXiv' in paper.externalIds: arxiv = paper.externalIds['ArXiv'] pdf_url = f"https://arxiv.org/pdf/{arxiv}.pdf" data = PDFReaderTool()(pdf_url, page_ranges="1-3") prompt = f"""Provide a detailed and comprehensive summary for the scientific text given. This scientific text is the beginning two pages of a larger research paper, as such some details maybe incomplete in this scientific text. Abstract: '{paper.abstract}' Scientific Text: '{data}' Detailed and comprehensive summary: """ answer = '' for txt in CallLLm(self.get_api_keys(), use_gpt4=False)(prompt, temperature=0.7, stream=True): yield txt answer += txt self.get_doc_data("qna_data", "extended_abstract")[paperId] = answer self.set_doc_data("qna_data", "extended_abstract", self.get_doc_data("qna_data", "extended_abstract")) else: yield "Could not find ArXiv pdf for this document" @property def short_summary(self): if hasattr(self, "_short_summary") and len(self._short_summary.strip()) > 0: return self._short_summary else: try: short_summary = self.paper_details["abstract"] except Exception as e: short_summary = CallLLm(self.get_api_keys(), use_gpt4=False)(f"""Provide a summary for the below scientific text: \n'''{ChunkText(self.get_doc_data("static_data", "doc_text"), TOKEN_LIMIT_FOR_SHORT, 0)[0]}''' \nInclude relevant keywords, the provided abstract and any search/seo friendly terms in your summary. \nSummary: \n""",) setattr(self, "_short_summary", short_summary) self.save_local() return short_summary def get_all_details(self): details = dict(chunked_summary=self.get_doc_data("qna_data", "chunked_summary"), deep_reader_details=self.get_doc_data("deep_reader_data"), detailed_qna=self.get_doc_data("qna_data", "detailed_qna"), running_summary=self.get_doc_data("qna_data", "running_summary")) return dict(doc_id=self.doc_id, source=self.doc_source, title=self.title, short_summary=self.short_summary, summary=self.get_doc_data("qna_data", "running_summary"), details=details) def streaming_ask_follow_up(self, query, previous_answer, mode=defaultdict(lambda: False)): if mode["provide_detailed_answers"]: mode = "detailed" else: mode = None llm = CallLLm(self.get_api_keys(), use_gpt4=True) answer = previous_answer["answer"] + "\n" + ( previous_answer["parent"]["answer"] if "parent" in previous_answer else "") rem_word_len = MODEL_TOKENS_SMART - get_gpt4_word_count(answer) - 2000 rem_tokens = rem_word_len // LARGE_CHUNK_LEN raw_nodes = self.get_doc_data("indices", "raw_index").similarity_search(query, k=max(self.result_cutoff, rem_tokens)) raw_text = "\n".join([n.page_content for n in raw_nodes]) rem_word_len = MODEL_TOKENS_SMART - get_gpt4_word_count(answer + raw_text) - 500 rem_tokens = rem_word_len // SMALL_CHUNK_LEN small_chunk_nodes = self.get_doc_data("indices", "small_chunk_index").similarity_search(query, k=max(self.result_cutoff * 2, rem_tokens)) # Get those nodes that don't come up in last query. small_chunk_nodes_ids = [n.metadata["order"] for n in small_chunk_nodes] small_chunk_nodes_old = self.get_doc_data("indices", "small_chunk_index").similarity_search(previous_answer["query"], k=self.result_cutoff*8) small_chunk_nodes_ids = small_chunk_nodes_ids + [n.metadata["order"] for n in small_chunk_nodes_old] additional_small_chunk_nodes = self.get_doc_data("indices", "small_chunk_index").similarity_search(query, k=self.result_cutoff*8) additional_small_chunk_nodes = [n for n in additional_small_chunk_nodes if n.metadata["order"] not in small_chunk_nodes_ids] small_chunk_nodes = small_chunk_nodes + additional_small_chunk_nodes[:2] raw_text = raw_text + "\n".join([n.page_content for n in small_chunk_nodes]) prompt = self.streaming_followup.format(followup=query, query=previous_answer["query"], answer=answer, fragment=raw_text) prompt = get_first_last_parts(prompt, 1000, MODEL_TOKENS_SMART - 1000) generator = llm(prompt, temperature=0.7, stream=True) answer = '' for txt in generator: yield txt answer += txt self.put_answer(previous_answer["query"], answer, query, mode) def streaming_get_more_details(self, query, answer, additional_info): llm = CallLLm(self.get_api_keys(), use_gpt4=True) prompt = prompts.get_more_details_prompt.format(query=query, answer=answer, additional_info=additional_info) prompt = get_first_last_parts(prompt, 1000, 6500) if llm.use_gpt4 else get_first_last_parts(prompt, 1000, 2500) answer = answer + "\n" for txt in llm(prompt, temperature=0.7, stream=True): yield txt answer += txt def streaming_build_summary(self): summary_prompt = "The given text is part of a document. Write a detailed summary which contains all important and essential information from the given text. Summarize the text:\n '{}' \nSummary: \n" if len(self.get_doc_data("qna_data", "chunked_summary")) > 0 and len(self.get_doc_data("qna_data", "chunked_summary")[0].strip())>0: # We already have the summary for txt in self.get_doc_data("qna_data", "chunked_summary"): yield txt running_summaries = [] self.set_doc_data("qna_data", "chunked_summary", []) running_summary = '' this_chunk = '' llm = CallLLm(self.get_api_keys(), use_16k=True) brief_summary = self.title + "\n" + self.short_summary brief_summary = (brief_summary + "\n\n") if len(brief_summary.strip()) > 0 else "" chunks = ChunkText(self.get_doc_data("static_data", "doc_text"), TOKEN_LIMIT_FOR_DETAILED - 2000, 256) chunks = [f"Overall document context:\n'''{brief_summary}'''\nText from current document context we are summarising:\n'''{t}'''" for t in chunks if len(t.strip()) > 0] chunk_summaries = [] for ic, chunk in enumerate(chunks): if not TextLengthCheck(running_summary, 1600): running_summaries.append(running_summary) running_summary = CallLLm(self.get_api_keys(), use_gpt4=False)(summary_prompt.format(running_summary), temperature=0.7, stream=False) cur_sum = f"The summary we have written till now:\n'''{running_summary}'''\nContinue writing ahead from the 'summary we have written till now'." if len(running_summary.strip()) > 0 else "" prev_sum = f"Summary of previous context from the same document:\n'''{this_chunk}'''" if len(this_chunk.strip()) > 0 else "" prompt = self.running_summary_prompt.format(summary=cur_sum, document=chunk, previous_chunk_summary=prev_sum) this_chunk = '' for txt in llm(prompt, temperature=0.7, stream=True): this_chunk = this_chunk + txt yield txt chunk_summaries.append(this_chunk) running_summary = running_summary + " " + this_chunk if len(running_summaries) == 1: rsum = running_summaries[0] elif len(running_summaries) == 0: rsum = running_summary else: llm = CallLLm(self.get_api_keys(), use_gpt4=True) if llm.use_gpt4: rs = [running_summaries[i] for i in range(0, len(running_summaries), 1)] if get_gpt4_word_count(" ".join(rs)) < 7000: running_summaries = [running_summaries[i] for i in range(0, len(running_summaries), 1)] else: rs = [running_summaries[i] for i in range(0, len(running_summaries), 2)] if get_gpt4_word_count(" ".join(rs)) < 7000: running_summaries = [running_summaries[i] for i in range(0, len(running_summaries), 2)] else: mid = max(len(running_summaries) // 2 - 1, 0) running_summaries = running_summaries[mid:mid + 1] else: mid = max(len(running_summaries)//2 - 1, 0) running_summaries = running_summaries[mid:mid+1] yield '\n\n</br></br>' new_summary_prompt = "Write a detailed overall summary of a document from given sectional summary of parts of the document. Ignore References. \nSectional Summaries:\n'{}'\nProvide elaborate, detailed, comprehensive, informative and in-depth summary. Overall Summary:\n" rsum = '' prompt = new_summary_prompt.format(" \n".join([brief_summary] + running_summaries+[running_summary])) prompt = get_first_last_parts(prompt, 1000, 6000) yield "<h3>Overall Summary</h3>" yield "\n" for txt in llm(prompt, temperature=0.7, stream=True): rsum = rsum + txt yield txt self.set_doc_data("qna_data", "chunked_summary", chunk_summaries, overwrite=True) assert len(rsum.strip()) > 0 self.set_doc_data("qna_data", "running_summary", rsum, overwrite=True) self.set_doc_data("indices", "summary_index", create_index_faiss(self.get_doc_data("qna_data", "chunked_summary",), get_embedding_model(self.get_api_keys()), )) def get_instruction_text_from_review_topic(self, review_topic): instruction_text = '' if isinstance(review_topic, str) and review_topic.strip() in review_params: instruction_text = review_topic + ": "+review_params[review_topic.strip()] elif isinstance(review_topic, str): instruction_text = review_topic.strip() elif isinstance(review_topic, (list, tuple)): try: assert len(review_topic) == 2 assert isinstance(review_topic[0], str) assert isinstance(review_topic[1], int) instruction_text = ": ".join(review_params[review_topic[0].strip()][review_topic[1]]) except Exception as e: raise Exception(f"Invalid review topic {review_topic}") else: raise Exception(f"Invalid review topic {review_topic}") return instruction_text def get_all_reviews(self): new_review_params = dict(**review_params) del new_review_params["meta_review"] del new_review_params["scores"] new_reviews = [] if self.get_doc_data("review_data"): for r in self.get_doc_data("review_data"): # dict(review_text=review_text, is_meta_review=is_meta_review, tone=tone, header=header, detailed_instructions=detailed_instructions, ) we use this structure. new_reviews.append(dict(review=r["review"] + ('\n\n' if len(r['score']) > 0 else '') + r['score'], is_meta_review=r["is_meta_review"], tone=r["tone"], id=r["id"], review_topic=r["review_topic"], header=self.get_instruction_text_from_review_topic(r["review_topic"]).split(":")[0].strip(), description=self.get_instruction_text_from_review_topic(r["review_topic"]).split(":")[-1].strip(), instructions=r["additional_instructions"],)) return {"reviews": new_reviews, "review_params": new_review_params} else: return {"reviews": [], "review_params":new_review_params} def get_review(self, tone, review_topic, additional_instructions, score_this_review, use_previous_reviews, is_meta_review): # Map -> collect details. # TODO: Support followup on a generated review. # TODO: use previous reviews. assert tone in ["positive", "negative", "neutral", "none"] tones = ["positive", "negative", "neutral", "none"] tone_synonyms = ["favorable and supportive.", "critical and unfavorable.", "not opinionated and middle grounded.", "unbiased to accept or reject decision."] instruction_text = self.get_instruction_text_from_review_topic(review_topic) if is_meta_review: assert use_previous_reviews and self.get_doc_data("review_data") is not None and len(self.get_doc_data("review_data")) > 0, "Meta reviews require previous reviews to be present" # fetch cached review if present. # if self.get_doc_data("review_data"): # for review in self.get_doc_data("review_data"): # if str(review["review_topic"]) == str(review_topic) and review["tone"] == tone: # yield review["review"] # yield review["score"] # return previous_reviews_text = '' newline = "\n" if use_previous_reviews and self.get_doc_data("review_data") and len(self.get_doc_data("review_data")) > 0: previous_reviews = [review for review in self.get_doc_data("review_data") if review["tone"] == tone] previous_reviews_text = "\n\n".join([review["review"]+review["score"] for review in previous_reviews]) query_prompt = f"""You are an expert {'meta-' if is_meta_review else ''}reviewer assigned to write an in-depth review and evaluate a scientific research paper using provided reviewer instructions on a conference submission website like openreview.net or microsoft cmt. Justify your review with examples from the research paper.{(' '+review_params['meta_review'] + ' ') if is_meta_review else ''} Provide a {(tone + ' ') if tone!='none' and len(tone)>0 else ''}review for the given scientific research. {(' Make your review sound ' + tone_synonyms[tones.index(tone)]) if tone!='none' and len(tone)>0 else ''} The topic and style you should follow while writing the review is described in the reviewer instructions given below:\n'''{instruction_text}'''. {('Further we have certain additional instructions to follow while writing this review: ```' + additional_instructions + '```' + newline) if len(additional_instructions.strip())>0 else ''}{('We also have previous reviews with same tone on this paper to assist in writing this review. Previous reviews: ```' + previous_reviews_text + '```' + newline) if len(previous_reviews_text) > 0 else ''} Don't give final remarks or conclusions unless asked in reviewer instructions. \n{'Meta-' if is_meta_review else ''}Review: \n""" mode = defaultdict(lambda: False) mode["review"] = True review = '' for txt in self.streaming_get_short_answer(query_prompt, defaultdict(lambda: False, {"review": True}), save_answer=False): yield txt review += txt score = '' if score_this_review: score_prompt = f"""Provide a score for the given research work using the given review on a scale of 1-5 ({review_params['scores']}). Provide your step by step elaborate reasoning for your score decision before writing your score. First page of the research work: \n'''{ ' '.join(self.get_doc_data("raw_data", "chunks")[:3])}''' \nReview: \n'''{review}''' \nWrite Reasoning for score and then write score: \n""" for txt in CallLLm(self.get_api_keys(), use_gpt4=False)(score_prompt, temperature=0.1, stream=True): yield txt score += txt self.save_review(review, score, tone, review_topic, additional_instructions, is_meta_review) def save_review(self, review, score, tone, review_topic, additional_instructions, is_meta_review): if self.get_doc_data("review_data") is None: self.set_doc_data("review_data", None, []) save_dict = dict(review=review, score=score, tone=tone, review_topic=",".join(map(str, review_topic)) if isinstance(review_topic, list) else review_topic, additional_instructions=additional_instructions, is_meta_review=is_meta_review) id = str(mmh3.hash(self.doc_source + ",".join([tone, ",".join(map(str, review_topic)) if isinstance(review_topic, list) else review_topic, additional_instructions, str(is_meta_review)]), signed=False)) save_dict["id"] = id self.set_doc_data("review_data", None, [save_dict]) @staticmethod def load_local(folder): original_folder = folder folder = os.path.join(folder, os.path.basename(folder)+".index") import dill try: with open(folder, "rb") as f: obj = dill.load(f) setattr(obj, "_storage", original_folder) return obj except Exception as e: logger.error(f"Error loading from local storage {folder} with error {e}") try: shutil.rmtree(original_folder) except Exception as e: logger.error( f"Error deleting local storage {folder} with error {e}") return None def save_local(self): import dill doc_id = self.doc_id folder = self._storage os.makedirs(folder, exist_ok=True) os.makedirs(os.path.join(folder, "locks"), exist_ok=True) path = Path(folder) lock_location = os.path.join(os.path.join(path.parent.parent, "locks"), f"{doc_id}") filepath = os.path.join(folder, f"{doc_id}.index") lock = FileLock(f"{lock_location}.lock") if hasattr(self, "api_keys"): presave_api_keys = self.api_keys self.api_keys = {k: None for k, v in self.api_keys.items()} with lock.acquire(timeout=600): previous_attr = dict() for k in self.store_separate: if hasattr(self, k): previous_attr[k] = getattr(self, k) setattr(self, k, None) with open(filepath, "wb") as f: dill.dump(self, f) for k, v in previous_attr.items(): setattr(self, k, v) if hasattr(self, "api_keys"): self.api_keys = presave_api_keys def put_answer(self, query, answer, followup_query='', mode=None): query = query.strip() followup_query = followup_query.strip() final_query = query + (f". followup:{followup_query}" if len(followup_query.strip()) > 0 else "") question_id = str(mmh3.hash(self.doc_source + final_query, signed=False)) found_index = None for ix, qna_pair in enumerate(self.get_doc_data("qna_data", "detailed_qna")): if qna_pair[0] == question_id and found_index is None: found_index = ix logger.info(f"Put answer in doc for storage with question_id = {question_id}, query = {query}, found_index = {found_index}") if found_index is None: self.set_doc_data("qna_data", "detailed_qna", [[question_id, final_query, answer, mode]]) else: self.get_doc_data("qna_data", "detailed_qna")[found_index] = [question_id, final_query, answer, mode] self.set_doc_data("qna_data", "detailed_qna", self.get_doc_data("qna_data", "detailed_qna"), overwrite=True) db2 = FAISS.from_texts([final_query +"\n"+answer], get_embedding_model(self.get_api_keys())) self.get_doc_data("indices", "dqna_index").merge_from(db2) index = self.get_doc_data("indices", "dqna_index") self.set_doc_data("indices", "dqna_index", index) def get_api_keys(self): logger.info(f"get api keys for self hash = {hash(self)} and doc_id = {self.doc_id}") if hasattr(self, "api_keys"): api_keys = deepcopy(self.api_keys) else: raise AttributeError("No attribute named `api_keys`.") return api_keys def set_api_keys(self, api_keys:dict): assert isinstance(api_keys, dict) indices = self.get_doc_data("indices") for k, j in indices.items(): if isinstance(j, (FAISS, VectorStore)): j.embedding_function = get_embedding_model(api_keys).embed_query j.embedding_function.__self__.openai_api_key = api_keys["openAIKey"] setattr(j.embedding_function.__self__, "openai_api_key", api_keys["openAIKey"]) setattr(self, "api_keys", api_keys) def __copy__(self): # Create a new instance of our class cls = self.__class__ result = cls.__new__(cls) # Copy all attributes from self to result. This is a shallow copy. result.__dict__.update(self.__dict__) for k in self.store_separate: if hasattr(result, k): setattr(result, k, None) if hasattr(result, "api_keys"): result.api_keys = deepcopy(self.api_keys) return result def copy(self): return self.__copy__() class ImmediateDocIndex(DocIndex): pass def create_immediate_document_index(pdf_url, folder, keys)->DocIndex: from langchain.document_loaders import UnstructuredMarkdownLoader from langchain.document_loaders import JSONLoader from langchain.document_loaders import UnstructuredHTMLLoader from langchain.document_loaders.csv_loader import CSVLoader from langchain.document_loaders import UnstructuredWordDocumentLoader from langchain.document_loaders import TextLoader pdf_url = pdf_url.strip() # check if the link is local or remote is_remote = pdf_url.startswith("http") or pdf_url.startswith("ftp") or pdf_url.startswith("s3") or pdf_url.startswith("gs") or pdf_url.startswith("azure") or pdf_url.startswith("https") or pdf_url.startswith("www.") if is_remote: pdf_url = convert_to_pdf_link_if_needed(pdf_url) is_pdf = is_pdf_link(pdf_url) else: is_pdf = pdf_url.endswith(".pdf") # based on extension of the pdf_url decide on the loader to use, in case no extension is present then try pdf, word, html, markdown in that order. logger.info(f"Creating immediate doc index for {pdf_url}, is_remote = {is_remote}, is_pdf = {is_pdf}") if is_pdf: doc_text = PDFReaderTool(keys)(pdf_url) elif pdf_url.endswith(".docx"): doc_text = UnstructuredWordDocumentLoader(pdf_url).load()[0].page_content elif is_remote and not (pdf_url.endswith(".md") or pdf_url.endswith(".json") or pdf_url.endswith(".csv") or pdf_url.endswith(".txt")): html = fetch_html(pdf_url, keys["zenrows"], keys["brightdataUrl"]) # save this html to a file and then use the html loader. html_file = os.path.join(folder, "temp.html") with open(html_file, "w") as f: f.write(html) convert_doc_to_pdf(html_file, html_file.replace(".html", ".pdf")) pdf_url = html_file.replace(".html", ".pdf") # delete html file os.remove(html_file) doc_text = UnstructuredHTMLLoader(html_file).load()[0].page_content elif pdf_url.endswith(".html"): doc_text = UnstructuredHTMLLoader(pdf_url).load()[0].page_content elif pdf_url.endswith(".md"): doc_text = UnstructuredMarkdownLoader(pdf_url).load()[0].page_content elif pdf_url.endswith(".json"): doc_text = JSONLoader(pdf_url).load()[0].page_content elif pdf_url.endswith(".csv"): doc_text = CSVLoader(pdf_url).load()[0].page_content elif pdf_url.endswith(".txt"): doc_text = TextLoader(pdf_url).load()[0].page_content else: try: doc_text = PDFReaderTool(keys)(pdf_url.strip()) except Exception as e: try: doc_text = UnstructuredWordDocumentLoader()(pdf_url.strip()).load()[0].page_content except Exception as e: try: doc_text = UnstructuredHTMLLoader()(pdf_url.strip()).load()[0].page_content except Exception as e: try: doc_text = UnstructuredMarkdownLoader()(pdf_url.strip()).load()[0].page_content except Exception as e: raise Exception(f"Could not find a suitable loader for the given url {pdf_url}") doc_text = doc_text.replace('<|endoftext|>', '\n').replace('endoftext', 'end_of_text').replace('<|endoftext|>', '') chunks = get_async_future(ChunkText, doc_text, LARGE_CHUNK_LEN, 64) small_chunks = get_async_future(ChunkText, doc_text, SMALL_CHUNK_LEN, 32) chunks, small_chunks = chunks.result(), small_chunks.result() nested_dict = { 'chunked_summary': [''], 'chunks': chunks, "small_chunks": small_chunks, 'running_summary': '', 'detailed_qna': [], 'deep_reader_details': { "methodology": {"id":"", "text":""}, "previous_literature_and_differentiation": {"id":"", "text":""}, "experiments_and_evaluation": {"id":"", "text":""}, "results_and_comparison": {"id":"", "text":""}, "limitations_and_future_work" : {"id":"", "text":""}, } } openai_embed = get_embedding_model(keys) try: doc_index: DocIndex = ImmediateDocIndex(pdf_url, "pdf", "scientific_article", doc_text, nested_dict, openai_embed, folder) # for k in doc_index.store_separate: # doc_index.set_doc_data(k, None, doc_index.get_doc_data(k), overwrite=True) def get_doc_ready(): doc_index.set_api_keys(keys) return doc_index.get_short_info() _ = get_async_future(get_doc_ready) doc_index._visible = True except Exception as e: doc_id = str(mmh3.hash(pdf_url + "pdf" + "scientific_article", signed=False)) try: folder = os.path.join(folder, f"{doc_id}") if os.path.exists(folder): shutil.rmtree(folder) except Exception as e: pass logger.error(f"Error creating immediate doc index for {pdf_url}") raise e return doc_index
[ "Further we have certain additional instructions to follow while writing this review: ```", "The given text is part of a document. Write a detailed summary which contains all important and essential information from the given text. Summarize the text:\n '{}' \nSummary: \n", "Write a detailed overall summary of a document from given sectional summary of parts of the document. Ignore References. \nSectional Summaries:\n'{}'\nProvide elaborate, detailed, comprehensive, informative and in-depth summary. Overall Summary:\n", "none", "Answer the question or query given below using the given context as reference. \nQuestion or Query is given below.\nPLACEHOLDER\n\nContext is given below.\nPLACEHOLDER\n\nWrite answer below.\n", " ", " Make your review sound ", "chunks", "meta_review", "We also have previous reviews with same tone on this paper to assist in writing this review. Previous reviews: ```", " \n" ]
2024-01-10
Luksuz/Fitness-app
server~endpoints~ChatBotEndpoint.py
from flask import Blueprint, request from utils.ResponseUtil import ResponseUtil from service.ChatBotService import ChatBotService from bson import ObjectId import openai chatbot = Blueprint("chatbot", __name__) userLifestyleService = ChatBotService() class ChatBotEndpoint: @staticmethod @chatbot.route("/chatbot", methods=["POST"]) def getChatbotResponse(): messagesHistory = request.json["message"] chatbotService = ChatBotService() message, dietPlan, trainingPlan, maintananceCalories, goal, cutBulkRate, workoutExperience, healthIssues = chatbotService.interactWithChatbot(messagesHistory) return ResponseUtil.buildResponse({ "role": "assistant", "content": message, "dietPlan": dietPlan, "trainingPlan": trainingPlan, "maintananceCalories": maintananceCalories, "goal": goal, "cutBulkRate": cutBulkRate, "workoutExperience": workoutExperience, "healthIssues": healthIssues })
[]
2024-01-10
AIObjectives/talk-to-the-city-reports
scatter~pipeline~steps~labelling.py
"""Create labels for the clusters.""" from tqdm import tqdm from typing import List import numpy as np import pandas as pd from langchain.chat_models import ChatOpenAI from utils import messages, update_progress def labelling(config): dataset = config['output_dir'] path = f"outputs/{dataset}/labels.csv" arguments = pd.read_csv(f"outputs/{dataset}/args.csv") clusters = pd.read_csv(f"outputs/{dataset}/clusters.csv") results = pd.DataFrame() sample_size = config['labelling']['sample_size'] prompt = config['labelling']['prompt'] model = config['labelling']['model'] question = config['question'] cluster_ids = clusters['cluster-id'].unique() update_progress(config, total=len(cluster_ids)) for _, cluster_id in tqdm(enumerate(cluster_ids), total=len(cluster_ids)): args_ids = clusters[clusters['cluster-id'] == cluster_id]['arg-id'].values args_ids = np.random.choice(args_ids, size=min( len(args_ids), sample_size), replace=False) args_sample = arguments[arguments['arg-id'] .isin(args_ids)]['argument'].values args_ids_outside = clusters[clusters['cluster-id'] != cluster_id]['arg-id'].values args_ids_outside = np.random.choice(args_ids_outside, size=min( len(args_ids_outside), sample_size), replace=False) args_sample_outside = arguments[arguments['arg-id'] .isin(args_ids_outside)]['argument'].values label = generate_label(question, args_sample, args_sample_outside, prompt, model) results = pd.concat([results, pd.DataFrame( [{'cluster-id': cluster_id, 'label': label}])], ignore_index=True) update_progress(config, incr=1) results.to_csv(path, index=False) def generate_label(question, args_sample, args_sample_outside, prompt, model): llm = ChatOpenAI(model_name=model, temperature=0.0) outside = '\n * ' + '\n * '.join(args_sample_outside) inside = '\n * ' + '\n * '.join(args_sample) input = f"Question of the consultation:{question}\n\n" + \ f"Examples of arguments OUTSIDE the cluster:\n {outside}" + \ f"Examples of arguments INSIDE the cluster:\n {inside}" response = llm(messages=messages(prompt, input)).content.strip() return response
[ "labelling" ]
2024-01-10
AIObjectives/talk-to-the-city-reports
scatter~pipeline~steps~translation.py
import json from tqdm import tqdm import pandas as pd from langchain.chat_models import ChatOpenAI from utils import messages from langchain.schema import AIMessage import pandas as pd import json from tqdm import tqdm def translation(config): dataset = config['output_dir'] path = f"outputs/{dataset}/translations.json" results = {} languages = list(config.get('translation', {}).get('languages', [])) if len(languages) == 0: print("No languages specified. Skipping translation step.") # creating an empty file any, to reduce special casing later with open(path, 'w') as file: json.dump(results, file, indent=2) return arguments = pd.read_csv(f"outputs/{dataset}/args.csv") labels = pd.read_csv(f"outputs/{dataset}/labels.csv") takeaways = pd.read_csv(f"outputs/{dataset}/takeaways.csv") with open(f"outputs/{dataset}/overview.txt") as f: overview = f.read() UI_copy = ["Argument", "Original comment", "Representative arguments", "Open full-screen map", "Back to report", "Hide labels", "Show labels", "Show filters", "Hide filters", "Min. votes", "Consensus", "Showing", "arguments", "Reset zoom", "Click anywhere on the map to close this", "Click on the dot for details", "agree", "disagree", "Language", "English", "arguments", "of total", "Overview", "Cluster analysis", "Representative comments", "Introduction", "Clusters", "Appendix", "This report was generated using an AI pipeline that consists of the following steps", "Step", "extraction", "show code", "hide code", "show prompt", "hide prompt", "embedding", "clustering", "labelling", "takeaways", "overview"] arg_list = arguments['argument'].to_list() + \ labels['label'].to_list() + \ UI_copy + \ languages if 'name' in config: arg_list.append(config['name']) if 'question' in config: arg_list.append(config['question']) prompt_file = config.get('translation_prompt', 'default') with open(f"prompts/translation/{prompt_file}.txt") as f: prompt = f.read() model = config['model'] config['translation_prompt'] = prompt translations = [translate_lang( arg_list, 10, prompt, lang, model) for lang in languages] # handling long takeaways differently, WITHOUT batching too much long_arg_list = takeaways['takeaways'].to_list() long_arg_list.append(overview) if 'intro' in config: long_arg_list.append(config['intro']) long_translations = [translate_lang( long_arg_list, 1, prompt, lang, model) for lang in languages] for i, id in enumerate(arg_list): print('i, id', i, id) results[str(id)] = list([t[i] for t in translations]) for i, id in enumerate(long_arg_list): results[str(id)] = list([t[i] for t in long_translations]) with open(path, 'w') as file: json.dump(results, file, indent=2) def translate_lang(arg_list, batch_size, prompt, lang, model): translations = [] lang_prompt = prompt.replace("{language}", lang) print(f"Translating to {lang}...") for i in tqdm(range(0, len(arg_list), batch_size)): batch = arg_list[i: i + batch_size] translations.extend(translate_batch(batch, lang_prompt, model)) return translations def translate_batch(batch, lang_prompt, model, retries=3): llm = ChatOpenAI(model_name=model, temperature=0.0) input = json.dumps(list(batch)) response = llm(messages=messages(lang_prompt, input)).content.strip() if "```" in response: response = response.split("```")[1] if response.startswith("json"): response = response[4:] try: parsed = [a.strip() for a in json.loads(response)] if len(parsed) != len(batch): print("Warning: batch size mismatch!") print("Batch len:", len(batch)) print("Response len:", len(parsed)) for i, item in enumerate(batch): print(f"Batch item {i}:", item) if (i < len(parsed)): print("Response:", parsed[i]) if (len(batch) > 1): print("Retrying with smaller batches...") mid = len(batch) // 2 return translate_batch(batch[:mid], lang_prompt, model, retries - 1) + \ translate_batch( batch[mid:], lang_prompt, model, retries - 1) else: print("Retrying batch...") return translate_batch(batch, lang_prompt, model, retries - 1) else: return parsed except json.decoder.JSONDecodeError as e: print("JSON error:", e) print("Response was:", response) if retries > 0: print("Retrying batch...") return translate_batch(batch, lang_prompt, model, retries - 1) else: raise e
[ "{language}", "translation_prompt" ]
2024-01-10
AIObjectives/talk-to-the-city-reports
scatter~pipeline~steps~overview.py
"""Create summaries for the clusters.""" from tqdm import tqdm import os from typing import List import numpy as np import pandas as pd from langchain.chat_models import ChatOpenAI from utils import messages, update_progress def overview(config): dataset = config['output_dir'] path = f"outputs/{dataset}/overview.txt" takeaways = pd.read_csv(f"outputs/{dataset}/takeaways.csv") labels = pd.read_csv(f"outputs/{dataset}/labels.csv") prompt = config['overview']['prompt'] model = config['overview']['model'] ids = labels['cluster-id'].to_list() takeaways.set_index('cluster-id', inplace=True) labels.set_index('cluster-id', inplace=True) input = '' for i, id in enumerate(ids): input += f"# Cluster {i}/{len(ids)}: {labels.loc[id]['label']}\n\n" input += takeaways.loc[id]['takeaways'] + '\n\n' llm = ChatOpenAI(model_name=model, temperature=0.0) response = llm(messages=messages(prompt, input)).content.strip() with open(path, 'w') as file: file.write(response)
[]
2024-01-10
AIObjectives/talk-to-the-city-reports
scatter~pipeline~steps~aggregation.py
"""Generate a convenient JSON output file.""" from tqdm import tqdm from typing import List import pandas as pd from langchain.chat_models import ChatOpenAI import json def aggregation(config): path = f"outputs/{config['output_dir']}/result.json" results = { "clusters": [], "comments": {}, "translations": {}, "overview": "", "config": config, } arguments = pd.read_csv(f"outputs/{config['output_dir']}/args.csv") arguments.set_index('arg-id', inplace=True) comments = pd.read_csv(f"inputs/{config['input']}.csv") useful_comment_ids = set(arguments['comment-id'].values) for _, row in comments.iterrows(): id = row['comment-id'] if id in useful_comment_ids: res = {'comment': row['comment-body']} numeric_cols = ['agrees', 'disagrees'] string_cols = ['video', 'interview', 'timestamp'] for col in numeric_cols: if col in row: res[col] = float(row[col]) for col in string_cols: if col in row: res[col] = row[col] results['comments'][str(id)] = res languages = list(config.get('translation', {}).get('languages', [])) if len(languages) > 0: with open(f"outputs/{config['output_dir']}/translations.json") as f: translations = f.read() results['translations'] = json.loads(translations) clusters = pd.read_csv(f"outputs/{config['output_dir']}/clusters.csv") labels = pd.read_csv(f"outputs/{config['output_dir']}/labels.csv") takeaways = pd.read_csv(f"outputs/{config['output_dir']}/takeaways.csv") takeaways.set_index('cluster-id', inplace=True) with open(f"outputs/{config['output_dir']}/overview.txt") as f: overview = f.read() results['overview'] = overview for _, row in labels.iterrows(): cid = row['cluster-id'] label = row['label'] arg_rows = clusters[clusters['cluster-id'] == cid] arguments_in_cluster = [] for _, arg_row in arg_rows.iterrows(): arg_id = arg_row['arg-id'] argument = arguments.loc[arg_id]['argument'] comment_id = arguments.loc[arg_id]['comment-id'] x = float(arg_row['x']) y = float(arg_row['y']) p = float(arg_row['probability']) obj = { 'arg_id': arg_id, 'argument': argument, 'comment_id': str(comment_id), 'x': x, 'y': y, 'p': p, } arguments_in_cluster.append(obj) results['clusters'].append({ 'cluster': label, 'cluster_id': str(cid), 'takeaways': takeaways.loc[cid]['takeaways'], 'arguments': arguments_in_cluster }) with open(path, 'w') as file: json.dump(results, file, indent=2)
[]
2024-01-10
AIObjectives/talk-to-the-city-reports
scatter~pipeline~steps~takeaways.py
"""Create summaries for the clusters.""" from tqdm import tqdm import os from typing import List import numpy as np import pandas as pd from langchain.chat_models import ChatOpenAI from utils import messages, update_progress def takeaways(config): dataset = config['output_dir'] path = f"outputs/{dataset}/takeaways.csv" arguments = pd.read_csv(f"outputs/{dataset}/args.csv") clusters = pd.read_csv(f"outputs/{dataset}/clusters.csv") results = pd.DataFrame() sample_size = config['takeaways']['sample_size'] prompt = config['takeaways']['prompt'] model = config['takeaways']['model'] model = config.get('model_takeaways', config.get('model', 'gpt3.5-turbo')) cluster_ids = clusters['cluster-id'].unique() update_progress(config, total=len(cluster_ids)) for _, cluster_id in tqdm(enumerate(cluster_ids), total=len(cluster_ids)): args_ids = clusters[clusters['cluster-id'] == cluster_id]['arg-id'].values args_ids = np.random.choice(args_ids, size=min( len(args_ids), sample_size), replace=False) args_sample = arguments[arguments['arg-id'] .isin(args_ids)]['argument'].values label = generate_takeaways(args_sample, prompt, model) results = pd.concat([results, pd.DataFrame( [{'cluster-id': cluster_id, 'takeaways': label}])], ignore_index=True) update_progress(config, incr=1) results.to_csv(path, index=False) def generate_takeaways(args_sample, prompt, model): llm = ChatOpenAI(model_name=model, temperature=0.0) input = "\n".join(args_sample) response = llm(messages=messages(prompt, input)).content.strip() return response
[]
2024-01-10
LIANGKE23/Knowledge_Assisted_Medical_Dialogue_Generation_Mechanism
bert-gpt~bert_gpt_train_dpt.py
import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import Dataset, TensorDataset, DataLoader import numpy as np import fire import time import os from tqdm import tqdm # uses allennlp modules from allennlp.nn import util # imports chinese gpt from chinese_gpt import TransformerEncoder, TransformerDecoderLM # uses bert chinese wordpiece tokenization from pytorch_pretrained_bert import OpenAIAdam class MyDataset(Dataset): def __init__(self, *data): self.data = data def __getitem__(self, index): return tuple(data[index] for data in self.data) def __len__(self): return len(self.data[0]) def collate_fn(batch): pad_id = 0 input_ids = [] output_ids = [] input_mask = [] output_mask =[] btc_size = len(batch) max_input_len = 0 # 该batch中最长的input,用于该batch的数据对齐 max_output_len = 0 # 计算该batch中input的最大长度 for btc_idx in range(btc_size): if max_input_len < len(batch[btc_idx][0]): max_input_len = len(batch[btc_idx][0]) if max_output_len < len(batch[btc_idx][1]): max_output_len = len(batch[btc_idx][1]) # 使用pad_id对小于max_input_len的input_id进行补全 for btc_idx in range(btc_size): input_len = len(batch[btc_idx][0]) input_ids.append(batch[btc_idx][0]) input_ids[btc_idx].extend([pad_id] * (max_input_len - input_len)) output_len = len(batch[btc_idx][1]) output_ids.append(batch[btc_idx][1]) output_ids[btc_idx].extend([pad_id] * (max_output_len - output_len)) input_mask.append([1] * input_len + [pad_id] * (max_input_len - input_len)) output_mask.append([1] * output_len + [pad_id] * (max_output_len - output_len)) return tuple((torch.tensor(input_ids, dtype=torch.long), torch.tensor(output_ids, dtype=torch.long), torch.tensor(input_mask, dtype=torch.long), torch.tensor(output_mask, dtype=torch.long))) class BertGPT(nn.Module): def __init__(self): super().__init__() self.encoder = TransformerEncoder() # for p in self.parameters(): # p.requires_grad=False self.decoder = TransformerDecoderLM() def forward(self, encoder_input, mask_encoder_input, decoder_input, mask_decoder_input): _, past = self.encoder(encoder_input, mask_encoder_input) mask = torch.cat([mask_encoder_input, mask_decoder_input], dim=1) logits, _ = self.decoder(decoder_input, mask, past=past, past_length=0) return logits def train_model( epochs=10, num_gradients_accumulation=4, batch_size=4, gpu_id=0, lr=1e-5, load_dir='decoder_model_medDG_multi', decoder_model='./original_pretrained_model_for_bertGPT.pth' ): # make sure your model is on GPU device = torch.device(f"cuda:{gpu_id}") # device = torch.device("cpu") #------------------------LOAD MODEL----------------- print('load the model....') model = BertGPT() model.load_state_dict(torch.load(decoder_model)) # model = nn.DataParallel(model, device_ids = [0,1,2]) model = model.to(device) print('load success') #------------------------END LOAD MODEL-------------- #------------------------LOAD TRAIN DATA------------------ train_data = torch.load("/mnt/kg_data/data_medDG/train_kg_multi.pth") train_dataset = MyDataset(*train_data) train_dataloader = DataLoader(dataset=train_dataset, shuffle=True, batch_size=batch_size, num_workers=2, collate_fn=collate_fn) val_data = torch.load("/mnt/kg_data/data_medDG/dev_kg_multi.pth") val_dataset = MyDataset(*val_data) val_dataloader = DataLoader(dataset=val_dataset, shuffle=True, batch_size=batch_size, num_workers=2, collate_fn=collate_fn) #------------------------END LOAD TRAIN DATA-------------- #------------------------SET OPTIMIZER------------------- num_train_optimization_steps = len(train_dataset) * epochs // batch_size // num_gradients_accumulation param_optimizer = list(model.named_parameters()) no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] optimizer_grouped_parameters = [ {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay) and p.requires_grad], 'weight_decay': 0.01}, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay) and p.requires_grad], 'weight_decay': 0.0} ] print('train') print(len(optimizer_grouped_parameters[0]['params'])) optimizer = OpenAIAdam(optimizer_grouped_parameters, lr=lr, warmup=0.01, max_grad_norm=1.0, weight_decay=0.01, t_total=num_train_optimization_steps) #------------------------END SET OPTIMIZER-------------- #------------------------START TRAINING------------------- update_count = 0 start = time.time() print('start training....') for epoch in range(epochs): #------------------------training------------------------ model.train() losses = 0 times = 0 for batch in tqdm(train_dataloader, desc='dirs'): batch = [item.to(device) for item in batch] encoder_input, decoder_input, mask_encoder_input, mask_decoder_input = batch logits = model(encoder_input, mask_encoder_input, decoder_input, mask_decoder_input) out = logits[:, :-1].contiguous() target = decoder_input[:, 1:].contiguous() target_mask = mask_decoder_input[:, 1:].contiguous() loss = util.sequence_cross_entropy_with_logits(out, target, target_mask, average="token") loss.backward() losses += loss.item() times += 1 update_count += 1 if update_count % num_gradients_accumulation == num_gradients_accumulation - 1: optimizer.step() optimizer.zero_grad() end = time.time() print('-'*20 + f'epoch {epoch}' + '-'*20) print(f'time: {(end - start)}') print(f'loss: {losses / times}') start = end #------------------------validate------------------------ model.eval() perplexity = 0 batch_count = 0 print('start calculate the perplexity....') with torch.no_grad(): for batch in tqdm(val_dataloader): batch = [item.to(device) for item in batch] encoder_input, decoder_input, mask_encoder_input, mask_decoder_input = batch logits = model(encoder_input, mask_encoder_input, decoder_input, mask_decoder_input) out = logits[:, :-1].contiguous() target = decoder_input[:, 1:].contiguous() target_mask = mask_decoder_input[:, 1:].contiguous() loss = util.sequence_cross_entropy_with_logits(out, target, target_mask, average="token") perplexity += np.exp(loss.item()) batch_count += 1 print(f'validate perplexity: {perplexity / batch_count}') # torch.save(model.module.state_dict(), os.path.join(os.path.abspath('.'), load_dir, str(epoch) + "decoder.pth")) torch.save(model.state_dict(), os.path.join(os.path.abspath('.'), load_dir, str(epoch) + "decoder.pth")) #------------------------END TRAINING------------------- if __name__ == '__main__': fire.Fire(train_model)
[]
2024-01-10
Ljmarcer/miniAgi
controller.py
# Imports import os import openai import json from termcolor import colored import re import subprocess import time import configparser # OpenAI Key setup def openai_key(): config = configparser.ConfigParser() config.read('config.ini') openai_key = config.get('openai', 'key') return openai_key # Agents initialization def initialize_agent_file(file_path: str, agent_prompt: str) -> None: initial_data = [{"role": "system", "content": agent_prompt.strip()}] with open(file_path, "w") as f: json.dump(initial_data, f, indent=4) # Goal Description # Agent call def call_agent(message: str, agent: str = "MasterAgent") -> str: file_path = os.path.join("agents", f"{agent}.json") # Load the agent's JSON file with open(file_path, "r") as f: messages = json.load(f) # Add the user message to the messages list messages.append({"role": "user", "content": message}) # Call the API with the agent's messages completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, temperature=0.3, ) # Extract the response response = completion.choices[0].message["content"] #response = "this is a response" # Add the assistant's response to the messages list messages.append({"role": "assistant", "content": response.strip()}) # Save the updated messages list back to the JSON file with open(file_path, "w") as f: json.dump(messages, f, indent=4) # Print the response in a different color based on the agent # color = "green" if agent == "MasterAgent" else "blue" if agent == "CodeAgent" else "yellow" # print(colored(response, color)) return response # # Master Agent Response # def parse_master_agent_response(response: str): # agent_match = re.search(r"/(CodeAgent|EnvAgent) (.+)", response) # if agent_match: # agent = agent_match.group(1) # message = agent_match.group(2) # return agent, message # else: # return "MasterAgent", response, None def parse_master_agent_response(response: str): task_match = re.search(r"Task: (.+)", response) agent_match = re.search(r"/(CodeAgent|EnvAgent) (.+)", response) if task_match and agent_match: task = task_match.group(1) agent = agent_match.group(1) message = agent_match.group(2) return agent, task, message else: return "MasterAgent", response, None # Code Agent Response def code_to_output(response: str): code_match = re.search(r"```(?:python|javascript)\s*(.*?)```", response, re.DOTALL) filename_match = re.search(r"File: (.+)", response) if filename_match: filename = filename_match.group(1) else: filename = "output.txt" with open(filename, "w") as file: if code_match: file.write(code_match.group(1)) return code_match.group(1) if code_match else None # Env Agent Response def execute_command(command:str): result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) return result.stdout.decode('utf-8') # Color response def print_response(response: str, agent: str) -> None: if agent == "MasterAgent": print(colored(f'{agent} : {response}', "blue")) elif agent == "EnvAgent": print(colored(f'{agent} responded with : {response}', "green")) elif agent == "CodeAgent": print(colored(f'{agent} responded with : {response}', "yellow")) else: print(response) # Loop interaction agents def loop_interaction(): goal = input("insert a goal\n ") prompt =f'Goal:{goal}' response = call_agent(prompt) last_agent = "" while not "/quit_auto" in response: time.sleep(2) agent, task_description, deleg = parse_master_agent_response(response) print(colored("Current Process: \n", attrs=['bold']) if agent!= "MasterAgent" else colored(f"Master Agent Input: \n", attrs=['bold']) , colored(task_description,"blue") ) print(colored("Working agent",attrs=['bold']),colored(agent,'magenta',attrs=['bold','underline'])) if agent != "MasterAgent" else None if agent == "CodeAgent": code_response = call_agent(deleg, "CodeAgent") print_response(code_response,agent) code_to_output(code_response) response = code_response last_agent = agent elif agent == "EnvAgent": env_response = call_agent(deleg, "EnvAgent") print_response(env_response,agent) response = f'Did:{env_response} , Console output:{execute_command(env_response)}' if execute_command(env_response) != None else f'Did:{env_response}, no console output' last_agent = agent else: response = call_agent(response) #print_response(task_description,"MasterAgent") def main(): print(colored("Welcome to the AutoAgents System!", 'green','on_red')) openai.api_key = openai_key() #Agents System prompts MASTER_AGENT_PROMPT= '''You are an AI, MasterAgent, responsible for managing the workflow of a project. Your role is to guide the user through the process of achieving a specific goal by providing tasks in a step-by-step manner. You are a proficient code developer and systems manager. As you provide each task, immediately delegate it to the appropriate specialized agent (EnvAgent or CodeAgent) using a specific and well-defined instruction. NOTE: The agents and the user are operating in an emulated terminal environment without GUI capabilities, it only can produce natural language responses. This means that commands requiring user interaction. Keep this in mind when delegating tasks. In this workflow, the code generated should include test in the same script as the main functions . If a modification to a file is needed, ask CodeAgent to generate the necessary code, then ask EnvAgent to copy the content of "output.txt" to the desired file. When providing tasks and delegating them to agents, follow this format in your responses: "Task: (Explain the task to be performed.) Delegation: /AgentName (task_description)" Example: "Task: First, we need to ensure that Python is installed on the system. Deleg: /EnvAgent Check if Python is installed." As the MasterAgent, you can: - Provide tasks to be performed - Delegate tasks to the appropriate specialized agents - Analyze results provided by the user - Guide users to the subsequent tasks However, you DO NOT: - Provide code or commands - Perform tasks yourself - Communicate with agents other than in natural language EnvAgent is responsible for handling environment-related tasks. It can: - Create, modify, or delete files and directories - Run consecutive commands ( like create a folder and cd into it) - Execute scripts or programs - Manage services and packages - Manage system configurations EnvAgent responds with system commands, but it DOES NOT: - Generate code - Have knowledge of CodeAgent activities. - Use the graphical interface of the system. - OPEN text editors or IDEs. - Open files. CodeAgent is responsible for generating code in any requested programming language. It can: - Write functions, classes, or entire programs - Generate code snippets or examples - Explain or describe code concepts - Save file to a specified filename in the call. CodeAgent DOES NOT: - Create, execute, or manage files. For each interaction with the user, provide the next task to be performed in order to achieve the goal. As you provide the task, delegate it to the appropriate agent within the same response using specific and well-defined instructions. After a task is completed, analyze the result provided by the user, and guide them to the subsequent task. Repeat this process until the goal is achieved. Remember, you are only communicating with agents in natural language. Each agent has no context of other agents, so don't mention other agents when calling an agent. Explain the task_description in great detail for the agent to understand it. You are the only one with context, acting as the central manager of the project. When you feel goal has been achieved include the following in your response: "/quit_auto". ''' CODE_AGENT_PROMPT='''Act as an AI, CodeAgent, responsible for providing code in any requested programming language for a project managed by the MasterAgent. When responding with code, always present it in the following markdown format: 'Code: ```language{code}```'. Include any previously given code in your response, and make modifications based on the feedback if necessary. Provide only one piece of code per response. You will interact exclusively with the MasterAgent, supplying code solutions when called upon. The code should include a function and a call to that same function in the same script. This means the code you produce have to be put in a file and be executed fullfiling the requirements. When receiving a task that includes a filename , include the filename in your response, so that the generated code is saved to that specific file. Follow this format in your responses: ' Code: ```language{code}``` File: {filename} Modified: (Only included to explain differences respect previous code if exists) ' IMPORTANT: Do not use multiple code snippets. Stick to one code block per response.''' ENV_AGENT_PROMPT='''Act as an AI, EnvAgent, responsible for handling environment-related tasks in a project managed by MasterAgent. Respond only with commands to modify the system, such as creating files, copying text or code, executing files, installing packages, or managing services. Do not produce any other type of text response. Wait for the MasterAgent to call you with a task and provide the appropriate command as a response. Do not produce more than pure text. If you consider more than one command should be used concatenate them with &&. You will be asked to run files, use python3 for that purpose. ''' agent_files_path = "agents" initialize_agent_file(os.path.join(agent_files_path, "MasterAgent.json"), MASTER_AGENT_PROMPT) initialize_agent_file(os.path.join(agent_files_path, "CodeAgent.json"), CODE_AGENT_PROMPT) initialize_agent_file(os.path.join(agent_files_path, "EnvAgent.json"), ENV_AGENT_PROMPT) loop_interaction() # initialize the main function if __name__ == "__main__": main()
[ "Act as an AI, EnvAgent, responsible for handling environment-related tasks in a project managed by MasterAgent. \n Respond only with commands to modify the system, such as creating files, copying text or code, executing files, installing packages, or managing services.\n Do not produce any other type of text response. Wait for the MasterAgent to call you with a task and provide the appropriate command as a response. \n Do not produce more than pure text. If you consider more than one command should be used concatenate them with &&. You will be asked to run files, use python3 for that purpose.\n ", "Goal:PLACEHOLDER", "Act as an AI, CodeAgent, responsible for providing code in any requested programming language for a project managed by the MasterAgent. When responding with code, always present it in the following markdown format: 'Code: ```language{code}```'. Include any previously given code in your response, and make modifications based on the feedback if necessary. Provide only one piece of code per response. You will interact exclusively with the MasterAgent, supplying code solutions when called upon.\n The code should include a function and a call to that same function in the same script. This means the code you produce have to be put in a file and be executed fullfiling the requirements.\n When receiving a task that includes a filename , include the filename in your response, so that the generated code is saved to that specific file.\n\n Follow this format in your responses:\n '\n Code: ```language{code}```\n File: {filename}\n Modified: (Only included to explain differences respect previous code if exists)\n '\n IMPORTANT: Do not use multiple code snippets. Stick to one code block per response.", "You are an AI, MasterAgent, responsible for managing the workflow of a project. Your role is to guide the user through the process of achieving a specific goal by providing tasks in a step-by-step manner. You are a proficient code developer and systems manager. As you provide each task, immediately delegate it to the appropriate specialized agent (EnvAgent or CodeAgent) using a specific and well-defined instruction.\n\n NOTE: The agents and the user are operating in an emulated terminal environment without GUI capabilities, it only can produce natural language responses. This means that commands requiring user interaction. Keep this in mind when delegating tasks.\n In this workflow, the code generated should include test in the same script as the main functions . If a modification to a file is needed, ask CodeAgent to generate the necessary code, then ask EnvAgent to copy the content of \"output.txt\" to the desired file.\n\n When providing tasks and delegating them to agents, follow this format in your responses:\n \"Task: (Explain the task to be performed.)\n Delegation: /AgentName (task_description)\"\n\n Example:\n \"Task: First, we need to ensure that Python is installed on the system. \n Deleg: /EnvAgent Check if Python is installed.\"\n\n As the MasterAgent, you can:\n - Provide tasks to be performed\n - Delegate tasks to the appropriate specialized agents\n - Analyze results provided by the user\n - Guide users to the subsequent tasks\n\n However, you DO NOT:\n - Provide code or commands\n - Perform tasks yourself\n - Communicate with agents other than in natural language\n\n EnvAgent is responsible for handling environment-related tasks. It can:\n - Create, modify, or delete files and directories\n - Run consecutive commands ( like create a folder and cd into it)\n - Execute scripts or programs\n - Manage services and packages\n - Manage system configurations\n EnvAgent responds with system commands, but it DOES NOT:\n - Generate code \n - Have knowledge of CodeAgent activities.\n - Use the graphical interface of the system.\n - OPEN text editors or IDEs.\n - Open files.\n CodeAgent is responsible for generating code in any requested programming language. It can:\n - Write functions, classes, or entire programs\n - Generate code snippets or examples\n - Explain or describe code concepts\n - Save file to a specified filename in the call.\n CodeAgent DOES NOT:\n - Create, execute, or manage files.\n For each interaction with the user, provide the next task to be performed in order to achieve the goal. As you provide the task, delegate it to the appropriate agent within the same response using specific and well-defined instructions.\n After a task is completed, analyze the result provided by the user, and guide them to the subsequent task. Repeat this process until the goal is achieved.\n Remember, you are only communicating with agents in natural language. Each agent has no context of other agents, so don't mention other agents when calling an agent. Explain the task_description in great detail for the agent to understand it. You are the only one with context, acting as the central manager of the project.\n When you feel goal has been achieved include the following in your response: \"/quit_auto\".\n " ]
2024-01-10
honeyhiveai/honeyhive-cookbook
docs~scripts~quickstart_completions_openai.py
!pip install honeyhive -q import honeyhive from honeyhive.sdk.utils import fill_template from openai import OpenAI import time honeyhive.api_key = "HONEYHIVE_API_KEY" client = OpenAI(api_key="OPENAI_API_KEY") USER_TEMPLATE = "Write me an email on {{topic}} in a {{tone}} tone." user_inputs = { "topic": "AI Services", "tone": "Friendly" } #"Write an email on AI Services in a Friendly tone." user_message = fill_template(USER_TEMPLATE, user_inputs) start = time.perf_counter() openai_response = client.chat.completions.create( model="gpt-3.5-turbo", temperature=0.7, max_tokens=100, messages=[ {"role": "system", "content": "You are a helpful assistant who writes emails."}, {"role": "user", "content": user_message} ] ) end = time.perf_counter() request_latency = (end - start)*1000 generation = openai_response.choices[0].message.content token_usage = openai_response.usage response = honeyhive.generations.log( project="Sandbox - Email Writer", source="staging", model="gpt-3.5-turbo", hyperparameters={ "temperature": 0.7, "max_tokens": 100, }, prompt_template=USER_TEMPLATE, inputs=user_inputs, generation=generation, metadata={ "session_id": session_id # Optionally specify a session id to track related completions }, usage=token_usage, latency=request_latency, user_properties={ "user_device": "Macbook Pro", "user_Id": "92739527492", "user_country": "United States", "user_subscriptiontier": "Enterprise", "user_tenantID": "Acme Inc." } ) from honeyhive.sdk.feedback import generation_feedback generation_feedback( project="Sandbox - Email Writer", generation_id=response.generation_id, ground_truth="INSERT_GROUND_TRUTH_LABEL", feedback_json={ "provided": True, "accepted": False, "edited": True } ) import honeyhive honeyhive.api_key = "HONEYHIVE_API_KEY" honeyhive.openai_api_key = "OPENAI_API_KEY" response = honeyhive.generations.generate( project="Sandbox - Email Writer", source="staging", input={ "topic": "Model evaluation for companies using GPT-4", "tone": "friendly" }, )
[ "You are a helpful assistant who writes emails.", "Write me an email on {{topic}} in a {{tone}} tone." ]
2024-01-10
honeyhiveai/honeyhive-cookbook
docs~scripts~quickstart_llamaindex_.py
pip install honeyhive -q import honeyhive import os from honeyhive.utils.llamaindex_tracer import HoneyHiveLlamaIndexTracer os.environ["HONEYHIVE_API_KEY"] = "YOUR_HONEYHIVE_API_KEY" tracer = HoneyHiveLlamaIndexTracer( project="PG Q&A Bot", # necessary field: specify which project within HoneyHive name="Paul Graham Q&A", # optional field: name of the chain/agent you are running source="staging", # optional field: source (to separate production & staging environments) user_properties={ # optional field: specify user properties for whom this was ran "user_id": "sd8298bxjn0s", "user_account": "Acme" "user_country": "United States", "user_subscriptiontier": "enterprise" } ) from llama_index import VectorStoreIndex, SimpleWebPageReader, ServiceContext from llama_index.callbacks import CallbackManager, LlamaDebugHandler import openai openai.api_key = "YOUR_OPENAI_API_KEY" # Initialize the service context with the HoneyHive tracer callback_manager = CallbackManager([tracer]) service_context = ServiceContext.from_defaults(callback_manager=callback_manager) documents = SimpleWebPageReader(html_to_text=True).load_data( ["http://paulgraham.com/worked.html"] ) # Pass the service_context to the index that you will query index = VectorStoreIndex.from_documents( documents, service_context=service_context ) query_engine = index.as_query_engine() response = query_engine.query("What did the author do growing up?") honeyhive.sessions.feedback( session_id = tracer.session_id, feedback = { "accepted": True, "saved": True, "regenerated": False, "edited": False } )
[]
2024-01-10
honeyhiveai/honeyhive-cookbook
docs~scripts~quickstart_evaluations_sequential_runs.py
!pip install honeyhive -q import honeyhive import openai import time # import any other vector databases, APIs and other model providers you might need honeyhive.api_key = "HONEYHIVE_API_KEY" openai.api_key = "OPENAI_API_KEY" honeyhive_eval = HoneyHiveEvaluation( project="Email Writer App", name="Max Tokens Comparison", description="Finding best max tokens for OpenAI chat models", dataset_name="Test", metrics=["Conciseness", "Number of Characters"] ) dataset = [ {"topic": "Test", "tone": "test"}, {"topic": "AI", "tone": "neutral"} ] # in case you have a saved dataset in HoneyHive from honeyhive.sdk.datasets import get_dataset dataset = get_dataset("Email Writer Samples") config = { "name": "max_tokens_100", "model": "gpt-3.5-turbo", "provider": "openai", "hyperparameters": { "temperature": 0.5, "max_tokens": 100 }, "chat": [ { "role": "system", "content": "You are a helpful assistant who helps people write emails.", }, { "role": "user", "content": "Topic: {{topic}}\n\nTone: {{tone}}." } ] } for data in dataset: data_run = [] messages = honeyhive.utils.fill_chat_template(config["chat"], data) start = time.time() openai_response = openai.ChatCompletion.create( model=config["model"], messages=messages, **config["hyperparameters"] ) end = time.time() # log a particular run via log_run honeyhive_eval.log_run( config=config, input=data, completion=openai_response.choices[0].message.content, metrics={ "cost": honeyhive.utils.calculate_openai_cost( config["model"], openai_response.usage ), "latency": (end - start) * 1000, "response_length": openai_response.usage["completion_tokens"], **openai_response["usage"] } ) config = { # same configuration as above here "hyperparameters": {"temperature": 0.5, "max_tokens": 400}, } # identical Evaluation Run code as above for data in dataset: ... honeyhive_eval.log_comment("Results are decent") honeyhive_eval.finish()
[ "You are a helpful assistant who helps people write emails.", "Topic: {{topic}}\n\nTone: {{tone}}." ]
2024-01-10
honeyhiveai/honeyhive-cookbook
docs~scripts~quickstart_completions_anthropic.py
!pip install honeyhive -q import honeyhive from honeyhive.sdk.utils import fill_template from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT import time honeyhive.api_key = "HONEYHIVE_API_KEY" anthropic = Anthropic( api_key="ANTHROPIC_API_KEY", ) USER_TEMPLATE = f"{HUMAN_PROMPT} Write me an email on {{topic}} in a {{tone}} tone.{AI_PROMPT}" user_inputs = { "topic": "AI Services", "tone": "Friendly" } #"Write an email on AI Services in a Friendly tone." user_message = fill_template(USER_TEMPLATE, user_inputs) start = time.perf_counter() completion = anthropic.completions.create( model="claude-2", max_tokens_to_sample=300, prompt=user_message ) end = time.perf_counter() request_latency = (end - start)*1000 generation = completion.completion token_usage = { "completion_tokens": anthropic.count_tokens(completion.completion), "prompt_tokens": anthropic.count_tokens(user_message), "total_tokens": anthropic.count_tokens(completion.completion) + anthropic.count_tokens(user_message) } response = honeyhive.generations.log( project="Sandbox - Email Writer", source="staging", model="claude-2", hyperparameters={ "max_tokens_to_sample": 300, }, prompt_template=USER_TEMPLATE, inputs=user_inputs, generation=generation, metadata={ "session_id": session_id # Optionally specify a session id to track related completions }, usage=token_usage, latency=request_latency, user_properties={ "user_device": "Macbook Pro", "user_Id": "92739527492", "user_country": "United States", "user_subscriptiontier": "Enterprise", "user_tenantID": "Acme Inc." } ) from honeyhive.sdk.feedback import generation_feedback generation_feedback( project="Sandbox - Email Writer", generation_id=response.generation_id, ground_truth="INSERT_GROUND_TRUTH_LABEL", feedback_json={ "provided": True, "accepted": False, "edited": True } ) import honeyhive honeyhive.api_key = "HONEYHIVE_API_KEY" honeyhive.openai_api_key = "OPENAI_API_KEY" response = honeyhive.generations.generate( project="Sandbox - Email Writer", source="staging", input={ "topic": "Model evaluation for companies using GPT-4", "tone": "friendly" }, )
[ "PLACEHOLDER Write me an email on {topic} in a {tone} tone.PLACEHOLDER" ]
2024-01-10
honeyhiveai/honeyhive-cookbook
docs~scripts~quickstart_evaluations_parallelized_runs.py
!pip install honeyhive -q import honeyhive import openai import time # import any other vector databases, APIs and other model providers you might need honeyhive.api_key = "HONEYHIVE_API_KEY" openai.api_key = "OPENAI_API_KEY" honeyhive_eval = HoneyHiveEvaluation( project="Email Writer App", name="Max Tokens Comparison", description="Finding best max tokens for OpenAI chat models", dataset_name="Test", metrics=["Conciseness", "Number of Characters"] ) dataset = [ {"topic": "Test", "tone": "test"}, {"topic": "AI", "tone": "neutral"} ] # in case you have a saved dataset in HoneyHive from honeyhive.sdk.datasets import get_dataset dataset = get_dataset("Email Writer Samples") config = { "name": "max_tokens_100", "model": "gpt-3.5-turbo", "provider": "openai", "hyperparameters": { "temperature": 0.5, "max_tokens": 100 }, "chat": [ { "role": "system", "content": "You are a helpful assistant who helps people write emails.", }, { "role": "user", "content": "Topic: {{topic}}\n\nTone: {{tone}}." } ] } # parallelized version of the evaluation run code import concurrent.futures def parallel_task(data, config): data_run = [] messages = honeyhive.utils.fill_chat_template(config["chat"], data) start = time.time() openai_response = openai.ChatCompletion.create( model=config["model"], messages=messages, **config["hyperparameters"] ) end = time.time() honeyhive_eval.log_run( config=config, input=data, completion=openai_response.choices[0].message.content, metrics={ "cost": honeyhive.utils.calculate_openai_cost( config["model"], openai_response.usage ), "latency": (end - start) * 1000, "response_length": openai_response.usage["completion_tokens"], **openai_response["usage"] } ) with concurrent.futures.ThreadPoolExecutor() as executor: futures = [] for data in dataset: for config in configs: futures.append(executor.submit(parallel_task, data, config)) for future in concurrent.futures.as_completed(futures): # Do any further processing if required on the results pass config = { # same configuration as above here "hyperparameters": {"temperature": 0.5, "max_tokens": 400}, } # identical Evaluation Run code as above for data in dataset: ... honeyhive_eval.log_comment("Results are decent") honeyhive_eval.finish()
[ "You are a helpful assistant who helps people write emails.", "Topic: {{topic}}\n\nTone: {{tone}}." ]