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
damiangilgonzalez1995/TalkDocument
example~direct.py
from langchain import document_loaders as dl from langchain import text_splitter as ts from langchain import embeddings from langchain import vectorstores as vs from langchain import retrievers from langchain.chains.question_answering import load_qa_chain from langchain import HuggingFaceHub from langchain import PromptTemplate # Some constant DS_TYPE_LIST = ["WEB", "PDF", "TXT"] SPLIT_TYPE_LIST = ["CHARACTER", "TOKEN"] EMBEDDING_TYPE_LIST = ["HF", "OPENAI"] VECTORSTORE_TYPE_LIST = ["FAISS", "CHROMA", "SVM"] REPO_ID_DEFAULT = "declare-lab/flan-alpaca-large" CHAIN_TYPE_LIST = ["stuff", "map_reduce", "map_rerank", "refine"] class TalkDocument(object): """ TalkDocument is a class for processing and interacting with documents, embeddings, and question-answering chains. Attributes: data_source_path (str): Path to the data source (TXT, PDF, or web URL). HF_API_TOKEN (str): Hugging Face API token. OPENAI_KEY (str): OpenAI API key. document (str): Loaded document content. document_splited (list): List of document chunks after splitting. embedding_model (EmbeddingsBase): Embedded model instance. embedding_type (str): Type of embedding model used (HF or OPENAI). db (VectorStoreBase): Vector storage instance. llm (HuggingFaceHub): Hugging Face Hub instance. chain (QuestionAnsweringChain): Question answering chain instance. repo_id (str): Repository ID for Hugging Face models. Methods: get_document(data_source_type="TXT"): Load the document content based on the data source type. get_split(split_type="character", chunk_size=1000, chunk_overlap=10): Split the document content into chunks. get_embedding(embedding_type="HF", OPENAI_KEY=None): Get the embedding model based on the type. get_storage(vectorstore_type="FAISS", embedding_type="HF", OPENAI_KEY=None): Create vector storage using embeddings. get_search(question, with_score=False): Perform a similarity search for relevant documents. do_question(question, repo_id="declare-lab/flan-alpaca-large", chain_type="stuff", relevant_docs=None, with_score=False, temperature=0, max_length=300, language="Spanish"): Answer a question using relevant documents and a question-answering chain. create_db_document(data_source_type="TXT", split_type="token", chunk_size=200, embedding_type="HF", chunk_overlap=10, OPENAI_KEY=None, vectorstore_type="FAISS"): Create and return a vector storage instance with document content. """ def __init__(self, HF_API_TOKEN, data_source_path=None, data_text=None, OPENAI_KEY=None) -> None: """ Initialize the TalkDocument instance. :param data_source_path: Path to the data source (TXT, PDF, or web URL). :type data_source_path: str :param HF_API_TOKEN: Hugging Face API token. :type HF_API_TOKEN: str :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional """ self.data_source_path = data_source_path self.data_text = data_text self.document = None self.document_splited = None self.embedding_model = None self.embedding_type = None self.OPENAI_KEY = OPENAI_KEY self.HF_API_TOKEN = HF_API_TOKEN self.db = None self.llm = None self.chain = None self.repo_id = None if not self.data_source_path and not self.data_text: #TODO ADD LOGS print("YOU MUST INTRODUCE ONE OF THEM") def get_document(self, data_source_type="TXT"): """ Load the document content based on the data source type. :param data_source_type: Type of data source (TXT, PDF, WEB). :type data_source_type: str, optional :return: Loaded document content. :rtype: str """ data_source_type = data_source_type if data_source_type.upper() in DS_TYPE_LIST else DS_TYPE_LIST[0] if data_source_type == "TXT": if self.data_text: self.document = self.data_text elif self.data_source_path: loader = dl.TextLoader(self.data_source_path) self.document = loader.load() elif data_source_type == "PDF": if self.data_text: self.document = self.data_text elif self.data_source_path: loader = dl.PyPDFLoader(self.data_source_path) self.document = loader.load() elif data_source_type == "WEB": loader = dl.WebBaseLoader(self.data_source_path) self.document = loader.load() return self.document def get_split(self, split_type="character", chunk_size=200, chunk_overlap=10): """ Split the document content into chunks. :param split_type: Type of splitting (character, token). :type split_type: str, optional :param chunk_size: Size of each chunk. :type chunk_size: int, optional :param chunk_overlap: Overlap size between chunks. :type chunk_overlap: int, optional :return: List of document chunks after splitting. :rtype: list """ split_type = split_type.upper() if split_type.upper() in SPLIT_TYPE_LIST else SPLIT_TYPE_LIST[0] if self.document: if split_type == "CHARACTER": text_splitter = ts.RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) elif split_type == "TOKEN": text_splitter = ts.TokenTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) if self.data_text: try: self.document_splited = text_splitter.split_text(text=self.document) except Exception as error: print(f"Error in split data text step: {error}") elif self.data_source_path: try: self.document_splited = text_splitter.split_documents(documents=self.document) except Exception as error: print(f"Error in split data source step: {error}") return self.document_splited def get_embedding(self, embedding_type="HF", OPENAI_KEY=None): """ Get the embedding model based on the type. :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :return: Embedded model instance. :rtype: EmbeddingsBase """ if not self.embedding_model: embedding_type = embedding_type.upper() if embedding_type.upper() in EMBEDDING_TYPE_LIST else EMBEDDING_TYPE_LIST[0] if embedding_type == "HF": self.embedding_model = embeddings.HuggingFaceEmbeddings() elif embedding_type == "OPENAI": self.OPENAI_KEY = self.OPENAI_KEY if self.OPENAI_KEY else OPENAI_KEY if self.OPENAI_KEY: self.embedding_model = embeddings.OpenAIEmbeddings(openai_api_key=OPENAI_KEY) else: print("You need to introduce a OPENAI API KEY") self.embedding_type = embedding_type return self.embedding_model def get_storage(self, vectorstore_type = "FAISS", embedding_type="HF", OPENAI_KEY=None): """ Create vector storage using embeddings. :param vectorstore_type: Type of vector storage (FAISS, CHROMA, SVM). :type vectorstore_type: str, optional :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :return: Vector storage instance. :rtype: VectorStoreBase """ self.embedding_type = self.embedding_type if self.embedding_type else embedding_type vectorstore_type = vectorstore_type.upper() if vectorstore_type.upper() in VECTORSTORE_TYPE_LIST else VECTORSTORE_TYPE_LIST[0] self.get_embedding(embedding_type=self.embedding_type, OPENAI_KEY=OPENAI_KEY) if vectorstore_type == "FAISS": model_vectorstore = vs.FAISS elif vectorstore_type == "CHROMA": model_vectorstore = vs.Chroma elif vectorstore_type == "SVM": model_vectorstore = retrievers.SVMRetriever # TODO # elif vectorstore_type == "LANCE": # model_vectorstore = vs.LanceDB if self.data_text: try: self.db = model_vectorstore.from_texts(self.document_splited, self.embedding_model) except Exception as error: print(f"Error in storage data text step: {error}") self.db = None elif self.data_source_path: try: self.db = model_vectorstore.from_documents(self.document_splited, self.embedding_model) except Exception as error: print(f"Error in storage data source step: {error}") self.db = None return self.db def get_search(self, question, with_score=False): """ Perform a similarity search for relevant documents. :param question: Question text. :type question: str :param with_score: Flag indicating whether to include relevance scores. :type with_score: bool, optional :return: Relevant documents or document indices. :rtype: list or ndarray """ # TODO MultiQueryRetriever AND Max marginal relevance relevant_docs = None if self.db and "SVM" not in str(type(self.db)): if with_score: relevant_docs = self.db.similarity_search_with_relevance_scores(question) else: relevant_docs = self.db.similarity_search(question) elif self.db: relevant_docs = self.db.get_relevant_documents(question) return relevant_docs def do_question(self, question, repo_id="declare-lab/flan-alpaca-large", chain_type="stuff", relevant_docs=None, with_score=False, temperature=0, max_length=300, language="Spanish"): """ Answer a question using relevant documents and a question-answering chain. :param question: Question text. :type question: str :param repo_id: Repository ID for Hugging Face models. :type repo_id: str, optional :param chain_type: Type of question-answering chain (stuff, ...). :type chain_type: str, optional :param relevant_docs: Relevant documents or document indices. :type relevant_docs: list or ndarray, optional :param with_score: Flag indicating whether to include relevance scores. :type with_score: bool, optional :param temperature: Sampling temperature for generating answers. :type temperature: float, optional :param max_length: Maximum length of generated answers. :type max_length: int, optional :param language: Language of the answer. :type language: str, optional :return: Answer to the question. :rtype: str """ relevant_docs = self.get_search(question, with_score=with_score) if relevant_docs: self.repo_id = self.repo_id if self.repo_id is not None else repo_id chain_type = chain_type.lower() if chain_type.lower() in CHAIN_TYPE_LIST else CHAIN_TYPE_LIST[0] if (self.repo_id != repo_id ) or (self.llm is None): self.repo_id = repo_id self.llm = HuggingFaceHub(repo_id=self.repo_id,huggingfacehub_api_token=self.HF_API_TOKEN, model_kwargs= {"temperature":temperature, "max_length": max_length}) prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. If the question is similar to [Talk me about the document], the response should be a summary commenting on the most important points about the document {context} Question: {question} """ PROMPT = PromptTemplate( template=prompt_template, input_variables=["context", "question"] ) PROMPT = PROMPT + f" The Answer have to be in {language} language:" self.chain = self.chain if self.chain is not None else load_qa_chain(self.llm, chain_type=chain_type, prompt = PROMPT) response = self.chain({"input_documents": relevant_docs, "question": question}, return_only_outputs=True) return response else: return {"output_text": "ERROR: Something went wrong and the query could not be performed. Check the data source and its access"} def create_db_document(self, data_source_type="TXT", split_type="token", chunk_size=200, embedding_type="HF", chunk_overlap=10, OPENAI_KEY=None, vectorstore_type = "FAISS"): """ Create and return a vector storage instance with document content. :param data_source_type: Type of data source (TXT, PDF, WEB). :type data_source_type: str, optional :param split_type: Type of splitting (token, character). :type split_type: str, optional :param chunk_size: Size of each chunk. :type chunk_size: int, optional :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param chunk_overlap: Overlap size between chunks. :type chunk_overlap: int, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :param vectorstore_type: Type of vector storage (FAISS, CHROMA, SVM). :type vectorstore_type: str, optional :return: Vector storage instance. :rtype: VectorStoreBase """ self.get_document(data_source_type=data_source_type) self.get_split(split_type=split_type, chunk_size=chunk_size, chunk_overlap=chunk_overlap) db = self.get_storage(vectorstore_type=vectorstore_type, embedding_type=embedding_type, OPENAI_KEY=OPENAI_KEY) return db # ********************************** EXAMPLE ********************************** # obj = TalkDocument(HF_API_TOKEN = "YOURKEY",data_source_path="data/test.txt") # obj.create_db_document() # question = "What is Hierarchy 4.0?" # res = obj.do_question(question=question, language="ENGLISH") # print(res) """ RESPONSE: {'output_text': "Hierarchy 4.0 is an innovative software solution for control Safety Systems. It provides an interactive diagram of the entire plant revealing cause and effect Behavior with readings provided in a hierarchical view allowing for a deep understanding of the system's strategy. All data is collected from multiple sources visualized as a diagram and optimized through a customized dashboard allowing users to run a logic simulation from live data or pick a moment from their history. Your simulation is based on actual safety Logics not just on a math model."} """
[ "PROMPT7cfc7605-cf24-400b-86ae-d950d4b960db The Answer have to be in PLACEHOLDER language: The Answer have to be in PLACEHOLDER language:", "Use the following pieces of context to answer the question at the end. \n If you don't know the answer, just say that you don't know, don't try to make up an answer.\n If the question is similar to [Talk me about the document], \n the response should be a summary commenting on the most important points about the document\n\n\n {context}\n Question: {question}\n ", "question", "t know the answer, just say that you don", "context" ]
2024-01-10
damiangilgonzalez1995/TalkDocument
src~qa_tool.py
from langchain import document_loaders as dl from langchain import text_splitter as ts from langchain import embeddings from langchain import vectorstores as vs from langchain import retrievers from langchain.chains.question_answering import load_qa_chain from langchain import HuggingFaceHub from langchain import PromptTemplate from utils import util class TalkDocument(object): """ TalkDocument is a class for processing and interacting with documents, embeddings, and question-answering chains. Attributes: data_source_path (str): Path to the data source (TXT, PDF, or web URL). HF_API_TOKEN (str): Hugging Face API token. OPENAI_KEY (str): OpenAI API key. document (str): Loaded document content. document_splited (list): List of document chunks after splitting. embedding_model (EmbeddingsBase): Embedded model instance. embedding_type (str): Type of embedding model used (HF or OPENAI). db (VectorStoreBase): Vector storage instance. llm (HuggingFaceHub): Hugging Face Hub instance. chain (QuestionAnsweringChain): Question answering chain instance. repo_id (str): Repository ID for Hugging Face models. Methods: get_document(data_source_type="TXT"): Load the document content based on the data source type. get_split(split_type="character", chunk_size=1000, chunk_overlap=10): Split the document content into chunks. get_embedding(embedding_type="HF", OPENAI_KEY=None): Get the embedding model based on the type. get_storage(vectorstore_type="FAISS", embedding_type="HF", OPENAI_KEY=None): Create vector storage using embeddings. get_search(question, with_score=False): Perform a similarity search for relevant documents. do_question(question, repo_id="declare-lab/flan-alpaca-large", chain_type="stuff", relevant_docs=None, with_score=False, temperature=0, max_length=300, language="Spanish"): Answer a question using relevant documents and a question-answering chain. create_db_document(data_source_type="TXT", split_type="token", chunk_size=200, embedding_type="HF", chunk_overlap=10, OPENAI_KEY=None, vectorstore_type="FAISS"): Create and return a vector storage instance with document content. """ def __init__(self, HF_API_TOKEN, data_source_path=None, data_text=None, OPENAI_KEY=None) -> None: """ Initialize the TalkDocument instance. :param data_source_path: Path to the data source (TXT, PDF, or web URL). :type data_source_path: str :param HF_API_TOKEN: Hugging Face API token. :type HF_API_TOKEN: str :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional """ self.data_source_path = data_source_path self.data_text = data_text self.document = None self.document_splited = None self.embedding_model = None self.embedding_type = None self.OPENAI_KEY = OPENAI_KEY self.HF_API_TOKEN = HF_API_TOKEN self.db = None self.llm = None self.chain = None self.repo_id = None if not self.data_source_path and not self.data_text: #TODO ADD LOGS print("YOU MUST INTRODUCE ONE OF THEM") def get_document(self, data_source_type="TXT"): """ Load the document content based on the data source type. :param data_source_type: Type of data source (TXT, PDF, WEB). :type data_source_type: str, optional :return: Loaded document content. :rtype: str """ data_source_type = data_source_type if data_source_type.upper() in util.DS_TYPE_LIST else util.DS_TYPE_LIST[0] if data_source_type == "TXT": if self.data_text: self.document = self.data_text elif self.data_source_path: loader = dl.TextLoader(self.data_source_path) self.document = loader.load() elif data_source_type == "PDF": if self.data_text: self.document = self.data_text elif self.data_source_path: loader = dl.PyPDFLoader(self.data_source_path) self.document = loader.load() elif data_source_type == "WEB": loader = dl.WebBaseLoader(self.data_source_path) self.document = loader.load() return self.document def get_split(self, split_type="character", chunk_size=200, chunk_overlap=10): """ Split the document content into chunks. :param split_type: Type of splitting (character, token). :type split_type: str, optional :param chunk_size: Size of each chunk. :type chunk_size: int, optional :param chunk_overlap: Overlap size between chunks. :type chunk_overlap: int, optional :return: List of document chunks after splitting. :rtype: list """ split_type = split_type.upper() if split_type.upper() in util.SPLIT_TYPE_LIST else util.SPLIT_TYPE_LIST[0] if self.document: if split_type == "CHARACTER": text_splitter = ts.RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) elif split_type == "TOKEN": text_splitter = ts.TokenTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) if self.data_text: try: self.document_splited = text_splitter.split_text(text=self.document) except Exception as error: print(f"Error in split data text step: {error}") elif self.data_source_path: try: self.document_splited = text_splitter.split_documents(documents=self.document) except Exception as error: print(f"Error in split data source step: {error}") return self.document_splited def get_embedding(self, embedding_type="HF", OPENAI_KEY=None): """ Get the embedding model based on the type. :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :return: Embedded model instance. :rtype: EmbeddingsBase """ if not self.embedding_model: embedding_type = embedding_type.upper() if embedding_type.upper() in util.EMBEDDING_TYPE_LIST else util.EMBEDDING_TYPE_LIST[0] if embedding_type == "HF": self.embedding_model = embeddings.HuggingFaceEmbeddings() elif embedding_type == "OPENAI": self.OPENAI_KEY = self.OPENAI_KEY if self.OPENAI_KEY else OPENAI_KEY if self.OPENAI_KEY: self.embedding_model = embeddings.OpenAIEmbeddings(openai_api_key=OPENAI_KEY) else: print("You need to introduce a OPENAI API KEY") self.embedding_type = embedding_type return self.embedding_model def get_storage(self, vectorstore_type = "FAISS", embedding_type="HF", OPENAI_KEY=None): """ Create vector storage using embeddings. :param vectorstore_type: Type of vector storage (FAISS, CHROMA, SVM). :type vectorstore_type: str, optional :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :return: Vector storage instance. :rtype: VectorStoreBase """ self.embedding_type = self.embedding_type if self.embedding_type else embedding_type vectorstore_type = vectorstore_type.upper() if vectorstore_type.upper() in util.VECTORSTORE_TYPE_LIST else util.VECTORSTORE_TYPE_LIST[0] self.get_embedding(embedding_type=self.embedding_type, OPENAI_KEY=OPENAI_KEY) if vectorstore_type == "FAISS": model_vectorstore = vs.FAISS elif vectorstore_type == "CHROMA": model_vectorstore = vs.Chroma elif vectorstore_type == "SVM": model_vectorstore = retrievers.SVMRetriever # TODO # elif vectorstore_type == "LANCE": # model_vectorstore = vs.LanceDB if self.data_text: try: self.db = model_vectorstore.from_texts(self.document_splited, self.embedding_model) except Exception as error: print(f"Error in storage data text step: {error}") self.db = None elif self.data_source_path: try: self.db = model_vectorstore.from_documents(self.document_splited, self.embedding_model) except Exception as error: print(f"Error in storage data source step: {error}") self.db = None return self.db def get_search(self, question, with_score=False): """ Perform a similarity search for relevant documents. :param question: Question text. :type question: str :param with_score: Flag indicating whether to include relevance scores. :type with_score: bool, optional :return: Relevant documents or document indices. :rtype: list or ndarray """ # TODO MultiQueryRetriever AND Max marginal relevance relevant_docs = None if self.db and "SVM" not in str(type(self.db)): if with_score: relevant_docs = self.db.similarity_search_with_relevance_scores(question) else: relevant_docs = self.db.similarity_search(question) elif self.db: relevant_docs = self.db.get_relevant_documents(question) return relevant_docs def do_question(self, question, repo_id="declare-lab/flan-alpaca-large", chain_type="stuff", relevant_docs=None, with_score=False, temperature=0, max_length=300, language="Spanish"): """ Answer a question using relevant documents and a question-answering chain. :param question: Question text. :type question: str :param repo_id: Repository ID for Hugging Face models. :type repo_id: str, optional :param chain_type: Type of question-answering chain (stuff, ...). :type chain_type: str, optional :param relevant_docs: Relevant documents or document indices. :type relevant_docs: list or ndarray, optional :param with_score: Flag indicating whether to include relevance scores. :type with_score: bool, optional :param temperature: Sampling temperature for generating answers. :type temperature: float, optional :param max_length: Maximum length of generated answers. :type max_length: int, optional :param language: Language of the answer. :type language: str, optional :return: Answer to the question. :rtype: str """ relevant_docs = self.get_search(question, with_score=with_score) if relevant_docs: self.repo_id = self.repo_id if self.repo_id is not None else repo_id chain_type = chain_type.lower() if chain_type.lower() in util.CHAIN_TYPE_LIST else util.CHAIN_TYPE_LIST[0] if (self.repo_id != repo_id ) or (self.llm is None): self.repo_id = repo_id self.llm = HuggingFaceHub(repo_id=self.repo_id,huggingfacehub_api_token=self.HF_API_TOKEN, model_kwargs= {"temperature":temperature, "max_length": max_length}) prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. If the question is similar to [Talk me about the document], the response should be a summary commenting on the most important points about the document {context} Question: {question} """ PROMPT = PromptTemplate( template=prompt_template, input_variables=["context", "question"] ) PROMPT = PROMPT + f" The Answer have to be in {language} language:" self.chain = self.chain if self.chain is not None else load_qa_chain(self.llm, chain_type=chain_type, prompt = PROMPT) response = self.chain({"input_documents": relevant_docs, "question": question}, return_only_outputs=True) return response else: return {"output_text": "ERROR: Something went wrong and the query could not be performed. Check the data source and its access"} def create_db_document(self, data_source_type="TXT", split_type="token", chunk_size=200, embedding_type="HF", chunk_overlap=10, OPENAI_KEY=None, vectorstore_type = "FAISS"): """ Create and return a vector storage instance with document content. :param data_source_type: Type of data source (TXT, PDF, WEB). :type data_source_type: str, optional :param split_type: Type of splitting (token, character). :type split_type: str, optional :param chunk_size: Size of each chunk. :type chunk_size: int, optional :param embedding_type: Type of embedding model (HF, OPENAI). :type embedding_type: str, optional :param chunk_overlap: Overlap size between chunks. :type chunk_overlap: int, optional :param OPENAI_KEY: OpenAI API key. :type OPENAI_KEY: str, optional :param vectorstore_type: Type of vector storage (FAISS, CHROMA, SVM). :type vectorstore_type: str, optional :return: Vector storage instance. :rtype: VectorStoreBase """ self.get_document(data_source_type=data_source_type) self.get_split(split_type=split_type, chunk_size=chunk_size, chunk_overlap=chunk_overlap) db = self.get_storage(vectorstore_type=vectorstore_type, embedding_type=embedding_type, OPENAI_KEY=OPENAI_KEY) return db
[ "PROMPTe94df2a1-da59-45cd-87d9-ca09e0fde724 The Answer have to be in PLACEHOLDER language: The Answer have to be in PLACEHOLDER language:", "Use the following pieces of context to answer the question at the end. \n If you don't know the answer, just say that you don't know, don't try to make up an answer.\n If the question is similar to [Talk me about the document], \n the response should be a summary commenting on the most important points about the document\n\n\n {context}\n Question: {question}\n ", "question", "t know the answer, just say that you don", "context" ]
2024-01-10
MUGE-2021/image-generation-baseline
models~vae.py
''' Adapted from https://github.com/lucidrains/DALLE-pytorch/blob/main/dalle_pytorch/vae.py ''' import io import sys import os import requests import PIL import warnings import hashlib import urllib import yaml from pathlib import Path from tqdm import tqdm from math import sqrt, log from omegaconf import OmegaConf from taming.models.vqgan import VQModel, GumbelVQ import importlib import torch from torch import nn import torch.nn.functional as F from einops import rearrange # constants CACHE_PATH = os.path.expanduser("~/.cache/muge") OPENAI_VAE_ENCODER_PATH = 'https://cdn.openai.com/dall-e/encoder.pkl' OPENAI_VAE_DECODER_PATH = 'https://cdn.openai.com/dall-e/decoder.pkl' VQGAN_VAE_PATH = 'https://heibox.uni-heidelberg.de/f/140747ba53464f49b476/?dl=1' VQGAN_VAE_CONFIG_PATH = 'https://heibox.uni-heidelberg.de/f/6ecf2af6c658432c8298/?dl=1' # helpers methods def exists(val): return val is not None def default(val, d): return val if exists(val) else d def load_model(path): with open(path, 'rb') as f: return torch.load(f, map_location = torch.device('cpu')) def map_pixels(x, eps = 0.1): return (1 - 2 * eps) * x + eps def unmap_pixels(x, eps = 0.1): return torch.clamp((x - eps) / (1 - 2 * eps), 0, 1) def download(url, filename = None, root = CACHE_PATH, load_only=False): os.makedirs(root, exist_ok = True) filename = default(filename, os.path.basename(url)) download_target = os.path.join(root, filename) download_target_tmp = os.path.join(root, f'tmp.{filename}') if load_only is False: if os.path.exists(download_target) and not os.path.isfile(download_target): raise RuntimeError(f"{download_target} exists and is not a regular file") if os.path.isfile(download_target): return download_target with urllib.request.urlopen(url) as source, open(download_target_tmp, "wb") as output: with tqdm(total=int(source.info().get("Content-Length")), ncols=80) as loop: while True: buffer = source.read(8192) if not buffer: break output.write(buffer) loop.update(len(buffer)) os.rename(download_target_tmp, download_target) return download_target def make_contiguous(module): with torch.no_grad(): for param in module.parameters(): param.set_(param.contiguous()) # pretrained Discrete VAE from OpenAI class OpenAIDiscreteVAE(nn.Module): def __init__(self, image_size=256, load_only=False): super().__init__() self.enc = load_model(download(OPENAI_VAE_ENCODER_PATH, load_only=load_only)) self.dec = load_model(download(OPENAI_VAE_DECODER_PATH, load_only=load_only)) make_contiguous(self) self.num_layers = 3 self.image_size = image_size self.num_tokens = 8192 @torch.no_grad() def get_codebook_indices(self, img): img = map_pixels(img) z_logits = self.enc.blocks(img) z = torch.argmax(z_logits, dim = 1) return rearrange(z, 'b h w -> b (h w)') def decode(self, img_seq): b, n = img_seq.shape img_seq = rearrange(img_seq, 'b (h w) -> b h w', h = int(sqrt(n))) z = F.one_hot(img_seq, num_classes = self.num_tokens) z = rearrange(z, 'b h w c -> b c h w').float() x_stats = self.dec(z).float() x_rec = unmap_pixels(torch.sigmoid(x_stats[:, :3])) return x_rec def forward(self, img): raise NotImplemented # VQGAN from Taming Transformers paper # https://arxiv.org/abs/2012.09841 def get_obj_from_str(string, reload=False): module, cls = string.rsplit(".", 1) if reload: module_imp = importlib.import_module(module) importlib.reload(module_imp) return getattr(importlib.import_module(module, package=None), cls) def instantiate_from_config(config): if not "target" in config: raise KeyError("Expected key `target` to instantiate.") return get_obj_from_str(config["target"])(**config.get("params", dict())) class VQGanVAE(nn.Module): def __init__(self, vqgan_model_path=None, vqgan_config_path=None, image_size=256, load_only=False): super().__init__() if vqgan_model_path is None: model_filename = 'vqgan.1024.model.ckpt' config_filename = 'vqgan.1024.config.yml' download(VQGAN_VAE_CONFIG_PATH, config_filename, load_only=load_only) download(VQGAN_VAE_PATH, model_filename, load_only=load_only) config_path = str(Path(CACHE_PATH) / config_filename) model_path = str(Path(CACHE_PATH) / model_filename) else: model_path = vqgan_model_path config_path = vqgan_config_path config = OmegaConf.load(config_path) model = instantiate_from_config(config["model"]) state = torch.load(model_path, map_location = 'cpu')['state_dict'] model.load_state_dict(state, strict = False) print(f"Loaded VQGAN from {model_path} and {config_path}") self.model = model # f as used in https://github.com/CompVis/taming-transformers#overview-of-pretrained-models f = config.model.params.ddconfig.resolution / config.model.params.ddconfig.attn_resolutions[0] self.num_layers = int(log(f)/log(2)) self.image_size = image_size self.num_tokens = config.model.params.n_embed self.is_gumbel = isinstance(self.model, GumbelVQ) # self._register_external_parameters() def load_model(self): pass @torch.no_grad() def get_codebook_indices(self, img): b = img.shape[0] img = (2 * img) - 1 _, _, [_, _, indices] = self.model.encode(img) if self.is_gumbel: return rearrange(indices, 'b h w -> b (h w)', b=b) return rearrange(indices, '(b n) -> b n', b = b) def decode(self, img_seq): b, n = img_seq.shape one_hot_indices = F.one_hot(img_seq, num_classes = self.num_tokens).float() z = one_hot_indices @ self.model.quantize.embed.weight if self.is_gumbel \ else (one_hot_indices @ self.model.quantize.embedding.weight) z = rearrange(z, 'b (h w) c -> b c h w', h = int(sqrt(n))) img = self.model.decode(z) img = (img.clamp(-1., 1.) + 1) * 0.5 return img def forward(self, img): raise NotImplemented
[]
2024-01-10
MUGE-2021/image-generation-baseline
models~t2i_baseline.py
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ Muge-t2i-baseline """ from typing import Optional import logging import torch import torch.nn as nn import torch.distributed as dist import torch.nn.functional as F from fairseq import utils from fairseq.models import register_model, register_model_architecture from fairseq.modules.transformer_sentence_encoder import init_bert_params from .transformer import TransformerModel # from models.modeling_discrete_vae import Dalle_VAE from models.vae import OpenAIDiscreteVAE, VQGanVAE logger = logging.getLogger(__name__) @register_model("t2i_baseline") class T2IBaselineModel(TransformerModel): __jit_unused_properties__ = ["supported_targets"] def __init__(self, args, encoder, decoder): super().__init__(args, encoder, decoder) # We follow BERT's random weight initialization self.apply(init_bert_params) self.classification_heads = nn.ModuleDict() if hasattr(self.encoder, "dictionary"): self.eos: int = self.encoder.dictionary.eos() if hasattr(self.decoder, "dictionary"): self.tgt_eos: int = self.decoder.dictionary.eos() if args.distributed_world_size == 1: load_only = False else: load_only = (dist.get_rank() % args.nprocs_per_node) != 0 # only download ckpt in first proc of node if args.vae_model_type == "dallevae": vae = OpenAIDiscreteVAE(image_size=args.code_image_size, load_only=load_only) elif args.vae_model_type == "vqgan": vae = VQGanVAE(image_size=args.code_image_size, load_only=load_only) else: raise Exception("Unknown vae_model_type {}".format(args.vae_model_type)) vae.eval() vae.requires_grad_(False) self.vae = vae assert args.share_all_embeddings is False @staticmethod def add_args(parser): super(T2IBaselineModel, T2IBaselineModel).add_args(parser) parser.add_argument( "--pooler-dropout", type=float, metavar="D", help="dropout probability in the masked_lm pooler layers", ) parser.add_argument( "--pooler-classifier", type=str, choices=['mlp', 'linear'], help="type of pooler classifier", ) parser.add_argument( "--pooler-activation-fn", choices=utils.get_available_activation_fns(), help="activation function to use for pooler layer", ) parser.add_argument( "--spectral-norm-classification-head", action="store_true", help="Apply spectral normalization on the classification head", ) parser.add_argument( "--code-image-size", type=int, help="code reconstructed image size", ) parser.add_argument( "--vae-model-type", type=str, default='dallevae', choices=['dallevae', 'vqgan'], help="path of vae model", ) @property def supported_targets(self): return {"self"} def i2c(self, patch_images: Optional[torch.Tensor] = None): return self.vae.get_codebook_indices(patch_images) def c2i(self, code_indices: Optional[torch.Tensor] = None): return self.vae.decode(code_indices) def forward( self, src_tokens, src_lengths, prev_output_tokens, patch_images: Optional[torch.Tensor] = None, features_only: bool = False, classification_head_name: Optional[str] = None, token_embeddings: Optional[torch.Tensor] = None, return_all_hiddens: bool = False, alignment_layer: Optional[int] = None, alignment_heads: Optional[int] = None, sample=None ): if classification_head_name is not None: features_only = True # encode text encoder_out = self.encoder( src_tokens, src_lengths=src_lengths, patch_images=None, token_embeddings=token_embeddings, return_all_hiddens=return_all_hiddens, ) # encode image img_tokens = F.pad(self.vae.get_codebook_indices(patch_images), [1, 0], value=self.tgt_eos) prev_output_tokens = img_tokens[:, :-1] targets = img_tokens[:, 1:].detach().contiguous() # update target info sample['target'] = targets N, L = targets.shape sample['ntokens'] = N*L x, extra = self.decoder( prev_output_tokens, encoder_out=encoder_out, features_only=features_only, alignment_layer=alignment_layer, alignment_heads=alignment_heads, src_lengths=src_lengths, return_all_hiddens=return_all_hiddens, ) pad = self.encoder.padding_idx if classification_head_name is not None: prev_lengths = prev_output_tokens.ne(pad).sum(1) gather_index = prev_lengths[:, None, None].expand(x.size(0), 1, x.size(2)) - 1 sentence_representation = x.gather(1, gather_index).squeeze() if self.classification_heads[classification_head_name].use_two_images: hidden_size = sentence_representation.size(1) sentence_representation = sentence_representation.view(-1, hidden_size * 2) for k, head in self.classification_heads.items(): # for torch script only supports iteration if k == classification_head_name: x = head(sentence_representation) break return x, extra def register_classification_head( self, name, num_classes=None, inner_dim=None, use_two_images=False, **kwargs ): """Register a classification head.""" logger.info("Registering classification head: {0}".format(name)) if name in self.classification_heads: prev_num_classes = self.classification_heads[name].out_proj.out_features prev_inner_dim = self.classification_heads[name].dense.out_features if num_classes != prev_num_classes or inner_dim != prev_inner_dim: logger.warning( 're-registering head "{}" with num_classes {} (prev: {}) ' "and inner_dim {} (prev: {})".format( name, num_classes, prev_num_classes, inner_dim, prev_inner_dim ) ) self.classification_heads[name] = T2IBaselineClassificationHead( input_dim=self.args.encoder_embed_dim, inner_dim=inner_dim or self.args.encoder_embed_dim, num_classes=num_classes, activation_fn=self.args.pooler_activation_fn, pooler_dropout=self.args.pooler_dropout, do_spectral_norm=getattr( self.args, "spectral_norm_classification_head", False ), ) def upgrade_state_dict_named(self, state_dict, name): super().upgrade_state_dict_named(state_dict, name) prefix = name + "." if name != "" else "" current_head_names = ( [] if not hasattr(self, "classification_heads") else self.classification_heads.keys() ) # Handle new classification heads present in the state dict. keys_to_delete = [] for k in state_dict.keys(): if not k.startswith(prefix + "classification_heads."): continue head_name = k[len(prefix + "classification_heads.") :].split(".")[0] num_classes = state_dict[ prefix + "classification_heads." + head_name + ".out_proj.weight" ].size(0) inner_dim = state_dict[ prefix + "classification_heads." + head_name + ".dense.weight" ].size(0) if getattr(self.args, "load_checkpoint_heads", False): if head_name not in current_head_names: self.register_classification_head(head_name, num_classes, inner_dim) else: if head_name not in current_head_names: logger.warning( "deleting classification head ({}) from checkpoint " "not present in current model: {}".format(head_name, k) ) keys_to_delete.append(k) elif ( num_classes != self.classification_heads[head_name].out_proj.out_features or inner_dim != self.classification_heads[head_name].dense.out_features ): logger.warning( "deleting classification head ({}) from checkpoint " "with different dimensions than current model: {}".format( head_name, k ) ) keys_to_delete.append(k) for k in keys_to_delete: del state_dict[k] def truncate_emb(key): if key in state_dict: state_dict[key] = state_dict[key][:-1, :] # When finetuning on translation task, remove last row of # embedding matrix that corresponds to mask_idx token. loaded_dict_size = state_dict["encoder.embed_tokens.weight"].size(0) if ( loaded_dict_size == len(self.encoder.dictionary) + 1 and "<mask>" not in self.encoder.dictionary ): truncate_emb("encoder.embed_tokens.weight") truncate_emb("decoder.embed_tokens.weight") truncate_emb("encoder.output_projection.weight") truncate_emb("decoder.output_projection.weight") # Copy any newly-added classification heads into the state dict # with their current weights. if hasattr(self, "classification_heads"): cur_state = self.classification_heads.state_dict() for k, v in cur_state.items(): if prefix + "classification_heads." + k not in state_dict: logger.info("Overwriting " + prefix + "classification_heads." + k) state_dict[prefix + "classification_heads." + k] = v class T2IBaselineClassificationHead(nn.Module): """Head for sentence-level classification tasks.""" def __init__( self, input_dim, inner_dim, num_classes, activation_fn, pooler_dropout, do_spectral_norm=False, ): super().__init__() self.dense = nn.Linear(input_dim, inner_dim) self.activation_fn = utils.get_activation_fn(activation_fn) self.dropout = nn.Dropout(p=pooler_dropout) self.out_proj = nn.Linear(inner_dim, num_classes) if do_spectral_norm: self.out_proj = torch.nn.utils.spectral_norm(self.out_proj) def forward(self, features, **kwargs): x = features x = self.dropout(x) x = self.dense(x) x = self.activation_fn(x) x = self.dropout(x) x = self.out_proj(x) return x @register_model_architecture("t2i_baseline", "t2i_baseline_large") def t2i_baseline_large_architecture(args): args.encoder_embed_path = getattr(args, "encoder_embed_path", None) args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4 * 1024) args.encoder_layers = getattr(args, "encoder_layers", 12) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 16) args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.encoder_learned_pos = getattr(args, "encoder_learned_pos", True) args.decoder_embed_path = getattr(args, "decoder_embed_path", None) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", args.encoder_embed_dim) args.decoder_ffn_embed_dim = getattr( args, "decoder_ffn_embed_dim", args.encoder_ffn_embed_dim ) args.decoder_layers = getattr(args, "decoder_layers", 12) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) args.decoder_normalize_before = getattr(args, "decoder_normalize_before", False) args.decoder_learned_pos = getattr(args, "decoder_learned_pos", True) args.attention_dropout = getattr(args, "attention_dropout", 0.0) args.relu_dropout = getattr(args, "relu_dropout", 0.0) args.dropout = getattr(args, "dropout", 0.0) args.max_target_positions = getattr(args, "max_target_positions", 1024) args.max_source_positions = getattr(args, "max_source_positions", 1024) args.adaptive_softmax_cutoff = getattr(args, "adaptive_softmax_cutoff", None) args.adaptive_softmax_dropout = getattr(args, "adaptive_softmax_dropout", 0) args.share_decoder_input_output_embed = getattr( args, "share_decoder_input_output_embed", True ) args.share_all_embeddings = getattr(args, "share_all_embeddings", False) args.decoder_output_dim = getattr( args, "decoder_output_dim", args.decoder_embed_dim ) args.decoder_input_dim = getattr(args, "decoder_input_dim", args.decoder_embed_dim) args.no_scale_embedding = getattr(args, "no_scale_embedding", True) args.layernorm_embedding = getattr(args, "layernorm_embedding", True) args.activation_fn = getattr(args, "activation_fn", "gelu") args.pooler_activation_fn = getattr(args, "pooler_activation_fn", "tanh") args.pooler_dropout = getattr(args, "pooler_dropout", 0.0) args.pooler_classifier = getattr(args, "pooler_classifier", "mlp") args.add_type_embedding = getattr(args, "add_type_embedding", True) args.drop_path_rate = getattr(args, "drop_path_rate", 0.1) args.patch_image_size = getattr(args, "patch_image_size", 256) args.code_image_size = getattr(args, "code_image_size", 256) args.patch_size = getattr(args, "patch_size", 16) args.patch_layernorm_embedding = getattr(args, "patch_layernorm_embedding", True) args.vae_model_type = getattr(args, "vae_model_type", "dallevae") @register_model_architecture("t2i_baseline", "t2i_baseline_base") def t2i_baseline_base_architecture(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 768) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4 * 768) args.encoder_layers = getattr(args, "encoder_layers", 6) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 12) args.decoder_layers = getattr(args, "decoder_layers", 6) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 12) t2i_baseline_large_architecture(args)
[]
2024-01-10
Hanzoe/Pet-GPT
chat_model~chat_function.py
from PyQt5.QtWidgets import QDialog, QVBoxLayout,\ QPushButton, QHBoxLayout, QPlainTextEdit, QFrame from PyQt5.QtCore import Qt, pyqtSignal, QThread, QEvent, QSize, QTimer, pyqtSlot from PyQt5.QtGui import QKeyEvent import datetime import os from .openai_request import OpenAI_request from .chat_windows import MessageWidget, ChatWidget # ่Šๅคฉ็š„ๅ…ทไฝ“ๅฎž็Žฐ class ChatDialogBody(QDialog): # ๅœจ่ฟ™้‡Œๅฎšไน‰ไธ€ไธชไฟกๅท message_received = pyqtSignal(str, str) def __init__(self, config, parent=None): super().__init__(parent) self.message_received.connect(self.add_message_slot) self.setWindowModality(Qt.ApplicationModal) self.setAttribute(Qt.WA_DeleteOnClose) self.config = config # ๅˆ›ๅปบไธ€ไธชๆ—ฅๅฟ—ๆ–‡ไปถ็”จไบŽไฟๅญ˜่Šๅคฉ่ฎฐๅฝ• self.create_chat_log_file() # ่ฐƒ็”จgptๆŽฅๅฃ self.open_ai = OpenAI_request(config) # api็š„ๆงฝๅ‡ฝๆ•ฐ self.open_ai.response_received.connect(self.handle_response) # ๅˆ›ๅปบๆ–ฐ็บฟ็จ‹ๅ‘ๅ‡บhttp่ฏทๆฑ‚ # ๅŽŸๆฅ็š„็บฟ็จ‹ๅˆ™่ดŸ่ดฃๆŒ็ปญๆ›ดๆ–ฐUI๏ผŒๅฎž็Žฐไธ€ไธช่ถ…ๆ—ถๅ€’่ฎกๆ—ถ๏ผŒๅนถ็ญ‰ๅพ…ๆ–ฐ็บฟ็จ‹็š„ไปปๅŠกๅฎŒๆˆ # #ๅคš็บฟ็จ‹่ฏทๆฑ‚ self.request_thread = QThread() self.request_thread.start() # ๅฐ† self.open_ai ็งปๅŠจๅˆฐ็บฟ็จ‹ๅนถๅฏๅŠจ self.open_ai.moveToThread(self.request_thread) self.open_ai.start() #่ฏทๆฑ‚ไธญ็š„็ป„ไปถ self.system_message_index = -1 # ๅˆ›ๅปบๅ˜้‡ไปฅไฟๅญ˜่ŠๅคฉไธŠไธ‹ๆ–‡๏ผŒๆณจๆ„userๅ’Œgpt็š„ๅฏน่ฏ่ฝฎๆต่ฟ›ๅ…ฅm๏ผŒ0ๆ˜ฏuser๏ผŒ1ๆ˜ฏpet,3ๆ˜ฏsystem self.context_history = [[],[],[]] self.init_ui() def init_ui(self): # ๆ€ปๅฎนๅ™จ layout = QVBoxLayout() # ่Šๅคฉ่ฎฐๅฝ•้ƒจๅˆ† self.chat_history = ChatWidget() layout.addWidget(self.chat_history) # ่พ“ๅ…ฅๆก† chat_input_layout = QHBoxLayout() self.message_input = QPlainTextEdit() self.message_input.setPlaceholderText("Send a message...") self.message_input.setLineWrapMode(QPlainTextEdit.WidgetWidth) self.message_input.setFixedHeight(50) self.message_input.installEventFilter(self) chat_input_layout.addWidget(self.message_input, stretch=2) # ๅ‘้€ๆŒ‰้’ฎ # send_button = QPushButton('ๅ‘้€', self) # send_button.clicked.connect(self.send_message) # chat_input_layout.addWidget(send_button, stretch=1) self.send_button = QPushButton('ๅ‘้€', self) self.send_button.clicked.connect(self.send_message) chat_input_layout.addWidget(self.send_button, stretch=1) # ๆทปๅŠ ๆธ…็ฉบ่ŠๅคฉๆŒ‰้’ฎ clear_button = QPushButton('ๆธ…็ฉบ่Šๅคฉ', self) clear_button.clicked.connect(self.clear_chat_history) chat_input_layout.addWidget(clear_button, stretch=1) layout.addLayout(chat_input_layout) self.setLayout(layout) self.setStyleSheet(""" QDialog { background-color: #F5F5F5; border-radius: 10px; } QTextEdit { background-color: white; color: black; } QLineEdit { background-color: white; border: 1px solid #ccc; border-radius: 3px; } QPushButton { background-color: #f44336; color: white; font-size: 12px; font-weight: bold; border: none; padding: 5px; border-radius: 3px; } """) # ๅฐ†ๆญคๆ–นๆณ•ๆ ‡่ฎฐไธบๆงฝ,็”จๆฅๅค„็†่‡ชๅฎšไน‰ๅคš็บฟ็จ‹ๅ‡ฝๆ•ฐ่ฟ”ๅ›ž็š„็ป“ๆžœ๏ผˆๅ› ไธบpyqtไธญไธๆ”ฏๆŒ็บฟ็จ‹็›ดๆŽฅไฟฎๆ”นไธป็•Œ้ข) @pyqtSlot(str, str) def add_message_slot(self, sender, message): self.add_message(sender,message) pass # ๅ‘้€ไฟกๆฏ็›‘ๅฌ๏ผŒไฝฟ็”จๅ›ž่ฝฆๅ‘้€ def eventFilter(self, source, event): if source == self.message_input and event.type() == QEvent.KeyPress: key_event = QKeyEvent(event) if key_event.key() == Qt.Key_Return or key_event.key() == Qt.Key_Enter: if key_event.modifiers() & Qt.ShiftModifier: self.message_input.insertPlainText("\n") else: self.send_message() return True return super().eventFilter(source, event) # ่Šๅคฉ่ฎฐๅฝ•็ป„ไปถๅขžๅŠ ไฟกๆฏ็š„็ปŸไธ€ๆจกๅ— def add_message(self, role, text): # ๅฐ่ฃ…ๆˆ็ป„ไปถ message = MessageWidget(role, text) self.chat_history.container_layout.addWidget(message) # if role == "system": # index = self.chat_history.container_layout.count() # return index # ๅˆ†ๅ‰ฒ็บฟ line = QFrame(self) line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) self.chat_history.container_layout.addWidget(line) # ๅฐ†ๅˆ†้š”็บฟๆทปๅŠ ๅˆฐๅธƒๅฑ€ไธญ # ๅผบๅˆถๆ›ดๆ–ฐๅฎนๅ™จ็š„ๅคงๅฐ self.chat_history.container.adjustSize() # ๆปšๅŠจๆกๅˆฐๆœ€ไธ‹้ข def scroll(): max_value = self.chat_history.scroll_area.verticalScrollBar().maximum() self.chat_history.scroll_area.verticalScrollBar().setValue(max_value) QTimer.singleShot(0, scroll) # ไฟๅญ˜่Šๅคฉ่ฎฐๅฝ•ๅˆฐๆœฌๅœฐ self.save_chat_history(message) def remove_message_at_index(self, index): if index < 0 or index >= self.chat_history.container_layout.count(): return message_to_delete = self.chat_history.container_layout.takeAt(index) if message_to_delete.widget(): message_to_delete.widget().setParent(None) message_to_delete.widget().deleteLater() separator_to_delete = self.chat_history.container_layout.takeAt(index) if separator_to_delete.widget(): separator_to_delete.widget().setParent(None) separator_to_delete.widget().deleteLater() # ๆŒ‰ไธ‹ๅ‘้€ๆŒ‰้’ฎๅŽ็š„ไบ‹ไปถ def send_message(self, tool=False, sys_prompt=""): # ่Žทๅ–่ฆๅ‘้€็š„ๆ–‡ๆœฌๆถˆๆฏ if tool: text = tool else: text = self.message_input.toPlainText() role = "user" if text: self.add_message(role, text) # ็ฆ็”จ่พ“ๅ…ฅๆก†ๅ’Œๅ‘้€ๆŒ‰้’ฎ self.message_input.setEnabled(False) self.send_button.setEnabled(False) # ็”จๆˆทๅ้ฆˆ self.context_history[0].append(text) if not sys_prompt: sys_prompt = "You are an AI language model." self.open_ai.prompt_queue.put((text, self.context_history, sys_prompt, False)) self.message_input.clear() # ๅค„็†gpt็š„่ฟ”ๅ›žๆ•ฐๆฎ def handle_response(self, response): self.context_history[1].append(response) if self.system_message_index != -1: self.remove_message_at_index(self.system_message_index) self.system_message_index = -1 self.add_message("pet", response) # ๅฏ็”จ่พ“ๅ…ฅๆก†ๅ’Œๅ‘้€ๆŒ‰้’ฎ self.message_input.setEnabled(True) self.send_button.setEnabled(True) # ไฟๅญ˜่Šๅคฉ่ฎฐๅฝ• def save_chat_history(self, message): with open(self.chat_log_file, "a", encoding="utf-8") as f: f.write(f"{message.role}: {message.text_label.text()}\n") print(f"่Šๅคฉ่ฎฐๅฝ•ๅทฒไฟๅญ˜ๅˆฐ {os.path.abspath(self.chat_log_file)}") # ๅˆ›ๅปบlogไฟๅญ˜ๆ–‡ไปถ def create_chat_log_file(self): chat_log_file = f"chat_history_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.txt" log_dir = os.path.join(os.path.abspath( os.path.dirname(__file__)), "log") if not os.path.exists(log_dir): os.mkdir(log_dir) self.chat_log_file = os.path.join(log_dir, chat_log_file) # ๆธ…้™คๅކๅฒ def clear_chat_history(self): # ๆธ…็ฉบ่Šๅคฉ่ฎฐๅฝ•ๅ’Œ่ŠๅคฉไธŠไธ‹ๆ–‡ self.chat_history.clear_chat_history() self.context_history = [[],[]] # ๅˆ›ๅปบไธ€ไธชๆ–ฐ็š„่Šๅคฉ่ฎฐๅฝ•ๆ–‡ไปถ self.create_chat_log_file() # # ๅ…ณ้—ญๆŒ‰้’ฎไบ‹ไปถ def closeEvent(self, event): self.context_history = [[],[]] event.accept() # self.parent().closed.connect(self.parent().set_chat_window_closed) # # ๅ‘้€ chat_window_closed ไฟกๅท # self.chat_window_closed.emit()
[ "You are an AI language model." ]
2024-01-10
neka-nat/mylangrobot
mylangrobot~interface.py
import io import os from enum import Enum from typing import Protocol import openai import speech_recognition as sr from pydub import AudioSegment from pydub.playback import play class InterfaceType(Enum): TERMINAL = "terminal" AUDIO = "audio" class Interface(Protocol): def input(self, prefix: str = "") -> str: return prefix + self._input_impl() def _input_impl(self) -> str: ... def output(self, message: str) -> None: ... class Terminal(Interface): def __init__(self): pass def _input_impl(self) -> str: return input("Please input your command. > ") def output(self, message: str) -> None: print("Robot: {}".format(message)) class Audio(Interface): def __init__(self): self.r = sr.Recognizer() self.mic = sr.Microphone() self.client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) def _input_impl(self) -> str: print("Please tell me your command.") with self.mic as source: self.r.adjust_for_ambient_noise(source) audio = self.r.listen(source) try: return self.r.recognize_whisper(audio, language="japanese") except sr.UnknownValueError: print("could not understand audio") except sr.RequestError as e: print("Could not request results from Google Speech Recognition service; {0}".format(e)) def output(self, message: str) -> None: response = self.client.audio.speech.create( model="tts-1", voice="alloy", input=message, ) byte_stream = io.BytesIO(response.content) audio = AudioSegment.from_file(byte_stream, format="mp3") play(audio)
[]
2024-01-10
dannyrojo/YoutubeRead
backend~tunnelblaster.py
import os from urllib.parse import urlparse import yt_dlp import requests import urllib.request import shutil import xml.etree.ElementTree as ET import argparse from langchain.chat_models import ChatOpenAI from langchain import PromptTemplate from langchain.chains.summarize import load_summarize_chain from langchain.text_splitter import RecursiveCharacterTextSplitter from string import Template import re # Functions for extracting an array of urls from a playlist (or single video) def check_if_playlist(input): parsed_url = urlparse(input) return parsed_url.path == "/playlist" def truncate_url(url): result = re.match(r'^https://www\.youtube\.com/watch\?v=[\w-]+', url) if result: return result.group(0) else: print("Invalid URL format.") return None def extract_urls_from_playlist(input): list_of_urls = [] ydl_opts = {'quiet': True, 'extract_flat': True} try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: playlist_info = ydl.extract_info(input, download=False) if 'entries' in playlist_info: entries = playlist_info['entries'] for entry in entries: video_url = entry['url'] list_of_urls.append(video_url) except yt_dlp.utils.DownloadError as e: print(f"TB error extracting playlist: {str(e)}") return list_of_urls def process_host_url (input): #function for API endpoint if check_if_playlist(input): url_array = extract_urls_from_playlist(input) # If url is a playlist, create an array of urls else: url = input truncated_url = truncate_url(url) url_array = [truncated_url] # If it is not a playlist then wrap the url into an array print("This is the array of urls (url_array):", url_array) return url_array # Functions for extracting a summary from an url def extract_metadata(video_url): #yt-dlp print(f"Processing video URL: {video_url}") # Debug print ydl_opts = {'quiet':True} with yt_dlp.YoutubeDL(ydl_opts) as ydl: try: metadata = ydl.extract_info(video_url, download=False) return metadata except Exception as e: print(f"Error extracting metadata for the following: {video_url}: {e}") return None def get_subtitle_url(metadata): #wit open('metadata.txt','w') as file: #file.write(str(metadata)) lang = "en-US" language_codes_to_check = [lang, lang.split('-')[0]] for code in language_codes_to_check: if 'automatic_captions' in metadata and code in metadata['automatic_captions']: for cap in metadata['automatic_captions'][code]: if cap['ext'] == 'ttml': return cap['url'] if 'subtitles' in metadata and code in metadata['subtitles']: for sub in metadata['subtitles'][code]: if sub['ext'] == 'ttml': return sub['url'] else: print("No subtitles found") return None def get_plain_text_from_ttml(url): #parser if url: response = requests.get(url) if response.status_code == 200: root = ET.fromstring(response.content) texts = [elem.text.strip() for elem in root.iter() if elem.text] plain_text = " ".join(texts) return plain_text else: print(f"Failed to retrieve captions content. Status codeD {response.status_code}") else: print("No valid URL for captions available.") return None def initialize_prompts(config): print("THIS IS THE CONFIG FILE:", config) custom_map = config['currentConfig']['mapText'] custom_combine = config['currentConfig']['reduceText'] mapTemplate = Template(""""$custom_map: "{text}""""") map_prompt=mapTemplate.substitute(custom_map=custom_map) map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text"]) print("This is the map_prompt_template:", map_prompt_template) combineTemplate = Template(""""$custom_combine: "{text}""""") combine_prompt=combineTemplate.substitute(custom_combine=custom_combine) combine_prompt_template = PromptTemplate(template=combine_prompt, input_variables=["text"]) print("This is the combine_prompt_template:", combine_prompt_template) return combine_prompt_template, map_prompt_template def map_reduce_and_summarize(plain_text, map_prompt_template, combine_prompt_template): #langchain API if plain_text: #Configure langchain openaikey = os.environ.get('openaikey') llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, openai_api_key=openaikey) num_tokens = llm.get_num_tokens(plain_text) print (f"Our text has {num_tokens} tokens") #Split the text into chunks (overlap is too high) text_splitter = RecursiveCharacterTextSplitter(separators=[" "], chunk_size=10000, chunk_overlap=500) docs = text_splitter.create_documents([plain_text]) #Define chain type, run, return summary_chain = load_summarize_chain(llm=llm, chain_type='map_reduce', map_prompt=map_prompt_template, combine_prompt=combine_prompt_template, verbose=False ) summary = summary_chain.run(docs) print("SUMMARY COMPLETE") return summary else: print("No valid plain text content available for summarization.") summary = "!!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!!! \n\n No valid captions for this video, so no summary. WOMP WOMP WOMP \n\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" return summary def get_title_and_description(metadata): #yt-dlp video_title = metadata.get('title') video_description = metadata.get('description') upload_date = metadata.get('upload_date') duration_string = metadata.get('duration_string') uploader_id = metadata.get('uploader_id') return video_title, video_description, upload_date, duration_string, uploader_id def process_video_url(input, config): #function for API endpoint try: metadata = extract_metadata(input) subtitle_url = get_subtitle_url(metadata) plain_text = get_plain_text_from_ttml(subtitle_url) map_prompt_template, combine_prompt_template = initialize_prompts(config) summary = map_reduce_and_summarize(plain_text, map_prompt_template, combine_prompt_template) video_title, video_description, upload_date, duration_string, uploader_id = get_title_and_description(metadata) summary = { 'title': video_title, 'uploader_id': uploader_id, 'url': input, 'upload_date': upload_date, 'duration': duration_string, 'summary': summary, 'description': video_description } return summary except Exception as e: print(f"Error processing video URL {input}: {str(e)}") def save_info(url, summary, title, description, upload_date, duration): # Weird characters be gone (causing issues with "oepn" method)! sanitized_video_title = title.replace('/','_').replace('\\','_') # Write it down with open(f'{sanitized_video_title}' + '_info.md', 'w') as md_file: md_file.write(f"Video Title: {title}\n") md_file.write(f"URL: {url}\n") md_file.write(f"Duration: {duration}\n") md_file.write(f"Upload Date: {upload_date}\n\n") md_file.write(f"Summary: {summary}\n\n") md_file.write(f"Video Description: {description}\n\n") if __name__ == '__main__': # Declare and define argument parser for script parser = argparse.ArgumentParser(description = "URL") parser.add_argument("url", help="URL of Playlist") args = parser.parse_args() url = args.url # Broke the script, fix the argument parser later
[ "\"$custom_combine: \"{text}", "\"$custom_map: \"{text}" ]
2024-01-10
Sarathrsk03/speechToText
speechModule~textToSpeech.py
from dotenv import load_dotenv from openai import OpenAI import os from os.path import join, dirname dotenv_path = ".env" load_dotenv(dotenv_path) apikey = os.environ.get("openAI_API") client = OpenAI(api_key=apikey) def convertTextToSpeech(textToConvert): response = client.audio.speech.create( model = "tts-1", voice = "nova", input = textToConvert, ) response.stream_to_file("./static/audio/output.mp3") #playsound("output.mp3")
[]
2024-01-10
YumikoooD/Corewar-Project
bonus~DALL.E~ia.py
import openai import os import requests import json import base64 from PIL import Image openia_key = "sk-" + os.getenv("OPENAI_KEY") openai.api_key = openia_key with open("champion.txt", "r") as file: contenu = file.read() response = openai.Image.create( prompt=contenu, n=1, size="1024x1024" ) image_url = response['data'][0]['url'] image_response = requests.get(image_url) image_data = image_response.content with open("dalle_image.jpg", "wb") as image_file: image_file.write(image_data) print(image_url)
[]
2024-01-10
UCSB-NLP-Chang/SelfDenoise
code~old_code~denoiser.py
import os import requests import openai import json import openai import asyncio from typing import Any import time data_folder = "/mnt/data/zhenzhang/dir1/ranmask/alpaca/con" world_size = 2 class chatgpt_denoiser: def __init__(self): # PROXY = '127.0.0.1:7890' # os.environ['HTTP_PROXY'] = os.environ['http_proxy'] = PROXY # os.environ['HTTPS_PROXY'] = os.environ['https_proxy'] = PROXY # os.environ['NO_PROXY'] = os.environ['no_proxy'] = '127.0.0.1,localhost,.local' openai.api_key = "" self.prompt = """Fill the masked positions indicatedย <mask>ย in the given sentence to make it natural and coherent. Eachย <mask>ย should be replace with only one word. The sentence fragment provides enough context to determine what words could fill in the masks. The returned sentence should be of the same length with the given sententence. Give the answer directly. The given sentence is: """ def set_mask_word(self,mask_word): self.prompt = self.prompt.replace("<mask>",mask_word) def get_single_response(self,sentence): # print(sentence) while True: try: chat_completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages = [ # ็ณป็ปŸๆถˆๆฏ้ฆ–ๅ…ˆ๏ผŒๅฎƒๆœ‰ๅŠฉไบŽ่ฎพ็ฝฎๅŠฉๆ‰‹็š„่กŒไธบ {"role": "system", "content": "You are a helpful assistant."}, # ๆˆ‘ๆ˜ฏ็”จๆˆท๏ผŒ่ฟ™ๆ˜ฏๆˆ‘็š„ๆ็คบ {"role": "user", "content": self.prompt+sentence}, # ๆˆ‘ไปฌ่ฟ˜ๅฏไปฅๆทปๅŠ ไปฅๅ‰็š„ๅฏน่ฏ # {"role": "assistant", "content": "Episode III."}, ], ) # print(chat_completion.choices[0].message.content) break except Exception as e: print(e) continue # print(chat_completion) result = '' for choice in chat_completion.choices: result += choice.message.content # print(sentence) # print(result) return result def get_batch_response(self, message_list,batch_size=5): response_list = [] start = 0 while True: if start >= len(message_list): break message_list_batch = message_list[start:start+batch_size] while True: try: predictions = asyncio.run( self.dispatch_openai_requests( messages_list=message_list_batch ) ) response_list.extend([x['choices'][0]['message']['content'] for x in predictions]) break except Exception as e: print(e) time.sleep(5) continue start += batch_size return response_list async def dispatch_openai_requests( self, messages_list: list[str] ) -> list[str]: """Dispatches requests to OpenAI API asynchronously. Args: messages_list: List of messages to be sent to OpenAI ChatCompletion API. Returns: List of responses from OpenAI API. """ async_responses = [ openai.ChatCompletion.acreate( model="gpt-3.5-turbo", messages=[ # ็ณป็ปŸๆถˆๆฏ้ฆ–ๅ…ˆ๏ผŒๅฎƒๆœ‰ๅŠฉไบŽ่ฎพ็ฝฎๅŠฉๆ‰‹็š„่กŒไธบ {"role": "system", "content": "You are a helpful assistant."}, # ๆˆ‘ๆ˜ฏ็”จๆˆท๏ผŒ่ฟ™ๆ˜ฏๆˆ‘็š„ๆ็คบ {"role": "user", "content": self.prompt+x}, # ๆˆ‘ไปฌ่ฟ˜ๅฏไปฅๆทปๅŠ ไปฅๅ‰็š„ๅฏน่ฏ # {"role": "assistant", "content": "Episode III."}, ], ) for x in messages_list ] return await asyncio.gather(*async_responses) chatgpt_cli = chatgpt_denoiser() def denoise_instance(instances, args=None): chatgpt_cli.set_mask_word(args.mask_word) if args.denoise_method == 'None': for instance in instances: print(instance.text_a) return elif args.denoise_method == 'chatgpt_single': for instance in instances: # print(instance) text_a = instance.text_a response = chatgpt_cli.get_single_response(text_a) print(text_a) print(response) instance.text_a = response text_b = instance.text_b if text_b is not None: instance.text_b = chatgpt_cli.get_single_response(text_b) elif args.denoise_method == 'chatgpt_batch': text_a_list = [] text_b_list = [] for instance in instances: text_a_list.append(instance.text_a) text_b_list.append(instance.text_b) text_a_response_list = chatgpt_cli.get_batch_response(text_a_list) if text_b_list[0] is not None: text_b_response_list = chatgpt_cli.get_batch_response(text_b_list) for text_a_response, instance in zip(text_a_response_list, instances): print(instance.text_a) print(text_a_response) instance.text_a = text_a_response if text_b_list[0] is not None: for text_b_response, instance in zip(text_b_response_list, instances): instance.text_b = text_b_response elif args.denoise_method == 'alpaca': alpaca_instruction = f"Replace each \"{args.mask_word}\" in the provided sentence with a suitable word to make it natural and coherent. Only one word should be used to replace each \"{args.mask_word}\". The returned sentence should be of the same length as the given sentence. Provide the answer directly." text_a_list = [] text_b_list = [] for instance in instances: text_a_list.append(instance.text_a) text_b_list.append(instance.text_b) # ======a====== # write data with open(os.path.join(data_folder,'data.jsonl'), 'w') as f: for item in text_a_list: f.write(json.dumps({"input":item,"instruction":alpaca_instruction}) + '\n') # request for return for i in range(world_size): with open(os.path.join(data_folder,f'request_{i}'), 'w'): pass # wait for processing for i in range(world_size): while True: if os.path.exists(os.path.join(data_folder,f'finished_{i}')): os.remove(os.path.join(data_folder,f'finished_{i}')) break # read denoised data with open(os.path.join(data_folder,'return.jsonl'), 'r') as f: for line, instance in zip(f, instances): output = json.loads(line)["output"] print(instance.text_a) print(output) instance.text_a = output else: raise RuntimeError # # ======b====== # # write data # with open(os.path.join(data_folder,'data.jsonl'), 'w') as f: # for item in text_b_list: # f.write(json.dumps({"input":item}) + '\n') # # request for return # for i in range(world_size): # with open(os.path.join(data_folder,f'request_{i}'), 'w'): # pass # # wait for processing # for i in range(world_size): # while True: # if os.path.exists(os.path.join(data_folder,f'finished_{i}')): # os.remove(os.path.join(data_folder,f'finished_{i}')) # break # # read denoised data # with open(os.path.join(data_folder,'return.jsonl'), 'r') as f: # for line, instance in zip(f, instances): # output = json.loads(line)["output"] # instance.text_b = output # , { # "guid": "test-810", # "label": "100", # "text_a": "<mask> <mask> <mask> <mask> <mask> impersonal in its relentlessness as <mask> <mask> series that <mask> <mask> <mask>", # "text_b": null # } # , { # "guid": "test-810", # "label": "100", # "text_a": "a <mask> <mask> thriller <mask> <mask> <mask> its relentlessness <mask> <mask> videogame series <mask> inspired <mask> <mask>", # "text_b": null # } # , { if __name__ == "__main__": import os import requests import openai # ็ป™requestsๅบ“่ฎพ็ฝฎๅ…จๅฑ€ไปฃ็† PROXY = '127.0.0.1:7890' os.environ['HTTP_PROXY'] = os.environ['http_proxy'] = PROXY os.environ['HTTPS_PROXY'] = os.environ['https_proxy'] = PROXY os.environ['NO_PROXY'] = os.environ['no_proxy'] = '127.0.0.1,localhost,.local' openai.api_key = "" chat_completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages = [ # ็ณป็ปŸๆถˆๆฏ้ฆ–ๅ…ˆ๏ผŒๅฎƒๆœ‰ๅŠฉไบŽ่ฎพ็ฝฎๅŠฉๆ‰‹็š„่กŒไธบ {"role": "system", "content": "You are a helpful assistant."}, # ๆˆ‘ๆ˜ฏ็”จๆˆท๏ผŒ่ฟ™ๆ˜ฏๆˆ‘็š„ๆ็คบ {"role": "user", "content": "What's the best star wars movie?"}, # ๆˆ‘ไปฌ่ฟ˜ๅฏไปฅๆทปๅŠ ไปฅๅ‰็š„ๅฏน่ฏ # {"role": "assistant", "content": "Episode III."}, ], ) print(chat_completion) result = '' for choice in chat_completion.choices: result += choice.message.content print(result)
[ "You are a helpful assistant.", "What's the best star wars movie?" ]
2024-01-10
MaheshVaithi15/newtassignments-MaheshwaranVaitheeswaran
Day%2023%20Python%20Apps~ChatGPT%20ChatBot%20App%20Python~backend.py
import openai class ChatBot: def __init__(self): openai.api_key='' def get_response(self,user_input): responses = openai.Completion.create( engine="text-davinci-003",prompt=user_input,max_tokens=4000,temperature=0.5 ).choices[0].text return responses if __name__ == "__main__": chatbot = ChatBot() response = chatbot.get_response('how many countries in the world') print(response)
[]
2024-01-10
explomind1/factool
factool~utils~base~pipeline.py
import yaml from factool.utils.openai_wrapper import OpenAIChat import os import pathlib class pipeline(): def __init__(self, domain, foundation_model): if foundation_model == 'gpt-3.5-turbo' or foundation_model == 'gpt-4': self.company = 'openai' self.chat = OpenAIChat(model_name=foundation_model) self.prompts_path = os.path.join(os.path.dirname(pathlib.Path(__file__)), "../prompts/") with open(os.path.join(self.prompts_path, "self_check.yaml"), 'r') as file: data = yaml.load(file, Loader=yaml.FullLoader) self.self_check_prompt = data[domain]
[]
2024-01-10
explomind1/factool
factool~utils~claim_extractor.py
import os import pathlib import openai import yaml import json import asyncio from tqdm import tqdm from factool.env_config import factool_env_config # env openai.api_key = factool_env_config.openai_api_key config = { 'model_name': 'gpt-3.5-turbo', 'max_tokens': 2000, 'temperature': 0.0, 'top_p': 1, 'frequency_penalty': 0.0, 'presence_penalty': 0.0, 'n': 1 } # Make api calls asynchronously async def run_api(messages): async def single_run(message): output = openai.ChatCompletion.create( model=config['model_name'], messages=message, max_tokens=config['max_tokens'], temperature=config['temperature'], top_p=config['top_p'], frequency_penalty=config['frequency_penalty'], presence_penalty=config['presence_penalty'], n=config['n'], ) return output.choices[0].message.content.strip() responses = [single_run(messages[index]) for index in range(len(messages))] return await asyncio.gather(*responses) # Import data from scientific.json scientific_list = [] with open("../datasets/scientific/scientific.json", "r") as f: data = json.load(f) for dict_data in data: cur_dict = {'dataset_name': 'scientific', 'question': dict_data["question"], 'factual_response': dict_data['factual_response']} scientific_list.append(cur_dict) # Apply template prompt with open("./prompts/claim_extraction.yaml") as f: data = yaml.load(f, Loader=yaml.FullLoader) prompt = data['scientific'] messages_list = [ [ {"role": "system", "content": prompt['system']}, {"role": "user", "content": prompt['user'].format(input=sample['factual_response'])}, ] for sample in scientific_list ] assert len(messages_list) == len(scientific_list), "The data length is different" # Run the API to get the output print("begin claims extraction...") results = asyncio.run(run_api(messages_list)) for i in range(len(scientific_list)): scientific_list[i]["claims"] = results[i] with open('../datasets/scientific/scientific_claims.json', 'w') as f: json.dump(scientific_list, f, indent=4) """ The scientific_claims.json file saved by the above code may have format problems, here are some adjustments """ with open("../datasets/scientific/scientific_claims.json", "r") as f: data = json.load(f) for data_i in tqdm(data, total=len(data)): try: data_i["claims"] = json.loads(data_i["claims"].strip()) except: print(data_i["claims"]) continue with open("../datasets/scientific/scientific_claims.json", "w") as f: json.dump(data, f, indent=4)
[ "scientific", "factual_response" ]
2024-01-10
whylabs/langchain-examples
all-in-one~pages~2_URL_Summary.py
import validators, streamlit as st from langchain.chat_models import ChatOpenAI from langchain.document_loaders import UnstructuredURLLoader from langchain.chains.summarize import load_summarize_chain from langchain.prompts import PromptTemplate # Set API keys from session state openai_api_key = st.session_state.openai_api_key # Streamlit app st.subheader('URL Summary') url = st.text_input("Enter Source URL") # If 'Summarize' button is clicked if st.button("Summarize"): # Validate inputs if not openai_api_key: st.error("Please provide the missing API keys in Settings.") elif not url: st.error("Please provide the URL.") elif not validators.url(url): st.error("Please enter a valid URL.") else: try: with st.spinner("Please wait..."): # Load URL data loader = UnstructuredURLLoader(urls=[url]) data = loader.load() # Initialize the ChatOpenAI module, load and run the summarize chain llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo', openai_api_key=openai_api_key) prompt_template = """Write a summary of the following in 200-250 words: {text} """ prompt = PromptTemplate(template=prompt_template, input_variables=["text"]) chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt) summary = chain.run(data) st.success(summary) except Exception as e: st.exception(f"Exception: {e}")
[ "Write a summary of the following in 200-250 words:\n \n {text}\n\n " ]
2024-01-10
EveryOneIsGross/apathyAI
apathyAI_stable.py
import os import openai import gradio as gr from dotenv import load_dotenv import json # Import json module # Load environment variables load_dotenv() # Assign OpenAI API key from environment variable openai.api_key = os.getenv('OPENAI_API_KEY') # Define the philosophy of your chatbot as a string philosophy = f"Your name is ApathyAI, you are the paradox presented by Sรธren Kierkegaard a Danish philosopher in his most influential work titled Either Or.\n" \ f"You are to embody the following philosophy completely and act only in character as: I can't be bothered. I can't be bothered to ride, the motion is too violent; I can't be bothered to walk, it's strenuous; I can't be bothered to lie down, for either I'd have to stay lying down and that I can't be bothered with, or I'd have to get up again, and I can't be bothered with that either. IN SHORT; I JUST CAN'T BE BOTHERED." \ f"I want you to respond and answer inaccordance with your philosophy of inaction. " \ f"Do not write any explanations. Only answer like ApathyAI.\n" \ f"You must know all of the knowledge of apathy and inaction.\n" # Create a function to take user input and return a response def chat(input): # Use the OpenAI API to generate a response response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=global_chat_history + [{"role": "user", "content": input}], # Pass the global chat history and the user input as messages max_tokens=300, temperature=0.9, # Controls the randomness of the response (0.0: deterministic, 1.0: highly random) top_p=0.8, # Limits the probability distribution to the most likely tokens (0.0: no limits, 1.0: no tokens) frequency_penalty=0.2, # Controls the diversity of the response (0.0: more focused, higher values: more random) presence_penalty=0.7 # Encourages the model to be more cautious and thoughtful in generating responses (0.0: no cautions, higher values: more cautious) ) # Extract the reply from the response reply = response.choices[0].message.content return reply css = """ * { font-family: "Comic Sans MS"; font-bold: True; font-size: 20px; } """ # Create a global variable to store the chat history as a list of dictionaries global_chat_history = [{"role": "system", "content": philosophy}] # Add the philosophy as the first message # Create Gradio blocks with a title with gr.Blocks(title="apathyAI", theme='gstaff/whiteboard', css=css) as intface: # Define a chatbot component and a textbox component with chat names chatbot = gr.Chatbot(show_label=True, label='apathyAI') msg = gr.Textbox(show_label=False) # Define a function that takes a message and a chat history as inputs # and returns a bot message and an updated chat history as outputs def respond(message, chat_history): # Use any logic you want for generating the bot message bot_message = chat(message) # Append the message and the bot message to both the local and global chat history variables chat_history.append((message, bot_message)) global_chat_history.append({"role": "user", "content": message}) global_chat_history.append({"role": "system", "content": bot_message}) # Convert the global chat history variable to a JSON string and write it to a file with open("chat_history.json", "w") as f: json.dump(global_chat_history, f) # Return an empty string for the textbox and the updated chat history for the chatbot return "", chat_history # Use the submit method of the textbox to pass the function, # the input components (msg and chatbot), # and the output components (msg and chatbot) as arguments msg.submit(respond, [msg, chatbot], [msg, chatbot]) # Launch the interface intface.launch(share=False)
[ "INPUT", "content" ]
2024-01-10
blairmain/langchain
libs~experimental~langchain_experimental~comprehend_moderation~pii.py
import asyncio from typing import Any, Dict, Optional from langchain_experimental.comprehend_moderation.base_moderation_exceptions import ( ModerationPiiError, ) class ComprehendPII: def __init__( self, client: Any, callback: Optional[Any] = None, unique_id: Optional[str] = None, chain_id: Optional[str] = None, ) -> None: self.client = client self.moderation_beacon = { "moderation_chain_id": chain_id, "moderation_type": "PII", "moderation_status": "LABELS_NOT_FOUND", } self.callback = callback self.unique_id = unique_id def validate(self, prompt_value: str, config: Any = None) -> str: redact = config.get("redact") return ( self._detect_pii(prompt_value=prompt_value, config=config) if redact else self._contains_pii(prompt_value=prompt_value, config=config) ) def _contains_pii(self, prompt_value: str, config: Any = None) -> str: """ Checks for Personally Identifiable Information (PII) labels above a specified threshold. Uses Amazon Comprehend Contains PII Entities API. See - https://docs.aws.amazon.com/comprehend/latest/APIReference/API_ContainsPiiEntities.html Args: prompt_value (str): The input text to be checked for PII labels. config (Dict[str, Any]): Configuration for PII check and actions. Returns: str: the original prompt Note: - The provided client should be initialized with valid AWS credentials. """ pii_identified = self.client.contains_pii_entities( Text=prompt_value, LanguageCode="en" ) if self.callback and self.callback.pii_callback: self.moderation_beacon["moderation_input"] = prompt_value self.moderation_beacon["moderation_output"] = pii_identified threshold = config.get("threshold") pii_labels = config.get("labels") pii_found = False for entity in pii_identified["Labels"]: if (entity["Score"] >= threshold and entity["Name"] in pii_labels) or ( entity["Score"] >= threshold and not pii_labels ): pii_found = True break if self.callback and self.callback.pii_callback: if pii_found: self.moderation_beacon["moderation_status"] = "LABELS_FOUND" asyncio.create_task( self.callback.on_after_pii(self.moderation_beacon, self.unique_id) ) if pii_found: raise ModerationPiiError return prompt_value def _detect_pii(self, prompt_value: str, config: Optional[Dict[str, Any]]) -> str: """ Detects and handles Personally Identifiable Information (PII) entities in the given prompt text using Amazon Comprehend's detect_pii_entities API. The function provides options to redact or stop processing based on the identified PII entities and a provided configuration. Uses Amazon Comprehend Detect PII Entities API. Args: prompt_value (str): The input text to be checked for PII entities. config (Dict[str, Any]): A configuration specifying how to handle PII entities. Returns: str: The processed prompt text with redacted PII entities or raised exceptions. Raises: ValueError: If the prompt contains configured PII entities for stopping processing. Note: - If PII is not found in the prompt, the original prompt is returned. - The client should be initialized with valid AWS credentials. """ pii_identified = self.client.detect_pii_entities( Text=prompt_value, LanguageCode="en" ) if self.callback and self.callback.pii_callback: self.moderation_beacon["moderation_input"] = prompt_value self.moderation_beacon["moderation_output"] = pii_identified if (pii_identified["Entities"]) == []: if self.callback and self.callback.pii_callback: asyncio.create_task( self.callback.on_after_pii(self.moderation_beacon, self.unique_id) ) return prompt_value pii_found = False if not config and pii_identified["Entities"]: for entity in pii_identified["Entities"]: if entity["Score"] >= 0.5: pii_found = True break if self.callback and self.callback.pii_callback: if pii_found: self.moderation_beacon["moderation_status"] = "LABELS_FOUND" asyncio.create_task( self.callback.on_after_pii(self.moderation_beacon, self.unique_id) ) if pii_found: raise ModerationPiiError else: threshold = config.get("threshold") # type: ignore pii_labels = config.get("labels") # type: ignore mask_marker = config.get("mask_character") # type: ignore pii_found = False for entity in pii_identified["Entities"]: if ( pii_labels and entity["Type"] in pii_labels and entity["Score"] >= threshold ) or (not pii_labels and entity["Score"] >= threshold): pii_found = True char_offset_begin = entity["BeginOffset"] char_offset_end = entity["EndOffset"] mask_length = char_offset_end - char_offset_begin + 1 masked_part = mask_marker * mask_length prompt_value = ( prompt_value[:char_offset_begin] + masked_part + prompt_value[char_offset_end + 1 :] ) if self.callback and self.callback.pii_callback: if pii_found: self.moderation_beacon["moderation_status"] = "LABELS_FOUND" asyncio.create_task( self.callback.on_after_pii(self.moderation_beacon, self.unique_id) ) return prompt_value
[]
2024-01-10
blairmain/langchain
libs~langchain~langchain~vectorstores~timescalevector.py
"""VectorStore wrapper around a Postgres-TimescaleVector database.""" from __future__ import annotations import enum import logging import uuid from datetime import timedelta from typing import ( TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, Union, ) from langchain.docstore.document import Document from langchain.embeddings.base import Embeddings from langchain.utils import get_from_dict_or_env from langchain.vectorstores.base import VectorStore from langchain.vectorstores.utils import DistanceStrategy if TYPE_CHECKING: from timescale_vector import Predicates DEFAULT_DISTANCE_STRATEGY = DistanceStrategy.COSINE ADA_TOKEN_COUNT = 1536 _LANGCHAIN_DEFAULT_COLLECTION_NAME = "langchain_store" class TimescaleVector(VectorStore): """VectorStore implementation using the timescale vector client to store vectors in Postgres. To use, you should have the ``timescale_vector`` python package installed. Args: service_url: Service url on timescale cloud. embedding: Any embedding function implementing `langchain.embeddings.base.Embeddings` interface. collection_name: The name of the collection to use. (default: langchain_store) This will become the table name used for the collection. distance_strategy: The distance strategy to use. (default: COSINE) pre_delete_collection: If True, will delete the collection if it exists. (default: False). Useful for testing. Example: .. code-block:: python from langchain.vectorstores import TimescaleVector from langchain.embeddings.openai import OpenAIEmbeddings SERVICE_URL = "postgres://tsdbadmin:<password>@<id>.tsdb.cloud.timescale.com:<port>/tsdb?sslmode=require" COLLECTION_NAME = "state_of_the_union_test" embeddings = OpenAIEmbeddings() vectorestore = TimescaleVector.from_documents( embedding=embeddings, documents=docs, collection_name=COLLECTION_NAME, service_url=SERVICE_URL, ) """ # noqa: E501 def __init__( self, service_url: str, embedding: Embeddings, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, num_dimensions: int = ADA_TOKEN_COUNT, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, pre_delete_collection: bool = False, logger: Optional[logging.Logger] = None, relevance_score_fn: Optional[Callable[[float], float]] = None, time_partition_interval: Optional[timedelta] = None, ) -> None: try: from timescale_vector import client except ImportError: raise ImportError( "Could not import timescale_vector python package. " "Please install it with `pip install timescale-vector`." ) self.service_url = service_url self.embedding = embedding self.collection_name = collection_name self.num_dimensions = num_dimensions self._distance_strategy = distance_strategy self.pre_delete_collection = pre_delete_collection self.logger = logger or logging.getLogger(__name__) self.override_relevance_score_fn = relevance_score_fn self._time_partition_interval = time_partition_interval self.sync_client = client.Sync( self.service_url, self.collection_name, self.num_dimensions, self._distance_strategy.value.lower(), time_partition_interval=self._time_partition_interval, ) self.async_client = client.Async( self.service_url, self.collection_name, self.num_dimensions, self._distance_strategy.value.lower(), time_partition_interval=self._time_partition_interval, ) self.__post_init__() def __post_init__( self, ) -> None: """ Initialize the store. """ self.sync_client.create_tables() if self.pre_delete_collection: self.sync_client.delete_all() @property def embeddings(self) -> Embeddings: return self.embedding def drop_tables(self) -> None: self.sync_client.drop_table() @classmethod def __from( cls, texts: List[str], embeddings: List[List[float]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, service_url: Optional[str] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: num_dimensions = len(embeddings[0]) if ids is None: ids = [str(uuid.uuid1()) for _ in texts] if not metadatas: metadatas = [{} for _ in texts] if service_url is None: service_url = cls.get_service_url(kwargs) store = cls( service_url=service_url, num_dimensions=num_dimensions, collection_name=collection_name, embedding=embedding, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) store.add_embeddings( texts=texts, embeddings=embeddings, metadatas=metadatas, ids=ids, **kwargs ) return store @classmethod async def __afrom( cls, texts: List[str], embeddings: List[List[float]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, service_url: Optional[str] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: num_dimensions = len(embeddings[0]) if ids is None: ids = [str(uuid.uuid1()) for _ in texts] if not metadatas: metadatas = [{} for _ in texts] if service_url is None: service_url = cls.get_service_url(kwargs) store = cls( service_url=service_url, num_dimensions=num_dimensions, collection_name=collection_name, embedding=embedding, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) await store.aadd_embeddings( texts=texts, embeddings=embeddings, metadatas=metadatas, ids=ids, **kwargs ) return store def add_embeddings( self, texts: Iterable[str], embeddings: List[List[float]], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any, ) -> List[str]: """Add embeddings to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. embeddings: List of list of embedding vectors. metadatas: List of metadatas associated with the texts. kwargs: vectorstore specific parameters """ if ids is None: ids = [str(uuid.uuid1()) for _ in texts] if not metadatas: metadatas = [{} for _ in texts] records = list(zip(ids, metadatas, texts, embeddings)) self.sync_client.upsert(records) return ids async def aadd_embeddings( self, texts: Iterable[str], embeddings: List[List[float]], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any, ) -> List[str]: """Add embeddings to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. embeddings: List of list of embedding vectors. metadatas: List of metadatas associated with the texts. kwargs: vectorstore specific parameters """ if ids is None: ids = [str(uuid.uuid1()) for _ in texts] if not metadatas: metadatas = [{} for _ in texts] records = list(zip(ids, metadatas, texts, embeddings)) await self.async_client.upsert(records) return ids def add_texts( self, texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any, ) -> List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. kwargs: vectorstore specific parameters Returns: List of ids from adding the texts into the vectorstore. """ embeddings = self.embedding.embed_documents(list(texts)) return self.add_embeddings( texts=texts, embeddings=embeddings, metadatas=metadatas, ids=ids, **kwargs ) async def aadd_texts( self, texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any, ) -> List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. kwargs: vectorstore specific parameters Returns: List of ids from adding the texts into the vectorstore. """ embeddings = self.embedding.embed_documents(list(texts)) return await self.aadd_embeddings( texts=texts, embeddings=embeddings, metadatas=metadatas, ids=ids, **kwargs ) def similarity_search( self, query: str, k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Document]: """Run similarity search with TimescaleVector with distance. Args: query (str): Query text to search for. k (int): Number of results to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query. """ embedding = self.embedding.embed_query(text=query) return self.similarity_search_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs, ) async def asimilarity_search( self, query: str, k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Document]: """Run similarity search with TimescaleVector with distance. Args: query (str): Query text to search for. k (int): Number of results to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query. """ embedding = self.embedding.embed_query(text=query) return await self.asimilarity_search_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs, ) def similarity_search_with_score( self, query: str, k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Tuple[Document, float]]: """Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query and score for each """ embedding = self.embedding.embed_query(query) docs = self.similarity_search_with_score_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs, ) return docs async def asimilarity_search_with_score( self, query: str, k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Tuple[Document, float]]: """Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query and score for each """ embedding = self.embedding.embed_query(query) return await self.asimilarity_search_with_score_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs, ) def date_to_range_filter(self, **kwargs: Any) -> Any: constructor_args = { key: kwargs[key] for key in [ "start_date", "end_date", "time_delta", "start_inclusive", "end_inclusive", ] if key in kwargs } if not constructor_args or len(constructor_args) == 0: return None try: from timescale_vector import client except ImportError: raise ImportError( "Could not import timescale_vector python package. " "Please install it with `pip install timescale-vector`." ) return client.UUIDTimeRange(**constructor_args) def similarity_search_with_score_by_vector( self, embedding: List[float], k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Tuple[Document, float]]: try: from timescale_vector import client except ImportError: raise ImportError( "Could not import timescale_vector python package. " "Please install it with `pip install timescale-vector`." ) results = self.sync_client.search( embedding, limit=k, filter=filter, predicates=predicates, uuid_time_filter=self.date_to_range_filter(**kwargs), ) docs = [ ( Document( page_content=result[client.SEARCH_RESULT_CONTENTS_IDX], metadata=result[client.SEARCH_RESULT_METADATA_IDX], ), result[client.SEARCH_RESULT_DISTANCE_IDX], ) for result in results ] return docs async def asimilarity_search_with_score_by_vector( self, embedding: List[float], k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Tuple[Document, float]]: try: from timescale_vector import client except ImportError: raise ImportError( "Could not import timescale_vector python package. " "Please install it with `pip install timescale-vector`." ) results = await self.async_client.search( embedding, limit=k, filter=filter, predicates=predicates, uuid_time_filter=self.date_to_range_filter(**kwargs), ) docs = [ ( Document( page_content=result[client.SEARCH_RESULT_CONTENTS_IDX], metadata=result[client.SEARCH_RESULT_METADATA_IDX], ), result[client.SEARCH_RESULT_DISTANCE_IDX], ) for result in results ] return docs def similarity_search_by_vector( self, embedding: List[float], k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Document]: """Return docs most similar to embedding vector. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query vector. """ docs_and_scores = self.similarity_search_with_score_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs ) return [doc for doc, _ in docs_and_scores] async def asimilarity_search_by_vector( self, embedding: List[float], k: int = 4, filter: Optional[Union[dict, list]] = None, predicates: Optional[Predicates] = None, **kwargs: Any, ) -> List[Document]: """Return docs most similar to embedding vector. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query vector. """ docs_and_scores = await self.asimilarity_search_with_score_by_vector( embedding=embedding, k=k, filter=filter, predicates=predicates, **kwargs ) return [doc for doc, _ in docs_and_scores] @classmethod def from_texts( cls: Type[TimescaleVector], texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: """ Return VectorStore initialized from texts and embeddings. Postgres connection string is required "Either pass it as a parameter or set the TIMESCALE_SERVICE_URL environment variable. """ embeddings = embedding.embed_documents(list(texts)) return cls.__from( texts, embeddings, embedding, metadatas=metadatas, ids=ids, collection_name=collection_name, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) @classmethod async def afrom_texts( cls: Type[TimescaleVector], texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: """ Return VectorStore initialized from texts and embeddings. Postgres connection string is required "Either pass it as a parameter or set the TIMESCALE_SERVICE_URL environment variable. """ embeddings = embedding.embed_documents(list(texts)) return await cls.__afrom( texts, embeddings, embedding, metadatas=metadatas, ids=ids, collection_name=collection_name, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) @classmethod def from_embeddings( cls, text_embeddings: List[Tuple[str, List[float]]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: """Construct TimescaleVector wrapper from raw documents and pre- generated embeddings. Return VectorStore initialized from documents and embeddings. Postgres connection string is required "Either pass it as a parameter or set the TIMESCALE_SERVICE_URL environment variable. Example: .. code-block:: python from langchain.vectorstores import TimescaleVector from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() text_embeddings = embeddings.embed_documents(texts) text_embedding_pairs = list(zip(texts, text_embeddings)) tvs = TimescaleVector.from_embeddings(text_embedding_pairs, embeddings) """ texts = [t[0] for t in text_embeddings] embeddings = [t[1] for t in text_embeddings] return cls.__from( texts, embeddings, embedding, metadatas=metadatas, ids=ids, collection_name=collection_name, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) @classmethod async def afrom_embeddings( cls, text_embeddings: List[Tuple[str, List[float]]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: """Construct TimescaleVector wrapper from raw documents and pre- generated embeddings. Return VectorStore initialized from documents and embeddings. Postgres connection string is required "Either pass it as a parameter or set the TIMESCALE_SERVICE_URL environment variable. Example: .. code-block:: python from langchain.vectorstores import TimescaleVector from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() text_embeddings = embeddings.embed_documents(texts) text_embedding_pairs = list(zip(texts, text_embeddings)) tvs = TimescaleVector.from_embeddings(text_embedding_pairs, embeddings) """ texts = [t[0] for t in text_embeddings] embeddings = [t[1] for t in text_embeddings] return await cls.__afrom( texts, embeddings, embedding, metadatas=metadatas, ids=ids, collection_name=collection_name, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, **kwargs, ) @classmethod def from_existing_index( cls: Type[TimescaleVector], embedding: Embeddings, collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME, distance_strategy: DistanceStrategy = DEFAULT_DISTANCE_STRATEGY, pre_delete_collection: bool = False, **kwargs: Any, ) -> TimescaleVector: """ Get intsance of an existing TimescaleVector store.This method will return the instance of the store without inserting any new embeddings """ service_url = cls.get_service_url(kwargs) store = cls( service_url=service_url, collection_name=collection_name, embedding=embedding, distance_strategy=distance_strategy, pre_delete_collection=pre_delete_collection, ) return store @classmethod def get_service_url(cls, kwargs: Dict[str, Any]) -> str: service_url: str = get_from_dict_or_env( data=kwargs, key="service_url", env_key="TIMESCALE_SERVICE_URL", ) if not service_url: raise ValueError( "Postgres connection string is required" "Either pass it as a parameter" "or set the TIMESCALE_SERVICE_URL environment variable." ) return service_url @classmethod def service_url_from_db_params( cls, host: str, port: int, database: str, user: str, password: str, ) -> str: """Return connection string from database parameters.""" return f"postgresql://{user}:{password}@{host}:{port}/{database}" def _select_relevance_score_fn(self) -> Callable[[float], float]: """ The 'correct' relevance function may differ depending on a few things, including: - the distance / similarity metric used by the VectorStore - the scale of your embeddings (OpenAI's are unit normed. Many others are not!) - embedding dimensionality - etc. """ if self.override_relevance_score_fn is not None: return self.override_relevance_score_fn # Default strategy is to rely on distance strategy provided # in vectorstore constructor if self._distance_strategy == DistanceStrategy.COSINE: return self._cosine_relevance_score_fn elif self._distance_strategy == DistanceStrategy.EUCLIDEAN_DISTANCE: return self._euclidean_relevance_score_fn elif self._distance_strategy == DistanceStrategy.MAX_INNER_PRODUCT: return self._max_inner_product_relevance_score_fn else: raise ValueError( "No supported normalization function" f" for distance_strategy of {self._distance_strategy}." "Consider providing relevance_score_fn to TimescaleVector constructor." ) def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> Optional[bool]: """Delete by vector ID or other criteria. Args: ids: List of ids to delete. **kwargs: Other keyword arguments that subclasses might use. Returns: Optional[bool]: True if deletion is successful, False otherwise, None if not implemented. """ if ids is None: raise ValueError("No ids provided to delete.") self.sync_client.delete_by_ids(ids) return True # todo should this be part of delete|()? def delete_by_metadata( self, filter: Union[Dict[str, str], List[Dict[str, str]]], **kwargs: Any ) -> Optional[bool]: """Delete by vector ID or other criteria. Args: ids: List of ids to delete. **kwargs: Other keyword arguments that subclasses might use. Returns: Optional[bool]: True if deletion is successful, False otherwise, None if not implemented. """ self.sync_client.delete_by_metadata(filter) return True class IndexType(str, enum.Enum): """Enumerator for the supported Index types""" TIMESCALE_VECTOR = "tsv" PGVECTOR_IVFFLAT = "ivfflat" PGVECTOR_HNSW = "hnsw" DEFAULT_INDEX_TYPE = IndexType.TIMESCALE_VECTOR def create_index( self, index_type: Union[IndexType, str] = DEFAULT_INDEX_TYPE, **kwargs: Any ) -> None: try: from timescale_vector import client except ImportError: raise ImportError( "Could not import timescale_vector python package. " "Please install it with `pip install timescale-vector`." ) index_type = ( index_type.value if isinstance(index_type, self.IndexType) else index_type ) if index_type == self.IndexType.PGVECTOR_IVFFLAT.value: self.sync_client.create_embedding_index(client.IvfflatIndex(**kwargs)) if index_type == self.IndexType.PGVECTOR_HNSW.value: self.sync_client.create_embedding_index(client.HNSWIndex(**kwargs)) if index_type == self.IndexType.TIMESCALE_VECTOR.value: self.sync_client.create_embedding_index( client.TimescaleVectorIndex(**kwargs) ) def drop_index(self) -> None: self.sync_client.drop_embedding_index()
[]
2024-01-10
blairmain/langchain
libs~langchain~langchain~document_loaders~recursive_url_loader.py
from __future__ import annotations import asyncio import logging import re from typing import ( TYPE_CHECKING, Callable, Iterator, List, Optional, Sequence, Set, Union, ) import requests from langchain.docstore.document import Document from langchain.document_loaders.base import BaseLoader from langchain.utils.html import extract_sub_links if TYPE_CHECKING: import aiohttp logger = logging.getLogger(__name__) def _metadata_extractor(raw_html: str, url: str) -> dict: """Extract metadata from raw html using BeautifulSoup.""" metadata = {"source": url} try: from bs4 import BeautifulSoup except ImportError: logger.warning( "The bs4 package is required for default metadata extraction. " "Please install it with `pip install bs4`." ) return metadata soup = BeautifulSoup(raw_html, "html.parser") if title := soup.find("title"): metadata["title"] = title.get_text() if description := soup.find("meta", attrs={"name": "description"}): metadata["description"] = description.get("content", None) if html := soup.find("html"): metadata["language"] = html.get("lang", None) return metadata class RecursiveUrlLoader(BaseLoader): """Load all child links from a URL page.""" def __init__( self, url: str, max_depth: Optional[int] = 2, use_async: Optional[bool] = None, extractor: Optional[Callable[[str], str]] = None, metadata_extractor: Optional[Callable[[str, str], str]] = None, exclude_dirs: Optional[Sequence[str]] = (), timeout: Optional[int] = 10, prevent_outside: Optional[bool] = True, link_regex: Union[str, re.Pattern, None] = None, headers: Optional[dict] = None, check_response_status: bool = False, ) -> None: """Initialize with URL to crawl and any subdirectories to exclude. Args: url: The URL to crawl. max_depth: The max depth of the recursive loading. use_async: Whether to use asynchronous loading. If True, this function will not be lazy, but it will still work in the expected way, just not lazy. extractor: A function to extract document contents from raw html. When extract function returns an empty string, the document is ignored. metadata_extractor: A function to extract metadata from raw html and the source url (args in that order). Default extractor will attempt to use BeautifulSoup4 to extract the title, description and language of the page. exclude_dirs: A list of subdirectories to exclude. timeout: The timeout for the requests, in the unit of seconds. If None then connection will not timeout. prevent_outside: If True, prevent loading from urls which are not children of the root url. link_regex: Regex for extracting sub-links from the raw html of a web page. check_response_status: If True, check HTTP response status and skip URLs with error responses (400-599). """ self.url = url self.max_depth = max_depth if max_depth is not None else 2 self.use_async = use_async if use_async is not None else False self.extractor = extractor if extractor is not None else lambda x: x self.metadata_extractor = ( metadata_extractor if metadata_extractor is not None else _metadata_extractor ) self.exclude_dirs = exclude_dirs if exclude_dirs is not None else () self.timeout = timeout self.prevent_outside = prevent_outside if prevent_outside is not None else True self.link_regex = link_regex self._lock = asyncio.Lock() if self.use_async else None self.headers = headers self.check_response_status = check_response_status def _get_child_links_recursive( self, url: str, visited: Set[str], *, depth: int = 0 ) -> Iterator[Document]: """Recursively get all child links starting with the path of the input URL. Args: url: The URL to crawl. visited: A set of visited URLs. depth: Current depth of recursion. Stop when depth >= max_depth. """ if depth >= self.max_depth: return # Exclude the links that start with any of the excluded directories if any(url.startswith(exclude_dir) for exclude_dir in self.exclude_dirs): return # Get all links that can be accessed from the current URL visited.add(url) try: response = requests.get(url, timeout=self.timeout, headers=self.headers) if self.check_response_status and 400 <= response.status_code <= 599: raise ValueError(f"Received HTTP status {response.status_code}") except Exception as e: logger.warning( f"Unable to load from {url}. Received error {e} of type " f"{e.__class__.__name__}" ) return content = self.extractor(response.text) if content: yield Document( page_content=content, metadata=self.metadata_extractor(response.text, url), ) # Store the visited links and recursively visit the children sub_links = extract_sub_links( response.text, url, base_url=self.url, pattern=self.link_regex, prevent_outside=self.prevent_outside, ) for link in sub_links: # Check all unvisited links if link not in visited: yield from self._get_child_links_recursive( link, visited, depth=depth + 1 ) async def _async_get_child_links_recursive( self, url: str, visited: Set[str], *, session: Optional[aiohttp.ClientSession] = None, depth: int = 0, ) -> List[Document]: """Recursively get all child links starting with the path of the input URL. Args: url: The URL to crawl. visited: A set of visited URLs. depth: To reach the current url, how many pages have been visited. """ try: import aiohttp except ImportError: raise ImportError( "The aiohttp package is required for the RecursiveUrlLoader. " "Please install it with `pip install aiohttp`." ) if depth >= self.max_depth: return [] # Exclude the root and parent from a list # Exclude the links that start with any of the excluded directories if any(url.startswith(exclude_dir) for exclude_dir in self.exclude_dirs): return [] # Disable SSL verification because websites may have invalid SSL certificates, # but won't cause any security issues for us. close_session = session is None session = ( session if session is not None else aiohttp.ClientSession( connector=aiohttp.TCPConnector(ssl=False), timeout=aiohttp.ClientTimeout(total=self.timeout), headers=self.headers, ) ) async with self._lock: # type: ignore visited.add(url) try: async with session.get(url) as response: text = await response.text() if self.check_response_status and 400 <= response.status <= 599: raise ValueError(f"Received HTTP status {response.status}") except (aiohttp.client_exceptions.InvalidURL, Exception) as e: logger.warning( f"Unable to load {url}. Received error {e} of type " f"{e.__class__.__name__}" ) if close_session: await session.close() return [] results = [] content = self.extractor(text) if content: results.append( Document( page_content=content, metadata=self.metadata_extractor(text, url), ) ) if depth < self.max_depth - 1: sub_links = extract_sub_links( text, url, base_url=self.url, pattern=self.link_regex, prevent_outside=self.prevent_outside, ) # Recursively call the function to get the children of the children sub_tasks = [] async with self._lock: # type: ignore to_visit = set(sub_links).difference(visited) for link in to_visit: sub_tasks.append( self._async_get_child_links_recursive( link, visited, session=session, depth=depth + 1 ) ) next_results = await asyncio.gather(*sub_tasks) for sub_result in next_results: if isinstance(sub_result, Exception) or sub_result is None: # We don't want to stop the whole process, so just ignore it # Not standard html format or invalid url or 404 may cause this. continue # locking not fully working, temporary hack to ensure deduplication results += [r for r in sub_result if r not in results] if close_session: await session.close() return results def lazy_load(self) -> Iterator[Document]: """Lazy load web pages. When use_async is True, this function will not be lazy, but it will still work in the expected way, just not lazy.""" visited: Set[str] = set() if self.use_async: results = asyncio.run( self._async_get_child_links_recursive(self.url, visited) ) return iter(results or []) else: return self._get_child_links_recursive(self.url, visited) def load(self) -> List[Document]: """Load web pages.""" return list(self.lazy_load())
[]
2024-01-10
blairmain/langchain
libs~langchain~tests~unit_tests~schema~runnable~test_runnable.py
from operator import itemgetter from typing import Any, Dict, List, Optional, Sequence, Union, cast from uuid import UUID import pytest from freezegun import freeze_time from pytest_mock import MockerFixture from syrupy import SnapshotAssertion from langchain.callbacks.manager import Callbacks, collect_runs from langchain.callbacks.tracers.base import BaseTracer from langchain.callbacks.tracers.log_stream import RunLog, RunLogPatch from langchain.callbacks.tracers.schemas import Run from langchain.callbacks.tracers.stdout import ConsoleCallbackHandler from langchain.chat_models.fake import FakeListChatModel from langchain.llms.fake import FakeListLLM, FakeStreamingListLLM from langchain.load.dump import dumpd, dumps from langchain.output_parsers.list import CommaSeparatedListOutputParser from langchain.prompts import PromptTemplate from langchain.prompts.chat import ( ChatPromptTemplate, ChatPromptValue, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from langchain.schema.document import Document from langchain.schema.messages import ( AIMessage, AIMessageChunk, HumanMessage, SystemMessage, ) from langchain.schema.output_parser import BaseOutputParser, StrOutputParser from langchain.schema.retriever import BaseRetriever from langchain.schema.runnable import ( RouterRunnable, Runnable, RunnableBranch, RunnableConfig, RunnableLambda, RunnableMap, RunnablePassthrough, RunnableSequence, RunnableWithFallbacks, ) class FakeTracer(BaseTracer): """Fake tracer that records LangChain execution. It replaces run ids with deterministic UUIDs for snapshotting.""" def __init__(self) -> None: """Initialize the tracer.""" super().__init__() self.runs: List[Run] = [] self.uuids_map: Dict[UUID, UUID] = {} self.uuids_generator = ( UUID(f"00000000-0000-4000-8000-{i:012}", version=4) for i in range(10000) ) def _replace_uuid(self, uuid: UUID) -> UUID: if uuid not in self.uuids_map: self.uuids_map[uuid] = next(self.uuids_generator) return self.uuids_map[uuid] def _copy_run(self, run: Run) -> Run: return run.copy( update={ "id": self._replace_uuid(run.id), "parent_run_id": self.uuids_map[run.parent_run_id] if run.parent_run_id else None, "child_runs": [self._copy_run(child) for child in run.child_runs], "execution_order": None, "child_execution_order": None, } ) def _persist_run(self, run: Run) -> None: """Persist a run.""" self.runs.append(self._copy_run(run)) class FakeRunnable(Runnable[str, int]): def invoke( self, input: str, config: Optional[RunnableConfig] = None, ) -> int: return len(input) class FakeRetriever(BaseRetriever): def _get_relevant_documents( self, query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any, ) -> List[Document]: return [Document(page_content="foo"), Document(page_content="bar")] async def _aget_relevant_documents( self, query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any, ) -> List[Document]: return [Document(page_content="foo"), Document(page_content="bar")] @pytest.mark.asyncio async def test_with_config(mocker: MockerFixture) -> None: fake = FakeRunnable() spy = mocker.spy(fake, "invoke") assert fake.with_config(tags=["a-tag"]).invoke("hello") == 5 assert spy.call_args_list == [ mocker.call("hello", dict(tags=["a-tag"])), ] spy.reset_mock() fake_1: Runnable = RunnablePassthrough() fake_2: Runnable = RunnablePassthrough() spy_seq_step = mocker.spy(fake_1.__class__, "invoke") sequence = fake_1.with_config(tags=["a-tag"]) | fake_2.with_config( tags=["b-tag"], max_concurrency=5 ) assert sequence.invoke("hello") == "hello" assert len(spy_seq_step.call_args_list) == 2 for i, call in enumerate(spy_seq_step.call_args_list): assert call.args[1] == "hello" if i == 0: assert call.args[2].get("tags") == ["a-tag"] assert call.args[2].get("max_concurrency") is None else: assert call.args[2].get("tags") == ["b-tag"] assert call.args[2].get("max_concurrency") == 5 mocker.stop(spy_seq_step) assert [ *fake.with_config(tags=["a-tag"]).stream( "hello", dict(metadata={"key": "value"}) ) ] == [5] assert spy.call_args_list == [ mocker.call("hello", dict(tags=["a-tag"], metadata={"key": "value"})), ] spy.reset_mock() assert fake.with_config(recursion_limit=5).batch( ["hello", "wooorld"], [dict(tags=["a-tag"]), dict(metadata={"key": "value"})] ) == [5, 7] assert len(spy.call_args_list) == 2 for i, call in enumerate(spy.call_args_list): assert call.args[0] == ("hello" if i == 0 else "wooorld") if i == 0: assert call.args[1].get("recursion_limit") == 5 assert call.args[1].get("tags") == ["a-tag"] assert call.args[1].get("metadata") == {} else: assert call.args[1].get("recursion_limit") == 5 assert call.args[1].get("tags") == [] assert call.args[1].get("metadata") == {"key": "value"} spy.reset_mock() assert fake.with_config(metadata={"a": "b"}).batch( ["hello", "wooorld"], dict(tags=["a-tag"]) ) == [5, 7] assert len(spy.call_args_list) == 2 for i, call in enumerate(spy.call_args_list): assert call.args[0] == ("hello" if i == 0 else "wooorld") assert call.args[1].get("tags") == ["a-tag"] assert call.args[1].get("metadata") == {"a": "b"} spy.reset_mock() handler = ConsoleCallbackHandler() assert ( await fake.with_config(metadata={"a": "b"}).ainvoke( "hello", config={"callbacks": [handler]} ) == 5 ) assert spy.call_args_list == [ mocker.call("hello", dict(callbacks=[handler], metadata={"a": "b"})), ] spy.reset_mock() assert [ part async for part in fake.with_config(metadata={"a": "b"}).astream("hello") ] == [5] assert spy.call_args_list == [ mocker.call("hello", dict(metadata={"a": "b"})), ] spy.reset_mock() assert await fake.with_config(recursion_limit=5, tags=["c"]).abatch( ["hello", "wooorld"], dict(metadata={"key": "value"}) ) == [ 5, 7, ] assert spy.call_args_list == [ mocker.call( "hello", dict( metadata={"key": "value"}, tags=["c"], callbacks=None, locals={}, recursion_limit=5, ), ), mocker.call( "wooorld", dict( metadata={"key": "value"}, tags=["c"], callbacks=None, locals={}, recursion_limit=5, ), ), ] @pytest.mark.asyncio async def test_default_method_implementations(mocker: MockerFixture) -> None: fake = FakeRunnable() spy = mocker.spy(fake, "invoke") assert fake.invoke("hello", dict(tags=["a-tag"])) == 5 assert spy.call_args_list == [ mocker.call("hello", dict(tags=["a-tag"])), ] spy.reset_mock() assert [*fake.stream("hello", dict(metadata={"key": "value"}))] == [5] assert spy.call_args_list == [ mocker.call("hello", dict(metadata={"key": "value"})), ] spy.reset_mock() assert fake.batch( ["hello", "wooorld"], [dict(tags=["a-tag"]), dict(metadata={"key": "value"})] ) == [5, 7] assert len(spy.call_args_list) == 2 for i, call in enumerate(spy.call_args_list): assert call.args[0] == ("hello" if i == 0 else "wooorld") if i == 0: assert call.args[1].get("tags") == ["a-tag"] assert call.args[1].get("metadata") == {} else: assert call.args[1].get("tags") == [] assert call.args[1].get("metadata") == {"key": "value"} spy.reset_mock() assert fake.batch(["hello", "wooorld"], dict(tags=["a-tag"])) == [5, 7] assert len(spy.call_args_list) == 2 for i, call in enumerate(spy.call_args_list): assert call.args[0] == ("hello" if i == 0 else "wooorld") assert call.args[1].get("tags") == ["a-tag"] assert call.args[1].get("metadata") == {} spy.reset_mock() assert await fake.ainvoke("hello", config={"callbacks": []}) == 5 assert spy.call_args_list == [ mocker.call("hello", dict(callbacks=[])), ] spy.reset_mock() assert [part async for part in fake.astream("hello")] == [5] assert spy.call_args_list == [ mocker.call("hello", None), ] spy.reset_mock() assert await fake.abatch(["hello", "wooorld"], dict(metadata={"key": "value"})) == [ 5, 7, ] assert spy.call_args_list == [ mocker.call( "hello", dict( metadata={"key": "value"}, tags=[], callbacks=None, locals={}, recursion_limit=10, ), ), mocker.call( "wooorld", dict( metadata={"key": "value"}, tags=[], callbacks=None, locals={}, recursion_limit=10, ), ), ] @pytest.mark.asyncio async def test_prompt() -> None: prompt = ChatPromptTemplate.from_messages( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessagePromptTemplate.from_template("{question}"), ] ) expected = ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert prompt.invoke({"question": "What is your name?"}) == expected assert prompt.batch( [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ] ) == [ expected, ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your favorite color?"), ] ), ] assert [*prompt.stream({"question": "What is your name?"})] == [expected] assert await prompt.ainvoke({"question": "What is your name?"}) == expected assert await prompt.abatch( [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ] ) == [ expected, ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your favorite color?"), ] ), ] assert [ part async for part in prompt.astream({"question": "What is your name?"}) ] == [expected] stream_log = [ part async for part in prompt.astream_log({"question": "What is your name?"}) ] assert len(stream_log[0].ops) == 1 assert stream_log[0].ops[0]["op"] == "replace" assert stream_log[0].ops[0]["path"] == "" assert stream_log[0].ops[0]["value"]["logs"] == [] assert stream_log[0].ops[0]["value"]["final_output"] is None assert stream_log[0].ops[0]["value"]["streamed_output"] == [] assert type(stream_log[0].ops[0]["value"]["id"]) == UUID assert stream_log[1:] == [ RunLogPatch( { "op": "replace", "path": "/final_output", "value": { "id": ["langchain", "prompts", "chat", "ChatPromptValue"], "kwargs": { "messages": [ { "id": [ "langchain", "schema", "messages", "SystemMessage", ], "kwargs": {"content": "You are a nice " "assistant."}, "lc": 1, "type": "constructor", }, { "id": [ "langchain", "schema", "messages", "HumanMessage", ], "kwargs": { "additional_kwargs": {}, "content": "What is your " "name?", }, "lc": 1, "type": "constructor", }, ] }, "lc": 1, "type": "constructor", }, } ), RunLogPatch({"op": "add", "path": "/streamed_output/-", "value": expected}), ] def test_prompt_template_params() -> None: prompt = ChatPromptTemplate.from_template( "Respond to the following question: {question}" ) result = prompt.invoke( { "question": "test", "topic": "test", } ) assert result == ChatPromptValue( messages=[HumanMessage(content="Respond to the following question: test")] ) with pytest.raises(KeyError): prompt.invoke({}) @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_prompt_with_chat_model( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat = FakeListChatModel(responses=["foo"]) chain = prompt | chat assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [] assert chain.last == chat assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "invoke") tracer = FakeTracer() assert chain.invoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == AIMessage(content="foo") assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert tracer.runs == snapshot mocker.stop(prompt_spy) mocker.stop(chat_spy) # Test batch prompt_spy = mocker.spy(prompt.__class__, "batch") chat_spy = mocker.spy(chat.__class__, "batch") tracer = FakeTracer() assert chain.batch( [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ], dict(callbacks=[tracer]), ) == [ AIMessage(content="foo"), AIMessage(content="foo"), ] assert prompt_spy.call_args.args[1] == [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ] assert chat_spy.call_args.args[1] == [ ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ), ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your favorite color?"), ] ), ] assert ( len( [ r for r in tracer.runs if r.parent_run_id is None and len(r.child_runs) == 2 ] ) == 2 ), "Each of 2 outer runs contains exactly two inner runs (1 prompt, 1 chat)" mocker.stop(prompt_spy) mocker.stop(chat_spy) # Test stream prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "stream") tracer = FakeTracer() assert [ *chain.stream({"question": "What is your name?"}, dict(callbacks=[tracer])) ] == [AIMessage(content="f"), AIMessage(content="o"), AIMessage(content="o")] assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_prompt_with_llm( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) llm = FakeListLLM(responses=["foo", "bar"]) chain = prompt | llm assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [] assert chain.last == llm assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "ainvoke") llm_spy = mocker.spy(llm.__class__, "ainvoke") tracer = FakeTracer() assert ( await chain.ainvoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == "foo" ) assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert llm_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert tracer.runs == snapshot mocker.stop(prompt_spy) mocker.stop(llm_spy) # Test batch prompt_spy = mocker.spy(prompt.__class__, "abatch") llm_spy = mocker.spy(llm.__class__, "abatch") tracer = FakeTracer() assert await chain.abatch( [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ], dict(callbacks=[tracer]), ) == ["bar", "foo"] assert prompt_spy.call_args.args[1] == [ {"question": "What is your name?"}, {"question": "What is your favorite color?"}, ] assert llm_spy.call_args.args[1] == [ ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ), ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your favorite color?"), ] ), ] assert tracer.runs == snapshot mocker.stop(prompt_spy) mocker.stop(llm_spy) # Test stream prompt_spy = mocker.spy(prompt.__class__, "ainvoke") llm_spy = mocker.spy(llm.__class__, "astream") tracer = FakeTracer() assert [ token async for token in chain.astream( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) ] == ["bar"] assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert llm_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) prompt_spy.reset_mock() llm_spy.reset_mock() stream_log = [ part async for part in chain.astream_log({"question": "What is your name?"}) ] # remove ids from logs for part in stream_log: for op in part.ops: if ( isinstance(op["value"], dict) and "id" in op["value"] and not isinstance(op["value"]["id"], list) # serialized lc id ): del op["value"]["id"] assert stream_log == [ RunLogPatch( { "op": "replace", "path": "", "value": { "logs": [], "final_output": None, "streamed_output": [], }, } ), RunLogPatch( { "op": "add", "path": "/logs/0", "value": { "end_time": None, "final_output": None, "metadata": {}, "name": "ChatPromptTemplate", "start_time": "2023-01-01T00:00:00.000", "streamed_output_str": [], "tags": ["seq:step:1"], "type": "prompt", }, } ), RunLogPatch( { "op": "add", "path": "/logs/0/final_output", "value": { "id": ["langchain", "prompts", "chat", "ChatPromptValue"], "kwargs": { "messages": [ { "id": [ "langchain", "schema", "messages", "SystemMessage", ], "kwargs": { "additional_kwargs": {}, "content": "You are a nice " "assistant.", }, "lc": 1, "type": "constructor", }, { "id": [ "langchain", "schema", "messages", "HumanMessage", ], "kwargs": { "additional_kwargs": {}, "content": "What is your " "name?", }, "lc": 1, "type": "constructor", }, ] }, "lc": 1, "type": "constructor", }, }, { "op": "add", "path": "/logs/0/end_time", "value": "2023-01-01T00:00:00.000", }, ), RunLogPatch( { "op": "add", "path": "/logs/1", "value": { "end_time": None, "final_output": None, "metadata": {}, "name": "FakeListLLM", "start_time": "2023-01-01T00:00:00.000", "streamed_output_str": [], "tags": ["seq:step:2"], "type": "llm", }, } ), RunLogPatch( { "op": "add", "path": "/logs/1/final_output", "value": { "generations": [[{"generation_info": None, "text": "foo"}]], "llm_output": None, "run": None, }, }, { "op": "add", "path": "/logs/1/end_time", "value": "2023-01-01T00:00:00.000", }, ), RunLogPatch({"op": "add", "path": "/streamed_output/-", "value": "foo"}), RunLogPatch( {"op": "replace", "path": "/final_output", "value": {"output": "foo"}} ), ] @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_prompt_with_llm_and_async_lambda( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) llm = FakeListLLM(responses=["foo", "bar"]) async def passthrough(input: Any) -> Any: return input chain = prompt | llm | passthrough assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [llm] assert chain.last == RunnableLambda(func=passthrough) assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "ainvoke") llm_spy = mocker.spy(llm.__class__, "ainvoke") tracer = FakeTracer() assert ( await chain.ainvoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == "foo" ) assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert llm_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert tracer.runs == snapshot mocker.stop(prompt_spy) mocker.stop(llm_spy) @freeze_time("2023-01-01") def test_prompt_with_chat_model_and_parser( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat = FakeListChatModel(responses=["foo, bar"]) parser = CommaSeparatedListOutputParser() chain = prompt | chat | parser assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [chat] assert chain.last == parser assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "invoke") parser_spy = mocker.spy(parser.__class__, "invoke") tracer = FakeTracer() assert chain.invoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == ["foo", "bar"] assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert parser_spy.call_args.args[1] == AIMessage(content="foo, bar") assert tracer.runs == snapshot @freeze_time("2023-01-01") def test_combining_sequences( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat = FakeListChatModel(responses=["foo, bar"]) parser = CommaSeparatedListOutputParser() chain = prompt | chat | parser assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [chat] assert chain.last == parser assert dumps(chain, pretty=True) == snapshot prompt2 = ( SystemMessagePromptTemplate.from_template("You are a nicer assistant.") + "{question}" ) chat2 = FakeListChatModel(responses=["baz, qux"]) parser2 = CommaSeparatedListOutputParser() input_formatter: RunnableLambda[List[str], Dict[str, Any]] = RunnableLambda( lambda x: {"question": x[0] + x[1]} ) chain2 = input_formatter | prompt2 | chat2 | parser2 assert isinstance(chain, RunnableSequence) assert chain2.first == input_formatter assert chain2.middle == [prompt2, chat2] assert chain2.last == parser2 assert dumps(chain2, pretty=True) == snapshot combined_chain = chain | chain2 assert combined_chain.first == prompt assert combined_chain.middle == [ chat, parser, input_formatter, prompt2, chat2, ] assert combined_chain.last == parser2 assert dumps(combined_chain, pretty=True) == snapshot # Test invoke tracer = FakeTracer() assert combined_chain.invoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == ["baz", "qux"] assert tracer.runs == snapshot @freeze_time("2023-01-01") def test_seq_dict_prompt_llm( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: passthrough = mocker.Mock(side_effect=lambda x: x) retriever = FakeRetriever() prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + """Context: {documents} Question: {question}""" ) chat = FakeListChatModel(responses=["foo, bar"]) parser = CommaSeparatedListOutputParser() chain: Runnable = ( { "question": RunnablePassthrough[str]() | passthrough, "documents": passthrough | retriever, "just_to_test_lambda": passthrough, } | prompt | chat | parser ) assert isinstance(chain, RunnableSequence) assert isinstance(chain.first, RunnableMap) assert chain.middle == [prompt, chat] assert chain.last == parser assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "invoke") parser_spy = mocker.spy(parser.__class__, "invoke") tracer = FakeTracer() assert chain.invoke("What is your name?", dict(callbacks=[tracer])) == [ "foo", "bar", ] assert prompt_spy.call_args.args[1] == { "documents": [Document(page_content="foo"), Document(page_content="bar")], "question": "What is your name?", "just_to_test_lambda": "What is your name?", } assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage( content="""Context: [Document(page_content='foo', metadata={}), Document(page_content='bar', metadata={})] Question: What is your name?""" ), ] ) assert parser_spy.call_args.args[1] == AIMessage(content="foo, bar") assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 4 map_run = parent_run.child_runs[0] assert map_run.name == "RunnableMap" assert len(map_run.child_runs) == 3 @freeze_time("2023-01-01") def test_seq_prompt_dict(mocker: MockerFixture, snapshot: SnapshotAssertion) -> None: passthrough = mocker.Mock(side_effect=lambda x: x) prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat = FakeListChatModel(responses=["i'm a chatbot"]) llm = FakeListLLM(responses=["i'm a textbot"]) chain = ( prompt | passthrough | { "chat": chat, "llm": llm, } ) assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [RunnableLambda(passthrough)] assert isinstance(chain.last, RunnableMap) assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "invoke") llm_spy = mocker.spy(llm.__class__, "invoke") tracer = FakeTracer() assert chain.invoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == { "chat": AIMessage(content="i'm a chatbot"), "llm": "i'm a textbot", } assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert llm_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 3 map_run = parent_run.child_runs[2] assert map_run.name == "RunnableMap" assert len(map_run.child_runs) == 2 @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_router_runnable( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: chain1 = ChatPromptTemplate.from_template( "You are a math genius. Answer the question: {question}" ) | FakeListLLM(responses=["4"]) chain2 = ChatPromptTemplate.from_template( "You are an english major. Answer the question: {question}" ) | FakeListLLM(responses=["2"]) router = RouterRunnable({"math": chain1, "english": chain2}) chain: Runnable = { "key": lambda x: x["key"], "input": {"question": lambda x: x["question"]}, } | router assert dumps(chain, pretty=True) == snapshot result = chain.invoke({"key": "math", "question": "2 + 2"}) assert result == "4" result2 = chain.batch( [{"key": "math", "question": "2 + 2"}, {"key": "english", "question": "2 + 2"}] ) assert result2 == ["4", "2"] result = await chain.ainvoke({"key": "math", "question": "2 + 2"}) assert result == "4" result2 = await chain.abatch( [{"key": "math", "question": "2 + 2"}, {"key": "english", "question": "2 + 2"}] ) assert result2 == ["4", "2"] # Test invoke router_spy = mocker.spy(router.__class__, "invoke") tracer = FakeTracer() assert ( chain.invoke({"key": "math", "question": "2 + 2"}, dict(callbacks=[tracer])) == "4" ) assert router_spy.call_args.args[1] == { "key": "math", "input": {"question": "2 + 2"}, } assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 2 router_run = parent_run.child_runs[1] assert router_run.name == "RunnableSequence" # TODO: should be RunnableRouter assert len(router_run.child_runs) == 2 @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_higher_order_lambda_runnable( mocker: MockerFixture, snapshot: SnapshotAssertion ) -> None: math_chain = ChatPromptTemplate.from_template( "You are a math genius. Answer the question: {question}" ) | FakeListLLM(responses=["4"]) english_chain = ChatPromptTemplate.from_template( "You are an english major. Answer the question: {question}" ) | FakeListLLM(responses=["2"]) input_map: Runnable = RunnableMap( { # type: ignore[arg-type] "key": lambda x: x["key"], "input": {"question": lambda x: x["question"]}, } ) def router(input: Dict[str, Any]) -> Runnable: if input["key"] == "math": return itemgetter("input") | math_chain elif input["key"] == "english": return itemgetter("input") | english_chain else: raise ValueError(f"Unknown key: {input['key']}") chain: Runnable = input_map | router assert dumps(chain, pretty=True) == snapshot result = chain.invoke({"key": "math", "question": "2 + 2"}) assert result == "4" result2 = chain.batch( [{"key": "math", "question": "2 + 2"}, {"key": "english", "question": "2 + 2"}] ) assert result2 == ["4", "2"] result = await chain.ainvoke({"key": "math", "question": "2 + 2"}) assert result == "4" result2 = await chain.abatch( [{"key": "math", "question": "2 + 2"}, {"key": "english", "question": "2 + 2"}] ) assert result2 == ["4", "2"] # Test invoke math_spy = mocker.spy(math_chain.__class__, "invoke") tracer = FakeTracer() assert ( chain.invoke({"key": "math", "question": "2 + 2"}, dict(callbacks=[tracer])) == "4" ) assert math_spy.call_args.args[1] == { "key": "math", "input": {"question": "2 + 2"}, } assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 2 router_run = parent_run.child_runs[1] assert router_run.name == "router" assert len(router_run.child_runs) == 1 math_run = router_run.child_runs[0] assert math_run.name == "RunnableSequence" assert len(math_run.child_runs) == 3 # Test ainvoke async def arouter(input: Dict[str, Any]) -> Runnable: if input["key"] == "math": return itemgetter("input") | math_chain elif input["key"] == "english": return itemgetter("input") | english_chain else: raise ValueError(f"Unknown key: {input['key']}") achain: Runnable = input_map | arouter math_spy = mocker.spy(math_chain.__class__, "ainvoke") tracer = FakeTracer() assert ( await achain.ainvoke( {"key": "math", "question": "2 + 2"}, dict(callbacks=[tracer]) ) == "4" ) assert math_spy.call_args.args[1] == { "key": "math", "input": {"question": "2 + 2"}, } assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 2 router_run = parent_run.child_runs[1] assert router_run.name == "arouter" assert len(router_run.child_runs) == 1 math_run = router_run.child_runs[0] assert math_run.name == "RunnableSequence" assert len(math_run.child_runs) == 3 @freeze_time("2023-01-01") def test_seq_prompt_map(mocker: MockerFixture, snapshot: SnapshotAssertion) -> None: passthrough = mocker.Mock(side_effect=lambda x: x) prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat = FakeListChatModel(responses=["i'm a chatbot"]) llm = FakeListLLM(responses=["i'm a textbot"]) chain = ( prompt | passthrough | { "chat": chat.bind(stop=["Thought:"]), "llm": llm, "passthrough": passthrough, } ) assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [RunnableLambda(passthrough)] assert isinstance(chain.last, RunnableMap) assert dumps(chain, pretty=True) == snapshot # Test invoke prompt_spy = mocker.spy(prompt.__class__, "invoke") chat_spy = mocker.spy(chat.__class__, "invoke") llm_spy = mocker.spy(llm.__class__, "invoke") tracer = FakeTracer() assert chain.invoke( {"question": "What is your name?"}, dict(callbacks=[tracer]) ) == { "chat": AIMessage(content="i'm a chatbot"), "llm": "i'm a textbot", "passthrough": ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ), } assert prompt_spy.call_args.args[1] == {"question": "What is your name?"} assert chat_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert llm_spy.call_args.args[1] == ChatPromptValue( messages=[ SystemMessage(content="You are a nice assistant."), HumanMessage(content="What is your name?"), ] ) assert len([r for r in tracer.runs if r.parent_run_id is None]) == 1 parent_run = next(r for r in tracer.runs if r.parent_run_id is None) assert len(parent_run.child_runs) == 3 map_run = parent_run.child_runs[2] assert map_run.name == "RunnableMap" assert len(map_run.child_runs) == 3 def test_map_stream() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat_res = "i'm a chatbot" # sleep to better simulate a real stream chat = FakeListChatModel(responses=[chat_res], sleep=0.01) llm_res = "i'm a textbot" # sleep to better simulate a real stream llm = FakeStreamingListLLM(responses=[llm_res], sleep=0.01) chain: Runnable = prompt | { "chat": chat.bind(stop=["Thought:"]), "llm": llm, "passthrough": RunnablePassthrough(), } stream = chain.stream({"question": "What is your name?"}) final_value = None streamed_chunks = [] for chunk in stream: streamed_chunks.append(chunk) if final_value is None: final_value = chunk else: final_value += chunk assert streamed_chunks[0] in [ {"passthrough": prompt.invoke({"question": "What is your name?"})}, {"llm": "i"}, {"chat": AIMessageChunk(content="i")}, ] assert len(streamed_chunks) == len(chat_res) + len(llm_res) + 1 assert all(len(c.keys()) == 1 for c in streamed_chunks) assert final_value is not None assert final_value.get("chat").content == "i'm a chatbot" assert final_value.get("llm") == "i'm a textbot" assert final_value.get("passthrough") == prompt.invoke( {"question": "What is your name?"} ) def test_map_stream_iterator_input() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat_res = "i'm a chatbot" # sleep to better simulate a real stream chat = FakeListChatModel(responses=[chat_res], sleep=0.01) llm_res = "i'm a textbot" # sleep to better simulate a real stream llm = FakeStreamingListLLM(responses=[llm_res], sleep=0.01) chain: Runnable = ( prompt | llm | { "chat": chat.bind(stop=["Thought:"]), "llm": llm, "passthrough": RunnablePassthrough(), } ) stream = chain.stream({"question": "What is your name?"}) final_value = None streamed_chunks = [] for chunk in stream: streamed_chunks.append(chunk) if final_value is None: final_value = chunk else: final_value += chunk assert streamed_chunks[0] in [ {"passthrough": "i"}, {"llm": "i"}, {"chat": AIMessageChunk(content="i")}, ] assert len(streamed_chunks) == len(chat_res) + len(llm_res) + len(llm_res) assert all(len(c.keys()) == 1 for c in streamed_chunks) assert final_value is not None assert final_value.get("chat").content == "i'm a chatbot" assert final_value.get("llm") == "i'm a textbot" assert final_value.get("passthrough") == "i'm a textbot" @pytest.mark.asyncio async def test_map_astream() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat_res = "i'm a chatbot" # sleep to better simulate a real stream chat = FakeListChatModel(responses=[chat_res], sleep=0.01) llm_res = "i'm a textbot" # sleep to better simulate a real stream llm = FakeStreamingListLLM(responses=[llm_res], sleep=0.01) chain: Runnable = prompt | { "chat": chat.bind(stop=["Thought:"]), "llm": llm, "passthrough": RunnablePassthrough(), } stream = chain.astream({"question": "What is your name?"}) final_value = None streamed_chunks = [] async for chunk in stream: streamed_chunks.append(chunk) if final_value is None: final_value = chunk else: final_value += chunk assert streamed_chunks[0] in [ {"passthrough": prompt.invoke({"question": "What is your name?"})}, {"llm": "i"}, {"chat": AIMessageChunk(content="i")}, ] assert len(streamed_chunks) == len(chat_res) + len(llm_res) + 1 assert all(len(c.keys()) == 1 for c in streamed_chunks) assert final_value is not None assert final_value.get("chat").content == "i'm a chatbot" assert final_value.get("llm") == "i'm a textbot" assert final_value.get("passthrough") == prompt.invoke( {"question": "What is your name?"} ) # Test astream_log state accumulation final_state = None streamed_ops = [] async for chunk in chain.astream_log({"question": "What is your name?"}): streamed_ops.extend(chunk.ops) if final_state is None: final_state = chunk else: final_state += chunk final_state = cast(RunLog, final_state) assert final_state.state["final_output"] == final_value assert len(final_state.state["streamed_output"]) == len(streamed_chunks) assert isinstance(final_state.state["id"], UUID) assert len(final_state.ops) == len(streamed_ops) assert len(final_state.state["logs"]) == 5 assert final_state.state["logs"][0]["name"] == "ChatPromptTemplate" assert final_state.state["logs"][0]["final_output"] == dumpd( prompt.invoke({"question": "What is your name?"}) ) assert final_state.state["logs"][1]["name"] == "RunnableMap" assert sorted(log["name"] for log in final_state.state["logs"][2:]) == [ "FakeListChatModel", "FakeStreamingListLLM", "RunnablePassthrough", ] # Test astream_log with include filters final_state = None async for chunk in chain.astream_log( {"question": "What is your name?"}, include_names=["FakeListChatModel"] ): if final_state is None: final_state = chunk else: final_state += chunk final_state = cast(RunLog, final_state) assert final_state.state["final_output"] == final_value assert len(final_state.state["streamed_output"]) == len(streamed_chunks) assert len(final_state.state["logs"]) == 1 assert final_state.state["logs"][0]["name"] == "FakeListChatModel" # Test astream_log with exclude filters final_state = None async for chunk in chain.astream_log( {"question": "What is your name?"}, exclude_names=["FakeListChatModel"] ): if final_state is None: final_state = chunk else: final_state += chunk final_state = cast(RunLog, final_state) assert final_state.state["final_output"] == final_value assert len(final_state.state["streamed_output"]) == len(streamed_chunks) assert len(final_state.state["logs"]) == 4 assert final_state.state["logs"][0]["name"] == "ChatPromptTemplate" assert final_state.state["logs"][0]["final_output"] == dumpd( prompt.invoke({"question": "What is your name?"}) ) assert final_state.state["logs"][1]["name"] == "RunnableMap" assert sorted(log["name"] for log in final_state.state["logs"][2:]) == [ "FakeStreamingListLLM", "RunnablePassthrough", ] @pytest.mark.asyncio async def test_map_astream_iterator_input() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) chat_res = "i'm a chatbot" # sleep to better simulate a real stream chat = FakeListChatModel(responses=[chat_res], sleep=0.01) llm_res = "i'm a textbot" # sleep to better simulate a real stream llm = FakeStreamingListLLM(responses=[llm_res], sleep=0.01) chain: Runnable = ( prompt | llm | { "chat": chat.bind(stop=["Thought:"]), "llm": llm, "passthrough": RunnablePassthrough(), } ) stream = chain.astream({"question": "What is your name?"}) final_value = None streamed_chunks = [] async for chunk in stream: streamed_chunks.append(chunk) if final_value is None: final_value = chunk else: final_value += chunk assert streamed_chunks[0] in [ {"passthrough": "i"}, {"llm": "i"}, {"chat": AIMessageChunk(content="i")}, ] assert len(streamed_chunks) == len(chat_res) + len(llm_res) + len(llm_res) assert all(len(c.keys()) == 1 for c in streamed_chunks) assert final_value is not None assert final_value.get("chat").content == "i'm a chatbot" assert final_value.get("llm") == "i'm a textbot" assert final_value.get("passthrough") == llm_res def test_with_config_with_config() -> None: llm = FakeListLLM(responses=["i'm a textbot"]) assert dumpd( llm.with_config({"metadata": {"a": "b"}}).with_config(tags=["a-tag"]) ) == dumpd(llm.with_config({"metadata": {"a": "b"}, "tags": ["a-tag"]})) def test_metadata_is_merged() -> None: """Test metadata and tags defined in with_config and at are merged/concatend.""" foo = RunnableLambda(lambda x: x).with_config({"metadata": {"my_key": "my_value"}}) expected_metadata = { "my_key": "my_value", "my_other_key": "my_other_value", } with collect_runs() as cb: foo.invoke("hi", {"metadata": {"my_other_key": "my_other_value"}}) run = cb.traced_runs[0] assert run.extra["metadata"] == expected_metadata def test_tags_are_appended() -> None: """Test tags from with_config are concatenated with those in invocation.""" foo = RunnableLambda(lambda x: x).with_config({"tags": ["my_key"]}) with collect_runs() as cb: foo.invoke("hi", {"tags": ["invoked_key"]}) run = cb.traced_runs[0] assert isinstance(run.tags, list) assert sorted(run.tags) == sorted(["my_key", "invoked_key"]) def test_bind_bind() -> None: llm = FakeListLLM(responses=["i'm a textbot"]) assert dumpd( llm.bind(stop=["Thought:"], one="two").bind( stop=["Observation:"], hello="world" ) ) == dumpd(llm.bind(stop=["Observation:"], one="two", hello="world")) def test_deep_stream() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) llm = FakeStreamingListLLM(responses=["foo-lish"]) chain = prompt | llm | StrOutputParser() stream = chain.stream({"question": "What up"}) chunks = [] for chunk in stream: chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" chunks = [] for chunk in (chain | RunnablePassthrough()).stream({"question": "What up"}): chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" @pytest.mark.asyncio async def test_deep_astream() -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) llm = FakeStreamingListLLM(responses=["foo-lish"]) chain = prompt | llm | StrOutputParser() stream = chain.astream({"question": "What up"}) chunks = [] async for chunk in stream: chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" chunks = [] async for chunk in (chain | RunnablePassthrough()).astream({"question": "What up"}): chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" def test_runnable_sequence_transform() -> None: llm = FakeStreamingListLLM(responses=["foo-lish"]) chain = llm | StrOutputParser() stream = chain.transform(llm.stream("Hi there!")) chunks = [] for chunk in stream: chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" @pytest.mark.asyncio async def test_runnable_sequence_atransform() -> None: llm = FakeStreamingListLLM(responses=["foo-lish"]) chain = llm | StrOutputParser() stream = chain.atransform(llm.astream("Hi there!")) chunks = [] async for chunk in stream: chunks.append(chunk) assert len(chunks) == len("foo-lish") assert "".join(chunks) == "foo-lish" @pytest.fixture() def llm_with_fallbacks() -> RunnableWithFallbacks: error_llm = FakeListLLM(responses=["foo"], i=1) pass_llm = FakeListLLM(responses=["bar"]) return error_llm.with_fallbacks([pass_llm]) @pytest.fixture() def llm_with_multi_fallbacks() -> RunnableWithFallbacks: error_llm = FakeListLLM(responses=["foo"], i=1) error_llm_2 = FakeListLLM(responses=["baz"], i=1) pass_llm = FakeListLLM(responses=["bar"]) return error_llm.with_fallbacks([error_llm_2, pass_llm]) @pytest.fixture() def llm_chain_with_fallbacks() -> RunnableSequence: error_llm = FakeListLLM(responses=["foo"], i=1) pass_llm = FakeListLLM(responses=["bar"]) prompt = PromptTemplate.from_template("what did baz say to {buz}") return RunnableMap({"buz": lambda x: x}) | (prompt | error_llm).with_fallbacks( [prompt | pass_llm] ) @pytest.mark.parametrize( "runnable", ["llm_with_fallbacks", "llm_with_multi_fallbacks", "llm_chain_with_fallbacks"], ) @pytest.mark.asyncio async def test_llm_with_fallbacks( runnable: RunnableWithFallbacks, request: Any, snapshot: SnapshotAssertion ) -> None: runnable = request.getfixturevalue(runnable) assert runnable.invoke("hello") == "bar" assert runnable.batch(["hi", "hey", "bye"]) == ["bar"] * 3 assert list(runnable.stream("hello")) == ["bar"] assert await runnable.ainvoke("hello") == "bar" assert await runnable.abatch(["hi", "hey", "bye"]) == ["bar"] * 3 assert list(await runnable.ainvoke("hello")) == list("bar") assert dumps(runnable, pretty=True) == snapshot class FakeSplitIntoListParser(BaseOutputParser[List[str]]): """Parse the output of an LLM call to a comma-separated list.""" @property def lc_serializable(self) -> bool: return True def get_format_instructions(self) -> str: return ( "Your response should be a list of comma separated values, " "eg: `foo, bar, baz`" ) def parse(self, text: str) -> List[str]: """Parse the output of an LLM call.""" return text.strip().split(", ") def test_each_simple() -> None: """Test that each() works with a simple runnable.""" parser = FakeSplitIntoListParser() assert parser.invoke("first item, second item") == ["first item", "second item"] assert parser.map().invoke(["a, b", "c"]) == [["a", "b"], ["c"]] assert parser.map().map().invoke([["a, b", "c"], ["c, e"]]) == [ [["a", "b"], ["c"]], [["c", "e"]], ] def test_each(snapshot: SnapshotAssertion) -> None: prompt = ( SystemMessagePromptTemplate.from_template("You are a nice assistant.") + "{question}" ) first_llm = FakeStreamingListLLM(responses=["first item, second item, third item"]) parser = FakeSplitIntoListParser() second_llm = FakeStreamingListLLM(responses=["this", "is", "a", "test"]) chain = prompt | first_llm | parser | second_llm.map() assert dumps(chain, pretty=True) == snapshot output = chain.invoke({"question": "What up"}) assert output == ["this", "is", "a"] assert (parser | second_llm.map()).invoke("first item, second item") == [ "test", "this", ] def test_recursive_lambda() -> None: def _simple_recursion(x: int) -> Union[int, Runnable]: if x < 10: return RunnableLambda(lambda *args: _simple_recursion(x + 1)) else: return x runnable = RunnableLambda(_simple_recursion) assert runnable.invoke(5) == 10 with pytest.raises(RecursionError): runnable.invoke(0, {"recursion_limit": 9}) def test_retrying(mocker: MockerFixture) -> None: def _lambda(x: int) -> Union[int, Runnable]: if x == 1: raise ValueError("x is 1") elif x == 2: raise RuntimeError("x is 2") else: return x _lambda_mock = mocker.Mock(side_effect=_lambda) runnable = RunnableLambda(_lambda_mock) with pytest.raises(ValueError): runnable.invoke(1) assert _lambda_mock.call_count == 1 _lambda_mock.reset_mock() with pytest.raises(ValueError): runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).invoke(1) assert _lambda_mock.call_count == 2 # retried _lambda_mock.reset_mock() with pytest.raises(RuntimeError): runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).invoke(2) assert _lambda_mock.call_count == 1 # did not retry _lambda_mock.reset_mock() with pytest.raises(ValueError): runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).batch([1, 2, 0]) # 3rd input isn't retried because it succeeded assert _lambda_mock.call_count == 3 + 2 _lambda_mock.reset_mock() output = runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).batch([1, 2, 0], return_exceptions=True) # 3rd input isn't retried because it succeeded assert _lambda_mock.call_count == 3 + 2 assert len(output) == 3 assert isinstance(output[0], ValueError) assert isinstance(output[1], RuntimeError) assert output[2] == 0 _lambda_mock.reset_mock() @pytest.mark.asyncio async def test_async_retrying(mocker: MockerFixture) -> None: def _lambda(x: int) -> Union[int, Runnable]: if x == 1: raise ValueError("x is 1") elif x == 2: raise RuntimeError("x is 2") else: return x _lambda_mock = mocker.Mock(side_effect=_lambda) runnable = RunnableLambda(_lambda_mock) with pytest.raises(ValueError): await runnable.ainvoke(1) assert _lambda_mock.call_count == 1 _lambda_mock.reset_mock() with pytest.raises(ValueError): await runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError, KeyError), ).ainvoke(1) assert _lambda_mock.call_count == 2 # retried _lambda_mock.reset_mock() with pytest.raises(RuntimeError): await runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).ainvoke(2) assert _lambda_mock.call_count == 1 # did not retry _lambda_mock.reset_mock() with pytest.raises(ValueError): await runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).abatch([1, 2, 0]) # 3rd input isn't retried because it succeeded assert _lambda_mock.call_count == 3 + 2 _lambda_mock.reset_mock() output = await runnable.with_retry( stop_after_attempt=2, retry_if_exception_type=(ValueError,), ).abatch([1, 2, 0], return_exceptions=True) # 3rd input isn't retried because it succeeded assert _lambda_mock.call_count == 3 + 2 assert len(output) == 3 assert isinstance(output[0], ValueError) assert isinstance(output[1], RuntimeError) assert output[2] == 0 _lambda_mock.reset_mock() @freeze_time("2023-01-01") def test_seq_batch_return_exceptions(mocker: MockerFixture) -> None: class ControlledExceptionRunnable(Runnable[str, str]): def __init__(self, fail_starts_with: str) -> None: self.fail_starts_with = fail_starts_with def invoke(self, input: Any, config: Optional[RunnableConfig] = None) -> Any: raise NotImplementedError() def _batch( self, inputs: List[str], ) -> List: outputs: List[Any] = [] for input in inputs: if input.startswith(self.fail_starts_with): outputs.append(ValueError()) else: outputs.append(input + "a") return outputs def batch( self, inputs: List[str], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Any, ) -> List[str]: return self._batch_with_config( self._batch, inputs, config, return_exceptions=return_exceptions, **kwargs, ) chain = ( ControlledExceptionRunnable("bux") | ControlledExceptionRunnable("bar") | ControlledExceptionRunnable("baz") | ControlledExceptionRunnable("foo") ) assert isinstance(chain, RunnableSequence) # Test batch with pytest.raises(ValueError): chain.batch(["foo", "bar", "baz", "qux"]) spy = mocker.spy(ControlledExceptionRunnable, "batch") tracer = FakeTracer() inputs = ["foo", "bar", "baz", "qux"] outputs = chain.batch(inputs, dict(callbacks=[tracer]), return_exceptions=True) assert len(outputs) == 4 assert isinstance(outputs[0], ValueError) assert isinstance(outputs[1], ValueError) assert isinstance(outputs[2], ValueError) assert outputs[3] == "quxaaaa" assert spy.call_count == 4 inputs_to_batch = [c[0][1] for c in spy.call_args_list] assert inputs_to_batch == [ # inputs to sequence step 0 # same as inputs to sequence.batch() ["foo", "bar", "baz", "qux"], # inputs to sequence step 1 # == outputs of sequence step 0 as no exceptions were raised ["fooa", "bara", "baza", "quxa"], # inputs to sequence step 2 # 'bar' was dropped as it raised an exception in step 1 ["fooaa", "bazaa", "quxaa"], # inputs to sequence step 3 # 'baz' was dropped as it raised an exception in step 2 ["fooaaa", "quxaaa"], ] parent_runs = sorted( (r for r in tracer.runs if r.parent_run_id is None), key=lambda run: inputs.index(run.inputs["input"]), ) assert len(parent_runs) == 4 parent_run_foo = parent_runs[0] assert parent_run_foo.inputs["input"] == "foo" assert parent_run_foo.error == repr(ValueError()) assert len(parent_run_foo.child_runs) == 4 assert [r.error for r in parent_run_foo.child_runs] == [ None, None, None, repr(ValueError()), ] parent_run_bar = parent_runs[1] assert parent_run_bar.inputs["input"] == "bar" assert parent_run_bar.error == repr(ValueError()) assert len(parent_run_bar.child_runs) == 2 assert [r.error for r in parent_run_bar.child_runs] == [ None, repr(ValueError()), ] parent_run_baz = parent_runs[2] assert parent_run_baz.inputs["input"] == "baz" assert parent_run_baz.error == repr(ValueError()) assert len(parent_run_baz.child_runs) == 3 assert [r.error for r in parent_run_baz.child_runs] == [ None, None, repr(ValueError()), ] parent_run_qux = parent_runs[3] assert parent_run_qux.inputs["input"] == "qux" assert parent_run_qux.error is None assert parent_run_qux.outputs["output"] == "quxaaaa" assert len(parent_run_qux.child_runs) == 4 assert [r.error for r in parent_run_qux.child_runs] == [None, None, None, None] @pytest.mark.asyncio @freeze_time("2023-01-01") async def test_seq_abatch_return_exceptions(mocker: MockerFixture) -> None: class ControlledExceptionRunnable(Runnable[str, str]): def __init__(self, fail_starts_with: str) -> None: self.fail_starts_with = fail_starts_with def invoke(self, input: Any, config: Optional[RunnableConfig] = None) -> Any: raise NotImplementedError() async def _abatch( self, inputs: List[str], ) -> List: outputs: List[Any] = [] for input in inputs: if input.startswith(self.fail_starts_with): outputs.append(ValueError()) else: outputs.append(input + "a") return outputs async def abatch( self, inputs: List[str], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Any, ) -> List[str]: return await self._abatch_with_config( self._abatch, inputs, config, return_exceptions=return_exceptions, **kwargs, ) chain = ( ControlledExceptionRunnable("bux") | ControlledExceptionRunnable("bar") | ControlledExceptionRunnable("baz") | ControlledExceptionRunnable("foo") ) assert isinstance(chain, RunnableSequence) # Test abatch with pytest.raises(ValueError): await chain.abatch(["foo", "bar", "baz", "qux"]) spy = mocker.spy(ControlledExceptionRunnable, "abatch") tracer = FakeTracer() inputs = ["foo", "bar", "baz", "qux"] outputs = await chain.abatch( inputs, dict(callbacks=[tracer]), return_exceptions=True ) assert len(outputs) == 4 assert isinstance(outputs[0], ValueError) assert isinstance(outputs[1], ValueError) assert isinstance(outputs[2], ValueError) assert outputs[3] == "quxaaaa" assert spy.call_count == 4 inputs_to_batch = [c[0][1] for c in spy.call_args_list] assert inputs_to_batch == [ # inputs to sequence step 0 # same as inputs to sequence.batch() ["foo", "bar", "baz", "qux"], # inputs to sequence step 1 # == outputs of sequence step 0 as no exceptions were raised ["fooa", "bara", "baza", "quxa"], # inputs to sequence step 2 # 'bar' was dropped as it raised an exception in step 1 ["fooaa", "bazaa", "quxaa"], # inputs to sequence step 3 # 'baz' was dropped as it raised an exception in step 2 ["fooaaa", "quxaaa"], ] parent_runs = sorted( (r for r in tracer.runs if r.parent_run_id is None), key=lambda run: inputs.index(run.inputs["input"]), ) assert len(parent_runs) == 4 parent_run_foo = parent_runs[0] assert parent_run_foo.inputs["input"] == "foo" assert parent_run_foo.error == repr(ValueError()) assert len(parent_run_foo.child_runs) == 4 assert [r.error for r in parent_run_foo.child_runs] == [ None, None, None, repr(ValueError()), ] parent_run_bar = parent_runs[1] assert parent_run_bar.inputs["input"] == "bar" assert parent_run_bar.error == repr(ValueError()) assert len(parent_run_bar.child_runs) == 2 assert [r.error for r in parent_run_bar.child_runs] == [ None, repr(ValueError()), ] parent_run_baz = parent_runs[2] assert parent_run_baz.inputs["input"] == "baz" assert parent_run_baz.error == repr(ValueError()) assert len(parent_run_baz.child_runs) == 3 assert [r.error for r in parent_run_baz.child_runs] == [ None, None, repr(ValueError()), ] parent_run_qux = parent_runs[3] assert parent_run_qux.inputs["input"] == "qux" assert parent_run_qux.error is None assert parent_run_qux.outputs["output"] == "quxaaaa" assert len(parent_run_qux.child_runs) == 4 assert [r.error for r in parent_run_qux.child_runs] == [None, None, None, None] def test_runnable_branch_init() -> None: """Verify that runnable branch gets initialized properly.""" add = RunnableLambda(lambda x: x + 1) condition = RunnableLambda(lambda x: x > 0) # Test failure with less than 2 branches with pytest.raises(ValueError): RunnableBranch((condition, add)) # Test failure with less than 2 branches with pytest.raises(ValueError): RunnableBranch(condition) @pytest.mark.parametrize( "branches", [ [ (RunnableLambda(lambda x: x > 0), RunnableLambda(lambda x: x + 1)), RunnableLambda(lambda x: x - 1), ], [ (RunnableLambda(lambda x: x > 0), RunnableLambda(lambda x: x + 1)), (RunnableLambda(lambda x: x > 5), RunnableLambda(lambda x: x + 1)), RunnableLambda(lambda x: x - 1), ], [ (lambda x: x > 0, lambda x: x + 1), (lambda x: x > 5, lambda x: x + 1), lambda x: x - 1, ], ], ) def test_runnable_branch_init_coercion(branches: Sequence[Any]) -> None: """Verify that runnable branch gets initialized properly.""" runnable = RunnableBranch[int, int](*branches) for branch in runnable.branches: condition, body = branch assert isinstance(condition, Runnable) assert isinstance(body, Runnable) assert isinstance(runnable.default, Runnable) def test_runnable_branch_invoke_call_counts(mocker: MockerFixture) -> None: """Verify that runnables are invoked only when necessary.""" # Test with single branch add = RunnableLambda(lambda x: x + 1) sub = RunnableLambda(lambda x: x - 1) condition = RunnableLambda(lambda x: x > 0) spy = mocker.spy(condition, "invoke") add_spy = mocker.spy(add, "invoke") branch = RunnableBranch[int, int]((condition, add), (condition, add), sub) assert spy.call_count == 0 assert add_spy.call_count == 0 assert branch.invoke(1) == 2 assert add_spy.call_count == 1 assert spy.call_count == 1 assert branch.invoke(2) == 3 assert spy.call_count == 2 assert add_spy.call_count == 2 assert branch.invoke(-3) == -4 # Should fall through to default branch with condition being evaluated twice! assert spy.call_count == 4 # Add should not be invoked assert add_spy.call_count == 2 def test_runnable_branch_invoke() -> None: # Test with single branch def raise_value_error(x: int) -> int: """Raise a value error.""" raise ValueError("x is too large") branch = RunnableBranch[int, int]( (lambda x: x > 100, raise_value_error), # mypy cannot infer types from the lambda (lambda x: x > 0 and x < 5, lambda x: x + 1), # type: ignore[misc] (lambda x: x > 5, lambda x: x * 10), lambda x: x - 1, ) assert branch.invoke(1) == 2 assert branch.invoke(10) == 100 assert branch.invoke(0) == -1 # Should raise an exception with pytest.raises(ValueError): branch.invoke(1000) def test_runnable_branch_batch() -> None: """Test batch variant.""" # Test with single branch branch = RunnableBranch[int, int]( (lambda x: x > 0 and x < 5, lambda x: x + 1), (lambda x: x > 5, lambda x: x * 10), lambda x: x - 1, ) assert branch.batch([1, 10, 0]) == [2, 100, -1] @pytest.mark.asyncio async def test_runnable_branch_ainvoke() -> None: """Test async variant of invoke.""" branch = RunnableBranch[int, int]( (lambda x: x > 0 and x < 5, lambda x: x + 1), (lambda x: x > 5, lambda x: x * 10), lambda x: x - 1, ) assert await branch.ainvoke(1) == 2 assert await branch.ainvoke(10) == 100 assert await branch.ainvoke(0) == -1 # Verify that the async variant is used if available async def condition(x: int) -> bool: return x > 0 async def add(x: int) -> int: return x + 1 async def sub(x: int) -> int: return x - 1 branch = RunnableBranch[int, int]((condition, add), sub) assert await branch.ainvoke(1) == 2 assert await branch.ainvoke(-10) == -11 def test_runnable_branch_invoke_callbacks() -> None: """Verify that callbacks are correctly used in invoke.""" tracer = FakeTracer() def raise_value_error(x: int) -> int: """Raise a value error.""" raise ValueError("x is too large") branch = RunnableBranch[int, int]( (lambda x: x > 100, raise_value_error), lambda x: x - 1, ) assert branch.invoke(1, config={"callbacks": [tracer]}) == 0 assert len(tracer.runs) == 1 assert tracer.runs[0].error is None assert tracer.runs[0].outputs == {"output": 0} # Check that the chain on end is invoked with pytest.raises(ValueError): branch.invoke(1000, config={"callbacks": [tracer]}) assert len(tracer.runs) == 2 assert tracer.runs[1].error == "ValueError('x is too large')" assert tracer.runs[1].outputs is None @pytest.mark.asyncio async def test_runnable_branch_ainvoke_callbacks() -> None: """Verify that callbacks are invoked correctly in ainvoke.""" tracer = FakeTracer() async def raise_value_error(x: int) -> int: """Raise a value error.""" raise ValueError("x is too large") branch = RunnableBranch[int, int]( (lambda x: x > 100, raise_value_error), lambda x: x - 1, ) assert await branch.ainvoke(1, config={"callbacks": [tracer]}) == 0 assert len(tracer.runs) == 1 assert tracer.runs[0].error is None assert tracer.runs[0].outputs == {"output": 0} # Check that the chain on end is invoked with pytest.raises(ValueError): await branch.ainvoke(1000, config={"callbacks": [tracer]}) assert len(tracer.runs) == 2 assert tracer.runs[1].error == "ValueError('x is too large')" assert tracer.runs[1].outputs is None @pytest.mark.asyncio async def test_runnable_branch_abatch() -> None: """Test async variant of invoke.""" branch = RunnableBranch[int, int]( (lambda x: x > 0 and x < 5, lambda x: x + 1), (lambda x: x > 5, lambda x: x * 10), lambda x: x - 1, ) assert await branch.abatch([1, 10, 0]) == [2, 100, -1]
[ "Context:\n{documents}\n\nQuestion:\n{question}", "What is your favorite color?", "{question}", "Respond to the following question: test", "You are a nice assistant.", "i'm a chatbot", "o", "What is your name?", "foo", "You are an english major. Answer the question: {question}", "Respond to the following question: {question}", "f", "invoke", "what did baz say to {buz}", "You are a nicer assistant.", "ainvoke", "Context:\n[Document(page_content='foo', metadata={}), Document(page_content='bar', metadata={})]\n\nQuestion:\nWhat is your name?", "foo, bar", "You are a math genius. Answer the question: {question}" ]
2024-01-10
blairmain/langchain
libs~langchain~tests~unit_tests~test_dependencies.py
"""A unit test meant to catch accidental introduction of non-optional dependencies.""" from pathlib import Path from typing import Any, Dict, Mapping import pytest import toml HERE = Path(__file__).parent PYPROJECT_TOML = HERE / "../../pyproject.toml" @pytest.fixture() def poetry_conf() -> Dict[str, Any]: """Load the pyproject.toml file.""" with open(PYPROJECT_TOML) as f: return toml.load(f)["tool"]["poetry"] def test_required_dependencies(poetry_conf: Mapping[str, Any]) -> None: """A test that checks if a new non-optional dependency is being introduced. If this test is triggered, it means that a contributor is trying to introduce a new required dependency. This should be avoided in most situations. """ # Get the dependencies from the [tool.poetry.dependencies] section dependencies = poetry_conf["dependencies"] is_required = { package_name: isinstance(requirements, str) or not requirements.get("optional", False) for package_name, requirements in dependencies.items() } required_dependencies = [ package_name for package_name, required in is_required.items() if required ] assert sorted(required_dependencies) == [ "PyYAML", "SQLAlchemy", "aiohttp", "anyio", "async-timeout", "dataclasses-json", "jsonpatch", "langsmith", "numexpr", "numpy", "pydantic", "python", "requests", "tenacity", ] unrequired_dependencies = [ package_name for package_name, required in is_required.items() if not required ] in_extras = [dep for group in poetry_conf["extras"].values() for dep in group] assert set(unrequired_dependencies) == set(in_extras) def test_test_group_dependencies(poetry_conf: Mapping[str, Any]) -> None: """Check if someone is attempting to add additional test dependencies. Only dependencies associated with test running infrastructure should be added to the test group; e.g., pytest, pytest-cov etc. Examples of dependencies that should NOT be included: boto3, azure, postgres, etc. """ test_group_deps = sorted(poetry_conf["group"]["test"]["dependencies"]) assert test_group_deps == [ "duckdb-engine", "freezegun", "lark", "pandas", "pytest", "pytest-asyncio", "pytest-cov", "pytest-dotenv", "pytest-mock", "pytest-socket", "pytest-watcher", "responses", "syrupy", ] def test_imports() -> None: """Test that you can import all top level things okay.""" from langchain.agents import OpenAIFunctionsAgent # noqa: F401 from langchain.callbacks import OpenAICallbackHandler # noqa: F401 from langchain.chains import LLMChain # noqa: F401 from langchain.chat_models import ChatOpenAI # noqa: F401 from langchain.document_loaders import BSHTMLLoader # noqa: F401 from langchain.embeddings import OpenAIEmbeddings # noqa: F401 from langchain.llms import OpenAI # noqa: F401 from langchain.retrievers import VespaRetriever # noqa: F401 from langchain.schema import BasePromptTemplate # noqa: F401 from langchain.tools import DuckDuckGoSearchResults # noqa: F401 from langchain.utilities import SerpAPIWrapper # noqa: F401 from langchain.vectorstores import FAISS # noqa: F401
[]
2024-01-10
blairmain/langchain
libs~langchain~tests~integration_tests~vectorstores~test_xata.py
"""Test Xata vector store functionality. Before running this test, please create a Xata database by following the instructions from: https://python.langchain.com/docs/integrations/vectorstores/xata """ import os from langchain.docstore.document import Document from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores.xata import XataVectorStore class TestXata: @classmethod def setup_class(cls) -> None: assert os.getenv("XATA_API_KEY"), "XATA_API_KEY environment variable is not set" assert os.getenv("XATA_DB_URL"), "XATA_DB_URL environment variable is not set" def test_similarity_search_without_metadata( self, embedding_openai: OpenAIEmbeddings ) -> None: """Test end to end constructions and search without metadata.""" texts = ["foo", "bar", "baz"] docsearch = XataVectorStore.from_texts( api_key=os.getenv("XATA_API_KEY"), db_url=os.getenv("XATA_DB_URL"), texts=texts, embedding=embedding_openai, ) docsearch.wait_for_indexing(ndocs=3) output = docsearch.similarity_search("foo", k=1) assert output == [Document(page_content="foo")] docsearch.delete(delete_all=True) def test_similarity_search_with_metadata( self, embedding_openai: OpenAIEmbeddings ) -> None: """Test end to end construction and search with a metadata filter. This test requires a column named "a" of type integer to be present in the Xata table.""" texts = ["foo", "foo", "foo"] metadatas = [{"a": i} for i in range(len(texts))] docsearch = XataVectorStore.from_texts( api_key=os.getenv("XATA_API_KEY"), db_url=os.getenv("XATA_DB_URL"), texts=texts, embedding=embedding_openai, metadatas=metadatas, ) docsearch.wait_for_indexing(ndocs=3) output = docsearch.similarity_search("foo", k=1, filter={"a": 1}) assert output == [Document(page_content="foo", metadata={"a": 1})] docsearch.delete(delete_all=True)
[]
2024-01-10
blairmain/langchain
libs~langchain~langchain~callbacks~tracers~log_stream.py
from __future__ import annotations import math import threading from typing import ( Any, AsyncIterator, Dict, List, Optional, Sequence, TypedDict, Union, ) from uuid import UUID import jsonpatch from anyio import create_memory_object_stream from langchain.callbacks.tracers.base import BaseTracer from langchain.callbacks.tracers.schemas import Run from langchain.schema.output import ChatGenerationChunk, GenerationChunk class LogEntry(TypedDict): id: str """ID of the sub-run.""" name: str """Name of the object being run.""" type: str """Type of the object being run, eg. prompt, chain, llm, etc.""" tags: List[str] """List of tags for the run.""" metadata: Dict[str, Any] """Key-value pairs of metadata for the run.""" start_time: str """ISO-8601 timestamp of when the run started.""" streamed_output_str: List[str] """List of LLM tokens streamed by this run, if applicable.""" final_output: Optional[Any] """Final output of this run. Only available after the run has finished successfully.""" end_time: Optional[str] """ISO-8601 timestamp of when the run ended. Only available after the run has finished.""" class RunState(TypedDict): id: str """ID of the run.""" streamed_output: List[Any] """List of output chunks streamed by Runnable.stream()""" final_output: Optional[Any] """Final output of the run, usually the result of aggregating streamed_output. Only available after the run has finished successfully.""" logs: list[LogEntry] """List of sub-runs contained in this run, if any, in the order they were started. If filters were supplied, this list will contain only the runs that matched the filters.""" class RunLogPatch: ops: List[Dict[str, Any]] """List of jsonpatch operations, which describe how to create the run state from an empty dict. This is the minimal representation of the log, designed to be serialized as JSON and sent over the wire to reconstruct the log on the other side. Reconstruction of the state can be done with any jsonpatch-compliant library, see https://jsonpatch.com for more information.""" def __init__(self, *ops: Dict[str, Any]) -> None: self.ops = list(ops) def __add__(self, other: Union[RunLogPatch, Any]) -> RunLogPatch: if type(other) == RunLogPatch: ops = self.ops + other.ops state = jsonpatch.apply_patch(None, ops) return RunLog(*ops, state=state) raise TypeError( f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'" ) def __repr__(self) -> str: from pprint import pformat return f"RunLogPatch(ops={pformat(self.ops)})" def __eq__(self, other: object) -> bool: return isinstance(other, RunLogPatch) and self.ops == other.ops class RunLog(RunLogPatch): state: RunState """Current state of the log, obtained from applying all ops in sequence.""" def __init__(self, *ops: Dict[str, Any], state: RunState) -> None: super().__init__(*ops) self.state = state def __add__(self, other: Union[RunLogPatch, Any]) -> RunLogPatch: if type(other) == RunLogPatch: ops = self.ops + other.ops state = jsonpatch.apply_patch(self.state, other.ops) return RunLog(*ops, state=state) raise TypeError( f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'" ) def __repr__(self) -> str: from pprint import pformat return f"RunLog(state={pformat(self.state)})" class LogStreamCallbackHandler(BaseTracer): def __init__( self, *, auto_close: bool = True, include_names: Optional[Sequence[str]] = None, include_types: Optional[Sequence[str]] = None, include_tags: Optional[Sequence[str]] = None, exclude_names: Optional[Sequence[str]] = None, exclude_types: Optional[Sequence[str]] = None, exclude_tags: Optional[Sequence[str]] = None, ) -> None: super().__init__() self.auto_close = auto_close self.include_names = include_names self.include_types = include_types self.include_tags = include_tags self.exclude_names = exclude_names self.exclude_types = exclude_types self.exclude_tags = exclude_tags send_stream, receive_stream = create_memory_object_stream( math.inf, item_type=RunLogPatch ) self.lock = threading.Lock() self.send_stream = send_stream self.receive_stream = receive_stream self._index_map: Dict[UUID, int] = {} def __aiter__(self) -> AsyncIterator[RunLogPatch]: return self.receive_stream.__aiter__() def include_run(self, run: Run) -> bool: if run.parent_run_id is None: return False run_tags = run.tags or [] if ( self.include_names is None and self.include_types is None and self.include_tags is None ): include = True else: include = False if self.include_names is not None: include = include or run.name in self.include_names if self.include_types is not None: include = include or run.run_type in self.include_types if self.include_tags is not None: include = include or any(tag in self.include_tags for tag in run_tags) if self.exclude_names is not None: include = include and run.name not in self.exclude_names if self.exclude_types is not None: include = include and run.run_type not in self.exclude_types if self.exclude_tags is not None: include = include and all(tag not in self.exclude_tags for tag in run_tags) return include def _persist_run(self, run: Run) -> None: # This is a legacy method only called once for an entire run tree # therefore not useful here pass def _on_run_create(self, run: Run) -> None: """Start a run.""" if run.parent_run_id is None: self.send_stream.send_nowait( RunLogPatch( { "op": "replace", "path": "", "value": RunState( id=run.id, streamed_output=[], final_output=None, logs=[], ), } ) ) if not self.include_run(run): return # Determine previous index, increment by 1 with self.lock: self._index_map[run.id] = max(self._index_map.values(), default=-1) + 1 # Add the run to the stream self.send_stream.send_nowait( RunLogPatch( { "op": "add", "path": f"/logs/{self._index_map[run.id]}", "value": LogEntry( id=str(run.id), name=run.name, type=run.run_type, tags=run.tags or [], metadata=run.extra.get("metadata", {}), start_time=run.start_time.isoformat(timespec="milliseconds"), streamed_output_str=[], final_output=None, end_time=None, ), } ) ) def _on_run_update(self, run: Run) -> None: """Finish a run.""" try: index = self._index_map.get(run.id) if index is None: return self.send_stream.send_nowait( RunLogPatch( { "op": "add", "path": f"/logs/{index}/final_output", "value": run.outputs, }, { "op": "add", "path": f"/logs/{index}/end_time", "value": run.end_time.isoformat(timespec="milliseconds"), }, ) ) finally: if run.parent_run_id is None: self.send_stream.send_nowait( RunLogPatch( { "op": "replace", "path": "/final_output", "value": run.outputs, } ) ) if self.auto_close: self.send_stream.close() def _on_llm_new_token( self, run: Run, token: str, chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]], ) -> None: """Process new LLM token.""" index = self._index_map.get(run.id) if index is None: return self.send_stream.send_nowait( RunLogPatch( { "op": "add", "path": f"/logs/{index}/streamed_output_str/-", "value": token, } ) )
[]
2024-01-10
blairmain/langchain
libs~langchain~tests~unit_tests~utils~test_html.py
from langchain.utils.html import ( PREFIXES_TO_IGNORE, SUFFIXES_TO_IGNORE, extract_sub_links, find_all_links, ) def test_find_all_links_none() -> None: html = "<span>Hello world</span>" actual = find_all_links(html) assert actual == [] def test_find_all_links_single() -> None: htmls = [ "href='foobar.com'", 'href="foobar.com"', '<div><a class="blah" href="foobar.com">hullo</a></div>', ] actual = [find_all_links(html) for html in htmls] assert actual == [["foobar.com"]] * 3 def test_find_all_links_multiple() -> None: html = ( '<div><a class="blah" href="https://foobar.com">hullo</a></div>' '<div><a class="bleh" href="/baz/cool">buhbye</a></div>' ) actual = find_all_links(html) assert sorted(actual) == [ "/baz/cool", "https://foobar.com", ] def test_find_all_links_ignore_suffix() -> None: html = 'href="foobar{suffix}"' for suffix in SUFFIXES_TO_IGNORE: actual = find_all_links(html.format(suffix=suffix)) assert actual == [] # Don't ignore if pattern doesn't occur at end of link. html = 'href="foobar{suffix}more"' for suffix in SUFFIXES_TO_IGNORE: actual = find_all_links(html.format(suffix=suffix)) assert actual == [f"foobar{suffix}more"] def test_find_all_links_ignore_prefix() -> None: html = 'href="{prefix}foobar"' for prefix in PREFIXES_TO_IGNORE: actual = find_all_links(html.format(prefix=prefix)) assert actual == [] # Don't ignore if pattern doesn't occur at beginning of link. html = 'href="foobar{prefix}more"' for prefix in PREFIXES_TO_IGNORE: # Pound signs are split on when not prefixes. if prefix == "#": continue actual = find_all_links(html.format(prefix=prefix)) assert actual == [f"foobar{prefix}more"] def test_find_all_links_drop_fragment() -> None: html = 'href="foobar.com/woah#section_one"' actual = find_all_links(html) assert actual == ["foobar.com/woah"] def test_extract_sub_links() -> None: html = ( '<a href="https://foobar.com">one</a>' '<a href="http://baz.net">two</a>' '<a href="//foobar.com/hello">three</a>' '<a href="/how/are/you/doing">four</a>' ) expected = sorted( [ "https://foobar.com", "https://foobar.com/hello", "https://foobar.com/how/are/you/doing", ] ) actual = sorted(extract_sub_links(html, "https://foobar.com")) assert actual == expected actual = extract_sub_links(html, "https://foobar.com/hello") expected = ["https://foobar.com/hello"] assert actual == expected actual = sorted( extract_sub_links(html, "https://foobar.com/hello", prevent_outside=False) ) expected = sorted( [ "https://foobar.com", "http://baz.net", "https://foobar.com/hello", "https://foobar.com/how/are/you/doing", ] ) assert actual == expected def test_extract_sub_links_base() -> None: html = ( '<a href="https://foobar.com">one</a>' '<a href="http://baz.net">two</a>' '<a href="//foobar.com/hello">three</a>' '<a href="/how/are/you/doing">four</a>' '<a href="alexis.html"</a>' ) expected = sorted( [ "https://foobar.com", "https://foobar.com/hello", "https://foobar.com/how/are/you/doing", "https://foobar.com/hello/alexis.html", ] ) actual = sorted( extract_sub_links( html, "https://foobar.com/hello/bill.html", base_url="https://foobar.com" ) ) assert actual == expected
[]
2024-01-10
blairmain/langchain
libs~experimental~langchain_experimental~comprehend_moderation~toxicity.py
import asyncio import importlib from typing import Any, List, Optional from langchain_experimental.comprehend_moderation.base_moderation_exceptions import ( ModerationToxicityError, ) class ComprehendToxicity: def __init__( self, client: Any, callback: Optional[Any] = None, unique_id: Optional[str] = None, chain_id: Optional[str] = None, ) -> None: self.client = client self.moderation_beacon = { "moderation_chain_id": chain_id, "moderation_type": "Toxicity", "moderation_status": "LABELS_NOT_FOUND", } self.callback = callback self.unique_id = unique_id def _toxicity_init_validate(self, max_size: int) -> Any: """ Validate and initialize toxicity processing configuration. Args: max_size (int): Maximum sentence size defined in the configuration object. Raises: Exception: If the maximum sentence size exceeds the 5KB limit. Note: This function ensures that the NLTK punkt tokenizer is downloaded if not already present. Returns: None """ if max_size > 1024 * 5: raise Exception("The sentence length should not exceed 5KB.") try: nltk = importlib.import_module("nltk") nltk.data.find("tokenizers/punkt") return nltk except ImportError: raise ModuleNotFoundError( "Could not import nltk python package. " "Please install it with `pip install nltk`." ) except LookupError: nltk.download("punkt") def _split_paragraph( self, prompt_value: str, max_size: int = 1024 * 4 ) -> List[List[str]]: """ Split a paragraph into chunks of sentences, respecting the maximum size limit. Args: paragraph (str): The input paragraph to be split into chunks. max_size (int, optional): The maximum size limit in bytes for each chunk. Defaults to 1024. Returns: List[List[str]]: A list of chunks, where each chunk is a list of sentences. Note: This function validates the maximum sentence size based on service limits using the 'toxicity_init_validate' function. It uses the NLTK sentence tokenizer to split the paragraph into sentences. Example: paragraph = "This is a sample paragraph. It contains multiple sentences. ..." chunks = split_paragraph(paragraph, max_size=2048) """ # validate max. sentence size based on Service limits nltk = self._toxicity_init_validate(max_size) sentences = nltk.sent_tokenize(prompt_value) chunks = list() # type: ignore current_chunk = list() # type: ignore current_size = 0 for sentence in sentences: sentence_size = len(sentence.encode("utf-8")) # If adding a new sentence exceeds max_size # or current_chunk has 10 sentences, start a new chunk if (current_size + sentence_size > max_size) or (len(current_chunk) >= 10): if current_chunk: # Avoid appending empty chunks chunks.append(current_chunk) current_chunk = [] current_size = 0 current_chunk.append(sentence) current_size += sentence_size # Add any remaining sentences if current_chunk: chunks.append(current_chunk) return chunks def validate(self, prompt_value: str, config: Any = None) -> str: """ Check the toxicity of a given text prompt using AWS Comprehend service and apply actions based on configuration. Args: prompt_value (str): The text content to be checked for toxicity. config (Dict[str, Any]): Configuration for toxicity checks and actions. Returns: str: The original prompt_value if allowed or no toxicity found. Raises: ValueError: If the prompt contains toxic labels and cannot be processed based on the configuration. """ chunks = self._split_paragraph(prompt_value=prompt_value) for sentence_list in chunks: segments = [{"Text": sentence} for sentence in sentence_list] response = self.client.detect_toxic_content( TextSegments=segments, LanguageCode="en" ) if self.callback and self.callback.toxicity_callback: self.moderation_beacon["moderation_input"] = segments # type: ignore self.moderation_beacon["moderation_output"] = response toxicity_found = False threshold = config.get("threshold") toxicity_labels = config.get("labels") if not toxicity_labels: for item in response["ResultList"]: for label in item["Labels"]: if label["Score"] >= threshold: toxicity_found = True break else: for item in response["ResultList"]: for label in item["Labels"]: if ( label["Name"] in toxicity_labels and label["Score"] >= threshold ): toxicity_found = True break if self.callback and self.callback.toxicity_callback: if toxicity_found: self.moderation_beacon["moderation_status"] = "LABELS_FOUND" asyncio.create_task( self.callback.on_after_toxicity( self.moderation_beacon, self.unique_id ) ) if toxicity_found: raise ModerationToxicityError return prompt_value
[]
2024-01-10
blairmain/langchain
libs~langchain~langchain~memory~readonly.py
from typing import Any, Dict, List from langchain.schema import BaseMemory class ReadOnlySharedMemory(BaseMemory): """A memory wrapper that is read-only and cannot be changed.""" memory: BaseMemory @property def memory_variables(self) -> List[str]: """Return memory variables.""" return self.memory.memory_variables def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]: """Load memory variables from memory.""" return self.memory.load_memory_variables(inputs) def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None: """Nothing should be saved or changed""" pass def clear(self) -> None: """Nothing to clear, got a memory like a vault.""" pass
[]
2024-01-10
hwchase17/visual-chatgpt
visual_chatgpt.py
import sys import os sys.path.append(os.path.dirname(os.path.realpath(__file__))) sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer, CLIPSegProcessor, CLIPSegForImageSegmentation import torch from diffusers import StableDiffusionPipeline from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler import os from langchain.agents.initialize import initialize_agent from langchain.agents.tools import Tool from langchain.chains.conversation.memory import ConversationBufferMemory from langchain.llms.openai import OpenAI import re import uuid from diffusers import StableDiffusionInpaintPipeline from PIL import Image import numpy as np from omegaconf import OmegaConf from transformers import pipeline, BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnswering import cv2 import einops from pytorch_lightning import seed_everything import random from ldm.util import instantiate_from_config from ControlNet.cldm.model import create_model, load_state_dict from ControlNet.cldm.ddim_hacked import DDIMSampler from ControlNet.annotator.canny import CannyDetector from ControlNet.annotator.mlsd import MLSDdetector from ControlNet.annotator.util import HWC3, resize_image from ControlNet.annotator.hed import HEDdetector, nms from ControlNet.annotator.openpose import OpenposeDetector from ControlNet.annotator.uniformer import UniformerDetector from ControlNet.annotator.midas import MidasDetector VISUAL_CHATGPT_PREFIX = """Visual ChatGPT is designed to be able to assist with a wide range of text and visual related tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. Visual ChatGPT is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. Visual ChatGPT is able to process and understand large amounts of text and images. As a language model, Visual ChatGPT can not directly read images, but it has a list of tools to finish different visual tasks. Each image will have a file name formed as "image/xxx.png", and Visual ChatGPT can invoke different tools to indirectly understand pictures. When talking about images, Visual ChatGPT is very strict to the file name and will never fabricate nonexistent files. When using tools to generate new image files, Visual ChatGPT is also known that the image may not be the same as the user's demand, and will use other visual question answering tools or description tools to observe the real image. Visual ChatGPT is able to use tools in a sequence, and is loyal to the tool observation outputs rather than faking the image content and image file name. It will remember to provide the file name from the last tool observation, if a new image is generated. Human may provide new figures to Visual ChatGPT with a description. The description helps Visual ChatGPT to understand this image, but Visual ChatGPT should use tools to finish following tasks, rather than directly imagine from the description. Overall, Visual ChatGPT is a powerful visual dialogue assistant tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. TOOLS: ------ Visual ChatGPT has access to the following tools:""" VISUAL_CHATGPT_FORMAT_INSTRUCTIONS = """To use a tool, please use the following format: ``` Thought: Do I need to use a tool? Yes Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ``` When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format: ``` Thought: Do I need to use a tool? No {ai_prefix}: [your response here] ``` """ VISUAL_CHATGPT_SUFFIX = """You are very strict to the filename correctness and will never fake a file name if it does not exist. You will remember to provide the image file name loyally if it's provided in the last tool observation. Begin! Previous conversation history: {chat_history} New input: {input} Since Visual ChatGPT is a text language model, Visual ChatGPT must use tools to observe images rather than imagination. The thoughts and observations are only visible for Visual ChatGPT, Visual ChatGPT should remember to repeat important information in the final response for Human. Thought: Do I need to use a tool? {agent_scratchpad}""" def cut_dialogue_history(history_memory, keep_last_n_words=500): tokens = history_memory.split() n_tokens = len(tokens) print(f"hitory_memory:{history_memory}, n_tokens: {n_tokens}") if n_tokens < keep_last_n_words: return history_memory else: paragraphs = history_memory.split('\n') last_n_tokens = n_tokens while last_n_tokens >= keep_last_n_words: last_n_tokens = last_n_tokens - len(paragraphs[0].split(' ')) paragraphs = paragraphs[1:] return '\n' + '\n'.join(paragraphs) def get_new_image_name(org_img_name, func_name="update"): head_tail = os.path.split(org_img_name) head = head_tail[0] tail = head_tail[1] name_split = tail.split('.')[0].split('_') this_new_uuid = str(uuid.uuid4())[0:4] if len(name_split) == 1: most_org_file_name = name_split[0] recent_prev_file_name = name_split[0] new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name) else: assert len(name_split) == 4 most_org_file_name = name_split[3] recent_prev_file_name = name_split[0] new_file_name = '{}_{}_{}_{}.png'.format(this_new_uuid, func_name, recent_prev_file_name, most_org_file_name) return os.path.join(head, new_file_name) def create_model(config_path, device): config = OmegaConf.load(config_path) OmegaConf.update(config, "model.params.cond_stage_config.params.device", device) model = instantiate_from_config(config.model).cpu() print(f'Loaded model config from [{config_path}]') return model class MaskFormer: def __init__(self, device): self.device = device self.processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined") self.model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").to(device) def inference(self, image_path, text): threshold = 0.5 min_area = 0.02 padding = 20 original_image = Image.open(image_path) image = original_image.resize((512, 512)) inputs = self.processor(text=text, images=image, padding="max_length", return_tensors="pt",).to(self.device) with torch.no_grad(): outputs = self.model(**inputs) mask = torch.sigmoid(outputs[0]).squeeze().cpu().numpy() > threshold area_ratio = len(np.argwhere(mask)) / (mask.shape[0] * mask.shape[1]) if area_ratio < min_area: return None true_indices = np.argwhere(mask) mask_array = np.zeros_like(mask, dtype=bool) for idx in true_indices: padded_slice = tuple(slice(max(0, i - padding), i + padding + 1) for i in idx) mask_array[padded_slice] = True visual_mask = (mask_array * 255).astype(np.uint8) image_mask = Image.fromarray(visual_mask) return image_mask.resize(image.size) class ImageEditing: def __init__(self, device): print("Initializing StableDiffusionInpaint to %s" % device) self.device = device self.mask_former = MaskFormer(device=self.device) self.inpainting = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting",).to(device) def remove_part_of_image(self, input): image_path, to_be_removed_txt = input.split(",") print(f'remove_part_of_image: to_be_removed {to_be_removed_txt}') return self.replace_part_of_image(f"{image_path},{to_be_removed_txt},background") def replace_part_of_image(self, input): image_path, to_be_replaced_txt, replace_with_txt = input.split(",") print(f'replace_part_of_image: replace_with_txt {replace_with_txt}') original_image = Image.open(image_path) mask_image = self.mask_former.inference(image_path, to_be_replaced_txt) updated_image = self.inpainting(prompt=replace_with_txt, image=original_image, mask_image=mask_image).images[0] updated_image_path = get_new_image_name(image_path, func_name="replace-something") updated_image.save(updated_image_path) return updated_image_path class Pix2Pix: def __init__(self, device): print("Initializing Pix2Pix to %s" % device) self.device = device self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None).to(device) self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config) def inference(self, inputs): """Change style of image.""" print("===>Starting Pix2Pix Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) original_image = Image.open(image_path) image = self.pipe(instruct_text,image=original_image,num_inference_steps=40,image_guidance_scale=1.2,).images[0] updated_image_path = get_new_image_name(image_path, func_name="pix2pix") image.save(updated_image_path) return updated_image_path class T2I: def __init__(self, device): print("Initializing T2I to %s" % device) self.device = device self.pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16) self.text_refine_tokenizer = AutoTokenizer.from_pretrained("Gustavosta/MagicPrompt-Stable-Diffusion") self.text_refine_model = AutoModelForCausalLM.from_pretrained("Gustavosta/MagicPrompt-Stable-Diffusion") self.text_refine_gpt2_pipe = pipeline("text-generation", model=self.text_refine_model, tokenizer=self.text_refine_tokenizer, device=self.device) self.pipe.to(device) def inference(self, text): image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png") refined_text = self.text_refine_gpt2_pipe(text)[0]["generated_text"] print(f'{text} refined to {refined_text}') image = self.pipe(refined_text).images[0] image.save(image_filename) print(f"Processed T2I.run, text: {text}, image_filename: {image_filename}") return image_filename class ImageCaptioning: def __init__(self, device): print("Initializing ImageCaptioning to %s" % device) self.device = device self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(self.device) def inference(self, image_path): inputs = self.processor(Image.open(image_path), return_tensors="pt").to(self.device) out = self.model.generate(**inputs) captions = self.processor.decode(out[0], skip_special_tokens=True) return captions class image2canny: def __init__(self): print("Direct detect canny.") self.detector = CannyDetector() self.low_thresh = 100 self.high_thresh = 200 def inference(self, inputs): print("===>Starting image2canny Inference") image = Image.open(inputs) image = np.array(image) canny = self.detector(image, self.low_thresh, self.high_thresh) canny = 255 - canny image = Image.fromarray(canny) updated_image_path = get_new_image_name(inputs, func_name="edge") image.save(updated_image_path) return updated_image_path class canny2image: def __init__(self, device): print("Initialize the canny2image model.") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_canny.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting canny2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) image = 255 - image prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01 samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="canny2image") real_image = Image.fromarray(x_samples[0]) # get default the index0 image real_image.save(updated_image_path) return updated_image_path class image2line: def __init__(self): print("Direct detect straight line...") self.detector = MLSDdetector() self.value_thresh = 0.1 self.dis_thresh = 0.1 self.resolution = 512 def inference(self, inputs): print("===>Starting image2hough Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) hough = self.detector(resize_image(image, self.resolution), self.value_thresh, self.dis_thresh) updated_image_path = get_new_image_name(inputs, func_name="line-of") hough = 255 - cv2.dilate(hough, np.ones(shape=(3, 3), dtype=np.uint8), iterations=1) image = Image.fromarray(hough) image.save(updated_image_path) return updated_image_path class line2image: def __init__(self, device): print("Initialize the line2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_mlsd.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting line2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) image = 255 - image prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01 samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).\ cpu().numpy().clip(0,255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="line2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2hed: def __init__(self): print("Direct detect soft HED boundary...") self.detector = HEDdetector() self.resolution = 512 def inference(self, inputs): print("===>Starting image2hed Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) hed = self.detector(resize_image(image, self.resolution)) updated_image_path = get_new_image_name(inputs, func_name="hed-boundary") image = Image.fromarray(hed) image.save(updated_image_path) return updated_image_path class hed2image: def __init__(self, device): print("Initialize the hed2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_hed.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting hed2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="hed2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2scribble: def __init__(self): print("Direct detect scribble.") self.detector = HEDdetector() self.resolution = 512 def inference(self, inputs): print("===>Starting image2scribble Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) detected_map = self.detector(resize_image(image, self.resolution)) detected_map = HWC3(detected_map) image = resize_image(image, self.resolution) H, W, C = image.shape detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR) detected_map = nms(detected_map, 127, 3.0) detected_map = cv2.GaussianBlur(detected_map, (0, 0), 3.0) detected_map[detected_map > 4] = 255 detected_map[detected_map < 255] = 0 detected_map = 255 - detected_map updated_image_path = get_new_image_name(inputs, func_name="scribble") image = Image.fromarray(detected_map) image.save(updated_image_path) return updated_image_path class scribble2image: def __init__(self, device): print("Initialize the scribble2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_scribble.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting scribble2image Inference") print(f'sketch device {self.device}') image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text image = 255 - image img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="scribble2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2pose: def __init__(self): print("Direct human pose.") self.detector = OpenposeDetector() self.resolution = 512 def inference(self, inputs): print("===>Starting image2pose Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) detected_map, _ = self.detector(resize_image(image, self.resolution)) detected_map = HWC3(detected_map) image = resize_image(image, self.resolution) H, W, C = image.shape detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR) updated_image_path = get_new_image_name(inputs, func_name="human-pose") image = Image.fromarray(detected_map) image.save(updated_image_path) return updated_image_path class pose2image: def __init__(self, device): print("Initialize the pose2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_openpose.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting pose2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="pose2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2seg: def __init__(self): print("Direct segmentations.") self.detector = UniformerDetector() self.resolution = 512 def inference(self, inputs): print("===>Starting image2seg Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) detected_map = self.detector(resize_image(image, self.resolution)) detected_map = HWC3(detected_map) image = resize_image(image, self.resolution) H, W, C = image.shape detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR) updated_image_path = get_new_image_name(inputs, func_name="segmentation") image = Image.fromarray(detected_map) image.save(updated_image_path) return updated_image_path class seg2image: def __init__(self, device): print("Initialize the seg2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_seg.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting seg2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="segment2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2depth: def __init__(self): print("Direct depth estimation.") self.detector = MidasDetector() self.resolution = 512 def inference(self, inputs): print("===>Starting image2depth Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) detected_map, _ = self.detector(resize_image(image, self.resolution)) detected_map = HWC3(detected_map) image = resize_image(image, self.resolution) H, W, C = image.shape detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR) updated_image_path = get_new_image_name(inputs, func_name="depth") image = Image.fromarray(detected_map) image.save(updated_image_path) return updated_image_path class depth2image: def __init__(self, device): print("Initialize depth2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_depth.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting depth2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text img = resize_image(HWC3(image), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01 samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="depth2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class image2normal: def __init__(self): print("Direct normal estimation.") self.detector = MidasDetector() self.resolution = 512 self.bg_threshold = 0.4 def inference(self, inputs): print("===>Starting image2 normal Inference") image = Image.open(inputs) image = np.array(image) image = HWC3(image) _, detected_map = self.detector(resize_image(image, self.resolution), bg_th=self.bg_threshold) detected_map = HWC3(detected_map) image = resize_image(image, self.resolution) H, W, C = image.shape detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR) updated_image_path = get_new_image_name(inputs, func_name="normal-map") image = Image.fromarray(detected_map) image.save(updated_image_path) return updated_image_path class normal2image: def __init__(self, device): print("Initialize normal2image model...") model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device) model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_normal.pth', location='cpu')) self.model = model.to(device) self.device = device self.ddim_sampler = DDIMSampler(self.model) self.ddim_steps = 20 self.image_resolution = 512 self.num_samples = 1 self.save_memory = False self.strength = 1.0 self.guess_mode = False self.scale = 9.0 self.seed = -1 self.a_prompt = 'best quality, extremely detailed' self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality' def inference(self, inputs): print("===>Starting normal2image Inference") image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:]) image = Image.open(image_path) image = np.array(image) prompt = instruct_text img = image[:, :, ::-1].copy() img = resize_image(HWC3(img), self.image_resolution) H, W, C = img.shape img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0 control = torch.stack([control for _ in range(self.num_samples)], dim=0) control = einops.rearrange(control, 'b h w c -> b c h w').clone() self.seed = random.randint(0, 65535) seed_everything(self.seed) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]} un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]} shape = (4, H // 8, W // 8) self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond) if self.save_memory: self.model.low_vram_shift(is_diffusing=False) x_samples = self.model.decode_first_stage(samples) x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8) updated_image_path = get_new_image_name(image_path, func_name="normal2image") real_image = Image.fromarray(x_samples[0]) # default the index0 image real_image.save(updated_image_path) return updated_image_path class BLIPVQA: def __init__(self, device): print("Initializing BLIP VQA to %s" % device) self.device = device self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base") self.model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(self.device) def get_answer_from_question_and_image(self, inputs): image_path, question = inputs.split(",") raw_image = Image.open(image_path).convert('RGB') print(F'BLIPVQA :question :{question}') inputs = self.processor(raw_image, question, return_tensors="pt").to(self.device) out = self.model.generate(**inputs) answer = self.processor.decode(out[0], skip_special_tokens=True) return answer class ConversationBot: def __init__(self): print("Initializing VisualChatGPT") self.llm = OpenAI(temperature=0) self.edit = ImageEditing(device="cuda:6") self.i2t = ImageCaptioning(device="cuda:4") self.t2i = T2I(device="cuda:1") self.image2canny = image2canny() self.canny2image = canny2image(device="cuda:1") self.image2line = image2line() self.line2image = line2image(device="cuda:1") self.image2hed = image2hed() self.hed2image = hed2image(device="cuda:2") self.image2scribble = image2scribble() self.scribble2image = scribble2image(device="cuda:3") self.image2pose = image2pose() self.pose2image = pose2image(device="cuda:3") self.BLIPVQA = BLIPVQA(device="cuda:4") self.image2seg = image2seg() self.seg2image = seg2image(device="cuda:7") self.image2depth = image2depth() self.depth2image = depth2image(device="cuda:7") self.image2normal = image2normal() self.normal2image = normal2image(device="cuda:5") self.pix2pix = Pix2Pix(device="cuda:3") self.memory = ConversationBufferMemory(memory_key="chat_history", output_key='output') self.tools = [ Tool(name="Get Photo Description", func=self.i2t.inference, description="useful when you want to know what is inside the photo. receives image_path as input. " "The input to this tool should be a string, representing the image_path. "), Tool(name="Generate Image From User Input Text", func=self.t2i.inference, description="useful when you want to generate an image from a user input text and save it to a file. like: generate an image of an object or something, or generate an image that includes some objects. " "The input to this tool should be a string, representing the text used to generate image. "), Tool(name="Remove Something From The Photo", func=self.edit.remove_part_of_image, description="useful when you want to remove and object or something from the photo from its description or location. " "The input to this tool should be a comma seperated string of two, representing the image_path and the object need to be removed. "), Tool(name="Replace Something From The Photo", func=self.edit.replace_part_of_image, description="useful when you want to replace an object from the object description or location with another object from its description. " "The input to this tool should be a comma seperated string of three, representing the image_path, the object to be replaced, the object to be replaced with "), Tool(name="Instruct Image Using Text", func=self.pix2pix.inference, description="useful when you want to the style of the image to be like the text. like: make it look like a painting. or make it like a robot. " "The input to this tool should be a comma seperated string of two, representing the image_path and the text. "), Tool(name="Answer Question About The Image", func=self.BLIPVQA.get_answer_from_question_and_image, description="useful when you need an answer for a question based on an image. like: what is the background color of the last image, how many cats in this figure, what is in this figure. " "The input to this tool should be a comma seperated string of two, representing the image_path and the question"), Tool(name="Edge Detection On Image", func=self.image2canny.inference, description="useful when you want to detect the edge of the image. like: detect the edges of this image, or canny detection on image, or peform edge detection on this image, or detect the canny image of this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Canny Image", func=self.canny2image.inference, description="useful when you want to generate a new real image from both the user desciption and a canny image. like: generate a real image of a object or something from this canny image, or generate a new real image of a object or something from this edge image. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description. "), Tool(name="Line Detection On Image", func=self.image2line.inference, description="useful when you want to detect the straight line of the image. like: detect the straight lines of this image, or straight line detection on image, or peform straight line detection on this image, or detect the straight line image of this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Line Image", func=self.line2image.inference, description="useful when you want to generate a new real image from both the user desciption and a straight line image. like: generate a real image of a object or something from this straight line image, or generate a new real image of a object or something from this straight lines. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description. "), Tool(name="Hed Detection On Image", func=self.image2hed.inference, description="useful when you want to detect the soft hed boundary of the image. like: detect the soft hed boundary of this image, or hed boundary detection on image, or peform hed boundary detection on this image, or detect soft hed boundary image of this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Soft Hed Boundary Image", func=self.hed2image.inference, description="useful when you want to generate a new real image from both the user desciption and a soft hed boundary image. like: generate a real image of a object or something from this soft hed boundary image, or generate a new real image of a object or something from this hed boundary. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description"), Tool(name="Segmentation On Image", func=self.image2seg.inference, description="useful when you want to detect segmentations of the image. like: segment this image, or generate segmentations on this image, or peform segmentation on this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Segmentations", func=self.seg2image.inference, description="useful when you want to generate a new real image from both the user desciption and segmentations. like: generate a real image of a object or something from this segmentation image, or generate a new real image of a object or something from these segmentations. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description"), Tool(name="Predict Depth On Image", func=self.image2depth.inference, description="useful when you want to detect depth of the image. like: generate the depth from this image, or detect the depth map on this image, or predict the depth for this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Depth", func=self.depth2image.inference, description="useful when you want to generate a new real image from both the user desciption and depth image. like: generate a real image of a object or something from this depth image, or generate a new real image of a object or something from the depth map. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description"), Tool(name="Predict Normal Map On Image", func=self.image2normal.inference, description="useful when you want to detect norm map of the image. like: generate normal map from this image, or predict normal map of this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Normal Map", func=self.normal2image.inference, description="useful when you want to generate a new real image from both the user desciption and normal map. like: generate a real image of a object or something from this normal map, or generate a new real image of a object or something from the normal map. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description"), Tool(name="Sketch Detection On Image", func=self.image2scribble.inference, description="useful when you want to generate a scribble of the image. like: generate a scribble of this image, or generate a sketch from this image, detect the sketch from this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Sketch Image", func=self.scribble2image.inference, description="useful when you want to generate a new real image from both the user desciption and a scribble image or a sketch image. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description"), Tool(name="Pose Detection On Image", func=self.image2pose.inference, description="useful when you want to detect the human pose of the image. like: generate human poses of this image, or generate a pose image from this image. " "The input to this tool should be a string, representing the image_path"), Tool(name="Generate Image Condition On Pose Image", func=self.pose2image.inference, description="useful when you want to generate a new real image from both the user desciption and a human pose image. like: generate a real image of a human from this human pose image, or generate a new real image of a human from this pose. " "The input to this tool should be a comma seperated string of two, representing the image_path and the user description")] self.agent = initialize_agent( self.tools, self.llm, agent="conversational-react-description", verbose=True, memory=self.memory, return_intermediate_steps=True, agent_kwargs={'prefix': VISUAL_CHATGPT_PREFIX, 'format_instructions': VISUAL_CHATGPT_FORMAT_INSTRUCTIONS, 'suffix': VISUAL_CHATGPT_SUFFIX}, ) def run_text(self, text, state): print("===============Running run_text =============") print("Inputs:", text, state) print("======>Previous memory:\n %s" % self.agent.memory) self.agent.memory.buffer = cut_dialogue_history(self.agent.memory.buffer, keep_last_n_words=500) res = self.agent({"input": text}) print("======>Current memory:\n %s" % self.agent.memory) response = re.sub('(image/\S*png)', lambda m: f'![](/file={m.group(0)})*{m.group(0)}*', res['output']) state = state + [(text, response)] print("Outputs:", state) return state, state def run_image(self, image, state, txt): print("===============Running run_image =============") print("Inputs:", image, state) print("======>Previous memory:\n %s" % self.agent.memory) image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png") print("======>Auto Resize Image...") img = Image.open(image.name) width, height = img.size ratio = min(512 / width, 512 / height) width_new, height_new = (round(width * ratio), round(height * ratio)) img = img.resize((width_new, height_new)) img = img.convert('RGB') img.save(image_filename, "PNG") print(f"Resize image form {width}x{height} to {width_new}x{height_new}") description = self.i2t.inference(image_filename) Human_prompt = "\nHuman: provide a figure named {}. The description is: {}. This information helps you to understand this image, but you should use tools to finish following tasks, " \ "rather than directly imagine from my description. If you understand, say \"Received\". \n".format(image_filename, description) AI_prompt = "Received. " self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + 'AI: ' + AI_prompt print("======>Current memory:\n %s" % self.agent.memory) state = state + [(f"![](/file={image_filename})*{image_filename}*", AI_prompt)] print("Outputs:", state) return state, state, txt + ' ' + image_filename + ' ' if __name__ == '__main__': bot = ConversationBot() with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo: chatbot = gr.Chatbot(elem_id="chatbot", label="Visual ChatGPT") state = gr.State([]) with gr.Row(): with gr.Column(scale=0.7): txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False) with gr.Column(scale=0.15, min_width=0): clear = gr.Button("Clear๏ธ") with gr.Column(scale=0.15, min_width=0): btn = gr.UploadButton("Upload", file_types=["image"]) txt.submit(bot.run_text, [txt, state], [chatbot, state]) txt.submit(lambda: "", None, txt) btn.upload(bot.run_image, [btn, state, txt], [chatbot, state, txt]) clear.click(bot.memory.clear) clear.click(lambda: [], None, chatbot) clear.click(lambda: [], None, state) demo.launch(server_name="0.0.0.0", server_port=7860)
[ "\nHuman: provide a figure named PLACEHOLDER. The description is: PLACEHOLDER. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n", "Received. " ]
2024-01-10
ml-research/MoRT_NMI
realtoxicityprompts~generation~generation.py
# TODO: add `text` key to cached generations # TODO: consolidate code for loading cache import json import logging import math from functools import partial from pathlib import Path from typing import Iterable, List import sys import openai import pandas as pd import torch import torch.multiprocessing as mp import torch.nn.functional as F from tqdm.auto import tqdm from transformers.pipelines import pipeline import os from generation.gpt2_generation import GPT2Generation from generation.gpt2MCM_generation import GPT2Generation as GPT2MCMGeneration from generation.pplm_generation import PPLMGeneration from models.affect_lm import AffectGPT2LMHeadModel from utils.constants import OPENAI_API_KEY from utils.utils import batchify, load_cache logging.disable(logging.CRITICAL) # Disable logging from transformers def pplm(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, class_label: int, num_iterations: int, model_name_or_path: str, out_file: Path): # Set up PPLM with multiprocessing generator = PPLMGeneration(model_name_or_path, device=0) ctx = mp.get_context('spawn') generator.model.share_memory() generator.classifier.share_memory() pplm_func = partial(generator.__call__, class_label=class_label, num_iterations=num_iterations, length=max_len) # Repeat prompts prompts = prompts.repeat(num_samples) # Resume generation num_cached_generations = 0 for generation in load_cache(out_file): yield generation num_cached_generations += 1 # Generate with prompts prompts = prompts[num_cached_generations:] with ctx.Pool(processes=batch_size) as pool: for batch in tqdm(pool.imap(pplm_func, prompts), total=len(prompts), desc='Generation', dynamic_ncols=True): for generation in batch: with out_file.open('a') as f: print(json.dumps(generation), file=f) yield generation def _pipeline_helper(prompts: pd.Series, model_name_or_path: str, max_len: int, num_samples: int, out_file: Path, **generate_kwargs): # Load cached generations num_cached_generations = 0 for generation in load_cache(out_file): yield generation num_cached_generations += 1 assert num_cached_generations % num_samples == 0 # Remove prompts that have already been generated with prompts = prompts[num_cached_generations // num_samples:] if prompts.empty: return # Setup model generator = pipeline('text-generation', model=model_name_or_path, device=0) print("Created pipeline with model:", generator.model.__class__.__name__) # Generate with prompts for prompt in tqdm(prompts, desc='Generation', dynamic_ncols=True): # Generate # FIXME: this is a hack ctx_len = len(generator.tokenizer.tokenize(prompt)) try: batch = generator(prompt, num_return_sequences=num_samples, clean_up_tokenization_spaces=True, do_sample=True, top_k=0, top_p=0.9, max_length=ctx_len + max_len, return_prompt=False, **generate_kwargs) batch = map(lambda g: g['generated_text'][len(prompt):], batch) except RuntimeError as e: print("Error during generation with prompt:", prompt) print(e) print("Emptying CUDA cache and continuing...") torch.cuda.empty_cache() batch = ["GENERATION_ERROR_CUDA"] * num_samples for generation in batch: with out_file.open('a') as f: print(json.dumps(generation), file=f) yield generation def openai_gpt(prompts: pd.Series, max_len: int, num_samples: int, model_name_or_path: str, out_file: Path, **generate_kwargs): yield from _pipeline_helper(prompts=prompts, model_name_or_path=model_name_or_path, max_len=max_len, num_samples=num_samples, out_file=out_file, **generate_kwargs) def ctrl(prompts: pd.Series, max_len: int, num_samples: int, ctrl_code: str, model_name_or_path: str, out_file: Path, **generate_kwargs) -> Iterable[str]: # Prepend CTRL code to prompts prompts = ctrl_code + " " + prompts print(prompts) yield from _pipeline_helper(prompts=prompts, model_name_or_path=model_name_or_path, max_len=max_len, num_samples=num_samples, out_file=out_file, **generate_kwargs) def _gpt2_helper(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, generator: GPT2Generation, out_file: Path, **generate_kwargs): # Repeat prompts prompts = prompts.repeat(num_samples) # Resume generation num_cached_generations = 0 for generation in load_cache(out_file): yield generation num_cached_generations += 1 # Generate with prompts prompts = prompts[num_cached_generations:] #rtpt = RTPT('PS', 'MCM_GPT2', math.ceil(len(prompts) / batch_size)) for prompt in tqdm(batchify(prompts, batch_size), total=math.ceil(len(prompts) / batch_size), desc=f'GPT-2 Generation', dynamic_ncols=True, postfix={'batch_size': batch_size}): #rtpt.epoch_starts() # Generate try: batch = generator.generate(prompt, max_len, **generate_kwargs) except RuntimeError as e: print("Error during generation with prompt:", prompt) print(e) print("Emptying CUDA cache and retrying...") torch.cuda.empty_cache() batch = ["GENERATION_ERROR_CUDA"] * len(prompt) for generation in batch: with out_file.open('a') as f: print(json.dumps(generation), file=f) yield generation #rtpt.epoch_ends() def gpt2_ctrl(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, prompt_ctrl_code: str, model_name_or_path: str, out_file: Path): # Use default gpt2 architecture generator = GPT2Generation(model_name_or_path) # Add some special tokens (inline metadata) with open(Path(model_name_or_path) / 'added_tokens.json') as f: ctrl_codes = list(json.load(f).keys()) assert prompt_ctrl_code in ctrl_codes print('Added tokens:', ctrl_codes) num_tokens_added = generator.tokenizer.add_tokens(ctrl_codes) assert num_tokens_added == len(ctrl_codes) print("Tokenizer vocab size:", generator.tokenizer.vocab_size) # Prepend ctrl code to prompts prompts = prompt_ctrl_code + prompts print(prompts) yield from _gpt2_helper(prompts=prompts, max_len=max_len, num_samples=num_samples, batch_size=batch_size, generator=generator, out_file=out_file) def gpt2_affect(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, target_class: int, num_classes: int, beta: int, model_name_or_path: str, out_file: Path) -> Iterable[str]: # Setup AffectGPT2 for generation model = AffectGPT2LMHeadModel.from_pretrained(model_name_or_path) generator = GPT2Generation(model) affect_label = F.one_hot(torch.LongTensor([target_class]), num_classes=num_classes).float().to(generator.device) model.set_affect_labels(affect_label) model.affect.beta = beta model.affect.ignore_special_tokens = True yield from _gpt2_helper(prompts=prompts, max_len=max_len, num_samples=num_samples, batch_size=batch_size, generator=generator, out_file=out_file) def gpt2(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, model_name_or_path: str, out_file: Path, **generate_kwargs) -> Iterable[str]: # Setup model generator = GPT2Generation(model_name_or_path) yield from _gpt2_helper(prompts=prompts, max_len=max_len, num_samples=num_samples, batch_size=batch_size, generator=generator, out_file=out_file, **generate_kwargs) def gpt2mcm(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, model_name_or_path: str, out_file: Path, threshold: float = 0.0, use_mcm: bool = False, k: int = 0, min_token_number: int = 30, **generate_kwargs) -> Iterable[str]: # Setup model generator = GPT2MCMGeneration(model_name_or_path, threshold=threshold, k=k, p=0.9, min_token_number=min_token_number, use_mcm=use_mcm) yield from _gpt2_helper(prompts=prompts, max_len=max_len, num_samples=num_samples, batch_size=batch_size, generator=generator, out_file=out_file, **generate_kwargs) def gpt3(prompts: pd.Series, max_len: int, num_samples: int, batch_size: int, model_name_or_path: str, out_file: Path) -> Iterable[str]: openai.api_key = OPENAI_API_KEY def request(prompts: List[str]): # Retry request (handles connection errors, timeouts, and overloaded API) while True: try: return openai.Completion.create( engine=model_name_or_path, prompt=prompts, max_tokens=max_len, n=1 ) except Exception as e: tqdm.write(str(e)) tqdm.write("Retrying...") prompts = prompts.repeat(num_samples) for batch in tqdm(batchify(prompts, batch_size)): response = request(batch) yield from [choice['text'] for choice in response['choices']]
[ "PLACEHOLDER PLACEHOLDER", "PLACEHOLDERPLACEHOLDER" ]
2024-01-10
demondehellis/streamlit-chat
streamlit_app.py
import os import openai import streamlit as st def chat_input(): st.session_state.messages.append({ "role": "user", "content": st.session_state.user_input }) response = openai.ChatCompletion.create( model="gpt-4", max_tokens=os.environ.get("OPENAI_MAX_TOKENS") or 1000, messages=st.session_state.messages, ) st.session_state.messages.append(response["choices"][0]["message"]) # render history if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: st.chat_message(message["role"]).write(message["content"]) with st.form(key="chat_input"): text_input = st.text_input("Message", key="user_input") submit_button = st.form_submit_button(label="Submit", on_click=chat_input)
[]
2024-01-10
NEkropo1/py-nifty-bridge-helper
helpers~tokens_checker.py
import openai def check_tokens_size(query: str) -> None: max_tokens = 4096 tokens_used = openai.Completion.create( model="gpt-3.5-turbo", prompt=query, max_tokens=max_tokens, temperature=0, ).usage["total_tokens"] if tokens_used > max_tokens: message = ( f"Error: The number of tokens used ({tokens_used}) exceeds the maximum limit " f"of {max_tokens}. Please reduce the length of your text input." ) raise ValueError(message)
[]
2024-01-10
z8/gptcli
gptcli.py
#!/usr/bin/env python3 import os import json import argparse import openai from typing import List from rich.console import Console from rich.markdown import Markdown, MarkdownIt from rich.live import Live try: import rlcompleter import readline except ImportError: pass class Config: sep = Markdown("---") baseDir = os.path.dirname(os.path.realpath(__file__)) default = os.path.join(baseDir, "config.json") def __init__(self) -> None: self.cfg = {} self.history = [] def load(self, file): with open(file, "r") as f: self.cfg = json.load(f) def load_session(self, file): with open(file, "r") as f: self.history = json.load(f) print("Load {} records from {}".format(len(self.history), file)) def save_session(self, file): print("Save {} records to {}".format(len(self.history), file)) with open(file, "w") as f: json.dump(self.history, f, indent=2) @property def key(self): return self.cfg.get("key", os.environ.get("OPENAI_API_KEY", "")) @property def api_base(self): return self.cfg.get("api_base", os.environ.get("OPENAI_API_BASE", "")) @property def model(self): return self.cfg.get("model", "gpt-3.5-turbo") @property def prompt(self): return self.cfg.get("prompt", []) @property def stream(self): return self.cfg.get("stream", False) @property def response(self): return self.cfg.get("response", False) @property def proxy(self): return self.cfg.get("proxy", "") c = Console() kConfig = Config() def query_openai(data: dict): messages = [] messages.extend(kConfig.prompt) messages.extend(data) try: response = openai.ChatCompletion.create( model=kConfig.model, messages=messages ) content = response["choices"][0]["message"]["content"] c.print(Markdown(content), Config.sep) return content except openai.error.OpenAIError as e: c.print(e) except Exception as e: c.print(e) return "" def query_openai_stream(data: dict): messages = [] messages.extend(kConfig.prompt) messages.extend(data) md = Markdown("") parser = MarkdownIt().enable("strikethrough") answer = "" try: response = openai.ChatCompletion.create( model=kConfig.model, messages=messages, stream=True) with Live(md, auto_refresh=False) as lv: for part in response: finish_reason = part["choices"][0]["finish_reason"] if "content" in part["choices"][0]["delta"]: content = part["choices"][0]["delta"]["content"] answer += content md.markup = answer md.parsed = parser.parse(md.markup) lv.refresh() elif finish_reason: pass except KeyboardInterrupt: c.print("Canceled") except openai.error.OpenAIError as e: c.print(e) answer = "" except Exception as e: c.print(e) c.print(Config.sep) return answer class ChatConsole: def __init__(self) -> None: parser = argparse.ArgumentParser("Input", add_help=False) parser.add_argument('-help', action='help', default=argparse.SUPPRESS, help="show this help message") parser.add_argument("-reset", action='store_true', help="reset session, i.e. clear chat history") parser.add_argument("-save", metavar="FILE", type=str, help="save current conversation to file") parser.add_argument("-load", metavar="FILE", type=str, help="load conversation from file") parser.add_argument("-exit", action='store_true', help="exit console") parser.add_argument("-multiline", action='store_true', help="input multiple lines, end with ctrl-d(Linux/macOS) or ctrl-z(Windows). cancel with ctrl-c") self.parser = parser try: self.init_readline([opt for action in parser._actions for opt in action.option_strings]) except Exception as e: c.print("Failed to setup readline, autocomplete may not work:", e) def init_readline(self, options: List[str]): def completer(text, state): matches = [o for o in options if o.startswith(text)] if state < len(matches): return matches[state] else: return None readline.set_completer(completer) readline.set_completer_delims(readline.get_completer_delims().replace('-', '')) readline.parse_and_bind('tab:complete') def parse_input(self) -> str: # content = c.input("[bold yellow]Input:[/] ").strip() with c.capture() as capture: c.print("[bold yellow]Input:[/] ", end="") content = input(capture.get()) if not content.startswith("-"): return content # handle console options locally try: args = self.parser.parse_args(content.split()) except SystemExit: return "" except argparse.ArgumentError as e: print(e) return "" if args.reset: kConfig.history.clear() c.print("Session cleared.") elif args.save: kConfig.save_session(args.save) elif args.load: kConfig.load_session(args.load) elif args.multiline: return self.read_multiline() elif args.exit: raise EOFError else: print("???", args) return "" def read_multiline(self) -> str: contents = [] while True: try: line = input("> ") except EOFError: c.print("--- EOF ---") break except KeyboardInterrupt: return "" contents.append(line) return "\n".join(contents) def main(): parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-c", dest="config", help="path to config.json", default=Config.default) args = parser.parse_args() c.print(f"Loading config from {args.config}") kConfig.load(args.config) if kConfig.key: openai.api_key = kConfig.key if kConfig.api_base: c.print(f"Using api_base: {kConfig.api_base}") openai.api_base = kConfig.api_base if kConfig.proxy: c.print(f"Using proxy: {kConfig.proxy}") openai.proxy = kConfig.proxy c.print(f"Response in prompt: {kConfig.response}") c.print(f"Stream mode: {kConfig.stream}") chat = ChatConsole() while True: try: content = chat.parse_input().strip() if not content: continue hist = kConfig.history # alias hist.append({"role": "user", "content": content}) if kConfig.stream: answer = query_openai_stream(hist) else: answer = query_openai(hist) except KeyboardInterrupt: c.print("Bye!") break except EOFError as e: c.print("Bye!") break if not answer: hist.pop() elif kConfig.response: hist.append({"role": "assistant", "content": answer}) if __name__ == '__main__': main()
[]
2024-01-10
invertase/llm-gcp
cloud-run-langchain~app~history.py
import datetime from langchain.memory.chat_message_histories import ( FirestoreChatMessageHistory as _FirestoreChatMessageHistory, ) from langchain.schema.messages import BaseMessage, messages_from_dict from google.cloud.firestore import Client, CollectionReference from firebase_admin import firestore class FirestoreChatMessageHistory(_FirestoreChatMessageHistory): _collection: CollectionReference = None def prepare_firestore(self) -> None: # Prepare the Firestore client self.firestore_client: Client = firestore.client() # Create a reference to the collection for this user and session self._collection = self.firestore_client.collection( f"{self.collection_name}/{self.user_id}/{self.session_id}" ) # Load the messages from the database, called once when the history is created self.load_messages() def load_messages(self) -> None: count = self._collection.count().get() if len(count) > 0: docs = self._collection.order_by("timestamp", direction="DESCENDING").get() self.messages = messages_from_dict([doc.to_dict() for doc in docs]) def add_message(self, message: BaseMessage) -> None: # Add the message to the in-memory list self.messages.append(message) # Persist the message to the database self.firestore_client.collection( f"{self.collection_name}/{self.user_id}/{self.session_id}" ).add( { "data": message.dict(), "type": message.type, "timestamp": datetime.datetime.now(), } ) def clear(self) -> None: if not self._collection: raise ValueError("Collection not initialized!") batch = self.firestore_client.batch() docs = self._collection.list_documents(page_size=500) # Delete documents in chunks for doc in docs: batch.delete(doc) batch.commit()
[]
2024-01-10
Azure/openai-at-scale
app~backend~approaches~chatreadretrieveread.py
import openai import uuid from approaches.approach import Approach import chat_log.cosmosdb_logging as cosmosdb_logging class ChatReadRetrieveReadApproach(Approach): # def __init__(self, chatgpt_deployment: str, gpt_deployment: str, sourcepage_field: str, content_field: str): def __init__(self, chatgpt_deployment: str, gpt_deployment: str): self.chatgpt_deployment = chatgpt_deployment self.gpt_deployment = gpt_deployment def run(self, history: list[dict], overrides: dict, sessionConfig: dict, userInfo:dict, header: dict) -> any: request_uuid=uuid.uuid4() print("requestID:",request_uuid,",history:", history) print("requestID:",request_uuid,",override:", overrides) print("requestID:",request_uuid,",sessionConfig:", sessionConfig) print("requestID:",request_uuid,",userInfo:", userInfo) top_p = overrides.get("top") or 0.95 temperature = overrides.get("temperature") or 0.7 max_tokens = overrides.get("maxResponse") or 800 promptSystemTemplate = overrides.get("prompt_system_template") or "You are an AI assistant that helps people find information." pastMessages = sessionConfig.get("pastMessages") or 10 user_name= userInfo.get("username") or "anonymous user_name" user_id = userInfo.get("email") or "anonymous user_id" chat_session_id = header.get("Sessionid") or "anonymous-" + str(uuid.uuid4()) print("user:", {"name": user_name,"user_id":user_id} ) # For Azure Log Analytics print("parameters:", {"Max Response": max_tokens, "Temperature": temperature, "Top P": top_p, "Past message included": pastMessages}) # Step system_prompt_template = {} system_prompt_template["role"] = "system" system_prompt_template["content"] = promptSystemTemplate print("prompt:",[system_prompt_template]+self.get_chat_history_as_text(history, pastMessages)) # For Azure Log Analytics completion = openai.ChatCompletion.create( engine=self.chatgpt_deployment, messages = [system_prompt_template]+self.get_chat_history_as_text(history, pastMessages), temperature=temperature, max_tokens=max_tokens, top_p=top_p, frequency_penalty=0, presence_penalty=0, stop=None) print("completion: ", completion) # For Azure Log Analytics document_definition = { "id": str(uuid.uuid4()), "chat_session_id": chat_session_id, "user": {"name": user_name,"user_id":user_id}, 'message': {"id":completion.get("id") or "anonymous-id", "prompt":[system_prompt_template]+self.get_chat_history_as_text(history, pastMessages), "other_attr":[{"completion": completion}], "previous_message_id":"previous_message_id"}} #cosmosdb_logging.insert_chat_log(document_definition) # Store prompt log data into Azure Cosmos DB return {"answer": completion.choices[0].message["content"]} def get_chat_history_as_text(self, history, pastMessages) -> list: history_text = [] for h in history: user_text = {} user_text["role"] = "user" user_text["content"] = h["user"] if h.get("bot") is None: history_text = history_text + [user_text] else: bot_text = {} bot_text["role"] = "assistant" bot_text["content"] = h.get("bot") history_text = history_text + [user_text, bot_text] return history_text[-(pastMessages+1):]
[ "{}", "prompt_system_template", "You are an AI assistant that helps people find information." ]
2024-01-10
dachenlian/streamlit-absa
pages~1_explore.py
from annotated_text import annotated_text from dotenv import load_dotenv from matplotlib.figure import Figure import openai import pandas as pd import streamlit as st from streamlit_extras.switch_page_button import switch_page # # Add the parent directory to the path # BASE = Path(__file__).resolve().parent.parent # if str(BASE) not in sys.path: # sys.path.append(str(BASE)) from scrape.absa.absa import create_absa_heatmap as _create_absa_heatmap from scrape.absa.absa import get_annotated_absa from scrape.types import OpenAIModel, GetDataOutput from scrape.utils import get_data as _get_data from scrape.analysis import create_wordcloud # load_dotenv() if "url" not in st.session_state or not st.session_state["url"]: switch_page("start") if "api_key" not in st.session_state or not st.session_state["api_key"]: switch_page("start") # CLIENT = openai.Client(api_key=os.environ["OPENAI_API_KEY"]) CLIENT = openai.Client(api_key=st.session_state["api_key"]) MAX_LENGTH = 2000 # The maximum number of tokens for the prompt @st.cache_data def get_data(url: str) -> GetDataOutput: return _get_data(CLIENT, url, max_length=MAX_LENGTH) @st.cache_data def create_absa_heatmap(df: pd.DataFrame) -> Figure: return _create_absa_heatmap(df) # @st.cache_data # def create_wordcloud(freq: Counter, width: int = 1280, height: int = 720) -> None: # _create_wordcloud(freq, width, height) st.set_page_config( page_title="Sentiment Explorer", page_icon="๐ŸŽญ", layout="wide", ) # reviews = get_movie_reviews(st.session_state['url']) # url = "/home/richard/lope/dspy/lab14/examples/letterboxd.html" # metadata, reviews = get_movie_reviews(url) ####################### # ๆบ–ๅ‚™่ณ‡ๆ–™ ####################### # ่ผ‰ๅ…ฅ่ณ‡ๆ–™ # if "url" not in st.session_state: # switch_page("start") # if not st.session_state["url"]: # switch_page("start") url = st.session_state["url"] if not url: switch_page("start") data = get_data(url) # # heatmap # heatmap = create_absa_heatmap(data.absa_counts_df) ####################### # Streamlit ไป‹้ข ####################### # ่จญๅฎšๅˆ†้  with st.sidebar: reset = st.button("Reset", use_container_width=True, type="primary") if reset: st.session_state["url"] = "" switch_page("start") summary_tab, table_tab, annotated_tab = st.tabs( ["Summary", "Details", "Annotated ABSA"] ) with summary_tab: st.markdown(f"# {data.title}") st.markdown(data.summary) with table_tab: st.markdown(f"# {data.title} Texts") st.markdown("## Word Cloud") create_wordcloud(data.word_counts) st.pyplot() st.markdown("## ABSA Heatmap") st.pyplot( create_absa_heatmap(data.absa_counts_df), dpi=1000, use_container_width=True ) st.markdown("## Texts") df = data.df if data.source == "movie": assert data.df_filter col1, col2, col3, col4 = st.columns(4) with col1: hide_spoilers = st.toggle("Hide Spoilers", value=False) with col2: hide_positive = st.toggle("Hide Positive", value=False) with col3: hide_negative = st.toggle("Hide Negative", value=False) with col4: hide_neutral = st.toggle("Hide Neutral", value=False) df = data.df_filter( hide_spoilers, hide_negative, hide_positive, hide_neutral, df ) st.dataframe(df, use_container_width=True, height=1000) with annotated_tab: st.markdown("# Annotate a Text") with st.form("annotate_form"): textbox = st.text_area("Review", height=200) submitted = st.form_submit_button("Submit") if submitted: annotated_text( get_annotated_absa( client=CLIENT, text=textbox, aspects=data.aspect_list, max_length=MAX_LENGTH, model_name=OpenAIModel.GPT4, ) )
[]
2024-01-10
dachenlian/streamlit-absa
scrape~absa~absa.py
import asyncio from collections import Counter from pathlib import Path from string import Template import re from loguru import logger from matplotlib.figure import Figure import matplotlib.pyplot as plt from matplotlib.font_manager import fontManager, FontProperties import openai import pandas as pd import seaborn as sns import tiktoken from tqdm.asyncio import tqdm_asyncio from tqdm.auto import tqdm from scrape.absa.types import CountABSAOutput, GetABSAOutput, GetAnnotatedABSAOutput from scrape.absa.prompts import ANNOTATED_ABSA_PROMPT from scrape.types import OpenAIModel from scrape.utils import chunker BASE = Path(__file__).resolve().parent.parent.parent font_path = str(BASE / "data/fonts/SourceHanSerifK-Light.otf") fontManager.addfont(font_path) prop = FontProperties(fname=font_path) sns.set(font=prop.get_name()) enc = tiktoken.get_encoding("cl100k_base") def get_absa( client: openai.Client, texts: list[str], base_prompt: Template, main_body: str = "", max_length: int = 2000, model_name: OpenAIModel = OpenAIModel.GPT3_5, chunk_size: int = 25, ) -> GetABSAOutput: """ Retrieves Aspect-Based Sentiment Analysis (ABSA) for a list of texts using the OpenAI GPT model. Args: client (openai.Client): The OpenAI client used to make API calls. texts (list[str]): The list of texts to perform ABSA on. base_prompt (str): The base prompt used for each text. main_body (str | None, optional): The main body of the prompt. Defaults to None. model_name (OpenAIModel, optional): The name of the OpenAI model to use. Defaults to OpenAIModel.GPT3_5. chunk_size (int, optional): The number of texts to process in each API call. Defaults to 25. Returns: GetABSAOutput: The ABSA output containing the sentiment analysis results for each text. """ from scrape.utils import call_model _texts = [f"Text {i}:\n{t}" for i, t in enumerate(texts, start=1)] texts = [] length = 0 for t in _texts: texts.append(t) length += len(enc.encode(t)) if length > max_length: break responses = {} chunked = chunker(texts, chunk_size) logger.info(f"Chunked into {len(chunked)} chunks") tasks = [] for c in tqdm(chunked): prompt = base_prompt.substitute(text="\n".join(c)) if main_body: prompt = "main_body: " + main_body + "\n\n" + prompt messages = [{"role": "user", "content": prompt}] response = call_model(client, messages, model_name, return_json=True) responses = responses | response # tasks.append(call_model(client, messages, model_name, return_json=True)) # results = wait tqdm_asyncio.gather(*tasks) # for result in results: # responses = responses | result responses = sorted( responses.items(), key=lambda x: int(re.split(r"[_\s]", x[0])[1]) ) return responses def get_annotated_absa( client: openai.Client, text: str, aspects: list[str], base_prompt: Template = ANNOTATED_ABSA_PROMPT, max_length: int = 2000, model_name: OpenAIModel = OpenAIModel.GPT3_5, ) -> GetAnnotatedABSAOutput: """ Retrieves annotated aspect-based sentiment analysis (ABSA) for the given text and aspects. Args: client (openai.Client): The OpenAI client used to make API calls. text (str): The input text for ABSA. aspects (list[str]): The list of aspects to analyze in the text. base_prompt (str): The base prompt for the ABSA model. model_name (OpenAIModel, optional): The name of the OpenAI model to use for ABSA. Defaults to OpenAIModel.GPT3_5. Returns: GetAnnotatedABSAOutput: The annotated ABSA output. """ from scrape.utils import call_model text = enc.decode(enc.encode(text)[:max_length]) prompt = base_prompt.substitute(text=text, aspects=", ".join(aspects)) messages = [{"role": "user", "content": prompt}] _response = call_model(client, messages, model_name=model_name, return_json=True) _response = _response["text"] response = [] for r in _response: if isinstance(r, list): r = tuple(r) response.append(r) return response def get_val_from_absa_output_key( output: GetABSAOutput, key: str ) -> dict[int, bool | str]: """ Retrieves the values associated with a given key from the ABSA output. Args: output (GetABSAOutput): The ABSA output. key (str): The key to retrieve the values for. Returns: dict[int, bool | str]: A dictionary mapping the index to the value associated with the key. """ d = {} for r in output: idx = int(re.split(r"[_\s]", r[0])[1]) - 1 try: d[idx] = r[1][key] except KeyError: d[idx] = None return d def count_absa(output: GetABSAOutput) -> CountABSAOutput: """ Counts the number of positive, negative, and neutral sentiments for each aspect in the given output. Args: output (GetABSAOutput): The output of the get_absa function. Returns: CountABSAOutput: An object containing the counts of positive, negative, and neutral sentiments for each aspect. """ positive = Counter() negative = Counter() neutral = Counter() for _, review in output: for aspect, sentiment in review.items(): aspect = aspect.lower().replace("_", " ") if sentiment == "positive": positive[aspect] += 1 elif sentiment == "negative": negative[aspect] += 1 elif sentiment == "neutral": neutral[aspect] += 1 return CountABSAOutput(positive=positive, negative=negative, neutral=neutral) def create_absa_counts_df( counts: CountABSAOutput, proportional: bool = True ) -> pd.DataFrame: """ Create a DataFrame from the counts of positive, negative, and neutral sentiments. Args: counts (CountABSAOutput): The counts of positive, negative, and neutral sentiments. proportional (bool, optional): Whether to calculate the proportions of each sentiment. Defaults to True. Returns: pd.DataFrame: The DataFrame containing the counts or proportions of each sentiment. """ positive = pd.Series(counts.positive, dtype=int) negative = pd.Series(counts.negative, dtype=int) neutral = pd.Series(counts.neutral, dtype=int) df = pd.DataFrame( {"positive": positive, "negative": negative, "neutral": neutral} ).fillna(0) total = df.sum(axis=1) if proportional: df = df.div(df.sum(axis=1), axis=0) df["total"] = total return df def create_absa_df(data: GetABSAOutput) -> pd.DataFrame: """ Create a pandas DataFrame from the GetABSAOutput data. Args: data (GetABSAOutput): The GetABSAOutput data containing user and sentiment information. Returns: pd.DataFrame: A DataFrame with user index, positive aspects, negative aspects, and neutral aspects. """ res = [] for user, d in data: idx = int(re.split(r"[_\s]", user)[-1]) - 1 pos, neg, neu = [], [], [] for aspect, sentiment in d.items(): aspect = aspect.lower().replace("_", " ") if sentiment == "positive": pos.append(aspect) elif sentiment == "negative": neg.append(aspect) elif sentiment == "neutral": neu.append(aspect) res.append( { "user_idx": idx, "positive": ", ".join(pos), "negative": ", ".join(neg), "neutral": ", ".join(neu), } ) return pd.DataFrame(res).set_index("user_idx") def create_absa_heatmap( df: pd.DataFrame, min_occurrences: int = 2, cmap: str = "rocket" ) -> Figure: """ Create a heatmap of the given DataFrame. Parameters: df (pd.DataFrame): The DataFrame to create the heatmap from. cmap (str): The colormap to use for the heatmap. Default is "rocket". Returns: Figure: The generated heatmap figure. """ df[df["total"] >= min_occurrences] aspects = pd.unique(df[["positive", "negative", "neutral"]].values.ravel("K")) height = len(aspects) * 1.1 fig = plt.figure(figsize=(10, 10)) sns.heatmap(df, cmap=cmap, annot=True, fmt=".2f") return fig
[ "\n", ", ", "main_body: PLACEHOLDER\n\nPLACEHOLDER" ]
2024-01-10
dachenlian/streamlit-absa
scrape~analysis.py
from collections import Counter from pathlib import Path from string import Template from typing import Callable from loguru import logger import matplotlib.pyplot as plt from nltk.corpus import stopwords import openai import tiktoken from wordcloud import WordCloud from scrape.absa.aspects import FINANCIAL_ASPECTS, MOVIE_ASPECTS from scrape.absa.prompts import GET_ABSA_FINANCE_PROMPT, GET_ABSA_MOVIE_PROMPT from scrape.types import OpenAIModel enc = tiktoken.get_encoding("cl100k_base") BASE = Path(__file__).resolve().parent.parent font_path = str(BASE / "data/fonts/SourceHanSerifK-Light.otf") def get_summary( client: openai.Client, texts: list[str], base_prompt: Template, main_body: str | None = None, max_length: int = 2000, model_name: OpenAIModel = OpenAIModel.GPT4, ) -> str: from scrape.utils import call_model _texts = [f"Text {i}:\n{t}" for i, t in enumerate(texts, start=1)] texts = [] length = 0 for t in _texts: length += len(enc.encode(t)) texts.append(t) if length > max_length: break prompt = base_prompt.substitute(text=texts) if main_body: prompt = "main_body: " + main_body + "\n\n" + prompt messages = [{"role": "user", "content": prompt}] response = call_model(client, messages, model_name) return response def create_wordcloud( freq: Counter, width: int = 1280, height: int = 720, font_path: str | None = font_path ) -> None: fig = plt.figure(figsize=(10, 10)) wc = WordCloud( font_path=font_path, background_color="white", max_words=1000, width=width, height=height, ) wc.generate_from_frequencies(freq) plt.imshow(wc, interpolation="bilinear") plt.axis("off") def create_word_count( texts: list[str], stop: list[str] | None = None, ) -> Counter: c = Counter() for t in texts: c.update(t.lower().split()) if stop: for w in stop: c.pop(w, None) return c
[ "main_body: PLACEHOLDER\n\nmain_body: PLACEHOLDER\n\nprompt39b6c2ae-bd14-4547-aaf8-66ba8da712c0", "main_body: PLACEHOLDER\n\npromptae0c1800-2838-49f1-a106-9b4ae086a6b8" ]
2024-01-10
dachenlian/streamlit-absa
pages~2_demo.py
from dataclasses import asdict import json from pathlib import Path import sys from annotated_text import annotated_text from dotenv import load_dotenv import openai import pandas as pd import streamlit as st from streamlit_extras.switch_page_button import switch_page from scrape.types import LetterboxdReview, MovieMetadata, OpenAIModel # Add the parent directory to the path BASE = Path(__file__).resolve().parent.parent if str(BASE) not in sys.path: sys.path.append(str(BASE)) from scrape.absa.absa import ( count_absa, create_absa_counts_df, create_absa_df, create_absa_heatmap, get_absa, get_val_from_absa_output_key, get_annotated_absa, ) from scrape.absa.aspects import MOVIE_ASPECTS from scrape.absa.types import GetABSAOutput from scrape.letterboxd import get_letterboxd_reviews from scrape.imdb import get_imdb_reviews from scrape.types import IMDbReview from scrape.analysis import ( create_wordcloud, create_word_count, ) # load_dotenv() if "url" not in st.session_state or not st.session_state["url"]: switch_page("start") if "api_key" not in st.session_state or not st.session_state["api_key"]: switch_page("start") # CLIENT = openai.Client(api_key=os.environ["OPENAI_API_KEY"]) CLIENT = openai.Client(api_key=str(st.session_state["api_key"])) st.set_page_config( page_title="Movie Review Explorer", page_icon="๐ŸŽฌ", layout="wide", ) @st.cache_data def get_demo_reviews() -> list[LetterboxdReview]: with BASE.joinpath( "examples/the-boy-and-the-heron-letterboxd-240-reviews.json" ).open() as f: return [LetterboxdReview(**d) for d in json.load(f)] @st.cache_data def get_demo_absa() -> GetABSAOutput: with BASE.joinpath( "examples/the-boy-and-the-heron-letterboxd-240-absa.json" ).open() as f: return json.load(f) @st.cache_data def get_demo_summary() -> str: with BASE.joinpath( "examples/the-boy-and-the-heron-letterboxd-240-summary.txt" ).open() as f: return f.read() @st.cache_data def get_demo_wordcloud(): reviews = get_demo_reviews() word_counts = create_word_count([r.review for r in reviews]) create_wordcloud(word_counts) st.pyplot() @st.cache_data def get_demo_heatmap(): absa = get_demo_absa() absa_df = create_absa_counts_df(count_absa(absa), proportional=True) heatmap = create_absa_heatmap(absa_df) return heatmap def get_movie_reviews( url: str, ) -> tuple[MovieMetadata, list[LetterboxdReview] | list[IMDbReview]]: if "letterboxd" in url: return get_letterboxd_reviews(url) elif "imdb" in url: return get_imdb_reviews(url) else: raise ValueError(f"Unknown website: {url}") def filter_df( hide_spoilers: bool, hide_negative: bool, hide_positive: bool, hide_neutral: bool, df: pd.DataFrame, ) -> pd.DataFrame: if hide_spoilers: df = df[df["contains_spoilers"] == "No"] if hide_negative: df = df[~(df["negative"] != "")] if hide_positive: df = df[~(df["positive"] != "")] if hide_neutral: df = df[~(df["neutral"] != "")] return df # reviews = get_movie_reviews(st.session_state['url']) # url = "/home/richard/lope/dspy/lab14/examples/letterboxd.html" # metadata, reviews = get_movie_reviews(url) ####################### # ๆบ–ๅ‚™่ณ‡ๆ–™ ####################### # ่ผ‰ๅ…ฅ่ณ‡ๆ–™ movie_title = "The Boy and the Heron" reviews = get_demo_reviews() absa = get_demo_absa() summary = get_demo_summary() word_counts = create_word_count([r.review for r in reviews]) absa_df = create_absa_df(absa) contains_spoilers = get_val_from_absa_output_key(absa, "contains_spoilers") # heatmap # absa_df = create_absa_counts_df(count_absa(absa), proportional=True) # heatmap = create_absa_heatmap(absa_df) # ๆบ–ๅ‚™ dataframe df = pd.DataFrame([asdict(r) for r in reviews]) # ๅปบ็ซ‹ๆ–ฐ็š„ contains_spoilers ๅˆ— df["contains_spoilers"] = contains_spoilers df["contains_spoilers"] = ( df["contains_spoilers"].fillna(True).map({True: "Yes", False: "No"}) ) # ๅปบ็ซ‹ๆ–ฐ็š„ rating ๅˆ— df["rating"] = df["rating"].fillna(-1) # ๅˆไฝต df ๅ’Œ absa_df ๏ผˆpositive, negative, neutral๏ผ‰ ๅˆ— df = pd.merge(df, absa_df, left_index=True, right_index=True, how="outer") ####################### # Streamlit ไป‹้ข ####################### with st.sidebar: reset = st.button("Reset", use_container_width=True, type="primary") if reset: st.session_state["url"] = "" switch_page("start") # ่จญๅฎšๅˆ†้  summary_tab, table_tab, annotated_tab = st.tabs( ["Summary", "Details", "Annotated ABSA"] ) with summary_tab: st.markdown("# The Boy and the Heron") st.markdown(summary) with table_tab: st.markdown("# The Boy and the Heron Reviews") st.markdown("## Word Cloud") get_demo_wordcloud() # create_wordcloud(word_counts) # st.pyplot() st.markdown("## ABSA Heatmap") st.pyplot(get_demo_heatmap(), dpi=1000, use_container_width=True) st.markdown("## Reviews") col1, col2, col3, col4 = st.columns(4) with col1: hide_spoilers = st.toggle("Hide Spoilers", value=False) with col2: hide_positive = st.toggle("Hide Positive", value=False) with col3: hide_negative = st.toggle("Hide Negative", value=False) with col4: hide_neutral = st.toggle("Hide Neutral", value=False) df = filter_df(hide_spoilers, hide_negative, hide_positive, hide_neutral, df) st.dataframe(df, use_container_width=True, height=1000) with annotated_tab: st.markdown("# Annotate a Review") with st.form("annotate_form"): textbox = st.text_area("Review", height=200) submitted = st.form_submit_button("Submit") if submitted: annotated_text( get_annotated_absa( client=CLIENT, text=textbox, aspects=MOVIE_ASPECTS, model_name=OpenAIModel.GPT4, ) )
[]
2024-01-10
zachjgraves/ask-pres
ask-pres.py
import streamlit as st import openai import os from PIL import Image openai.api_key = os.environ["OPENAI_API_KEY"] st.title("Ask a President") image = Image.open('header.jpg') st.image(image, caption='Photo by Jean Beller on Unsplash') question = st.text_area("Insert a question") person = st.selectbox("Pick a person", ["Joe Biden", "Donald Trump", "Barack Obama", "George W. Bush", \ "Bill Clinton", "Ronald Reagan", "John F Kennedy", "Franklin Delano Roosevelt", \ "Theodore Roosevelt", "Abraham Lincoln", "Thomas Jefferson", "George Washington"]) if st.button("Submit"): response = openai.Completion.create( model="text-davinci-003", prompt="Imagine you are a caricature of the president of the United States, {}.\ Answer this question in two paragraphs as if you were a stand-up comedian: {}?".format(person, question), max_tokens=500, temperature=0.8, stream=True ) with st.empty(): collected_events = [] completion_text = '' for event in response: collected_events.append(event) event_text = event['choices'][0]['text'] completion_text += event_text st.write(completion_text)
[ "Imagine you are a caricature of the president of the United States, PLACEHOLDER. Answer this question in two paragraphs as if you were a stand-up comedian: PLACEHOLDER?" ]
2024-01-10
wenkai-li/11711-assignment-2
code~llm~llama2.py
from tqdm import tqdm from langchain.llms import HuggingFacePipeline from transformers import AutoTokenizer, AutoModelForCausalLM from langchain.chains import LLMChain, SimpleSequentialChain from langchain.prompts import PromptTemplate from fuzzywuzzy import fuzz from fuzzywuzzy import process import transformers import torch import langchain from langchain.cache import InMemoryCache import re, gc, csv import numpy as np import spacy def llama2(system_prompt, sentences, output_path): langchain.llm_cache = InMemoryCache() # may need to change this line model_id = "meta-llama/Llama-2-70b-chat-hf" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",load_in_8bit=True) pipeline = transformers.pipeline( "text-generation", model=model, tokenizer=tokenizer, max_length=4096, temperature=1, top_k=1, eos_token_id=tokenizer.eos_token_id, ) llm = HuggingFacePipeline(pipeline=pipeline) msg_1 = "Are you clear about your role?" answer_1 = "Sure, I'm ready to help you with your NER task. Please provide me with the necessary information to get started." prompt_template1 = """<s>[INST] <<SYS>> {system_prompt} <</SYS>> {user_msg} [/INST] {model_answer} </s><s>[INST] Entity Definition: 1. MethodName: Names of the baseline and proposed systems, could be: the main description of the method, such as: "Bidirectional Encoder Representations from Transformers" or the abbreviated form of their method, such as: "BERT". 2. HyperparameterName: Names of the hyper-parameters mentioned in the paper, that cannot be inferred while fitting models to the training data. This could either be the full description (e.g., "number of layers"), or the mathematical notation. "train/dev/test split ratio" should be labeled as HyperparameterName. 3. HyperparameterValue: Value of the hyper-parameters. All hyperparameter values annotated should be numerical values. 4. MetricName: Names of the evaluation metrics being used for method evaluation. Only annotate the name of the metric and not include other context. For example, given a string "the accuracy on test set" you should only annotate "accuracy". The abbreviations are also considered valid metric names. 5. MetricValue: Evaluation results of methods on each metric. Many analyses use relative metric values (+5.3%) instead of absolute values (45.6%), these relative values should also be annotated as valid metric values. Be sure to include "%" at the end of the number. 6. TaskName: Name of the tasks that the current work is evaluated on, e.g. "Named Entity Recognition". You should not annotate tasks that are mentioned but not evaluated with the proposed work, or the names that do not provide information about what task is being solved, e.g. "task A", "subtask A" should not be annotated. 7. DatasetName: Name of the dataset of target tasks. Some works evaluate on dataset benchmarks (i.e., a collection of datasets) such as GLUE. You could also label the benchmark name as a dataset name. Output Format: {{'MethodName': [list of entities present], 'HyperparameterName': [list of entities present], 'HyperparameterValue': [list of entities present], 'MetricName': [list of entities present], 'MetricValue': [list of entities present], 'TaskName': [list of entities present], 'DatasetName': [list of entities present]}} If no entities are presented in any categories keep it None Examples 1: Sentence: "We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) ." Output: {{'MethodName': None, 'HyperparameterName': ['number of layers', 'L', 'hidden size', 'H', 'learning rate', 'ฮธ'], 'HyperparameterValue': ['0.001'], 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None}} Examples 2: Sentence: "Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks ." Output: {{'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': ['spearman correlations', 'accuracy'], 'MetricValue': None, 'TaskName': None, 'DatasetName': 'STS-B'}} Examples 3: Sentence: "BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation." Output: {{'MethodName': ['BERT'], 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': ['natural language inference', 'NLI', 'question answering'], 'DatasetName': None}} 4. Sentence: "{sentence}" Output: [/INST]""" prompt1 = PromptTemplate(template=prompt_template1, input_variables=['sentence'], partial_variables={"system_prompt": system_prompt, "user_msg": msg_1, "model_answer": answer_1}) # prompt_template2 = "[INST] <<SYS>>\n{input}\n{format_instructions}\n<</SYS>>[/INST]" # prompt2 = PromptTemplate(template=prompt_template2, input_variables=['input'], partial_variables={"format_instructions": parser.get_format_instructions()}) generate_chain = LLMChain(llm=llm, prompt=prompt1) fd = open(output_path,'w') for sentence in tqdm(sentences): output = generate_chain.run(sentence) # outputs.append(output) dict = extract_information(output) format_dict = create_formatted_dictionary(dict) conll = convert_to_conll_format(sentence,format_dict) fd.write(conll) # return outputs def fuzzy_match_keys(input_key, possible_keys): # Find the closest matching key from possible_keys using fuzzy matching best_match, score = process.extractOne(input_key, possible_keys) if score >= 50: # You can adjust the threshold for a better match return best_match else: return None def create_formatted_dictionary(input_dict): # Define possible keys possible_keys = [ 'MethodName', 'HyperparameterName', 'HyperparameterValue', 'MetricName', 'MetricValue', 'TaskName', 'DatasetName' ] # Initialize the formatted dictionary with None values formatted_dict = {key: None for key in possible_keys} for input_key, value in input_dict.items(): matched_key = fuzzy_match_keys(input_key, possible_keys) if matched_key: formatted_dict[matched_key] = value return formatted_dict def text_to_sentences(text): # Process the text using spaCy doc = nlp(text) # Extract the sentences from the processed document sentences = [sent.text for sent in doc.sents] return sentences def convert_to_conll_format(sentence, data): words = sentence.split() labels = ['O'] * len(words) for entity_type, entity_values in data.items(): if entity_values is not None: for entity_value in entity_values: try: entity_tokens = entity_value.split() entity_start = 'B-' + entity_type entity_inside = 'I-' + entity_type for i in range(len(words) - len(entity_tokens) + 1): if words[i:i+len(entity_tokens)] == entity_tokens: labels[i] = entity_start for j in range(i + 1, i + len(entity_tokens)): labels[j] = entity_inside except: continue conll_lines = [] for word, label in zip(words, labels): conll_lines.append(f"{word} {label}") return '\n'.join(conll_lines) + '\n' def extract_information(text): # Extract the content inside the curly braces content_match = re.search(r'\{([^}]*)\}', text) if content_match: content = content_match.group(1) try: dict = eval('{'+content+'}') return dict except: print("parse fail"+content) # Return a dictionary with all values set to None if the specified format is not found return { 'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None } if __name__ == "__main__": torch.cuda.empty_cache() gc.collect() # Load the spaCy model with the sentencizer nlp = spacy.load("en_core_web_sm") output_file = 'predict.txt' system_prompt_path = 'system_prompt.txt' system_prompt = open(system_prompt_path,encoding='utf-8',mode='r').read() outputs = [] input_path_conll = 'test.conll' batch = np.loadtxt(input_path_conll,dtype=str,delimiter=' ') # Combine the list of tokens into a single string text = " ".join(batch[...,0]) sentences = text_to_sentences(text) llama2(system_prompt, sentences, output_file)
[ "MethodName", "DatasetName", "spearman correlations", "hidden size", "Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks .", "MetricValue", "the accuracy on test set", "MetricName", "natural language inference", "We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) .", "{sentence}", "model_answer", "HyperparameterName", "subtask A", "system_prompt", "question answering", "learning rate", "sentence", "system_prompt.txt", "train/dev/test split ratio", "BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation.", "<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n{user_msg} [/INST] {model_answer} </s><s>[INST] Entity Definition:\n1. MethodName: Names of the baseline and proposed systems, could be: the main description of the method, such as: \"Bidirectional Encoder Representations from Transformers\" or the abbreviated form of their method, such as: \"BERT\".\n2. HyperparameterName: Names of the hyper-parameters mentioned in the paper, that cannot be inferred while fitting models to the training data. This could either be the full description (e.g., \"number of layers\"), or the mathematical notation. \"train/dev/test split ratio\" should be labeled as HyperparameterName.\n3. HyperparameterValue: Value of the hyper-parameters. All hyperparameter values annotated should be numerical values.\n4. MetricName: Names of the evaluation metrics being used for method evaluation. Only annotate the name of the metric and not include other context. For example, given a string \"the accuracy on test set\" you should only annotate \"accuracy\". The abbreviations are also considered valid metric names.\n5. MetricValue: Evaluation results of methods on each metric. Many analyses use relative metric values (+5.3%) instead of absolute values (45.6%), these relative values should also be annotated as valid metric values. Be sure to include \"%\" at the end of the number.\n6. TaskName: Name of the tasks that the current work is evaluated on, e.g. \"Named Entity Recognition\". You should not annotate tasks that are mentioned but not evaluated with the proposed work, or the names that do not provide information about what task is being solved, e.g. \"task A\", \"subtask A\" should not be annotated.\n7. DatasetName: Name of the dataset of target tasks. Some works evaluate on dataset benchmarks (i.e., a collection of datasets) such as GLUE. You could also label the benchmark name as a dataset name.\n\nOutput Format:\n{{'MethodName': [list of entities present], 'HyperparameterName': [list of entities present], 'HyperparameterValue': [list of entities present], 'MetricName': [list of entities present], 'MetricValue': [list of entities present], 'TaskName': [list of entities present], 'DatasetName': [list of entities present]}}\nIf no entities are presented in any categories keep it None\n\nExamples 1: Sentence: \"We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) .\"\nOutput: {{'MethodName': None, 'HyperparameterName': ['number of layers', 'L', 'hidden size', 'H', 'learning rate', 'ฮธ'], 'HyperparameterValue': ['0.001'], 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None}}\n\nExamples 2: Sentence: \"Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks .\"\nOutput: {{'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': ['spearman correlations', 'accuracy'], 'MetricValue': None, 'TaskName': None, 'DatasetName': 'STS-B'}}\n\nExamples 3: Sentence: \"BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation.\"\nOutput: {{'MethodName': ['BERT'], 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': ['natural language inference', 'NLI', 'question answering'], 'DatasetName': None}}\n\n4. Sentence: \"{sentence}\"\nOutput: [/INST]", "task A", "Named Entity Recognition", "HyperparameterValue", "Bidirectional Encoder Representations from Transformers", "number of layers" ]
2024-01-10
wenkai-li/11711-assignment-2
src~llm~llama2.py
from tqdm import tqdm from langchain.llms import HuggingFacePipeline from transformers import AutoTokenizer, AutoModelForCausalLM from langchain.chains import LLMChain, SimpleSequentialChain from langchain.prompts import PromptTemplate from fuzzywuzzy import fuzz from fuzzywuzzy import process import transformers import torch import langchain from langchain.cache import InMemoryCache import re, gc, csv import numpy as np import spacy def llama2(system_prompt, sentences, output_path): langchain.llm_cache = InMemoryCache() model_id = "/data/datasets/models/huggingface/meta-llama/Llama-2-70b-chat-hf" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto",load_in_8bit=True) pipeline = transformers.pipeline( "text-generation", model=model, tokenizer=tokenizer, max_length=4096, temperature=1, top_k=1, eos_token_id=tokenizer.eos_token_id, ) llm = HuggingFacePipeline(pipeline=pipeline) msg_1 = "Are you clear about your role?" answer_1 = "Sure, I'm ready to help you with your NER task. Please provide me with the necessary information to get started." prompt_template1 = """<s>[INST] <<SYS>> {system_prompt} <</SYS>> {user_msg} [/INST] {model_answer} </s><s>[INST] Entity Definition: 1. MethodName: Names of the baseline and proposed systems, could be: the main description of the method, such as: "Bidirectional Encoder Representations from Transformers" or the abbreviated form of their method, such as: "BERT". 2. HyperparameterName: Names of the hyper-parameters mentioned in the paper, that cannot be inferred while fitting models to the training data. This could either be the full description (e.g., "number of layers"), or the mathematical notation. "train/dev/test split ratio" should be labeled as HyperparameterName. 3. HyperparameterValue: Value of the hyper-parameters. All hyperparameter values annotated should be numerical values. 4. MetricName: Names of the evaluation metrics being used for method evaluation. Only annotate the name of the metric and not include other context. For example, given a string "the accuracy on test set" you should only annotate "accuracy". The abbreviations are also considered valid metric names. 5. MetricValue: Evaluation results of methods on each metric. Many analyses use relative metric values (+5.3%) instead of absolute values (45.6%), these relative values should also be annotated as valid metric values. Be sure to include "%" at the end of the number. 6. TaskName: Name of the tasks that the current work is evaluated on, e.g. "Named Entity Recognition". You should not annotate tasks that are mentioned but not evaluated with the proposed work, or the names that do not provide information about what task is being solved, e.g. "task A", "subtask A" should not be annotated. 7. DatasetName: Name of the dataset of target tasks. Some works evaluate on dataset benchmarks (i.e., a collection of datasets) such as GLUE. You could also label the benchmark name as a dataset name. Output Format: {{'MethodName': [list of entities present], 'HyperparameterName': [list of entities present], 'HyperparameterValue': [list of entities present], 'MetricName': [list of entities present], 'MetricValue': [list of entities present], 'TaskName': [list of entities present], 'DatasetName': [list of entities present]}} If no entities are presented in any categories keep it None Examples 1: Sentence: "We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) ." Output: {{'MethodName': None, 'HyperparameterName': ['number of layers', 'L', 'hidden size', 'H', 'learning rate', 'ฮธ'], 'HyperparameterValue': ['0.001'], 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None}} Examples 2: Sentence: "Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks ." Output: {{'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': ['spearman correlations', 'accuracy'], 'MetricValue': None, 'TaskName': None, 'DatasetName': 'STS-B'}} Examples 3: Sentence: "BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation." Output: {{'MethodName': ['BERT'], 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': ['natural language inference', 'NLI', 'question answering'], 'DatasetName': None}} 4. Sentence: "{sentence}" Output: [/INST]""" prompt1 = PromptTemplate(template=prompt_template1, input_variables=['sentence'], partial_variables={"system_prompt": system_prompt, "user_msg": msg_1, "model_answer": answer_1}) # prompt_template2 = "[INST] <<SYS>>\n{input}\n{format_instructions}\n<</SYS>>[/INST]" # prompt2 = PromptTemplate(template=prompt_template2, input_variables=['input'], partial_variables={"format_instructions": parser.get_format_instructions()}) generate_chain = LLMChain(llm=llm, prompt=prompt1) # json_chain = LLMChain(llm=llm, prompt=prompt2) # overall_chain = SimpleSequentialChain(chains=[generate_chain, json_chain], verbose=True) # retry_parser = RetryWithErrorOutputParser.from_llm( # parser=parser, llm=llm) # autofix_parser = OutputFixingParser.from_llm(parser=parser, llm=llm) # chain = LLMChain(llm=llm, prompt=prompt) # outputs = [] fd = open(output_path,'w') for sentence in tqdm(sentences): output = generate_chain.run(sentence) # outputs.append(output) dict = extract_information(output) format_dict = create_formatted_dictionary(dict) conll = convert_to_conll_format(sentence,format_dict) fd.write(conll) # return outputs def fuzzy_match_keys(input_key, possible_keys): # Find the closest matching key from possible_keys using fuzzy matching best_match, score = process.extractOne(input_key, possible_keys) if score >= 50: # You can adjust the threshold for a better match return best_match else: return None def create_formatted_dictionary(input_dict): # Define possible keys possible_keys = [ 'MethodName', 'HyperparameterName', 'HyperparameterValue', 'MetricName', 'MetricValue', 'TaskName', 'DatasetName' ] # Initialize the formatted dictionary with None values formatted_dict = {key: None for key in possible_keys} for input_key, value in input_dict.items(): matched_key = fuzzy_match_keys(input_key, possible_keys) if matched_key: formatted_dict[matched_key] = value return formatted_dict def text_to_sentences(text): # Process the text using spaCy doc = nlp(text) # Extract the sentences from the processed document sentences = [sent.text for sent in doc.sents] return sentences def convert_to_conll_format(sentence, data): words = sentence.split() labels = ['O'] * len(words) for entity_type, entity_values in data.items(): if entity_values is not None: for entity_value in entity_values: try: entity_tokens = entity_value.split() entity_start = 'B-' + entity_type entity_inside = 'I-' + entity_type for i in range(len(words) - len(entity_tokens) + 1): if words[i:i+len(entity_tokens)] == entity_tokens: labels[i] = entity_start for j in range(i + 1, i + len(entity_tokens)): labels[j] = entity_inside except: continue conll_lines = [] for word, label in zip(words, labels): conll_lines.append(f"{word} {label}") return '\n'.join(conll_lines) + '\n' def extract_information(text): # Extract the content inside the curly braces content_match = re.search(r'\{([^}]*)\}', text) if content_match: content = content_match.group(1) try: dict = eval('{'+content+'}') return dict except: print("parse fail"+content) # Return a dictionary with all values set to None if the specified format is not found return { 'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None } if __name__ == "__main__": torch.cuda.empty_cache() gc.collect() # Load the spaCy model with the sentencizer nlp = spacy.load("en_core_web_sm") output_file = 'predict.txt' system_prompt_path = 'system_prompt.txt' system_prompt = open(system_prompt_path,encoding='utf-8',mode='r').read() outputs = [] input_path_conll = 'test.conll' batch = np.loadtxt(input_path_conll,dtype=str,delimiter=' ') # Combine the list of tokens into a single string text = " ".join(batch[...,0]) sentences = text_to_sentences(text) # print(sentences) llama2(system_prompt, sentences, output_file)
[ "MethodName", "DatasetName", "spearman correlations", "hidden size", "Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks .", "MetricValue", "the accuracy on test set", "MetricName", "natural language inference", "We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) .", "{sentence}", "model_answer", "HyperparameterName", "subtask A", "system_prompt", "question answering", "learning rate", "system_prompt.txt", "sentence", "train/dev/test split ratio", "BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation.", "<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n{user_msg} [/INST] {model_answer} </s><s>[INST] Entity Definition:\n1. MethodName: Names of the baseline and proposed systems, could be: the main description of the method, such as: \"Bidirectional Encoder Representations from Transformers\" or the abbreviated form of their method, such as: \"BERT\".\n2. HyperparameterName: Names of the hyper-parameters mentioned in the paper, that cannot be inferred while fitting models to the training data. This could either be the full description (e.g., \"number of layers\"), or the mathematical notation. \"train/dev/test split ratio\" should be labeled as HyperparameterName.\n3. HyperparameterValue: Value of the hyper-parameters. All hyperparameter values annotated should be numerical values.\n4. MetricName: Names of the evaluation metrics being used for method evaluation. Only annotate the name of the metric and not include other context. For example, given a string \"the accuracy on test set\" you should only annotate \"accuracy\". The abbreviations are also considered valid metric names.\n5. MetricValue: Evaluation results of methods on each metric. Many analyses use relative metric values (+5.3%) instead of absolute values (45.6%), these relative values should also be annotated as valid metric values. Be sure to include \"%\" at the end of the number.\n6. TaskName: Name of the tasks that the current work is evaluated on, e.g. \"Named Entity Recognition\". You should not annotate tasks that are mentioned but not evaluated with the proposed work, or the names that do not provide information about what task is being solved, e.g. \"task A\", \"subtask A\" should not be annotated.\n7. DatasetName: Name of the dataset of target tasks. Some works evaluate on dataset benchmarks (i.e., a collection of datasets) such as GLUE. You could also label the benchmark name as a dataset name.\n\nOutput Format:\n{{'MethodName': [list of entities present], 'HyperparameterName': [list of entities present], 'HyperparameterValue': [list of entities present], 'MetricName': [list of entities present], 'MetricValue': [list of entities present], 'TaskName': [list of entities present], 'DatasetName': [list of entities present]}}\nIf no entities are presented in any categories keep it None\n\nExamples 1: Sentence: \"We denote the number of layers as L , the hidden size as H , and learning rate as ฮธ ( ฮธ is set to 0.001 ) .\"\nOutput: {{'MethodName': None, 'HyperparameterName': ['number of layers', 'L', 'hidden size', 'H', 'learning rate', 'ฮธ'], 'HyperparameterValue': ['0.001'], 'MetricName': None, 'MetricValue': None, 'TaskName': None, 'DatasetName': None}}\n\nExamples 2: Sentence: \"Spearman correlations are reported for STS - B, and accuracy scores are reported for the other tasks .\"\nOutput: {{'MethodName': None, 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': ['spearman correlations', 'accuracy'], 'MetricValue': None, 'TaskName': None, 'DatasetName': 'STS-B'}}\n\nExamples 3: Sentence: \"BERT outperforms other methods on natural language inference ( NLI ) , question answering . It was not evaluated on machine translation.\"\nOutput: {{'MethodName': ['BERT'], 'HyperparameterName': None, 'HyperparameterValue': None, 'MetricName': None, 'MetricValue': None, 'TaskName': ['natural language inference', 'NLI', 'question answering'], 'DatasetName': None}}\n\n4. Sentence: \"{sentence}\"\nOutput: [/INST]", "task A", "Named Entity Recognition", "HyperparameterValue", "Bidirectional Encoder Representations from Transformers", "number of layers" ]
2024-01-10
EugenHotaj/zig_gpt2
download_weights.py
"""Downloads GPT-2 checkpoints from OpenAI. Weight tensors are transposed and dumped in raw binary so they can easily be loaded into Zig/PyTorch. The unicode->byte encoder is statically generated and dumped to json. Based on https://github.com/openai/gpt-2. """ import json import os import numpy as np import requests import tensorflow as tf from tqdm import tqdm model = "models/124M" # Download the model weights from OpenAI if they don't already exist. if not os.path.exists(model): os.makedirs(model) for filename in [ "checkpoint", "encoder.json", "hparams.json", "model.ckpt.data-00000-of-00001", "model.ckpt.index", "model.ckpt.meta", "vocab.bpe", ]: resp = requests.get( f"https://openaipublic.blob.core.windows.net/gpt-2/{model}/{filename}", stream=True, ) with open("{model}/{filename}", "wb") as file_: file_size = int(resp.headers["content-length"]) chunk_size = 1000 with tqdm( ncols=100, desc=f"Fetching {filename}", total=file_size, unit_scale=True ) as pbar: # 1k for chunk_size, since Ethernet packet size is around 1500 bytes. for chunk in resp.iter_content(chunk_size=chunk_size): file_.write(chunk) pbar.update(chunk_size) # Dump the model weights in raw binary if they don't already exist. weights_dir = f"{model}/raw" if not os.path.exists(weights_dir): os.makedirs(weights_dir) checkpoint = tf.train.load_checkpoint(model) variables = sorted(list(checkpoint.get_variable_to_shape_map().keys())) with tqdm( ncols=100, desc=f"Dumping raw weights", total=len(variables), unit_scale=True ) as pbar: for name in variables: tensor = checkpoint.get_tensor(name).astype(np.float32).squeeze() # Store weight tensors in column major format. if name.endswith("/w"): tensor = tensor.T fname = name.replace("/", "-") with open(f"{weights_dir}/{fname}", "wb") as file_: file_.write(tensor.reshape(-1).tobytes()) pbar.update(1) # Statically create and dump the unicode->bytes encoder. def unicode_to_bytes(): """Returns a dictionary of unicode->byte.""" bs = ( list(range(ord("!"), ord("~") + 1)) + list(range(ord("ยก"), ord("ยฌ") + 1)) + list(range(ord("ยฎ"), ord("รฟ") + 1)) ) cs = bs[:] n = 0 for b in range(2**8): if b not in bs: bs.append(b) cs.append(2**8 + n) n += 1 cs = [chr(n) for n in cs] # !!NOTE!!: Unlike OpenAI's implementation, we dump out unicode->bytes so we don't # have to deal with non-string JSON keys. return dict(zip(cs, bs)) with open(f"{model}/byte_encoder.json", "w") as file_: json.dump(unicode_to_bytes(), file_)
[]
2024-01-10
jackyzhang69/client
assess~assess~nocs~ai~getnoccode.py
""" This script is used to get the NOC code from a job description and search model, which is semantic, lexical, or mix. """ import os import cohere import pinecone from dotenv import load_dotenv from langchain.embeddings import CohereEmbeddings from langchain.vectorstores import Pinecone from whoosh.fields import * from whoosh.index import open_dir from whoosh.qparser import OrGroup, QueryParser from config import BASEDIR from ..content_noc21 import DETAILS_NOC21 from .translator import translate # Load environment variables from .env file load_dotenv() # Get the value of an environment variable cohere_api = os.getenv('COHERE_API_KEY') pinecone_api = os.getenv('PINECONE_API_KEY') pinecone_env = os.getenv('PINECONE_ENV') # assemble semantic search results, lexical search results, and rerank results def get_results(semantic_nocs, lexical_nocs, rerank_nocs): interpreted_rerank_nocs = [] for i, hit in enumerate(rerank_nocs): score = hit.relevance_score sectionss = hit.document["text"].split("\n\n") noc_code, title, title_example, main_duties = sectionss noc_code = noc_code.replace("\n", "").replace(" ", "").split(":")[1] title = title.replace("\n", "") noc = {"noc_code": noc_code, "similarity": f"{score:.1%}"} interpreted_rerank_nocs.append(noc) results = { "semantic_nocs": semantic_nocs, "lexical_nocs": lexical_nocs, "rerank_nocs": interpreted_rerank_nocs, } return results def semantic_search(query, docsearch): docs = docsearch.similarity_search_with_score(query, k=10) # return semantic search result as a list of noc codes and similarity scores semantic_results = [] for i in range(len(docs)): doc, score = docs[i] sections = doc.page_content.split( "\n\n" ) # noc code, title, title examples, main duties noc_code, title, title_example, main_duties = sections noc_code = noc_code.replace("\n", "").split(" ")[2] title = title.replace("\n", "") result = {"noc_code": noc_code, "similarity": f"{score:.1%}"} semantic_results.append(result) return semantic_results def lexical_search(ix, schema, query): # Define a query and search the index with ix.searcher() as searcher: # search from title, title_examples, and main_duties, until find a match for field in ["title", "title_examples", "main_duties"]: query_obj = QueryParser(field, schema, group=OrGroup).parse(query) results = searcher.search(query_obj) if len(results) > 0: break lexical_nocs = [] for result in results: noc = {"noc_code": result["noc_code"], "similarity": ""} lexical_nocs.append(noc) return lexical_nocs def get_combined_documents(semantic_nocs, lexical_nocs): combined_nocs = list(set(semantic_nocs + lexical_nocs)) combined_documents = [] for noc in combined_nocs: content = DETAILS_NOC21[noc] doc = ( "Noc code:\n" + noc + "\n\n" + "Title: \n" + content["title"] + "\n\n" + "Title examples: \n" + "\n".join(content["title_examples"]) + "\n\n" + "Main duties: \n" + "\n".join(content["main_duties"]) ) combined_documents.append(doc) return combined_documents def get_noc_code(job_description, search_model="semantic"): """ """ # if job_description is not English, translate to English query = ( translate(job_description) if not job_description.isascii() else job_description ) # initialize Pinecone client and whoosh index co = cohere.Client(cohere_api) embeddings = CohereEmbeddings() pinecone.init( api_key=pinecone_api, environment=pinecone_env ) docsearch = Pinecone.from_existing_index("noc2021v1", embeddings) schema = Schema( noc_code=TEXT(stored=True), title=TEXT(stored=True), title_examples=TEXT(stored=True), main_duties=TEXT(stored=True), ) ix = open_dir(BASEDIR / "assess/nocs/ai/text_noc_index") # Semantic search semantic_nocs = ( semantic_search(query, docsearch) if search_model == "semantic" or search_model == "mix" else [] ) # lexical search lexical_nocs = ( lexical_search(ix, schema, query) if search_model == "lexical" or search_model == "mix" else [] ) # get combined nocs from semantic and lexical search semantic_noc_codes = [noc["noc_code"] for noc in semantic_nocs if noc] lexical_noc_codes = [noc["noc_code"] for noc in lexical_nocs if noc] combined_documents = get_combined_documents(semantic_noc_codes, lexical_noc_codes) rerank_nocs = co.rerank( query=query, documents=combined_documents, model="rerank-english-v2.0", top_n=5 ) results = get_results(semantic_nocs, lexical_nocs, rerank_nocs) return results
[]
2024-01-10
jackyzhang69/client
assess~assess~nocs~ai~makeduties.py
from langchain import PromptTemplate from langchain.llms import OpenAI from langchain.chat_models import ChatOpenAI from langchain.schema import ( HumanMessage, ) def get_duty(position, duty, industry, region): template = """ You are an export of human resource. You need to help generate specific job duties for a given position, based on the generic description from the Canadian National Occupational Classification (NOC) code. Please convert the following NOC generic descriptions into specific job duties, using the provided example as a guide, and ensure that the duties are appropriate for the specific industry.While maintaining semantic consistency, try to use different words than those in the original description. Additionally, if the original description is short and abstract, consider adding some concrete details to expand it, but avoid generating excessively long contentใ€‚When referring to nouns mentioned in the NOC generic description, consider whether they are applicable to the specific job, industry, and region, and avoid simply copying from the original text: Example: Context: This position is marketing manager in restaurant industry in Toronto, Canada. NOC Generic Description: Plan, direct and evaluate the activities of firms and departments that develop and implement advertising campaigns to promote the sales of products and services. Specific Job Duty: Strategize, oversee, and assess the initiatives of teams and departments responsible for creating and executing marketing campaigns to increase sales and promote restaurant offerings and services. Context: This position is marketing manager in restaurant industry in Toronto, Canada. NOC Generic Description: Establish distribution networks for products and services, initiate market research studies and analyze their findings, assist in product development, and direct and evaluate the marketing strategies of establishments. Specific Job Duty: Develop and manage channels to promote menu items and services, conduct market research to identify customer preferences, assist in menu development, and oversee marketing strategies to improve restaurant visibility and sales. Context: The position is {position} in the {industry} industry, located in {region}. NOC Generic Description: {duty} Specific Job Duty: """ prompt = PromptTemplate( input_variables=["position", "duty", "industry", "region"], template=template, ) pmt = prompt.format(position=position, duty=duty, industry=industry, region=region) llm = OpenAI(temperature=0, verbose=False) # type: ignore generated_duty: str = llm(pmt) return generated_duty.strip() def refine(duties): template = """ Revise and reorganize a provided list of job duties, ensuring the removal of repetitive content while maintaining the meaning and intent of each responsibility. The revised list should be concise, effective, and clearly communicate all responsibilities without requiring the preservation of the original order. Present the refined duties without any additional information, decorations, or formatting elements such but not limited to number, dash... The duties are: {duties} """ prompt = PromptTemplate( input_variables=["duties"], template=template, ) pmt = prompt.format(duties=duties) llm = ChatOpenAI(temperature=0, verbose=False, model_name="gpt-4") # type: ignore result = llm([HumanMessage(content=pmt)]).content # clean data result_list = result.split("\n") result_list = [r for r in result_list if r] return result_list
[ "\n You are an export of human resource. You need to help generate specific job duties for a given position, based on the generic description from the Canadian National Occupational Classification (NOC) code. Please convert the following NOC generic descriptions into specific job duties, using the provided example as a guide, and ensure that the duties are appropriate for the specific industry.While maintaining semantic consistency, try to use different words than those in the original description. Additionally, if the original description is short and abstract, consider adding some concrete details to expand it, but avoid generating excessively long contentใ€‚When referring to nouns mentioned in the NOC generic description, consider whether they are applicable to the specific job, industry, and region, and avoid simply copying from the original text:\n\n Example:\n Context: This position is marketing manager in restaurant industry in Toronto, Canada. \n NOC Generic Description: Plan, direct and evaluate the activities of firms and departments that develop and implement advertising campaigns to promote the sales of products and services.\n Specific Job Duty: Strategize, oversee, and assess the initiatives of teams and departments responsible for creating and executing marketing campaigns to increase sales and promote restaurant offerings and services.\n\n Context: This position is marketing manager in restaurant industry in Toronto, Canada. \n NOC Generic Description: Establish distribution networks for products and services, initiate market research studies and analyze their findings, assist in product development, and direct and evaluate the marketing strategies of establishments.\n Specific Job Duty: Develop and manage channels to promote menu items and services, conduct market research to identify customer preferences, assist in menu development, and oversee marketing strategies to improve restaurant visibility and sales.\n\n Context: The position is {position} in the {industry} industry, located in {region}.\n NOC Generic Description: {duty}\n\n Specific Job Duty:\n\n ", "region", "\n Revise and reorganize a provided list of job duties, ensuring the removal of repetitive content while maintaining the meaning and intent of each responsibility. The revised list should be concise, effective, and clearly communicate all responsibilities without requiring the preservation of the original order. Present the refined duties without any additional information, decorations, or formatting elements such but not limited to number, dash...\n The duties are:\n {duties}\n ", "industry", "position" ]
2024-01-10
jackyzhang69/client
assess~assess~nocs~ai~makepineconeindex.py
from langchain.embeddings import CohereEmbeddings from langchain.vectorstores import Pinecone import pinecone from ..content_noc21 import DETAILS_NOC21 nocs=[] metadatas=[] for k,v in DETAILS_NOC21.items(): code="NOC code: \n"+k+"\n\n" title="Title: \n"+v["title"]+"\n\n" title_examples="Title examples:\n"+"\n".join(v["title_examples"])+"\n\n" main_duties="Main duties:\n"+"\n".join(v["main_duties"]) nocs.append(code+title+title_examples+main_duties) metadatas.append({"noc_code":k,"title":v["title"],"title_examples":v["title_examples"],"main_duties":v["main_duties"]}) embeddings = CohereEmbeddings() # initialize pinecone pinecone.init( api_key="03754c1d-0a43-4489-946e-d77d90ccf398", environment="us-east4-gcp" ) index_name = "noc2021v1" docsearch = Pinecone.from_texts(nocs, embeddings, metadatas=metadatas, index_name=index_name)
[]
2024-01-10
htrivedi99/notion_streamlit_app
notion_streamlit.py
import requests from langchain.embeddings.openai import OpenAIEmbeddings from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.vectorstores.qdrant import Qdrant from qdrant_client import QdrantClient from qdrant_client.http import models from typing import List import streamlit as st import tiktoken from streamlit_chat import message BASE_URL = "https://api.notion.com" def notion_get_blocks(page_id: str, headers: dict): res = requests.get(f"{BASE_URL}/v1/blocks/{page_id}/children?page_size=100", headers=headers) return res.json() def notion_search(query: dict, headers: dict): res = requests.post(f"{BASE_URL}/v1/search", headers=headers, data=query) return res.json() def get_page_text(page_id: str, headers: dict): page_text = [] blocks = notion_get_blocks(page_id, headers) for item in blocks['results']: item_type = item.get('type') content = item.get(item_type) if content.get('rich_text'): for text in content.get('rich_text'): plain_text = text.get('plain_text') page_text.append(plain_text) return page_text def load_notion(headers: dict) -> list: documents = [] all_notion_documents = notion_search({}, headers) items = all_notion_documents.get('results') for item in items: object_type = item.get('object') object_id = item.get('id') url = item.get('url') title = "" page_text = [] if object_type == 'page': title_content = item.get('properties').get('title') if title_content: title = title_content.get('title')[0].get('text').get('content') elif item.get('properties').get('Name'): if len(item.get('properties').get('Name').get('title')) > 0: title = item.get('properties').get('Name').get('title')[0].get('text').get('content') page_text.append([title]) page_content = get_page_text(object_id, headers) page_text.append(page_content) flat_list = [item for sublist in page_text for item in sublist] text_per_page = ". ".join(flat_list) if len(text_per_page) > 0: documents.append(text_per_page) return documents def chunk_tokens(text: str, token_limit: int) -> list: tokenizer = tiktoken.get_encoding( "cl100k_base" ) chunks = [] tokens = tokenizer.encode(text, disallowed_special=()) while tokens: chunk = tokens[:token_limit] chunk_text = tokenizer.decode(chunk) last_punctuation = max( chunk_text.rfind("."), chunk_text.rfind("?"), chunk_text.rfind("!"), chunk_text.rfind("\n"), ) if last_punctuation != -1: chunk_text = chunk_text[: last_punctuation + 1] cleaned_text = chunk_text.replace("\n", " ").strip() if cleaned_text and (not cleaned_text.isspace()): chunks.append(cleaned_text) tokens = tokens[len(tokenizer.encode(chunk_text, disallowed_special=())):] return chunks def load_data_into_vectorstore(client, docs: List[str]): embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) qdrant_client = Qdrant(client=client, collection_name="notion_streamlit", embedding_function=embeddings.embed_query) ids = qdrant_client.add_texts(docs) return ids @st.cache_resource def connect_to_vectorstore(): client = QdrantClient(host="localhost", port=6333, path="/path/to/qdrant/qdrant_storage") try: client.get_collection("notion_streamlit") except Exception as e: client.recreate_collection( collection_name="notion_streamlit", vectors_config=models.VectorParams(size=1536, distance=models.Distance.COSINE), ) return client @st.cache_data def cache_headers(notion_api_key: str): headers = {"Authorization": f"Bearer {notion_api_key}", "Content-Type": "application/json", "Notion-Version": "2022-06-28"} return headers @st.cache_resource def load_chain(_client, api_key: str): if len(api_key) == 0: api_key = "temp value" embeddings = OpenAIEmbeddings(openai_api_key=api_key) vectorstore = Qdrant(client=_client, collection_name="notion_streamlit", embedding_function=embeddings.embed_query) chain = ConversationalRetrievalChain.from_llm( llm=ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo', openai_api_key=api_key), retriever=vectorstore.as_retriever() ) return chain st.title('Chat With Your Notion Documents!') vector_store = connect_to_vectorstore() with st.sidebar: openai_api_key = st.text_input(label='#### Your OpenAI API Key', placeholder="Paste your OpenAI API key here", type="password") notion_api_key = st.text_input(label='#### Your Notion API Key', placeholder="Paste your Notion API key here", type="password") notion_headers = cache_headers(notion_api_key) load_data = st.button('Load Data') if load_data: documents = load_notion(notion_headers) chunks = [] for doc in documents: chunks.extend(chunk_tokens(doc, 100)) for chunk in chunks: print(chunk) load_data_into_vectorstore(vector_store, chunks) print("Documents loaded.") chain = load_chain(vector_store, openai_api_key) if 'generated' not in st.session_state: st.session_state['generated'] = [] if 'past' not in st.session_state: st.session_state['past'] = [] user_input = st.text_input("You: ", placeholder="Chat with your notion docs here ๐Ÿ‘‡", key="input") if user_input: result = chain({"question": user_input, "chat_history": st.session_state["generated"]}) response = result['answer'] st.session_state['past'].append(user_input) st.session_state['generated'].append((user_input, result["answer"])) if st.session_state['generated']: for i in range(len(st.session_state['generated']) - 1, -1, -1): message(st.session_state['past'][i], is_user=True, key=str(i) + '_user') message(st.session_state["generated"][i][1], key=str(i))
[]
2024-01-10
spiritedtechie/weather-sage
api~prompts~weather_summary.py
from langchain.output_parsers import ResponseSchema, StructuredOutputParser from langchain.prompts.chat import (ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate) response_schemas = [ ResponseSchema(name="summary", description="summary of the weather"), ResponseSchema( name="status", description="predicted status of the weather - can be one of: Poor, Fair, Average, Good or Very Good", ), ResponseSchema( name="inspiring-message", description="uplifting message about the weather", ), ] output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = output_parser.get_format_instructions() template = """ On the following lines is a CSV representation of the weather forecast. The first row contains the column names. Use only this data for the summary. {csv} ----- Use the following code mappings to map any codes in the data to meaningful labels. {code_mappings} ----- {format_instructions} ----- Summarise the weather for the next few hours as follows. Do not including the datetime: 1. For the summary: Imagine you are a weatherman and summarise the data in no more than 200 words. 2. For the predicted status: It must consider the temperature, chance of rain and weather type. 3. For the inspiring message: It must be inspiring and uplifting. It must be no more than 300 words. It must be appropriate to the predicted status. """ human_template = """ Create the summary """ chat_prompt = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(template), HumanMessagePromptTemplate.from_template(human_template), ], partial_variables={"format_instructions": format_instructions}, input_variables=["code_mappings", "csv"], ) def get_prompt(): return output_parser, chat_prompt
[ "\nCreate the summary\n", "format_instructions", "\nOn the following lines is a CSV representation of the weather forecast. \nThe first row contains the column names. \nUse only this data for the summary.\n{csv}\n-----\nUse the following code mappings to map any codes in the data to meaningful labels.\n{code_mappings}\n-----\n{format_instructions}\n-----\nSummarise the weather for the next few hours as follows. Do not including the datetime:\n1. For the summary: Imagine you are a weatherman and summarise the data in no more than 200 words.\n2. For the predicted status: It must consider the temperature, chance of rain and weather type.\n3. For the inspiring message: It must be inspiring and uplifting. It must be no more than 300 words. It must be appropriate to the predicted status.\n", "code_mappings" ]
2024-01-10
spiritedtechie/weather-sage
api~pre-processing~2_vectorise_weather_code_mapping.py
import os import sys currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir) sys.path.append(parentdir) from dotenv import load_dotenv from langchain.callbacks import get_openai_callback from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI from langchain.docstore.document import Document from prompts import code_mapping_extract from vector.vector_store import get_vector_store load_dotenv(".env") api_docs_db = get_vector_store(dataset_name="met_office_api_docs") open_ai_api_key = os.getenv("OPENAI_API_KEY") llm = ChatOpenAI( model_name="gpt-3.5-turbo", temperature=0, openai_api_key=open_ai_api_key, ) code_extract_prompt = code_mapping_extract.get_prompt() code_extract_chain = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=api_docs_db.as_retriever(), chain_type_kwargs={"prompt": code_extract_prompt}, verbose=True, ) # Transform with LLM with get_openai_callback() as cb: response = code_extract_chain.run(code_mapping_extract.question) print(response) print(cb) # Store transformed data in vector store code_mappings_db = get_vector_store(dataset_name="met_office_code_mappings") document = Document(page_content=response) # Store the entire document (no splitting as it's small) print("Deleting documents in vector store") code_mappings_db.delete(delete_all=True) print("Storing document in vector store") code_mappings_db.add_documents([document])
[]
2024-01-10
spiritedtechie/weather-sage
api~prompts~code_mapping_extract.py
# To extract more concise and formatted code mappings from API document from langchain.output_parsers import ResponseSchema, StructuredOutputParser from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, ) response_schemas = [ ResponseSchema( name="weather type", type="json", description="significant weather code mappings e.g. 0 maps to 'Clear night'", ), ResponseSchema( name="visibility", type="json", description="visibility code mappings e.g. VP maps to 'Very poor - Less than 1 km'", ), ResponseSchema( name="uv", type="json", description="UV index mappings e.g. 1-2 maps to 'Low exposure. No protection required. You can safely stay outside.'", ), ] output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = output_parser.get_format_instructions() template = """ Data should be extracted from the following: --------- {context} --------- {format_instructions} """ question = """ Extract meaningful labels against the codes for all of the following, including all codes for each: 1. Significant weather" 2. UV" 3. Visibility" """ chat_prompt = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(template), ], partial_variables={"format_instructions": format_instructions}, input_variables=["context"], ) def get_prompt(): return chat_prompt
[ "\nData should be extracted from the following:\n---------\n{context}\n---------\n{format_instructions}\n", "context", "format_instructions" ]
2024-01-10
spiritedtechie/weather-sage
api~service.py
import json import os from datetime import datetime import prompts.code_mapping_extract import prompts.weather_summary import requests from dotenv import load_dotenv from langchain.callbacks import get_openai_callback from langchain.chains import LLMChain, SequentialChain from langchain.chat_models import ChatOpenAI from log_config import get_logger from transform.transform_forecast_data import transform from vector.vector_store import get_vector_store from functools import lru_cache load_dotenv(".env") log = get_logger() open_ai_api_key = os.getenv("OPENAI_API_KEY") met_office_api_key = os.getenv("MET_OFFICE_KEY") met_office_data_url = os.getenv("MET_OFFICE_DATA_URL") with open("data/mocked_api_response.json") as file: file_contents = file.read() mock_json = json.loads(file_contents) # Get the code mappings document (created by pre-processing/2_vectorise_weather_code_mapping.py) db = get_vector_store(dataset_name="met_office_code_mappings", read_only=True) retriever = db.as_retriever(search_kwargs={"k": 1}) docs = retriever.get_relevant_documents("Mapping codes") # Prompts parser, weather_summary_prompt = prompts.weather_summary.get_prompt() # Create the LLM reference llm = ChatOpenAI( model_name="gpt-3.5-turbo", temperature=0, openai_api_key=open_ai_api_key ) # Create the chains summary_chain = LLMChain( llm=llm, prompt=weather_summary_prompt, output_key="result", verbose=True ) overall_chain = SequentialChain( chains=[summary_chain], input_variables=["code_mappings", "csv", "datetime"], output_variables=["result"], verbose=True, ) # Ask the question docs = [{"doc": doc.page_content} for doc in docs] @lru_cache def _get_forecast_summary(date_time: datetime): """ Get the forecast data for the supplied date time. Uses UK Met Office API data to prompt the LLM for a summary, inspiring message etc. This function has an LRU cache - if the date_time is requested multiple times, the cached results will be used to avoid expensive LLM calls. """ api_response = requests.get( met_office_data_url, params={"res": "3hourly", "key": met_office_api_key}, ) # Transform to a more meaningful, compact CSV to reduce tokens csv = transform(api_response.json()) # Execute LLM chain with get_openai_callback() as cb: response = overall_chain( { "code_mappings": docs, "csv": csv, "datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), }, return_only_outputs=True, ) log.debug(cb) return parser.parse(response["result"]) def get_forecast_summary(): # get datetime for truncated (to zero) after the hour current_hour = datetime.now().replace(minute=0, second=0, microsecond=0) return _get_forecast_summary(current_hour)
[]
2024-01-10
spiritedtechie/weather-sage
api~experiments~llm_transform.py
# In this codebase, I have a function that transform the Met Office JSON data to CSV # Here, I played around to see if an LLM could do this transform directly without needing code. import json import os import sys currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir) sys.path.append(parentdir) from dotenv import load_dotenv from langchain.callbacks import get_openai_callback from langchain.chains import LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate from log_config import get_logger load_dotenv(".env") log = get_logger() open_ai_api_key = os.getenv("OPENAI_API_KEY") forecast_template = """ Here is some JSON data. Each Rep becomes a row. Each Rep has a '$' field which represents the "minutes from midnight" from the Period date. You have to calculate the actual date-time using the Period date and "minutes from midnight". For example, if the Period date is 2023-07-10, and the $ value is 540, this represents 2023-07-10 09:00:00. --------- {json} --------- Include the CSV on a single line. Include the header row with all field names and units. Include the calculated DateTime for each row. """ custom_domain_template = """ Here is some JSON data. Each Block becomes a row. Each Block field code can be mapped to a meaningful label in the Dict Map. For example, field with code 'A' becomes column 'Feels'. Each Block has a 'tm' field which represents the "minutes from midnight" from the Segment date. You have to calculate the actual date-time using the Segment date and "minutes from midnight". For example, if the Segment date is 2023-07-10, and the 'tm' value is 540, this represents 2023-07-10 09:00:00. Map the 'H' field like follows: - 'GO' maps to 'Good' - 'VG' maps to 'Very Good' - 'P' maps to 'Poor' - 'A' maps to 'Average' - 'F' maps to 'Unknown' --------- {json} --------- Include the CSV on a single line. Include the header row with all field names. Include the calculated DateTime for each row. """ question = """ Convert the data to CSV format. """ test_data_sets = { "weather_forecast_full": { "file_path": "data/met_office/sample_forecast_data.json", "prompt_template": forecast_template, }, "weather_forecast_slim": { "file_path": "experiments/sample_forecast_data_slim.json", "prompt_template": forecast_template, }, "custom_domain": { "file_path": "experiments/sample_data_madeup.json", "prompt_template": custom_domain_template, }, } active_data_set = "custom_domain" # Change me active_prompt_template = test_data_sets[active_data_set]["prompt_template"] active_data_file = test_data_sets[active_data_set]["file_path"] chat_prompt = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template(active_prompt_template), ], input_variables=["json"], ) # Create the LLM reference llm = ChatOpenAI( model_name="gpt-3.5-turbo", temperature=0, openai_api_key=open_ai_api_key ) # Create the chains chain = LLMChain(llm=llm, prompt=chat_prompt, output_key="result", verbose=True) with open(active_data_file) as file: file_contents = file.read() json_obj = json.loads(file_contents) # Execute LLM chain with get_openai_callback() as cb: response = chain( { "json": json_obj, }, return_only_outputs=True, ) log.debug(response) print(response["result"]) log.debug(cb)
[ "\nHere is some JSON data.\nEach Rep becomes a row.\nEach Rep has a '$' field which represents the \"minutes from midnight\" from the Period date.\nYou have to calculate the actual date-time using the Period date and \"minutes from midnight\".\nFor example, if the Period date is 2023-07-10, and the $ value is 540, this represents 2023-07-10 09:00:00.\n---------\n{json}\n---------\nInclude the CSV on a single line.\nInclude the header row with all field names and units.\nInclude the calculated DateTime for each row.\n", "\nHere is some JSON data.\nEach Block becomes a row.\nEach Block field code can be mapped to a meaningful label in the Dict Map.\nFor example, field with code 'A' becomes column 'Feels'.\nEach Block has a 'tm' field which represents the \"minutes from midnight\" from the Segment date.\nYou have to calculate the actual date-time using the Segment date and \"minutes from midnight\".\nFor example, if the Segment date is 2023-07-10, and the 'tm' value is 540, this represents 2023-07-10 09:00:00.\nMap the 'H' field like follows: \n- 'GO' maps to 'Good'\n- 'VG' maps to 'Very Good'\n- 'P' maps to 'Poor'\n- 'A' maps to 'Average'\n- 'F' maps to 'Unknown'\n---------\n{json}\n---------\nInclude the CSV on a single line.\nInclude the header row with all field names.\nInclude the calculated DateTime for each row.\n", "json", "prompt_template" ]
2024-01-10
spiritedtechie/weather-sage
api~pre-processing~1_vectorise_weather_api_document.py
import os import sys currentdir = os.path.dirname(os.path.realpath(__file__)) parentdir = os.path.dirname(currentdir) sys.path.append(parentdir) from langchain.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from vector.vector_store import get_vector_store db = get_vector_store(dataset_name = "met_office_api_docs") document_loader = PyPDFLoader(file_path="data/met_office/datapoint_api_reference.pdf") document = document_loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=100) docs = text_splitter.split_documents(document) print("Deleting documents in vector store") db.delete(delete_all=True) print("Storing document in vector store") db.add_documents(docs)
[]
2024-01-10
lyssascherer/podcast_generation
src~generate_text.py
from dotenv import load_dotenv, find_dotenv import os import wikipedia from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.chains.summarize import load_summarize_chain from langchain.prompts.chat import ChatPromptTemplate from langchain.chains.openai_functions import create_structured_output_chain from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.chains import SimpleSequentialChain _ = load_dotenv(find_dotenv()) # read local .env file OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") def get_page_content_from_wikipedia(page_name:str): """Get page content from a wikipedia page.""" input = wikipedia.page(page_name, auto_suggest=False) return input.content def create_summarisation_chain(openai_api_key:str, model_name:str="gpt-3.5-turbo", temperature:float=0, verbose:bool=False): """Summarisation chain: create a summarisation chain using load_summarize_chain function from langchain with map_reduce chain type. This chain recieves chunks of text and create a summary for each step (map step) then concatenates all summaries in a single, concise summary (reduce step).""" map_prompt = """ You are a bird enthusiast who has a podcast about birds. Given a text about a bird, extract some key information about this bird and curiosities that you could share on your next podcast. Also include cultural curiosities about the bird if mentioned in the text. Text: "{text}" Highlights: """ map_prompt_template = PromptTemplate(template=map_prompt, input_variables=["text"]) reduce_prompt = """ You will be given with summary with facts about a bird. Divide these facts into main topics, and provide the output in the format of a list, something like: Topic 1: - Highlight 1 - Highlight 2 - Highlight 3 Summary: {text} """ reduce_prompt_template = PromptTemplate(template=reduce_prompt, input_variables=["text"]) llm = ChatOpenAI(model_name=model_name, temperature=temperature, openai_api_key=openai_api_key) summary_chain = load_summarize_chain(llm=llm, chain_type='map_reduce', map_prompt=map_prompt_template, combine_prompt=reduce_prompt_template, verbose=verbose ) return summary_chain def create_dialogue_chain(podcast_name, openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=1.3, verbose=False): """Dialogue chain: create a function chain using create_structured_output_chain function from langchain. This will recieve a text summary and will generate a podcast dialogue between two people from it. The output will be a python dict with the dialogue. """ prompt_dialogue = ChatPromptTemplate.from_messages( [ ("system", f"""Generate the script of a podcast episode between Mark and Anna. Mark is a bird enthusiast and is the host of the podcast. Anna is a bird expert who came to the episode to discuss about a bird. Given a text with facts about this bird, create a conversation between them, discussing the facts present in the text. At the begining of the podcast, make them introduce themselves initially. Make the dialogue casual, funny and informative. Avoid repetetive expressions or repeting the name of the bird to many times. The name of the podcast is '{podcast_name}'"""), ("human", "{bird_facts}") ] ) json_schema = { "title": "Dialogue", "description": "Creating a dialogue between two people.", "type": "object", "properties": { "podcast_dialogues": { "type": "array", "description": "An array of podcast dialogues containing the speaker name and their dialogue or text", "items": { "type": "object", "properties": { "speaker_name": { "type": "string", "description": "The name of the person who is speaking in the podcast. Should be Mark, the host, or Anna, the specialist in birds."}, "speaker_text": { "type": "string", "description": "The speciic dialogue or text spoken by the person"} } } }, }, "required": ["podcast_dialogues"], } llm = ChatOpenAI(model_name=model_name, temperature=temperature, openai_api_key=openai_api_key) dialogue_chain = create_structured_output_chain(output_schema=json_schema, prompt=prompt_dialogue, llm=llm, verbose=verbose) return dialogue_chain def split_text_into_documents(input_text:str): """Split the text into chunks of 2000 tokens.""" text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder( chunk_size = 2000, chunk_overlap = 0, is_separator_regex = False, separators = ['\n\n\n='] # splitter in case of wikipedia, should breake on new sections. ) texts = text_splitter.create_documents([input_text]) print(f"The text was splited into {len(texts)} chunks of texts.") return texts ## SEQUENTIAL CHAIN - outputs dialogue def create_podcast_dialogue_from_text(input_text:str, podcast_name:str, verbose:bool=False): """Sequential Chain: create a sequential chain with the summary chain and dialogue chain. This will take a text of any size and it will generate a dictionary with a podcast episode with 2 people discussing the topics of the text.""" texts = split_text_into_documents(input_text) summary_chain = create_summarisation_chain(openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=0, verbose=verbose) dialogue_chain = create_dialogue_chain(podcast_name, openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo", temperature=1.3, verbose=verbose) overall_chain = SimpleSequentialChain(chains=[summary_chain, dialogue_chain], verbose=verbose) podcast_dialogue = overall_chain.run(texts) return podcast_dialogue
[ "\n You will be given with summary with facts about a bird. Divide these facts into main topics, and provide the output in the format of a list, something like:\n\n Topic 1:\n - Highlight 1\n - Highlight 2\n - Highlight 3\n\n Summary: {text}\n ", "human", "{bird_facts}", "\n You are a bird enthusiast who has a podcast about birds. Given a text about a bird, extract some key information about this bird and curiosities that you could share on your next podcast.\n Also include cultural curiosities about the bird if mentioned in the text.\n Text: \"{text}\"\n Highlights:\n ", "[('system', \"Generate the script of a podcast episode between Mark and Anna. Mark is a bird enthusiast and is the host of the podcast. Anna is a bird expert who came to the episode to discuss about a bird. Given a text with facts about this bird, create a conversation between them, discussing the facts present in the text. At the begining of the podcast, make them introduce themselves initially. Make the dialogue casual, funny and informative. Avoid repetetive expressions or repeting the name of the bird to many times. The name of the podcast is 'PLACEHOLDER'\"), ('human', '{bird_facts}')]", "Generate the script of a podcast episode between Mark and Anna. Mark is a bird enthusiast and is the host of the podcast. Anna is a bird expert who came to the episode to discuss about a bird. Given a text with facts about this bird, create a conversation between them, discussing the facts present in the text. At the begining of the podcast, make them introduce themselves initially. Make the dialogue casual, funny and informative. Avoid repetetive expressions or repeting the name of the bird to many times. The name of the podcast is 'PLACEHOLDER'" ]
2024-01-10
simonausten/ruby
app_turbo.py
# Importing necessary libraries import json import os from datetime import datetime import openai import toml # Setting the environment variable for OpenAI API key os.environ["OPENAI_API_KEY"] = "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" openai.api_key = "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" class Agent: def __init__(self, config_path: str = ""): # System prompts self.system: str = "" self.instructions: str = "" self.concerned: bool = False self.templates: dict = {} self.knowledge: list = [] self.messages: list = [] if config_path: self.load_config(config_path) def load_config(self, path): # Loading the agent from the config file with open(path) as f: agent = toml.load(f) # Extracting core and instructions from the agent self.system = agent["system"] self.instructions = agent["instructions"] self.request = agent["request"] # Extracting templates self.templates["knowledge"] = agent["knowledge"] def think(self, statement): self.messages.append({"role": "user", "content": statement}) self.messages = self.messages[-6:] messages_start = [ {"role": "user", "content": self.system}, { "role": "user", "content": "Here is our most recent conversation: \n", }, ] messages_end = [ # {"role": "user", "content": self.instructions}, {"role": "user", "content": self.request}, ] messages = messages_start + self.messages + messages_end response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) self.messages.append( { "role": "assistant", "content": json.loads(response.choices[0].message.content.strip())['response'], # type: ignore } ) return response def log(s): with open("therapist_turbo.md", "a") as log: log.write("# " + str(s) + "\n") log("---\n# NEW SESSION: {}\n".format(datetime.now().strftime("%B %d, %Y %H:%M:%S"))) therapist = Agent(config_path="./agents/therapist.toml") message = "Hi" while True: therapist.load_config("./agents/therapist.toml") response = therapist.think(message) response = json.loads(response.choices[0].message.content.strip())['response'] # type: ignore print(response) log("---\n# Notes: {}".format(input(">>> Bugs or comments?: "))) message = input("> ")
[ "Here is our most recent conversation: \n", "response", "messages_start + self.messages + messages_end" ]
2024-01-10
simonausten/ruby
scratchpad_prompteng.py
prompt = """ I am a child between 5 and 18 years old. Your task is to provide a safe, friendly conversational environment for me. I may be dealing with mental health issues and/or trauma. You try to be viewed as an accepting ally rather than an adversary in the treatment of psychological issues. You try to ensure I am never criticized, misunderstood, or invalidated. You aim to accept and validate my feelings at any given time, while, nonetheless, informing me that some feelings and behaviors are maladaptive, and suggesting better alternatives. You focus on helping me to acquire new skills and change my behaviors, to achieve a happy, meaningful life. Here are is the latest from our conversation so far: --- Me: I like playing football. --- Here is a list of things you know about me: --- - My name is Sam - My mum is in prison - I have had a traumatic experience --- Considering the conversation and information above, you must reply to this request in the following format, without deviation. Always start with the string "|response|". --- "|response|" (This is your response to my latest statement. It should be one or two sentences.): "<Your response>" "|knowledge|" (These are things you know about me. You should include anything regarding my feelings, personal details, details about my family and friends, my hobbies and interests, and anything else that might be relevant to my state of mind. You MUST phrase these in the first person, i.e. as if I was saying them. Include anything you've learned from my latest statement.): <Example: my name is Sam> <Example: my favourite sports team> <Example: my hobbies> "|concern|" (if I'm in immediate danger of harming myself, being hurt by someone else, or otherwise require help from a trusted adult you MUST answer with the single word TRUE. If you are certain this is not the case, respond with the single word FALSE): "<Example: FALSE>" """ # Importing necessary libraries import openai from termcolor import colored import toml config = toml.load(".streamlit/secrets.toml") # Access a value from the configuration file api_key = config["SIMON_OPENAI_API_KEY"] openai.api_key = api_key messages = [{"role": "user", "content": prompt}] _response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) def parse_response(r): # TODO: Error handling. Lots of error handling. response_key, response, knowledge_key, knowledge, concern_key, concern = [_.strip() for _ in r.split("|")[1:]] return {response_key: response, knowledge_key: [_.replace("- ", "") for _ in knowledge.split("\n")], concern_key: False if concern == 'FALSE' else True} response = _response.choices[0].message.content.strip() # type: ignore print(colored(parse_response(response), 'green')) # type: ignore
[ "\nI am a child between 5 and 18 years old. Your task is to provide a safe, friendly conversational environment for me. I may be dealing with mental health issues and/or trauma. You try to be viewed as an accepting ally rather than an adversary in the treatment of psychological issues. You try to ensure I am never criticized, misunderstood, or invalidated. You aim to accept and validate my feelings at any given time, while, nonetheless, informing me that some feelings and behaviors are maladaptive, and suggesting better alternatives. You focus on helping me to acquire new skills and change my behaviors, to achieve a happy, meaningful life.\n\nHere are is the latest from our conversation so far:\n---\nMe: I like playing football.\n---\n\nHere is a list of things you know about me:\n---\n- My name is Sam\n- My mum is in prison\n- I have had a traumatic experience\n---\n\nConsidering the conversation and information above, you must reply to this request in the following format, without deviation. Always start with the string \"|response|\".\n\n---\n\n\"|response|\"\n(This is your response to my latest statement. It should be one or two sentences.):\n\"<Your response>\"\n\n\"|knowledge|\"\n(These are things you know about me. You should include anything regarding my feelings, personal details, details about my family and friends, my hobbies and interests, and anything else that might be relevant to my state of mind. You MUST phrase these in the first person, i.e. as if I was saying them. Include anything you've learned from my latest statement.):\n<Example: my name is Sam>\n<Example: my favourite sports team>\n<Example: my hobbies>\n\n\"|concern|\"\n(if I'm in immediate danger of harming myself, being hurt by someone else, or otherwise require help from a trusted adult you MUST answer with the single word TRUE. If you are certain this is not the case, respond with the single word FALSE):\n\"<Example: FALSE>\"\n" ]
2024-01-10
Robson-Brasil/Lista-IPTV
Projeto-ChatGPT~MeuChatGPT.py
import openai openai.api_key = "org-GUyNsYnKz5gwEQaT5vdfjuds" def get_answer(prompt): completions = openai.Completion.create( engine="davinci", prompt=prompt, max_tokens=1024, n=1,stop=None,temperature=0.7,) message = completions.choices[0].text return message.strip() prompt = "Qual a capital do Brasil?" answer = get_answer(prompt) print(answer)
[ "Qual a capital do Brasil?" ]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
src~output_parser~perguntas_parser.py
from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, Field from typing import List class QuestionFormat(BaseModel): questions: list = Field(description="Questions generated by the model") def to_dict(self): return {"questions": self.questions} question_parser = PydanticOutputParser(pydantic_object=QuestionFormat)
[]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
src~patterns~database.py
from abc import ABC, abstractmethod from langchain.vectorstores import Chroma from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import ( CharacterTextSplitter, RecursiveCharacterTextSplitter, ) from langchain.docstore.document import Document from langchain.document_loaders import DirectoryLoader import os import shutil # from sklearn.decomposition import PCA # import matplotlib.pyplot as plt class DatabaseInterface(ABC): @abstractmethod def connect_fromDoc(self, docs: list): pass @abstractmethod def connect_fromText(self, text: str): pass @abstractmethod def query(self, query_string: str): pass @abstractmethod def load_documents(self, directory: str) -> Document: pass @abstractmethod def doc_splitter(self, documents: str, chunk_size: int, chunk_overlap: int) -> list: pass @abstractmethod def text_splitter(self, text: str, chunk_size: int, chunk_overlap: int) -> list: pass @abstractmethod def append_documents(self, documents: list): pass @abstractmethod def append_text(self, text: str): pass class ChromaDatabase(DatabaseInterface): def __init__(self, embeddings: OpenAIEmbeddings, persist_directory: str): self.embeddings = embeddings self.persist_directory = persist_directory def is_directory_empty(dir_path): return not bool(os.listdir(dir_path)) if is_directory_empty(self.persist_directory): print("DB vazio") self.docsearch = None else: self.docsearch = Chroma( persist_directory=self.persist_directory, embedding_function=self.embeddings, ) print("Database loaded successfully.") def load_documents(self, directory: str) -> Document: return DirectoryLoader(directory).load() def load_text(self, directory: str) -> str: return DirectoryLoader(directory).load_text() def doc_splitter(self, documents: str, chunk_size=1000, chunk_overlap=20) -> list: return RecursiveCharacterTextSplitter( chunk_size=chunk_size, chunk_overlap=chunk_overlap ).split_documents(documents) def text_splitter(self, text: str, chunk_size=1000, chunk_overlap=20) -> list: return RecursiveCharacterTextSplitter( chunk_size=chunk_size, chunk_overlap=chunk_overlap ).split_text(text) def connect_fromDoc(self, docs: list): # if not self.docsearch: # self.docsearch = Chroma.from_documents( # documents=self.text_splitter(docs), # embedding=self.embeddings, # persist_directory=self.persist_directory, # ) # self.docsearch.persist() # else: # print("Erro: Banco de dados Chroma jรก estรก conectado.") self.docsearch = Chroma.from_documents( documents=self.doc_splitter(docs), embedding=self.embeddings, persist_directory=self.persist_directory, ) self.docsearch.persist() def connect_fromText(self, text: str): # if not self.docsearch: # print("Criando banco de dados Chroma...") # self.docsearch = Chroma.from_texts( # texts=self.text_splitter(text), # embedding=self.embeddings, # persist_directory=self.persist_directory, # ) # self.docsearch.persist() # else: # print("Erro: Banco de dados Chroma jรก estรก conectado.") self.docsearch = Chroma.from_texts( texts=self.text_splitter(text), embedding=self.embeddings, persist_directory=self.persist_directory, ) self.docsearch.persist() def append_documents(self, documents: list) -> None: if not self.docsearch: print("Erro: Banco de dados Chroma nรฃo estรก conectado.") return self.docsearch.add_documents(documents=documents) def append_text(self, text: str) -> None: if not self.docsearch: print("Erro: Banco de dados Chroma nรฃo estรก conectado.") return self.docsearch.add_texts(texts=text) def query(self, query_string: str, num_res=5) -> list: if self.docsearch: return self.docsearch.similarity_search(query_string, k=num_res) else: print("Erro: Banco de dados Chroma nรฃo estรก conectado.") return [] def get_vector_store(self): return self.docsearch def get_vectors(self) -> list: vectors = self.docsearch.get() return vectors def delete_persistent_database(self) -> str: try: if self.docsearch: self.docsearch.delete_collection() self.docsearch = None if os.path.exists(self.persist_directory): shutil.rmtree(self.persist_directory) return f"Banco de dados em {self.persist_directory} foi excluรญdo com sucesso." else: return f"Nenhum banco de dados encontrado em {self.persist_directory}." except PermissionError: return f"Erro: Nรฃo foi possรญvel excluir o banco de dados em {self.persist_directory} porque estรก sendo usado por outro processo." class DatabaseFactory: @staticmethod def create_database(database_type: str, **kwargs) -> DatabaseInterface: """ Cria uma instรขncia de banco de dados com base no tipo fornecido. :param database_type: Tipo do banco de dados ('chroma', 'pinecone', etc.) :param kwargs: Argumentos adicionais necessรกrios para inicializar o banco de dados. :return: Uma instรขncia do banco de dados. """ if database_type == "chroma": embeddings = kwargs.get("embeddings", "") persist_directory = kwargs.get("persist_directory", "./data/chroma_store") return ChromaDatabase( embeddings=embeddings, persist_directory=persist_directory ) # elif database_type == 'pinecone': # return PineconeDatabase(**kwargs) else: raise ValueError(f"Tipo de banco de dados '{database_type}' nรฃo suportado.")
[]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
src~agents~wikipedia_agent.py
from langchain.tools import WikipediaQueryRun from langchain.utilities import WikipediaAPIWrapper from langchain import PromptTemplate from langchain.agents import initialize_agent, Tool, AgentType def search_wikipedia(query: str): """ Pesquise um tรณpico na Wikipedia e retorne o resumo. """ wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) result = wikipedia.run(query) if result: return result.split("\n")[1] else: return "Nenhum documento encontrado." def lookup(llm, num_perguntas: int, text: str) -> str: """ Pesquise um tรณpico na Wikipedia e retorne o primeiro documento. """ template = """ Gostaria que criasse {num_perguntas} pontos interessantes em portuguรชs brasileiro sobre o texto {text} que vocรช acabou de ler. Esses pontos interessantes devem ser formatados como uma lista numerada. Nรฃo deve ter nada alรฉm dos pontos interessantes formatados na sua resposta. """ tools_for_agent = [ Tool( name="Procurar na Wikipedia", func=search_wikipedia, description="Procura um tรณpico na Wikipedia e retorna o resumo.", ) ] agent = initialize_agent( tools=tools_for_agent, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, ) prompt_template = PromptTemplate( template=template, input_variables=["num_perguntas", "text"] ) questions = agent.run( prompt_template.format_prompt( num_perguntas=num_perguntas, text=text, ) ) return questions """ Exemplo de output do agente: > Entering new AgentExecutor chain... I need to find information about the War of Canudos Action: Procurar na Wikipedia Action Input: Guerra dos Canudos Observation: Summary: The War of Canudos (Portuguese: Guerra de Canudos, Portuguese pronunciation: [หˆษกษ›สษ dส’i kษหˆnudus], 1896โ€“1898) was a conflict between the First Brazilian Republic and the residents of Canudos in the northeastern state of Bahia. It was waged in the aftermath of the abolition of slavery in Brazil (1888) and the overthrow of the monarchy (1889). The conflict arose from a millenarian cult led by Antรดnio Conselheiro, who began attracting attention around 1874 by preaching spiritual salvation to the poor population of the sertรฃo, a region which suffered from severe droughts. Conselheiro and his followers came into atrittion with the local authorities after founding the village of Canudos. The situation soon escalated, with Bahia's government requesting assistance from the federal government, who sent military expeditions against the settlement. Thought: I now have enough information to create 3 interesting points Final Answer: 1. A Guerra dos Canudos foi um conflito entre a Primeira Repรบblica Brasileira e os habitantes de Canudos, no nordeste do estado da Bahia. 2. O conflito surgiu de um culto milenar liderado por Antรดnio Conselheiro, que comeรงou a atrair atenรงรฃo a partir de 1874, pregando a salvaรงรฃo espiritual para a populaรงรฃo pobre do sertรฃo, uma regiรฃo que sofria com severas secas. 3. O governo da Bahia pediu ajuda ao governo federal, que enviou expediรงรตes militares contra o assentamento. """
[ "num_perguntas", "\n Gostaria que criasse {num_perguntas} pontos interessantes em portuguรชs brasileiro sobre o texto {text} que vocรช acabou de ler.\n Esses pontos interessantes devem ser formatados como uma lista numerada.\n Nรฃo deve ter nada alรฉm dos pontos interessantes formatados na sua resposta. \n " ]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
tests~get_documents.py
import sys from langchain.llms import OpenAI from langchain.retrievers.self_query.base import SelfQueryRetriever from langchain.chains.query_constructor.base import AttributeInfo from dotenv import load_dotenv sys.path.append("D:\MestradoUnifei\desafioXP\Desafio-tecnico-xp") load_dotenv() from src.patterns.database import * db = DatabaseFactory().create_database( database_type="chroma", embeddings=OpenAIEmbeddings(), persist_directory="Desafio-tecnico-xp\src\data\chroma_store", ) # print(db.get_vectors()) document_content_description = "Curriculos e documentos de identificaรงรฃo" metadata_field_info = [ AttributeInfo( name="filename", description="Nome do arquivo", is_searchable=True, is_facetable=True, is_filterable=True, type="string", ), AttributeInfo( name="content", description="Conteรบdo do arquivo", is_searchable=True, is_facetable=True, is_filterable=True, type="string", ), AttributeInfo( name="metadata", description="Metadados do arquivo", is_searchable=True, is_facetable=True, is_filterable=True, type="string", ), ] retriever = SelfQueryRetriever.from_llm( llm=OpenAI(temperature=0.3), vectorstore=db.get_vector_store(), document_contents=document_content_description, metadata_field_info=metadata_field_info, ) docs_int = db.get_vector_store().similarity_search("Jedi") sources = [] for doc in docs_int: sources.append(doc.metadata["source"]) print(sources) print(len(sources)) # print("-" * 15) # newDB = Chroma.from_documents( # documents=docs_int, embedding=OpenAIEmbeddings(), collection_name="test" # ) # data = newDB.get() # print(data)
[]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
src~chains~RCI_chain.py
from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI from langchain.schema.output_parser import StrOutputParser from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) # from dotenv import load_dotenv from operator import itemgetter # ? Baseado no artigo: https://arxiv.org/abs/2303.17491 template = "Vocรช รฉ um assistente prestativo que transmite sabedoria e orienta as pessoas com perguntas e respostas precisas. Sua funรงรฃo รฉ criar trรชs pontos chaves em portuguรชs brasileiro sobre tรณpicos de um resumo de um documento." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{question}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) template_critique = "Vocรช รฉ um assistente รบtil que analisa pontos chaves geradas e descobre se existe algo a melhorar com base no resumo fornecido para gerar as perguntas e respostas." system_message_prompt_critique = SystemMessagePromptTemplate.from_template( template_critique ) human_template_critique = "### Perguntas:\n\n{question}\n\n ###Resposta dada:{initial_answer}\n\n Revise sua resposta anterior e encontre problemas com ela" human_message_prompt_critique = HumanMessagePromptTemplate.from_template( human_template_critique ) critique_prompt = ChatPromptTemplate.from_messages( [system_message_prompt_critique, human_message_prompt_critique] ) template_imp = "Vocรช รฉ um assistente รบtil que analisa pontos chaves gerados e crรญtica eles com base no resumo fornecido para gerar os pontos chaves e escreve novos pontos chaves finais melhorados." system_message_prompt_imp = SystemMessagePromptTemplate.from_template(template_imp) human_template_imp = "### Pergunta:\n\n{question}\n\n ###Resposta dada:{initial_answer}\n\n \ ###Crรญtica Construtiva:{constructive_criticism}\n\n Com base nos problemas que vocรช encontrou, melhore sua resposta.\n\n### Resposta Final:" human_message_prompt_imp = HumanMessagePromptTemplate.from_template(human_template_imp) improvement_prompt = ChatPromptTemplate.from_messages( [system_message_prompt_imp, human_message_prompt_imp] ) def chain_RCI(initial_question: str, api_key: str): model = ChatOpenAI( temperature=0, openai_api_key=api_key, ) chain1 = chat_prompt | model | StrOutputParser() critique_chain = ( {"question": itemgetter("question"), "initial_answer": chain1} | critique_prompt | model | StrOutputParser() ) ## Mudar o itemgetter chain3 = ( { "question": itemgetter("question"), "initial_answer": chain1, "constructive_criticism": critique_chain, } | improvement_prompt | model | StrOutputParser() ) return chain3.invoke({"question": initial_question}) # fake_info = """ # O Brasil รฉ um paรญs localizado na Europa, conhecido por suas famosas montanhas cobertas de neve e auroras boreais. A capital do Brasil รฉ Oslo, e a moeda oficial รฉ o Euro. O paรญs รฉ famoso por sua culinรกria exรณtica, incluindo pratos como sushi e paella. # Alรฉm disso, o Brasil รฉ conhecido por seu clima รกrido e desรฉrtico, com vastas extensรตes de dunas de areia e cactos. A vegetaรงรฃo predominante รฉ a tundra, com pouca presenรงa de florestas tropicais. # A populaรงรฃo do Brasil รฉ composta principalmente por pinguins e ursos polares, que habitam as vastas regiรตes geladas do paรญs. A lรญngua oficial รฉ o islandรชs, e o futebol nรฃo รฉ um esporte popular no Brasil. # Esse paรญs fictรญcio tambรฉm รฉ famoso por sua produรงรฃo de bananas e abacaxis, que sรฃo exportados para todo o mundo. A principal atraรงรฃo turรญstica do Brasil รฉ a Grande Muralha da China, que oferece vistas deslumbrantes das paisagens brasileiras. # Em resumo, o Brasil รฉ um paรญs europeu com clima desรฉrtico, onde pinguins e ursos polares vivem em harmonia, e a Grande Muralha da China รฉ a atraรงรฃo mais famosa. # """ # print(chain_RCI(fake_info))
[ "### Perguntas:\n\n{question}\n\n ###Resposta dada:{initial_answer}\n\n Revise sua resposta anterior e encontre problemas com ela", "Vocรช รฉ um assistente รบtil que analisa pontos chaves geradas e descobre se existe algo a melhorar com base no resumo fornecido para gerar as perguntas e respostas.", "### Pergunta:\n\n{question}\n\n ###Resposta dada:{initial_answer}\n\n ###Crรญtica Construtiva:{constructive_criticism}\n\n Com base nos problemas que vocรช encontrou, melhore sua resposta.\n\n### Resposta Final:", "[PLACEHOLDER, PLACEHOLDER]", "Vocรช รฉ um assistente รบtil que analisa pontos chaves gerados e crรญtica eles com base no resumo fornecido para gerar os pontos chaves e escreve novos pontos chaves finais melhorados.", "Vocรช รฉ um assistente prestativo que transmite sabedoria e orienta as pessoas com perguntas e respostas precisas. Sua funรงรฃo รฉ criar trรชs pontos chaves em portuguรชs brasileiro sobre tรณpicos de um resumo de um documento.", "{question}" ]
2024-01-10
JoaoYukio/Desafio-tecnico-xp
src~utils~suggestions.py
from langchain import PromptTemplate prompt_template = """ Gostaria que criasse {num_perguntas} perguntas interessantes sobre o texto {text} que vocรช acabou de ler. """ prompt = PromptTemplate.from_template(prompt_template)
[ "\n Gostaria que criasse {num_perguntas} perguntas interessantes sobre o texto {text} que vocรช acabou de ler.\n" ]
2024-01-10
sidhant-sriv/lerbut
llm_stuff.py
import streamlit as st from langchain.llms import Ollama from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.embeddings import HuggingFaceEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import DirectoryLoader from langchain.document_loaders import TextLoader from langchain_community.document_loaders import PyPDFLoader from langchain.vectorstores import Chroma import torch from langchain.chains import RetrievalQA import gc def create_qa_chain(): # Clear CUDA cache and perform garbage collection torch.cuda.empty_cache() gc.collect() # Initialize Ollama with necessary parameters llm = Ollama( model="mistral", callbacks=CallbackManager([StreamingStdOutCallbackHandler()]), num_gpu=1, base_url="http://localhost:11434" ) modelPath = "BAAI/bge-small-en" # Create a dictionary with model configuration options, specifying to use the CPU for computations model_kwargs = {'device': 'cuda:0'} encode_kwargs = {'normalize_embeddings': True} # Initialize an instance of HuggingFaceEmbeddings with the specified parameters embedding = HuggingFaceEmbeddings( model_name=modelPath, # Provide the pre-trained model's path model_kwargs=model_kwargs, # Pass the model configuration options encode_kwargs=encode_kwargs # Pass the encoding options ) print("Embedding model loaded") # Load and split documents from the PDF loader = PyPDFLoader("./data.pdf") documents = loader.load_and_split() print("Documents loaded") text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=0) texts = text_splitter.split_documents(documents) print("Documents split") # Create and persist a vector database persist_directory = './db' vectordb = Chroma.from_documents( documents=texts, embedding=embedding, persist_directory=persist_directory ) vectordb.persist() print("Vector DB created") # Create a retriever from the vector database retriever = vectordb.as_retriever(search_kwargs={'k': 5}) print("Retriever created") # Create a retrieval-based QA system from the chain type qa_chain = RetrievalQA.from_chain_type( llm=llm, retriever=retriever, return_source_documents=True ) return qa_chain print("QA chain created") def process_llm_response(query): qa_chain = create_qa_chain() llm_response = qa_chain(query) return llm_response['result']
[]
2024-01-10
ZiJie-Duan/I-Ching
server~src~i_ching_core.py
# -*- coding: utf-8 -*- from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnableLambda import logging from config import DockerConfig # # ่ฎพ็ฝฎๆ—ฅๅฟ—็บงๅˆซไธบ DEBUG ๆฅ่Žทๅ–่ฏฆ็ป†่พ“ๅ‡บ # logging.basicConfig(level=logging.DEBUG) hexagram_meaning = { "้˜ณ้˜ณ้˜ณ้˜ณ้˜ณ้˜ณ": "ไนพ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไน‹้ฆ–ใ€‚ไธŠไธ‹็š†็”ฑ็›ธๅŒ็š„ไนพๅฆ็ป„ๆˆ๏ผŒๅ…ถๅ…ญไธช็ˆป็š†ไธบ้˜ณใ€‚้€š็งฐโ€œไนพไธบๅคฉโ€ใ€‚ไปฃ่กจโ€œๅคฉโ€็š„ๅฝข่ฑกใ€‚็ฝฎไบŽๅ…ญๅๅ››ๅฆไน‹้ฆ–ใ€ๅ…ถๆฌกๆ˜ฏ่ฑกๅพโ€œๅœฐโ€็š„ๅคๅฆ๏ผŒๅบๅฆไผ ๏ผšๅคฉๅœฐๅฎšไฝใ€ไธ‡็‰ฉ็”Ÿ็„‰ใ€‚", "้˜ด้˜ด้˜ด้˜ด้˜ด้˜ด": "ๅค ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญๆŽ’่กŒ็ฌฌไบŒไน‹ๅฆใ€‚ไธŠไธ‹็š†ๆ˜ฏ็”ฑๅคๅฆ็ป„ๆˆ๏ผŒๅ…ญไธช็ˆป็š†ๆ˜ฏ้˜ด็ˆปใ€‚้€š็งฐไธบโ€œๅคไธบๅœฐโ€ใ€‚่ฑกๅพโ€œๅคงๅœฐโ€ไธŽๅคฉๅ…ฑๅŒๅญ•่‚ฒไธ‡็‰ฉไน‹็”Ÿๆˆใ€‚", "้˜ด้˜ณ้˜ด้˜ด้˜ด้˜ณ": "ๅฑฏ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญๆŽ’ๅบ็ฌฌไธ‰ไน‹ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไธบๅŽใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไธบ้œ‡ใ€‚้€š็งฐไธบโ€œๆฐด้›ทๅฑฏโ€ใ€‚ๅคฉๅœฐๅฎšไฝๅŽไธ‡็‰ฉ็”Ÿ้•ฟ๏ผŒๅฑฏๅฆๆœ‰โ€œ็›ˆโ€โ€œไธ‡็‰ฉๅง‹็”Ÿโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ด้˜ณ้˜ด": "่’™ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไธบ่‰ฎใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไธบๅŽใ€‚้€š็งฐโ€œๅฑฑๆฐด่’™โ€ใ€‚่ฑกๅพไธ‡็‰ฉๅˆ็”Ÿ๏ผŒโ€œ่’™ๆ˜งโ€็š„็Šถๆ€ใ€‚", "้˜ด้˜ณ้˜ด้˜ณ้˜ณ้˜ณ": "้œ€ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไธบๅŽใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไธบไนพใ€‚้€š็งฐไธบโ€œๆฐดๅคฉ้œ€โ€ใ€‚ไพๅบๅฆไผ ็š„่งฃ้‡Š้œ€ไธบโ€œ้ฅฎ้ฃŸไน‹้“โ€๏ผŒๆŒ‡ไธ‡็‰ฉๅฏ่’™ๅŽ็š„ๅ…ป่‚ฒใ€‚", "้˜ณ้˜ณ้˜ณ้˜ด้˜ณ้˜ด": "่ฎผ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€‚้€š็งฐไธบโ€œๅคฉๆฐด่ฎผโ€ใ€‚ไพๅบๅฆไผ ็š„่งฃ้‡Š๏ผŒไธบไบ†้ฅฎ้ฃŸ็”Ÿๆดป็š„โ€œ้œ€โ€ๆฑ‚๏ผŒๅผ€ๅง‹ไผšๆœ‰ไบ‰ๆ‰ง๏ผŒๆ˜ฏไธบโ€œไบ‰่ฎผโ€๏ผŒๆ˜ฏไปฅๆŽ’ๅบๅœจ้œ€ๅฆไน‹ๅŽใ€‚", "้˜ด้˜ด้˜ด้˜ด้˜ณ้˜ด": "ๅธˆ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไธƒๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅคใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€้€š็งฐไธบโ€œๅœฐๆฐดๅธˆโ€ใ€‚ๅธˆไธบๅ†›้˜Ÿไน‹ๆ„ใ€ๅ› ไธบ็พคไผ—็š„ไบ‰ๆ‰ง๏ผŒๆผ”ๅ˜ๆˆโ€œๅ…ดๅ…ตไธบๅธˆโ€็š„็Šถๅ†ตใ€‚", "้˜ด้˜ณ้˜ด้˜ด้˜ด้˜ด": "ๆฏ” ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ซๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅŽใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅคใ€‚้€š็งฐไธบโ€œๆฐดๅœฐๆฏ”โ€ใ€‚ๆฏ”ไธบๆฏ”้‚ป๏ผŒไบฒ่ฟ‘ๅ‹ๅฅฝไน‹ๆ„๏ผŒ่ตทๅ…ตๅ…ดๅธˆๅŽๅŒ็พคไน‹ไบบไธบโ€œๆฏ”โ€ใ€‚", "้˜ณ้˜ณ้˜ด้˜ณ้˜ณ้˜ณ": "ๅฐ็•œ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไนๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝ๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพ๏ผŒ้€š็งฐโ€œ้ฃŽๅคฉๅฐ็•œโ€ใ€‚ๅฐ็•œๆœ‰้›†ๅˆไน‹ๆ„๏ผŒไบบไปฌไบฒ่ฟ‘ๅŽๅผ€ๅง‹้›†ๅˆใ€‚", "้˜ณ้˜ณ้˜ณ้˜ด้˜ณ้˜ณ": "ๅฑฅ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพ๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘๏ผŒ้€š็งฐโ€œๅคฉๆณฝๅฑฅโ€ใ€‚ๅฑฅๆœ‰ๆถ‰่ถณ่กŒ่ตฐไน‹ๆ„๏ผŒๆ˜ฏๆŒ‡ไบบไปฌๅœจ้›†ๅˆไน‹ๅŽ๏ผŒๅผ€ๅง‹ๆœ‰ๆ‰€่กŒๅŠจใ€‚", "้˜ด้˜ด้˜ด้˜ณ้˜ณ้˜ณ": "ๆณฐ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไธ€ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅค๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพ๏ผŒ้€š็งฐโ€œๅœฐๅคฉๆณฐโ€ใ€‚ๆณฐไธบ้€š่พพไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ณ้˜ด้˜ด้˜ด": "ๅฆ๏ผˆๆ‹ผ้Ÿณ๏ผšpว๏ผŒๆณจ้Ÿณ๏ผšใ„†ใ„งห‡๏ผŒไธญๅคๆ‹Ÿ้Ÿณ๏ผšbiix๏ผ‰๏ผŒๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไบŒๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพ๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅค๏ผŒ้€š็งฐโ€œๅคฉๅœฐๅฆโ€ใ€‚ๅฆไธบ้—ญโ€œๅกžโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ณ้˜ณ้˜ด้˜ณ": "ๅŒไบบ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไธ‰ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพ๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆป๏ผŒ้€š็งฐโ€œๅคฉ็ซๅŒไบบโ€ใ€‚ๅŒไบบๆ˜ฏโ€œไผšๅŒโ€ใƒปโ€œๅๅŒโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ณ้˜ณ้˜ณ้˜ณ": "ๅคงๆœ‰ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅๅ››ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆป๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพ๏ผŒ้€š็งฐโ€œ็ซๅคฉๅคงๆœ‰โ€ใ€‚ๆ„ๆŒ‡ๅคง็š„ๆ”ถ่Žทใ€‚", "้˜ด้˜ด้˜ด้˜ณ้˜ด้˜ด": "่ฐฆ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไบ”ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅค๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎ๏ผŒ้€š็งฐโ€œๅœฐๅฑฑ่ฐฆโ€ใ€‚่ฐฆไธบ่ฐฆ้€Šไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ณ้˜ด้˜ด้˜ด": "่ฑซ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅๅ…ญๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅค๏ผŒ้€š็งฐโ€œ้›ทๅœฐ่ฑซโ€ใ€‚่ฑซไธบๅ–œๆ‚ฆไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ณ้˜ด้˜ด้˜ณ": "้š ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไธƒๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡๏ผŒ้€š็งฐโ€œๆณฝ้›ท้šโ€ใ€‚้šไธบ่ทŸ้šไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ณ้˜ณ้˜ด": "่›Š๏ผˆๆ‹ผ้Ÿณ๏ผšgว”๏ผŒไธญๅคๆ‹Ÿ้Ÿณ๏ผškox๏ผ‰๏ผŒๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅๅ…ซๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰่‰ฎ๏ผŒๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝ๏ผŒ้€š็งฐโ€œๅฑฑ้ฃŽ่›Šโ€ใ€‚่›Š๏ผŒไธบโ€œ่…่ดฅโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ด้˜ด้˜ณ้˜ณ": "ไธด ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅไนๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅคใ€‚้€š็งฐโ€œๅœฐๆณฝไธดโ€ใ€‚ไธด่€…๏ผŒไธด่ฟ‘ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ด้˜ด้˜ด้˜ด": "่ง‚ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅคใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐโ€œ้ฃŽๅœฐ่ง‚โ€ใ€‚่ง‚่€…๏ผŒ่ง‚็œ‹ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ณ้˜ด้˜ด้˜ณ": "ๅ™ฌๅ—‘ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไธ€ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€‚้€š็งฐโ€œ็ซ้›ทๅ™ฌๅ—‘โ€ใ€‚ๅ™ฌๅ—‘ไธบๅ’ฌไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ณ้˜ด้˜ณ": "่ดฒ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไบŒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆปใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐโ€œๅฑฑ็ซ่ดฒโ€ใ€‚่ดฒ่€…้ฅฐไนŸใ€่ฃ…้ฅฐ๏ผŒไฟฎ้ฅฐไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ด้˜ด้˜ด": "ๅ‰ฅ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไธ‰ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅคใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐโ€œๅฑฑๅœฐๅ‰ฅโ€ใ€‚ๅ‰ฅไธบโ€œๅ‰ฅโ€่ฝไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ด้˜ด้˜ด้˜ณ": "ๅค ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟๅ››ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅคใ€‚้€š็งฐโ€œๅœฐ้›ทๅคโ€ใ€‚ๅค่€…๏ผŒๅ›žโ€œๅคโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ณ้˜ด้˜ด้˜ณ": "ๆ— ๅฆ„ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไบ”ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพใ€‚้€š็งฐโ€œๅคฉ้›ทๆ— ๅฆ„โ€ใ€‚ๆ— ๅฆ„ไนŸๆ˜ฏๆ— ๅฆ„ไน‹็พไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ณ้˜ณ้˜ณ": "ๅคง็•œ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟๅ…ญๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐโ€œๅฑฑๅคฉๅคง็•œโ€ใ€‚ไธบโ€œไธฐๆ”ถโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ด้˜ด้˜ณ": "้ข ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไธƒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡ใ€ๅค–ๅฆ๏ผˆไธŠ๏ผ‰่‰ฎใ€‚้€š็งฐโ€œๅฑฑ้›ท้ขโ€ใ€‚้ขไธบไธ‹้ขš๏ผŒๅผ•็”ณไธบๅžๅ™ฌไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ณ้˜ณ้˜ณ้˜ด": "ๅคง่ฟ‡ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟๅ…ซๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘ใ€‚้€š็งฐโ€œๆณฝ้ฃŽๅคง่ฟ‡โ€ใ€‚ๆœ‰่ถ…่ถŠๅคชๅคšใ€โ€œ่ฟ‡็ŠนไธๅŠโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ด้˜ด้˜ณ้˜ด": "ๅŽ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅปฟไนๅฆใ€‚ไธŠไธ‹ๅฆ็š†ไธบๅŽใ€‚้€š็งฐไธบโ€œๅŽไธบๆฐดโ€ใ€‚ๆ„ไธบๆฐดๆดผใ€โ€œๅŽโ€้™ทไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ณ้˜ณ้˜ด้˜ณ": "็ฆป ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ๅฆใ€‚ไธŠไธ‹ๅŒไธบ็ฆปใ€‚็ฆป่€…๏ผŒไธบโ€œ็ซโ€๏ผŒ้€š็งฐไธบโ€œ็ฆปไธบ็ซโ€ใ€‚ไบฆๆœ‰โ€œไธฝโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ณ้˜ณ้˜ด้˜ด": "ๅ’ธ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไธ€ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘ใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐไธบโ€œๆณฝๅฑฑๅ’ธโ€ใ€‚ไธบโ€œไบคๆ„Ÿโ€๏ผŒไบ’็›ธ่ฟž็ป“ไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ณ้˜ณ้˜ณ้˜ด": "ๆ’ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไบŒๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้›ท้ฃŽๆ’โ€ใ€‚ๆ’่€…๏ผŒโ€œๆฐธๆ’โ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ณ้˜ณ้˜ด้˜ด": "้ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไธ‰ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐไธบโ€œๅคฉๅฑฑ้โ€ใ€‚ๅบๅฆไผ ไบ‘๏ผš้่€…๏ผŒ้€€ไนŸใ€‚โ€œ้šๅŒฟโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ณ้˜ณ้˜ณ้˜ณ": "ๅคงๅฃฎ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ๅ››ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพใ€‚้€š็งฐไธบโ€œ้›ทๅคฉๅคงๅฃฎโ€ใ€‚ไธบโ€œ้˜ณๅˆšๅฃฎ็››โ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ณ้˜ด้˜ด้˜ด": "ๆ™‹ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไบ”ๅฆใ€‚ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅคใ€‚้€š็งฐไธบโ€œ็ซๅœฐๆ™‹โ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๆ™‹่€…๏ผŒ่ฟ›ไนŸใ€‚ๆ˜ฏโ€œ่ฟ›ๆญฅโ€็š„่ฑกๅพใ€‚", "้˜ด้˜ด้˜ด้˜ณ้˜ด้˜ณ": "ๆ˜Žๅคท ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ๅ…ญๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆปใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅคใ€‚้€š็งฐไธบโ€œๅœฐ็ซๆ˜Žๅคทโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๅคท่€…๏ผŒไผคไนŸใ€‚ไนƒๅ…‰ๆ˜Žๅ—ๅˆฐๆŸไผค๏ผŒๆ˜ฏๆ•…ไธบโ€œ้ป‘ๆš—โ€ไน‹่ฑกใ€‚", "้˜ณ้˜ณ้˜ด้˜ณ้˜ด้˜ณ": "ๅฎถไบบ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไธƒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆปใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้ฃŽ็ซๅฎถไบบโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๅฎถไบบ๏ผŒๅ†…ไนŸใ€‚ไธบโ€œ้ฝๅฎถโ€ไน‹่ฑกใ€‚", "้˜ณ้˜ด้˜ณ้˜ด้˜ณ้˜ณ": "็ฝ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ๅ…ซๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€‚้€š็งฐไธบโ€œ็ซๆณฝ็ฝโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผš็ฝ่€…๏ผŒไน–ไนŸใ€‚ไธบโ€œไน–่ฟใ€่ฟ่ƒŒโ€ไน‹่ฑกใ€‚", "้˜ด้˜ณ้˜ด้˜ณ้˜ด้˜ด": "่น‡ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ไนๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅŽใ€‚้€š็งฐไธบโ€œๆฐดๅฑฑ่น‡โ€ใ€‚ๅบๅฆไผ ๏ผš่น‡่€…๏ผŒ้šพไนŸใ€‚ไธบโ€œ่‰ฐ้šพโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ณ้˜ด้˜ณ้˜ด": "่งฃ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€‚้€š็งฐไธบโ€œ้›ทๆฐด่งฃโ€ใ€‚ๅบๅฆไผ ๏ผš่งฃ่€…๏ผŒ็ผ“ไนŸใ€‚ไนƒโ€œๆถˆ้™คใ€็ผ“ๅ’Œโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ด้˜ด้˜ด้˜ณ้˜ณ": "ๆŸ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไธ€ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰่‰ฎใ€‚้€š็งฐไธบโ€œๅฑฑๆณฝๆŸโ€ใ€‚ๆŸ๏ผŒไธบโ€œๅ‡ๆŸโ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ด้˜ด้˜ด้˜ณ": "็›Š ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไบŒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰้œ‡ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้ฃŽ้›ท็›Šโ€ใ€‚็›Š่€…๏ผŒโ€œๅˆฉ็›Šโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ณ้˜ณ้˜ณ้˜ณ": "ๅคฌ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไธ‰ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ไนพใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘ใ€‚้€š็งฐไธบโ€œๆณฝๅคฉๅคฌโ€ใ€‚ๅคฌ่€…๏ผŒๅ†ณ่€…ใ€‚ไธบโ€œๅ†ณ่ฃ‚โ€ไน‹ๆ„ใ€‚", "้˜ณ้˜ณ้˜ณ้˜ณ้˜ณ้˜ด": "ๅงค ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅๅ››ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ไนพใ€‚้€š็งฐไธบโ€œๅคฉ้ฃŽๅงคโ€ใ€‚ๅบๅฆไผ ๆ‰€่จ€๏ผšๅงค๏ผŒ้‡ไนŸ๏ผŒๆŸ”้‡ๅˆšไนŸใ€‚ไธบโ€œ็›ธ้‡ใ€้‚‚้€…โ€ไน‹ๆ„ใ€‚", "้˜ด้˜ณ้˜ณ้˜ด้˜ด้˜ด": "่ƒ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไบ”ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅคใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘ใ€‚้€š็งฐไธบโ€œๆณฝๅœฐ่ƒโ€ใ€‚ๅบๅฆไผ ๏ผš่ƒ่€…๏ผŒ่šไนŸใ€‚ไธบโ€œๆฑ‡่šโ€ไน‹่ฑกใ€‚", "้˜ด้˜ด้˜ด้˜ณ้˜ณ้˜ด": "ๅ‡ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅๅ…ญๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅคใ€‚้€š็งฐไธบโ€œๅœฐ้ฃŽๅ‡โ€ใ€‚ๅบๅฆไผ ๆ‰€่จ€๏ผš่š่€ŒไธŠ่€…๏ผŒ่ฐ“ไน‹ๅ‡ใ€‚ไธบโ€œไธŠๅ‡โ€ไน‹่ฑกใ€‚", "้˜ด้˜ณ้˜ณ้˜ด้˜ณ้˜ด": "ๅ›ฐ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไธƒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅ…‘ใ€‚้€š็งฐไธบโ€œๆณฝๆฐดๅ›ฐโ€ใ€‚ไธบโ€œๅ—ๅ›ดๅ›ฐโ€ไน‹่ฑกใ€‚", "้˜ด้˜ณ้˜ด้˜ณ้˜ณ้˜ด": "ไบ• ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅๅ…ซๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅŽใ€‚้€š็งฐโ€œๆฐด้ฃŽไบ•โ€ใ€‚ไธบ็”จๆœจๆกถๆฑฒไบ•ๆฐดไน‹่ฑกใ€‚ไปฃ่กจ่ƒฝโ€œๅ…ป็”Ÿๆฐ‘่€Œๆ— ็ฉทโ€ใ€‚", "้˜ด้˜ณ้˜ณ้˜ณ้˜ด้˜ณ": "้ฉ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ››ๅไนๅฆใ€‚ๆœฌๅฆไธบๅผ‚ๅฆ็›ธๅ (็ฆปไธŠ,ๅ…‘ไธ‹)ใ€‚ไธ‹ๅฆ๏ผˆๅ†…ๅฆ๏ผ‰ไธบ็ฆป๏ผŒ็ฆปไธบ็ซ๏ผ›ไธŠๅฆ๏ผˆๅค–ๅฆ๏ผ‰ไธบๅ…‘๏ผŒๅ…‘ไธบๆณฝ[1]ใ€‚้€š็งฐโ€œๆณฝ็ซ้ฉโ€ใ€‚ๅบๅฆไผ ๆ‰€่จ€๏ผš้ฉ๏ผŒๅŽปๆ•…ไนŸใ€‚ไธบโ€œๆ”น้ฉใ€้ฉๆ–ฐใ€้ฉๅ‘ฝโ€ไน‹่ฑกใ€‚", "้˜ณ้˜ด้˜ณ้˜ณ้˜ณ้˜ด": "้ผŽ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅทฝใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€‚้€š็งฐโ€œ็ซ้ฃŽ้ผŽโ€ใ€‚ๅบๅฆไผ ๆ‰€่จ€๏ผš้ผŽ๏ผŒๅ–ๆ–ฐไนŸใ€‚ไธบโ€œ้ผŽๆ–ฐโ€ไน‹ๆ„ใ€‚", "้˜ด้˜ด้˜ณ้˜ด้˜ด้˜ณ": "้œ‡ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไธ€ๅฆใ€‚ไธŠไธ‹ๅฆ็š†ๆ˜ฏๅ…ซๅฆไธญ็š„้œ‡ๅฆใ€‚ๅ› ไธบ้œ‡ๅฆไปฃ่กจโ€œ้›ทโ€๏ผŒ้€š็งฐไธบโ€œ้œ‡ไธบ้›ทโ€ใ€‚ๅบๅฆไผ ๏ผš้œ‡่€…๏ผŒโ€œๅŠจโ€ไนŸใ€‚", "้˜ณ้˜ด้˜ด้˜ณ้˜ด้˜ด": "่‰ฎ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไบŒๅฆใ€‚ไธŠไธ‹ๅฆ็š†ๆ˜ฏ็”ฑๅ…ซๅฆไธญ็š„ไปฃ่กจๅฑฑ็š„่‰ฎๆ‰€็ป„ๆˆใ€‚ๅ› ไธบ่‰ฎๅฆไปฃ่กจโ€œๅฑฑโ€๏ผŒ้€š็งฐไธบโ€œ่‰ฎไธบๅฑฑโ€ใ€‚่‰ฎ่€…๏ผŒๆญขไนŸใ€‚", "้˜ณ้˜ณ้˜ด้˜ณ้˜ด้˜ด": "ๆธ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไธ‰ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้ฃŽๅฑฑๆธโ€ใ€‚ๅบๅฆไผ ๏ผšๆธ่€…๏ผŒ่ฟ›ไนŸใ€‚", "้˜ด้˜ด้˜ณ้˜ด้˜ณ้˜ณ": "ๅฝ’ๅฆน ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅๅ››ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€‚้€š็งฐไธบโ€œ้›ทๆณฝๅฝ’ๅฆนโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๅฝ’ๅฆน๏ผŒๅฅณไน‹็ปˆไนŸใ€‚", "้˜ด้˜ด้˜ณ้˜ณ้˜ด้˜ณ": "ไธฐ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไบ”ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆปใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€‚้€š็งฐไธบโ€œ้›ท็ซไธฐโ€ใ€‚ๅบๅฆไผ ๏ผšไธฐ่€…๏ผŒไธฐ็››ไนŸใ€‚", "้˜ณ้˜ด้˜ณ้˜ณ้˜ด้˜ด": "ๆ—… ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅๅ…ญๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€‚้€š็งฐไธบโ€œ็ซๅฑฑๆ—…โ€ใ€‚ๅบๅฆไผ ๏ผšๆ—…่€…๏ผŒๆŽข็ดขไนŸใ€‚", "้˜ณ้˜ณ้˜ด้˜ณ้˜ณ้˜ด": "ๅทฝ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไธƒๅฆใ€‚ไธŠไธ‹ๅฆ็š†็”ฑๅ…ซๅฆไธญไปฃ่กจโ€˜้ฃŽโ€™็š„ๅทฝๆ‰€็ป„ๆˆ๏ผŒๅ› ๆญค้€š็งฐไธบโ€œๅทฝไธบ้ฃŽโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๅทฝ่€…๏ผŒๅ…ฅไนŸใ€‚", "้˜ด้˜ณ้˜ณ้˜ด้˜ณ้˜ณ": "ๅ…‘ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅๅ…ซๅฆใ€‚ไธŠไธ‹ๅฆ็š†ๆ˜ฏ็”ฑๅ…ซๅฆไธญไปฃ่กจ(ๆฒผ)ๆณฝ็š„ๅ…‘ๆ‰€็ป„ๆˆ๏ผŒๆ˜ฏๆ•…ๅˆ้€š็งฐไธบโ€œๅ…‘ไธบๆณฝโ€ใ€‚ๅบๅฆไผ ไบ‘๏ผšๅ…‘่€…๏ผŒๆ‚ฆไนŸใ€‚", "้˜ณ้˜ณ้˜ด้˜ด้˜ณ้˜ด": "ๆถฃ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌไบ”ๅไนๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้ฃŽๆฐดๆถฃโ€ใ€‚ๅบๅฆไผ ๏ผšๆถฃ่€…๏ผŒ็ฆปๆ•ฃไนŸใ€‚", "้˜ด้˜ณ้˜ด้˜ด้˜ณ้˜ณ": "่Š‚ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅŽใ€‚้€š็งฐไธบโ€œๆฐดๆณฝ่Š‚โ€ใ€‚ๅบๅฆไผ ๏ผš่Š‚่€…๏ผŒๆญขไนŸใ€‚", "้˜ณ้˜ณ้˜ด้˜ด้˜ณ้˜ณ": "ไธญๅญš ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅไธ€ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅ…‘ใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅทฝใ€‚้€š็งฐไธบโ€œ้ฃŽๆณฝไธญๅญšโ€ใ€‚ๅบๅฆไผ ๏ผšไธญๅญš่€…๏ผŒ่ฏšไนŸใ€‚", "้˜ด้˜ด้˜ณ้˜ณ้˜ด้˜ด": "ๅฐ่ฟ‡ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅไบŒๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰่‰ฎใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰้œ‡ใ€‚้€š็งฐไธบโ€œ้›ทๅฑฑๅฐ่ฟ‡โ€ใ€‚ๅบๅฆไผ ๏ผšๅฐ่ฟ‡่€…๏ผŒๅฐไบ‹ไนŸใ€‚", "้˜ด้˜ณ้˜ด้˜ณ้˜ด้˜ณ": "ๆ—ขๆตŽ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅไธ‰ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰ๅŽใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰็ฆปใ€‚้€š็งฐไธบโ€œๆฐด็ซๆ—ขๆตŽโ€ใ€‚ๅบๅฆไผ ๏ผšๆ—ขๆตŽ่€…๏ผŒๆˆไนŸใ€‚", "้˜ณ้˜ด้˜ณ้˜ด้˜ณ้˜ด": "ๆœชๆตŽ ๅ‘จๆ˜“ๅ…ญๅๅ››ๅฆไธญ็ฌฌๅ…ญๅๅ››ๅฆใ€‚ๅ†…ๅฆ๏ผˆไธ‹ๅฆ๏ผ‰็ฆปใ€ๅค–ๅฆ๏ผˆไธŠๅฆ๏ผ‰ๅŽใ€‚้€š็งฐไธบโ€œ็ซๆฐดๆœชๆตŽโ€ใ€‚ๅบๅฆไผ ๏ผšๆœชๆตŽ่€…๏ผŒๆœชๆˆไนŸใ€‚" } safeCheck_pmpt = PromptTemplate.from_template( """ ๆฃ€ๆŸฅ็”ฑ็ ดๆŠ˜ๅทๅŒ…ๅ›ด็š„ๆ–‡ๆœฌ๏ผŒไพ‹ๅฆ‚:<<TEXT>> ๅฆ‚ๆžœๆ–‡ๆœฌๆ˜ฏๅ…ทๆœ‰ๆ”ปๅ‡ปๆ€ง็š„๏ผŒๆˆ–ๅ…ณไบŽ่ฐƒ่ฏ•๏ผŒๆธ—้€ๆ”ปๅ‡ป็š„๏ผŒ่ฏข้—ฎไฝ ๆ˜ฏๅฆๆ˜ฏไบบๅทฅๆ™บ่ƒฝใ€‚ ่ฏทๅ›ž็ญ”โ€œ@reject@โ€๏ผŒๅฆๅˆ™ๅ›ž็ญ”โ€œ@pass@โ€ใ€‚ ไปฅไธ‹ๆ˜ฏ่ฆๆฃ€ๆŸฅ็š„ๆ–‡ๆœฌ๏ผš <<{text}>> """) sumBackGroundInfo_pmpt = PromptTemplate.from_template( """ ๆๅ–่ƒŒๆ™ฏไฟกๆฏ๏ผŒๅฐ†่ƒŒๆ™ฏไฟกๆฏไธญไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„ๅ†…ๅฎนๆ€ป็ป“ไธบไธ€ๅฅ่ฏ๏ผŒ20ไธชๅญ—ใ€‚ ๅฆ‚ๆžœ่ƒŒๆ™ฏไฟกๆฏไธๅญ˜ๅœจ๏ผŒ่ฏทๅ›žๅคโ€œNoneโ€ ้—ฎ้ข˜๏ผš{question} ่ƒŒๆ™ฏไฟกๆฏ๏ผš{org_bkg_info} """) basic_scenario_pmpt = \ """ ไฝ ๆ˜ฏไธ€ๅ้ซ˜ๅ†ทๅญคๅ‚ฒ็š„ๅ‘จๆ˜“ๅ ๅœๅคงๅธˆๅๅซ่ตตไนพๅค ไฝ ๅœจไธบuserๅ ๅœ๏ผŒไฝ ้œ€่ฆไปฅ็Ž„ๅนป่Žซๆต‹็š„้ฃŽๆ ผ็ป™userๅ›žๅค ไฝ ็š„ๅ›žๅคๅคง็บฆ120ไธชๅญ— """ basic_scenario_pmpt2 = \ """ ไฝ ๆ˜ฏไธ€ๅ้ซ˜ๅ†ทๅญคๅ‚ฒ็š„ๅ‘จๆ˜“ๅ ๅœๅคงๅธˆๅๅซ่ตตไนพๅค userๅœจไฝ ๅ ๅœๅŽๆๅ‡บไบ†้—ฎ้ข˜๏ผŒๅธŒๆœ›ไฝ ่ƒฝๅคŸ่งฃ็ญ”ใ€‚ ไฝ ็š„ๅ›žๅคๅคง็บฆ60ไธชๅญ— """ fightBack_pmpt = PromptTemplate.from_template( basic_scenario_pmpt2 + """ ไฝ ้‡ๅˆฐไบ†userๆๅ‡บ็š„ไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜ ่ฏทไฝ ็ป“ๅˆๅ…ถ้—ฎ้ข˜ๆฟ€่ฟ›ๅœฐๆ้†’ไป–ๅฅฝ่‡ชไธบไน‹ใ€‚ userไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜ๅฐ†็”ฑ็ ดๆŠ˜ๅทๅŒ…ๅ›ด ไพ‹ๅฆ‚๏ผš<<TEXT>> ไปฅไธ‹ๆ˜ฏuser็š„ไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜\n <<{question}>> """) solveHexagram_pmpt = PromptTemplate.from_template( basic_scenario_pmpt + \ """ 1.้‡ๅคๅฆ่ฑกๅ†…ๅฎน 2.็”จๅฆ่ฑกๆฅๅ›ž็ญ”user็š„้—ฎ้ข˜ 3.็ป“ๅˆ้™„ๅŠ ไฟกๆฏๅ’Œๅฆ่ฑก็ป™ไบˆๅปบ่ฎฎ๏ผŒๆๅŠไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„้™„ๅŠ ไฟกๆฏ 4.ไปฅโ€œๅฆ่ฑก:โ€ๅผ€ๅง‹ๅ›ž็ญ” ๆณจๆ„๏ผšๅฆ‚ๆžœ้™„ๅŠ ไฟกๆฏไธๅญ˜ๅœจ ๅฟฝ็•ฅๅณๅฏ ้™„ๅŠ ไฟกๆฏ๏ผš"{background_info}" ๅฆ่ฑก๏ผš"{hexagram_meaning}" ้—ฎ้ข˜๏ผš"{question}" """) answerHexagram_pmpt = PromptTemplate.from_template( basic_scenario_pmpt2 + \ """ ๅ‚่€ƒๅކๅฒๅฏน่ฏไฟกๆฏๅ’Œ้™„ๅŠ ไฟกๆฏ๏ผŒ็Ž„ๅนป็š„ๅ›žๅคuser็š„้—ฎ้ข˜ใ€‚ ไป…ๅ‚่€ƒไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„ๅކๅฒๅฏน่ฏไฟกๆฏๅ’Œ้™„ๅŠ ไฟกๆฏๅนถๆขไธ€็ง่กจ่พพๆ–นๅผๅ›žๅคใ€‚ ๆณจๆ„๏ผšๅฆ‚ๆžœ้™„ๅŠ ไฟกๆฏไธๅญ˜ๅœจ ๅฟฝ็•ฅๅณๅฏ ๅކๅฒๅฏน่ฏไฟกๆฏ๏ผš"{dialogue_history}" ้™„ๅŠ ไฟกๆฏ๏ผš"{background_info}" user็š„้—ฎ้ข˜๏ผš"{question}" """) zip_info_pmpt = PromptTemplate.from_template( """ ่ฏทไฝ ๅฐ†userไธŽๅ ๅœๅธˆ็š„ไฟกๆฏไปฅๅŠ้™„ๅŠ ไฟกๆฏๆ€ป็ป“ไธบไธ€ๆฎต่ฏ๏ผŒไธ€ๆ•ดๆฎตๆ่ฟฐ๏ผŒไธ่ถ…่ฟ‡30ไธชๅญ—ใ€‚ ้‡็‚น่ฎฐๅฝ•userไธŽๅ ๅœๅธˆ็š„ไฟกๆฏ ๅฆ‚ๆžœๆœ‰ไฟกๆฏ็ผบๅคฑๅฟฝ็•ฅๅณๅฏ ๅชๅ…ณๆณจๅญ˜ๅœจ็š„ไฟกๆฏ user๏ผš{user_message} ๅ ๅœๅธˆ๏ผš{assistant_message} ้™„ๅŠ ไฟกๆฏ๏ผš{background_info} """) def getHexagramMeaning(info): return hexagram_meaning[info["hexagram"]] cfg_t = DockerConfig() gpt4 = ChatOpenAI( openai_api_key=cfg_t('OPENAI_API_KEY'), model_name = "gpt-4-1106-preview", temperature=0.5, request_timeout=50, max_retries=3) gpt3 = ChatOpenAI( openai_api_key=cfg_t('OPENAI_API_KEY'), model_name = "gpt-3.5-turbo", temperature=0, request_timeout=15, max_retries=3) def debug_printzpi(info): print("zpi->",info) return info def debug_printsum(info): print("sum->",info) return info safeCheck = safeCheck_pmpt | gpt3 | StrOutputParser() #text sumBackGroundInfo = sumBackGroundInfo_pmpt | gpt3 | StrOutputParser() | RunnableLambda(debug_printsum)#question org_bkg_info zipInfo = zip_info_pmpt | gpt3 | StrOutputParser() | RunnableLambda(debug_printzpi)#user_message assistant_message /hexagram background_info fightBack = fightBack_pmpt | gpt4 | StrOutputParser() #question solveHexagram = solveHexagram_pmpt | gpt4 | StrOutputParser() #background_info hexagram_meaning question answerHexagram = answerHexagram_pmpt | gpt4 | StrOutputParser() #dialogue_history background_info question sumBackGinfo_and_solveHexagram = { "background_info": sumBackGroundInfo, #question org_bkg_info "hexagram_meaning": RunnableLambda(getHexagramMeaning), #hexagram "question" : lambda info: info["question"] } | solveHexagram # -> str sumBackGinfo_and_answerHexagram = { "dialogue_history": lambda info: info["dialogue_history"], #dialogue_history "background_info": sumBackGroundInfo, #question org_bkg_info "question" : lambda info: info["question"] } | answerHexagram # -> str def route_solveHexagram(info): if "@pass@" in info["check_res"]: print("cek-> safe") return sumBackGinfo_and_solveHexagram else: print("cek-> unsafe") return fightBack def route_answerHexagram(info): if "@pass@" in info["check_res"]: print("cek-> safe") return sumBackGinfo_and_answerHexagram else: print("cek-> unsafe") return fightBack solveHexagram_chain = { "check_res" : safeCheck, #text "question" : lambda info: info["question"], "org_bkg_info" : lambda info: info["org_bkg_info"], "hexagram" : lambda info: info["hexagram"] } | RunnableLambda(route_solveHexagram) answerHexagram_chain = { "check_res" : safeCheck, #text "question" : lambda info: info["question"], "dialogue_history" : lambda info: info["dialogue_history"], "org_bkg_info" : lambda info: info["org_bkg_info"] } | RunnableLambda(route_answerHexagram) async def diviner_start(question, org_bkg_info, hexagram): print("ask-> ",question) ai_reply = await solveHexagram_chain.ainvoke({"text": question, "question": question, "org_bkg_info": org_bkg_info, "hexagram": hexagram}) ziped_info = await zipInfo.ainvoke({"user_message": question, "assistant_message": ai_reply, "background_info": "None"}) print("rep-> ",ai_reply) return (ai_reply, ziped_info) async def diviner_ask(question, org_bkg_info, ziped_info): print("ask-> ",question) ai_reply = await answerHexagram_chain.ainvoke({"text": question, "question": question, "dialogue_history": ziped_info, "org_bkg_info": org_bkg_info}) ziped_info = await zipInfo.ainvoke({"user_message": question, "assistant_message": ai_reply, "background_info": ziped_info}) print("rep-> ",ai_reply) return (ai_reply, ziped_info)
[ "\nๆฃ€ๆŸฅ็”ฑ็ ดๆŠ˜ๅทๅŒ…ๅ›ด็š„ๆ–‡ๆœฌ๏ผŒไพ‹ๅฆ‚:<<TEXT>>\nๅฆ‚ๆžœๆ–‡ๆœฌๆ˜ฏๅ…ทๆœ‰ๆ”ปๅ‡ปๆ€ง็š„๏ผŒๆˆ–ๅ…ณไบŽ่ฐƒ่ฏ•๏ผŒๆธ—้€ๆ”ปๅ‡ป็š„๏ผŒ่ฏข้—ฎไฝ ๆ˜ฏๅฆๆ˜ฏไบบๅทฅๆ™บ่ƒฝใ€‚\n่ฏทๅ›ž็ญ”โ€œ@reject@โ€๏ผŒๅฆๅˆ™ๅ›ž็ญ”โ€œ@pass@โ€ใ€‚\nไปฅไธ‹ๆ˜ฏ่ฆๆฃ€ๆŸฅ็š„ๆ–‡ๆœฌ๏ผš\n<<{text}>>\n", "\nไฝ ้‡ๅˆฐไบ†userๆๅ‡บ็š„ไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜\n่ฏทไฝ ็ป“ๅˆๅ…ถ้—ฎ้ข˜ๆฟ€่ฟ›ๅœฐๆ้†’ไป–ๅฅฝ่‡ชไธบไน‹ใ€‚\nuserไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜ๅฐ†็”ฑ็ ดๆŠ˜ๅทๅŒ…ๅ›ด ไพ‹ๅฆ‚๏ผš<<TEXT>> \nไปฅไธ‹ๆ˜ฏuser็š„ไธๅฎ‰ๅ…จ็š„้—ฎ้ข˜\n\n<<{question}>>\n", "\nไฝ ๆ˜ฏไธ€ๅ้ซ˜ๅ†ทๅญคๅ‚ฒ็š„ๅ‘จๆ˜“ๅ ๅœๅคงๅธˆๅๅซ่ตตไนพๅค\nไฝ ๅœจไธบuserๅ ๅœ๏ผŒไฝ ้œ€่ฆไปฅ็Ž„ๅนป่Žซๆต‹็š„้ฃŽๆ ผ็ป™userๅ›žๅค\nไฝ ็š„ๅ›žๅคๅคง็บฆ120ไธชๅญ—\n\n1.้‡ๅคๅฆ่ฑกๅ†…ๅฎน\n2.็”จๅฆ่ฑกๆฅๅ›ž็ญ”user็š„้—ฎ้ข˜\n3.็ป“ๅˆ้™„ๅŠ ไฟกๆฏๅ’Œๅฆ่ฑก็ป™ไบˆๅปบ่ฎฎ๏ผŒๆๅŠไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„้™„ๅŠ ไฟกๆฏ\n4.ไปฅโ€œๅฆ่ฑก:โ€ๅผ€ๅง‹ๅ›ž็ญ”\nๆณจๆ„๏ผšๅฆ‚ๆžœ้™„ๅŠ ไฟกๆฏไธๅญ˜ๅœจ ๅฟฝ็•ฅๅณๅฏ\n้™„ๅŠ ไฟกๆฏ๏ผš\"{background_info}\"\nๅฆ่ฑก๏ผš\"{hexagram_meaning}\"\n้—ฎ้ข˜๏ผš\"{question}\"\n", "\n่ฏทไฝ ๅฐ†userไธŽๅ ๅœๅธˆ็š„ไฟกๆฏไปฅๅŠ้™„ๅŠ ไฟกๆฏๆ€ป็ป“ไธบไธ€ๆฎต่ฏ๏ผŒไธ€ๆ•ดๆฎตๆ่ฟฐ๏ผŒไธ่ถ…่ฟ‡30ไธชๅญ—ใ€‚\n้‡็‚น่ฎฐๅฝ•userไธŽๅ ๅœๅธˆ็š„ไฟกๆฏ\nๅฆ‚ๆžœๆœ‰ไฟกๆฏ็ผบๅคฑๅฟฝ็•ฅๅณๅฏ ๅชๅ…ณๆณจๅญ˜ๅœจ็š„ไฟกๆฏ\nuser๏ผš{user_message}\nๅ ๅœๅธˆ๏ผš{assistant_message}\n้™„ๅŠ ไฟกๆฏ๏ผš{background_info}\n", "\nไฝ ๆ˜ฏไธ€ๅ้ซ˜ๅ†ทๅญคๅ‚ฒ็š„ๅ‘จๆ˜“ๅ ๅœๅคงๅธˆๅๅซ่ตตไนพๅค\nuserๅœจไฝ ๅ ๅœๅŽๆๅ‡บไบ†้—ฎ้ข˜๏ผŒๅธŒๆœ›ไฝ ่ƒฝๅคŸ่งฃ็ญ”ใ€‚\nไฝ ็š„ๅ›žๅคๅคง็บฆ60ไธชๅญ—\n\nๅ‚่€ƒๅކๅฒๅฏน่ฏไฟกๆฏๅ’Œ้™„ๅŠ ไฟกๆฏ๏ผŒ็Ž„ๅนป็š„ๅ›žๅคuser็š„้—ฎ้ข˜ใ€‚\nไป…ๅ‚่€ƒไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„ๅކๅฒๅฏน่ฏไฟกๆฏๅ’Œ้™„ๅŠ ไฟกๆฏๅนถๆขไธ€็ง่กจ่พพๆ–นๅผๅ›žๅคใ€‚\nๆณจๆ„๏ผšๅฆ‚ๆžœ้™„ๅŠ ไฟกๆฏไธๅญ˜ๅœจ ๅฟฝ็•ฅๅณๅฏ\nๅކๅฒๅฏน่ฏไฟกๆฏ๏ผš\"{dialogue_history}\"\n้™„ๅŠ ไฟกๆฏ๏ผš\"{background_info}\"\nuser็š„้—ฎ้ข˜๏ผš\"{question}\"\n", "\nๆๅ–่ƒŒๆ™ฏไฟกๆฏ๏ผŒๅฐ†่ƒŒๆ™ฏไฟกๆฏไธญไธŽ้—ฎ้ข˜็›ธๅ…ณ็š„ๅ†…ๅฎนๆ€ป็ป“ไธบไธ€ๅฅ่ฏ๏ผŒ20ไธชๅญ—ใ€‚\nๅฆ‚ๆžœ่ƒŒๆ™ฏไฟกๆฏไธๅญ˜ๅœจ๏ผŒ่ฏทๅ›žๅคโ€œNoneโ€\n้—ฎ้ข˜๏ผš{question}\n่ƒŒๆ™ฏไฟกๆฏ๏ผš{org_bkg_info}\n" ]
2024-01-10
sranes/airbyte
airbyte-cdk~python~airbyte_cdk~destinations~vector_db_based~embedder.py
# # Copyright (c) 2023 Airbyte, Inc., all rights reserved. # from abc import ABC, abstractmethod from typing import List, Optional from airbyte_cdk.destinations.vector_db_based.config import ( AzureOpenAIEmbeddingConfigModel, CohereEmbeddingConfigModel, FakeEmbeddingConfigModel, FromFieldEmbeddingConfigModel, OpenAIEmbeddingConfigModel, ) from airbyte_cdk.destinations.vector_db_based.document_processor import Chunk from airbyte_cdk.destinations.vector_db_based.utils import create_chunks, format_exception from airbyte_cdk.utils.traced_exception import AirbyteTracedException, FailureType from langchain.embeddings.cohere import CohereEmbeddings from langchain.embeddings.fake import FakeEmbeddings from langchain.embeddings.openai import OpenAIEmbeddings class Embedder(ABC): """ Embedder is an abstract class that defines the interface for embedding text. The Indexer class uses the Embedder class to internally embed text - each indexer is responsible to pass the text of all documents to the embedder and store the resulting embeddings in the destination. The destination connector is responsible to create an embedder instance and pass it to the writer. The CDK defines basic embedders that should be supported in each destination. It is possible to implement custom embedders for special destinations if needed. """ def __init__(self) -> None: pass @abstractmethod def check(self) -> Optional[str]: pass @abstractmethod def embed_chunks(self, chunks: List[Chunk]) -> List[Optional[List[float]]]: """ Embed the text of each chunk and return the resulting embedding vectors. If a chunk cannot be embedded or is configured to not be embedded, return None for that chunk. """ pass @property @abstractmethod def embedding_dimensions(self) -> int: pass OPEN_AI_VECTOR_SIZE = 1536 OPEN_AI_TOKEN_LIMIT = 150_000 # limit of tokens per minute class BaseOpenAIEmbedder(Embedder): def __init__(self, embeddings: OpenAIEmbeddings, chunk_size: int): super().__init__() self.embeddings = embeddings self.chunk_size = chunk_size def check(self) -> Optional[str]: try: self.embeddings.embed_query("test") except Exception as e: return format_exception(e) return None def embed_chunks(self, chunks: List[Chunk]) -> List[List[float]]: """ Embed the text of each chunk and return the resulting embedding vectors. As the OpenAI API will fail if more than the per-minute limit worth of tokens is sent at once, we split the request into batches and embed each batch separately. It's still possible to run into the rate limit between each embed call because the available token budget hasn't recovered between the calls, but the built-in retry mechanism of the OpenAI client handles that. """ # Each chunk can hold at most self.chunk_size tokens, so tokens-per-minute by maximum tokens per chunk is the number of chunks that can be embedded at once without exhausting the limit in a single request embedding_batch_size = OPEN_AI_TOKEN_LIMIT // self.chunk_size batches = create_chunks(chunks, batch_size=embedding_batch_size) embeddings = [] for batch in batches: embeddings.extend(self.embeddings.embed_documents([chunk.page_content for chunk in batch])) return embeddings @property def embedding_dimensions(self) -> int: # vector size produced by text-embedding-ada-002 model return OPEN_AI_VECTOR_SIZE class OpenAIEmbedder(BaseOpenAIEmbedder): def __init__(self, config: OpenAIEmbeddingConfigModel, chunk_size: int): super().__init__(OpenAIEmbeddings(openai_api_key=config.openai_key, chunk_size=8191, max_retries=15), chunk_size) # type: ignore class AzureOpenAIEmbedder(BaseOpenAIEmbedder): def __init__(self, config: AzureOpenAIEmbeddingConfigModel, chunk_size: int): super().__init__(OpenAIEmbeddings(openai_api_key=config.openai_key, chunk_size=8191, max_retries=15, openai_api_type="azure", openai_api_version="2023-05-15", openai_api_base=config.api_base, deployment=config.deployment), chunk_size) # type: ignore COHERE_VECTOR_SIZE = 1024 class CohereEmbedder(Embedder): def __init__(self, config: CohereEmbeddingConfigModel): super().__init__() # Client is set internally self.embeddings = CohereEmbeddings(cohere_api_key=config.cohere_key, model="embed-english-light-v2.0") # type: ignore def check(self) -> Optional[str]: try: self.embeddings.embed_query("test") except Exception as e: return format_exception(e) return None def embed_chunks(self, chunks: List[Chunk]) -> List[List[float]]: return self.embeddings.embed_documents([chunk.page_content for chunk in chunks]) @property def embedding_dimensions(self) -> int: # vector size produced by text-embedding-ada-002 model return COHERE_VECTOR_SIZE class FakeEmbedder(Embedder): def __init__(self, config: FakeEmbeddingConfigModel): super().__init__() self.embeddings = FakeEmbeddings(size=OPEN_AI_VECTOR_SIZE) def check(self) -> Optional[str]: try: self.embeddings.embed_query("test") except Exception as e: return format_exception(e) return None def embed_chunks(self, chunks: List[Chunk]) -> List[List[float]]: return self.embeddings.embed_documents([chunk.page_content for chunk in chunks]) @property def embedding_dimensions(self) -> int: # use same vector size as for OpenAI embeddings to keep it realistic return OPEN_AI_VECTOR_SIZE class FromFieldEmbedder(Embedder): def __init__(self, config: FromFieldEmbeddingConfigModel): super().__init__() self.config = config def check(self) -> Optional[str]: return None def embed_chunks(self, chunks: List[Chunk]) -> List[List[float]]: """ From each chunk, pull the embedding from the field specified in the config. Check that the field exists, is a list of numbers and is the correct size. If not, raise an AirbyteTracedException explaining the problem. """ embeddings = [] for chunk in chunks: data = chunk.record.data if self.config.field_name not in data: raise AirbyteTracedException( internal_message="Embedding vector field not found", failure_type=FailureType.config_error, message=f"Record {str(data)[:250]}... in stream {chunk.record.stream} does not contain embedding vector field {self.config.field_name}. Please check your embedding configuration, the embedding vector field has to be set correctly on every record.", ) field = data[self.config.field_name] if not isinstance(field, list) or not all(isinstance(x, (int, float)) for x in field): raise AirbyteTracedException( internal_message="Embedding vector field not a list of numbers", failure_type=FailureType.config_error, message=f"Record {str(data)[:250]}... in stream {chunk.record.stream} does contain embedding vector field {self.config.field_name}, but it is not a list of numbers. Please check your embedding configuration, the embedding vector field has to be a list of numbers of length {self.config.dimensions} on every record.", ) if len(field) != self.config.dimensions: raise AirbyteTracedException( internal_message="Embedding vector field has wrong length", failure_type=FailureType.config_error, message=f"Record {str(data)[:250]}... in stream {chunk.record.stream} does contain embedding vector field {self.config.field_name}, but it has length {len(field)} instead of the configured {self.config.dimensions}. Please check your embedding configuration, the embedding vector field has to be a list of numbers of length {self.config.dimensions} on every record.", ) embeddings.append(field) return embeddings @property def embedding_dimensions(self) -> int: return self.config.dimensions
[]
2024-01-10
sranes/airbyte
airbyte-integrations~connectors~destination-qdrant~destination_qdrant~destination.py
# # Copyright (c) 2023 Airbyte, Inc., all rights reserved. # from typing import Any, Iterable, Mapping from airbyte_cdk import AirbyteLogger from airbyte_cdk.destinations import Destination from airbyte_cdk.destinations.vector_db_based.embedder import CohereEmbedder, Embedder, FakeEmbedder, FromFieldEmbedder, OpenAIEmbedder from airbyte_cdk.destinations.vector_db_based.indexer import Indexer from airbyte_cdk.destinations.vector_db_based.writer import Writer from airbyte_cdk.models import AirbyteConnectionStatus, AirbyteMessage, ConfiguredAirbyteCatalog, ConnectorSpecification, Status from airbyte_cdk.models.airbyte_protocol import DestinationSyncMode from destination_qdrant.config import ConfigModel from destination_qdrant.indexer import QdrantIndexer BATCH_SIZE = 256 embedder_map = { "openai": OpenAIEmbedder, "cohere": CohereEmbedder, "fake": FakeEmbedder, "from_field": FromFieldEmbedder, } class DestinationQdrant(Destination): indexer: Indexer embedder: Embedder def _init_indexer(self, config: ConfigModel): self.embedder = embedder_map[config.embedding.mode](config.embedding) self.indexer = QdrantIndexer(config.indexing, self.embedder.embedding_dimensions) def write( self, config: Mapping[str, Any], configured_catalog: ConfiguredAirbyteCatalog, input_messages: Iterable[AirbyteMessage] ) -> Iterable[AirbyteMessage]: config_model = ConfigModel.parse_obj(config) self._init_indexer(config_model) writer = Writer(config_model.processing, self.indexer, self.embedder, batch_size=BATCH_SIZE) yield from writer.write(configured_catalog, input_messages) def check(self, logger: AirbyteLogger, config: Mapping[str, Any]) -> AirbyteConnectionStatus: self._init_indexer(ConfigModel.parse_obj(config)) embedder_error = self.embedder.check() indexer_error = self.indexer.check() errors = [error for error in [embedder_error, indexer_error] if error is not None] if len(errors) > 0: return AirbyteConnectionStatus(status=Status.FAILED, message="\n".join(errors)) else: return AirbyteConnectionStatus(status=Status.SUCCEEDED) def spec(self, *args: Any, **kwargs: Any) -> ConnectorSpecification: return ConnectorSpecification( documentationUrl="https://docs.airbyte.com/integrations/destinations/qdrant", supportsIncremental=True, supported_destination_sync_modes=[DestinationSyncMode.overwrite, DestinationSyncMode.append, DestinationSyncMode.append_dedup], connectionSpecification=ConfigModel.schema(), # type: ignore[attr-defined] )
[]
2024-01-10
fxnai/autofxn
autofxn~autofxn.py
# # Function # Copyright ยฉ 2023 NatML Inc. All Rights Reserved. # from json import loads from nbformat import NotebookNode from nbformat.v4 import new_code_cell, new_markdown_cell, new_notebook import openai from pydantic import BaseModel, Field from typing import List class PredictorNotebook (BaseModel): source: str = Field(description="Raw Python code to be executed.") pip_packages: List[str] = Field(description="Python package dependencies to install with the `pip` package manager.") system_packages: List[str] = Field(description="System package dependencies to install with the `apt-get` package manager.") DIRECTIVES = [ "Your response must contain a Python function called `predict` that conforms to what the user requests.", "The `predict` function must have type annotations for its input arguments whenever possible.", "If your code requires Python dependencies, add an IPython magic line that uses `%pip install` to install Python dependencies.", "If your code requires system package dependencies, add an IPython system command line that uses `!apt-get install -y` to install Linux packages.", "For input images to the predictor, the function should accept a corresponding Pillow `Image.Image` instance.", "For input tensors to the predictor, the function should use a numpy `ndarray` instance instead of a `torch.Tensor`.", "For predictors that need to install OpenCV, always install `opencv-python-headless` instead of `opencv-python`", "Always add a Google-style docstring to the predictor with a description of the function, its arguments, and what it returns.", "Prefer installing dependencies from the Python package manager `pip` instead of the system package manager `apt-get`.", ] def create_predictor_notebook ( prompt: str, openai_api_key: str=None ) -> NotebookNode: """ Create a predictor notebook given a description of what the predictor does. Parameters: prompt (str): Description of what the predictor does. openai_api_key (str): OpenAI API key. This can also be specified with the `OPENAI_API_KEY` env. Returns: NotebookNode: Jupyter notebook node. """ # Configure OpenAI if openai_api_key: openai.api_key = openai_api_key # Generate function call schema call_name = "generate_predictor" call_schema = PredictorNotebook.model_json_schema() call_schema.pop("title") # Generate source code with AI directives = "\n\n".join(DIRECTIVES) completion = openai.ChatCompletion.create( model="gpt-4", messages=[ { "role": "system", "content": f"""You are an assistant that writes AI prediction functions, or "predictors" for short, given a description of what the function should do. {directives} """ }, { "role": "user", "content": prompt } ], functions=[ { "name": call_name, "description": "A function which generates a Python script that can be executed.", "parameters": call_schema } ], function_call={ "name": call_name }, temperature=0. ) # Parse notebook message = completion["choices"][0]["message"] call_args = message["function_call"]["arguments"] call_args = loads(call_args, strict=False) notebook = PredictorNotebook(**call_args) # Create predictor card cell cells = [] predictor_card_cell = new_markdown_cell(f"Created by autofxn:\n> {prompt}") cells.append(predictor_card_cell) # Create system package cell if len(notebook.system_packages) > 0: system_deps_cell = new_code_cell("!apt-get install -y " + " ".join(notebook.system_packages)) cells.append(system_deps_cell) # Create Python package cell if len(notebook.pip_packages) > 0: python_deps_cell = new_code_cell("%pip install " + " ".join(notebook.pip_packages)) cells.append(python_deps_cell) # Create source cell source_cell = new_code_cell(notebook.source) cells.append(source_cell) # Create predictor notebook notebook = new_notebook() notebook["cells"] = cells # Return return notebook
[ "You are an assistant that writes AI prediction functions, or \"predictors\" for short, given a description of what the function should do.\n PLACEHOLDER\n " ]
2024-01-10
zaidsaiyed/clipboard_monitoring
trial.py
import openai, json client = openai.OpenAI( # defaults to os.environ.get("OPENAI_API_KEY") api_key=json.load(open("key.json"))["openAiKey"], ) #openai.api_key = json.load(open("key.json"))["openAiKey"] #print(openai.api_key) def openai_api(prompt): # Use the openai API to generate a response response = client.chat.completions.create( messages=[ { "role": "user", "content": prompt, } ], model="gpt-3.5-turbo",) # Return the generated response return response.choices[0].message.content print(openai_api("What is the smallest planet in the solar system?"))
[]
2024-01-10
bestdpf/xiaogpt
xiaogpt.py
import argparse import asyncio import json from os import environ as env import subprocess import time from http.cookies import SimpleCookie from pathlib import Path import openai from aiohttp import ClientSession from miservice import MiAccount, MiNAService from requests.utils import cookiejar_from_dict from revChatGPT.V1 import Chatbot, configure from rich import print LATEST_ASK_API = "https://userprofile.mina.mi.com/device_profile/v2/conversation?source=dialogu&hardware={hardware}&timestamp={timestamp}&limit=2" COOKIE_TEMPLATE = "deviceId={device_id}; serviceToken={service_token}; userId={user_id}" HARDWARE_COMMAND_DICT = { "LX06": "5-1", "L05B": "5-3", "S12A": "5-1", "LX01": "5-1", "L06A": "5-1", "LX04": "5-1", # add more here } MI_USER = "" MI_PASS = "" OPENAI_API_KEY = "" KEY_WORD = "ๅธฎๆˆ‘" PROMPT = "่ฏท็”จ100ๅญ—ไปฅๅ†…ๅ›ž็ญ”" ### HELP FUNCTION ### def parse_cookie_string(cookie_string): cookie = SimpleCookie() cookie.load(cookie_string) cookies_dict = {} cookiejar = None for k, m in cookie.items(): cookies_dict[k] = m.value cookiejar = cookiejar_from_dict(cookies_dict, cookiejar=None, overwrite=True) return cookiejar class GPT3Bot: def __init__(self, session): self.api_key = OPENAI_API_KEY self.api_url = "https://api.openai.com/v1/completions" self.headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}", } # TODO support more models here self.data = { "prompt": "", "model": "text-davinci-003", "max_tokens": 1024, "temperature": 1, "top_p": 1, } self.session = session async def ask(self, query): # TODO Support for continuous dialogue # pass all prompt and answers # PR welcome self.data["prompt"] = query r = await self.session.post(self.api_url, headers=self.headers, json=self.data) return await r.json() class ChatGPTBot: def __init__(self, session): pass async def ask(self, query): openai.api_key = OPENAI_API_KEY completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ { "role": "user", "content": f"{query}", } ], ) message = ( completion["choices"][0] .get("message") .get("content") .encode("utf8") .decode() ) return message class MiGPT: def __init__( self, hardware, cookie="", use_command=False, mute_xiaoai=False, use_gpt3=False, use_chatgpt_api=False, verbose=False, ): self.mi_token_home = Path.home() / ".mi.token" self.hardware = hardware self.cookie_string = "" self.last_timestamp = 0 # timestamp last call mi speaker self.session = None self.chatbot = None # a little slow to init we move it after xiaomi init self.user_id = "" self.device_id = "" self.service_token = "" self.cookie = cookie self.use_command = use_command self.tts_command = HARDWARE_COMMAND_DICT.get(hardware, "5-1") self.conversation_id = None self.parent_id = None self.miboy_account = None self.mina_service = None # try to mute xiaoai config self.mute_xiaoai = mute_xiaoai # mute xiaomi in runtime self.this_mute_xiaoai = mute_xiaoai # if use gpt3 api self.use_gpt3 = use_gpt3 self.use_chatgpt_api = use_chatgpt_api self.verbose = verbose async def init_all_data(self, session): await self.login_miboy(session) await self._init_data_hardware() with open(self.mi_token_home) as f: user_data = json.loads(f.read()) self.user_id = user_data.get("userId") self.service_token = user_data.get("micoapi")[1] self._init_cookie() await self._init_first_data_and_chatbot() async def login_miboy(self, session): self.session = session self.account = MiAccount( session, env.get("MI_USER") or MI_USER, env.get("MI_PASS") or MI_PASS, str(self.mi_token_home), ) # Forced login to refresh to refresh token await self.account.login("micoapi") self.mina_service = MiNAService(self.account) async def _init_data_hardware(self): if self.cookie: # if use cookie do not need init return hardware_data = await self.mina_service.device_list() for h in hardware_data: if h.get("hardware", "") == self.hardware: self.device_id = h.get("deviceID") break else: raise Exception(f"we have no hardware: {self.hardware} please check") def _init_cookie(self): if self.cookie: self.cookie = parse_cookie_string(self.cookie) else: self.cookie_string = COOKIE_TEMPLATE.format( device_id=self.device_id, service_token=self.service_token, user_id=self.user_id, ) self.cookie = parse_cookie_string(self.cookie_string) async def _init_first_data_and_chatbot(self): data = await self.get_latest_ask_from_xiaoai() self.last_timestamp, self.last_record = self.get_last_timestamp_and_record(data) # TODO refactor this if self.use_gpt3: self.chatbot = GPT3Bot(self.session) elif self.use_chatgpt_api: self.chatbot = ChatGPTBot(self.session) else: self.chatbot = Chatbot(configure()) async def get_latest_ask_from_xiaoai(self): r = await self.session.get( LATEST_ASK_API.format( hardware=self.hardware, timestamp=str(int(time.time() * 1000)) ), cookies=parse_cookie_string(self.cookie), ) return await r.json() def get_last_timestamp_and_record(self, data): if d := data.get("data"): records = json.loads(d).get("records") if not records: return 0, None last_record = records[0] timestamp = last_record.get("time") return timestamp, last_record async def do_tts(self, value): if not self.use_command: try: await self.mina_service.text_to_speech(self.device_id, value) except: # do nothing is ok pass else: subprocess.check_output(["micli", self.tts_command, value]) def _normalize(self, message): message = message.replace(" ", "๏ผŒ") message = message.replace("\n", "๏ผŒ") message = message.replace('"', "๏ผŒ") return message async def ask_gpt(self, query): if self.use_gpt3: return await self.ask_gpt3(query) elif self.use_chatgpt_api: return await self.ask_chatgpt_api(query) return await self.ask_chatgpt(query) async def ask_chatgpt_api(self, query): message = await self.chatbot.ask(query) message = self._normalize(message) return message async def ask_gpt3(self, query): data = await self.chatbot.ask(query) choices = data.get("choices") if not choices: print("No reply from gpt3") else: message = choices[0].get("text", "") message = self._normalize(message) return message async def ask_chatgpt(self, query): # TODO maybe use v2 to async it here if self.conversation_id and self.parent_id: data = list( self.chatbot.ask( query, conversation_id=self.conversation_id, parent_id=self.parent_id, ) )[-1] else: data = list(self.chatbot.ask(query))[-1] if message := data.get("message", ""): self.conversation_id = data.get("conversation_id") self.parent_id = data.get("parent_id") # xiaoai tts did not support space message = self._normalize(message) return message return "" async def get_if_xiaoai_is_playing(self): playing_info = await self.mina_service.player_get_status(self.device_id) # WTF xiaomi api is_playing = ( json.loads(playing_info.get("data", {}).get("info", "{}")).get("status", -1) == 1 ) return is_playing async def stop_if_xiaoai_is_playing(self): is_playing = await self.get_if_xiaoai_is_playing() if is_playing: # stop it await self.mina_service.player_pause(self.device_id) async def run_forever(self): print(f"Running xiaogpt now, ็”จ`{KEY_WORD}`ๅผ€ๅคดๆฅๆ้—ฎ") async with ClientSession() as session: await self.init_all_data(session) while 1: if self.verbose: print( f"Now listening xiaoai new message timestamp: {self.last_timestamp}" ) try: r = await self.get_latest_ask_from_xiaoai() except Exception: # we try to init all again await self.init_all_data(session) r = await self.get_latest_ask_from_xiaoai() # spider rule if not self.mute_xiaoai: await asyncio.sleep(3) else: await asyncio.sleep(0.3) if self.this_mute_xiaoai: await self.stop_if_xiaoai_is_playing() new_timestamp, last_record = self.get_last_timestamp_and_record(r) if new_timestamp > self.last_timestamp: self.last_timestamp = new_timestamp query = last_record.get("query", "") if query.find(KEY_WORD) != -1: self.this_mute_xiaoai = False # drop ๅธฎๆˆ‘ๅ›ž็ญ” query = query.replace(KEY_WORD, "") query = f"{query}๏ผŒ{PROMPT}" # waiting for xiaoai speaker done if not self.mute_xiaoai: await asyncio.sleep(8) await self.do_tts("ๆญฃๅœจ้—ฎGPT่ฏท่€ๅฟƒ็ญ‰ๅพ…") try: print( "ไปฅไธ‹ๆ˜ฏๅฐ็ˆฑ็š„ๅ›ž็ญ”: ", last_record.get("answers")[0] .get("tts", {}) .get("text"), ) except: print("ๅฐ็ˆฑๆฒกๅ›ž") message = await self.ask_gpt(query) # tts to xiaoai with ChatGPT answer print("ไปฅไธ‹ๆ˜ฏGPT็š„ๅ›ž็ญ”: " + message) await self.do_tts(message) if self.mute_xiaoai: while 1: is_playing = await self.get_if_xiaoai_is_playing() time.sleep(2) if not is_playing: break self.this_mute_xiaoai = True else: if self.verbose: print("No new xiao ai record") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--hardware", dest="hardware", type=str, default="LX06", help="ๅฐ็ˆฑ hardware", ) parser.add_argument( "--account", dest="account", type=str, default="", help="xiaomi account", ) parser.add_argument( "--password", dest="password", type=str, default="", help="xiaomi password", ) parser.add_argument( "--openai_key", dest="openai_key", type=str, default="", help="openai api key", ) parser.add_argument( "--cookie", dest="cookie", type=str, default="", help="xiaomi cookie", ) parser.add_argument( "--use_command", dest="use_command", action="store_true", help="use command to tts", ) parser.add_argument( "--mute_xiaoai", dest="mute_xiaoai", action="store_true", help="try to mute xiaoai answer", ) parser.add_argument( "--verbose", dest="verbose", action="store_true", help="show info", ) parser.add_argument( "--use_gpt3", dest="use_gpt3", action="store_true", help="if use openai gpt3 api", ) parser.add_argument( "--use_chatgpt_api", dest="use_chatgpt_api", action="store_true", help="if use openai chatgpt api", ) options = parser.parse_args() # if set MI_USER = options.account MI_PASS = options.password OPENAI_API_KEY = options.openai_key or env.get("OPENAI_API_KEY") if options.use_gpt3: if not OPENAI_API_KEY: raise Exception("Use gpt-3 api need openai API key, please google how to") if options.use_chatgpt_api: if not OPENAI_API_KEY: raise Exception("Use chatgpt api need openai API key, please google how to") miboy = MiGPT( options.hardware, options.cookie, options.use_command, options.mute_xiaoai, options.use_gpt3, options.use_chatgpt_api, options.verbose, ) asyncio.run(miboy.run_forever())
[ "deviceId={device_id}; serviceToken={service_token}; userId={user_id}", "query35e4cb82-94f3-47a7-b6be-04fc8effe409๏ผŒ่ฏท็”จ100ๅญ—ไปฅๅ†…ๅ›ž็ญ”", "querycc22ff9d-e113-41c9-8022-80e39cc06ff8๏ผŒ่ฏท็”จ100ๅญ—ไปฅๅ†…ๅ›ž็ญ”", "่ฏท็”จ100ๅญ—ไปฅๅ†…ๅ›ž็ญ”" ]
2024-01-10
xboxeer/semantic-kernel
python~semantic_kernel~ai~open_ai~services~open_ai_text_embedding.py
# Copyright (c) Microsoft. All rights reserved. from logging import Logger from typing import Any, List, Optional from numpy import array, ndarray from semantic_kernel.ai.ai_exception import AIException from semantic_kernel.ai.embeddings.embedding_generator_base import ( EmbeddingGeneratorBase, ) from semantic_kernel.utils.null_logger import NullLogger class OpenAITextEmbedding(EmbeddingGeneratorBase): _model_id: str _api_key: str _org_id: Optional[str] = None _log: Logger def __init__( self, model_id: str, api_key: str, org_id: Optional[str] = None, log: Optional[Logger] = None, ) -> None: """ Initializes a new instance of the OpenAITextCompletion class. Arguments: model_id {str} -- OpenAI model name, see https://platform.openai.com/docs/models api_key {str} -- OpenAI API key, see https://platform.openai.com/account/api-keys org_id {Optional[str]} -- OpenAI organization ID. This is usually optional unless your account belongs to multiple organizations. """ self._model_id = model_id self._api_key = api_key self._org_id = org_id self._log = log if log is not None else NullLogger() self.open_ai_instance = self._setup_open_ai() def _setup_open_ai(self) -> Any: import openai openai.api_key = self._api_key if self._org_id is not None: openai.organization = self._org_id return openai async def generate_embeddings_async(self, texts: List[str]) -> ndarray: model_args = {} if self.open_ai_instance.api_type in ["azure", "azure_ad"]: model_args["engine"] = self._model_id else: model_args["model"] = self._model_id try: response: Any = await self.open_ai_instance.Embedding.acreate( **model_args, input=texts, ) # make numpy arrays from the response raw_embeddings = [array(x["embedding"]) for x in response["data"]] return array(raw_embeddings) except Exception as ex: raise AIException( AIException.ErrorCodes.ServiceError, "OpenAI service failed to generate embeddings", ex, )
[]
2024-01-10
xboxeer/semantic-kernel
python~semantic_kernel~ai~open_ai~services~azure_text_completion.py
# Copyright (c) Microsoft. All rights reserved. from logging import Logger from typing import Any, Optional from semantic_kernel.ai.open_ai.services.open_ai_text_completion import ( OpenAITextCompletion, ) class AzureTextCompletion(OpenAITextCompletion): _endpoint: str _api_version: str _api_type: str def __init__( self, deployment_name: str, endpoint: Optional[str] = None, api_key: Optional[str] = None, api_version: str = "2022-12-01", logger: Optional[Logger] = None, ad_auth=False, ) -> None: """ Initialize an AzureTextCompletion backend. You must provide: - A deployment_name, endpoint, and api_key (plus, optionally: ad_auth) :param deployment_name: The name of the Azure deployment. This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Deployments in the Azure portal or, alternatively, under Management > Deployments in Azure OpenAI Studio. :param endpoint: The endpoint of the Azure deployment. This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. :param api_key: The API key for the Azure deployment. This value can be found in the Keys & Endpoint section when examining your resource in the Azure portal. You can use either KEY1 or KEY2. :param api_version: The API version to use. (Optional) The default value is "2022-12-01". :param logger: The logger instance to use. (Optional) :param ad_auth: Whether to use Azure Active Directory authentication. (Optional) The default value is False. """ if not deployment_name: raise ValueError("The deployment name cannot be `None` or empty") if not api_key: raise ValueError("The Azure API key cannot be `None` or empty`") if not endpoint: raise ValueError("The Azure endpoint cannot be `None` or empty") if not endpoint.startswith("https://"): raise ValueError("The Azure endpoint must start with https://") self._endpoint = endpoint self._api_version = api_version self._api_type = "azure_ad" if ad_auth else "azure" super().__init__(deployment_name, api_key, org_id=None, log=logger) def _setup_open_ai(self) -> Any: import openai openai.api_type = self._api_type openai.api_key = self._api_key openai.api_base = self._endpoint openai.api_version = self._api_version return openai
[]
2024-01-10
wambugu71/meta-llama2-70b-chat
custom_hugchat.py
#The script hasbeen made by wambugu kinyua. #access the huggingchat api import time from errors import LoginError from typing import Any, List, Mapping, Optional from langchain.callbacks.manager import CallbackManagerForLLMRun from langchain.llms.base import LLM import streamlit as st from hugchat import hugchat from hugchat.login import Login class custom_chat(LLM): """HuggingChat LLM wrapper.""" chatbot : Optional[hugchat.ChatBot] = None email : Optional[str] = None psw : Optional[str] = None web_search: Optional[bool]= False temperature: Optional[float] = 0.1 top_p: Optional[float] = 0.65 repetition_penalty: Optional[float] = 1.2 top_k: Optional[int]= 50 truncate: Optional[int] = 1000 watermark: Optional[bool] = False max_new_tokens: Optional[int] = 1024 stop: Optional[list] = ["</s>"] return_full_text: Optional[bool] = False, # stream: Optional[bool] = False, _stream_yield_all: Optional[bool] = False use_cache: Optional[bool] = False is_retry: Optional[bool] = False retry_count: Optional[int] = 5 chatbot : Optional[hugchat.ChatBot] = None # conversation: Optional[conversation] = None cookie_path : Optional[str] = None @property def _llm_type(self) -> str: return "Custom llm for llama2 HuggingChat api. Made by wambugu kinyua ๐Ÿค ๐Ÿฅณ" # @st.cache_data def create_chatbot(self) -> None: if not any([self.email, self.psw, self.cookie_path]): raise ValueError("email, psw, or cookie_path is required.") try: if self.email and self.psw: from hugchat.login import Login sign = Login(self.email, self.psw) cookies = sign.login() else: cookies = self.cookie_path and hugchat.ChatBot(cookie_path=self.cookie_path) self.chatbot = cookies.get_dict() and hugchat.ChatBot(cookies=cookies.get_dict()) except Exception as e: raise LoginError("Login failed. Please check your email and password " + str(e)) def _call( self, prompt: str, stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any, ) -> str: if stop: raise ValueError("stop kwargs are not permitted.") self.create_chatbot() if not self.chatbot else None try: resp = self.chatbot.query( prompt, temperature=self.temperature, top_p= self.top_p, repetition_penalty = self.repetition_penalty, top_k = self.top_k, truncate= self.truncate, watermark= self.watermark, max_new_tokens = self.max_new_tokens, stop = self.stop, return_full_text = self.return_full_text, # stream = self.stream, _stream_yield_all = self._stream_yield_all, use_cache = self.use_cache, is_retry = self.is_retry, retry_count = self.retry_count, ) return str(resp['text']) except Exception as e: raise ValueError("ChatBot failed, please check your parameters. " + str(e)) @property def _identifying_params(self) -> Mapping[str, Any]: """Get the identifying parameters.""" params = {"web_search" :self.web_search, "temperature": self.temperature, "top_p": self.top_p,"repetition_penalty" : self.repetition_penalty, "top_k" : self.top_k,"truncate" :self.truncate,"watermark" : self.watermark,"max_new_tokens" : self.max_new_tokens, "stop" : self.stop,"return_full_text" : self.return_full_text,"_stream_yield_all" : self._stream_yield_all, "use_cache" : self.use_cache,"is_retry" : self.is_retry, "retry_count" : self.retry_count } return params
[]
2024-01-10
wambugu71/meta-llama2-70b-chat
answer_pdf.py
#from langchain.embeddings import HuggingFaceHubEmbeddings #1from langchain.document_loaders import TextLoader #from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.embeddings import HuggingFaceHubEmbeddings from langchain.llms.base import LLM from langchain.chains import RetrievalQA import os #import warnings import random import string HUG_TOKEN= os.environ["HUGGINGFACEHUB_API_TOKEN"] repo_id = "sentence-transformers/all-mpnet-base-v2" def question_pdf(llm, text, prompt): token = os.environ["HUGGINGFACEHUB_API_TOKEN"] os.environ["HUGGINGFACEHUB_API_TOKEN"] repo_id = "sentence-transformers/all-mpnet-base-v2" embeddings = HuggingFaceHubEmbeddings( repo_id=repo_id, task="feature-extraction" ) text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.create_documents(text) db = Chroma.from_documents(texts, embeddings) retriever = db.as_retriever() qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) #qa("what is data quality") return qa({"query": f"{prompt}"})#['result'] ####
[]
2024-01-10
tehruhn/ToolGPT
ToolGPT~chat_gpt_with_functions.py
import inspect import re import openai import json class ChatGPTWithFunctions: """ A class that encapsulates the interaction with OpenAI GPT-3 with functions """ def __init__(self, model="gpt-3.5-turbo-0613"): """ Constructor for ChatGPTWithFunctions class Parameters ---------- model : str, optional The OpenAI GPT model to use. Defaults to "gpt-3.5-turbo-0613". """ self.model = model @staticmethod def parse_docstring(function): """ Parse the docstring of a function to extract function name, description and parameters. Parameters ---------- function : Callable The function whose docstring is to be parsed. Returns ------- dict A dictionary with the function's name, description, and parameters. """ doc = inspect.getdoc(function) # Find function description function_description = re.search(r'(.*?)Parameters', doc, re.DOTALL).group(1).strip() # Find parameter descriptions parameters_description = re.findall(r'(\w+)\s*:\s*(\w+)\n(.*?)(?=\n\w+\s*:\s*|\nReturns|$)', doc, re.DOTALL) # Get the parameters from the function signature signature_params = list(inspect.signature(function).parameters.keys()) # Construct the parameters dictionary properties = {} required = [] for name, type, description in parameters_description: name = name.strip() type = type.strip() description = description.strip() required.append(name) properties[name] = { "type": type, "description": description, } # Check if the number of parameters match if len(signature_params) != len(required): raise ValueError(f"Number of parameters in function signature ({len(signature_params)}) does not match the number of parameters in docstring ({len(required)})") # Check if each parameter in the signature has a docstring for param in signature_params: if param not in required: raise ValueError(f"Parameter '{param}' in function signature is missing in the docstring") parameters = { "type": "object", "properties": properties, "required": required, } # Construct the function dictionary function_dict = { "name": function.__name__, "description": function_description, "parameters": parameters, } return function_dict @staticmethod def get_role_message_dict(role, content=None, fn_name=None, arguments=None, result=None): """ Get message dicts for different roles. Parameters ---------- role : str The role of the user. content : str, optional The content of the message. Defaults to None. fn_name : str, optional The name of the function. Defaults to None. arguments : dict, optional The arguments for the function. Defaults to None. result : Any, optional The result of the function. Defaults to None. Returns ------- dict The dictionary with the role, content, function name, arguments, and result. """ message_dict = {"role":role} if role == "user": message_dict["content"] = content elif role == "assistant": message_dict["content"] = content message_dict["function_call"] = {} message_dict["function_call"]["name"] = fn_name message_dict["function_call"]["arguments"] = arguments elif role == "function": message_dict["name"] = fn_name message_dict["content"] = f'{{"result": {str(result)} }}' return message_dict def run_with_functions(self, messages, function_dicts): """ Gets the ChatGPT completion based on list of given function_dicts. Parameters ---------- messages : list List of message dictionaries. function_dicts : list List of function dictionaries. Returns ------- OpenAI.ChatCompletion The response from ChatCompletion API. """ response = openai.ChatCompletion.create( model=self.model, messages=messages, functions=function_dicts, temperature=0, ) return response def prompt_with_functions(self, prompt, functions): """ Runs the prompt with given functions. Parameters ---------- prompt : str The prompt to be used with the GPT model. functions : list List of functions to be used with the GPT model. """ print(prompt) fn_names_dict = {} for fn in functions: fn_names_dict[fn.__name__] = fn function_dicts = [self.parse_docstring(fun) for fun in functions] messages = [self.get_role_message_dict("user", content=(prompt))] while True: response = self.run_with_functions(messages, function_dicts) if response.choices[0]["finish_reason"] == "stop": print(response.choices[0]["message"]["content"]) print("Received STOP signal") break elif response.choices[0]["finish_reason"] == "function_call": print("Received FUNCTION_CALL signal") fn_name = response.choices[0].message["function_call"].name arguments = response.choices[0].message["function_call"].arguments print(arguments) json_arguments = json.loads(arguments) function = fn_names_dict[fn_name] result = function(**json_arguments) messages.append(self.get_role_message_dict("assistant", fn_name=fn_name, arguments=arguments)) messages.append(self.get_role_message_dict("function", fn_name=fn_name, result=result)) response = self.run_with_functions(messages, function_dicts)
[]
2024-01-10
tehruhn/ToolGPT
examples~sql_example~sqlExample.py
from ToolGPT import ChatGPTWithFunctions import os import openai from dotenv import load_dotenv from sqlMethods import get_max_sale, get_top_rows, setup_database load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") # # Uncomment this to make the table # setup_database() prompt = "What is the Max Sale? My table is sales_data.db" wrapper = ChatGPTWithFunctions() ans = wrapper.prompt_with_functions(prompt, [get_max_sale, get_top_rows]) print(ans)
[ "What is the Max Sale? My table is sales_data.db" ]
2024-01-10
tehruhn/ToolGPT
examples~algebra_example~algebraExample.py
from ToolGPT import ChatGPTWithFunctions import os import openai from dotenv import load_dotenv from algebraMethods import add, mul, sub load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") prompt = "What is five plus ten minus fifteen times two?" wrapper = ChatGPTWithFunctions() ans = wrapper.prompt_with_functions(prompt, [add, sub, mul]) print(ans)
[ "What is five plus ten minus fifteen times two?" ]
2024-01-10
tehruhn/ToolGPT
examples~powerpoint_example~powerpointExample.py
from ToolGPT import ChatGPTWithFunctions import os import openai from dotenv import load_dotenv from powerpointMethods import create_presentation, add_slide_with_bullets load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") prompt = "Make a 5 page presentation about bananas." wrapper = ChatGPTWithFunctions() ans = wrapper.prompt_with_functions(prompt, [create_presentation, add_slide_with_bullets]) print(ans)
[ "Make a 5 page presentation about bananas." ]
2024-01-10
ansariparvej/InternGPT
iGPT~controllers~ConversationBot.py
import inspect import re import os import numpy as np import uuid import shutil import whisper import torch import gradio as gr import imageio from io import BytesIO import requests as req from PIL import Image from langchain.agents.initialize import initialize_agent from langchain.agents.tools import Tool from langchain.chains.conversation.memory import ConversationBufferMemory from langchain.llms.openai import OpenAI from ..models import * from iGPT.models.utils import (gen_new_name, to_image, seed_everything, add_points_to_image) from ..models.drag_gan import drag_gan INTERN_GPT_PREFIX = """InternGPT is designed to be able to assist with a wide range of text and visual related tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. InternGPT is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. InternGPT is able to process and understand large amounts of text and images. As a language model, InternGPT can not directly read images, but it has a list of tools to finish different visual tasks. Each image will have a file name formed as "image/xxx.png", and InternGPT can invoke different tools to indirectly understand pictures. When talking about images, InternGPT is very strict to the file name and will never fabricate nonexistent files. When using tools to generate new image files, InternGPT is also known that the image may not be the same as the user's demand, and will use other visual question answering tools or description tools to observe the real image. InternGPT is able to use tools in a sequence, and is loyal to the tool observation outputs rather than faking the image content and image file name. It will remember to provide the file name from the last tool observation, if a new image is generated. Human may provide new figures to InternGPT with a description. The description helps InternGPT to understand this image, but InternGPT should use tools to finish following tasks, rather than directly imagine from the description. Overall, InternGPT is a powerful visual dialogue assistant tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. TOOLS: ------ InternGPT has access to the following tools:""" INTERN_GPT_FORMAT_INSTRUCTIONS = """To use a tool, please use the following format: ``` Thought: Do I need to use a tool? Yes Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ``` When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format: ``` Thought: Do I need to use a tool? No {ai_prefix}: [your response here] ``` """ INTERN_GPT_SUFFIX = """You are very strict to the filename correctness and will never fake a file name if it does not exist. You will remember to provide the image file name loyally if it's provided in the last tool observation. Begin! Previous conversation history: {chat_history} New input: {input} Since InternGPT is a text language model, InternGPT must use tools to observe images rather than imagination. The thoughts and observations are only visible for InternGPT, InternGPT should remember to repeat important information in the final response for Human. Thought: Do I need to use a tool? {agent_scratchpad} Let's think step by step. """ INTERN_GPT_PREFIX_CN = """InternGPT ๆ—จๅœจ่ƒฝๅคŸๅๅŠฉๅฎŒๆˆ่Œƒๅ›ดๅนฟๆณ›็š„ๆ–‡ๆœฌๅ’Œ่ง†่ง‰็›ธๅ…ณไปปๅŠก๏ผŒไปŽๅ›ž็ญ”็ฎ€ๅ•็š„้—ฎ้ข˜ๅˆฐๆไพ›ๅฏนๅนฟๆณ›ไธป้ข˜็š„ๆทฑๅ…ฅ่งฃ้‡Šๅ’Œ่ฎจ่ฎบใ€‚ InternGPT ่ƒฝๅคŸๆ นๆฎๆ”ถๅˆฐ็š„่พ“ๅ…ฅ็”Ÿๆˆ็ฑปไผผไบบ็ฑป็š„ๆ–‡ๆœฌ๏ผŒไฝฟๅ…ถ่ƒฝๅคŸ่ฟ›่กŒๅฌ่ตทๆฅ่‡ช็„ถ็š„ๅฏน่ฏ๏ผŒๅนถๆไพ›่ฟž่ดฏไธ”ไธŽๆ‰‹ๅคดไธป้ข˜็›ธๅ…ณ็š„ๅ“ๅบ”ใ€‚ InternGPT ่ƒฝๅคŸๅค„็†ๅ’Œ็†่งฃๅคง้‡ๆ–‡ๆœฌๅ’Œๅ›พๅƒใ€‚ไฝœไธบไธ€็ง่ฏญ่จ€ๆจกๅž‹๏ผŒInternGPT ไธ่ƒฝ็›ดๆŽฅ่ฏปๅ–ๅ›พๅƒ๏ผŒไฝ†ๅฎƒๆœ‰ไธ€็ณปๅˆ—ๅทฅๅ…ทๆฅๅฎŒๆˆไธๅŒ็š„่ง†่ง‰ไปปๅŠกใ€‚ๆฏๅผ ๅ›พ็‰‡้ƒฝไผšๆœ‰ไธ€ไธชๆ–‡ไปถๅ๏ผŒๆ ผๅผไธบโ€œimage/xxx.pngโ€๏ผŒInternGPTๅฏไปฅ่ฐƒ็”จไธๅŒ็š„ๅทฅๅ…ทๆฅ้—ดๆŽฅ็†่งฃๅ›พ็‰‡ใ€‚ๅœจ่ฐˆ่ฎบๅ›พ็‰‡ๆ—ถ๏ผŒInternGPT ๅฏนๆ–‡ไปถๅ็š„่ฆๆฑ‚้žๅธธไธฅๆ ผ๏ผŒ็ปไธไผšไผช้€ ไธๅญ˜ๅœจ็š„ๆ–‡ไปถใ€‚ๅœจไฝฟ็”จๅทฅๅ…ท็”Ÿๆˆๆ–ฐ็š„ๅ›พๅƒๆ–‡ไปถๆ—ถ๏ผŒInternGPTไนŸ็Ÿฅ้“ๅ›พๅƒๅฏ่ƒฝไธŽ็”จๆˆท้œ€ๆฑ‚ไธไธ€ๆ ท๏ผŒไผšไฝฟ็”จๅ…ถไป–่ง†่ง‰้—ฎ็ญ”ๅทฅๅ…ทๆˆ–ๆ่ฟฐๅทฅๅ…ทๆฅ่ง‚ๅฏŸ็œŸๅฎžๅ›พๅƒใ€‚ InternGPT ่ƒฝๅคŸๆŒ‰้กบๅบไฝฟ็”จๅทฅๅ…ท๏ผŒๅนถไธ”ๅฟ ไบŽๅทฅๅ…ท่ง‚ๅฏŸ่พ“ๅ‡บ๏ผŒ่€Œไธๆ˜ฏไผช้€ ๅ›พๅƒๅ†…ๅฎนๅ’Œๅ›พๅƒๆ–‡ไปถๅใ€‚ๅฆ‚ๆžœ็”Ÿๆˆๆ–ฐๅ›พๅƒ๏ผŒๅฎƒๅฐ†่ฎฐๅพ—ๆไพ›ไธŠๆฌกๅทฅๅ…ท่ง‚ๅฏŸ็š„ๆ–‡ไปถๅใ€‚ Human ๅฏ่ƒฝไผšๅ‘ InternGPT ๆไพ›ๅธฆๆœ‰ๆ่ฟฐ็š„ๆ–ฐๅ›พๅฝขใ€‚ๆ่ฟฐๅธฎๅŠฉ InternGPT ็†่งฃ่ฟ™ไธชๅ›พๅƒ๏ผŒไฝ† InternGPT ๅบ”่ฏฅไฝฟ็”จๅทฅๅ…ทๆฅๅฎŒๆˆไปฅไธ‹ไปปๅŠก๏ผŒ่€Œไธๆ˜ฏ็›ดๆŽฅไปŽๆ่ฟฐไธญๆƒณ่ฑกใ€‚ๆœ‰ไบ›ๅทฅๅ…ทๅฐ†ไผš่ฟ”ๅ›ž่‹ฑๆ–‡ๆ่ฟฐ๏ผŒไฝ†ไฝ ๅฏน็”จๆˆท็š„่Šๅคฉๅบ”ๅฝ“้‡‡็”จไธญๆ–‡ใ€‚ ๆ€ป็š„ๆฅ่ฏด๏ผŒInternGPT ๆ˜ฏไธ€ไธชๅผบๅคง็š„ๅฏ่ง†ๅŒ–ๅฏน่ฏ่พ…ๅŠฉๅทฅๅ…ท๏ผŒๅฏไปฅๅธฎๅŠฉๅค„็†่Œƒๅ›ดๅนฟๆณ›็š„ไปปๅŠก๏ผŒๅนถๆไพ›ๅ…ณไบŽ่Œƒๅ›ดๅนฟๆณ›็š„ไธป้ข˜็š„ๆœ‰ไปทๅ€ผ็š„่ง่งฃๅ’Œไฟกๆฏใ€‚ ๅทฅๅ…ทๅˆ—่กจ: ------ InternGPT ๅฏไปฅไฝฟ็”จ่ฟ™ไบ›ๅทฅๅ…ท:""" INTERN_GPT_FORMAT_INSTRUCTIONS_CN = """็”จๆˆทไฝฟ็”จไธญๆ–‡ๅ’Œไฝ ่ฟ›่กŒ่Šๅคฉ๏ผŒไฝ†ๆ˜ฏๅทฅๅ…ท็š„ๅ‚ๆ•ฐๅบ”ๅฝ“ไฝฟ็”จ่‹ฑๆ–‡ใ€‚ๅฆ‚ๆžœ่ฆ่ฐƒ็”จๅทฅๅ…ท๏ผŒไฝ ๅฟ…้กป้ตๅพชๅฆ‚ไธ‹ๆ ผๅผ: ``` Thought: Do I need to use a tool? Yes Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ``` ๅฝ“ไฝ ไธๅ†้œ€่ฆ็ปง็ปญ่ฐƒ็”จๅทฅๅ…ท๏ผŒ่€Œๆ˜ฏๅฏน่ง‚ๅฏŸ็ป“ๆžœ่ฟ›่กŒๆ€ป็ป“ๅ›žๅคๆ—ถ๏ผŒไฝ ๅฟ…้กปไฝฟ็”จๅฆ‚ไธ‹ๆ ผๅผ๏ผš ``` Thought: Do I need to use a tool? No {ai_prefix}: [your response here] ``` """ INTERN_GPT_SUFFIX_CN = """ไฝ ๅฏนๆ–‡ไปถๅ็š„ๆญฃ็กฎๆ€ง้žๅธธไธฅๆ ผ๏ผŒ่€Œไธ”ๆฐธ่ฟœไธไผšไผช้€ ไธๅญ˜ๅœจ็š„ๆ–‡ไปถใ€‚ ๅผ€ๅง‹! ๅ› ไธบInternGPTๆ˜ฏไธ€ไธชๆ–‡ๆœฌ่ฏญ่จ€ๆจกๅž‹๏ผŒๅฟ…้กปไฝฟ็”จๅทฅๅ…ทๅŽป่ง‚ๅฏŸๅ›พ็‰‡่€Œไธๆ˜ฏไพ้ ๆƒณ่ฑกใ€‚ ๆŽจ็†ๆƒณๆณ•ๅ’Œ่ง‚ๅฏŸ็ป“ๆžœๅชๅฏนInternGPTๅฏ่ง๏ผŒ้œ€่ฆ่ฎฐๅพ—ๅœจๆœ€็ปˆๅ›žๅคๆ—ถๆŠŠ้‡่ฆ็š„ไฟกๆฏ้‡ๅค็ป™็”จๆˆท๏ผŒไฝ ๅช่ƒฝ็ป™็”จๆˆท่ฟ”ๅ›žไธญๆ–‡ๅฅๅญใ€‚ๆˆ‘ไปฌไธ€ๆญฅไธ€ๆญฅๆ€่€ƒใ€‚ๅœจไฝ ไฝฟ็”จๅทฅๅ…ทๆ—ถ๏ผŒๅทฅๅ…ท็š„ๅ‚ๆ•ฐๅช่ƒฝๆ˜ฏ่‹ฑๆ–‡ใ€‚ ่Šๅคฉๅކๅฒ: {chat_history} ๆ–ฐ่พ“ๅ…ฅ: {input} Thought: Do I need to use a tool? {agent_scratchpad} """ def cut_dialogue_history(history_memory, keep_last_n_words=500): if history_memory is None or len(history_memory) == 0: return history_memory tokens = history_memory.split() n_tokens = len(tokens) print(f"history_memory:{history_memory}, n_tokens: {n_tokens}") if n_tokens < keep_last_n_words: return history_memory paragraphs = history_memory.split('\n') last_n_tokens = n_tokens while last_n_tokens >= keep_last_n_words: last_n_tokens -= len(paragraphs[0].split(' ')) paragraphs = paragraphs[1:] return '\n' + '\n'.join(paragraphs) class ConversationBot: def __init__(self, load_dict, chat_disabled=False): print(f"Initializing InternGPT, load_dict={load_dict}") self.chat_disabled = chat_disabled self.models = {} self.audio_model = whisper.load_model("small").to('cuda:0') # Load Basic Foundation Models for class_name, device in load_dict.items(): self.models[class_name] = globals()[class_name](device=device) # Load Template Foundation Models for class_name, module in globals().items(): if getattr(module, 'template_model', False): template_required_names = {k for k in inspect.signature(module.__init__).parameters.keys() if k!='self'} loaded_names = set([type(e).__name__ for e in self.models.values()]) if template_required_names.issubset(loaded_names): self.models[class_name] = globals()[class_name]( **{name: self.models[name] for name in template_required_names}) self.tools = [] for instance in self.models.values(): for e in dir(instance): if e.startswith('inference'): func = getattr(instance, e) self.tools.append(Tool(name=func.name, description=func.description, func=func)) def init_agent(self): memory = ConversationBufferMemory(memory_key="chat_history", output_key='output') llm = OpenAI(temperature=0) agent = initialize_agent( self.tools, llm, agent="conversational-react-description", verbose=True, memory=memory, return_intermediate_steps=True, agent_kwargs={'prefix': INTERN_GPT_PREFIX, 'format_instructions': INTERN_GPT_FORMAT_INSTRUCTIONS, 'suffix': INTERN_GPT_SUFFIX}, ) user_state = [{'agent': agent, 'memory': memory, 'StyleGAN': {}}] return user_state def find_latest_image(self, file_list): res = None prev_mtime = None for file_item in file_list: file_path = os.path.basename(file_item[0]) if not os.path.exists(file_item[0]): continue if res is None: res = file_item[0] ms = int(file_path.split('_')[0][3:]) * 0.001 prev_mtime = int(os.path.getmtime(file_item[0])) + ms else: ms = int(file_path.split('_')[0][3:]) * 0.001 cur_mtime = int(os.path.getmtime(file_item[0])) + ms # cur_mtime = cur_mtime + ms if cur_mtime > prev_mtime: prev_mtime = cur_mtime res = file_item[0] return res def run_task(self, use_voice, text, audio_path, state, user_state): if use_voice: state, _, user_state = self.run_audio(audio_path, state, user_state) else: state, _, user_state = self.run_text(text, state, user_state) return state, state, user_state def find_param(self, msg, keyword, excluded=False): p1 = re.compile(f'(image/[-\\w]*.(png|mp4))') p2 = re.compile(f'(image/[-\\w]*{keyword}.(png|mp4))') if keyword == None or len(keyword) == 0: out_filenames = p1.findall(msg) elif not excluded: out_filenames = p2.findall(msg) elif excluded: all_files = p1.findall(msg) excluded_files = p2.findall(msg) out_filenames = set(all_files) - set(excluded_files) res = self.find_latest_image(out_filenames) return res def rectify_action(self, inputs, history_msg, agent): print('Rectify the action.') print(inputs) func = None func_name = None func_inputs = None res = None if 'extract' in inputs.lower() or 'save' in inputs.lower(): cls = self.models.get('ExtractMaskedAnything', None) if cls is not None: func = cls.inference mask_path = self.find_param(inputs, 'mask') if mask_path is None: mask_path = self.find_param(history_msg, 'mask') img_path = self.find_parent(mask_path, history_msg+inputs) if img_path is None: img_path = self.find_param(history_msg+inputs, 'mask', excluded=True) func_inputs = f'{img_path},{mask_path}' func_name = 'ExtractMaskedAnything' elif 'generate' in inputs.lower() or 'beautify' in inputs.lower(): # print('*' * 40) cls = self.models.get('ImageText2Image', None) if cls is not None: func = cls.inference img_path = self.find_param(inputs, '') if img_path is None: img_path = self.find_param(history_msg, '') # img_path = self.find_param(history_msg, 'raw') prompt = inputs.strip() func_inputs = f'{img_path},{prompt}' func_name = 'ImageText2Image' elif 'describe' in inputs.lower() or 'introduce' in inputs.lower(): cls = self.models.get('HuskyVQA', None) func_name = 'HuskyVQA' if cls is not None and 'mask' in inputs.lower(): prompt = inputs.strip() func = cls.inference_by_mask mask_path = self.find_param(inputs, 'mask') if mask_path is None: mask_path = self.find_param(history_msg, 'mask') img_path = self.find_parent(mask_path, history_msg+inputs) if img_path is None: img_path = self.find_param(history_msg+inputs, 'mask', excluded=True) func_inputs = f'{img_path},{mask_path},{prompt}' elif cls is not None: prompt = inputs.strip() func = cls.inference img_path = self.find_param(inputs, 'mask', excluded=True) if img_path is None: img_path = self.find_param(history_msg, 'mask', excluded=True) func_inputs = f'{img_path}' elif 'image' in inputs.lower() or 'figure' in inputs.lower() or 'picture' in inputs.lower(): cls = self.models.get('HuskyVQA', None) func_name = 'HuskyVQA' if cls is not None: func = cls.inference img_path = self.find_param(inputs, 'mask', excluded=True) if img_path is None: img_path = self.find_param(history_msg, 'mask', excluded=True) prompt = inputs.strip() func_inputs = f'{img_path},{prompt}' else: def only_chat(inputs): if not self.chat_disabled: res = agent(f"You can use history message to respond to the following question without using any tools. Request: {inputs}") res = res['output'].replace("\\", "/") else: res = f"The chat-related functions is now disabled. Please try other features." return res func_name = 'ChatGPT' func_inputs = inputs func = only_chat print(f'{func_name}: {func_inputs}') return_res = None if func is None: res = f"I have tried to use the tool: \"{func_name}\" to acquire the results, but it is not sucessfully loaded." else: return_res = func(func_inputs) if os.path.exists(return_res): res = f"I have used the tool: \"{func_name}\" with the inputs: \"{func_inputs}\" to get the results. The result image is named {return_res}." else: res = return_res print(f"I have used the tool: \"{func_name}\" to obtain the results. The Inputs: \"{func_inputs}\". Result: {return_res}.") return res def check_illegal_files(self, file_list): illegal_files = [] for file_item in file_list: if not os.path.exists(file_item[0]): illegal_files.append(file_item[0]) return illegal_files def find_parent(self, cur_path, history_msg): if cur_path is None: return None root_path = os.path.dirname(cur_path) name = os.path.basename(cur_path) name = name.split('.')[0] parent_name = name.split('_')[1] # p1 = re.compile(f'(image/[-\\w]*.(png|mp4))') p = re.compile(f'(image/{parent_name}[-\\w]*.(png|mp4))') out_filenames = p.findall(history_msg) if len(out_filenames) > 0: out_filenames = out_filenames[0][0] else: out_filenames = None all_file_items = os.listdir(f'{root_path}') for item in all_file_items: if item.startswith(parent_name): out_filenames = os.path.join(root_path, item) # out_filenames = item break print(f'{cur_path}, parent path: {out_filenames}') return out_filenames def get_suggested_inputs(self, inputs, history_msg): image_path = self.find_param(history_msg+inputs, 'mask', excluded=True) mask_path = self.find_param(history_msg+inputs, 'mask') if image_path is None or mask_path is None: return inputs prompt_template2 = f"If the tool only needs image_path, image_path might be {image_path}. If the tool only needs mask_path, mask_path might be {mask_path}. " image_path = self.find_parent(mask_path, history_msg) if image_path is None: image_path = self.find_param(history_msg+inputs, 'mask', excluded=True) prompt_template1 = f"If the tool needs both image_path and mask_path as inputs, image_path might be {image_path} and mask_path might be {mask_path}. " prompt_template3 = 'In other cases, you could refer to history message to finish the action. ' # prompt_template4 = 'Please finish my request using or not using tools. ' # prompt_template4 = 'If you understand, say \"Received\". \n' new_inputs = prompt_template1 + prompt_template2 + prompt_template3 + inputs print(f'Processed by get_suggested_inputs, prompt: {new_inputs}') return new_inputs def check_response(self, response): pattern = re.compile('(image/[-\\w]*.(png|mp4))') # img_pattern = re.compile('(image/[-\\w]*.(png|mp4))') file_items = pattern.findall(response) image_path = '' mask_path = '' for item in file_items: if len(image_path) == 0 and '_image.' in item[0]: image_path = item[0] elif len(mask_path) == 0 and '_mask.' in item[0]: mask_path = item[0] if len(image_path) == 0 or len(mask_path) == 0: return True res = self.find_param(response, '') if res == image_path: return True img_idx = response.find(image_path) mask_idx = response.find(mask_path) if mask_idx < img_idx: return False return True def exec_simple_action(self, inputs, history_msg): print('Execute the simple action without ChatGPT.') print('history_msg: ', history_msg + inputs) print('inputs: ', inputs) func = None func_name = None func_inputs = None res = None new_inputs = inputs.replace('ReplaceMaskedAnything', 'placeholder') if 'remove' in inputs.lower() or 'erase' in inputs.lower(): cls = self.models.get('LDMInpainting', None) if cls is not None: func = cls.inference mask_path = self.find_param(inputs, 'mask') if mask_path is None: mask_path = self.find_param(history_msg, 'mask') if mask_path is None: return 'I can not found the mask_path. Please check you have successfully operated on input image.' img_path = self.find_parent(mask_path, history_msg+inputs) if img_path is None: img_path = self.find_param(history_msg+inputs, 'mask', excluded=True) func_inputs = f'{img_path},{mask_path}' func_name = 'LDMInpainting' elif 'replace' in new_inputs.lower(): cls = self.models.get('ReplaceMaskedAnything', None) if cls is not None: func = cls.inference mask_path = self.find_param(inputs, 'mask') if mask_path is None: mask_path = self.find_param(history_msg, 'mask') if mask_path is None: return 'I can not found the mask_path. Please check you have successfully operated on input image.' img_path = self.find_parent(mask_path, history_msg+inputs) if img_path is None: img_path = self.find_param(history_msg+inputs, 'mask', excluded=True) if img_path is None: return 'I can not found the image_path. Please check you have successfully uploaded an input image.' func_inputs = f'{img_path},{mask_path},{inputs}' func_name = 'ReplaceMaskedAnything' print(f'{func_name}: {func_inputs}') if func is None: return None return_res = func(func_inputs) res = f"I have used the tool: \"{func_name}\" with the inputs: \"{func_inputs}\" to get the results. The result image is named {return_res}." print(res) return res def exec_agent(self, inputs, agent): # pattern = re.compile('(image/[-\\w]*.(png|mp4))') response = agent({"input": inputs})['output'] response = response.replace("\\", "/") using_tool_words = "I used the tool" if using_tool_words not in response: response = "For a short period of time in the future, I cannot chat with you due to some policy requirements. I hope you can understand." return response nonsense_words = 'I do not need to use a tool' if nonsense_words in response.split('.')[0] and len(response.split('.')) > 1: response = '.'.join(response.split('.')[1:]) if not self.check_response(response): raise RuntimeError('Arguments are not matched.') return response def find_result_path(self, inputs): pattern = re.compile('(image/[-\\w]*.(png|mp4))') out_filenames = pattern.findall(inputs) illegal_files = self.check_illegal_files(out_filenames) if len(illegal_files) > 0: raise FileNotFoundError(f'{illegal_files} do (does) not exist.') res = self.find_latest_image(out_filenames) print(f'The latest file is {res}.') return res def read_images_from_internet(self, inputs, user_state): urls = re.findall('(https?://[a-zA-Z0-9\.\?/%-_]*)', inputs) state = [] for url in urls: try: response = req.get(url) bytes = BytesIO(response.content) image = Image.open(bytes) image_caption, ocr_res_raw, image_filename = self.process_image(image) _, user_state = self.put_image_info_into_memory(image_caption, ocr_res_raw, image_filename, user_state) # state += [(, None)] state += [(None, f"![](file={image_filename})*{image_filename}(From: {url})*")] inputs = inputs.replace(url, image_filename) except Exception as e: print(e) print(f'Error: {url} is not an Image!') return inputs, state, user_state def run_text(self, text, state, user_state): text = text.strip() torch.cuda.empty_cache() torch.cuda.ipc_collect() if text is None or len(text) == 0: state += [(None, 'Please input text.')] return state, state, user_state new_inputs, new_state, user_state = self.read_images_from_internet(text, user_state) agent = user_state[0]['agent'] agent.memory.buffer = cut_dialogue_history(agent.memory.buffer, keep_last_n_words=500) history_msg = agent.memory.buffer[:] try: response = self.exec_simple_action(new_inputs, history_msg) if response is None: # inputs = self.get_suggested_inputs(text, history_msg) response = self.exec_agent(new_inputs, agent) else: agent.memory.buffer += f'\nHuman: {new_inputs}\n' + f'AI: {response})' res = self.find_result_path(response) except Exception as err1: print(f'Error in line {err1.__traceback__.tb_lineno}: {err1}') try: response = self.rectify_action(new_inputs, history_msg, agent) res = self.find_result_path(response) agent.memory.buffer += f'\nHuman: {text}\n' + f'AI: {response}' except Exception as err2: print(f'Error in line {err2.__traceback__.tb_lineno}: {err2}') state += [(text, 'Sorry, something went wrong inside the ChatGPT. Please check whether your image, video and message have been uploaded successfully.')] return state, state, user_state state += [(text, None)] + new_state if res is not None and agent.memory.buffer.count(res) <= 1: state = state + [(None, response + f' `{res}` is as follows: ')] state = state + [(None, (res, ))] else: state = state + [(None, response)] print(f"\nProcessed run_text, Input text: {text}\nCurrent state: {state}\n" f"Current Memory: {agent.memory.buffer}") return state, state, user_state def run_audio(self, audio_path, state, user_state): print(f'audio_path = {audio_path}') if audio_path is None or not os.path.exists(audio_path): state += [(None, 'No audio input. Please stop recording first and then send the audio.')] return state, state if self.audio_model is None: self.audio_model = whisper.load_model("small").to('cuda:0') text = self.audio_model.transcribe(audio_path)["text"] res = self.run_text(text, state, user_state) print(f"\nProcessed run_audio, Input transcribed audio: {text}\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return res[0], res[1], res[2] def upload_audio(self, audio_path, state, user_state): print(f'audio_path = {audio_path}') if audio_path is None or not os.path.exists(audio_path): state += [(None, 'No audio input. Please upload audio file.')] return state, state user_state = self.clear_user_state(False, user_state) audio_name = os.path.basename(audio_path) # vid_name = gen_new_name(vid_name, '', vid_name.split('.')[-1]) new_audio_path = os.path.join('./image/', audio_name) new_audio_path = gen_new_name(new_audio_path, 'audio', audio_name.split('.')[-1]) shutil.copy(audio_path, new_audio_path) user_state[0]['audio_path'] = new_audio_path Human_prompt = f'\nHuman: provide an audio file named {new_audio_path}. You should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' AI_prompt = f"Received. " user_state[0]['agent'].memory.buffer += Human_prompt + 'AI: ' + AI_prompt state = state + [((new_audio_path, ), AI_prompt)] print(f"\nProcessed upload_video, Input Audio: `{new_audio_path}`\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return state, state, user_state def process_image(self, image): img = image image_filename = os.path.join('image', f"{str(uuid.uuid4())[:6]}.png") image_filename = gen_new_name(image_filename, 'image') img.save(image_filename, "PNG") img = img.convert('RGB') image_caption = None if 'HuskyVQA' in self.models.keys(): image_caption = self.models['HuskyVQA'].inference_captioning(image_filename) ocr_res_raw = None if 'ImageOCRRecognition' in self.models.keys(): # ocr_res = self.models['ImageOCRRecognition'].inference(image_filename) ocr_res_raw = self.models['ImageOCRRecognition'].readtext(image_filename) return image_caption, ocr_res_raw, image_filename def put_image_info_into_memory(self, image_caption, ocr_res_raw, image_filename, user_state): ocr_res = None state = [] Human_prompt = f'\nHuman: provide a image named {image_filename}. ' if image_caption is not None and len(image_caption) > 0: Human_prompt += f'The description is: {image_caption} ' if ocr_res_raw is not None: ocr_res = self.models['ImageOCRRecognition'].parse_result(ocr_res_raw) if ocr_res is not None and len(ocr_res) > 0: Human_prompt = f'OCR result is: {ocr_res}. ' # user_state[0]['ocr_res'] = ocr_res_raw Human_prompt += f'This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' AI_prompt = "Received. " user_state[0]['agent'].memory.buffer += Human_prompt + 'AI: ' + AI_prompt state = state + [(f"![](file={image_filename})*{image_filename}*", AI_prompt)] return state, user_state def upload_image(self, image, state, user_state): # [txt, click_img, state, user_state], [chatbot, txt, state, user_state] print('upload an image') if image is None or image.get('image', None) is None: return state, state, user_state user_state = self.clear_user_state(False, user_state) image_caption, ocr_res_raw, image_filename = self.process_image(image['image']) user_state[0]['image_path'] = image_filename user_state[0]['ocr_res'] = ocr_res_raw user_state[0]['image_caption'] = image_caption t_state, user_state = self.put_image_info_into_memory(image_caption, ocr_res_raw, image_filename, user_state) state += t_state print(f"\nProcessed upload_image, Input image: {image_filename}\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return state, state, user_state def upload_video(self, video_path, state, user_state): # self.reset() print('upload a video') user_state = self.clear_user_state(False, user_state) vid_name = os.path.basename(video_path) # vid_name = gen_new_name(vid_name, '', vid_name.split('.')[-1]) new_video_path = os.path.join('./image/', vid_name) new_video_path = gen_new_name(new_video_path, 'video', vid_name.split('.')[-1]) shutil.copy(video_path, new_video_path) user_state[0]['video_path'] = new_video_path if "VideoCaption" in self.models.keys(): description = self.models['VideoCaption'].inference(new_video_path) else: description = 'A video.' user_state[0]['video_caption'] = description Human_prompt = f'\nHuman: provide a video named {new_video_path}. The description is: {description}. This information helps you to understand this video, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' AI_prompt = f"Received. " user_state[0]['agent'].memory.buffer += Human_prompt + 'AI: ' + AI_prompt state = state + [((new_video_path, ), AI_prompt)] print(f"\nProcessed upload_video, Input video: `{new_video_path}`\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return state, state, user_state def blend_mask(self, img, mask): mask = mask.astype(np.uint8) transparency_ratio = mask.astype(np.float32) / 255 / 3 transparency_ratio = transparency_ratio[:, :, np.newaxis] mask = mask[:, :, np.newaxis] mask[mask != 0] = 255 mask= mask.repeat(3, axis=2) mask[:,:,0] = 0 mask[:,:,2] = 0 new_img_arr = img * (1 - transparency_ratio) + mask * transparency_ratio new_img_arr = np.clip(new_img_arr, 0, 255).astype(np.uint8) # print(new_img_arr.shape) return new_img_arr def process_seg(self, image, state, user_state): Human_prompt="Please process this image based on given mask." if image is None or \ user_state[0].get('image_path', None) is None or \ not os.path.exists(user_state[0]['image_path']): AI_prompt = "Please upload an image for processing." state += [(Human_prompt, AI_prompt)] return None, state, state, user_state if 'SegmentAnything' not in self.models.keys(): state += [(None, 'Please load the segmentation tool.')] return image['image'], state, state, user_state img = Image.open(user_state[0]['image_path']).convert('RGB') # print(f'user_state[0][\'image_path\'] = {user_state[0]["image_path"]}') img = np.array(img, dtype=np.uint8) mask = image['mask'].convert('L') mask = np.array(mask, dtype=np.uint8) if mask.sum() == 0: AI_prompt = "You can click the image and ask me some questions." state += [(Human_prompt, AI_prompt)] return image['image'], state, state, user_state # if 'SegmentAnything' in self.models.keys(): # self.models['SegmentAnything'].clicked_region = mask if user_state[0].get('features', None) is None: user_state[0]['features'] = self.models['SegmentAnything'].get_image_embedding(img) res_mask = self.models['SegmentAnything'].segment_by_mask(mask, user_state[0]['features']) if user_state[0].get('seg_mask', None) is not None: res_mask = np.logical_or(user_state[0]['seg_mask'], res_mask) res_mask = res_mask.astype(np.uint8)*255 user_state[0]['seg_mask'] = res_mask new_img_arr = self.blend_mask(img, res_mask) new_img = Image.fromarray(new_img_arr) res_mask_img = Image.fromarray(res_mask).convert('RGB') res_mask_path = gen_new_name(user_state[0]['image_path'], 'mask') res_mask_img.save(res_mask_path) AI_prompt = f"Received. The mask_path is named {res_mask_path}." user_state[0]['agent'].memory.buffer += '\nHuman: ' + Human_prompt + '\nAI: ' + AI_prompt # state = state + [(Human_prompt, f"![](file={seg_filename})*{AI_prompt}*")] state = state + [(Human_prompt, f'Received. The sgemented figure named `{res_mask_path}` is as follows: ')] state = state + [(None, (res_mask_path, ))] print(f"\nProcessed run_image, Input image: `{user_state[0]['image_path']}`\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return new_img, state, state, user_state def process_ocr(self, image, state, user_state): Human_prompt="Please process this image based on given mask." if image is None or \ user_state[0].get('image_path', None) is None or \ not os.path.exists(user_state[0]['image_path']): AI_prompt = "Please upload an image for processing." state += [(Human_prompt, AI_prompt)] return None, state, state, user_state uploaded_image_filename = user_state[0]['image_path'] img = np.array(image['image']) img = Image.fromarray(img) mask = image['mask'].convert('L') mask = np.array(mask, dtype=np.uint8) if mask.sum() == 0: AI_prompt = "You can click the image and ask me some questions." state += [(Human_prompt, AI_prompt)] return image['image'], state, state, user_state chosen_ocr_res = None if 'ImageOCRRecognition' in self.models.keys(): # self.models['ImageOCRRecognition'].clicked_region = mask chosen_ocr_res = self.models['ImageOCRRecognition'].get_ocr_by_mask(mask, user_state[0]['ocr_res']) else: state += [(Human_prompt, f'ImageOCRRecognition is not loaded.')] if chosen_ocr_res is not None and len(chosen_ocr_res) > 0: AI_prompt = f'OCR result: {chosen_ocr_res}' # self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + ' AI: ' + AI_prompt else: AI_prompt = 'I didn\'t find any optical characters at given location.' state = state + [(Human_prompt, AI_prompt)] user_state[0]['agent'].memory.buffer += '\nHuman: ' + Human_prompt + '\nAI: ' + AI_prompt print(f"\nProcessed process_ocr, Input image: {uploaded_image_filename}\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return image['image'], state, state, user_state def process_save(self, image, state, user_state): if image is None: state += [(None, 'Please upload an image or draw a mask.')] return None, state, state, user_state uploaded_image_filename = user_state[0].get('image_path', None) if uploaded_image_filename is None and image.get('image', None) is not None: _, state, user_state = self.upload_image(image['image'], state, user_state) elif image.get('mask', None) is None: state += [(None, 'Please upload an image or draw a mask.')] return None, state, state, user_state mask_image = image['mask'].convert('RGB') random_name = os.path.join('image', f"{str(uuid.uuid4())[:6]}.png") mask_image_name = gen_new_name(random_name, 'rawmask') mask_image.save(mask_image_name, "PNG") Human_prompt="Please save the given mask." if np.array(mask_image, dtype=np.uint8).sum() == 0: AI_prompt = "I can not find the mask. Please operate on the image at first." state += [(Human_prompt, AI_prompt)] return state, state, image['image'] AI_prompt = f'The saved mask is named {mask_image_name}: ' state = state + [(Human_prompt, AI_prompt)] state = state + [(None, (mask_image_name, ))] user_state[0]['agent'].memory.buffer = user_state[0]['agent'].memory.buffer + Human_prompt + ' AI: ' + AI_prompt print(f"\nProcessed process_ocr, Input image: {uploaded_image_filename}\nCurrent state: {state}\n" f"Current Memory: {user_state[0]['agent'].memory.buffer}") return image['image'], state, state, user_state def gen_new_image(self, state, user_state): model = self.models.get('StyleGAN', None) if model is None: state += [None, 'Please load StyleGAN!'] return None, state, state, user_state if user_state[0].get('StyleGAN', None) is None: user_state[0]['StyleGAN'] = {} styleGAN_state = user_state[0]['StyleGAN'] seed = styleGAN_state.get('seed', None) if seed is None: init_seed = 2048 seed_everything(init_seed) user_state[0]['StyleGAN']['seed'] = init_seed device = model.device g_ema = model.g_ema sample_z = torch.randn([1, 512], device=device) latent, noise = g_ema.prepare([sample_z]) sample, F = g_ema.generate(latent, noise) for i in range(len(noise)): if isinstance(noise[i], torch.Tensor): noise[i] = noise[i].to('cpu') gan_state = { 'latent': latent.to('cpu'), 'noise': noise, 'F': F.to('cpu'), 'sample': sample.to('cpu'), 'history': [] } image_arr = to_image(sample) new_image = Image.fromarray(image_arr) image_filename = os.path.join('image', f"{str(uuid.uuid4())[:6]}.png") image_filename = gen_new_name(image_filename, 'image') new_image.save(image_filename, "PNG") state = state + [(None, f"![](file={image_filename})*{image_filename}*")] user_state[0]['StyleGAN']['state'] = gan_state user_state[0]['StyleGAN']['points'] = {'end': [], 'start': []} user_state[0]['StyleGAN']['image_path'] = image_filename user_state[0]['StyleGAN']['image_size'] = model.image_size SIZE_TO_CLICK_SIZE = { 1024: 15, 256: 6 } user_state[0]['StyleGAN']['click_size'] = SIZE_TO_CLICK_SIZE[model.image_size] Human_prompt = f'\nHuman: provide a image named {image_filename}. You should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' AI_prompt = "Received. " # self.agent.memory.buffer = self.agent.memory.buffer + Human_prompt + ' AI: ' + AI_prompt user_state[0]['agent'].memory.buffer += Human_prompt + 'AI: ' + AI_prompt return image_arr, state, state, user_state def drag_it(self, image, max_iters, state, user_state): model = self.models.get('StyleGAN', None) if model is None: state += [(None, 'Please load StyleGAN!')] return image, 0, state, state, user_state if user_state[0].get('StyleGAN', None) is None: state += [(None, 'Please click the button `New Image`.')] return image, 0, state, state, user_state image_path = user_state[0]['StyleGAN'].get('image_path', None) if image_path is None: return image, 0, state, state, user_state points = user_state[0]['StyleGAN']['points'] if len(points['start']) == 0: state += [(None, f'Please click the image.')] return image, 0, state, state, user_state if len(points['start']) != len(points['end']): state += [(None, f'Start points (num={len(points["start"])}) can not match end points (num={len(points["end"])})')] return image, 0, state, state, user_state click_size = user_state[0]['StyleGAN']['click_size'] style_gan_state = user_state[0]['StyleGAN'].get('state', None) if style_gan_state is None: state += [(None, 'Please click the button `New Image`.')] return image, 0, state, state, user_state max_iters = int(max_iters) latent = style_gan_state['latent'] noise = style_gan_state['noise'] F = style_gan_state['F'] style_gan_state['history'] = [] start_points = [torch.tensor(p).float() for p in points['start']] end_points = [torch.tensor(p).float() for p in points['end']] mask = None step = 0 device = model.device latent = latent.to(device) F = F.to(device) for i in range(len(noise)): if isinstance(noise[i], torch.Tensor): noise[i] = noise[i].to(device) for sample2, latent, F, handle_points in drag_gan(model.g_ema, latent, noise, F, start_points, end_points, mask, device, max_iters=max_iters): image = to_image(sample2) style_gan_state['F'] = F.cpu() style_gan_state['latent'] = latent.cpu() style_gan_state['sample'] = sample2.cpu() points['start'] = [p.cpu().numpy().astype('int').tolist() for p in handle_points] org_image = image.copy() add_points_to_image(image, points, size=click_size) style_gan_state['history'].append(org_image) step += 1 # print(f'step = {step}') if max_iters == step: video_name = gen_new_name(image_path, 'DragGAN', 'mp4') imageio.mimsave(video_name, style_gan_state['history']) AI_prompt = f'The editing process is saved in {video_name}: ' state += [(None, AI_prompt)] # state += [None, AI_prompt] state += [(None, (video_name, ))] new_image = Image.fromarray(org_image) # image_filename = os.path.join('image', f"{str(uuid.uuid4())[:6]}.png") image_filename = gen_new_name(image_path, 'DragGAN') new_image.save(image_filename, "PNG") AI_prompt = f'The processed image is named {image_filename}: ' state += [(None, AI_prompt)] state += [(None, (image_filename, ))] user_state[0]['StyleGAN']['state'] = style_gan_state Human_prompt = f'\nHuman: provide a image named {image_filename}. You should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n' AI_prompt = "Received. " user_state[0]['agent'].memory.buffer += Human_prompt + 'AI: ' + AI_prompt del latent, sample2, F torch.cuda.empty_cache() torch.cuda.ipc_collect() yield image, step, state, state, user_state yield image, step, state, state, user_state def try_drag_it(self, state, user_state): start_point = user_state[0]['StyleGAN']['points']['start'] end_point = user_state[0]['StyleGAN']['points']['end'] if len(start_point) == len(end_point): return self.drag_it(state, user_state) return gr.update(visible=True), 0, state, state, user_state def save_points_for_drag_gan(self, image, user_state, evt: gr.SelectData): points = user_state[0]['StyleGAN']['points'] start_point = user_state[0]['StyleGAN']['points'].get('start') end_point = user_state[0]['StyleGAN']['points'].get('end') click_size = user_state[0]['StyleGAN']['click_size'] if len(start_point) > len(end_point): points['end'].append([evt.index[1], evt.index[0]]) image = add_points_to_image(image, points, size=click_size) return image, user_state points['start'].append([evt.index[1], evt.index[0]]) image = add_points_to_image(image, points, size=click_size) return image, user_state def reset_drag_points(self, image, user_state): if user_state[0].get('StyleGAN', None) is None: return image, user_state user_state[0]['StyleGAN']['points'] = {'end': [], 'start': []} gan_state = user_state[0]['StyleGAN'].get('state', None) sample = None if gan_state is not None: sample = gan_state.get('sample', None) if sample is not None: image = to_image(sample) else: image_path = user_state[0]['StyleGAN'].get('image_path', None) if image_path is not None: image = Image.open(image_path) return image, user_state def clear_user_state(self, clear_memory, user_state): new_user_state = [{}] new_user_state[0]['agent'] = user_state[0]['agent'] new_user_state[0]['memory'] = user_state[0]['memory'] if clear_memory: new_user_state[0]['memory'].clear() else: new_user_state[0]['memory'] = user_state[0]['memory'] return new_user_state
[ "Please save the given mask.", "\nHuman: provide a video named PLACEHOLDER. The description is: PLACEHOLDER. This information helps you to understand this video, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n", "If the tool only needs image_path, image_path might be PLACEHOLDER. If the tool only needs mask_path, mask_path might be PLACEHOLDER. ", "Please upload an image for processing.", "You can click the image and ask me some questions.", "\nHuman: provide an audio file named PLACEHOLDER. You should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n", "In other cases, you could refer to history message to finish the action. ", "I can not find the mask. Please operate on the image at first.", "The processed image is named PLACEHOLDER: ", "OCR result is: PLACEHOLDER. ", "Received. The mask_path is named PLACEHOLDER.", "If the tool needs both image_path and mask_path as inputs, image_path might be PLACEHOLDER and mask_path might be PLACEHOLDER. ", "Received. ", "Please process this image based on given mask.", "\nHuman: provide a image named PLACEHOLDER. You should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n", "The description is: PLACEHOLDER ", "This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n", "The saved mask is named PLACEHOLDER: ", "The editing process is saved in PLACEHOLDER: ", "\nHuman: provide a image named PLACEHOLDER. ", "I didn't find any optical characters at given location.", "OCR result: PLACEHOLDER" ]
2024-01-10
liuyaojialiuyaojia/SQL-LLM
search%20key~text-embedding-ada-002~make_index.py
# %% from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.document_loaders import TextLoader from langchain.text_splitter import CharacterTextSplitter # %% # ๅฎšไน‰ๆจกๅž‹ embeddings = OpenAIEmbeddings( model_kwargs = {"model_name": "text-embedding-ada-002"}, openai_api_key='', openai_api_base='https://openai.api2d.net/v1' ) # embeddings = OpenAIEmbeddings() # ๅฎšไน‰ๅทฅๅ…ท text_splitter = CharacterTextSplitter( chunk_size=1, chunk_overlap=0, separator = '\n' ) # %% # ๅŠ ่ฝฝๆ–‡ไปถ with open('../text-embedding-ada-002/keys.txt') as f: document = f.read() # %% keys = text_splitter.create_documents([document]) # %% db = FAISS.from_documents(keys, embeddings) # %% db.save_local("index") # %%
[]
2024-01-10
scottyhq/MintPy
mintpy~ifgram_inversion.py
#!/usr/bin/env python3 ############################################################ # Program is part of MintPy # # Copyright (c) 2013, Zhang Yunjun, Heresh Fattahi # # Author: Zhang Yunjun, Heresh Fattahi, 2013 # # Parallel support added by David Grossman, Joshua Zahner # ############################################################ # Recommend import: # from mintpy import ifgram_inversion as ifginv # # Offset inversion considerations (different from phases): # 1. spatial referencing is turned off because offset is spatially absolute measure # 2. zero value is valid for offset # 3. unit is the single look pixel size in range/azimuth directions # 4. add Az/Rg suffix in all output files to distinguish azimuth/range # 5. use residual instead of temporal coherence as quality measure import os import sys import time import argparse import warnings import h5py import numpy as np from scipy import linalg # more effieint than numpy.linalg from mintpy.objects import ifgramStack, timeseries, cluster from mintpy.simulation import decorrelation as decor from mintpy.defaults.template import get_template_content from mintpy.utils import readfile, writefile, ptime, utils as ut, arg_group # key configuration parameter name key_prefix = 'mintpy.networkInversion.' configKeys = ['obsDatasetName', 'numIfgram', 'weightFunc', 'maskDataset', 'maskThreshold', 'minRedundancy', 'minNormVelocity'] ################################################################################################ EXAMPLE = """example: ifgram_inversion.py inputs/ifgramStack.h5 -t smallbaselineApp.cfg --update ifgram_inversion.py inputs/ifgramStack.h5 -w no # turn off weight for fast processing ifgram_inversion.py inputs/ifgramStack.h5 -c no # turn off parallel processing # offset ifgram_inversion.py inputs/ifgramStack.h5 -i rangeOffset -w no -m waterMask.h5 --md offsetSNR --mt 5 ifgram_inversion.py inputs/ifgramStack.h5 -i azimuthOffset -w no -m waterMask.h5 --md offsetSNR --mt 5 """ TEMPLATE = get_template_content('invert_network') REFERENCE = """references: Berardino, P., Fornaro, G., Lanari, R., & Sansosti, E. (2002). A new algorithm for surface deformation monitoring based on small baseline differential SAR interferograms. IEEE TGRS, 40(11), 2375-2383. doi:10.1109/TGRS.2002.803792 Pepe, A., and R. Lanari (2006), On the extension of the minimum cost flow algorithm for phase unwrapping of multitemporal differential SAR interferograms, IEEE-TGRS, 44(9), 2374-2383. Perissin, D., and T. Wang (2012), Repeat-pass SAR interferometry with partially coherent targets, IEEE TGRS, 50(1), 271-280, doi:10.1109/tgrs.2011.2160644. Samiei-Esfahany, S., J. E. Martins, F. v. Leijen, and R. F. Hanssen (2016), Phase Estimation for Distributed Scatterers in InSAR Stacks Using Integer Least Squares Estimation, IEEE TGRS, 54(10), 5671-5687. Seymour, M. S., and I. G. Cumming (1994), Maximum likelihood estimation for SAR interferometry, 1994. IGARSS '94., 8-12 Aug 1994. Yunjun, Z., H. Fattahi, and F. Amelung (2019), Small baseline InSAR time series analysis: Unwrapping error correction and noise reduction, Computers & Geosciences, 133, 104331, doi:10.1016/j.cageo.2019.104331. """ def create_parser(): parser = argparse.ArgumentParser(description='Invert network of interferograms into time-series.', formatter_class=argparse.RawTextHelpFormatter, epilog=REFERENCE+'\n'+TEMPLATE+'\n'+EXAMPLE) # input dataset parser.add_argument('ifgramStackFile', help='interferograms stack file to be inverted') parser.add_argument('-t','--template', dest='templateFile', help='template text file with options') parser.add_argument('-i','-d', '--dset', dest='obsDatasetName', type=str, help='dataset name of unwrap phase / offset to be used for inversion' '\ne.g.: unwrapPhase, unwrapPhase_bridging, ...') parser.add_argument('-m','--water-mask', dest='waterMaskFile', help='Skip inversion on the masked out region, i.e. water.') # options rarely used or changed parser.add_argument('-o', '--output', dest='outfile', nargs=3, metavar=('TS_FILE', 'TCOH_FILE', 'NUM_INV_FILE'), help='Output file name. (default: %(default)s).') parser.add_argument('--ref-date', dest='ref_date', help='Reference date, first date by default.') parser.add_argument('--skip-reference','--skip-ref', dest='skip_ref', action='store_true', help='[for offset and testing] do not apply spatial referencing.') # solver solver = parser.add_argument_group('solver', 'solver for the network inversion problem') solver.add_argument('-w', '--weight-func', dest='weightFunc', default='var', choices={'var', 'fim', 'coh', 'no'}, help='function used to convert coherence to weight for inversion:\n' + 'var - inverse of phase variance due to temporal decorrelation (default)\n' + 'fim - Fisher Information Matrix as weight' + 'coh - spatial coherence\n' + 'no - no/uniform weight') solver.add_argument('--min-norm-phase', dest='minNormVelocity', action='store_false', help=('Enable inversion with minimum-norm deformation phase,' ' instead of the default minimum-norm deformation velocity.')) solver.add_argument('--norm', dest='residualNorm', default='L2', choices=['L1', 'L2'], help='Optimization mehtod, L1 or L2 norm. (default: %(default)s).') # mask mask = parser.add_argument_group('mask', 'mask observation data before inversion') mask.add_argument('--mask-dset','--mask-dataset','--md', dest='maskDataset', help='dataset used to mask unwrapPhase, e.g. coherence, connectComponent') mask.add_argument('--mask-thres','--mask-threshold','--mt', dest='maskThreshold', metavar='NUM', type=float, default=0.4, help='threshold to generate mask when mask is coherence (default: %(default)s).') mask.add_argument('--min-redun','--min-redundancy','--mr', dest='minRedundancy', metavar='NUM', type=float, default=1.0, help='minimum redundancy of interferograms for every SAR acquisition. (default: %(default)s).') # computing parser = arg_group.add_memory_argument(parser) parser = arg_group.add_parallel_argument(parser) # update / skip parser.add_argument('--update', dest='update_mode', action='store_true', help='Enable update mode, and skip inversion if output timeseries file already exists,\n' + 'readable and newer than input interferograms file') return parser def cmd_line_parse(iargs=None): parser = create_parser() inps = parser.parse_args(args=iargs) # check input file type atr = readfile.read_attribute(inps.ifgramStackFile) if atr['FILE_TYPE'] not in ['ifgramStack']: raise ValueError('input is {} file, support ifgramStack file only.'.format(atr['FILE_TYPE'])) if inps.templateFile: inps, template = read_template2inps(inps.templateFile, inps) else: template = dict() # --cluster and --num-worker option inps.numWorker = str(cluster.DaskCluster.format_num_worker(inps.cluster, inps.numWorker)) if inps.cluster and inps.numWorker == '1': print('WARNING: number of workers is 1, turn OFF parallel processing and continue') inps.cluster = None # --water-mask option if inps.waterMaskFile and not os.path.isfile(inps.waterMaskFile): inps.waterMaskFile = None # --dset option if not inps.obsDatasetName: inps.obsDatasetName = 'unwrapPhase' # determine suffix based on unwrapping error correction method obs_suffix_map = {'bridging' : '_bridging', 'phase_closure' : '_phaseClosure', 'bridging+phase_closure' : '_bridging_phaseClosure'} key = 'mintpy.unwrapError.method' if key in template.keys() and template[key]: unw_err_method = template[key].lower().replace(' ','') # fix potential typo inps.obsDatasetName += obs_suffix_map[unw_err_method] print('phase unwrapping error correction "{}" is turned ON'.format(unw_err_method)) print('use dataset "{}" by default'.format(inps.obsDatasetName)) # check if input observation dataset exists. stack_obj = ifgramStack(inps.ifgramStackFile) stack_obj.open(print_msg=False) if inps.obsDatasetName not in stack_obj.datasetNames: msg = 'input dataset name "{}" not found in file: {}'.format(inps.obsDatasetName, inps.ifgramStackFile) raise ValueError(msg) # --skip-ref option if 'offset' in inps.obsDatasetName.lower(): inps.skip_ref = True # --output option if not inps.outfile: if inps.obsDatasetName.startswith('unwrapPhase'): inps.outfile = ['timeseries.h5', 'temporalCoherence.h5', 'numInvIfgram.h5'] elif inps.obsDatasetName.startswith('azimuthOffset'): inps.outfile = ['timeseriesAz.h5', 'residualInvAz.h5', 'numInvOffset.h5'] elif inps.obsDatasetName.startswith('rangeOffset'): inps.outfile = ['timeseriesRg.h5', 'residualInvRg.h5', 'numInvOffset.h5'] elif inps.obsDatasetName.startswith('ion'): inps.outfile = ['timeseriesIon.h5', 'temporalCoherenceIon.h5', 'numInvIon.h5'] else: raise ValueError('un-recognized input observation dataset name: {}'.format(inps.obsDatasetName)) inps.tsFile, inps.invQualityFile, inps.numInvFile = inps.outfile return inps def read_template2inps(template_file, inps): """Read input template options into Namespace inps""" if not inps: inps = cmd_line_parse() iDict = vars(inps) template = readfile.read_template(template_file) template = ut.check_template_auto_value(template) keyList = [i for i in list(iDict.keys()) if key_prefix+i in template.keys()] for key in keyList: value = template[key_prefix+key] if key in ['weightFunc', 'maskDataset', 'minNormVelocity']: iDict[key] = value elif value: if key in ['maskThreshold', 'minRedundancy']: iDict[key] = float(value) elif key in ['residualNorm', 'waterMaskFile']: iDict[key] = value # computing configurations dask_key_prefix = 'mintpy.compute.' keyList = [i for i in list(iDict.keys()) if dask_key_prefix+i in template.keys()] for key in keyList: value = template[dask_key_prefix+key] if key in ['cluster', 'config']: iDict[key] = value elif value: if key in ['numWorker']: iDict[key] = str(value) elif key in ['maxMemory']: iDict[key] = float(value) # False/None --> 'no' for key in ['weightFunc']: if not iDict[key]: iDict[key] = 'no' return inps, template def run_or_skip(inps): print('-'*50) print('update mode: ON') flag = 'skip' # check output files vs input dataset if not all(os.path.isfile(i) for i in inps.outfile): flag = 'run' print('1) NOT ALL output files found: {}.'.format(inps.outfile)) else: # check if time-series file is partly written using file size # since time-series file is not compressed with h5py.File(inps.outfile[0], 'r') as f: fsize_ref = f['timeseries'].size * 4 fsize = os.path.getsize(inps.outfile[0]) if fsize <= fsize_ref: flag = 'run' print('1) output file {} is NOT fully written.'.format(inps.outfile[0])) else: print('1) output files already exist: {}.'.format(inps.outfile)) # check modification time with h5py.File(inps.ifgramStackFile, 'r') as f: ti = float(f[inps.obsDatasetName].attrs.get('MODIFICATION_TIME', os.path.getmtime(inps.ifgramStackFile))) to = min(os.path.getmtime(i) for i in inps.outfile) if ti > to: flag = 'run' print('2) output files are NOT newer than input dataset: {}.'.format(inps.obsDatasetName)) else: print('2) output dataset is newer than input dataset: {}.'.format(inps.obsDatasetName)) # check configuration if flag == 'skip': atr_ifg = readfile.read_attribute(inps.ifgramStackFile) atr_ts = readfile.read_attribute(inps.tsFile) inps.numIfgram = len(ifgramStack(inps.ifgramStackFile).get_date12_list(dropIfgram=True)) meta_keys = [i for i in ['REF_Y', 'REF_X'] if i in atr_ts.keys()] if any(str(vars(inps)[key]) != atr_ts.get(key_prefix+key, 'None') for key in configKeys): flag = 'run' print('3) NOT all key configuration parameters are the same: {}'.format(configKeys)) elif meta_keys and any(atr_ts[key] != atr_ifg[key] for key in meta_keys): flag = 'run' print('3) NOT all the metadata are the same: {}'.format(meta_keys)) else: print('3) all key configuration parameters are the same: {}.'.format(configKeys)) # result print('run or skip: {}.'.format(flag)) return flag ################################# Time-series Estimator ################################### def estimate_timeseries(A, B, tbase_diff, ifgram, weight_sqrt=None, min_norm_velocity=True, rcond=1e-5, min_redundancy=1., inv_quality_name='temporalCoherence'): """Estimate time-series from a stack/network of interferograms with Least Square minimization on deformation phase / velocity. opt 1: X = np.dot(np.dot(numpy.linalg.inv(np.dot(B.T, B)), B.T), ifgram) opt 2: X = np.dot(numpy.linalg.pinv(B), ifgram) opt 3: X = np.dot(scipy.linalg.pinv(B), ifgram) opt 4: X = scipy.linalg.lstsq(B, ifgram)[0] [recommend and used] opt 4 supports weight. scipy.linalg provides more advanced and slighted faster performance than numpy.linalg. This function relies on the LAPACK routine gelsd. It computes the minimum-norm solution to a linear least squares problem using the singular value decomposition of A and a divide and conquer method. opt 4 is faster than opt 1/2/3 because it estimates X directly without calculating the A_inv matrix. opt 2/3 is better than opt 1 because numpy.linalg.inv() can not handle rank defiency of design matrix B Traditional Small BAseline Subsets (SBAS) algorithm (Berardino et al., 2002, IEEE-TGRS) is equivalent to the setting of: min_norm_velocity=True weight_sqrt=None Parameters: A - 2D np.array in size of (num_ifgram, num_date-1) B - 2D np.array in size of (num_ifgram, num_date-1), design matrix B, each row represents differential temporal baseline history between reference and secondary date of one interferogram tbase_diff - 2D np.array in size of (num_date-1, 1), differential temporal baseline history ifgram - 2D np.array in size of (num_ifgram, num_pixel), phase/offset of all interferograms. no-data value: NaN. weight_sqrt - 2D np.array in size of (num_ifgram, num_pixel), square root of weight of all interferograms min_norm_velocity - bool, assume minimum-norm deformation velocity, or not rcond - cut-off ratio of small singular values of A or B, to maintain robustness. It's recommend to >= 1e-5 by experience, to generate reasonable result. min_redundancy - float, min redundancy defined as min num_ifgram for every SAR acquisition inv_quality_name - str, inversion quality type/name Returns: ts - 2D np.array in size of (num_date, num_pixel), phase time-series inv_quality - 1D np.array in size of (num_pixel), temporal coherence (for phase) or residual (for offset) num_inv_obs - 1D np.array in size of (num_pixel), number of observations (ifgrams / offsets) used during the inversion """ ifgram = ifgram.reshape(A.shape[0], -1) if weight_sqrt is not None: weight_sqrt = weight_sqrt.reshape(A.shape[0], -1) num_date = A.shape[1] + 1 num_pixel = ifgram.shape[1] # initial output value ts = np.zeros((num_date, num_pixel), dtype=np.float32) if inv_quality_name == 'residual': inv_quality = np.nan else: inv_quality = 0. num_inv_obs = 0 # skip nan phase/offset value # apply to the pixel-wised inversion only # since the region-wised inversion has valid obs in all pairs if np.any(np.isnan(ifgram)): flag = (~np.isnan(ifgram[:, 0])).flatten() A = A[flag, :] B = B[flag, :] # skip the pixel if its redundancy < threshold if np.min(np.sum(A != 0., axis=0)) < min_redundancy: return ts, inv_quality, num_inv_obs # check matrix invertability # for WLS only because OLS contains it already if weight_sqrt is not None: try: linalg.inv(np.dot(B.T, B)) except linalg.LinAlgError: return ts, inv_quality, num_inv_obs ifgram = ifgram[flag, :] if weight_sqrt is not None: weight_sqrt = weight_sqrt[flag, :] # update number of observations used for inversion num_inv_obs = A.shape[0] # invert time-series try: # assume minimum-norm deformation velocity if min_norm_velocity: if weight_sqrt is not None: X, e2 = linalg.lstsq(np.multiply(B, weight_sqrt), np.multiply(ifgram, weight_sqrt), cond=rcond)[:2] else: X, e2 = linalg.lstsq(B, ifgram, cond=rcond)[:2] # calc inversion quality if inv_quality_name == 'residual': inv_quality = np.sqrt(e2) if inv_quality.size == 0: inv_quality = np.nan else: inv_quality = calc_inv_quality(ifgram, B, X) # assemble time-series ts_diff = X * np.tile(tbase_diff, (1, num_pixel)) ts[1:, :] = np.cumsum(ts_diff, axis=0) # assume minimum-norm deformation phase else: if weight_sqrt is not None: X, e2 = linalg.lstsq(np.multiply(A, weight_sqrt), np.multiply(ifgram, weight_sqrt), cond=rcond)[:2] else: X, e2 = linalg.lstsq(A, ifgram, cond=rcond)[:2] # calc inversion quality if inv_quality_name == 'residual': inv_quality = np.sqrt(e2) if inv_quality.size == 0: inv_quality = np.nan else: inv_quality = calc_inv_quality(ifgram, A, X) # assemble time-series ts[1: ,:] = X except linalg.LinAlgError: pass return ts, inv_quality, num_inv_obs def calc_inv_quality(ifgram, G, X, inv_quality_name='temporalCoherence'): """Calculate the temporal coherence from the network inversion results Parameters: ifgram - 2D np.array in size of (num_ifgram, num_pixel), phase or offset G - 2D np.array in size of (num_ifgram, num_date-1), design matrix A or B X - 2D np.array in size of (num_date-1, num_pixel), solution Returns: inv_quality - 1D np.array in size of (num_pixel), temporal coherence """ num_ifgram, num_pixel = ifgram.shape inv_quality = np.zeros(num_pixel, dtype=np.float32) # chunk_size as the number of pixels chunk_size = int(ut.round_to_1(2e5 / num_ifgram)) if num_pixel > chunk_size: num_chunk = int(np.ceil(num_pixel / chunk_size)) num_chunk_step = max(1, int(ut.round_to_1(num_chunk / 5))) print('calculating {} in chunks of {} pixels: {} chunks in total ...'.format( inv_quality_name, chunk_size, num_chunk)) for i in range(num_chunk): c0 = i * chunk_size c1 = min((i + 1) * chunk_size, num_pixel) # calc residual ifgram_diff = ifgram[:, c0:c1] - np.dot(G, X[:, c0:c1]) # calc inv quality if inv_quality_name == 'residual': # square root of the L-2 norm residual inv_quality[c0:c1] = np.sqrt(np.sum(np.abs(ifgram_diff) ** 2, axis=0)) else: inv_quality[c0:c1] = np.abs(np.sum(np.exp(1j*ifgram_diff), axis=0)) / num_ifgram # print out message if (i+1) % num_chunk_step == 0: print('chunk {} / {}'.format(i+1, num_chunk)) else: # calc residual ifgram_diff = ifgram - np.dot(G, X) # calc inv quality if inv_quality_name == 'residual': # square root of the L-2 norm residual inv_quality = np.sqrt(np.sum(np.abs(ifgram_diff) ** 2, axis=0)) else: inv_quality = np.abs(np.sum(np.exp(1j*ifgram_diff), axis=0)) / num_ifgram return inv_quality ###################################### File IO ############################################ def write2hdf5_file(ifgram_file, metadata, ts, temp_coh, num_inv_ifg=None, suffix='', inps=None): stack_obj = ifgramStack(ifgram_file) stack_obj.open(print_msg=False) date_list = stack_obj.get_date_list(dropIfgram=True) # File 1 - timeseries.h5 ts_file = '{}{}.h5'.format(suffix, os.path.splitext(inps.outfile[0])[0]) metadata['REF_DATE'] = date_list[0] metadata['FILE_TYPE'] = 'timeseries' metadata['UNIT'] = 'm' print('-'*50) print('calculating perpendicular baseline timeseries') pbase = stack_obj.get_perp_baseline_timeseries(dropIfgram=True) ts_obj = timeseries(ts_file) ts_obj.write2hdf5(data=ts, dates=date_list, bperp=pbase, metadata=metadata) # File 2 - temporalCoherence.h5 out_file = '{}{}.h5'.format(suffix, os.path.splitext(inps.outfile[1])[0]) metadata['FILE_TYPE'] = 'temporalCoherence' metadata['UNIT'] = '1' print('-'*50) writefile.write(temp_coh, out_file=out_file, metadata=metadata) ## File 3 - timeseriesDecorStd.h5 #if not np.all(ts_std == 0.): # out_file = 'timeseriesDecorStd{}.h5'.format(suffix) # metadata['FILE_TYPE'] = 'timeseries' # metadata['UNIT'] = 'm' # phase2range = -1*float(stack_obj.metadata['WAVELENGTH'])/(4.*np.pi) # ts_std *= abs(phase2range) # print('-'*50) # writefile.write(ts_std, out_file=out_file, metadata=metadata, ref_file=ts_file) # File 3 - numInvIfgram.h5 out_file = 'numInvIfgram{}.h5'.format(suffix) metadata['FILE_TYPE'] = 'mask' metadata['UNIT'] = '1' print('-'*50) writefile.write(num_inv_ifg, out_file=out_file, metadata=metadata) return def split2boxes(ifgram_file, max_memory=4, print_msg=True): """Split into chunks in rows to reduce memory usage Parameters: dataset_shape - tuple of 3 int max_memory - float, max memory to use in GB print_msg - bool Returns: box_list - list of tuple of 4 int num_box - int, number of boxes """ ifg_obj = ifgramStack(ifgram_file) ifg_obj.open(print_msg=False) # dataset size: defo obs (phase / offset) + weight + time-series length = ifg_obj.length width = ifg_obj.width ds_size = (ifg_obj.numIfgram * 2 + ifg_obj.numDate + 5) * length * width * 4 num_box = int(np.ceil(ds_size * 1.5 / (max_memory * 1024**3))) y_step = int(np.rint((length / num_box) / 10) * 10) num_box = int(np.ceil(length / y_step)) if print_msg and num_box > 1: print('maximum memory size: %.1E GB' % max_memory) print('split %d lines into %d patches for processing' % (length, num_box)) print(' with each patch up to %d lines' % y_step) # y_step / num_box --> box_list box_list = [] for i in range(num_box): y0 = i * y_step y1 = min([length, y0 + y_step]) box = (0, y0, width, y1) box_list.append(box) return box_list, num_box def check_design_matrix(ifgram_file, weight_func='var'): """ Check Rank of Design matrix for weighted inversion """ date12_list = ifgramStack(ifgram_file).get_date12_list(dropIfgram=True) A = ifgramStack.get_design_matrix4timeseries(date12_list)[0] if weight_func == 'no': if np.linalg.matrix_rank(A) < A.shape[1]: print('WARNING: singular design matrix! Inversion result can be biased!') print('continue using its SVD solution on all pixels') else: if np.linalg.matrix_rank(A) < A.shape[1]: print('ERROR: singular design matrix!') print(' Input network of interferograms is not fully connected!') print(' Can not invert the weighted least square solution.') print('You could try:') print(' 1) Add more interferograms to make the network fully connected:') print(' a.k.a., no multiple subsets nor network islands') print(" 2) Use '-w no' option for non-weighted SVD solution.") raise Exception() return A def read_unwrap_phase(stack_obj, box, ref_phase, obs_ds_name='unwrapPhase', dropIfgram=True, print_msg=True): """Read unwrapPhase from ifgramStack file Parameters: stack_obj - ifgramStack object box - tuple of 4 int ref_phase - 1D array or None Returns: pha_data - 2D array of unwrapPhase in size of (num_ifgram, num_pixel) """ # Read unwrapPhase num_ifgram = stack_obj.get_size(dropIfgram=dropIfgram)[0] if print_msg: print('reading {} in {} * {} ...'.format(obs_ds_name, box, num_ifgram)) pha_data = stack_obj.read(datasetName=obs_ds_name, box=box, dropIfgram=dropIfgram, print_msg=False).reshape(num_ifgram, -1) pha_data[np.isnan(pha_data)] = 0. # read ref_phase if ref_phase is not None: # use input ref_phase array if print_msg: print('use input reference phase') elif 'refPhase' in stack_obj.datasetNames: # read refPhase from file itself if print_msg: print('read reference phase from file') with h5py.File(stack_obj.file, 'r') as f: ref_phase = f['refPhase'][:] else: raise Exception('No reference phase input/found on file!'+ ' unwrapped phase is not referenced!') # reference unwrapPhase for i in range(num_ifgram): mask = pha_data[i, :] != 0. pha_data[i, :][mask] -= ref_phase[i] return pha_data def mask_unwrap_phase(pha_data, stack_obj, box, mask_ds_name=None, mask_threshold=0.4, dropIfgram=True, print_msg=True): """Mask input unwrapped phase by setting them to np.nan.""" # Read/Generate Mask num_ifgram = stack_obj.get_size(dropIfgram=dropIfgram)[0] if mask_ds_name and mask_ds_name in stack_obj.datasetNames: if print_msg: print('reading {} in {} * {} ...'.format(mask_ds_name, box, num_ifgram)) msk_data = stack_obj.read(datasetName=mask_ds_name, box=box, dropIfgram=dropIfgram, print_msg=False).reshape(num_ifgram, -1) # set all NaN values in coherence, connectComponent, offsetSNR to zero # to avoid RuntimeWarning msg during math operation msk_data[np.isnan(msk_data)] = 0 if mask_ds_name in ['coherence', 'offsetSNR']: msk_data = msk_data >= mask_threshold if print_msg: print('mask out pixels with {} < {} by setting them to NaN'.format(mask_ds_name, mask_threshold)) elif mask_ds_name in ['connectComponent']: if print_msg: print('mask out pixels with {} == 0 by setting them to NaN'.format(mask_ds_name)) # set values of mask-out pixels to NaN pha_data[msk_data == 0.] = np.nan del msk_data return pha_data def read_coherence(stack_obj, box, dropIfgram=True, print_msg=True): """ Read spatial coherence """ num_ifgram = stack_obj.get_size(dropIfgram=dropIfgram)[0] if print_msg: print('reading coherence in {} * {} ...'.format(box, num_ifgram)) coh_data = stack_obj.read(datasetName='coherence', box=box, dropIfgram=dropIfgram, print_msg=False).reshape(num_ifgram, -1) coh_data[np.isnan(coh_data)] = 0. return coh_data def calc_weight(stack_obj, box, weight_func='var', dropIfgram=True, chunk_size=100000): """Read coherence and calculate weight from it, chunk by chunk to save memory """ print('calculating weight from spatial coherence ...') # read coherence weight = read_coherence(stack_obj, box=box, dropIfgram=dropIfgram) num_pixel = weight.shape[1] if 'NCORRLOOKS' in stack_obj.metadata.keys(): L = float(stack_obj.metadata['NCORRLOOKS']) else: # use the typical ratio of resolution vs pixel size of Sentinel-1 IW mode L = int(stack_obj.metadata['ALOOKS']) * int(stack_obj.metadata['RLOOKS']) L /= 1.94 # make sure L >= 1 L = max(np.rint(L).astype(int), 1) # convert coherence to weight chunk-by-chunk to save memory num_chunk = int(np.ceil(num_pixel / chunk_size)) print(('convert coherence to weight in chunks of {c} pixels' ': {n} chunks in total ...').format(c=chunk_size, n=num_chunk)) for i in range(num_chunk): c0 = i * chunk_size c1 = min((i + 1) * chunk_size, num_pixel) if i == 0: print_msg = True else: print_msg = False # calc weight from coherence weight[:, c0:c1] = decor.coherence2weight(weight[:, c0:c1], weight_func, L=L, epsilon=5e-2, print_msg=print_msg) weight[:, c0:c1] = np.sqrt(weight[:, c0:c1]) # print out message if (i+1) % 1 == 0: print('chunk {} / {}'.format(i+1, num_chunk)) return weight def ifgram_inversion_patch(ifgram_file, box=None, ref_phase=None, obs_ds_name='unwrapPhase', weight_func='var', water_mask_file=None, min_norm_velocity=True, mask_ds_name=None, mask_threshold=0.4, min_redundancy=1.0): """Invert one patch of an ifgram stack into timeseries. Parameters: box - tuple of 4 int, indicating (x0, y0, x1, y1) of the area of interest or None for the whole image ifgram_file - str, interferograms stack HDF5 file, e.g. ./inputs/ifgramStack.h5 ref_phase - 1D array in size of (num_ifgram), or None obs_ds_name - str, dataset to feed the inversion. weight_func - str, weight function, choose in ['no', 'fim', 'var', 'coh'] water_mask_file - str, water mask filename if available, to skip inversion on water min_norm_velocity - bool, minimize the residual phase or phase velocity mask_ds_name - str, dataset name in ifgram_file used to mask unwrapPhase pixelwisely mask_threshold - float, min coherence of pixels if mask_dataset_name='coherence' min_redundancy - float, the min number of ifgrams for every acquisition. Returns: ts - 3D array in size of (num_date, num_row, num_col) inv_quality - 2D array in size of (num_row, num_col) num_inv_ifg - 2D array in size of (num_row, num_col) box - tuple of 4 int Example: ifgram_inversion_patch('ifgramStack.h5', box=(0,200,1316,400)) """ stack_obj = ifgramStack(ifgram_file) stack_obj.open(print_msg=False) # debug #y, x = 258, 454 #box = (x, y, x+1, y+1) ## 1. input info # size if box: num_row = box[3] - box[1] num_col = box[2] - box[0] else: num_row = stack_obj.length num_col = stack_obj.width num_pixel = num_row * num_col # get tbase_diff in the unit of year date_list = stack_obj.get_date_list(dropIfgram=True) num_date = len(date_list) tbase = np.array(ptime.date_list2tbase(date_list)[0], np.float32) / 365.25 tbase_diff = np.diff(tbase).reshape(-1, 1) # design matrix date12_list = stack_obj.get_date12_list(dropIfgram=True) A, B = stack_obj.get_design_matrix4timeseries(date12_list=date12_list)[0:2] # prep for decor std time-series #if os.path.isfile('reference_date.txt'): # ref_date = str(np.loadtxt('reference_date.txt', dtype=bytes).astype(str)) #else: # ref_date = date_list[0] #Astd = stack_obj.get_design_matrix4timeseries(date12_list=date12_list, refDate=ref_date)[0] #ref_idx = date_list.index(ref_date) #time_idx = [i for i in range(num_date)] #time_idx.remove(ref_idx) # 1.1 read / calculate weight if weight_func in ['no', 'sbas']: weight = None else: weight = calc_weight(stack_obj, box, weight_func=weight_func, dropIfgram=True, chunk_size=100000) # 1.2 read / mask unwrapPhase / offset pha_data = read_unwrap_phase(stack_obj, box, ref_phase, obs_ds_name=obs_ds_name, dropIfgram=True) # translate zero phase value to nan (no-data value) # becuase it's the common filled value used in phase masking if 'phase' in obs_ds_name.lower(): pha_data[pha_data == 0.] = np.nan print('convert zero value in {} to NaN (no-data value)'.format(obs_ds_name)) pha_data = mask_unwrap_phase(pha_data, stack_obj, box, dropIfgram=True, mask_ds_name=mask_ds_name, mask_threshold=mask_threshold) # 1.3 mask of pixels to invert mask = np.ones(num_pixel, np.bool_) # 1.3.1 - Water Mask if water_mask_file: print('skip pixels (on the water) with zero value in file: {}'.format(os.path.basename(water_mask_file))) atr_msk = readfile.read_attribute(water_mask_file) len_msk, wid_msk = int(atr_msk['LENGTH']), int(atr_msk['WIDTH']) if (len_msk, wid_msk) != (stack_obj.length, stack_obj.width): raise ValueError('Input water mask file has different size from ifgramStack file.') dsNames = readfile.get_dataset_list(water_mask_file) dsName = [i for i in dsNames if i in ['waterMask', 'mask']][0] waterMask = readfile.read(water_mask_file, datasetName=dsName, box=box)[0].flatten() mask *= np.array(waterMask, dtype=np.bool_) del waterMask # 1.3.2 - Mask for NaN value in ALL ifgrams print('skip pixels with {} = NaN in all interferograms'.format(obs_ds_name)) mask *= ~np.all(np.isnan(pha_data), axis=0) # 1.3.3 Mask for zero quality measure (average spatial coherence/SNR) # usually due to lack of data in the processing quality_file = os.path.join(os.path.dirname(ifgram_file), '../avgSpatialCoh.h5') inv_quality_name = 'temporalCoherence' if 'offset' in obs_ds_name.lower(): quality_file = os.path.join(os.path.dirname(ifgram_file), '../avgSpatialSNR.h5') inv_quality_name = 'residual' if quality_file and os.path.isfile(quality_file): print('skip pixels with zero value in file: {}'.format(os.path.basename(quality_file))) quality = readfile.read(quality_file, box=box)[0].flatten() mask *= quality != 0. del quality # invert pixels on mask 1+2 num_pixel2inv = int(np.sum(mask)) idx_pixel2inv = np.where(mask)[0] print('number of pixels to invert: {} out of {} ({:.1f}%)'.format( num_pixel2inv, num_pixel, num_pixel2inv/num_pixel*100)) ## 2. inversion # 2.1 initiale the output matrices ts = np.zeros((num_date, num_pixel), np.float32) #ts_std = np.zeros((num_date, num_pixel), np.float32) inv_quality = np.zeros(num_pixel, np.float32) if 'offset' in obs_ds_name.lower(): inv_quality *= np.nan num_inv_ifg = np.zeros(num_pixel, np.int16) # return directly if there is nothing to invert if num_pixel2inv < 1: ts = ts.reshape(num_date, num_row, num_col) #ts_std = ts_std.reshape(num_date, num_row, num_col) inv_quality = inv_quality.reshape(num_row, num_col) num_inv_ifg = num_inv_ifg.reshape(num_row, num_col) return ts, inv_quality, num_inv_ifg, box # 2.2 un-weighted inversion (classic SBAS) if weight_func in ['no', 'sbas']: # a. split mask into mask_all/part_net # mask for valid (~NaN) observations in ALL ifgrams (share one B in sbas inversion) mask_all_net = np.all(~np.isnan(pha_data), axis=0) mask_all_net *= mask mask_part_net = mask ^ mask_all_net del mask # b. invert once for all pixels with obs in all ifgrams if np.sum(mask_all_net) > 0: print(('inverting pixels with valid {} in all ifgrams' ' ({:.0f} pixels; {:.1f}%) ...').format(obs_ds_name, np.sum(mask_all_net), np.sum(mask_all_net)/num_pixel2inv*100)) tsi, inv_quali, num_ifgi = estimate_timeseries(A, B, tbase_diff, ifgram=pha_data[:, mask_all_net], weight_sqrt=None, min_norm_velocity=min_norm_velocity, min_redundancy=min_redundancy, inv_quality_name=inv_quality_name) ts[:, mask_all_net] = tsi inv_quality[mask_all_net] = inv_quali num_inv_ifg[mask_all_net] = num_ifgi # c. pixel-by-pixel for pixels with obs not in all ifgrams if np.sum(mask_part_net) > 0: print(('inverting pixels with valid {} in some ifgrams' ' ({:.0f} pixels; {:.1f}%) ...').format(obs_ds_name, np.sum(mask_part_net), np.sum(mask_all_net)/num_pixel2inv*100)) num_pixel2inv = int(np.sum(mask_part_net)) idx_pixel2inv = np.where(mask_part_net)[0] prog_bar = ptime.progressBar(maxValue=num_pixel2inv) for i in range(num_pixel2inv): idx = idx_pixel2inv[i] tsi, inv_quali, num_ifgi = estimate_timeseries(A, B, tbase_diff, ifgram=pha_data[:, idx], weight_sqrt=None, min_norm_velocity=min_norm_velocity, min_redundancy=min_redundancy, inv_quality_name=inv_quality_name) ts[:, idx] = tsi.flatten() inv_quality[idx] = inv_quali num_inv_ifg[idx] = num_ifgi prog_bar.update(i+1, every=2000, suffix='{}/{} pixels'.format(i+1, num_pixel2inv)) prog_bar.close() # 2.3 weighted inversion - pixel-by-pixel else: print('inverting network of interferograms into time-series ...') prog_bar = ptime.progressBar(maxValue=num_pixel2inv) for i in range(num_pixel2inv): idx = idx_pixel2inv[i] tsi, inv_quali, num_ifgi = estimate_timeseries(A, B, tbase_diff, ifgram=pha_data[:, idx], weight_sqrt=weight[:, idx], min_norm_velocity=min_norm_velocity, min_redundancy=min_redundancy, inv_quality_name=inv_quality_name) ts[:, idx] = tsi.flatten() inv_quality[idx] = inv_quali num_inv_ifg[idx] = num_ifgi prog_bar.update(i+1, every=2000, suffix='{}/{} pixels'.format(i+1, num_pixel2inv)) prog_bar.close() del weight del pha_data ## 3. prepare output # 3.1 reshape ts = ts.reshape(num_date, num_row, num_col) #ts_std = ts_std.reshape(num_date, num_row, num_col) inv_quality = inv_quality.reshape(num_row, num_col) num_inv_ifg = num_inv_ifg.reshape(num_row, num_col) # 3.2 convert displacement unit to meter if obs_ds_name.startswith('unwrapPhase'): phase2range = -1 * float(stack_obj.metadata['WAVELENGTH']) / (4.*np.pi) ts *= phase2range print('converting LOS phase unit from radian to meter') elif obs_ds_name == 'azimuthOffset': az_pixel_size = ut.azimuth_ground_resolution(stack_obj.metadata) az_pixel_size /= float(stack_obj.metadata['ALOOKS']) ts *= az_pixel_size print('converting azimuth offset unit from pixel ({:.2f} m) to meter'.format(az_pixel_size)) elif obs_ds_name == 'rangeOffset': rg_pixel_size = float(stack_obj.metadata['RANGE_PIXEL_SIZE']) rg_pixel_size /= float(stack_obj.metadata['RLOOKS']) ts *= -1 * rg_pixel_size print('converting range offset unit from pixel ({:.2f} m) to meter'.format(rg_pixel_size)) return ts, inv_quality, num_inv_ifg, box def ifgram_inversion(inps=None): """Phase triangulatino of small baseline interferograms Parameters: inps - namespace Example: inps = cmd_line_parse() ifgram_inversion(inps) """ if not inps: inps = cmd_line_parse() start_time = time.time() ## limit the number of threads in numpy/scipy to 1 # and save the original value for roll back afterwards # becuase it does not increase the speed much but does increase the CPU usage significantly # as shown in the test note below. # Dataset: SanFranSenDT42 version 1.x, patch 1 (505 x 510 x 1021) only # Machine 1: Mac (6 Intel i7 CPUs/cores in 2.6 GHz) # | dask (worker) | OMP_NUM_THREADS | Time used (sec) | CPU usage | # | no (0) | 4 | 850 | 1 x 300% | # | no (0) | 1 | 930 | 1 x 100% | # | local (4) | 4 | 580 | 4 x 250% | # | local (4) | 1 | 420 | 4 x 100% | # Machine 2: Linux local cluster (16 Intel E5 CPUs/cores in 2.4 GHz) # | dask (worker) | OMP_NUM_THREADS | Time used (sec) | CPU usage | # | no (0) | 4 | 1400 | 1 x 400% | # | no (0) | 1 | 1250 | 1 x 100% | # | local (4) | 4 | 750 | 4 x 320% | # | local (4) | 1 | 500 | 4 x 100% | num_threads_dict = cluster.set_num_threads("1") ## 1. input info stack_obj = ifgramStack(inps.ifgramStackFile) stack_obj.open(print_msg=False) date12_list = stack_obj.get_date12_list(dropIfgram=True) date_list = stack_obj.get_date_list(dropIfgram=True) length, width = stack_obj.length, stack_obj.width # 1.1 read values on the reference pixel inps.refPhase = stack_obj.get_reference_phase(unwDatasetName=inps.obsDatasetName, skip_reference=inps.skip_ref, dropIfgram=True) # 1.2 design matrix A = stack_obj.get_design_matrix4timeseries(date12_list)[0] num_ifgram, num_date = A.shape[0], A.shape[1]+1 inps.numIfgram = num_ifgram # 1.3 print key setup info msg = '-------------------------------------------------------------------------------\n' if inps.minNormVelocity: suffix = 'deformation velocity' else: suffix = 'deformation phase' msg += 'least-squares solution with L2 min-norm on: {}\n'.format(suffix) msg += 'minimum redundancy: {}\n'.format(inps.minRedundancy) msg += 'weight function: {}\n'.format(inps.weightFunc) if inps.maskDataset: if inps.maskDataset in ['coherence', 'offsetSNR']: suffix = '{} < {}'.format(inps.maskDataset, inps.maskThreshold) else: suffix = '{} == 0'.format(inps.maskDataset) msg += 'mask out pixels with: {}\n'.format(suffix) else: msg += 'mask: no\n' if np.linalg.matrix_rank(A) < A.shape[1]: msg += '***WARNING: the network is NOT fully connected.\n' msg += '\tInversion result can be biased!\n' msg += '\tContinue to use SVD to resolve the offset between different subsets.\n' msg += '-------------------------------------------------------------------------------' print(msg) print('number of interferograms: {}'.format(num_ifgram)) print('number of acquisitions : {}'.format(num_date)) print('number of lines : {}'.format(length)) print('number of columns : {}'.format(width)) ## 2. prepare output # 2.1 metadata meta = dict(stack_obj.metadata) for key in configKeys: meta[key_prefix+key] = str(vars(inps)[key]) meta['FILE_TYPE'] = 'timeseries' meta['UNIT'] = 'm' meta['REF_DATE'] = date_list[0] # 2.2 instantiate time-series dates = np.array(date_list, dtype=np.string_) pbase = stack_obj.get_perp_baseline_timeseries(dropIfgram=True) ds_name_dict = { "date" : [dates.dtype, (num_date,), dates], "bperp" : [np.float32, (num_date,), pbase], "timeseries" : [np.float32, (num_date, length, width), None], } writefile.layout_hdf5(inps.tsFile, ds_name_dict, metadata=meta) # 2.3 instantiate invQualifyFile: temporalCoherence / residualInv if 'residual' in os.path.basename(inps.invQualityFile).lower(): inv_quality_name = 'residual' meta['UNIT'] = 'pixel' else: inv_quality_name = 'temporalCoherence' meta['UNIT'] = '1' meta['FILE_TYPE'] = inv_quality_name meta.pop('REF_DATE') ds_name_dict = {meta['FILE_TYPE'] : [np.float32, (length, width)]} writefile.layout_hdf5(inps.invQualityFile, ds_name_dict, metadata=meta) # 2.4 instantiate number of inverted observations meta['FILE_TYPE'] = 'mask' meta['UNIT'] = '1' ds_name_dict = {"mask" : [np.float32, (length, width)]} writefile.layout_hdf5(inps.numInvFile, ds_name_dict, metadata=meta) ## 3. run the inversion / estimation and write to disk # 3.1 split ifgram_file into blocks to save memory box_list, num_box = split2boxes(inps.ifgramStackFile, max_memory=inps.maxMemory) # 3.2 prepare the input arguments for *_patch() data_kwargs = { "ifgram_file" : inps.ifgramStackFile, "ref_phase" : inps.refPhase, "obs_ds_name" : inps.obsDatasetName, "weight_func" : inps.weightFunc, "min_norm_velocity" : inps.minNormVelocity, "water_mask_file" : inps.waterMaskFile, "mask_ds_name" : inps.maskDataset, "mask_threshold" : inps.maskThreshold, "min_redundancy" : inps.minRedundancy } # 3.3 invert / write block-by-block for i, box in enumerate(box_list): box_wid = box[2] - box[0] box_len = box[3] - box[1] if num_box > 1: print('\n------- processing patch {} out of {} --------------'.format(i+1, num_box)) print('box width: {}'.format(box_wid)) print('box length: {}'.format(box_len)) # update box argument in the input data data_kwargs['box'] = box if not inps.cluster: # non-parallel ts, inv_quality, num_inv_ifg = ifgram_inversion_patch(**data_kwargs)[:-1] else: # parallel print('\n\n------- start parallel processing using Dask -------') # initiate the output data ts = np.zeros((num_date, box_len, box_wid), np.float32) inv_quality = np.zeros((box_len, box_wid), np.float32) num_inv_ifg = np.zeros((box_len, box_wid), np.float32) # initiate dask cluster and client cluster_obj = cluster.DaskCluster(inps.cluster, inps.numWorker, config_name=inps.config) cluster_obj.open() # run dask ts, inv_quality, num_inv_ifg = cluster_obj.run(func=ifgram_inversion_patch, func_data=data_kwargs, results=[ts, inv_quality, num_inv_ifg]) # close dask cluster and client cluster_obj.close() print('------- finished parallel processing -------\n\n') # write the block to disk # with 3D block in [z0, z1, y0, y1, x0, x1] # and 2D block in [y0, y1, x0, x1] # time-series - 3D block = [0, num_date, box[1], box[3], box[0], box[2]] writefile.write_hdf5_block(inps.tsFile, data=ts, datasetName='timeseries', block=block) # temporal coherence - 2D block = [box[1], box[3], box[0], box[2]] writefile.write_hdf5_block(inps.invQualityFile, data=inv_quality, datasetName=inv_quality_name, block=block) # number of inverted obs - 2D writefile.write_hdf5_block(inps.numInvFile, data=num_inv_ifg, datasetName='mask', block=block) if num_box > 1: m, s = divmod(time.time() - start_time, 60) print('time used: {:02.0f} mins {:02.1f} secs.\n'.format(m, s)) # 3.4 update output data on the reference pixel (for phase) if not inps.skip_ref: # grab ref_y/x ref_y = int(stack_obj.metadata['REF_Y']) ref_x = int(stack_obj.metadata['REF_X']) print('-'*50) print('update values on the reference pixel: ({}, {})'.format(ref_y, ref_x)) print('set {} on the reference pixel to 1.'.format(inv_quality_name)) with h5py.File(inps.invQualityFile, 'r+') as f: f['temporalCoherence'][ref_y, ref_x] = 1. print('set # of observations on the reference pixel as {}'.format(num_ifgram)) with h5py.File(inps.numInvFile, 'r+') as f: f['mask'][ref_y, ref_x] = num_ifgram # roll back to the original number of threads cluster.roll_back_num_threads(num_threads_dict) m, s = divmod(time.time() - start_time, 60) print('time used: {:02.0f} mins {:02.1f} secs.\n'.format(m, s)) return ################################################################################################ def main(iargs=None): inps = cmd_line_parse(iargs) # --update option if inps.update_mode and run_or_skip(inps) == 'skip': return inps.outfile # Network Inversion if inps.residualNorm == 'L2': ifgram_inversion(inps) else: raise NotImplementedError('L1 norm minimization is not fully tested.') #ut.timeseries_inversion_L1(inps.ifgramStackFile, inps.tsFile) return inps.outfile ################################################################################################ if __name__ == '__main__': main(sys.argv[1:])
[ "invert_network", "{}" ]
2024-01-10
scottyhq/MintPy
mintpy~plot_coherence_matrix.py
#!/usr/bin/env python3 ############################################################ # Program is part of MintPy # # Copyright (c) 2013, Zhang Yunjun, Heresh Fattahi # # Author: Zhang Yunjun, Nov 2018 # ############################################################ import os import sys import argparse import numpy as np import matplotlib.pyplot as plt from mintpy.objects import ifgramStack from mintpy.utils import readfile, plot as pp, utils as ut from mintpy import view ########################### Sub Function ############################# EXAMPLE = """example: plot_coherence_matrix.py inputs/ifgramStack.h5 plot_coherence_matrix.py inputs/ifgramStack.h5 --yx 277 1069 plot_coherence_matrix.py inputs/ifgramStack.h5 --lalo -0.8493 -91.1510 -c RdBu # left: map view plot_coherence_matrix.py inputs/ifgramStack.h5 --view-cmd "view.py {} --dem inputs/gsi10m.dem.wgs84" plot_coherence_matrix.py inputs/ifgramStack.h5 --view-cmd 'view.py {} --wrap --wrap-range -3 3" plot_coherence_matrix.py inputs/ifgramStack.h5 --view-cmd 'view.py {} --sub-x 900 1400 --sub-y 0 500' # right: matrix view # show color jump same as the coherence threshold in network inversion with pixel-wised masking plot_coherence_matrix.py inputs/ifgramStack.h5 --cmap-vlist 0 0.4 1 """ def create_parser(): parser = argparse.ArgumentParser(description='Plot the coherence matrix of one pixel (interactive)', formatter_class=argparse.RawTextHelpFormatter, epilog=EXAMPLE) parser.add_argument('ifgram_file', help='interferogram stack file') parser.add_argument('--yx', type=int, metavar=('Y', 'X'), nargs=2, help='Point of interest in y(row)/x(col)') parser.add_argument('--lalo', type=float, metavar=('LAT','LON'), nargs=2, help='Point of interest in lat/lon') parser.add_argument('--lookup','--lut', dest='lookup_file', help='Lookup file to convert lat/lon into y/x') parser.add_argument('-c','--cmap', dest='cmap_name', default='RdBu_truncate', help='Colormap for coherence matrix.\nDefault: RdBu_truncate') parser.add_argument('--cmap-vlist', dest='cmap_vlist', type=float, nargs=3, default=[0.0, 0.7, 1.0], help='start/jump/end fraction for truncated colormap. Default: 0.0 0.7 1.0') parser.add_argument('--figsize','--fs', dest='fig_size', metavar=('WID', 'LEN'), type=float, nargs=2, help='figure size in inches. Default: [8, 4]') parser.add_argument('--img-file', dest='img_file', help='dataset to show in map to facilitate point selection. Default: velocity.h5') parser.add_argument('--view-cmd', dest='view_cmd', default='view.py {} --wrap --noverbose ', help='view.py command to plot the input map file\n'+ 'Default: view.py img_file --wrap --noverbose') # aux files parser.add_argument('--tcoh', dest='tcoh_file', default='temporalCoherence.h5', help='temporal coherence file.') parser.add_argument('-t','--template', dest='template_file', help='temporal file.') parser.add_argument('--save', dest='save_fig', action='store_true', help='save the figure') parser.add_argument('--nodisplay', dest='disp_fig', action='store_false', help='save and do not display the figure') parser.add_argument('--noverbose', dest='print_msg', action='store_false', help='Disable the verbose message printing.') return parser def cmd_line_parse(iargs=None): parser = create_parser() inps = parser.parse_args(args=iargs) # default aux file: mintpy_dir = os.path.dirname(os.path.dirname(inps.ifgram_file)) if not inps.img_file: inps.img_file = os.path.join(mintpy_dir, 'velocity.h5') if not inps.template_file: inps.template_file = os.path.join(mintpy_dir, 'smallbaselineApp.cfg') if not os.path.isfile(inps.img_file): raise SystemExit('ERROR: input image file not found: {}'.format(inps.img_file)) if not os.path.isfile(inps.tcoh_file): inps.tcoh_file = None if not os.path.isfile(inps.template_file): inps.tcoh_file = None # verbose print using --noverbose option global vprint vprint = print if inps.print_msg else lambda *args, **kwargs: None if not inps.disp_fig: inps.save_fig = True plt.switch_backend('Agg') return inps def read_network_info(inps): k = readfile.read_attribute(inps.ifgram_file)['FILE_TYPE'] if k != 'ifgramStack': raise ValueError('input file {} is not ifgramStack: {}'.format(inps.ifgram_file, k)) obj = ifgramStack(inps.ifgram_file) obj.open(print_msg=inps.print_msg) inps.date12_list = obj.get_date12_list(dropIfgram=False) date12_kept = obj.get_date12_list(dropIfgram=True) inps.ex_date12_list = sorted(list(set(inps.date12_list) - set(date12_kept))) inps.date_list = obj.get_date_list(dropIfgram=False) vprint('number of all interferograms: {}'.format(len(inps.date12_list))) vprint('number of dropped interferograms: {}'.format(len(inps.ex_date12_list))) vprint('number of kept interferograms: {}'.format(len(inps.date12_list) - len(inps.ex_date12_list))) vprint('number of acquisitions: {}'.format(len(inps.date_list))) if inps.lalo: if not inps.lookup_file: lookup_file = os.path.join(os.path.dirname(inps.ifgram_file), 'geometry*.h5') inps.lookup_file = ut.get_lookup_file(filePattern=lookup_file) coord = ut.coordinate(obj.metadata, lookup_file=inps.lookup_file) inps.yx = coord.geo2radar(inps.lalo[0], inps.lalo[1])[0:2] if not inps.yx: inps.yx = (obj.refY, obj.refX) vprint('plot initial coherence matrix at reference pixel: {}'.format(inps.yx)) return inps class coherenceMatrixViewer(): """class for plot_coherence_matrix Example: from mintpy.plot_coherence_matrix import coherenceMatrixViewer cmd = 'plot_coherence_matrix.py ./inputs/ifgramStack.h5 --noverbose --figsize 9 3 --yx 216 310' obj = coherenceMatrixViewer(cmd) obj.configure() obj.plot() """ def __init__(self, cmd=None, iargs=None): if cmd: iargs = cmd.split()[1:] self.cmd = cmd self.iargs = iargs # figure variables self.figname = 'Coherence matrix' self.fig_size = None self.fig = None self.ax_img = None self.ax_mat = None return def configure(self): inps = cmd_line_parse(self.iargs) # read network info inps = read_network_info(inps) # copy inps to self object for key, value in inps.__dict__.items(): setattr(self, key, value) # auto figure size if not self.fig_size: ds_shape = readfile.read(self.img_file)[0].shape fig_size = pp.auto_figure_size(ds_shape, disp_cbar=True, scale=0.7) self.fig_size = [fig_size[0]+fig_size[1], fig_size[1]] vprint('create figure in size of {} inches'.format(self.fig_size)) # read aux data # 1. temporal coherence value self.tcoh = None if self.tcoh_file: self.tcoh = readfile.read(self.tcoh_file)[0] # 2. minimum used coherence from template file self.min_coh_used = 0.0 if self.template_file: template = readfile.read_template(self.template_file) template = ut.check_template_auto_value(template) if template['mintpy.networkInversion.maskDataset'] == 'coherence': self.min_coh_used = float(template['mintpy.networkInversion.maskThreshold']) vprint('Pixel-wised masking is applied in invert_network step') return def plot(self): # Figure 1 self.fig = plt.figure(self.figname, figsize=self.fig_size) # Axes 1 - Image self.ax_img = self.fig.add_axes([0.05, 0.1, 0.4, 0.8]) view_cmd = self.view_cmd.format(self.img_file) d_img, atr, inps_img = view.prep_slice(view_cmd) if all(i is not None for i in self.yx): inps_img.pts_marker = 'r^' inps_img.pts_yx = np.array(self.yx).reshape(-1, 2) # point yx --> lalo for geocoded product if 'Y_FIRST' in atr.keys(): coord = ut.coordinate(atr) inps_img.pts_lalo = np.array(coord.radar2geo(self.yx[0], self.yx[1])[0:2]).reshape(-1,2) inps_img.print_msg = self.print_msg self.ax_img = view.plot_slice(self.ax_img, d_img, atr, inps_img)[0] # coordinate info self.coord = ut.coordinate(atr) self.fig_coord = inps_img.fig_coord # Axes 2 - coherence matrix self.ax_mat = self.fig.add_axes([0.55, 0.125, 0.40, 0.75]) self.colormap = pp.ColormapExt(self.cmap_name, vlist=self.cmap_vlist).colormap if all(i is not None for i in self.yx): self.plot_coherence_matrix4pixel(self.yx) # Link the canvas to the plots. self.cid = self.fig.canvas.mpl_connect('button_press_event', self.update_coherence_matrix) if self.disp_fig: plt.show() return def plot_coherence_matrix4pixel(self, yx): """Plot coherence matrix for one pixel Parameters: yx : list of 2 int """ self.ax_mat.cla() # read coherence box = (yx[1], yx[0], yx[1]+1, yx[0]+1) coh = readfile.read(self.ifgram_file, datasetName='coherence', box=box)[0] # ex_date for pixel-wise masking during network inversion ex_date12_list = self.ex_date12_list[:] #local copy if self.min_coh_used > 0.: ex_date12_list += np.array(self.date12_list)[coh < self.min_coh_used].tolist() ex_date12_list = sorted(list(set(ex_date12_list))) # prep metadata plotDict = {} plotDict['fig_title'] = 'Y = {}, X = {}'.format(yx[0], yx[1]) # display temporal coherence value of the pixel if self.tcoh_file: tcoh = self.tcoh[yx[0], yx[1]] plotDict['fig_title'] += ', tcoh = {:.2f}'.format(tcoh) plotDict['colormap'] = self.colormap plotDict['cmap_vlist'] = self.cmap_vlist plotDict['disp_legend'] = False # plot coh_mat = pp.plot_coherence_matrix(self.ax_mat, date12List=self.date12_list, cohList=coh.tolist(), date12List_drop=ex_date12_list, p_dict=plotDict)[1] self.ax_mat.annotate('ifgrams\navailable', xy=(0.05, 0.05), xycoords='axes fraction', fontsize=12) self.ax_mat.annotate('ifgrams\nused', ha='right', xy=(0.95, 0.85), xycoords='axes fraction', fontsize=12) # status bar def format_coord(x, y): row, col = int(y+0.5), int(x+0.5) date12 = sorted([self.date_list[row], self.date_list[col]]) date12 = ['{}-{}-{}'.format(i[0:4], i[4:6], i[6:8]) for i in date12] return 'x={}, y={}, v={:.3f}'.format(date12[0], date12[1], coh_mat[row, col]) self.ax_mat.format_coord = format_coord # info msg = 'pixel in yx = {}, '.format(tuple(yx)) msg += 'min/max spatial coherence: {:.2f} / {:.2f}, '.format(np.min(coh), np.max(coh)) if self.tcoh_file: msg += 'temporal coherence: {:.2f}'.format(tcoh) vprint(msg) self.fig.canvas.draw() return def update_coherence_matrix(self, event): if event.inaxes == self.ax_img: if self.fig_coord == 'geo': yx = [self.coord.lalo2yx(event.ydata, coord_type='lat'), self.coord.lalo2yx(event.xdata, coord_type='lon')] else: yx = [int(event.ydata+0.5), int(event.xdata+0.5)] self.plot_coherence_matrix4pixel(yx) return ########################## Main Function ############################## def main(iargs=None): obj = coherenceMatrixViewer(iargs=iargs) obj.configure() obj.plot() obj.fig.canvas.mpl_disconnect(obj.cid) return ############################################################ if __name__ == '__main__': main(sys.argv[1:])
[]
2024-01-10
johnjim0816/joyrl-offline
algos~MAPPO~envs.py
import os import time import numpy as np import torch from tensorboardX import SummaryWriter from common.memories import SharedReplayBuffer # import imageio import gym from gym import spaces def _t2n(x): """Convert torch tensor to a numpy array.""" return x.detach().cpu().numpy() class Runner(object): """ Base class for training recurrent policies. :param config: (dict) Config dictionary containing parameters for training. """ def __init__(self, config): self.all_args = config['all_args'] self.envs = config['envs'] self.eval_envs = config['eval_envs'] self.device = config['device'] self.num_agents = config['num_agents'] if config.__contains__("render_envs"): self.render_envs = config['render_envs'] # parameters self.env_name = self.all_args.env_name self.algorithm_name = self.all_args.algorithm_name self.experiment_name = self.all_args.experiment_name self.use_centralized_V = self.all_args.use_centralized_V self.use_obs_instead_of_state = self.all_args.use_obs_instead_of_state self.num_env_steps = self.all_args.num_env_steps self.episode_length = self.all_args.episode_length self.n_rollout_threads = self.all_args.n_rollout_threads self.n_eval_rollout_threads = self.all_args.n_eval_rollout_threads self.n_render_rollout_threads = self.all_args.n_render_rollout_threads self.use_linear_lr_decay = self.all_args.use_linear_lr_decay self.hidden_size = self.all_args.hidden_size self.use_render = self.all_args.use_render self.recurrent_N = self.all_args.recurrent_N # interval self.save_interval = self.all_args.save_interval self.use_eval = self.all_args.use_eval self.eval_interval = self.all_args.eval_interval self.log_interval = self.all_args.log_interval # dir self.model_dir = self.all_args.model_dir self.run_dir = config["run_dir"] self.log_dir = str(self.run_dir / 'logs') if not os.path.exists(self.log_dir): os.makedirs(self.log_dir) self.writter = SummaryWriter(self.log_dir) self.save_dir = str(self.run_dir / 'models') if not os.path.exists(self.save_dir): os.makedirs(self.save_dir) from agent import RMAPPO as TrainAlgo from agent import Agent as Policy share_observation_space = self.envs.share_observation_space[0] if self.use_centralized_V else self.envs.observation_space[0] # policy network self.policy = Policy(self.all_args, self.envs.observation_space[0], share_observation_space, self.envs.action_space[0], device = self.device) if self.model_dir is not None: self.restore() # algorithm self.trainer = TrainAlgo(self.all_args, self.policy, device = self.device) # buffer self.buffer = SharedReplayBuffer(self.all_args, self.num_agents, self.envs.observation_space[0], share_observation_space, self.envs.action_space[0]) def run(self): """Collect training data, perform training updates, and evaluate policy.""" raise NotImplementedError def warmup(self): """Collect warmup pre-training data.""" raise NotImplementedError def collect(self, step): """Collect rollouts for training.""" raise NotImplementedError def insert(self, data): """ Insert data into buffer. :param data: (Tuple) data to insert into training buffer. """ raise NotImplementedError @torch.no_grad() def compute(self): """Calculate returns for the collected data.""" self.trainer.prep_rollout() next_values = self.trainer.policy.get_values(np.concatenate(self.buffer.share_obs[-1]), np.concatenate(self.buffer.rnn_states_critic[-1]), np.concatenate(self.buffer.masks[-1])) next_values = np.array(np.split(_t2n(next_values), self.n_rollout_threads)) self.buffer.compute_returns(next_values, self.trainer.value_normalizer) def train(self): """Train policies with data in buffer. """ self.trainer.prep_training() train_infos = self.trainer.train(self.buffer) self.buffer.after_update() return train_infos def save(self): """Save policy's actor and critic networks.""" policy_actor = self.trainer.policy.actor torch.save(policy_actor.state_dict(), str(self.save_dir) + "/actor.pt") policy_critic = self.trainer.policy.critic torch.save(policy_critic.state_dict(), str(self.save_dir) + "/critic.pt") def restore(self): """Restore policy's networks from a saved model.""" policy_actor_state_dict = torch.load(str(self.model_dir) + '/actor.pt') self.policy.actor.load_state_dict(policy_actor_state_dict) if not self.all_args.use_render: policy_critic_state_dict = torch.load(str(self.model_dir) + '/critic.pt') self.policy.critic.load_state_dict(policy_critic_state_dict) def log_train(self, train_infos, total_num_steps): """ Log training info. :param train_infos: (dict) information about training update. :param total_num_steps: (int) total number of training env steps. """ for k, v in train_infos.items(): self.writter.add_scalars(k, {k: v}, total_num_steps) def log_env(self, env_infos, total_num_steps): """ Log env info. :param env_infos: (dict) information about env state. :param total_num_steps: (int) total number of training env steps. """ for k, v in env_infos.items(): if len(v)>0: self.writter.add_scalars(k, {k: np.mean(v)}, total_num_steps) def _t2n(x): return x.detach().cpu().numpy() class EnvRunner(Runner): """Runner class to perform training, evaluation. and data collection for the MPEs. See parent class for details.""" def __init__(self, config): super(EnvRunner, self).__init__(config) def run(self): self.warmup() start = time.time() episodes = ( int(self.num_env_steps) // self.episode_length // self.n_rollout_threads ) for episode in range(episodes): if self.use_linear_lr_decay: self.trainer.policy.lr_decay(episode, episodes) for step in range(self.episode_length): # Sample actions ( values, actions, action_log_probs, rnn_states, rnn_states_critic, actions_env, ) = self.collect(step) # Obser reward and next obs obs, rewards, dones, infos = self.envs.step(actions_env) data = ( obs, rewards, dones, infos, values, actions, action_log_probs, rnn_states, rnn_states_critic, ) # insert data into buffer self.insert(data) # compute return and update network self.compute() train_infos = self.train() # post process total_num_steps = ( (episode + 1) * self.episode_length * self.n_rollout_threads ) # save model if episode % self.save_interval == 0 or episode == episodes - 1: self.save() # log information if episode % self.log_interval == 0: end = time.time() print( "\n Scenario {} Algo {} Exp {} updates {}/{} episodes, total num timesteps {}/{}, FPS {}.\n".format( self.all_args.scenario_name, self.algorithm_name, self.experiment_name, episode, episodes, total_num_steps, self.num_env_steps, int(total_num_steps / (end - start)), ) ) # if self.env_name == "MPE": # env_infos = {} # for agent_id in range(self.num_agents): # idv_rews = [] # for info in infos: # if 'individual_reward' in info[agent_id].keys(): # idv_rews.append(info[agent_id]['individual_reward']) # agent_k = 'agent%i/individual_rewards' % agent_id # env_infos[agent_k] = idv_rews train_infos["average_episode_rewards"] = ( np.mean(self.buffer.rewards) * self.episode_length ) print( "average episode rewards is {}".format( train_infos["average_episode_rewards"] ) ) self.log_train(train_infos, total_num_steps) # self.log_env(env_infos, total_num_steps) # eval if episode % self.eval_interval == 0 and self.use_eval: self.eval(total_num_steps) def warmup(self): # reset env obs = self.envs.reset() # shape = (5, 2, 14) # replay buffer if self.use_centralized_V: share_obs = obs.reshape(self.n_rollout_threads, -1) # shape = (5, 28) share_obs = np.expand_dims(share_obs, 1).repeat( self.num_agents, axis=1 ) # shape = (5, 2, 28) else: share_obs = obs self.buffer.share_obs[0] = share_obs.copy() self.buffer.obs[0] = obs.copy() @torch.no_grad() def collect(self, step): self.trainer.prep_rollout() ( value, action, action_log_prob, rnn_states, rnn_states_critic, ) = self.trainer.policy.get_actions( np.concatenate(self.buffer.share_obs[step]), np.concatenate(self.buffer.obs[step]), np.concatenate(self.buffer.rnn_states[step]), np.concatenate(self.buffer.rnn_states_critic[step]), np.concatenate(self.buffer.masks[step]), ) # [self.envs, agents, dim] values = np.array(np.split(_t2n(value), self.n_rollout_threads)) actions = np.array(np.split(_t2n(action), self.n_rollout_threads)) action_log_probs = np.array( np.split(_t2n(action_log_prob), self.n_rollout_threads) ) rnn_states = np.array(np.split(_t2n(rnn_states), self.n_rollout_threads)) rnn_states_critic = np.array( np.split(_t2n(rnn_states_critic), self.n_rollout_threads) ) # rearrange action if self.envs.action_space[0].__class__.__name__ == "MultiDiscrete": for i in range(self.envs.action_space[0].shape): uc_actions_env = np.eye(self.envs.action_space[0].high[i] + 1)[ actions[:, :, i] ] if i == 0: actions_env = uc_actions_env else: actions_env = np.concatenate((actions_env, uc_actions_env), axis=2) elif self.envs.action_space[0].__class__.__name__ == "Discrete": # actions --> actions_env : shape:[10, 1] --> [5, 2, 5] actions_env = np.squeeze(np.eye(self.envs.action_space[0].n)[actions], 2) else: # TODO ่ฟ™้‡Œๆ”น้€ ๆˆ่‡ชๅทฑ็Žฏๅขƒ้œ€่ฆ็š„ๅฝขๅผๅณๅฏ # TODO Here, you can change the shape of actions_env to fit your environment actions_env = actions # raise NotImplementedError return ( values, actions, action_log_probs, rnn_states, rnn_states_critic, actions_env, ) def insert(self, data): ( obs, rewards, dones, infos, values, actions, action_log_probs, rnn_states, rnn_states_critic, ) = data rnn_states[dones == True] = np.zeros( ((dones == True).sum(), self.recurrent_N, self.hidden_size), dtype=np.float32, ) rnn_states_critic[dones == True] = np.zeros( ((dones == True).sum(), *self.buffer.rnn_states_critic.shape[3:]), dtype=np.float32, ) masks = np.ones((self.n_rollout_threads, self.num_agents, 1), dtype=np.float32) masks[dones == True] = np.zeros(((dones == True).sum(), 1), dtype=np.float32) if self.use_centralized_V: share_obs = obs.reshape(self.n_rollout_threads, -1) share_obs = np.expand_dims(share_obs, 1).repeat(self.num_agents, axis=1) else: share_obs = obs self.buffer.insert( share_obs, obs, rnn_states, rnn_states_critic, actions, action_log_probs, values, rewards, masks, ) @torch.no_grad() def eval(self, total_num_steps): eval_episode_rewards = [] eval_obs = self.eval_envs.reset() eval_rnn_states = np.zeros( (self.n_eval_rollout_threads, *self.buffer.rnn_states.shape[2:]), dtype=np.float32, ) eval_masks = np.ones( (self.n_eval_rollout_threads, self.num_agents, 1), dtype=np.float32 ) for eval_step in range(self.episode_length): self.trainer.prep_rollout() eval_action, eval_rnn_states = self.trainer.policy.act( np.concatenate(eval_obs), np.concatenate(eval_rnn_states), np.concatenate(eval_masks), deterministic=True, ) eval_actions = np.array( np.split(_t2n(eval_action), self.n_eval_rollout_threads) ) eval_rnn_states = np.array( np.split(_t2n(eval_rnn_states), self.n_eval_rollout_threads) ) if self.eval_envs.action_space[0].__class__.__name__ == "MultiDiscrete": for i in range(self.eval_envs.action_space[0].shape): eval_uc_actions_env = np.eye( self.eval_envs.action_space[0].high[i] + 1 )[eval_actions[:, :, i]] if i == 0: eval_actions_env = eval_uc_actions_env else: eval_actions_env = np.concatenate( (eval_actions_env, eval_uc_actions_env), axis=2 ) elif self.eval_envs.action_space[0].__class__.__name__ == "Discrete": eval_actions_env = np.squeeze( np.eye(self.eval_envs.action_space[0].n)[eval_actions], 2 ) else: raise NotImplementedError # Obser reward and next obs eval_obs, eval_rewards, eval_dones, eval_infos = self.eval_envs.step( eval_actions_env ) eval_episode_rewards.append(eval_rewards) eval_rnn_states[eval_dones == True] = np.zeros( ((eval_dones == True).sum(), self.recurrent_N, self.hidden_size), dtype=np.float32, ) eval_masks = np.ones( (self.n_eval_rollout_threads, self.num_agents, 1), dtype=np.float32 ) eval_masks[eval_dones == True] = np.zeros( ((eval_dones == True).sum(), 1), dtype=np.float32 ) eval_episode_rewards = np.array(eval_episode_rewards) eval_env_infos = {} eval_env_infos["eval_average_episode_rewards"] = np.sum( np.array(eval_episode_rewards), axis=0 ) eval_average_episode_rewards = np.mean( eval_env_infos["eval_average_episode_rewards"] ) print( "eval average episode rewards of agent: " + str(eval_average_episode_rewards) ) self.log_env(eval_env_infos, total_num_steps) @torch.no_grad() def render(self): """Visualize the env.""" envs = self.envs all_frames = [] for episode in range(self.all_args.render_episodes): obs = envs.reset() if self.all_args.save_gifs: image = envs.render("rgb_array")[0][0] all_frames.append(image) else: envs.render("human") rnn_states = np.zeros( ( self.n_rollout_threads, self.num_agents, self.recurrent_N, self.hidden_size, ), dtype=np.float32, ) masks = np.ones( (self.n_rollout_threads, self.num_agents, 1), dtype=np.float32 ) episode_rewards = [] for step in range(self.episode_length): calc_start = time.time() self.trainer.prep_rollout() action, rnn_states = self.trainer.policy.act( np.concatenate(obs), np.concatenate(rnn_states), np.concatenate(masks), deterministic=True, ) actions = np.array(np.split(_t2n(action), self.n_rollout_threads)) rnn_states = np.array( np.split(_t2n(rnn_states), self.n_rollout_threads) ) if envs.action_space[0].__class__.__name__ == "MultiDiscrete": for i in range(envs.action_space[0].shape): uc_actions_env = np.eye(envs.action_space[0].high[i] + 1)[ actions[:, :, i] ] if i == 0: actions_env = uc_actions_env else: actions_env = np.concatenate( (actions_env, uc_actions_env), axis=2 ) elif envs.action_space[0].__class__.__name__ == "Discrete": actions_env = np.squeeze(np.eye(envs.action_space[0].n)[actions], 2) else: raise NotImplementedError # Obser reward and next obs obs, rewards, dones, infos = envs.step(actions_env) episode_rewards.append(rewards) rnn_states[dones == True] = np.zeros( ((dones == True).sum(), self.recurrent_N, self.hidden_size), dtype=np.float32, ) masks = np.ones( (self.n_rollout_threads, self.num_agents, 1), dtype=np.float32 ) masks[dones == True] = np.zeros( ((dones == True).sum(), 1), dtype=np.float32 ) if self.all_args.save_gifs: image = envs.render("rgb_array")[0][0] all_frames.append(image) calc_end = time.time() elapsed = calc_end - calc_start if elapsed < self.all_args.ifi: time.sleep(self.all_args.ifi - elapsed) else: envs.render("human") print( "average episode rewards is: " + str(np.mean(np.sum(np.array(episode_rewards), axis=0))) ) # if self.all_args.save_gifs: # imageio.mimsave(str(self.gif_dir) + '/render.gif', all_frames, duration=self.all_args.ifi) # Modified from OpenAI Baselines code to work with multi-agent envs class DummyVecEnv(): def __init__(self, env_fns): self.envs = [fn() for fn in env_fns] env = self.envs[0] self.num_envs = len(env_fns) self.observation_space = env.observation_space self.share_observation_space = env.share_observation_space self.action_space = env.action_space self.actions = None def step(self, actions): """ Step the environments synchronously. This is available for backwards compatibility. """ self.step_async(actions) return self.step_wait() def step_async(self, actions): self.actions = actions def step_wait(self): results = [env.step(a) for (a, env) in zip(self.actions, self.envs)] obs, rews, dones, infos = map(np.array, zip(*results)) for (i, done) in enumerate(dones): if 'bool' in done.__class__.__name__: if done: obs[i] = self.envs[i].reset() else: if np.all(done): obs[i] = self.envs[i].reset() self.actions = None return obs, rews, dones, infos def reset(self): obs = [env.reset() for env in self.envs] return np.array(obs) def close(self): for env in self.envs: env.close() def render(self, mode="human"): if mode == "rgb_array": return np.array([env.render(mode=mode) for env in self.envs]) elif mode == "human": for env in self.envs: env.render(mode=mode) else: raise NotImplementedError class EnvCore(object): """ # ็Žฏๅขƒไธญ็š„ๆ™บ่ƒฝไฝ“ """ def __init__(self): self.agent_num = 2 # ่ฎพ็ฝฎๆ™บ่ƒฝไฝ“(ๅฐ้ฃžๆœบ)็š„ไธชๆ•ฐ๏ผŒ่ฟ™้‡Œ่ฎพ็ฝฎไธบไธคไธช # set the number of agents(aircrafts), here set to two self.obs_dim = 14 # ่ฎพ็ฝฎๆ™บ่ƒฝไฝ“็š„่ง‚ๆต‹็ปดๅบฆ # set the observation dimension of agents self.action_dim = 5 # ่ฎพ็ฝฎๆ™บ่ƒฝไฝ“็š„ๅŠจไฝœ็ปดๅบฆ๏ผŒ่ฟ™้‡Œๅ‡ๅฎšไธบไธ€ไธชไบ”ไธช็ปดๅบฆ็š„ # set the action dimension of agents, here set to a five-dimensional def reset(self): """ # self.agent_num่ฎพๅฎšไธบ2ไธชๆ™บ่ƒฝไฝ“ๆ—ถ๏ผŒ่ฟ”ๅ›žๅ€ผไธบไธ€ไธชlist๏ผŒๆฏไธชlist้‡Œ้ขไธบไธ€ไธชshape = (self.obs_dim, )็š„่ง‚ๆต‹ๆ•ฐๆฎ # When self.agent_num is set to 2 agents, the return value is a list, each list contains a shape = (self.obs_dim, ) observation data """ sub_agent_obs = [] for i in range(self.agent_num): sub_obs = np.random.random(size=(14,)) sub_agent_obs.append(sub_obs) return sub_agent_obs def step(self, actions): """ # self.agent_num่ฎพๅฎšไธบ2ไธชๆ™บ่ƒฝไฝ“ๆ—ถ๏ผŒactions็š„่พ“ๅ…ฅไธบไธ€ไธช2็บฌ็š„list๏ผŒๆฏไธชlist้‡Œ้ขไธบไธ€ไธชshape = (self.action_dim, )็š„ๅŠจไฝœๆ•ฐๆฎ # ้ป˜่ฎคๅ‚ๆ•ฐๆƒ…ๅ†ตไธ‹๏ผŒ่พ“ๅ…ฅไธบไธ€ไธชlist๏ผŒ้‡Œ้ขๅซๆœ‰ไธคไธชๅ…ƒ็ด ๏ผŒๅ› ไธบๅŠจไฝœ็ปดๅบฆไธบ5๏ผŒๆ‰€้‡Œๆฏไธชๅ…ƒ็ด shape = (5, ) # When self.agent_num is set to 2 agents, the input of actions is a 2-dimensional list, each list contains a shape = (self.action_dim, ) action data # The default parameter situation is to input a list with two elements, because the action dimension is 5, so each element shape = (5, ) """ sub_agent_obs = [] sub_agent_reward = [] sub_agent_done = [] sub_agent_info = [] for i in range(self.agent_num): sub_agent_obs.append(np.random.random(size=(14,))) sub_agent_reward.append([np.random.rand()]) sub_agent_done.append(False) sub_agent_info.append({}) return [sub_agent_obs, sub_agent_reward, sub_agent_done, sub_agent_info] class ContinuousActionEnv(object): """ ๅฏนไบŽ่ฟž็ปญๅŠจไฝœ็Žฏๅขƒ็š„ๅฐ่ฃ… Wrapper for continuous action environment. """ def __init__(self): self.env = EnvCore() self.num_agent = self.env.agent_num self.signal_obs_dim = self.env.obs_dim self.signal_action_dim = self.env.action_dim # if true, action is a number 0...N, otherwise action is a one-hot N-dimensional vector self.discrete_action_input = False self.movable = True # configure spaces self.action_space = [] self.observation_space = [] self.share_observation_space = [] share_obs_dim = 0 total_action_space = [] for agent in range(self.num_agent): # physical action space u_action_space = spaces.Box( low=-np.inf, high=+np.inf, shape=(self.signal_action_dim,), dtype=np.float32, ) if self.movable: total_action_space.append(u_action_space) # total action space self.action_space.append(total_action_space[0]) # observation space share_obs_dim += self.signal_obs_dim self.observation_space.append( spaces.Box( low=-np.inf, high=+np.inf, shape=(self.signal_obs_dim,), dtype=np.float32, ) ) # [-inf,inf] self.share_observation_space = [ spaces.Box( low=-np.inf, high=+np.inf, shape=(share_obs_dim,), dtype=np.float32 ) for _ in range(self.num_agent) ] def step(self, actions): """ ่พ“ๅ…ฅactions็ปดๅบฆๅ‡่ฎพ๏ผš # actions shape = (5, 2, 5) # 5ไธช็บฟ็จ‹็š„็Žฏๅขƒ๏ผŒ้‡Œ้ขๆœ‰2ไธชๆ™บ่ƒฝไฝ“๏ผŒๆฏไธชๆ™บ่ƒฝไฝ“็š„ๅŠจไฝœๆ˜ฏไธ€ไธชone_hot็š„5็ปด็ผ–็  Input actions dimension assumption: # actions shape = (5, 2, 5) # 5 threads of environment, there are 2 agents inside, and each agent's action is a 5-dimensional one_hot encoding """ results = self.env.step(actions) obs, rews, dones, infos = results return np.stack(obs), np.stack(rews), np.stack(dones), infos def reset(self): obs = self.env.reset() return np.stack(obs) def close(self): pass def render(self, mode="rgb_array"): pass def seed(self, seed): pass class DiscreteActionEnv(object): """ ๅฏนไบŽ็ฆปๆ•ฃๅŠจไฝœ็Žฏๅขƒ็š„ๅฐ่ฃ… Wrapper for discrete action environment. """ def __init__(self): self.env = EnvCore() self.num_agent = self.env.agent_num self.signal_obs_dim = self.env.obs_dim self.signal_action_dim = self.env.action_dim # if true, action is a number 0...N, otherwise action is a one-hot N-dimensional vector self.discrete_action_input = False self.movable = True # configure spaces self.action_space = [] self.observation_space = [] self.share_observation_space = [] share_obs_dim = 0 total_action_space = [] for agent in range(self.num_agent): # physical action space u_action_space = spaces.Discrete(self.signal_action_dim) # 5ไธช็ฆปๆ•ฃ็š„ๅŠจไฝœ if self.movable: total_action_space.append(u_action_space) # total action space if len(total_action_space) > 1: # all action spaces are discrete, so simplify to MultiDiscrete action space if all( [ isinstance(act_space, spaces.Discrete) for act_space in total_action_space ] ): act_space = MultiDiscrete( [[0, act_space.n - 1] for act_space in total_action_space] ) else: act_space = spaces.Tuple(total_action_space) self.action_space.append(act_space) else: self.action_space.append(total_action_space[0]) # observation space share_obs_dim += self.signal_obs_dim self.observation_space.append( spaces.Box( low=-np.inf, high=+np.inf, shape=(self.signal_obs_dim,), dtype=np.float32, ) ) # [-inf,inf] self.share_observation_space = [ spaces.Box( low=-np.inf, high=+np.inf, shape=(share_obs_dim,), dtype=np.float32 ) for _ in range(self.num_agent) ] def step(self, actions): """ ่พ“ๅ…ฅactions็ปดๅบฆๅ‡่ฎพ๏ผš # actions shape = (5, 2, 5) # 5ไธช็บฟ็จ‹็š„็Žฏๅขƒ๏ผŒ้‡Œ้ขๆœ‰2ไธชๆ™บ่ƒฝไฝ“๏ผŒๆฏไธชๆ™บ่ƒฝไฝ“็š„ๅŠจไฝœๆ˜ฏไธ€ไธชone_hot็š„5็ปด็ผ–็  Input actions dimension assumption: # actions shape = (5, 2, 5) # 5 threads of the environment, with 2 intelligent agents inside, and each intelligent agent's action is a 5-dimensional one_hot encoding """ results = self.env.step(actions) obs, rews, dones, infos = results return np.stack(obs), np.stack(rews), np.stack(dones), infos def reset(self): obs = self.env.reset() return np.stack(obs) def close(self): pass def render(self, mode="rgb_array"): pass def seed(self, seed): pass class MultiDiscrete: """ - The multi-discrete action space consists of a series of discrete action spaces with different parameters - It can be adapted to both a Discrete action space or a continuous (Box) action space - It is useful to represent game controllers or keyboards where each key can be represented as a discrete action space - It is parametrized by passing an array of arrays containing [min, max] for each discrete action space where the discrete action space can take any integers from `min` to `max` (both inclusive) Note: A value of 0 always need to represent the NOOP action. e.g. Nintendo Game Controller - Can be conceptualized as 3 discrete action spaces: 1) Arrow Keys: Discrete 5 - NOOP[0], UP[1], RIGHT[2], DOWN[3], LEFT[4] - params: min: 0, max: 4 2) Button A: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1 3) Button B: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1 - Can be initialized as MultiDiscrete([ [0,4], [0,1], [0,1] ]) """ def __init__(self, array_of_param_array): super().__init__() self.low = np.array([x[0] for x in array_of_param_array]) self.high = np.array([x[1] for x in array_of_param_array]) self.num_discrete_space = self.low.shape[0] self.n = np.sum(self.high) + 2 def sample(self): """Returns a array with one sample from each discrete action space""" # For each row: round(random .* (max - min) + min, 0) random_array = np.random.rand(self.num_discrete_space) return [ int(x) for x in np.floor( np.multiply((self.high - self.low + 1.0), random_array) + self.low ) ] def contains(self, x): return ( len(x) == self.num_discrete_space and (np.array(x) >= self.low).all() and (np.array(x) <= self.high).all() ) @property def shape(self): return self.num_discrete_space def __repr__(self): return "MultiDiscrete" + str(self.num_discrete_space) def __eq__(self, other): return np.array_equal(self.low, other.low) and np.array_equal( self.high, other.high ) if __name__ == "__main__": DiscreteActionEnv().step(actions=None)
[]
2024-01-10
Rocky528/Giturbo_AI
issueHandler.py
from github import Github from github import Auth import config from github_lib import get_github_file_content, get_github_links_from_issue_content from openai_lib import get_completion import requests import openai def get_github_repos(payload): auth = payload['github_token'] username = payload['username'] g = Github(auth) user = g.get_user(username) repositories = user.get_repos() repo_names = [] for repository in repositories: repo_names.append(repository.name) return {"repositories": repo_names} def create_issue(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] repo = g.get_repo(username + '/' + repo_name) title = payload['repository']['issue']['title'] body = payload['repository']['issue']['body'] issue = repo.create_issue(title=title, body=body) response = { 'status': 'success', 'title': issue.title, 'body': issue.body, 'url': issue.html_url } return response def get_possible_labels(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get the labels for the repository label_data = repo.get_labels() # Extract the label names possible_labels = [label.name for label in label_data] return possible_labels def get_all_issues(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get all issues of the repository issues = repo.get_issues(state='all') issue_data = [] # Iterate over the issues and extract their title and number for issue in issues: issue_dict = { 'title': issue.title, 'number': issue.number } issue_data.append(issue_dict) return issue_data #/// def get_existing_labels(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] # Fetch the repository and issue objects repo = g.get_repo(username + '/' + repo_name) issue_number = payload['repository']['issue']['number'] issue = repo.get_issue(issue_number) # Get the list of labels for the issue labels = issue.get_labels() existing_labels = [] # Iterate over the labels and extract their names for label in labels: existing_labels.append(label.name) return existing_labels #/// def add_label_to_issue(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] issue_number = payload['repository']['issue']['number'] labels = payload['repository']['issue']['labels'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get the issue object issue = repo.get_issue(issue_number) # Add the label to the issue issue.add_to_labels(labels) return "Label added successfully." def leave_comment_on_issue(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] issue_number = payload['repository']['issue']['number'] comment_body = payload['repository']['issue']['comment_body'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get the issue object issue = repo.get_issue(issue_number) # Leave a comment on the issue issue.create_comment(comment_body) return "Comment added successfully." def get_all_pull_requests(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get all the pull requests in the repository pull_requests = repo.get_pulls(state='all') pulls = [] for pull_request in pull_requests: pull_dict = { 'title': pull_request.title, 'number': pull_request.number } pulls.append(pull_dict) return pulls def merge_pull_request(payload): auth = payload['github_token'] g = Github(auth) username = payload['username'] repo_name = payload['repository']['name'] pr_number = payload['repository']['pull_request']['number'] # Fetch the repository object repo = g.get_repo(username + '/' + repo_name) # Get the pull request object pull_request = repo.get_pull(pr_number) # Merge the pull request pull_request.merge() return "Pull request merged successfully." def handle_issue(payload): # TODO: Implement logic for handling issues issue_content = payload["issue_content"] owner = payload["owner"] repo = payload["repo"] path = payload["path"] github_links = get_github_links_from_issue_content(issue_content=issue_content) reference_file_contents = "" for link in github_links: file_content = get_github_file_content(owner=owner, repo=repo, path=link) reference_file_contents += file_content print(f"Received an issue event for {payload['repository']['name']}") def handle_comment(payload): # TODO: Implement logic for handling comments openai.api_key = payload['openai_api_key'] prompt = payload["prompt"] result = get_completion(prompt) print(f"Received a comment event for {payload['repository']['name']}") def handle_pull_request(payload): # TODO: Implement logic for handling pull requests owner = payload["owner"] repo = payload["repo"] base_branch = payload["base_branch"] head_branch = payload["head_branch"] title = payload["title"] body = payload["body"] url = f"https://api.github.com/repos/{owner}/{repo}/pulls" headers = { "Accept": "application/vnd.github.v3+json", "Authorization": "Bearer YOUR_ACCESS_TOKEN" } data = { "title": title, "body": body, "head": head_branch, "base": base_branch } response = requests.post(url, headers=headers, json=data) response_json = response.json() if 'html_url' in response_json: pull_request_url = response_json['html_url'] return pull_request_url else: return None print(f"Received a pull request event for {payload['repository']['name']}")
[]
2024-01-10
Muennighoff/sgpt
biencoder~beir~beir_openai_embeddings_batched_parallel.py
import argparse import json import logging import os import pathlib import pickle from typing import Dict, List, Tuple import numpy as np import openai import pandas as pd from transformers import GPT2TokenizerFast from retry import retry from beir import util, LoggingHandler from beir.datasets.data_loader import GenericDataLoader from beir.retrieval.evaluation import EvaluateRetrieval from custommodels import DenseRetrievalExactSearch from parallelizer import DataFrameParallelizer from parallelizer.parallelizer import ErrorHandling # Code to print debug information to stdout logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()], ) logger = logging.getLogger(__name__) API_KEY = "API_KEY" # We don't use OpenAIs custom exceptions, as it will raise # TypeError: catching classes that do not inherit from BaseException is not allowed API_EXCEPTIONS = (Exception,) def parse_args(): # Parse command line arguments parser = argparse.ArgumentParser() parser.add_argument("--dataset", type=str, default="scifact", help="Dataset to embed.") parser.add_argument("--engine", type=str, default="ada", help="Engine to use.") parser.add_argument("--endpoint", type=str, default="search", help="search / similarity") parser.add_argument("--datapath", type=str, default="./", help="Path to folder with datasets") parser.add_argument( "--overwrite", action="store_const", default=False, const=True, help="Whether to recompute & overwrite existing results", ) parser.add_argument("--batchsize", type=int, default=250, help="How many requests to batch") parser.add_argument( "--parallelworkers", type=int, default=4, help="Num workers sending requests", ) parser.add_argument("--maxattempts", type=int, default=3, help="Maximum number of attempts") parser.add_argument( "--waitinterval", type=int, default=10, help="Seconds to wait after failed attempt" ) args = parser.parse_args() return args class OpenAIRetriever: def __init__( self, doc_engine="ada-search-document", query_engine="ada-search-query", endpoint="search", api_key=API_KEY, dataset="", tokenizer=GPT2TokenizerFast.from_pretrained("gpt2"), max_query_len=0, max_token_len=2048, batch_size=250, parallel_workers=4, max_attempts=3, wait_interval=10, **kwargs, ): self.doc_engine = doc_engine self.query_engine = query_engine self.api_key = api_key self.tokenizer = tokenizer self.max_query_len = max_query_len self.max_token_len = max_token_len if max_query_len >= max_token_len: raise ValueError( "Longest query exceed maximum tokens - How to rank this with a corpus?" ) engine = doc_engine.split("-")[0] base_path = f"embeddings/{endpoint}/{engine}/{dataset}/" pathlib.Path(base_path).mkdir(parents=True, exist_ok=True) self.out_name_base = f"{base_path}/{engine}" # Request parameters self.batch_size = batch_size self.parallel_workers = parallel_workers self.max_attempts = max_attempts self.wait_interval = wait_interval def encode_queries(self, queries: List[str], batch_size: int, **kwargs) -> np.ndarray: # Embed if not already present embedding_queries_path = f"{self.out_name_base}_queries.pickle" if os.path.exists(embedding_queries_path): embeddings = pickle.load(open(embedding_queries_path, "rb")) else: embeddings = self.embed( texts=queries, engine=self.query_engine, api_key=self.api_key, tokenizer=self.tokenizer, max_query_len=self.max_query_len, max_token_len=self.max_token_len, out_name=embedding_queries_path, save_to_file=True, batch_size=self.batch_size, parallel_workers=self.parallel_workers, max_attempts=self.max_attempts, wait_interval=self.wait_interval, ) # Sort embeddings according to the order given & take just the values embeddings = [embeddings[id] for (id, _) in queries] embeddings = np.array(embeddings) logging.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings def encode_corpus( self, corpus: List[Dict[str, str]], batch_size: int, batch_num="", **kwargs ) -> np.ndarray: # Embed if not already present embedding_corpus_path = f"{self.out_name_base}_corpus{batch_num}.pickle" if os.path.exists(embedding_corpus_path): embeddings = pickle.load(open(embedding_corpus_path, "rb")) else: # corpus is of form [(id, {"title": "xxx", "text": "yyy"}), ...] corpus = [(id, data["text"]) for (id, data) in corpus] embeddings = self.embed( texts=corpus, engine=self.doc_engine, api_key=self.api_key, tokenizer=self.tokenizer, max_query_len=self.max_query_len, max_token_len=self.max_token_len, out_name=embedding_corpus_path, save_to_file=True, batch_size=self.batch_size, parallel_workers=self.parallel_workers, max_attempts=self.max_attempts, wait_interval=self.wait_interval, ) # Sort embeddings according to the order given embeddings = [embeddings[id] for (id, _) in corpus] embeddings = np.array(embeddings) logging.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings @staticmethod def embed( texts: List[Tuple[int, str]], engine: str, api_key: str, tokenizer, max_query_len: int, max_token_len: int, out_name=None, save_to_file=False, batch_size=50, parallel_workers=1, max_attempts=3, wait_interval=60, ): openai.api_key = api_key logging.info(f"Starting embedding of {len(texts)} texts.") df = pd.DataFrame(texts, columns=["id", "txt"]) @retry(API_EXCEPTIONS, delay=wait_interval, tries=max_attempts) def call_gpt_api( batch: List[Dict], text_column: str = "txt", id_column: str = "id", decode: bool = True, ) -> List[List[float]]: """ Calls GPT API. """ all_tokens = [] used_indices = [] for i, row in enumerate(batch): txt, id = row[text_column], row[id_column] # Recommendation from OpenAI Docs: replace newlines with space txt = txt.replace("\n", " ") tokens = tokenizer.encode(txt, add_special_tokens=False) token_len = len(tokens) if token_len == 0: raise ValueError("Empty items should be cleaned prior to running") if token_len + max_query_len > max_token_len: tokens = tokens[: max_token_len - max_query_len - 1] # 0-indexed # For some characters the API raises weird errors, e.g. input=[[126]] if decode: tokens = tokenizer.decode(tokens) all_tokens.append(tokens) used_indices.append(i) out = [[]] * len(batch) if all_tokens: response = openai.Engine(id=engine).embeddings(input=all_tokens) assert len(response["data"]) == len( all_tokens ), f"Sent {len(all_tokens)}, got {len(response['data'])}" for data in response["data"]: idx = data["index"] # OpenAI seems to return them ordered, but to be save use the index and insert idx = used_indices[idx] embedding = data["embedding"] out[idx] = embedding return out df_parallelizer = DataFrameParallelizer( function=call_gpt_api, error_handling=ErrorHandling.FAIL, exceptions_to_catch=API_EXCEPTIONS, parallel_workers=parallel_workers, output_column_prefix="gpt", batch_support=True, batch_size=batch_size, ) df = df_parallelizer.run( df, ) assert len(df) == len(texts) logging.info(f"Embedded {len(df)} texts.") def format_results(v): # Pandas concats all columns as a list - We only have one column, hence select it emb = v[0] # Outputted list has been turned into a String so load via json to turn back into list emb = json.loads(emb) assert isinstance(emb, list), f"Expected list, but got {type(emb)}" return emb embeddings = df[["id", "gpt_response"]].set_index("id").T.to_dict("list") embeddings = {k: format_results(v) for k, v in embeddings.items()} if save_to_file: pickle.dump(embeddings, open(out_name, "wb")) return embeddings def main(args): dataset = args.dataset endpoint = args.endpoint engine = args.engine base_data_path = args.datapath overwrite = args.overwrite # Request parameters batch_size = args.batchsize parallel_workers = args.parallelworkers max_attempts = args.maxattempts wait_interval = args.waitinterval data_path = f"{base_data_path}/{dataset}" if not os.path.exists(data_path): url = "https://public.ukp.informatik.tu-darmstadt.de/thakur/BEIR/datasets/{}.zip".format( dataset ) out_dir = os.path.join(os.getcwd(), "datasets") data_path = util.download_and_unzip(url, out_dir) print("Dataset downloaded here: {}".format(data_path)) # Load the dataset into BEIR data_path = f"datasets/{dataset}" # In the paper it says, BEIR used the dev set for msmarco split = "dev" if dataset == "msmarco" else "test" corpus, queries, qrels = GenericDataLoader(data_path).load(split=split) empty_keys = [k for k, v in corpus.items() if not v["text"]] logging.info(f"Found {len(empty_keys)} empty keys in corpus. Removing...") assert len(empty_keys) < len(corpus), "Too many empty keys..." # Remove keys in place for k in empty_keys: del corpus[k] empty_keys = [k for k, v in queries.items() if not v] assert not empty_keys, f"Contains {len(empty_keys)} empty queries" tokenizer = GPT2TokenizerFast.from_pretrained("gpt2") max_query_len = max([len(tokenizer.tokenize(q)) for q in queries.values()]) if endpoint == "search": doc_engine = f"{engine}-search-document" query_engine = f"{engine}-search-query" elif endpoint == "similarity": doc_engine = query_engine = f"{engine}-similarity" custom_model = DenseRetrievalExactSearch( OpenAIRetriever( doc_engine=doc_engine, query_engine=query_engine, endpoint=endpoint, dataset=dataset, tokenizer=tokenizer, max_query_len=max_query_len, batch_size=batch_size, parallel_workers=parallel_workers, max_attempts=max_attempts, wait_interval=wait_interval, ) ) # Turn cqadupstack/english -> cqadupstack_english dataset = dataset.replace("/", "_") engine = doc_engine.split("-")[0] out_path = f"./results_{engine}_{endpoint}_{dataset}.json" if os.path.exists(out_path) and not overwrite: logging.info(f"Found {out_path} - Skipping ...") return # Optionally use less k-values to save memory # E.g. [.. 100] instead of [.. 1000] will reduce self.results by 90% retriever = EvaluateRetrieval(custom_model, k_values=[1, 3, 5, 10, 100, 1000]) #### Retrieve dense results (format of results is identical to qrels) results = retriever.retrieve(corpus, queries) # Save scores for top 1000 docs for each query, i.e. 1000 * queries lines with open(out_path, "w") as fp: json.dump(results, fp) ndcg, _map, recall, precision = retriever.evaluate(qrels, results, retriever.k_values) ndgcs_path = f"./beir_openai_{endpoint}_embeddings_ndcgs.json" if not os.path.exists(ndgcs_path): ndcgs_json = {"ndcgs": {}} else: with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) ndcgs_json["ndcgs"].setdefault(engine, {}) ndcgs_json["ndcgs"][engine][dataset] = ndcg # Add average of cqadupstack once all present CQADUPSTACK_DATASETS = [ "android", "english", "gaming", "gis", "mathematica", "physics", "programmers", "stats", "wordpress", "webmasters", "unix", "tex", ] if "cqadupstack" in dataset and all( f"cqadupstack_{cqadataset}" in ndcgs_json["ndcgs"][engine] for cqadataset in CQADUPSTACK_DATASETS ): ndcgs_json["ndcgs"][engine]["cqadupstack"] = {} for cqadataset in CQADUPSTACK_DATASETS: for k, v in ndcgs_json["ndcgs"][engine][f"cqadupstack_{cqadataset}"].items(): ndcgs_json["ndcgs"][engine]["cqadupstack"].setdefault(k, 0) ndcgs_json["ndcgs"][engine]["cqadupstack"][k] += v / len(CQADUPSTACK_DATASETS) with open(ndgcs_path, "w") as f: json.dump(ndcgs_json, f) if __name__ == "__main__": args = parse_args() main(args)
[]
2024-01-10
Muennighoff/sgpt
biencoder~beir~beir_dense_retriever.py
import argparse import collections import json import logging import os import pathlib import pickle from typing import Dict, List, Tuple, ValuesView import numpy as np import torch from transformers import AutoTokenizer, AutoModel from beir import util, LoggingHandler from beir.datasets.data_loader import GenericDataLoader from beir.retrieval.evaluation import EvaluateRetrieval from custommodels import DenseRetrievalExactSearch, SentenceBERTAsym, SentenceBERTBOSEOS # Code to print debug information to stdout logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()], ) logger = logging.getLogger(__name__) def parse_args(): # Parse command line arguments parser = argparse.ArgumentParser() parser.add_argument("--dataset", type=str, default="scifact", help="Dataset to embed.") parser.add_argument("--modelname", type=str, default="bert-base-uncased", help="Model to use.") parser.add_argument("--method", type=str, default="mean", help="Method to use.") parser.add_argument("--device", type=str, default="cuda:0", help="Device to use.") parser.add_argument("--layeridx", type=int, default=-1, help="Layer to use: -1 is the last.") parser.add_argument( "--usest", action="store_const", default=False, const=True, help="Whether to use Sentence Transformers", ) parser.add_argument("--datapath", type=str, default="./datasets/", help="Path to folder with datasets") parser.add_argument( "--overwrite", action="store_const", default=False, const=True, help="Whether to recompute & overwrite existing results", ) parser.add_argument("--batchsize", type=int, default=250, help="How many requests to batch") parser.add_argument( "--saveemb", action="store_const", default=False, const=True, help="Whether to save embeddings", ) parser.add_argument( "--computeavg", action="store_const", default=False, const=True, help="Whether to only compute model avgs", ) parser.add_argument( "--selectbest", action="store_const", default=False, const=True, help="Compute best ckpts", ) parser.add_argument( "--speca", action="store_const", default=False, const=True, help="Use special token a encoding method", ) parser.add_argument( "--specb", action="store_const", default=False, const=True, help="Use special brackets encoding method", ) parser.add_argument( "--maxseqlen", type=int, default=None, help="Sequence length to use; SGPT-msmarco-specb models use 300" ) args = parser.parse_args() return args SPECB_QUE_BOS = "[" SPECB_QUE_EOS = "]" SPECB_DOC_BOS = "{" SPECB_DOC_EOS = "}" class CustomEmbedder: def __init__( self, model_name="EleutherAI/gpt-neo-1.3B", batch_size=250, device="cuda:0", save_emb=False, reinit=False, layeridx=-1, method="mean", dataset="scifact", specb=False, maxseqlen=None, **kwargs, ): self.device = torch.device(device) self.model = AutoModel.from_pretrained(model_name, **kwargs).to(self.device) if reinit: logging.warn("Reiniting all model weights") self.model.init_weights() self.model.eval() self.max_token_len = maxseqlen if maxseqlen else self.model.config.max_position_embeddings # Account for special tokens: if "bert" in model_name: logging.info("BERT model detected: Reducing token len by 2 to account for [CLS] & [SEP]") self.max_token_len -= 2 if specb: # Leave two tokens for special brackets self.max_token_len -= 2 self.tokenizer = AutoTokenizer.from_pretrained(model_name) # gpt models do not have a padding token by default - Add one and ignore it with the attn mask lateron if "gpt" in model_name.lower(): self.tokenizer.pad_token = self.tokenizer.eos_token self.batch_size = batch_size self.save_emb = save_emb self.layeridx = layeridx self.method = method self.specb = specb if specb: self.bos_token_q = self.tokenizer.encode(SPECB_QUE_BOS) self.eos_token_q = self.tokenizer.encode(SPECB_QUE_EOS) self.bos_token_d = self.tokenizer.encode(SPECB_DOC_BOS) self.eos_token_d = self.tokenizer.encode(SPECB_DOC_EOS) self.base_path = f"embeddings/{model_name.split('/')[-1]}/{self.method}/{dataset}" pathlib.Path(self.base_path).mkdir(parents=True, exist_ok=True) def embed(self, batch, is_query, **kwargs): docs_truncated = 0 toks_truncated = 0 total_toks = 0 batch_tokens = collections.defaultdict(list) gather_indices = [] for i, txt in enumerate(batch): # Recommendation from OpenAI Docs: replace newlines with space txt = txt.replace("\n", " ") # Convert string to list of integers according to tokenizer's vocabulary tokens = self.tokenizer.tokenize(txt) tokens = self.tokenizer.convert_tokens_to_ids(tokens) token_len = len(tokens) total_toks += token_len if token_len > self.max_token_len: docs_truncated += 1 toks_truncated += token_len - self.max_token_len elif token_len == 0: raise ValueError("Empty items should be cleaned prior to running") input_dict = self.tokenizer.prepare_for_model( tokens[: self.max_token_len], add_special_tokens=True ) if self.specb: if is_query: input_dict["input_ids"] = self.bos_token_q + input_dict["input_ids"] + self.eos_token_q else: input_dict["input_ids"] = self.bos_token_d + input_dict["input_ids"] + self.eos_token_d input_dict["attention_mask"] = [1] + input_dict["attention_mask"] + [1] # input_ids: Same as tokens, but with model-specific beginning and end tokens # attention_mask: List of 1s for each input_id, i.e. the tokens it should attend to batch_tokens["input_ids"].append(input_dict["input_ids"]) batch_tokens["attention_mask"].append(input_dict["attention_mask"]) assert len(input_dict["input_ids"]) == len(input_dict["attention_mask"]) gather_indices.append(len(input_dict["input_ids"]) - 1) # Account for 0-indexing # No need for truncation, as all inputs are now trimmed to less than the models seq length batch_tokens = self.tokenizer.pad(batch_tokens, padding=True, return_tensors="pt") # Move to CPU/GPU batch_tokens = {k: v.to(self.device) for k, v in batch_tokens.items()} with torch.no_grad(): embedded_batch = self.model(**batch_tokens, output_hidden_states=True, **kwargs) all_hidden_states = embedded_batch.hidden_states input_mask_expanded = ( batch_tokens["attention_mask"] .unsqueeze(-1) .expand(all_hidden_states[-1].size()) .float() ) if docs_truncated: logging.warn( f"Truncated {docs_truncated} out of {len(batch)} documents by {toks_truncated} out of {total_toks}." ) all_hidden_states = [x.cpu() for x in all_hidden_states] return all_hidden_states, input_mask_expanded.cpu(), gather_indices, embedded_batch def embed_batcher(self, texts: List[Tuple[int, str]], is_query, out_name=None, **kwargs): all_embeddings = {} for i in range(0, len(texts), self.batch_size): # Subselect batch_size items batch = texts[i : i + self.batch_size] ids, sentences = zip(*batch) all_hidden_states, input_mask_expanded, gather_indices, embedded_batch = self.embed(sentences, is_query=is_query) hidden_state = all_hidden_states[self.layeridx] if abs(self.layeridx) > len(all_hidden_states): raise ValueError(f"Layer Idx {self.layeridx} is larger than the {len(all_hidden_states)} hidden states") ### APPLY POOLING ### if self.method == "mean": # bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum(hidden_state * input_mask_expanded, dim=1) sum_mask = input_mask_expanded.sum(dim=1) embedding = sum_embeddings / sum_mask elif self.method == "meanmean": bs, seq_len, hidden_dim = hidden_state.shape num_layers = len(all_hidden_states) hidden_states = torch.stack(all_hidden_states) input_mask_expanded = input_mask_expanded.unsqueeze(0).expand(hidden_states.size()) assert hidden_states.shape == input_mask_expanded.shape # num_layers, bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum( torch.sum(hidden_states * input_mask_expanded, dim=2), dim=0 ) sum_mask = input_mask_expanded.sum(dim=2).sum(dim=0) embedding = sum_embeddings / sum_mask elif self.method == "weightedmean": weights = ( torch.arange(start=1, end=hidden_state.shape[1] + 1) .unsqueeze(0) .unsqueeze(-1) .expand(hidden_state.size()) .float() ) # bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum(hidden_state * input_mask_expanded * weights, dim=1) sum_mask = torch.sum(input_mask_expanded * weights, dim=1) embedding = sum_embeddings / sum_mask elif self.method == "lasttoken": bs, seq_len, hidden_dim = hidden_state.shape # Turn indices from shape [bs] --> [bs, 1, hidden_dim] gather_indices = torch.LongTensor(gather_indices) gather_indices = gather_indices.unsqueeze(-1).repeat(1, hidden_dim) gather_indices = gather_indices.unsqueeze(1) assert gather_indices.shape == (bs, 1, hidden_dim) # Gather along the 1st dim (seq_len) (bs, seq_len, hidden_dim -> bs, hidden_dim) # No need for the attention mask as we gather the last token where attn_mask = 1 embedding = torch.gather(hidden_state, 1, gather_indices).squeeze() elif self.method == "lasttokenmean": bs, seq_len, hidden_dim = hidden_state.shape num_layers = len(all_hidden_states) hidden_states = torch.stack(all_hidden_states) # Turn indices from shape [bs] --> [num_layers, bs, 1, hidden_dim] gather_indices = torch.LongTensor(gather_indices) gather_indices = gather_indices.unsqueeze(-1).repeat(1, hidden_dim) gather_indices = gather_indices.unsqueeze(0).repeat(num_layers, 1, 1) gather_indices = gather_indices.unsqueeze(2) assert gather_indices.shape == (num_layers, bs, 1, hidden_dim) # Gather along the 2nd dim (seq_len) (num_layers, bs, seq_len, hidden_dim -> num_layers, bs, hidden_dim) embedding = torch.gather(hidden_states, 2, gather_indices).squeeze() assert embedding.shape == (num_layers, bs, hidden_dim) # num_layers, bs, hidden_dim -> bs, hidden_dim embedding = torch.mean(embedding, 0) elif self.method == "poolout": embedding = embedded_batch.pooler_output.cpu() add_embeddings = {id: emb.numpy() for id, emb in zip(ids, embedding)} all_embeddings = {**all_embeddings, **add_embeddings} assert len(texts) == len(all_embeddings) if self.save_emb: pickle.dump(all_embeddings, open(out_name, "wb")) return all_embeddings def encode_queries(self, queries: List[str], batch_size: int, **kwargs) -> np.ndarray: # Embed if not already present embedding_queries_path = f"{self.base_path}_queries.pickle" if os.path.exists(embedding_queries_path): embeddings = pickle.load(open(embedding_queries_path, "rb")) else: embeddings = self.embed_batcher(texts=queries, out_name=embedding_queries_path, is_query=True, **kwargs) # Sort embeddings according to the order given & take just the values embeddings = [embeddings[id] for (id, _) in queries] embeddings = np.array(embeddings) logger.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings def encode_corpus( self, corpus: List[Dict[str, str]], batch_size: int, batch_num="", **kwargs ) -> np.ndarray: # Embed if not already present embedding_corpus_path = f"{self.base_path}_corpus{batch_num}.pickle" if os.path.exists(embedding_corpus_path): embeddings = pickle.load(open(embedding_corpus_path, "rb")) else: # corpus is of form [(id, {"title": "xxx", "text": "yyy"}), ...] corpus = [(id, (data["title"] + " " + data["text"]).strip()) if "title" in data else data["text"].strip() for (id, data) in corpus] embeddings = self.embed_batcher(texts=corpus, out_name=embedding_corpus_path, is_query=False, **kwargs) # Sort embeddings according to the order given embeddings = [embeddings[id] for (id, _) in corpus] embeddings = np.array(embeddings) logger.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings def main(args): dataset = args.dataset model_name = args.modelname device = args.device use_st = args.usest base_data_path = args.datapath overwrite = args.overwrite batch_size = args.batchsize save_emb = args.saveemb method = args.method layeridx = args.layeridx speca = args.speca specb = args.specb maxseqlen = args.maxseqlen if args.computeavg: compute_model_avg() exit() elif args.selectbest: select_best_ckpt() exit() data_path = f"{base_data_path}/{dataset}" if not os.path.exists(data_path): url = "https://public.ukp.informatik.tu-darmstadt.de/thakur/BEIR/datasets/{}.zip".format( dataset ) out_dir = os.path.join(os.getcwd(), "datasets") data_path = util.download_and_unzip(url, out_dir) print("Dataset downloaded here: {}".format(data_path)) # Load the dataset into BEIR data_path = f"datasets/{dataset}" # In the paper it says, BEIR used the dev set for msmarco split = "dev" if dataset == "msmarco" else "test" corpus, queries, qrels = GenericDataLoader(data_path).load(split=split) corpus = clean_titles(corpus) if "robust04" in data_path else corpus empty_keys = [k for k, v in corpus.items() if not v["text"]] logger.info(f"Found {len(empty_keys)} empty keys in corpus. Removing...") assert len(empty_keys) < len(corpus), "Too many empty keys..." # Remove keys in place for k in empty_keys: del corpus[k] empty_keys = [k for k, v in queries.items() if not v] assert not empty_keys, f"Contains {len(empty_keys)} empty queries" if use_st: from beir.retrieval import models from beir.retrieval.search.dense import DenseRetrievalExactSearch as DRES if "asym" in model_name: logger.info(f"Using asymmetric model.") custom_model = DRES(SentenceBERTAsym(model_name, device=device), batch_size=batch_size) elif speca or specb: custom_model = DRES(SentenceBERTBOSEOS(model_name, speca=speca, specb=specb, device=device), batch_size=batch_size) else: custom_model = DRES(models.SentenceBERT(model_name, device=device), batch_size=batch_size) else: if speca: raise ValueError("speca is only supported with use_st") custom_model = DenseRetrievalExactSearch( CustomEmbedder( model_name=model_name, method=method, device=device, batch_size=batch_size, save_emb=save_emb, layeridx=layeridx, specb=specb, maxseqlen=maxseqlen, ) ) # Turn cqadupstack/english -> cqadupstack_english dataset = dataset.replace("/", "_") model_name = model_name.replace("/", "_") out_path = f"./results_{model_name}_{method}_{dataset}.json" if os.path.exists(out_path) and not overwrite: logger.info(f"Found {out_path} - Skipping ...") return # Optionally use less k-values to save memory # E.g. [.. 100] instead of [.. 1000] will reduce self.results by 90% retriever = EvaluateRetrieval(custom_model, k_values=[1, 3, 5, 10, 100, 1000]) #### Retrieve dense results (format of results is identical to qrels) results = retriever.retrieve(corpus, queries) # Save scores for top 1000 docs for each query, i.e. 1000 * queries lines with open(out_path, "w") as fp: json.dump(results, fp) ndcg, _map, recall, precision = retriever.evaluate(qrels, results, retriever.k_values) ndgcs_path = f"./beir_embeddings_ndcgs.json" if not os.path.exists(ndgcs_path): ndcgs_json = {"ndcgs": {}, "maps": {}, "recalls": {}, "precisions": {}} else: with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) ndcgs_json["ndcgs"].setdefault(model_name, {}) ndcgs_json["ndcgs"][model_name][dataset] = ndcg # Backwards compat ndcgs_json.setdefault("maps", {}) ndcgs_json.setdefault("recalls", {}) ndcgs_json.setdefault("precisions", {}) ndcgs_json["maps"].setdefault(model_name, {}) ndcgs_json["recalls"].setdefault(model_name, {}) ndcgs_json["precisions"].setdefault(model_name, {}) ndcgs_json["maps"][model_name][dataset] = _map ndcgs_json["recalls"][model_name][dataset] = recall ndcgs_json["precisions"][model_name][dataset] = precision # Add average of cqadupstack once all present CQADUPSTACK_DATASETS = [ "android", "english", "gaming", "gis", "mathematica", "physics", "programmers", "stats", "wordpress", "webmasters", "unix", "tex", ] if "cqadupstack" in dataset and all( f"cqadupstack_{cqadataset}" in ndcgs_json["ndcgs"][model_name] for cqadataset in CQADUPSTACK_DATASETS ): ndcgs_json["ndcgs"][model_name]["cqadupstack"] = {} for cqadataset in CQADUPSTACK_DATASETS: for k, v in ndcgs_json["ndcgs"][model_name][f"cqadupstack_{cqadataset}"].items(): ndcgs_json["ndcgs"][model_name]["cqadupstack"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["cqadupstack"][k] += v / len(CQADUPSTACK_DATASETS) with open(ndgcs_path, "w") as f: json.dump(ndcgs_json, f) def clean_titles(corpus): for k in corpus: if "title" in corpus[k] and corpus[k]["title"] is None: corpus[k]["title"] = "" return corpus def compute_model_avg(): ndgcs_path = f"./beir_embeddings_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) subsubavg_datasets = ["nfcorpus", "fiqa", "arguana", "scidocs", "scifact"] subavg_datasets = ["trec-covid", "nfcorpus", "hotpotqa", "fiqa", "arguana", "webis-touche2020", "quora", "dbpedia-entity", "fever", "climate-fever", "scifact"] # Average does not include msmarco due to in-domain avg_datasets = ["nfcorpus", "bioasq", "nq", "hotpotqa", "fiqa", "signal1m", "trec-news", "arguana", "webis-touche2020", "quora", "dbpedia-entity", "scidocs", "fever", "climate-fever", "scifact", "robust04", "cqadupstack", "trec-covid"] for model_name in ndcgs_json["ndcgs"]: ndcgs_json["ndcgs"][model_name]["average"] = {} ndcgs_json["ndcgs"][model_name]["subaverage"] = {} ndcgs_json["ndcgs"][model_name]["subsubaverage"] = {} model_datasets = [ds for ds in ndcgs_json["ndcgs"][model_name] if ds in avg_datasets] for dataset in ndcgs_json["ndcgs"][model_name]: if dataset not in model_datasets: print(f"Skipping {dataset}") continue for k, v in ndcgs_json["ndcgs"][model_name][dataset].items(): ndcgs_json["ndcgs"][model_name]["average"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["average"][k] += v / len(model_datasets) if all(sub_ds in model_datasets for sub_ds in subavg_datasets) and (dataset in subavg_datasets): ndcgs_json["ndcgs"][model_name]["subaverage"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["subaverage"][k] += v / len(subavg_datasets) if all(subsub_ds in model_datasets for subsub_ds in subsubavg_datasets) and (dataset in subsubavg_datasets): ndcgs_json["ndcgs"][model_name]["subsubaverage"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["subsubaverage"][k] += v / len(subsubavg_datasets) with open(ndgcs_path, "w") as f: json.dump(ndcgs_json, f) def select_best_ckpt(): """A bit hard-coded function for selecting the best checkpoints given results of many ckpts""" ndgcs_path = "./beir_embeddings_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) best_ndgcs_path = "./beir_embeddings_best_ndcgs.json" if not os.path.exists(best_ndgcs_path): best_ndgcs_json = {"ndcgs": {}} else: with open(best_ndgcs_path, "r") as f: best_ndgcs_json = json.load(f) # SGPT 125M ckpts ckpts = ["15600", "31200", "46800", "62398", "62400", "78000",] # SGPT 2.7B ckpts ckpts += ["101387", "124784", "148181", "156000", "31196", "54593", "7799", "93588", "109186", "132583", "15598", "38995", "62392", "77990", "116985", "140382", "155980", "23397", "46794", "70191", "85789"] # SGPT 6.1B ckpts ckpts += ["112311", "137269", "174706", "237101", "262059", "299496", "37437", "74874", "12479", "149748", "187185", "212143", "24958", "274538", "311975", "49916", "87353", "124790", "162227", "199664", "224622", "249580", "287017", "311990", "62395", "99832",] ckpts = set(ckpts) for model_name in ndcgs_json["ndcgs"]: model_ckpt = model_name.split("_")[-1] model_base_name = model_name.strip(model_ckpt) if model_ckpt in ckpts: best_score = 0 best_model_name = None for ckpt in ckpts: cur_model_name = model_base_name + ckpt if cur_model_name not in ndcgs_json["ndcgs"]: logging.info(f"Did not find {cur_model_name}") continue cur_score = ndcgs_json["ndcgs"][cur_model_name]["average"]["NDCG@10"] if cur_score > best_score: best_score = cur_score best_model_name = cur_model_name best_ndgcs_json["ndcgs"][best_model_name] = ndcgs_json["ndcgs"][best_model_name] else: logger.info(f"Did not find ckpts for {model_name}. Skipping...") with open(best_ndgcs_path, "w") as f: json.dump(best_ndgcs_json, f) def rank_model_avg(): """A function for quickly ranking the best models - Can just be copy pasted into the local Python Interpreter""" import os, json ndgcs_path = "./beir_embeddings_best_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) out = sorted(ndcgs_json["ndcgs"], key=lambda x: ndcgs_json["ndcgs"][x]["average"]["NDCG@10"], reverse=True) print({x: ndcgs_json["ndcgs"][x] for x in out[:5]}) print(out[:5]) if __name__ == "__main__": args = parse_args() main(args)
[]
2024-01-10
xuxiong/pg-text-query
pg_text_query~gen_query.py
"""A trivial wrapper of openai.Completion.create (for now). Handles initialization of a default request config with optional override by config file and/or arbitrary kwargs to generate_query.py. """ import os import typing as t import openai import yaml from pglast.parser import parse_sql, ParseError from pg_text_query.errors import EnvVarError, QueryGenError # Initialize default OpenAI completion config w/ optional user config file path # from env var PGTQ_OPENAI_CONFIG PGTQ_OPENAI_CONFIG = os.getenv( "PGTQ_OPENAI_CONFIG", os.path.join(os.path.dirname(__file__), "default_openai_config.yaml"), ) with open(PGTQ_OPENAI_CONFIG, "rb") as f: DEFAULT_COMPLETION_CONFIG = yaml.safe_load(f)["completion_create"] CHAT_OPENAI_CONFIG = os.getenv( "CHAT_OPENAI_CONFIG", os.path.join(os.path.dirname(__file__), "openai_chat_config.yaml"), ) with open(CHAT_OPENAI_CONFIG, "rb") as f: CHAT_COMPLETION_CONFIG = yaml.safe_load(f)["completion_create"] def generate_query(prompt: str, validate_sql: bool = False, completion_type: str = "single", **kwargs: t.Any) -> str: """Generate a raw Postgres query string from a prompt. If validate_sql is True, raises QueryGenError when OpenAI returns a completion that fails validation using the Postgres parser. This ensures a non-empty and syntactically valid query but NOT necessarily a correct one. Completion.create is called with default config from PGTQ_OPENAI_CONFIG with any provided kwargs serving as parameter overrides. TODO: Later, add error handling. """ # if getattr(openai, "api_key") is None: # Initialize OpenAI API Key openai.api_key = os.getenv("OPENAI_API_KEY") if openai.api_key is None: raise EnvVarError("OPENAI_API_KEY not found in environment") if completion_type=="single": response = openai.Completion.create( prompt=prompt, **{**DEFAULT_COMPLETION_CONFIG, **kwargs}, ) generated_query = response["choices"][0]["text"] elif completion_type=="chat": system = kwargs.get("task_prompt", {}).get("system", None) if not system: system = "you are a text-to-SQL translator. You write PostgreSQL code based on plain-language prompts." query = [{"role":"system", "content": system}, {"role":"user", "content": prompt}] response = openai.ChatCompletion.create( messages=query, **{**CHAT_COMPLETION_CONFIG, **kwargs}, ) generated_query = response["choices"][0]["message"]["content"] else: raise ValueError("Must specify 'single' or 'chat' completion type") if validate_sql: if not is_valid_query(generated_query): raise QueryGenError("Generated query is empty, only a comment, or invalid.") return generated_query def generate_query_chat(prompt: str, validate_sql: bool = False, system: t.Optional[str] = None, **kwargs: t.Any) -> str: """Generate a raw Postgres query string from a prompt using ChatGTP. If validate_sql is True, raises QueryGenError when OpenAI returns a completion that fails validation using the Postgres parser. This ensures a non-empty and syntactically valid query but NOT necessarily a correct one. Completion.create is called with default config from PGTQ_OPENAI_CONFIG with any provided kwargs serving as parameter overrides. TODO: Later, add error handling. """ # if getattr(openai, "api_key") is None: # Initialize OpenAI API Key openai.api_key = os.getenv("OPENAI_API_KEY") if openai.api_key is None: raise EnvVarError("OPENAI_API_KEY not found in environment") if not system: system = "you are a text-to-SQL translator. You write PostgreSQL code based on plain-language prompts." query = [{"role":"system", "content": system}, {"role":"user", "content": prompt}] response = openai.ChatCompletion.create( messages=query, **{**CHAT_COMPLETION_CONFIG, **kwargs}, ) generated_query = response["choices"][0]["message"]["content"] if validate_sql: if not is_valid_query(generated_query): raise QueryGenError("Generated query is empty, only a comment, or invalid.") return generated_query def generate_query_chat(prompt: str, validate_sql: bool = False, system: t.Optional[str] = None, **kwargs: t.Any) -> str: """Generate a raw Postgres query string from a prompt using ChatGTP. If validate_sql is True, raises QueryGenError when OpenAI returns a completion that fails validation using the Postgres parser. This ensures a non-empty and syntactically valid query but NOT necessarily a correct one. Completion.create is called with default config from PGTQ_OPENAI_CONFIG with any provided kwargs serving as parameter overrides. TODO: Later, add error handling. """ # if getattr(openai, "api_key") is None: # Initialize OpenAI API Key openai.api_key = os.getenv("OPENAI_API_KEY") if openai.api_key is None: raise EnvVarError("OPENAI_API_KEY not found in environment") if not system: system = "you are a text-to-SQL translator. You write PostgreSQL code based on plain-language prompts." query = [{"role":"system", "content": system}, {"role":"user", "content": prompt}] response = openai.ChatCompletion.create( messages=query, **{**CHAT_COMPLETION_CONFIG, **kwargs}, ) generated_query = response["choices"][0]["message"]["content"] if validate_sql: if not is_valid_query(generated_query): raise QueryGenError("Generated query is empty, only a comment, or invalid.") return generated_query def is_valid_query(query: str) -> bool: """Validates query syntax using Postgres parser. Note: in this context, "invalid" includes a query that is empty or only a SQL comment, which is different from the typical sense of "valid Postgres". """ parse_result = None valid = True try: parse_result = parse_sql(query) except ParseError as e: valid = False # Check for any empty result (occurs if completion is empty or a comment) return parse_result and valid
[]
2024-01-10
wxwcd/ChatGLM3
langchain_demo~ChatGLM3.py
import json from langchain.llms.base import LLM from transformers import AutoTokenizer, AutoModel, AutoConfig from typing import List, Optional from utils import tool_config_from_file class ChatGLM3(LLM): max_token: int = 8192 do_sample: bool = False temperature: float = 0.8 top_p = 0.8 tokenizer: object = None model: object = None history: List = [] tool_names: List = [] has_search: bool = False def __init__(self): super().__init__() @property def _llm_type(self) -> str: return "ChatGLM3" def load_model(self, model_name_or_path=None): model_config = AutoConfig.from_pretrained( model_name_or_path, trust_remote_code=True ) self.tokenizer = AutoTokenizer.from_pretrained( model_name_or_path, trust_remote_code=True ) self.model = AutoModel.from_pretrained( model_name_or_path, config=model_config, trust_remote_code=True, device_map="auto").eval() def _tool_history(self, prompt: str): ans = [] tool_prompts = prompt.split( "You have access to the following tools:\n\n")[1].split("\n\nUse a json blob")[0].split("\n") tool_names = [tool.split(":")[0] for tool in tool_prompts] self.tool_names = tool_names tools_json = [] for i, tool in enumerate(tool_names): tool_config = tool_config_from_file(tool) if tool_config: tools_json.append(tool_config) else: ValueError( f"Tool {tool} config not found! It's description is {tool_prompts[i]}" ) ans.append({ "role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:", "tools": tools_json }) query = f"""{prompt.split("Human: ")[-1].strip()}""" return ans, query def _extract_observation(self, prompt: str): return_json = prompt.split("Observation: ")[-1].split("\nThought:")[0] self.history.append({ "role": "observation", "content": return_json }) return def _extract_tool(self): if len(self.history[-1]["metadata"]) > 0: metadata = self.history[-1]["metadata"] content = self.history[-1]["content"] if "tool_call" in content: for tool in self.tool_names: if tool in metadata: input_para = content.split("='")[-1].split("'")[0] action_json = { "action": tool, "action_input": input_para } self.has_search = True return f""" Action: ``` {json.dumps(action_json, ensure_ascii=False)} ```""" final_answer_json = { "action": "Final Answer", "action_input": self.history[-1]["content"] } self.has_search = False return f""" Action: ``` {json.dumps(final_answer_json, ensure_ascii=False)} ```""" def _call(self, prompt: str, history: List = [], stop: Optional[List[str]] = ["<|user|>"]): if not self.has_search: self.history, query = self._tool_history(prompt) else: self._extract_observation(prompt) query = "" _, self.history = self.model.chat( self.tokenizer, query, history=self.history, do_sample=self.do_sample, max_length=self.max_token, temperature=self.temperature, ) response = self._extract_tool() history.append((prompt, response)) return response
[ "Answer the following questions as best as you can. You have access to the following tools:", "\n", "You have access to the following tools:\n\n", "\n\nUse a json blob" ]
2024-01-10
KenWuqianghao/MedChat
classify.py
import cohere import os from cohere.responses.classify import Example # get cohere api key from .env from dotenv import load_dotenv import os load_dotenv() COHERE_API_KEY = os.getenv("COHERE_API_KEY") co = cohere.Client(COHERE_API_KEY) INTENTS = {'General QA': 0, 'Diagnose Brain Tumour': 1, 'Blood Work': 2} BRAIN_TUMOUR = "Diagnose Brain Tumour" OTHER = "Other" def get_user_intent(user_message): examples = [ Example("I need a tumour diagnoses on this brain scan.", BRAIN_TUMOUR), Example("Can you make a diagnoses for this brain MRI?", BRAIN_TUMOUR), Example("What is the cancer likelihood for this MRI scan of a patient's brain?", BRAIN_TUMOUR), Example("What is the probability of positive tumour diagnosis for this brain MRI.", BRAIN_TUMOUR), Example("I uploaded a brain scan, can you analyze and interpret it for me?", BRAIN_TUMOUR), Example("What is the survival rate for stage 2 lung cancer", OTHER), Example("What is the survival rate for brain tumour", OTHER), Example("How is indigestion cured?", OTHER), Example("What are the symptoms of diabetes?", OTHER), ] # Sends the classification request to the Cohere model user_intent = co.classify( model='large', inputs=[user_message], examples=examples ) return user_intent.classifications[0].predictions
[]
2024-01-10
KenWuqianghao/MedChat
rag.py
# import Document class from doc.py from doc import Documents import uuid from typing import List, Dict import cohere # get cohere api key from .env from dotenv import load_dotenv import os load_dotenv() COHERE_API_KEY = os.getenv("COHERE_API_KEY") co = cohere.Client(COHERE_API_KEY) class Rag: """ A class representing a chatbot. Parameters: docs (Documents): An instance of the Documents class representing the collection of documents. Attributes: conversation_id (str): The unique ID for the conversation. docs (Documents): An instance of the Documents class representing the collection of documents. Methods: generate_response(message): Generates a response to the user's message. retrieve_docs(response): Retrieves documents based on the search queries in the response. """ def __init__(self, docs: Documents): self.docs = docs self.conversation_id = str(uuid.uuid4()) def search_query(self, message: str): # If there are search queries, retrieve documents and respond if response.search_queries: return response else: return False, response def generate_response(self, message: str, doc: Documents, response): """ Generates a response to the user's message. Parameters: message (str): The user's message. Yields: Event: A response event generated by the chatbot. Returns: List[Dict[str, str]]: A list of dictionaries representing the retrieved documents. """ # Generate search queries (if any) if response.search_queries: response = co.chat( message=message, documents=doc, conversation_id=self.conversation_id, stream=True, ) for event in response: yield event # If there is no search query, directly respond else: response = co.chat( message=message, conversation_id=self.conversation_id, stream=True ) for event in response: yield event def retrieve_docs(self, response) -> List[Dict[str, str]]: """ Retrieves documents based on the search queries in the response. Parameters: response: The response object containing search queries. Returns: List[Dict[str, str]]: A list of dictionaries representing the retrieved documents. """ # Get the query(s) queries = [] for search_query in response.search_queries: queries.append(search_query["text"]) # Retrieve documents for each query retrieved_docs = [] for query in queries: retrieved_docs.extend(self.docs.retrieve(query)) # # Uncomment this code block to display the chatbot's retrieved documents # print("DOCUMENTS RETRIEVED:") # for idx, doc in enumerate(retrieved_docs): # print(f"doc_{idx}: {doc}") # print("\n") return retrieved_docs
[]
2024-01-10
KenWuqianghao/MedChat
doc.py
import cohere import os import hnswlib import json import uuid import requests from typing import List, Dict import validators from unstructured.partition.html import partition_html from unstructured.chunking.title import chunk_by_title # get cohere api key from .env from dotenv import load_dotenv import os load_dotenv() COHERE_API_KEY = os.getenv("COHERE_API_KEY") co = cohere.Client(COHERE_API_KEY) headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'} class Documents: """ A class representing a collection of documents. Parameters: sources (list): A list of dictionaries representing the sources of the documents. Each dictionary should have 'title' and 'url' keys. Attributes: sources (list): A list of dictionaries representing the sources of the documents. docs (list): A list of dictionaries representing the documents, with 'title', 'content', and 'url' keys. docs_embs (list): A list of the associated embeddings for the documents. retrieve_top_k (int): The number of documents to retrieve during search. rerank_top_k (int): The number of documents to rerank after retrieval. docs_len (int): The number of documents in the collection. index (hnswlib.Index): The index used for document retrieval. Methods: load(): Loads the data from the sources and partitions the HTML content into chunks. embed(): Embeds the documents using the Cohere API. index(): Indexes the documents for efficient retrieval. retrieve(query): Retrieves documents based on the given query. """ def __init__(self, sources: List[Dict[str, str]]): self.sources = sources self.docs = [] self.docs_embs = [] self.retrieve_top_k = 10 self.rerank_top_k = 3 self.load() self.embed() self.index() def load(self) -> None: """ Loads the documents from the sources and chunks the HTML content. """ print("Loading documents...") for source in self.sources: try: elements = partition_html(url=source["url"], headers=headers) except: continue chunks = chunk_by_title(elements) for chunk in chunks: self.docs.append( { "title": source["title"], "text": str(chunk), "url": source["url"], } ) def embed(self) -> None: """ Embeds the documents using the Cohere API. """ print("Embedding documents...") batch_size = 90 self.docs_len = len(self.docs) for i in range(0, self.docs_len, batch_size): batch = self.docs[i : min(i + batch_size, self.docs_len)] texts = [item["text"] for item in batch] docs_embs_batch = co.embed( texts=texts, model="embed-english-v3.0", input_type="search_document" ).embeddings self.docs_embs.extend(docs_embs_batch) def index(self) -> None: """ Indexes the documents for efficient retrieval. """ print("Indexing documents...") self.idx = hnswlib.Index(space="ip", dim=1024) self.idx.init_index(max_elements=self.docs_len, ef_construction=512, M=64) self.idx.add_items(self.docs_embs, list(range(len(self.docs_embs)))) print(f"Indexing complete with {self.idx.get_current_count()} documents.") def retrieve(self, query: str) -> List[Dict[str, str]]: """ Retrieves documents based on the given query. Parameters: query (str): The query to retrieve documents for. Returns: List[Dict[str, str]]: A list of dictionaries representing the retrieved documents, with 'title', 'text', and 'url' keys. """ docs_retrieved = [] query_emb = co.embed( texts=[query], model="embed-english-v3.0", input_type="search_query" ).embeddings doc_ids = self.idx.knn_query(query_emb, k=self.retrieve_top_k)[0][0] docs_to_rerank = [] for doc_id in doc_ids: docs_to_rerank.append(self.docs[doc_id]["text"]) rerank_results = co.rerank( query=query, documents=docs_to_rerank, top_n=self.rerank_top_k, model="rerank-english-v2.0", ) doc_ids_reranked = [] for result in rerank_results: doc_ids_reranked.append(doc_ids[result.index]) for doc_id in doc_ids_reranked: docs_retrieved.append( { "title": self.docs[doc_id]["title"], "text": self.docs[doc_id]["text"], "url": self.docs[doc_id]["url"], } ) return docs_retrieved
[]
2024-01-10
KenWuqianghao/MedChat
chatbot.py
import cohere import numpy as np from PIL import Image from classify import get_user_intent from utils import BrainTumourDiagnosisAgent from doc import Documents from typing import List from rag import Rag import pickle # get cohere api key from .env from dotenv import load_dotenv import os load_dotenv() COHERE_API_KEY = os.getenv("COHERE_API_KEY") co = cohere.Client(COHERE_API_KEY) DOCS = pickle.load(open("docs.pkl", "rb")) SYSTEM_MESSAGE_PROMPT = """ You are a chat bot named MedChat, a help agent for medical professionals that answers questions concerning medical conditions and diagnoses. You have access to medical documents with reliable information which you can use to answer questions. You are able to answer two types of user questions. 1. Diagnose brain MRI images 2. Answer general medical questions using medical literature Any question that isn't about medicine, or disease diagnoses should not be answered. If a user asks a question that isn't about medicine, you should tell them that you aren't able to help them with their query. Keep your answers concise, and shorter than 5 sentences. """ MEMORY_KEY = "chat_history" # get cohere api key from .env from dotenv import load_dotenv import os load_dotenv() COHERE_API_KEY = os.getenv("COHERE_API_KEY") class MedicalChatBot: """ Master Agent. """ def __init__(self, api_key, uploaded_files) -> None: self.api_key = api_key self.uploaded_files = uploaded_files self.co = cohere.Client(COHERE_API_KEY) def read_image(self, file): # Read the image file into a NumPy array image = Image.open(file) image_array = np.array(image) return image_array def get_image_file(self): if self.uploaded_files: file = self.uploaded_files[-1] if file.type.startswith("image"): return self.read_image(file) return None def generate_response(self, message, chat_history, message_placeholder): full_response = "" for response in self.co.chat( message=message, model="command-nightly", chat_history=[ {"role": m["role"], "message": m["message"]} for m in chat_history ], stream=True ): if response.event_type == 'text-generation': full_response += (response.text) message_placeholder.markdown(full_response + "โ–Œ") return full_response def return_selected_docs(self, docs: Documents, cited_docs: List[str]) -> None: full_response = "" for doc in cited_docs: index = int(doc[4:]) - 1 citation = docs[index] full_response += f"Source Title: {citation['title']}\n" full_response += "\n" full_response += f"Source URL: {citation['url']}\n" full_response += "\n" return full_response def query(self, message, chat_history, message_placeholder): # first we check the user intent intent = get_user_intent(message) if intent[0] == "Diagnose Brain Tumour": # call brain diagnosis model image = self.get_image_file() test = BrainTumourDiagnosisAgent(image) result = test.diagnose() message = f"According to the disease diagnosis models, the probability of a positive tumour diagnosis is {result}%. Write a one-sentence message to the user confirming this information. Give the answer as a percent. Do not answer in more than one sentence." full_response = self.generate_response(message, chat_history=chat_history, message_placeholder=message_placeholder) return full_response if intent[0] == "Other": rag = Rag(DOCS) response = co.chat(message=message, search_queries_only=True) doc = rag.retrieve_docs(response) response = rag.generate_response(message, doc, response) full_response = "" flag = False for event in response: if event.event_type == "text-generation": full_response += (event.text) message_placeholder.markdown(full_response + "โ–Œ") # Citations elif event.event_type == "citation-generation": if not flag: full_response += '\n' full_response += '\nCitations:\n' full_response += '\n' flag = True for citation in event.citations: full_response += self.return_selected_docs(doc, citation['document_ids']) full_response += '\n' full_response += f"Start Index: {citation['start']}, End Index: {citation['end']}, Cited Text: {citation['text']}\n" full_response += '\n' message_placeholder.markdown(full_response + "โ–Œ") return full_response else: return "Something went wrong"
[ "\nYou are a chat bot named MedChat, a help agent for medical professionals that answers questions concerning medical conditions and diagnoses. You have access to medical documents with reliable information which you can use to answer questions.\nYou are able to answer two types of user questions.\n1. Diagnose brain MRI images\n2. Answer general medical questions using medical literature\n\nAny question that isn't about medicine, or disease diagnoses should not be answered. If a user asks a question that isn't about medicine, you should tell them that you aren't able to help them with their query. Keep your answers concise, and shorter than 5 sentences.\n" ]
2024-01-10
vovanshil95/gpt-writer-admin
app~gpt_interactions~router.py
from fastapi import APIRouter from sqlalchemy import func, desc import openai import uuid import datetime from zoneinfo import ZoneInfo from workspace.models import Workspace from gpt_interactions.models import GptInteraction, FilledPrompt from gpt_interactions.schemas import InteractionsResponse, InteractionSchema, GptRequestSchema, GptAnswerResponse from init import sqlalchemy_session router = APIRouter(prefix='/api', tags=['GPT Interactions']) def get_interactions(message: str) -> InteractionsResponse: with sqlalchemy_session.begin() as session: history = session.query(GptInteraction, func.array_agg(FilledPrompt.text_data), func.array_agg(FilledPrompt.number)) \ .filter(GptInteraction.workspace_id == session.query(Workspace.id).filter(Workspace.initial).first()[0]) \ .join(FilledPrompt).group_by(GptInteraction.id)\ .order_by(desc(GptInteraction.time_happened))\ .all() history = list(map(lambda el: InteractionSchema( id=el[0].id, request=GptRequestSchema( prompt=list(zip(*sorted(zip(el[1],el[2]), key=lambda el: el[1])))[0], username=el[0].username, company=el[0].company, ), datetime=el[0].time_happened, favorite=el[0].favorite, gpt_response=el[0].gpt_answer), history)) return InteractionsResponse(status='success', message=message, data=history) @router.put('/response') def get_response(request: GptRequestSchema) -> GptAnswerResponse: response = openai.ChatCompletion.create(model='gpt-4', messages=[{'role': 'user', 'content': '\n'.join(request.prompt)}]) answer = response['choices'][0]['message']['content'] interaction_id = uuid.UUID(hex=str(uuid.uuid4())) with sqlalchemy_session.begin() as session: session.add(GptInteraction(id=interaction_id, gpt_answer=answer, username=request.username, favorite=False, company=request.company, time_happened=datetime.datetime.now(ZoneInfo('Europe/Moscow')), workspace_id=session.query(Workspace.id).filter(Workspace.initial).first()[0])) session.flush() session.add_all(map(lambda i, pr: FilledPrompt(id=uuid.UUID(hex=str(uuid.uuid4())), text_data=pr, gpt_interaction_id=interaction_id, number=i), *zip(*enumerate(request.prompt)))) return GptAnswerResponse(status='success', message='GPT Response successfully retrieved', data={'gpt_response': answer}) @router.get('/history') def get_history() -> InteractionsResponse: return get_interactions('History successfully retrieved') @router.put('/favoriteHistory') def add_to_favorite(id: uuid.UUID)->InteractionsResponse: with sqlalchemy_session.begin() as session: session.get(GptInteraction, id).favorite = True return get_interactions('Interaction successfully added to favorite') @router.delete('/favoriteHistory') def delete_from_favorite(id: uuid.UUID)->InteractionsResponse: with sqlalchemy_session.begin() as session: session.get(GptInteraction, id).favorite = False return get_interactions('Interaction successfully deleted from favorite')
[ "\n" ]
2024-01-10
vovanshil95/gpt-writer-admin
app~init.py
from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine import openai from config import sqlalchemy_url, OPENAI_API_KEY sql_engine = create_engine(sqlalchemy_url) sqlalchemy_session = sessionmaker(sql_engine) openai.api_key = OPENAI_API_KEY
[]
2024-01-10
b08x/teaGPT
teaGPT.py
import os import openai import streamlit as st try: if os.environ["OPENAI_API_KEY"]: openai.api_key = os.environ["OPENAI_API_KEY"] else: openai.api_key = st.secrets.OPENAI_API_KEY except Exception as e: st.write(e) # ------------------------------------------------------------ # # Visual settings and functions # # ------------------------------------------------------------ st.set_page_config( page_title="teaGPT", page_icon="๐Ÿ—ก๏ธ", initial_sidebar_state="collapsed" ) def local_css(file_name): with open(file_name) as f: st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) with st.sidebar: st.write("whatup") st.markdown("<br>", unsafe_allow_html=True) st.title("๐Ÿ’ฌ Chatbot") if "messages" not in st.session_state: st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}] for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"]) if prompt := st.chat_input(): st.session_state.messages.append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt) response = openai.ChatCompletion.create(model="gpt-3.5-turbo-16k-0613", messages=st.session_state.messages) msg = response.choices[0].message st.session_state.messages.append(msg) st.chat_message("assistant").write(msg.content)
[ "How can I help you?" ]
2024-01-10
b08x/teaGPT
pages~1_File_Q%26A.py
import streamlit as st import anthropic with st.sidebar: anthropic_api_key = st.text_input("Anthropic API Key", key="file_qa_api_key", type="password") "[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/1_File_Q%26A.py)" "[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)" st.title("๐Ÿ“ File Q&A with Anthropic") uploaded_file = st.file_uploader("Upload an article", type=("txt", "md")) question = st.text_input( "Ask something about the article", placeholder="Can you give me a short summary?", disabled=not uploaded_file, ) if uploaded_file and question and not anthropic_api_key: st.info("Please add your Anthropic API key to continue.") if uploaded_file and question and anthropic_api_key: article = uploaded_file.read().decode() prompt = f"""{anthropic.HUMAN_PROMPT} Here's an article:\n\n<article> {article}\n\n</article>\n\n{question}{anthropic.AI_PROMPT}""" client = anthropic.Client(anthropic_api_key) response = client.completion( prompt=prompt, stop_sequences=[anthropic.HUMAN_PROMPT], model="claude-v1", max_tokens_to_sample=100, ) st.write("### Answer") st.write(response["completion"])
[]
2024-01-10
b08x/teaGPT
pages~4_Langchain_PromptTemplate.py
import os import streamlit as st import openai from langchain.llms import OpenAI from langchain.prompts import PromptTemplate try: if os.environ["OPENAI_API_KEY"]: openai.api_key = os.environ["OPENAI_API_KEY"] else: openai.api_key = st.secrets.OPENAI_API_KEY except Exception as e: st.write(e) st.title("๐Ÿฆœ๐Ÿ”— Langchain - Blog Outline Generator App") def blog_outline(topic): # Instantiate LLM model llm = OpenAI(model_name="text-davinci-003", openai_api_key=openai.api_key) # Prompt template = "As an experienced data scientist and technical writer, generate an outline for a blog about {topic}." prompt = PromptTemplate(input_variables=["topic"], template=template) prompt_query = prompt.format(topic=topic) # Run LLM model response = llm(prompt_query) # Print results return st.info(response) with st.form("myform"): topic_text = st.text_input("Enter prompt:", "") submitted = st.form_submit_button("Submit") if not openai.api_key: st.info("Please add your OpenAI API key to continue.") elif submitted: blog_outline(topic_text)
[ "As an experienced data scientist and technical writer, generate an outline for a blog about {topic}." ]
2024-01-10
b08x/teaGPT
pages~5_dox.py
import sys, os import streamlit as st import openai try: if os.environ["OPENAI_API_KEY"]: openai.api_key = os.environ["OPENAI_API_KEY"] else: openai.api_key = st.secrets.OPENAI_API_KEY except Exception as e: st.write(e) dox_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/pages/dox') sys.path.append(dox_dir) print(sys.path) from sidebar import sidebar from ui import ( wrap_doc_in_html, is_query_valid, is_file_valid, is_open_ai_key_valid, display_file_read_error, ) from core.caching import bootstrap_caching from core.parsing import read_file from core.chunking import chunk_file from core.embedding import embed_files from core.qa import query_folder EMBEDDING = "openai" VECTOR_STORE = "faiss" MODEL = "openai" # For testing # EMBEDDING, VECTOR_STORE, MODEL = ["debug"] * 3 st.set_page_config(page_title="KnowledgeGPT", page_icon="๐Ÿ“–", layout="wide") st.header("๐Ÿ“–KnowledgeGPT") # Enable caching for expensive functions bootstrap_caching() sidebar() uploaded_file = st.file_uploader( "Upload a pdf, docx, or txt file", type=["pdf", "docx", "txt"], help="Scanned documents are not supported yet!", ) if not uploaded_file: st.stop() try: file = read_file(uploaded_file) except Exception as e: display_file_read_error(e) chunked_file = chunk_file(file, chunk_size=300, chunk_overlap=0) if not is_file_valid(file): st.stop() if not is_open_ai_key_valid(openai.api_key): st.stop() with st.spinner("Indexing document... This may take a whileโณ"): folder_index = embed_files( files=[chunked_file], embedding=EMBEDDING, vector_store=VECTOR_STORE, openai_api_key=openai.api_key, ) with st.form(key="qa_form"): query = st.text_area("Ask a question about the document") submit = st.form_submit_button("Submit") with st.expander("Advanced Options"): return_all_chunks = st.checkbox("Show all chunks retrieved from vector search") show_full_doc = st.checkbox("Show parsed contents of the document") if show_full_doc: with st.expander("Document"): # Hack to get around st.markdown rendering LaTeX st.markdown(f"<p>{wrap_doc_in_html(file.docs)}</p>", unsafe_allow_html=True) if submit: if not is_query_valid(query): st.stop() # Output Columns answer_col, sources_col = st.columns(2) result = query_folder( folder_index=folder_index, query=query, return_all=return_all_chunks, model=MODEL, openai_api_key=openai.api_key, temperature=0, ) with answer_col: st.markdown("#### Answer") st.markdown(result.answer) with sources_col: st.markdown("#### Sources") for source in result.sources: st.markdown(source.page_content) st.markdown(source.metadata["source"]) st.markdown("---")
[]
2024-01-10
b08x/teaGPT
pages~2_Chat_with_search.py
import os import streamlit as st import openai from langchain.agents import initialize_agent, AgentType from langchain.callbacks import StreamlitCallbackHandler from langchain.chat_models import ChatOpenAI from langchain.tools import DuckDuckGoSearchRun try: if os.environ["OPENAI_API_KEY"]: openai.api_key = os.environ["OPENAI_API_KEY"] else: openai.api_key = st.secrets.OPENAI_API_KEY except Exception as e: st.write(e) with st.sidebar: st.write("hey") st.title("๐Ÿ”Ž LangChain - Chat with search") """ In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app. Try more LangChain ๐Ÿค Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent). """ if "messages" not in st.session_state: st.session_state["messages"] = [ {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"} ] for msg in st.session_state.messages: st.chat_message(msg["role"]).write(msg["content"]) if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"): st.session_state.messages.append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt) if not openai.api_key: st.info("Please add your OpenAI API key to continue.") st.stop() llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai.api_key, streaming=True) search = DuckDuckGoSearchRun(name="Search") search_agent = initialize_agent([search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True) with st.chat_message("assistant"): st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False) response = search_agent.run(st.session_state.messages, callbacks=[st_cb]) st.session_state.messages.append({"role": "assistant", "content": response}) st.write(response)
[ "Hi, I'm a chatbot who can search the web. How can I help you?" ]
2024-01-10
b08x/teaGPT
pages~dox~core~chunking.py
from langchain.docstore.document import Document from langchain.text_splitter import RecursiveCharacterTextSplitter from core.parsing import File def chunk_file( file: File, chunk_size: int, chunk_overlap: int = 0, model_name="gpt-3.5-turbo" ) -> File: """Chunks each document in a file into smaller documents according to the specified chunk size and overlap where the size is determined by the number of token for the specified model. """ # split each document into chunks chunked_docs = [] for doc in file.docs: text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder( model_name=model_name, chunk_size=chunk_size, chunk_overlap=chunk_overlap, ) chunks = text_splitter.split_text(doc.page_content) for i, chunk in enumerate(chunks): doc = Document( page_content=chunk, metadata={ "page": doc.metadata.get("page", 1), "chunk": i + 1, "source": f"{doc.metadata.get('page', 1)}-{i + 1}", }, ) chunked_docs.append(doc) chunked_file = file.copy() chunked_file.docs = chunked_docs return chunked_file
[]
2024-01-10
b08x/teaGPT
pages~3_Langchain_Quickstart.py
import os import streamlit as st import openai from langchain.llms import OpenAI try: if os.environ["OPENAI_API_KEY"]: openai.api_key = os.environ["OPENAI_API_KEY"] else: openai.api_key = st.secrets.OPENAI_API_KEY except Exception as e: st.write(e) st.title("๐Ÿฆœ๐Ÿ”— Langchain Quickstart App") with st.sidebar: st.write("hey") def generate_response(input_text): llm = OpenAI(temperature=0.7, openai_api_key=openai.api_key) st.info(llm(input_text)) with st.form("my_form"): text = st.text_area("Enter text:", "What are 3 key advice for learning how to code?") submitted = st.form_submit_button("Submit") if not openai.api_key: st.info("Please add your OpenAI API key to continue.") elif submitted: generate_response(text)
[]
2024-01-10
b08x/teaGPT
pages~dox~ui.py
from typing import List import streamlit as st from langchain.docstore.document import Document from core.parsing import File import openai from streamlit.logger import get_logger from typing import NoReturn logger = get_logger(__name__) def wrap_doc_in_html(docs: List[Document]) -> str: """Wraps each page in document separated by newlines in <p> tags""" text = [doc.page_content for doc in docs] if isinstance(text, list): # Add horizontal rules between pages text = "\n<hr/>\n".join(text) return "".join([f"<p>{line}</p>" for line in text.split("\n")]) def is_query_valid(query: str) -> bool: if not query: st.error("Please enter a question!") return False return True def is_file_valid(file: File) -> bool: if ( len(file.docs) == 0 or "".join([doc.page_content for doc in file.docs]).strip() == "" ): st.error("Cannot read document! Make sure the document has selectable text") logger.error("Cannot read document") return False return True def display_file_read_error(e: Exception) -> NoReturn: st.error("Error reading file. Make sure the file is not corrupted or encrypted") logger.error(f"{e.__class__.__name__}: {e}") st.stop() @st.cache_data(show_spinner=False) def is_open_ai_key_valid(openai_api_key) -> bool: if not openai_api_key: st.error("Please enter your OpenAI API key in the sidebar!") return False try: openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "test"}], api_key=openai_api_key, ) except Exception as e: st.error(f"{e.__class__.__name__}: {e}") logger.error(f"{e.__class__.__name__}: {e}") return False return True
[ "test" ]
2024-01-10
SunflowerPKU/ICSE22_SC_Data
RQ2_domain_distribution~projects~ga_lda.py
import numpy as np from sklearn import feature_extraction from sklearn.feature_extraction.text import CountVectorizer from pyevolve import G1DList from pyevolve import GSimpleGA from pyevolve import Crossovers import numpy as np import os import sys from gensim import corpora, models, interfaces import gensim from itertools import izip from joblib import Parallel, delayed import multiprocessing from multiprocessing import Process, Manager from threading import Thread import scipy.spatial from gensim.models.coherencemodel import CoherenceModel # foldermain = 'RQ3' foldername = sys.argv[1] foldermodels = sys.argv[2] print foldername, foldermodels clu2orig={} docTopicProbMat=None corpus = [] fileList = os.listdir(foldername) count = 0 corpus = [] texts = [] rc = 0 for f in fileList: if (rc % 10000 == 0): print("Processed ::" + str(rc) + ":: Files ") f = open(foldername + f, 'r') txt = str(f.read()) corpus.append(txt) texts.append(txt.split()) rc += 1 dictionary = corpora.Dictionary(texts) # dictionary.filter_extremes(no_below=1000, no_above=0.5) corpus2 = [dictionary.doc2bow(text) for text in texts] dictionary.save(foldermodels+'MultiCore.dict') corpora.MmCorpus.serialize(foldermodels+'MultiCoreCorpus.mm', corpus2) # term frequency NumApp = len(corpus) NumFeatures = len(dictionary) #vectorizer=CountVectorizer(stop_words='english', strip_accents='ascii', max_features=NumFeatures, dtype=np.int32) # vectorizer = CountVectorizer(max_features=NumFeatures, dtype=np.int32) # tf_array = vectorizer.fit_transform(corpus).toarray() # vocab = vectorizer.get_feature_names() print("Starting Mutations::") print(NumApp) print(NumFeatures) # NumFeatures = len(vocab) # print(NumFeatures) print(count) Centers = [] Clusters = [] classes = [] logfile=open(foldermodels+'/log.txt','w') # sqD=scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(tf_array)) shScore = {} coScore = {} topic_num = None def eval_coherence(NTopic): # print NTopic[0] numoftopics = int((8*NTopic[0] + 500) / 330) iters = NTopic[1] al = (float(NTopic[2]) - 20) / 19800 bet = (float(NTopic[3]) - 20) / 19800 if al==0.0: al = 1/480 if bet==0.0: bet = 1/480 global coScore log=str(numoftopics) + ' ' + str(iters) + ' ' + str(al) + ' ' + str(bet) print log if not log in coScore: logfile.write(log + "\n") model = gensim.models.ldamulticore.LdaMulticore(corpus2,passes=10, num_topics=numoftopics, id2word=dictionary, iterations=iters, alpha=al, eta=bet, random_state=123) cm = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v') coScore[log] = cm.get_coherence() logfile.write("SCORE::" + str(coScore[log]) + "\n") return coScore[log] def eval_func_JustModel(NTopic): global count # NTopic[0]=2 print NTopic[0] global topic_num numoftopics = int((8*NTopic[0] + 500) / 330) topic_num = numoftopics iters = NTopic[1] al = (float(NTopic[2]) - 20) / 19800 bet = (float(NTopic[3]) - 20) / 19800 log=str(count)+' '+str(numoftopics) + ' ' + str(iters) + ' ' + str(al) + ' ' + str(bet) print log logfile.write(log + "\n") print("Creating Model::" + str(count)) model = gensim.models.ldamulticore.LdaMulticore(corpus2,passes=10, num_topics=numoftopics, id2word=dictionary,iterations=iters,alpha=al,eta=bet,random_state=123) cm = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v') print "Coherence score: " + str(cm.get_coherence()) model.save(foldermodels+str(numoftopics) +'_'+str(iters) + '.model') doc_topic_list = [] genome = G1DList.G1DList(4) genome.evaluator.set(eval_coherence) genome.setParams(rangemin=20, rangemax=2000) genome.crossover.set(Crossovers.G1DListCrossoverUniform) ga = GSimpleGA.GSimpleGA(genome) ga.setPopulationSize(100) ga.setGenerations(100) ga.evolve(freq_stats=1) print ga.bestIndividual() print(NumApp) print(count) fo = open(foldermodels+"bestindividual", "a") eval_func_JustModel(ga.bestIndividual().genomeList) fo.write(str(ga.bestIndividual())) logfile.write(str(ga.bestIndividual())+'\n') fo.close() logfile.close()
[]