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
vivkow/MA_Neuro
lda_tools.py
import re import matplotlib.pyplot as plt import numpy as np import gensim import gensim.corpora as corpora import pyLDAvis import pyLDAvis.gensim # don't skip this import spacy from gensim.models import CoherenceModel from gensim.utils import simple_preprocess def sentences_to_words(sentences): for sentence in sentences: yield (gensim.utils.simple_preprocess(str(sentence), deacc=True)) def remove_stopwords(texts, stop_words): return [ [word for word in simple_preprocess(str(doc)) if word not in stop_words] for doc in texts ] def make_bigrams(texts, bigram_mod): return [bigram_mod[doc] for doc in texts] def make_trigrams(texts, bigram_mod, trigram_mod): return [trigram_mod[bigram_mod[doc]] for doc in texts] def lemmatization(texts, nlp, allowed_postags=["NOUN", "ADJ", "VERB", "ADV"]): texts_out = [] for sent in texts: doc = nlp(" ".join(sent)) texts_out.append( [token.lemma_ for token in doc if token.pos_ in allowed_postags] ) return texts_out def clean_words(words, stop_words, trigram_mod=None): data_words_nostops = remove_stopwords(words, stop_words) # data_words_grams = make_bigrams(data_words_nostops, bigram_mod) # if trigram_mod is not None: # data_words_grams = make_trigrams( # data_words_grams, bigram_mod, trigram_mod # ) # nlp = spacy.load("en", disable=["parser", "ner"]) # VK starting from spacy 3.0 version, "en" is not supported as shortcut anymore nlp = spacy.load("en_core_web_sm", disable=["parser", "ner"]) # VK added this line data_lemmatized = lemmatization( data_words_nostops, nlp, allowed_postags=["NOUN", "ADJ", "VERB", "ADV"] ) return data_lemmatized def compute_coherence_values( limit, mallet_path, dictionary, corpus, texts, start=2, step=2 ): """ Compute c_v coherence for various number of topics Parameters: ---------- dictionary : Gensim dictionary corpus : Gensim corpus texts : List of input texts limit : Max num of topics Returns: ------- model_list : List of LDA topic models coherence_values : Coherence values corresponding to the LDA model with respective number of topics """ coherence_values = [] model_list = [] for num_topics in range(start, limit, step): model = gensim.models.wrappers.LdaMallet( mallet_path, corpus=corpus, num_topics=num_topics, id2word=dictionary, random_seed=100 ) model_list.append(model) coherencemodel = CoherenceModel( model=model, texts=texts, dictionary=dictionary, coherence="c_v" ) coherence_values.append(coherencemodel.get_coherence()) return model_list, coherence_values
[]
2024-01-10
vinoth12940/ask-multiple-pdfs-main
testingApp.py
import os import streamlit as st import pandas as pd from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationalRetrievalChain css = ''' <style> .chat-message { padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex } .chat-message.user { background-color: #2b313e } .chat-message.bot { background-color: #475063 } .chat-message .avatar { width: 20%; } .chat-message .avatar img { max-width: 78px; max-height: 78px; border-radius: 50%; object-fit: cover; } .chat-message .message { width: 80%; padding: 0 1.5rem; color: #fff; } ''' bot_template = ''' <div class="chat-message bot"> <div class="avatar"> <img src="AIImage.jpeg" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;"> </div> <div class="message">{{MSG}}</div> </div> ''' user_template = ''' <div class="chat-message user"> <div class="avatar"> <img src="human.png"> </div> <div class="message">{{MSG}}</div> </div> ''' def get_csv_text(csv_files): text = "" for csv_file in csv_files: df = pd.read_csv(csv_file) text += df.to_string(index=False) return text def get_text_chunks(text): text_splitter = CharacterTextSplitter( separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len ) chunks = text_splitter.split_text(text) return chunks def get_vectorstore(text_chunks): embeddings = OpenAIEmbeddings() vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings) return vectorstore def get_conversation_chain(vectorstore): llm = ChatOpenAI() memory = ConversationBufferMemory( memory_key='chat_history', return_messages=True) conversation_chain = ConversationalRetrievalChain.from_llm( llm=llm, retriever=vectorstore.as_retriever(), memory=memory ) return conversation_chain def handle_userinput(user_question): if st.session_state.conversation is not None: response = st.session_state.conversation({'question': user_question}) st.session_state.chat_history = response['chat_history'] for i, message in enumerate(st.session_state.chat_history): if i % 2 == 0: st.write(user_template.replace( "{{MSG}}", message.content), unsafe_allow_html=True) else: st.write(bot_template.replace( "{{MSG}}", message.content), unsafe_allow_html=True) else: st.write("Please upload CSV files and click 'Process' to start a conversation.") def main(): st.set_page_config(page_title="Chat with CSV", page_icon=":CSV:") st.write(css, unsafe_allow_html=True) if "conversation" not in st.session_state: st.session_state.conversation = None if "chat_history" not in st.session_state: st.session_state.chat_history = None st.header("Chat with CSV :books:") user_question = st.text_input("Ask a question about your data:") if user_question: handle_userinput(user_question) with st.sidebar: st.subheader("Your data") csv_files = st.file_uploader( "Upload your CSV files here and click on 'Process'", type=["csv"], accept_multiple_files=True) if st.button("Process"): with st.spinner("Processing"): # get csv text raw_text = get_csv_text(csv_files) # get the text chunks text_chunks = get_text_chunks(raw_text) # create vector store vectorstore = get_vectorstore(text_chunks) # create conversation chain st.session_state.conversation = get_conversation_chain( vectorstore) if __name__ == '__main__': os.environ["OPENAI_API_KEY"] = "" main()
[ "\n<div class=\"chat-message user\">\n <div class=\"avatar\">\n <img src=\"human.png\">\n </div> \n <div class=\"message\">{{MSG}}</div>\n</div>\n", "\n<div class=\"chat-message bot\">\n <div class=\"avatar\">\n <img src=\"AIImage.jpeg\" style=\"max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;\">\n </div>\n <div class=\"message\">{{MSG}}</div>\n</div>\n" ]
2024-01-10
vinoth12940/ask-multiple-pdfs-main
csvFileChat.py
import streamlit as st from dotenv import load_dotenv import pandas as pd from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationalRetrievalChain from htmlTemplates import css, bot_template, user_template import os def get_csv_text(csv_file): df = pd.read_csv(csv_file) text = df.to_string(index=False) return text def get_text_chunks(text): text_splitter = CharacterTextSplitter( separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len ) chunks = text_splitter.split_text(text) return chunks def get_vectorstore(text_chunks): embeddings = OpenAIEmbeddings() vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings) return vectorstore def get_conversation_chain(vectorstore): llm = ChatOpenAI() memory = ConversationBufferMemory( memory_key='chat_history', return_messages=True) conversation_chain = ConversationalRetrievalChain.from_llm( llm=llm, retriever=vectorstore.as_retriever(), memory=memory ) return conversation_chain def handle_userinput(user_question): if st.session_state.conversation is not None: response = st.session_state.conversation({'question': user_question}) st.session_state.chat_history = response['chat_history'] for i, message in enumerate(st.session_state.chat_history): if i % 2 == 0: st.write(user_template.replace( "{{MSG}}", message.content), unsafe_allow_html=True) else: st.write(bot_template.replace( "{{MSG}}", message.content), unsafe_allow_html=True) else: st.write("Please upload a CSV file and click 'Process' to start a conversation.") def main(): load_dotenv() st.set_page_config(page_title="Chat with CSV", page_icon=":CSV:") st.write(css, unsafe_allow_html=True) if "conversation" not in st.session_state: st.session_state.conversation = None if "chat_history" not in st.session_state: st.session_state.chat_history = None st.header("Chat with CSV :books:") user_question = st.text_input("Ask a question about your data:") if user_question: handle_userinput(user_question) with st.sidebar: st.subheader("Your data") csv_file = st.file_uploader( "Upload your CSV file here and click on 'Process'", type=["csv"]) if st.button("Process"): with st.spinner("Processing"): # get csv text raw_text = get_csv_text(csv_file) # get the text chunks text_chunks = get_text_chunks(raw_text) # create vector store vectorstore = get_vectorstore(text_chunks) # create conversation chain st.session_state.conversation = get_conversation_chain( vectorstore) if __name__ == '__main__': os.environ["OPENAI_API_KEY"] = "" main()
[]
2024-01-10
JohnYe/langchain
00_%E5%BC%80%E7%AF%87%E8%AF%8D_%E5%A5%87%E7%82%B9%E6%97%B6%E5%88%BB~01_%E7%AE%80%E5%8D%95%E6%96%87%E6%9C%AC%E7%94%9F%E6%88%90.py
'''欢迎来到LangChain实战课 https://time.geekbang.org/column/intro/100617601 作者 黄佳''' import os os.environ["OPENAI_API_KEY"] = '你的Open AI API Key' from langchain.llms import OpenAI llm = OpenAI(model_name="text-davinci-003",max_tokens=200) text = llm("请给我写一句情人节红玫瑰的中文宣传语") print(text)
[]
2024-01-10
binary-husky/hmp2g
ALGORITHM~stable_baselines3~common~policies.py
"""Policies: abstract base class and concrete implementations.""" import collections from abc import ABC, abstractmethod from functools import partial from typing import Any, Dict, List, Optional, Tuple, Type, Union import gym import numpy as np import torch as th from torch import nn from stable_baselines3.common.distributions import ( BernoulliDistribution, CategoricalDistribution, DiagGaussianDistribution, Distribution, MultiCategoricalDistribution, StateDependentNoiseDistribution, make_proba_distribution, ) from stable_baselines3.common.preprocessing import get_action_dim, maybe_transpose, preprocess_obs from stable_baselines3.common.torch_layers import BaseFeaturesExtractor, FlattenExtractor, MlpExtractor, NatureCNN, create_mlp from stable_baselines3.common.type_aliases import Schedule from stable_baselines3.common.utils import get_device, is_vectorized_observation from stable_baselines3.common.vec_env.obs_dict_wrapper import ObsDictWrapper class BaseModel(nn.Module, ABC): """ The base model object: makes predictions in response to observations. In the case of policies, the prediction is an action. In the case of critics, it is the estimated value of the observation. :param observation_space: The observation space of the environment :param action_space: The action space of the environment :param features_extractor_class: Features extractor to use. :param features_extractor_kwargs: Keyword arguments to pass to the features extractor. :param features_extractor: Network to extract features (a CNN when using images, a nn.Flatten() layer otherwise) :param normalize_images: Whether to normalize images or not, dividing by 255.0 (True by default) :param optimizer_class: The optimizer to use, ``th.optim.Adam`` by default :param optimizer_kwargs: Additional keyword arguments, excluding the learning rate, to pass to the optimizer """ def __init__( self, observation_space: gym.spaces.Space, action_space: gym.spaces.Space, features_extractor_class: Type[BaseFeaturesExtractor] = FlattenExtractor, features_extractor_kwargs: Optional[Dict[str, Any]] = None, features_extractor: Optional[nn.Module] = None, normalize_images: bool = True, optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam, optimizer_kwargs: Optional[Dict[str, Any]] = None, ): super(BaseModel, self).__init__() if optimizer_kwargs is None: optimizer_kwargs = {} if features_extractor_kwargs is None: features_extractor_kwargs = {} self.observation_space = observation_space self.action_space = action_space self.features_extractor = features_extractor self.normalize_images = normalize_images self.optimizer_class = optimizer_class self.optimizer_kwargs = optimizer_kwargs self.optimizer = None # type: Optional[th.optim.Optimizer] self.features_extractor_class = features_extractor_class self.features_extractor_kwargs = features_extractor_kwargs @abstractmethod def forward(self, *args, **kwargs): pass def _update_features_extractor( self, net_kwargs: Dict[str, Any], features_extractor: Optional[BaseFeaturesExtractor] = None ) -> Dict[str, Any]: """ Update the network keyword arguments and create a new features extractor object if needed. If a ``features_extractor`` object is passed, then it will be shared. :param net_kwargs: the base network keyword arguments, without the ones related to features extractor :param features_extractor: a features extractor object. If None, a new object will be created. :return: The updated keyword arguments """ net_kwargs = net_kwargs.copy() if features_extractor is None: # The features extractor is not shared, create a new one features_extractor = self.make_features_extractor() net_kwargs.update(dict(features_extractor=features_extractor, features_dim=features_extractor.features_dim)) return net_kwargs def make_features_extractor(self) -> BaseFeaturesExtractor: """ Helper method to create a features extractor.""" return self.features_extractor_class(self.observation_space, **self.features_extractor_kwargs) def extract_features(self, obs: th.Tensor) -> th.Tensor: """ Preprocess the observation if needed and extract features. :param obs: :return: """ assert self.features_extractor is not None, "No features extractor was set" preprocessed_obs = preprocess_obs(obs, self.observation_space, normalize_images=self.normalize_images) return self.features_extractor(preprocessed_obs) def _get_constructor_parameters(self) -> Dict[str, Any]: """ Get data that need to be saved in order to re-create the model when loading it from disk. :return: The dictionary to pass to the as kwargs constructor when reconstruction this model. """ return dict( observation_space=self.observation_space, action_space=self.action_space, # Passed to the constructor by child class # squash_output=self.squash_output, # features_extractor=self.features_extractor normalize_images=self.normalize_images, ) @property def device(self) -> th.device: """Infer which device this policy lives on by inspecting its parameters. If it has no parameters, the 'cpu' device is used as a fallback. :return:""" for param in self.parameters(): return param.device return get_device("cpu") def save(self, path: str) -> None: """ Save model to a given location. :param path: """ th.save({"state_dict": self.state_dict(), "data": self._get_constructor_parameters()}, path) @classmethod def load(cls, path: str, device: Union[th.device, str] = "auto") -> "BaseModel": """ Load model from path. :param path: :param device: Device on which the policy should be loaded. :return: """ device = get_device(device) saved_variables = th.load(path, map_location=device) # Create policy object model = cls(**saved_variables["data"]) # pytype: disable=not-instantiable # Load weights model.load_state_dict(saved_variables["state_dict"]) model.to(device) return model def load_from_vector(self, vector: np.ndarray) -> None: """ Load parameters from a 1D vector. :param vector: """ th.nn.utils.vector_to_parameters(th.FloatTensor(vector).to(self.device), self.parameters()) def parameters_to_vector(self) -> np.ndarray: """ Convert the parameters to a 1D vector. :return: """ return th.nn.utils.parameters_to_vector(self.parameters()).detach().cpu().numpy() class BasePolicy(BaseModel): """The base policy object. Parameters are mostly the same as `BaseModel`; additions are documented below. :param args: positional arguments passed through to `BaseModel`. :param kwargs: keyword arguments passed through to `BaseModel`. :param squash_output: For continuous actions, whether the output is squashed or not using a ``tanh()`` function. """ def __init__(self, *args, squash_output: bool = False, **kwargs): super(BasePolicy, self).__init__(*args, **kwargs) self._squash_output = squash_output @staticmethod def _dummy_schedule(progress_remaining: float) -> float: """ (float) Useful for pickling policy.""" del progress_remaining return 0.0 @property def squash_output(self) -> bool: """(bool) Getter for squash_output.""" return self._squash_output @staticmethod def init_weights(module: nn.Module, gain: float = 1) -> None: """ Orthogonal initialization (used in PPO and A2C) """ if isinstance(module, (nn.Linear, nn.Conv2d)): nn.init.orthogonal_(module.weight, gain=gain) if module.bias is not None: module.bias.data.fill_(0.0) @abstractmethod def _predict(self, observation: th.Tensor, deterministic: bool = False) -> th.Tensor: """ Get the action according to the policy for a given observation. By default provides a dummy implementation -- not all BasePolicy classes implement this, e.g. if they are a Critic in an Actor-Critic method. :param observation: :param deterministic: Whether to use stochastic or deterministic actions :return: Taken action according to the policy """ def predict( self, observation: np.ndarray, state: Optional[np.ndarray] = None, mask: Optional[np.ndarray] = None, deterministic: bool = False, ) -> Tuple[np.ndarray, Optional[np.ndarray]]: """ Get the policy action and state from an observation (and optional state). Includes sugar-coating to handle different observations (e.g. normalizing images). :param observation: the input observation :param state: The last states (can be None, used in recurrent policies) :param mask: The last masks (can be None, used in recurrent policies) :param deterministic: Whether or not to return deterministic actions. :return: the model's action and the next state (used in recurrent policies) """ # TODO (GH/1): add support for RNN policies # if state is None: # state = self.initial_state # if mask is None: # mask = [False for _ in range(self.n_envs)] if isinstance(observation, dict): observation = ObsDictWrapper.convert_dict(observation) else: observation = np.array(observation) # Handle the different cases for images # as PyTorch use channel first format observation = maybe_transpose(observation, self.observation_space) vectorized_env = is_vectorized_observation(observation, self.observation_space) observation = observation.reshape((-1,) + self.observation_space.shape) observation = th.as_tensor(observation).to(self.device) with th.no_grad(): actions = self._predict(observation, deterministic=deterministic) # Convert to numpy actions = actions.cpu().numpy() if isinstance(self.action_space, gym.spaces.Box): if self.squash_output: # Rescale to proper domain when using squashing actions = self.unscale_action(actions) else: # Actions could be on arbitrary scale, so clip the actions to avoid # out of bound error (e.g. if sampling from a Gaussian distribution) actions = np.clip(actions, self.action_space.low, self.action_space.high) if not vectorized_env: if state is not None: raise ValueError("Error: The environment must be vectorized when using recurrent policies.") actions = actions[0] return actions, state def scale_action(self, action: np.ndarray) -> np.ndarray: """ Rescale the action from [low, high] to [-1, 1] (no need for symmetric action space) :param action: Action to scale :return: Scaled action """ low, high = self.action_space.low, self.action_space.high return 2.0 * ((action - low) / (high - low)) - 1.0 def unscale_action(self, scaled_action: np.ndarray) -> np.ndarray: """ Rescale the action from [-1, 1] to [low, high] (no need for symmetric action space) :param scaled_action: Action to un-scale """ low, high = self.action_space.low, self.action_space.high return low + (0.5 * (scaled_action + 1.0) * (high - low)) class ActorCriticPolicy(BasePolicy): """ Policy class for actor-critic algorithms (has both policy and value prediction). Used by A2C, PPO and the likes. :param observation_space: Observation space :param action_space: Action space :param lr_schedule: Learning rate schedule (could be constant) :param net_arch: The specification of the policy and value networks. :param activation_fn: Activation function :param ortho_init: Whether to use or not orthogonal initialization :param use_sde: Whether to use State Dependent Exploration or not :param log_std_init: Initial value for the log standard deviation :param full_std: Whether to use (n_features x n_actions) parameters for the std instead of only (n_features,) when using gSDE :param sde_net_arch: Network architecture for extracting features when using gSDE. If None, the latent features from the policy will be used. Pass an empty list to use the states as features. :param use_expln: Use ``expln()`` function instead of ``exp()`` to ensure a positive standard deviation (cf paper). It allows to keep variance above zero and prevent it from growing too fast. In practice, ``exp()`` is usually enough. :param squash_output: Whether to squash the output using a tanh function, this allows to ensure boundaries when using gSDE. :param features_extractor_class: Features extractor to use. :param features_extractor_kwargs: Keyword arguments to pass to the features extractor. :param normalize_images: Whether to normalize images or not, dividing by 255.0 (True by default) :param optimizer_class: The optimizer to use, ``th.optim.Adam`` by default :param optimizer_kwargs: Additional keyword arguments, excluding the learning rate, to pass to the optimizer """ def __init__( self, observation_space: gym.spaces.Space, action_space: gym.spaces.Space, lr_schedule: Schedule, net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None, activation_fn: Type[nn.Module] = nn.Tanh, ortho_init: bool = True, use_sde: bool = False, log_std_init: float = 0.0, full_std: bool = True, sde_net_arch: Optional[List[int]] = None, use_expln: bool = False, squash_output: bool = False, features_extractor_class: Type[BaseFeaturesExtractor] = FlattenExtractor, features_extractor_kwargs: Optional[Dict[str, Any]] = None, normalize_images: bool = True, optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam, optimizer_kwargs: Optional[Dict[str, Any]] = None, ): if optimizer_kwargs is None: optimizer_kwargs = {} # Small values to avoid NaN in Adam optimizer if optimizer_class == th.optim.Adam: optimizer_kwargs["eps"] = 1e-5 super(ActorCriticPolicy, self).__init__( observation_space, action_space, features_extractor_class, features_extractor_kwargs, optimizer_class=optimizer_class, optimizer_kwargs=optimizer_kwargs, squash_output=squash_output, ) # Default network architecture, from stable-baselines if net_arch is None: if features_extractor_class == FlattenExtractor: net_arch = [dict(pi=[64, 64], vf=[64, 64])] else: net_arch = [] self.net_arch = net_arch self.activation_fn = activation_fn self.ortho_init = ortho_init self.features_extractor = features_extractor_class(self.observation_space, **self.features_extractor_kwargs) self.features_dim = self.features_extractor.features_dim self.normalize_images = normalize_images self.log_std_init = log_std_init dist_kwargs = None # Keyword arguments for gSDE distribution if use_sde: dist_kwargs = { "full_std": full_std, "squash_output": squash_output, "use_expln": use_expln, "learn_features": sde_net_arch is not None, } self.sde_features_extractor = None self.sde_net_arch = sde_net_arch self.use_sde = use_sde self.dist_kwargs = dist_kwargs # Action distribution self.action_dist = make_proba_distribution(action_space, use_sde=use_sde, dist_kwargs=dist_kwargs) self._build(lr_schedule) def _get_constructor_parameters(self) -> Dict[str, Any]: data = super()._get_constructor_parameters() default_none_kwargs = self.dist_kwargs or collections.defaultdict(lambda: None) data.update( dict( net_arch=self.net_arch, activation_fn=self.activation_fn, use_sde=self.use_sde, log_std_init=self.log_std_init, squash_output=default_none_kwargs["squash_output"], full_std=default_none_kwargs["full_std"], sde_net_arch=self.sde_net_arch, use_expln=default_none_kwargs["use_expln"], lr_schedule=self._dummy_schedule, # dummy lr schedule, not needed for loading policy alone ortho_init=self.ortho_init, optimizer_class=self.optimizer_class, optimizer_kwargs=self.optimizer_kwargs, features_extractor_class=self.features_extractor_class, features_extractor_kwargs=self.features_extractor_kwargs, ) ) return data def reset_noise(self, n_envs: int = 1) -> None: """ Sample new weights for the exploration matrix. :param n_envs: """ assert isinstance(self.action_dist, StateDependentNoiseDistribution), "reset_noise() is only available when using gSDE" self.action_dist.sample_weights(self.log_std, batch_size=n_envs) def _build_mlp_extractor(self) -> None: """ Create the policy and value networks. Part of the layers can be shared. """ # Note: If net_arch is None and some features extractor is used, # net_arch here is an empty list and mlp_extractor does not # really contain any layers (acts like an identity module). self.mlp_extractor = MlpExtractor( self.features_dim, net_arch=self.net_arch, activation_fn=self.activation_fn, device=self.device ) def _build(self, lr_schedule: Schedule) -> None: """ Create the networks and the optimizer. :param lr_schedule: Learning rate schedule lr_schedule(1) is the initial learning rate """ self._build_mlp_extractor() latent_dim_pi = self.mlp_extractor.latent_dim_pi # Separate features extractor for gSDE if self.sde_net_arch is not None: self.sde_features_extractor, latent_sde_dim = create_sde_features_extractor( self.features_dim, self.sde_net_arch, self.activation_fn ) if isinstance(self.action_dist, DiagGaussianDistribution): self.action_net, self.log_std = self.action_dist.proba_distribution_net( latent_dim=latent_dim_pi, log_std_init=self.log_std_init ) elif isinstance(self.action_dist, StateDependentNoiseDistribution): latent_sde_dim = latent_dim_pi if self.sde_net_arch is None else latent_sde_dim self.action_net, self.log_std = self.action_dist.proba_distribution_net( latent_dim=latent_dim_pi, latent_sde_dim=latent_sde_dim, log_std_init=self.log_std_init ) elif isinstance(self.action_dist, CategoricalDistribution): self.action_net = self.action_dist.proba_distribution_net(latent_dim=latent_dim_pi) elif isinstance(self.action_dist, MultiCategoricalDistribution): self.action_net = self.action_dist.proba_distribution_net(latent_dim=latent_dim_pi) elif isinstance(self.action_dist, BernoulliDistribution): self.action_net = self.action_dist.proba_distribution_net(latent_dim=latent_dim_pi) else: raise NotImplementedError(f"Unsupported distribution '{self.action_dist}'.") self.value_net = nn.Linear(self.mlp_extractor.latent_dim_vf, 1) # Init weights: use orthogonal initialization # with small initial weight for the output if self.ortho_init: # TODO: check for features_extractor # Values from stable-baselines. # features_extractor/mlp values are # originally from openai/baselines (default gains/init_scales). module_gains = { self.features_extractor: np.sqrt(2), self.mlp_extractor: np.sqrt(2), self.action_net: 0.01, self.value_net: 1, } for module, gain in module_gains.items(): module.apply(partial(self.init_weights, gain=gain)) # Setup optimizer with initial learning rate self.optimizer = self.optimizer_class(self.parameters(), lr=lr_schedule(1), **self.optimizer_kwargs) def forward(self, obs: th.Tensor, deterministic: bool = False) -> Tuple[th.Tensor, th.Tensor, th.Tensor]: """ Forward pass in all the networks (actor and critic) :param obs: Observation :param deterministic: Whether to sample or use deterministic actions :return: action, value and log probability of the action """ latent_pi, latent_vf, latent_sde = self._get_latent(obs) # Evaluate the values for the given observations values = self.value_net(latent_vf) distribution = self._get_action_dist_from_latent(latent_pi, latent_sde=latent_sde) actions = distribution.get_actions(deterministic=deterministic) log_prob = distribution.log_prob(actions) return actions, values, log_prob def _get_latent(self, obs: th.Tensor) -> Tuple[th.Tensor, th.Tensor, th.Tensor]: """ Get the latent code (i.e., activations of the last layer of each network) for the different networks. :param obs: Observation :return: Latent codes for the actor, the value function and for gSDE function """ # Preprocess the observation if needed features = self.extract_features(obs) latent_pi, latent_vf = self.mlp_extractor(features) # Features for sde latent_sde = latent_pi if self.sde_features_extractor is not None: latent_sde = self.sde_features_extractor(features) return latent_pi, latent_vf, latent_sde def _get_action_dist_from_latent(self, latent_pi: th.Tensor, latent_sde: Optional[th.Tensor] = None) -> Distribution: """ Retrieve action distribution given the latent codes. :param latent_pi: Latent code for the actor :param latent_sde: Latent code for the gSDE exploration function :return: Action distribution """ mean_actions = self.action_net(latent_pi) if isinstance(self.action_dist, DiagGaussianDistribution): return self.action_dist.proba_distribution(mean_actions, self.log_std) elif isinstance(self.action_dist, CategoricalDistribution): # Here mean_actions are the logits before the softmax return self.action_dist.proba_distribution(action_logits=mean_actions) elif isinstance(self.action_dist, MultiCategoricalDistribution): # Here mean_actions are the flattened logits return self.action_dist.proba_distribution(action_logits=mean_actions) elif isinstance(self.action_dist, BernoulliDistribution): # Here mean_actions are the logits (before rounding to get the binary actions) return self.action_dist.proba_distribution(action_logits=mean_actions) elif isinstance(self.action_dist, StateDependentNoiseDistribution): return self.action_dist.proba_distribution(mean_actions, self.log_std, latent_sde) else: raise ValueError("Invalid action distribution") def _predict(self, observation: th.Tensor, deterministic: bool = False) -> th.Tensor: """ Get the action according to the policy for a given observation. :param observation: :param deterministic: Whether to use stochastic or deterministic actions :return: Taken action according to the policy """ latent_pi, _, latent_sde = self._get_latent(observation) distribution = self._get_action_dist_from_latent(latent_pi, latent_sde) return distribution.get_actions(deterministic=deterministic) def evaluate_actions(self, obs: th.Tensor, actions: th.Tensor) -> Tuple[th.Tensor, th.Tensor, th.Tensor]: """ Evaluate actions according to the current policy, given the observations. :param obs: :param actions: :return: estimated value, log likelihood of taking those actions and entropy of the action distribution. """ latent_pi, latent_vf, latent_sde = self._get_latent(obs) distribution = self._get_action_dist_from_latent(latent_pi, latent_sde) log_prob = distribution.log_prob(actions) values = self.value_net(latent_vf) return values, log_prob, distribution.entropy() class ActorCriticCnnPolicy(ActorCriticPolicy): """ CNN policy class for actor-critic algorithms (has both policy and value prediction). Used by A2C, PPO and the likes. :param observation_space: Observation space :param action_space: Action space :param lr_schedule: Learning rate schedule (could be constant) :param net_arch: The specification of the policy and value networks. :param activation_fn: Activation function :param ortho_init: Whether to use or not orthogonal initialization :param use_sde: Whether to use State Dependent Exploration or not :param log_std_init: Initial value for the log standard deviation :param full_std: Whether to use (n_features x n_actions) parameters for the std instead of only (n_features,) when using gSDE :param sde_net_arch: Network architecture for extracting features when using gSDE. If None, the latent features from the policy will be used. Pass an empty list to use the states as features. :param use_expln: Use ``expln()`` function instead of ``exp()`` to ensure a positive standard deviation (cf paper). It allows to keep variance above zero and prevent it from growing too fast. In practice, ``exp()`` is usually enough. :param squash_output: Whether to squash the output using a tanh function, this allows to ensure boundaries when using gSDE. :param features_extractor_class: Features extractor to use. :param features_extractor_kwargs: Keyword arguments to pass to the features extractor. :param normalize_images: Whether to normalize images or not, dividing by 255.0 (True by default) :param optimizer_class: The optimizer to use, ``th.optim.Adam`` by default :param optimizer_kwargs: Additional keyword arguments, excluding the learning rate, to pass to the optimizer """ def __init__( self, observation_space: gym.spaces.Space, action_space: gym.spaces.Space, lr_schedule: Schedule, net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None, activation_fn: Type[nn.Module] = nn.Tanh, ortho_init: bool = True, use_sde: bool = False, log_std_init: float = 0.0, full_std: bool = True, sde_net_arch: Optional[List[int]] = None, use_expln: bool = False, squash_output: bool = False, features_extractor_class: Type[BaseFeaturesExtractor] = NatureCNN, features_extractor_kwargs: Optional[Dict[str, Any]] = None, normalize_images: bool = True, optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam, optimizer_kwargs: Optional[Dict[str, Any]] = None, ): super(ActorCriticCnnPolicy, self).__init__( observation_space, action_space, lr_schedule, net_arch, activation_fn, ortho_init, use_sde, log_std_init, full_std, sde_net_arch, use_expln, squash_output, features_extractor_class, features_extractor_kwargs, normalize_images, optimizer_class, optimizer_kwargs, ) class ContinuousCritic(BaseModel): """ Critic network(s) for DDPG/SAC/TD3. It represents the action-state value function (Q-value function). Compared to A2C/PPO critics, this one represents the Q-value and takes the continuous action as input. It is concatenated with the state and then fed to the network which outputs a single value: Q(s, a). For more recent algorithms like SAC/TD3, multiple networks are created to give different estimates. By default, it creates two critic networks used to reduce overestimation thanks to clipped Q-learning (cf TD3 paper). :param observation_space: Obervation space :param action_space: Action space :param net_arch: Network architecture :param features_extractor: Network to extract features (a CNN when using images, a nn.Flatten() layer otherwise) :param features_dim: Number of features :param activation_fn: Activation function :param normalize_images: Whether to normalize images or not, dividing by 255.0 (True by default) :param n_critics: Number of critic networks to create. :param share_features_extractor: Whether the features extractor is shared or not between the actor and the critic (this saves computation time) """ def __init__( self, observation_space: gym.spaces.Space, action_space: gym.spaces.Space, net_arch: List[int], features_extractor: nn.Module, features_dim: int, activation_fn: Type[nn.Module] = nn.ReLU, normalize_images: bool = True, n_critics: int = 2, share_features_extractor: bool = True, ): super().__init__( observation_space, action_space, features_extractor=features_extractor, normalize_images=normalize_images, ) action_dim = get_action_dim(self.action_space) self.share_features_extractor = share_features_extractor self.n_critics = n_critics self.q_networks = [] for idx in range(n_critics): q_net = create_mlp(features_dim + action_dim, 1, net_arch, activation_fn) q_net = nn.Sequential(*q_net) self.add_module(f"qf{idx}", q_net) self.q_networks.append(q_net) def forward(self, obs: th.Tensor, actions: th.Tensor) -> Tuple[th.Tensor, ...]: # Learn the features extractor using the policy loss only # when the features_extractor is shared with the actor with th.set_grad_enabled(not self.share_features_extractor): features = self.extract_features(obs) qvalue_input = th.cat([features, actions], dim=1) return tuple(q_net(qvalue_input) for q_net in self.q_networks) def q1_forward(self, obs: th.Tensor, actions: th.Tensor) -> th.Tensor: """ Only predict the Q-value using the first network. This allows to reduce computation when all the estimates are not needed (e.g. when updating the policy in TD3). """ with th.no_grad(): features = self.extract_features(obs) return self.q_networks[0](th.cat([features, actions], dim=1)) def create_sde_features_extractor( features_dim: int, sde_net_arch: List[int], activation_fn: Type[nn.Module] ) -> Tuple[nn.Sequential, int]: """ Create the neural network that will be used to extract features for the gSDE exploration function. :param features_dim: :param sde_net_arch: :param activation_fn: :return: """ # Special case: when using states as features (i.e. sde_net_arch is an empty list) # don't use any activation function sde_activation = activation_fn if len(sde_net_arch) > 0 else None latent_sde_net = create_mlp(features_dim, -1, sde_net_arch, activation_fn=sde_activation, squash_output=False) latent_sde_dim = sde_net_arch[-1] if len(sde_net_arch) > 0 else features_dim sde_features_extractor = nn.Sequential(*latent_sde_net) return sde_features_extractor, latent_sde_dim _policy_registry = dict() # type: Dict[Type[BasePolicy], Dict[str, Type[BasePolicy]]] def get_policy_from_name(base_policy_type: Type[BasePolicy], name: str) -> Type[BasePolicy]: """ Returns the registered policy from the base type and name. See `register_policy` for registering policies and explanation. :param base_policy_type: the base policy class :param name: the policy name :return: the policy """ if base_policy_type not in _policy_registry: raise KeyError(f"Error: the policy type {base_policy_type} is not registered!") if name not in _policy_registry[base_policy_type]: raise KeyError( f"Error: unknown policy type {name}," f"the only registed policy type are: {list(_policy_registry[base_policy_type].keys())}!" ) return _policy_registry[base_policy_type][name] def register_policy(name: str, policy: Type[BasePolicy]) -> None: """ Register a policy, so it can be called using its name. e.g. SAC('MlpPolicy', ...) instead of SAC(MlpPolicy, ...). The goal here is to standardize policy naming, e.g. all algorithms can call upon "MlpPolicy" or "CnnPolicy", and they receive respective policies that work for them. Consider following: OnlinePolicy -- OnlineMlpPolicy ("MlpPolicy") -- OnlineCnnPolicy ("CnnPolicy") OfflinePolicy -- OfflineMlpPolicy ("MlpPolicy") -- OfflineCnnPolicy ("CnnPolicy") Two policies have name "MlpPolicy" and two have "CnnPolicy". In `get_policy_from_name`, the parent class (e.g. OnlinePolicy) is given and used to select and return the correct policy. :param name: the policy name :param policy: the policy class """ sub_class = None for cls in BasePolicy.__subclasses__(): if issubclass(policy, cls): sub_class = cls break if sub_class is None: raise ValueError(f"Error: the policy {policy} is not of any known subclasses of BasePolicy!") if sub_class not in _policy_registry: _policy_registry[sub_class] = {} if name in _policy_registry[sub_class]: # Check if the registered policy is same # we try to register. If not so, # do not override and complain. if _policy_registry[sub_class][name] != policy: raise ValueError(f"Error: the name {name} is already registered for a different policy, will not override.") _policy_registry[sub_class][name] = policy
[]
2024-01-10
binary-husky/hmp2g
ALGORITHM~stable_baselines3~sac~sac_original.py
from typing import Any, Dict, List, Optional, Tuple, Type, Union import gym import numpy as np import torch as th from torch.nn import functional as F from stable_baselines3.common import logger from stable_baselines3.common.noise import ActionNoise from stable_baselines3.common.off_policy_algorithm import OffPolicyAlgorithm from stable_baselines3.common.type_aliases import GymEnv, MaybeCallback, Schedule from stable_baselines3.common.utils import polyak_update from stable_baselines3.sac.policies import SACPolicy class SAC(OffPolicyAlgorithm): """ Soft Actor-Critic (SAC) Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor, This implementation borrows code from original implementation (https://github.com/haarnoja/sac) from OpenAI Spinning Up (https://github.com/openai/spinningup), from the softlearning repo (https://github.com/rail-berkeley/softlearning/) and from Stable Baselines (https://github.com/hill-a/stable-baselines) Paper: https://arxiv.org/abs/1801.01290 Introduction to SAC: https://spinningup.openai.com/en/latest/algorithms/sac.html Note: we use double q target and not value target as discussed in https://github.com/hill-a/stable-baselines/issues/270 :param policy: The policy model to use (MlpPolicy, CnnPolicy, ...) :param env: The environment to learn from (if registered in Gym, can be str) :param learning_rate: learning rate for adam optimizer, the same learning rate will be used for all networks (Q-Values, Actor and Value function) it can be a function of the current progress remaining (from 1 to 0) :param buffer_size: size of the replay buffer :param learning_starts: how many steps of the model to collect transitions for before learning starts :param batch_size: Minibatch size for each gradient update :param tau: the soft update coefficient ("Polyak update", between 0 and 1) :param gamma: the discount factor :param train_freq: Update the model every ``train_freq`` steps. Alternatively pass a tuple of frequency and unit like ``(5, "step")`` or ``(2, "episode")``. :param gradient_steps: How many gradient steps to do after each rollout (see ``train_freq``) Set to ``-1`` means to do as many gradient steps as steps done in the environment during the rollout. :param action_noise: the action noise type (None by default), this can help for hard exploration problem. Cf common.noise for the different action noise type. :param optimize_memory_usage: Enable a memory efficient variant of the replay buffer at a cost of more complexity. See https://github.com/DLR-RM/stable-baselines3/issues/37#issuecomment-637501195 :param ent_coef: Entropy regularization coefficient. (Equivalent to inverse of reward scale in the original SAC paper.) Controlling exploration/exploitation trade-off. Set it to 'auto' to learn it automatically (and 'auto_0.1' for using 0.1 as initial value) :param target_update_interval: update the target network every ``target_network_update_freq`` gradient steps. :param target_entropy: target entropy when learning ``ent_coef`` (``ent_coef = 'auto'``) :param use_sde: Whether to use generalized State Dependent Exploration (gSDE) instead of action noise exploration (default: False) :param sde_sample_freq: Sample a new noise matrix every n steps when using gSDE Default: -1 (only sample at the beginning of the rollout) :param use_sde_at_warmup: Whether to use gSDE instead of uniform sampling during the warm up phase (before learning starts) :param create_eval_env: Whether to create a second environment that will be used for evaluating the agent periodically. (Only available when passing string for the environment) :param policy_kwargs: additional arguments to be passed to the policy on creation :param verbose: the verbosity level: 0 no output, 1 info, 2 debug :param seed: Seed for the pseudo random generators :param device: Device (cpu, cuda, ...) on which the code should be run. Setting it to auto, the code will be run on the GPU if possible. :param _init_setup_model: Whether or not to build the network at the creation of the instance """ def __init__( self, policy: Union[str, Type[SACPolicy]], env: Union[GymEnv, str], learning_rate: Union[float, Schedule] = 3e-4, buffer_size: int = 1000000, learning_starts: int = 100, batch_size: int = 256, tau: float = 0.005, gamma: float = 0.99, train_freq: Union[int, Tuple[int, str]] = 1, gradient_steps: int = 1, action_noise: Optional[ActionNoise] = None, optimize_memory_usage: bool = False, ent_coef: Union[str, float] = "auto", target_update_interval: int = 1, target_entropy: Union[str, float] = "auto", use_sde: bool = False, sde_sample_freq: int = -1, use_sde_at_warmup: bool = False, tensorboard_log: Optional[str] = None, create_eval_env: bool = False, policy_kwargs: Dict[str, Any] = None, verbose: int = 0, seed: Optional[int] = None, device: Union[th.device, str] = "auto", _init_setup_model: bool = True, ): super(SAC, self).__init__( policy, env, SACPolicy, learning_rate, buffer_size, learning_starts, batch_size, tau, gamma, train_freq, gradient_steps, action_noise, policy_kwargs=policy_kwargs, tensorboard_log=tensorboard_log, verbose=verbose, device=device, create_eval_env=create_eval_env, seed=seed, use_sde=use_sde, sde_sample_freq=sde_sample_freq, use_sde_at_warmup=use_sde_at_warmup, optimize_memory_usage=optimize_memory_usage, supported_action_spaces=(gym.spaces.Box), ) self.target_entropy = target_entropy self.log_ent_coef = None # type: Optional[th.Tensor] # Entropy coefficient / Entropy temperature # Inverse of the reward scale self.ent_coef = ent_coef self.target_update_interval = target_update_interval self.ent_coef_optimizer = None if _init_setup_model: self._setup_model() def _setup_model(self) -> None: super(SAC, self)._setup_model() self._create_aliases() # Target entropy is used when learning the entropy coefficient if self.target_entropy == "auto": # automatically set target entropy if needed self.target_entropy = -np.prod(self.env.action_space.shape).astype(np.float32) else: # Force conversion # this will also throw an error for unexpected string self.target_entropy = float(self.target_entropy) # The entropy coefficient or entropy can be learned automatically # see Automating Entropy Adjustment for Maximum Entropy RL section # of https://arxiv.org/abs/1812.05905 if isinstance(self.ent_coef, str) and self.ent_coef.startswith("auto"): # Default initial value of ent_coef when learned init_value = 1.0 if "_" in self.ent_coef: init_value = float(self.ent_coef.split("_")[1]) assert init_value > 0.0, "The initial value of ent_coef must be greater than 0" # Note: we optimize the log of the entropy coeff which is slightly different from the paper # as discussed in https://github.com/rail-berkeley/softlearning/issues/37 self.log_ent_coef = th.log(th.ones(1, device=self.device) * init_value).requires_grad_(True) self.ent_coef_optimizer = th.optim.Adam([self.log_ent_coef], lr=self.lr_schedule(1)) else: # Force conversion to float # this will throw an error if a malformed string (different from 'auto') # is passed self.ent_coef_tensor = th.tensor(float(self.ent_coef)).to(self.device) def _create_aliases(self) -> None: self.actor = self.policy.actor self.critic = self.policy.critic self.critic_target = self.policy.critic_target def train(self, gradient_steps: int, batch_size: int = 64) -> None: # Update optimizers learning rate optimizers = [self.actor.optimizer, self.critic.optimizer] if self.ent_coef_optimizer is not None: optimizers += [self.ent_coef_optimizer] # Update learning rate according to lr schedule self._update_learning_rate(optimizers) ent_coef_losses, ent_coefs = [], [] actor_losses, critic_losses = [], [] for gradient_step in range(gradient_steps): # Sample replay buffer replay_data = self.replay_buffer.sample(batch_size, env=self._vec_normalize_env) # We need to sample because `log_std` may have changed between two gradient steps if self.use_sde: self.actor.reset_noise() # Action by the current actor for the sampled state actions_pi, log_prob = self.actor.action_log_prob(replay_data.observations) log_prob = log_prob.reshape(-1, 1) ent_coef_loss = None if self.ent_coef_optimizer is not None: # Important: detach the variable from the graph # so we don't change it with other losses # see https://github.com/rail-berkeley/softlearning/issues/60 ent_coef = th.exp(self.log_ent_coef.detach()) ent_coef_loss = -(self.log_ent_coef * (log_prob + self.target_entropy).detach()).mean() ent_coef_losses.append(ent_coef_loss.item()) else: ent_coef = self.ent_coef_tensor ent_coefs.append(ent_coef.item()) # Optimize entropy coefficient, also called # entropy temperature or alpha in the paper if ent_coef_loss is not None: self.ent_coef_optimizer.zero_grad() ent_coef_loss.backward() self.ent_coef_optimizer.step() with th.no_grad(): # Select action according to policy next_actions, next_log_prob = self.actor.action_log_prob(replay_data.next_observations) # Compute the next Q values: min over all critics targets next_q_values = th.cat(self.critic_target(replay_data.next_observations, next_actions), dim=1) next_q_values, _ = th.min(next_q_values, dim=1, keepdim=True) # add entropy term next_q_values = next_q_values - ent_coef * next_log_prob.reshape(-1, 1) # td error + entropy term target_q_values = replay_data.rewards + (1 - replay_data.dones) * self.gamma * next_q_values # Get current Q-values estimates for each critic network # using action from the replay buffer current_q_values = self.critic(replay_data.observations, replay_data.actions) # Compute critic loss critic_loss = 0.5 * sum([F.mse_loss(current_q, target_q_values) for current_q in current_q_values]) critic_losses.append(critic_loss.item()) # Optimize the critic self.critic.optimizer.zero_grad() critic_loss.backward() self.critic.optimizer.step() # Compute actor loss # Alternative: actor_loss = th.mean(log_prob - qf1_pi) # Mean over all critic networks q_values_pi = th.cat(self.critic.forward(replay_data.observations, actions_pi), dim=1) min_qf_pi, _ = th.min(q_values_pi, dim=1, keepdim=True) actor_loss = (ent_coef * log_prob - min_qf_pi).mean() actor_losses.append(actor_loss.item()) # Optimize the actor self.actor.optimizer.zero_grad() actor_loss.backward() self.actor.optimizer.step() # Update target networks if gradient_step % self.target_update_interval == 0: polyak_update(self.critic.parameters(), self.critic_target.parameters(), self.tau) self._n_updates += gradient_steps logger.record("train/n_updates", self._n_updates, exclude="tensorboard") logger.record("train/ent_coef", np.mean(ent_coefs)) logger.record("train/actor_loss", np.mean(actor_losses)) logger.record("train/critic_loss", np.mean(critic_losses)) if len(ent_coef_losses) > 0: logger.record("train/ent_coef_loss", np.mean(ent_coef_losses)) def learn( self, total_timesteps: int, callback: MaybeCallback = None, log_interval: int = 4, eval_env: Optional[GymEnv] = None, eval_freq: int = -1, n_eval_episodes: int = 5, tb_log_name: str = "SAC", eval_log_path: Optional[str] = None, reset_num_timesteps: bool = True, ) -> OffPolicyAlgorithm: return super(SAC, self).learn( total_timesteps=total_timesteps, callback=callback, log_interval=log_interval, eval_env=eval_env, eval_freq=eval_freq, n_eval_episodes=n_eval_episodes, tb_log_name=tb_log_name, eval_log_path=eval_log_path, reset_num_timesteps=reset_num_timesteps, ) def _excluded_save_params(self) -> List[str]: return super(SAC, self)._excluded_save_params() + ["actor", "critic", "critic_target"] def _get_torch_save_params(self) -> Tuple[List[str], List[str]]: state_dicts = ["policy", "actor.optimizer", "critic.optimizer"] saved_pytorch_variables = ["log_ent_coef"] if self.ent_coef_optimizer is not None: state_dicts.append("ent_coef_optimizer") else: saved_pytorch_variables.append("ent_coef_tensor") return state_dicts, saved_pytorch_variables
[]
2024-01-10
AkshayChan/Vision-and-Robotics
reacher3D~rendering.py
""" 2D rendering framework Adapted by Joshua Smith from OpenAI Gym Important note most of the drawing functions have not been made into 3D only the targets and spheres are 3D. """ from __future__ import division import os import six import sys from gym.utils import reraise from gym import error RAD2DEG = 57.29577951308232 GOLDENRATIO = (1+5**0.5)/2 import ctypes try: import pyglet except ImportError as e: reraise(suffix="HINT: you can install pyglet directly via 'pip install pyglet'. But if you really just want to install all Gym dependencies and not have to think about it, 'pip install -e .[all]' or 'pip install gym[all]' will do it.") try: from pyglet.gl import * except ImportError as e: reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") import math import numpy as np from pyquaternion import Quaternion def get_display(spec): """Convert a display specification (such as :0) into an actual Display object. Pyglet only supports multiple Displays on Linux. """ if spec is None: return None elif isinstance(spec, six.string_types): return pyglet.canvas.Display(spec) else: raise error.Error('Invalid display specification: {}. (Must be a string like :0 or None.)'.format(spec)) class Viewer(object): def __init__(self, width, height, display=None): display = get_display(display) self.width = width self.height = height self.window_xy = pyglet.window.Window(caption='xy-plane',width=width, height=height, display=display) self.window_xy.on_close = self.window_closed_by_user self.window_xz = pyglet.window.Window(caption='xz-plane',width=width, height=height, display=display) self.window_xz.on_close = self.window_closed_by_user self.geoms = [] self.onetime_geoms = [] self.transform_xy = Transform() self.transform_xz = Transform() self.perspective_transform_on = False glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) def close(self): self.window_xy.close() def window_closed_by_user(self): self.close() def set_bounds(self, left, right, bottom, top): assert right > left and top > bottom scalex = self.width/(right-left) scaley = self.height/(top-bottom) self.transform_xy = Transform( translation=(-left*scalex, -bottom*scaley,-bottom*scaley), scale=(scalex, scaley, scaley)) self.transform_xz = Transform( translation=(-left*scalex, -bottom*scaley,-bottom*scaley), scale=(scalex, scaley, scaley)) def add_geom(self, geom): self.geoms.append(geom) def add_onetime(self, geom): self.onetime_geoms.append(geom) def render(self, return_rgb_array=False): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClearColor(1,1,1,1) self.window_xy.switch_to() self.window_xy.clear() self.window_xy.dispatch_events() glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.perspective_transform_on: glOrtho(-400,400,-400,400,800,1600) resolution = (800/8.4) #This is an example of a calibrated camera in opengl, it has the focal length of 7.35m projection = (GLfloat *16)(7.35*resolution,0,0,0 ,0,7.35*resolution,0,0, 0,0,8.4*resolution+16.8*resolution,-1, 0,0,8.4*resolution*16.8*resolution,0) glMultMatrixf(projection) #X axis offset glTranslatef((2.4*800)/8.4,0,0) else: glOrtho(-self.width/2,self.width/2,-self.height/2,self.height/2,800,1600) self.transform_xy.enable() glPushMatrix() glMatrixMode(GL_MODELVIEW) glTranslatef(-4.2,-4.2,-16.8) for geom in self.geoms: geom.render() for geom in self.onetime_geoms: geom.render() glPopMatrix() self.transform_xy.disable() arrxy = None if return_rgb_array: buffer = pyglet.image.get_buffer_manager().get_color_buffer() image_data = buffer.get_image_data() arrxy = np.fromstring(image_data.data, dtype=np.uint8, sep='') # In https://github.com/openai/gym-http-api/issues/2, we # discovered that someone using Xmonad on Arch was having # a window of size 598 x 398, though a 600 x 400 window # was requested. (Guess Xmonad was preserving a pixel for # the boundary.) So we use the buffer height/width rather # than the requested one. arrxy = arrxy.reshape(buffer.height, buffer.width, 4) arrxy = arrxy[::-1,:,0:3] self.window_xy.flip() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClearColor(1,1,1,1) self.window_xz.switch_to() self.window_xz.clear() self.window_xz.dispatch_events() glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.perspective_transform_on: glOrtho(-400,400,-400,400,800,1600) projection = (GLfloat *16)(700,0,0,0 ,0,700,0,0, 0,0,800+1600,-1, 0,0,800*1600,0) glMultMatrixf(projection) glTranslatef(-(2.4*800)/8.4,0,0) else: glOrtho(-self.width/2,self.width/2,-self.height/2,self.height/2,800,1600) #glOrtho(0,self.width,0,self.height,0,-5000) self.transform_xz.enable() glPushMatrix() glMatrixMode(GL_MODELVIEW) glTranslatef(-4.2,-4.2,-16.8) if not self.perspective_transform_on: #rotate onto the xz plane glRotatef(-90, 1, 0,0) for geom in self.geoms: geom.render() for geom in self.onetime_geoms: geom.render() glPopMatrix() self.transform_xz.disable() arrxz = None if return_rgb_array: buffer = pyglet.image.get_buffer_manager().get_color_buffer() image_data = buffer.get_image_data() arrxz = np.fromstring(image_data.data, dtype=np.uint8, sep='') # In https://github.com/openai/gym-http-api/issues/2, we # discovered that someone using Xmonad on Arch was having # a window of size 598 x 398, though a 600 x 400 window # was requested. (Guess Xmonad was preserving a pixel for # the boundary.) So we use the buffer height/width rather # than the requested one. arrxz = arrxz.reshape(buffer.height, buffer.width, 4) arrxz = arrxz[::-1,:,0:3] self.window_xz.flip() self.onetime_geoms = [] return (arrxy,arrxz) # Convenience def draw_sphere(self, radius=10, res=2, filled=True, **attrs): geom = make_sphere(radius=radius, res=res, filled=filled) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_polygon(self, v, filled=True, **attrs): geom = make_polygon(v=v, filled=filled) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_polyline(self, v, **attrs): geom = make_polyline(v=v) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def draw_line(self, start, end, **attrs): geom = Line(start, end) _add_attrs(geom, attrs) self.add_onetime(geom) return geom def get_array(self): self.window_xy.flip() image_data = pyglet.image.get_buffer_manager().get_color_buffer().get_image_data() self.window_xy.flip() arr = np.fromstring(image_data.data, dtype=np.uint8, sep='') arr = arr.reshape(self.height, self.width, 4) return arr[::-1,:,0:3] def _add_attrs(geom, attrs): if "color" in attrs: geom.set_color(*attrs["color"]) if "linewidth" in attrs: geom.set_linewidth(attrs["linewidth"]) class Geom(object): def __init__(self): self._color=Color((1.0, 0, 0, 1.0)) self.attrs = [self._color] def render(self): for attr in reversed(self.attrs): attr.enable() self.render1() for attr in self.attrs: attr.disable() def render1(self): raise NotImplementedError def add_attr(self, attr): self.attrs.append(attr) return def set_color(self, r, g, b): self._color.vec4 = (r, g, b, 1) class Attr(object): def enable(self): raise NotImplementedError def disable(self): pass class Transform(Attr): def __init__(self, translation=(0.0, 0.0, 0.0), rotation=Quaternion(), scale=(1,1,1)): self.set_translation(*translation) self.set_rotation(rotation) self.set_scale(*scale) def enable(self): glMatrixMode(GL_MODELVIEW) glPushMatrix() glTranslatef(self.translation[0], self.translation[1], self.translation[2]) # translate to GL loc ppint glRotatef(RAD2DEG * self.rotation.angle,self.rotation.axis[0],self.rotation.axis[1],self.rotation.axis[2]) glScalef(self.scale[0], self.scale[1], self.scale[2]) def disable(self): glPopMatrix() def set_translation(self, newx, newy, newz): self.translation = (float(newx), float(newy), float(newz)) def set_rotation(self, quat): self.rotation = quat def set_scale(self, newx, newy, newz): self.scale = (float(newx), float(newy), float(newz)) class Color(Attr): def __init__(self, vec4): self.vec4 = vec4 def enable(self): glColor4f(*self.vec4) class LineStyle(Attr): def __init__(self, style): self.style = style def enable(self): glEnable(GL_LINE_STIPPLE) glLineStipple(1, self.style) def disable(self): glDisable(GL_LINE_STIPPLE) class LineWidth(Attr): def __init__(self, stroke): self.stroke = stroke def enable(self): glLineWidth(self.stroke) class Point(Geom): def __init__(self): Geom.__init__(self) def render1(self): glBegin(GL_POINTS) # draw point glVertex3f(0.0, 0.0, 0.0) glEnd() class FilledPolygon(Geom): def __init__(self, v): Geom.__init__(self) self.v = v def render1(self): if len(self.v) == 4 : glBegin(GL_QUADS) elif len(self.v) > 4 : glBegin(GL_POLYGON) else: glBegin(GL_TRIANGLES) for p in self.v: glVertex3f(p[0], p[1], p[2]) # draw each vertex glEnd() #This methods allow you to specify vertices and faces to draw class FilledPolygonJosh(Geom): def __init__(self, v,f, draw_type): Geom.__init__(self) self.v = v self.f = f self.type = draw_type def render1(self): glBegin(self.type) for f in self.f: for f1 in f: glVertex3f(self.v[f1][0], self.v[f1][1], self.v[f1][2]) # draw each vertex glEnd() #Find the mid point between vertices def mid_point(points, v1, v2): a = (points[v1]+points[v2])/2 if not any((a==x).all() for x in points): points.append(a) return next((i for i, x in enumerate(points) if np.all(x==a)),-1) #Subdivide triangles to make more faces which causes higher resolution in 3D def subdivide_triangles((points,faces), face): a = mid_point(points,face[0],face[1]) b = mid_point(points,face[1],face[2]) c = mid_point(points,face[2],face[0]) faces.append((face[0],a,c)) faces.append((face[1],b,a)) faces.append((face[2],c,b)) faces.append((a,b,c)) #Makes an icosahedron sphere. Any res over around 3 or 4 gets incredibly slow to generate def make_sphere(radius=10, res=2, filled=True): points = [] phi = (1+math.sqrt(5))/2 points.append(np.array([-1, phi, 0])) points.append(np.array([1, phi, 0])) points.append(np.array([-1, -phi, 0])) points.append(np.array([1, -phi, 0])) points.append(np.array([0, -1, phi])) points.append(np.array([0, 1, phi])) points.append(np.array([0, -1, -phi])) points.append(np.array([0, 1, -phi])) points.append(np.array([phi, 0, -1])) points.append(np.array([phi, 0, 1])) points.append(np.array([-phi, 0, -1])) points.append(np.array([-phi, 0, 1])) points2 = [] for p in points: points2.append(p*(radius/(2*math.sin(2*math.pi/5)))) points = points2 pList=[] pList.append((0,11,5)) pList.append((0,5,1)) pList.append((0,1,7)) pList.append((0,7,10)) pList.append((0,10,11)) pList.append((1,5,9)) pList.append((5,11,4)) pList.append((11,10,2)) pList.append((10,7,6)) pList.append((7,1,8)) pList.append((3,9,4)) pList.append((3,4,2)) pList.append((3,2,6)) pList.append((3,6,8)) pList.append((3,8,9)) pList.append((4,9,5)) pList.append((2,4,11)) pList.append((6,2,10)) pList.append((8,6,7)) pList.append((9,8,1)) faces=[] for _ in range(0,res): faceTmp = [] for p_item in pList: subdivide_triangles((points,faceTmp),p_item) pList = faceTmp faces = pList points2=[] for point in points: m = np.linalg.norm(point) points2.append(point*(radius/m)) points = points2 return FilledPolygonJosh(points,faces,GL_TRIANGLES) #Creates a start shape with a set number of lines def make_valid_target(radius=10, spikes=2, filled=True): points = [] faces = [] x = math.sqrt((math.pow(1.0,2)/3.0)) points.append(np.array([(-1),(-.1),(-0.1)])) points.append(np.array([(-1),(-.1),(-0.1)])) points.append(np.array([(1),(-.1),(0.1)])) points.append(np.array([(1),(-.1),(-0.1)])) points.append(np.array([(-1),(.1),(0.1)])) points.append(np.array([(1),(.1),(0.1)])) points.append(np.array([(1),(.1),(-0.1)])) points.append(np.array([(-1),(.1),(-0.1)])) #XY plane rotations for i in range(1,spikes+1): angle = ((2*math.pi)/spikes)*i rotz = np.matrix([[np.cos(angle),-np.sin(angle),0],[np.sin(angle),np.cos(angle),0],[0,0,1]]) rotx = np.matrix([[1,0,0],[0,np.cos(angle),-np.sin(angle)],[0,np.sin(angle),np.cos(angle)]]) roty = np.matrix([[np.cos(angle),0,-np.sin(angle)],[0,1,0],[np.sin(angle),0,np.cos(angle)]]) rot = rotz t = (rot*np.matrix(points[0]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[1]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[2]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[3]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[4]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[5]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[6]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[7]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) #XZ plane rotations for i in range(1,spikes+1): angle = ((2*math.pi)/spikes)*i rotz = np.matrix([[np.cos(angle),-np.sin(angle),0],[np.sin(angle),np.cos(angle),0],[0,0,1]]) rotx = np.matrix([[1,0,0],[0,np.cos(angle),-np.sin(angle)],[0,np.sin(angle),np.cos(angle)]]) roty = np.matrix([[np.cos(angle),0,-np.sin(angle)],[0,1,0],[np.sin(angle),0,np.cos(angle)]]) rot = roty t = (rot*np.matrix(points[0]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[1]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[2]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[3]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[4]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[5]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[6]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) t = (rot*np.matrix(points[7]).T) points.append(np.array([t[0,0],t[1,0],t[2,0]])) points2=[] for point in points: m = np.linalg.norm(point) points2.append(point*(radius/m)) points = points2 faces.append((0,3,1)) faces.append((3,2,1)) faces.append((1,2,5)) faces.append((1,5,4)) faces.append((2,3,6)) faces.append((2,6,5)) faces.append((0,1,7)) faces.append((0,4,7)) faces.append((4,5,6)) faces.append((4,5,7)) faces.append((0,6,3)) faces.append((0,7,6)) for i in range(1,spikes*2+1): faces.append((0+8*i,3+8*i,1+8*i)) faces.append((3+8*i,2+8*i,1+8*i)) faces.append((1+8*i,2+8*i,5+8*i)) faces.append((1+8*i,5+8*i,4+8*i)) faces.append((2+8*i,3+8*i,6+8*i)) faces.append((2+8*i,6+8*i,5+8*i)) faces.append((0+8*i,1+8*i,7+8*i)) faces.append((0+8*i,4+8*i,7+8*i)) faces.append((4+8*i,5+8*i,6+8*i)) faces.append((4+8*i,5+8*i,7+8*i)) faces.append((0+8*i,6+8*i,3+8*i)) faces.append((0+8*i,7+8*i,6+8*i)) return FilledPolygonJosh(points,faces,GL_TRIANGLES) #Makes a sphere combined with a cube for the invalid target to try and give some variation def make_invalid_target(radius=10, res=2,ss=0.4, filled=True): points = [] phi = (1+math.sqrt(5))/2 points.append(np.array([-1, phi, 0])) points.append(np.array([1, phi, 0])) points.append(np.array([-1, -phi, 0])) points.append(np.array([1, -phi, 0])) points.append(np.array([0, -1, phi])) points.append(np.array([0, 1, phi])) points.append(np.array([0, -1, -phi])) points.append(np.array([0, 1, -phi])) points.append(np.array([phi, 0, -1])) points.append(np.array([phi, 0, 1])) points.append(np.array([-phi, 0, -1])) points.append(np.array([-phi, 0, 1])) points2 = [] for p in points: points2.append(p*(radius/(2*math.sin(2*math.pi/5)))) points = points2 pList=[] pList.append((0,11,5)) pList.append((0,5,1)) pList.append((0,1,7)) pList.append((0,7,10)) pList.append((0,10,11)) pList.append((1,5,9)) pList.append((5,11,4)) pList.append((11,10,2)) pList.append((10,7,6)) pList.append((7,1,8)) pList.append((3,9,4)) pList.append((3,4,2)) pList.append((3,2,6)) pList.append((3,6,8)) pList.append((3,8,9)) pList.append((4,9,5)) pList.append((2,4,11)) pList.append((6,2,10)) pList.append((8,6,7)) pList.append((9,8,1)) faces=[] for _ in range(0,res): faceTmp = [] for p_item in pList: subdivide_triangles((points,faceTmp),p_item) pList = faceTmp faces = pList points2=[] for point in points: m = np.linalg.norm(point) points2.append(point*(radius/m)) points = points2 points3=[] size = len(points) r = math.sqrt(math.pow(radius,2)/3.0)#radius-(radius*0.19)#math.sqrt(2*math.pow(radius,2)) points3.append(np.array([(-1*r),(-1*r),(-1*r)])) points3.append(np.array([(-1*r),(-1*r),(1*r)])) points3.append(np.array([(1*r),(-1*r),(1*r)])) points3.append(np.array([(1*r),(-1*r),(-1*r)])) points3.append(np.array([(-1*r),(1*r),(1*r)])) points3.append(np.array([(1*r),(1*r),(1*r)])) points3.append(np.array([(1*r),(1*r),(-1*r)])) points3.append(np.array([(-1*r),(1*r),(-1*r)])) points2=[] for point in points3: m = np.linalg.norm(point) points.append(point*((radius+ss*radius)/m)) faces.append((size,size+3,size+1)) faces.append((size+3,size+2,size+1)) faces.append((size+1,size+2,size+5)) faces.append((size+1,size+5,size+4)) faces.append((size+2,size+3,size+6)) faces.append((size+2,size+6,size+5)) faces.append((size+0,size+1,size+7)) faces.append((size+0,size+4,size+7)) faces.append((size+4,size+5,size+6)) faces.append((size+4,size+5,size+7)) faces.append((size+0,size+6,size+3)) faces.append((size+0,size+7,size+6)) return FilledPolygonJosh(points,faces,GL_TRIANGLES) def make_polygon(v, filled=True): if filled: return FilledPolygon(v) else: return PolyLine(v, True) def make_polyline(v): return PolyLine(v, False) def make_capsule(length, width): l, r, t, b = 0, length, width/2, -width/2 box = make_polygon([(l,b), (l,t), (r,t), (r,b)]) circ0 = make_circle(width/2) circ1 = make_circle(width/2) circ1.add_attr(Transform(translation=(length, 0))) geom = Compound([box, circ0, circ1]) return geom def make_cuboid(length, width): l, r, t, b = 0, length, width/2, -width/2 points = [] points.append(np.array([0,width/2,-width/2])) points.append(np.array([0,width/2,width/2])) points.append(np.array([0,-width/2,width/2])) points.append(np.array([0,-width/2,-width/2])) points.append(np.array([length,width/2,-width/2])) points.append(np.array([length,width/2,width/2])) points.append(np.array([length,-width/2,width/2])) points.append(np.array([length,-width/2,-width/2])) faces = [] faces.append((0,1,2,3)) faces.append((0,4,5,1)) faces.append((1,5,6,2)) faces.append((2,6,7,3)) faces.append((4,0,3,7)) faces.append((5,4,7,6)) return FilledPolygonJosh(points,faces,GL_QUADS) class Compound(Geom): def __init__(self, gs): Geom.__init__(self) self.gs = gs for g in self.gs: g.attrs = [a for a in g.attrs if not isinstance(a, Color)] def render1(self): for g in self.gs: g.render() class PolyLine(Geom): def __init__(self, v, close): Geom.__init__(self) self.v = v self.close = close self.linewidth = LineWidth(1) self.add_attr(self.linewidth) def render1(self): glBegin(GL_LINE_LOOP if self.close else GL_LINE_STRIP) for p in self.v: glVertex3f(p[0], p[1],0) # draw each vertex glEnd() def set_linewidth(self, x): self.linewidth.stroke = x class Line(Geom): def __init__(self, start=(0.0, 0.0), end=(0.0, 0.0)): Geom.__init__(self) self.start = start self.end = end self.linewidth = LineWidth(1) self.add_attr(self.linewidth) def render1(self): glBegin(GL_LINES) glVertex2f(*self.start) glVertex2f(*self.end) glEnd() class Image(Geom): def __init__(self, fname, width, height): Geom.__init__(self) self.width = width self.height = height img = pyglet.image.load(fname) self.img = img self.flip = False def render1(self): self.img.blit(-self.width/2, -self.height/2, width=self.width, height=self.height) # ================================================================ class SimpleImageViewer(object): def __init__(self, display=None): self.window = None self.isopen = False self.display = display def imshow(self, arr): if self.window is None: height, width, channels = arr.shape self.window = pyglet.window.Window(width=width, height=height, display=self.display) self.width = width self.height = height self.isopen = True assert arr.shape == (self.height, self.width, 3), "You passed in an image with the wrong number shape" image = pyglet.image.ImageData(self.width, self.height, 'RGB', arr.tobytes(), pitch=self.width * -3) self.window.clear() self.window.switch_to() self.window.dispatch_events() image.blit(0,0) self.window.flip() def close(self): if self.isopen: self.window.close() self.isopen = False def __del__(self): self.close()
[]
2024-01-10
zdenis23/rulm
self_instruct~src~benchmarks~eval_zs_rsg.py
from typing import Tuple, Callable import re import copy from pathlib import Path from tqdm import tqdm import fire from datasets import load_dataset from nltk import edit_distance from sklearn.metrics import accuracy_score from sklearn.metrics import matthews_corrcoef from src.util.io import write_jsonl from src.util.chat import Conversation from src.util.dl import gen_batch from src.util.load import load_saiga from src.util.openai import openai_batch_completion, OpenAIDecodingArguments HF_DATASET = "RussianNLP/russian_super_glue" def generate( model, tokenizer, prompts, generation_config, debug: bool = True ): data = tokenizer( prompts, return_tensors="pt", truncation=True, padding=True, ) data = {k: v.to(model.device) for k, v in data.items()} output_ids = model.generate( **data, generation_config=generation_config ) outputs = [] for sample_output_ids, sample_input_ids in zip(output_ids, data["input_ids"]): sample_output_ids = sample_output_ids[len(sample_input_ids):] sample_output = tokenizer.decode(sample_output_ids, skip_special_tokens=True) sample_output = sample_output.replace("</s>", "").strip() if debug: print(tokenizer.decode(sample_input_ids, skip_special_tokens=True)) print(sample_output) print() outputs.append(sample_output) return outputs def predict_saiga_zero_shot( model, tokenizer, generation_config, template_path, prompts, max_prompt_tokens: int = None, debug: bool = False ): default_conversation = Conversation.from_template(template_path) clean_prompts = [] for prompt in prompts: conversation = copy.deepcopy(default_conversation) conversation.add_user_message(prompt) prompt = conversation.get_prompt(tokenizer, max_tokens=max_prompt_tokens) clean_prompts.append(prompt) return generate( model=model, tokenizer=tokenizer, prompts=clean_prompts, generation_config=generation_config, debug=debug ) def find_lcs(s1, s2): max_lcs = "" for i in range(len(s1)): for j in range(i + 1, len(s1)): ss1 = s1[i:j] if ss1 in s2 and len(ss1) > len(max_lcs): max_lcs = ss1 return max_lcs # DaNetQA DANETQA_PROMPT = '''Контекст: {passage} Используя контекст, ответь одним словом на вопрос: {question}''' DANETQA_YES_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|верно|правда|может)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) DANETQA_NO_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(нет|неверно|неправда|не|ложь|редко)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def clean_danetqa_response(response): result = True if bool(DANETQA_YES_RE.match(response)): result = True elif bool(DANETQA_NO_RE.match(response)): result = False else: print("ERROR! Не удалось найти Да/Нет в ответе модели и преобразовать его в bool:", response) return result def predict_danetqa( split, predict_func, output_path, batch_size: int = 4, nrows: int = None, template: str = DANETQA_PROMPT, clean_func: Callable = clean_danetqa_response ): records = list(load_dataset(HF_DATASET, "danetqa", split=split)) if nrows: records = records[:nrows] prompts = [] for record in records: prompt = template.format(passage=record["passage"], question=record["question"]) prompts.append(prompt) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) labels, predictions = [], [] for record, response in zip(records, responses): prediction = clean_func(response) record["prediction"] = prediction label = record["label"] if label != -1: labels.append(label) predictions.append(prediction) if labels: print("danetqa accuracy:", accuracy_score(labels, predictions)) outputs = [] for record in records: label = str(record["prediction"]).lower() outputs.append({"idx": record["idx"], "label": label}) write_jsonl(outputs, output_path) return records # TERRA TERRA_PROMPT = '''Текст: {premise} Утверждение: {hypothesis} Используя текст, ответь одним словом на вопрос: Вероятно ли утверждение при условии остального текста?''' TERRA_ENTAILMENT_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|верно|правда|может|являются|вероятно)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) TERRA_NOT_ENTAILMENT_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(нет|неверно|неверное|невероятно|не вероятно|не)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def terra_to_bool(response): return response == "entailment" def clean_terra_response(response): result = "not_entailment" if bool(TERRA_ENTAILMENT_RE.match(response)): result = "entailment" elif bool(TERRA_NOT_ENTAILMENT_RE.match(response)): result = "not_entailment" else: print("ERROR! Не удалось найти Да/Нет в ответе модели и преобразовать его в bool", response) return result def predict_terra( split, predict_func, output_path, batch_size: int = 8, nrows: int = None, template: str = TERRA_PROMPT, clean_func=clean_terra_response ): records = list(load_dataset(HF_DATASET, "terra", split=split)) if nrows: records = records[:nrows] prompts = [] for record in records: prompts.append(template.format( premise=record["premise"], hypothesis=record["hypothesis"] )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) labels, predictions = [], [] for record, response in zip(records, responses): prediction = clean_func(response) record["prediction"] = prediction label = record["label"] if label != -1: labels.append(1 - label) predictions.append(terra_to_bool(prediction)) if labels: print("terra accuracy:", accuracy_score(labels, predictions)) outputs = [{"idx": r["idx"], "label": r["prediction"]} for r in records] write_jsonl(outputs, output_path) return records # RWSD RWSD_PROMPT = 'Текст: "{text}"\nНа основе текста одним словом ответь на вопрос: К кому или к чему относится местоимение во фразе "{span2}"?' def clean_rwsd_response(response, span1): lcs = find_lcs(span1.lower(), response.lower()) return len(lcs) >= 3 def predict_rwsd( split, predict_func, output_path, batch_size: int = 4, nrows: int = None, template: str = RWSD_PROMPT, clean_func: Callable = clean_rwsd_response ): records = list(load_dataset(HF_DATASET, "rwsd", split=split)) if nrows: records = records[:nrows] prompts = [] for record in records: prompts.append(template.format( text=record["text"], span2=record["span2_text"], span1=record["span1_text"], )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) labels, predictions = [], [] for record, response in zip(records, responses): prediction = clean_func(response, record["span1_text"]) record["prediction"] = prediction label = record["label"] if label != -1: labels.append(label) predictions.append(prediction) if labels: print("rwsd accuracy:", accuracy_score(labels, predictions)) outputs = [{"idx": r["idx"], "label": str(r["prediction"])} for r in records] write_jsonl(outputs, output_path) return records # MUSERC MUSERC_SINGLE_PROMPT = """Текст: {text} Вопрос: {question} Является ли "{answer}" правильным ответом на этот вопрос? Основываясь на тексте, ответь только "да" или "нет".""" MUSERC_SINGLE_YES_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|является)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) MUSERC_SINGLE_NO_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(нет|не)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def clean_muserc_single_response(response): result = False if bool(MUSERC_SINGLE_YES_RE.match(response)): result = True elif bool(MUSERC_SINGLE_NO_RE.match(response)): result = False else: print("ERROR! Не удалось найти Да/Нет в ответе модели и преобразовать его в bool:", response) return result def predict_muserc( split, predict_func, output_path, batch_size: int = 2, nrows: int = None, template: str = MUSERC_SINGLE_PROMPT, clean_func: Callable = clean_muserc_single_response ): records = list(load_dataset(HF_DATASET, "muserc", split=split)) if nrows: records = records[:nrows] prompts = list() for record in records: text, question, answer = record["paragraph"], record["question"], record["answer"] answer = answer.rstrip(".") prompts.append(template.format( text=text, question=question, answer=answer )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) labels, predictions = [], [] for record, response in zip(records, responses): record["prediction"] = clean_func(response) if record["label"] != -1: labels.append(record["label"]) predictions.append(record["prediction"]) if labels: print("muserc accuracy:", accuracy_score(labels, predictions)) outputs = [] prev_idx = None for record in records: idx = record["idx"] pidx, qidx, aidx = idx["paragraph"], idx["question"], idx["answer"] ppidx, pqidx = None, None if prev_idx: ppidx, pqidx = prev_idx["paragraph"], prev_idx["question"] if ppidx != pidx: outputs.append({"idx": pidx, "passage": {"questions": []}}) assert len(outputs) - 1 == pidx paragraph = outputs[-1] if pqidx != qidx: paragraph["passage"]["questions"].append({"idx": qidx, "answers": []}) question = paragraph["passage"]["questions"][-1] answer = {"idx": aidx, "label": int(record["prediction"])} question["answers"].append(answer) prev_idx = idx write_jsonl(outputs, output_path) return records # RUCOS def rucos_clean_text(text): text = " ".join([s.strip().rstrip(".") + "." for s in text.split("@header")]).strip() text = " ".join([s.strip().rstrip(".") + "." for s in text.split("@context")]).strip() text = " ".join([s.strip().rstrip(".") + "." for s in text.split("@highlight")]).strip() text = " ".join([s.strip() for s in text.split("\n") if s.strip()]) return text RUCOS_MASK = "[entity]" RUCOS_PROMPT = """Контекст: {text} Запрос: {query} Какое имя человека или название организации или название места должно быть вместо {mask} в запросе? Ответь не более чем 3 словами в соответствии с контекстом.""" def clean_rucos_response(response, entities): answers = [] for answer in entities: lcs = find_lcs(response.strip(), answer.strip()) answers.append((len(lcs), answer)) return max(answers)[1] def predict_rucos( split, predict_func, output_path, batch_size: int = 4, nrows: int = None, debug: bool = False, template: str = RUCOS_PROMPT, clean_func: Callable = clean_rucos_response ): records = list(load_dataset(HF_DATASET, "rucos", split=split)) if nrows: records = records[:nrows] prompts = list() for record in records: entities = record["entities"] query = record["query"] text = rucos_clean_text(record["passage"]) entities = [e.strip().strip(",") for e in entities] query = query.replace("@placeholder", RUCOS_MASK) prompts.append(template.format( text=text, query=query, mask=RUCOS_MASK )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) correct_count, all_count = 0, 0 for response, record in zip(responses, records): final_response = clean_func(response, record["entities"]) record["prediction"] = final_response answers = record["answers"] if answers: all_count += 1 prediction = record["prediction"].strip().lower() for answer in answers: answer = answer.strip().lower() if edit_distance(answer, prediction) <= 2: correct_count += 1 break if all_count > 0: print("rucos accuracy:", correct_count / all_count) outputs = [{"idx": r["idx"]["query"], "label": r["prediction"]} for r in records] write_jsonl(outputs, output_path) return records # LIDIRUS LIDIRUS_PROMPT = '''Текст: "{sentence1}" Используя текст, можно ли сказать, что утверждение "{sentence2}" точно корректно относительно ситуации из текста? Ответь только "да" или "нет".''' LIDIRUS_ENTAILMENT_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|верно|правда|может|вероятна|верная)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) LIDIRUS_NOT_ENTAILMENT_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(не|нет|неверно|неверное|невероятна|неверная)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def lidirus_to_bool(response): return response == "entailment" def clean_lidirus_response(response): result = "not_entailment" if bool(LIDIRUS_ENTAILMENT_RE.match(response)): result = "entailment" elif bool(LIDIRUS_NOT_ENTAILMENT_RE.match(response)): result = "not_entailment" else: print("ERROR! Не удалось найти Да/Нет в ответе модели и преобразовать его в bool", response) return result def predict_lidirus( predict_func, output_path, batch_size: int = 4, nrows: int = None, template: str = LIDIRUS_PROMPT, clean_func: Callable = clean_lidirus_response ): records = list(load_dataset(HF_DATASET, "lidirus", split="test")) if nrows: records = records[:nrows] prompts = [template.format( sentence1=r["sentence1"], sentence2=r["sentence2"] ) for r in records] responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) labels, predictions = [], [] for record, response in zip(records, responses): prediction = clean_func(response) record["prediction"] = prediction label = record["label"] labels.append(1 - label) predictions.append(lidirus_to_bool(prediction)) print("lidirus accuracy:", accuracy_score(labels, predictions)) print("lidirus corr:", matthews_corrcoef(labels, predictions)) outputs = [{"idx": r["idx"], "label": r["prediction"]} for r in records] write_jsonl(outputs, output_path) return records # PARUS PARUS_CAUSE_PROMPT = """Выбери одну наиболее вероятную причину исключительно из двух предложенных вариантов. Варианты: {choice1}; {choice2} {premise}, так как...""" PARUS_EFFECT_PROMPT = """Выбери одно наиболее вероятное следствие исключительно из двух предложенных вариантов. Варианты: {choice1}; {choice2} {premise}, поэтому...""" def predict_parus( split, predict_func, output_path, batch_size: int = 12, nrows: int = None, template_cause: str = PARUS_CAUSE_PROMPT, template_effect: str = PARUS_EFFECT_PROMPT ): records = list(load_dataset(HF_DATASET, "parus", split=split)) if nrows: records = records[:nrows] prompts = [] for r in records: idx = r["idx"] c1 = r["choice1"].rstrip(".").lower() c2 = r["choice2"].rstrip(".").lower() premise = r["premise"].rstrip(".") is_cause = r["question"] == "cause" template = template_cause if is_cause else template_effect prompts.append(template.format( premise=premise, choice1=c1, choice2=c2 )) responses = list() for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) assert len(responses) == len(records) for idx, (response, record) in enumerate(zip(responses, records)): response = response.lower() c1 = record["choice1"].rstrip(".").lower() c2 = record["choice2"].rstrip(".").lower() c1_lcs = find_lcs(response, c1) c2_lcs = find_lcs(response, c2) record["prediction"] = int(len(c2_lcs) > len(c1_lcs)) if records[0]["label"] != -1: y_true, y_pred = [], [] for r in records: y_pred.append(r["prediction"]) y_true.append(r["label"]) score = accuracy_score(y_true, y_pred) print("parus accuracy:", score) outputs = [{"idx": r["idx"], "label": int(r["prediction"])} for r in records] write_jsonl(outputs, output_path) return records # RCB RCB_PROMPT = """Дан текст: "{premise}" Ответь на вопрос по тексту "да", "нет" или "может быть": {question}""" RCB_YES_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|верно|вероятно)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) RCB_NO_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(нет|неверно|неверное|невероятно|не)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def clean_rcb_response(response): is_contradiction = bool(RCB_NO_RE.match(response)) is_entailment = bool(RCB_YES_RE.match(response)) if is_contradiction: return "contradiction" if is_entailment: return "entailment" return "neutral" def rcb_label2index(label): mapping = { "entailment": 0, "contradiction": 1, "neutral": 2 } return mapping[label] def predict_rcb( split, predict_func, output_path, batch_size: int = 8, nrows: int = None, template: str = RCB_PROMPT, clean_func: Callable = clean_rcb_response ): records = list(load_dataset(HF_DATASET, "rcb", split=split)) if nrows: records = records[:nrows] questions = [record["hypothesis"].rstrip(".") + "?" for record in records] prompts = [] for record, question in zip(records, questions): prompts.append(template.format( premise=record["premise"], question=question )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) for r, response in zip(records, responses): r["prediction"] = clean_func(response) if records[0]["label"] != -1: labels = [r["label"] for r in records] responses = [rcb_label2index(r["prediction"]) for r in records] print("rcb accuracy:", accuracy_score(labels, responses)) outputs = [{"idx": r["idx"], "label": r["prediction"]} for r in records] write_jsonl(outputs, output_path) return records # RUSSE RUSSE_PROMPT = '''Ответь только "да" или "нет" на вопрос: В текстовом фрагменте "{sentence1}" и текстовом фрагменте "{sentence2}" означают ли слова "{word}" разное?''' RUSSE_YES_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(да|верно|вероятно|одно)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) RUSSE_NO_RE = re.compile( r"^[^\w]*(Выходные данные|Выход|Ответ|Оценка)?[^\w]*(нет|не)", re.IGNORECASE | re.MULTILINE | re.DOTALL ) def clean_russe_response(response): if bool(RUSSE_YES_RE.match(response)): return 0 if bool(RUSSE_NO_RE.match(response)): return 1 print("ERROR! Не удалось найти Да/Нет в ответе модели и преобразовать его в bool:", response) return 1 def predict_russe( split, predict_func, output_path, batch_size: int = 8, nrows: int = None, template: str = RUSSE_PROMPT, clean_func: Callable = clean_russe_response ): records = list(load_dataset(HF_DATASET, "russe", split=split)) if nrows: records = records[:nrows] prompts = [] for record in records: prompts.append(template.format( sentence1=record["sentence1"], sentence2=record["sentence2"], word=record["word"] )) responses = [] for batch in tqdm(gen_batch(prompts, batch_size), total=len(prompts) // batch_size + 1): responses.extend(predict_func(batch)) for r, response in zip(records, responses): r["prediction"] = clean_func(response) if records[0]["label"] != -1: labels = [r["label"] for r in records] responses = [r["prediction"] for r in records] print("russe accuracy:", accuracy_score(labels, responses)) outputs = [{ "idx": r["idx"], "label": str(bool(r["prediction"])).lower() } for r in records] write_jsonl(outputs, output_path) return records ALL_TASKS = ("danetqa", "lidirus", "muserc", "parus", "rcb", "rucos", "russe", "rwsd", "terra") def main( model_name, nrows: int = None, template_path: str = "internal_prompts/saiga_v2.json", split: str = "test", predictions_dir: str = "submission", debug: bool = False, tasks: Tuple[str] = ALL_TASKS ): predictions_dir = Path(predictions_dir) predict_short = None predict_long = None if model_name not in ("gpt-4", "gpt-3.5-turbo"): model, tokenizer, generation_config = load_saiga(model_name) generation_config.no_repeat_ngram_size = 64 generation_config.temperature = 0.01 def predict_saiga_zero_shot_bound(batch): generation_config.max_new_tokens = 256 return predict_saiga_zero_shot( model=model, tokenizer=tokenizer, generation_config=generation_config, template_path=template_path, prompts=batch, debug=debug ) def predict_saiga_zero_shot_bound_short(batch): generation_config.max_new_tokens = 8 return predict_saiga_zero_shot( model=model, tokenizer=tokenizer, generation_config=generation_config, template_path=template_path, prompts=batch, debug=debug ) predict_long = predict_saiga_zero_shot_bound predict_short = predict_saiga_zero_shot_bound_short else: def predict_chatgpt(batch): messages = [[{"role": "user", "content": prompt}] for prompt in batch] responses = openai_batch_completion(messages, model_name=model_name) responses = [r.message.content for r in responses] if debug: for prompt, response in zip(batch, responses): print(prompt) print(response) print() return responses def predict_chatgpt_short(batch): messages = [[{"role": "user", "content": prompt}] for prompt in batch] responses = openai_batch_completion( messages, decoding_args=OpenAIDecodingArguments(max_tokens=16), model_name=model_name ) responses = [r.message.content for r in responses] if debug: for prompt, response in zip(batch, responses): print(prompt) print(response) print() return responses predict_long = predict_chatgpt predict_short = predict_chatgpt_short if "danetqa" in tasks: predict_danetqa( split=split, predict_func=predict_short, output_path=predictions_dir / "DaNetQA.jsonl", nrows=nrows ) if "terra" in tasks: predict_terra( split=split, predict_func=predict_short, output_path=predictions_dir / "TERRa.jsonl", nrows=nrows ) if "rwsd" in tasks: predict_rwsd( split=split, predict_func=predict_long, output_path=predictions_dir / "RWSD.jsonl", nrows=nrows ) if "rucos" in tasks: predict_rucos( split=split, predict_func=predict_long, output_path=predictions_dir / "RuCoS.jsonl", nrows=nrows ) if "lidirus" in tasks: predict_lidirus( predict_func=predict_short, output_path=predictions_dir / "LiDiRus.jsonl", nrows=nrows ) if "parus" in tasks: predict_parus( split=split, predict_func=predict_long, output_path=predictions_dir / "PARus.jsonl", nrows=nrows ) if "rcb" in tasks: predict_rcb( split=split, predict_func=predict_long, output_path=predictions_dir / "RCB.jsonl", nrows=nrows ) if "russe" in tasks: predict_russe( split=split, predict_func=predict_short, output_path=predictions_dir / "RUSSE.jsonl", nrows=nrows ) if "muserc" in tasks: predict_muserc( split=split, predict_func=predict_short, output_path=predictions_dir / "MuSeRC.jsonl", nrows=nrows ) if __name__ == "__main__": fire.Fire(main)
[ "Контекст: {passage}\n\nИспользуя контекст, ответь одним словом на вопрос: {question}", "sentence2", "Выбери одно наиболее вероятное следствие исключительно из двух предложенных вариантов.\n\nВарианты: {choice1}; {choice2}\n\n{premise}, поэтому...", "Текст: {text}\n\nВопрос: {question}\n\nЯвляется ли \"{answer}\" правильным ответом на этот вопрос? Основываясь на тексте, ответь только \"да\" или \"нет\".", "Текст: \"{sentence1}\"\n\nИспользуя текст, можно ли сказать, что утверждение \"{sentence2}\" точно корректно относительно ситуации из текста? Ответь только \"да\" или \"нет\".", "Выбери одну наиболее вероятную причину исключительно из двух предложенных вариантов.\n\nВарианты: {choice1}; {choice2}\n\n{premise}, так как...", "Дан текст: \"{premise}\"\n\nОтветь на вопрос по тексту \"да\", \"нет\" или \"может быть\": {question}", "question", "Контекст: {text}\nЗапрос: {query}\n\nКакое имя человека или название организации или название места должно быть вместо {mask} в запросе? Ответь не более чем 3 словами в соответствии с контекстом.", "Текст: {premise} Утверждение: {hypothesis}\nИспользуя текст, ответь одним словом на вопрос: Вероятно ли утверждение при условии остального текста?", "sentence1", "[]", "Текст: \"{text}\"\nНа основе текста одним словом ответь на вопрос: К кому или к чему относится местоимение во фразе \"{span2}\"?", "Ответь только \"да\" или \"нет\" на вопрос:\nВ текстовом фрагменте \"{sentence1}\" и текстовом фрагменте \"{sentence2}\" означают ли слова \"{word}\" разное?" ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~webapi~services~trading_advice_service.py
from dotenv import load_dotenv import os import pandas as pd import pandas_ta as ta import yfinance as yf from langchain import OpenAI from langchain import PromptTemplate from langchain import FewShotPromptTemplate from langchain.chat_models import ChatOpenAI from KZ_project.Infrastructure.services.kayze_assistant_service.kayze_assistant import ( KayzeAssistant, ) load_dotenv() openai_api_key = os.getenv("OPENAI_API_KEY") os.environ["OPENAI_API_KEY"] = openai_api_key verbose = True llm = ChatOpenAI(temperature=0.9, openai_api_key=openai_api_key) conversation_stages = { "1": "Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.", "2": "Discover Preferences: Ask the client about their hobbies, interests or other personal information to provide a more personalized service.", "3": "Education Service Presentation: Provide more detailed information about the education services offered by the company.", "4": "AI Trading Service Presentation: Provide more detailed information about the AI trading services offered by the company.", "5": "Close: Ask if they want to proceed with the service. This could be starting a trial, setting up a meeting, or any other suitable next step.", "6": "Company Info: Provide general information about company like what is company and what are purposes and aimed etc.", "7": "Trading Advice Service Presentation: Provide and give detailed trading advice about to asked specific coin or asset", } config = dict( agent_name="KayZe", agent_role="Service Representative", company_name="KZEngine", company_values="Our vision is helping people trading decision when buy and sell decion process, via the Artificial Intelligence and MAchine Learning process.", conversation_purpose="Choosing the right service for the client and showing them the best option.", conversation_history=[], conversation_type="talking", conversation_stage=conversation_stages.get( "1", "Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.", ), ) kayze_agent = KayzeAssistant.from_llm(llm, verbose=False, **config) kayze_agent.seed_agent() def create_openai_model(model_name: str = "text-davinci-003", temperature: float = 0.7): openai = OpenAI( # model_name='text-davinci-003', model_name=model_name, temperature=temperature, ) return openai def create_fewshot_template(): examples = [ { "query": f"RSI indicator value is 70.34, MFI indicator value is 59.02, DMP indicator value is 31.3," f" DMN indicator value is 12.77 and ADX indicator value is 41.26." f" What is your advice for trading for those indicator values?", "answer": "The RSI indicator value being above 70 at this moment indicates an overbought zone has been entered." "The MFI value trending above the average confirms the flow of money. On the DMi side, " "the DMP (positive directional movement indicator) value is above " "the DMN (negative directional movement indicator) value, " "and ADX is strongly trending above 25 and at 40 levels, " "indicating a strong bull trend that has entered in a short period of time. " "When considering the flow of money and the overbought zone, it may be advisable " "to take some profits and waiting next market movements.", }, { "query": f"RSI indicator value is 40.14, MFI indicator value is 41, DMP indicator value is 21.01," f" DMN indicator value is 23.67 and ADX indicator value is 20.76." f" What is your advice for trading for those indicator values?", "answer": "The RSI indicator value dropping around 40 indicates approaching the selling zone. " "The MFI index also dropping around 40 supports this. Although ADX suggests that there is no strong trend below 25, " "it can be observed that DMN is above DMP, creating selling pressure. " "My recommendation would be to wait for a better buying opportunity at this point.", }, ] # create a example template example_template = """ User: {query} AI: {answer} """ # create a prompt example from above template example_prompt = PromptTemplate( input_variables=["query", "answer"], template=example_template ) # now break our previous prompt into a prefix and suffix # the prefix is our instructions prefix = """Answer the question based on the context below. You are the trading advisor. Also you are expert on RSI, MFI and DMP, DMI indicators. Context: RSI indicator value range betwwen 0-100. RSI value 70 and above meaning that overbought area. So you should sell your coin. also value 80 is a extreme overbought and you act cautiously. RSI value 30 that meaning is overselling area. and value 20 is extreme overselling are. if the RSI value are 20-30s you should bought this coin. 30-70 range waiting or you can look other indicator results. MFI indicator value range betwen 0-100. MFI value 80 and above meaning that overbought area. So you should sell your coin. also value 90 is a extreme overbought and you should act cautiously. MFI value 20 that meaning is overselling area. and value 10 is extreme overselling are. if the MFI value are 10-20s you should bought this coin. 20-80 range waiting or you can look other indicator results. Else it shows overselling condition between 0-25. DMI indicator is a collection of indicators including DMP, DMI, and ADX. The Plus Direction Indicator DMP and Minus Direction Indicator DMI show the current price direction. When the DMP is above DMN, the current price momentum is up. When the DMN is above DMP, the current price momentum is down. ADX measures the strength of the trend, either up or down; a reading above 25 indicates a strong trend. Here are some examples: """ # and the suffix our user input and output indicator suffix = """ User: {query} AI: """ # now create the few shot prompt template few_shot_prompt_template = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["query"], example_separator="\n\n", ) return few_shot_prompt_template def get_response_llm(model, fewshot_template, query: str): return model(fewshot_template.format(query=query)) def fetch_data(symbol: str, period: str, interval: str): # Fetch Bitcoin data from Yahoo Finance ohlc_data = yf.download( tickers=symbol, period=period, interval=interval, progress=False ) return ohlc_data def calculate_dmi_rsi_mfi(data): data.ta.adx(length=14, append=True) data.ta.rsi(length=14, append=True) data.ta.mfi(length=14, append=True) data = data.dropna(axis=0) return data def create_query(indicator_data, symbol): rsi_14 = indicator_data.RSI_14.iloc[-1] mfi_14 = indicator_data.MFI_14.iloc[-1] dmp_14 = indicator_data.DMP_14.iloc[-1] dmn_14 = indicator_data.DMN_14.iloc[-1] adx_14 = indicator_data.ADX_14.iloc[-1] query = ( f"For {symbol}: RSI indicator value is {rsi_14:.2f}, MFI indicator value is \ {mfi_14:.2f}, DMP indicator value is {dmp_14:.2f}," f" DMN indicator value is {dmn_14:.2f} and ADX indicator value is {adx_14:.2f}." f" What is your advice for trading for those indicator values?" ) return query def get_ohlc_data(symbol: str): # 'BTC-USD' df = fetch_data(symbol, "1mo", "1h") indicator_data = calculate_dmi_rsi_mfi(df) return indicator_data if __name__ == "__main__": symbol = "BTC-USD" openai = create_openai_model() fewshot = create_fewshot_template() df = get_ohlc_data(symbol) query_test = create_query(df, symbol) advice_test = get_response_llm(openai, fewshot, query_test) print(advice_test)
[ "\n\n", "\n User: {query}\n AI: {answer}\n ", "Answer the question based on the context below.\n You are the trading advisor. Also you are expert on RSI, MFI and DMP, DMI indicators.\n\n Context: RSI indicator value range betwwen 0-100. RSI value 70 and above meaning that overbought area.\n So you should sell your coin. also value 80 is a extreme overbought and you act cautiously.\n RSI value 30 that meaning is overselling area. and value 20 is extreme overselling are.\n if the RSI value are 20-30s you should bought this coin. 30-70 range waiting or\n you can look other indicator results.\n MFI indicator value range betwen 0-100. MFI value 80 and above meaning that overbought area.\n So you should sell your coin. also value 90 is a extreme overbought and you should act cautiously.\n MFI value 20 that meaning is overselling area. and value 10 is extreme overselling are.\n if the MFI value are 10-20s you should bought this coin. 20-80 range waiting or\n you can look other indicator results.\n Else it shows overselling condition between 0-25.\n DMI indicator is a collection of indicators including DMP, DMI, and ADX. The Plus Direction Indicator DMP and\n Minus Direction Indicator DMI show the current price direction. When the DMP is above DMN,\n the current price momentum is up. When the DMN is above DMP, the current price momentum is down.\n ADX measures the strength of the trend, either up or down; a reading above 25 indicates a strong trend.\n Here are some examples:\n ", "answer", "\n User: {query}\n AI: " ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~redis_chatbot_service~transformers.py
from typing import Iterator from numpy import array, average import openai import pandas as pd import numpy as np from KZ_project.Infrastructure.redis_db.config import TEXT_EMBEDDING_CHUNK_SIZE, EMBEDDINGS_MODEL from KZ_project.Infrastructure.redis_db.database import load_vectors def get_col_average_from_list_of_lists(list_of_lists): """Return the average of each column in a list of lists.""" if len(list_of_lists) == 1: return list_of_lists[0] else: list_of_lists_array = array(list_of_lists) average_embedding = average(list_of_lists_array, axis=0) return average_embedding.tolist() # Create embeddings for a text using a tokenizer and an OpenAI engine def create_embeddings_for_text(text, tokenizer): """Return a list of tuples (text_chunk, embedding) and an average embedding for a text.""" token_chunks = list(chunks(text, TEXT_EMBEDDING_CHUNK_SIZE, tokenizer)) text_chunks = [tokenizer.decode(chunk) for chunk in token_chunks] embeddings_response = get_embeddings(text_chunks, EMBEDDINGS_MODEL) embeddings = [embedding["embedding"] for embedding in embeddings_response] text_embeddings = list(zip(text_chunks, embeddings)) average_embedding = get_col_average_from_list_of_lists(embeddings) return (text_embeddings, average_embedding) def get_embeddings(text_array, engine): return openai.Engine(id=engine).embeddings(input=text_array)["data"] # Split a text into smaller chunks of size n, preferably ending at the end of a sentence def chunks(text, n, tokenizer): """Yield successive n-sized chunks from text.""" tokens = tokenizer.encode(text) i = 0 while i < len(tokens): # Find the nearest end of sentence within a range of 0.5 * n and 1.5 * n tokens j = min(i + int(1.5 * n), len(tokens)) while j > i + int(0.5 * n): # Decode the tokens and check for full stop or newline chunk = tokenizer.decode(tokens[i:j]) if chunk.endswith(".") or chunk.endswith("\n"): break j -= 1 # If no end of sentence found, use n tokens as the chunk size if j == i + int(0.5 * n): j = min(i + n, len(tokens)) yield tokens[i:j] i = j def get_unique_id_for_file_chunk(filename, chunk_index): return str(filename + "-!" + str(chunk_index)) def handle_file_string(file, tokenizer, redis_conn, text_embedding_field, index_name): """ Handle a file string by cleaning it up, creating embeddings, and uploading them to Redis. Args: file (tuple): A tuple containing the filename and file body string. tokenizer: The tokenizer object to use for encoding and decoding text. redis_conn: The Redis connection object. text_embedding_field (str): The field in Redis where the text embeddings will be stored. index_name: The name of the index or identifier for the embeddings. Returns: None Raises: Exception: If there is an error creating embeddings or uploading to Redis. """ filename = file[0] file_body_string = file[1] # Clean up the file string by replacing newlines, double spaces, and semi-colons clean_file_body_string = file_body_string.replace(" ", " ").replace("\n", "; ").replace(';', ' ') # Add the filename to the text to embed text_to_embed = "Filename is: {}; {}".format(filename, clean_file_body_string) try: # Create embeddings for the text text_embeddings, average_embedding = create_embeddings_for_text(text_to_embed, tokenizer) # print("[handle_file_string] Created embedding for {}".format(filename)) except Exception as e: print("[handle_file_string] Error creating embedding: {}".format(e)) # Get the vectors array of triples: file_chunk_id, embedding, metadata for each embedding # Metadata is a dict with keys: filename, file_chunk_index vectors = [] for i, (text_chunk, embedding) in enumerate(text_embeddings): id = get_unique_id_for_file_chunk(filename, i) vectors.append({'id': id, "vector": embedding, 'metadata': {"filename": filename, "text_chunk": text_chunk, "file_chunk_index": i}}) # print(vectors) try: # Load vectors into Redis load_vectors(redis_conn, vectors, text_embedding_field) except Exception as e: print(f'Ran into a problem uploading to Redis: {e}') # Make a class to generate batches for insertion class BatchGenerator: def __init__(self, batch_size: int = 10) -> None: self.batch_size = batch_size # Makes chunks out of an input DataFrame def to_batches(self, df: pd.DataFrame) -> Iterator[pd.DataFrame]: splits = self.splits_num(df.shape[0]) if splits <= 1: yield df else: for chunk in np.array_split(df, splits): yield chunk # Determines how many chunks DataFrame contains def splits_num(self, elements: int) -> int: return round(elements / self.batch_size) __call__ = to_batches
[]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~kayze_assistant_service~kayze_assistant.py
import os from dotenv import load_dotenv from KZ_project.Infrastructure.services.kayze_assistant_service.service_chains import ServiceConversationChain from KZ_project.Infrastructure.services.kayze_assistant_service.service_chains import ServiceSelectionChain from typing import Dict, List, Any from langchain.llms import BaseLLM from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.chat_models import ChatOpenAI from KZ_project.Infrastructure.services.kayze_assistant_service.symbol_generation_service import SymboGenerationPromptService from KZ_project.Infrastructure.services.kayze_assistant_service.trading_advisor import TradingAdvisor from KZ_project.Infrastructure.services.redis_chatbot_service.index_redis_service import IndexRedisService class KayzeAssistant(Chain, BaseModel): """Controller model for the GptVerse Assistant.""" stage_id = "1" conversation_history: List[str] = [] current_conversation_stage: str = "1" service_selection_chain: ServiceSelectionChain = Field(...) service_conversation_utterance_chain: ServiceConversationChain = Field(...) conversation_stage_dict: Dict = { "1": "Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.", "2": "Discover Preferences: Ask the client about their hobbies, interests or other personal information to provide a more personalized service.", "3": "Education Service Presentation: Provide more detailed information about the education services offered by the company.", "4": "AI Trading Service Presentation: Provide more detailed information about the AI trading services offered by the company.", "5": "Close: Ask if they want to proceed with the service. This could be starting a trial, setting up a meeting, or any other suitable next step.", "6": "Company Info: Provide general information about company like what is company and what are purposes and aimed etc.", "7": "Trading Advice Service Presentation: Provide and give detailed trading advice about to asked specific coin or asset" } agent_name: str = "AI Assistant" agent_role: str = "Service Representative" company_name: str = "GptVerse" company_business: str = "GptVerse is a company dedicated to the metaverse. We provide education and AI trading services." company_values: str = "Our vision is to adapt people to the metaverse with AI processes, education, and AI trading systems, thereby helping people act like they are in a metaverse platform." conversation_purpose: str = "Choosing the right service for the client and showing them the best option. If the service is selected \ then provide more detailed information about service." conversation_type: str = "Chatting" def retrieve_conversation_stage(self, key): return self.conversation_stage_dict.get(key, "1") @property def input_keys(self) -> List[str]: return [] @property def output_keys(self) -> List[str]: return [] def seed_agent(self): # Step 1: seed the conversation self.current_conversation_stage = self.retrieve_conversation_stage("1") self.conversation_history = [] def determine_conversation_stage(self): conversation_stage_id = self.service_selection_chain.run( conversation_history='"\n"'.join(self.conversation_history), current_conversation_stage=self.current_conversation_stage, ) # # testing purposes....!!!!!! # if conversation_stage_id == "1": # self.conversation_history = [] self.current_conversation_stage = self.retrieve_conversation_stage( conversation_stage_id ) self.stage_id = conversation_stage_id # print(f"Conversation Stage: {self.current_conversation_stage}") def human_step(self, human_input): # process human input human_input = human_input + "<END_OF_TURN>" self.conversation_history.append(human_input) self.conversation_history = self.conversation_history[-5:] def step(self): self._call(inputs={}) def _call(self, inputs: Dict[str, Any]) -> None: """Run one step of the KayzeAssistant.""" # print(f"stage: {self.stage_id}") if self.stage_id == "6": # print("you are the company dtailed phase!!!!") redis_service = IndexRedisService() response_f1 = redis_service.response_f1_query(self.conversation_history[-1]) # print(f'last questions : {self.conversation_history[-1]}') response_f1 = response_f1 + " <END_OF_TURN>" self.conversation_history.append(response_f1) # print(f"{self.agent_name}: ", response_f1.rstrip("<END_OF_TURN>")) if self.stage_id == "4": # print("you are the ai trading dtailed phase!!!!") redis_service = IndexRedisService() response_f1 = redis_service.response_f1_query(self.conversation_history[-1]) # print(f'last questions : {self.conversation_history[-1]}') response_f1 = response_f1 + " <END_OF_TURN>" self.conversation_history.append(response_f1) # print(f"{self.agent_name}: ", response_f1.rstrip("<END_OF_TURN>")) if self.stage_id == "7": # print(f'last conversation , {self.conversation_history[-1]}') symbol = SymboGenerationPromptService.get_symbol(self.conversation_history[-1]) # print(symbol) tradv = TradingAdvisor.get_advice(symbol) tradv = f'For the {symbol}: ' + tradv + " <END_OF_TURN>" self.conversation_history.append(tradv) # print(f"{self.agent_name}: ", tradv.rstrip("<END_OF_TURN>")) # Generate agent's utterance ai_message = self.service_conversation_utterance_chain.run( agent_name=self.agent_name, agent_role=self.agent_role, company_name=self.company_name, company_values=self.company_values, conversation_purpose=self.conversation_purpose, conversation_history="\n".join(self.conversation_history), conversation_stage=self.current_conversation_stage, conversation_type=self.conversation_type, ) # Add agent's response to conversation history self.conversation_history.append(ai_message) self.conversation_history = self.conversation_history[-5:] # print(f"{self.agent_name} - base: ", ai_message.rstrip("<END_OF_TURN>")) return {} def get_response(self, chat: str): self.human_step(chat) self.determine_conversation_stage() self.step() return self.conversation_history[-1].rstrip("<END_OF_TURN>") @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = False, **kwargs) -> "KayzeAssistant": """Initialize the KayzeAssistant Controller.""" service_selection_chain = ServiceSelectionChain.from_llm(llm, verbose=verbose) service_conversation_utterance_chain = ServiceConversationChain.from_llm( llm, verbose=verbose ) return cls( service_selection_chain=service_selection_chain, service_conversation_utterance_chain=service_conversation_utterance_chain, verbose=verbose, **kwargs, ) if __name__ == '__main__': import os from dotenv import load_dotenv from langchain.chat_models import ChatOpenAI load_dotenv() openai_api_key = os.getenv('OPENAI_API_KEY') verbose = True llm = ChatOpenAI(temperature=0.9, openai_api_key=openai_api_key) conversation_stages = { "1": "Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.", "2": "Discover Preferences: Ask the client about their hobbies, interests or other personal information to provide a more personalized service.", "3": "Education Service Presentation: Provide more detailed information about the education services offered by the company.", "4": "AI Trading Service Presentation: Provide more detailed information about the AI trading services offered by the company.", "5": "Close: Ask if they want to proceed with the service. This could be starting a trial, setting up a meeting, or any other suitable next step.", "6": "Company Info: Provide general information about company like what is company and what are purposes and aimed etc.", "7": "Trading Advice Service Presentation: Provide and give detailed trading advice about to asked specific coin or asset" } config = dict( agent_name="AI Assistant", agent_role="Service Representative", company_name="GptVerse", company_values="Our vision is adaptive people to metaverse with AI process, education and ai trading systems. So people act like a metaverse platform.", conversation_purpose="Choosing the right service for the client and showing them the best option.", conversation_history=[ ], conversation_type="talking", conversation_stage=conversation_stages.get( "1", "Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.", ), ) kayze_agent = KayzeAssistant.from_llm(llm, verbose=False, **config) kayze_agent.seed_agent() res = kayze_agent.get_response("hello!") print(res)
[]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~twitter_service~twitter_collection.py
import datetime from tweepy import OAuth1UserHandler import tweepy import pandas as pd import os from dotenv import load_dotenv import datetime from datetime import timedelta as td from KZ_project.Infrastructure.constant import DATA_PATH from KZ_project.Infrastructure.logger.logger import Logger from langchain.document_loaders import TwitterTweetLoader import warnings warnings.filterwarnings('ignore') load_dotenv() access_tokent = os.getenv('TW_access_token') access_token_secrett = os.getenv('TW_access_token_secret') consumer_keyt = os.getenv('TW_consumer_key') consumer_secrett = os.getenv('TW_consumer_secret') MY_BEARER_TOKENt = os.getenv('TW_BEARER_TOKEN') class TwitterCollection(): def __init__( self, access_token=access_tokent, access_token_secret=access_token_secrett, consumer_key=consumer_keyt, consumer_secret=consumer_secrett, bearer_token=MY_BEARER_TOKENt, logger: Logger = None, connection: bool = True ): self.api = None self.access_token = access_token self.access_token_secret = access_token_secret self.consumer_key = consumer_key self.consumer_secret = consumer_secret self.bearer_token = bearer_token self.logger = logger self.connect = connection if connection: self.connect_twitter() self.client = tweepy.Client(bearer_token=self.bearer_token) def log(self, text): if self.logger: self.logger.append_log(text) else: print(text) def connect_twitter(self): try: # auth = OAuth1UserHandler(self.consumer_key, self.consumer_secret) # auth.set_access_token(self.access_token, self.access_token_secret) self.api = tweepy.Client( access_token=self.access_token, access_token_secret=self.access_token_secret, consumer_key=self.consumer_key, consumer_secret=self.consumer_secret, bearer_token=self.bearer_token, wait_on_rate_limit=True ) self.log("Authentication Successfull") except: self.log("Error: Authentication Failed") def get_users_twitter(self, file: str) -> list: with open(file, 'r') as f: usernames = f.read().splitlines() return usernames def get_tweet_contents(self, tw_counts_points: int) -> pd.DataFrame: loader = TwitterTweetLoader.from_bearer_token( oauth2_bearer_token=self.bearer_token, twitter_users=self.get_users_twitter(os.path.join(DATA_PATH, 'tweets_data/list_users_twitter.txt')), number_tweets=tw_counts_points, # Default value is 100 ) contents = [] documents = loader.load() for doc in documents: contents.append(doc.dict()) df = pd.DataFrame(contents) df['created_at'] = pd.to_datetime(df['metadata'].apply(lambda x: x['created_at']), format='%a %b %d %H:%M:%S %z %Y') df['screen_name'] = df['metadata'].apply(lambda x: x['user_info']['screen_name']) df = df[['screen_name', 'page_content', 'created_at']] df = df.rename(columns={'page_content': 'text', 'screen_name': 'username'}) return df def post_tweet_with_media(self, tweet: str, media_path: str = None): tweet = tweet media_path = media_path if media_path: media = self.api.media_upload(media_path) # Post the tweet with the media ID response = self.api.create_tweet(text=tweet, media_ids=[media.media_id]) else: response = self.api.create_tweet(text=tweet) return response def get_tweets(self, search_query: str, lang: str, start_time: str, end_time: str): query = f"#{search_query} lang:{lang} -is:retweet" tweets = self.client.search_recent_tweets(query=query, start_time=start_time, end_time=end_time, tweet_fields=["created_at", "text", "source"], user_fields=["name", "username", "location", "verified", "description"], max_results=100, # max result is 100 expansions='author_id' ) return tweets def converts_tweets_pd(self, tweets: dict) -> pd.DataFrame(): tweet_info_ls = [] for user in tweets.includes.get('users', ''): for tweet, user in zip(tweets.data, tweets.includes['users']): tweet_info = { 'created_at': tweet.created_at, 'text': tweet.text, 'source': tweet.source, 'name': user.name, 'username': user.username, 'location': user.location, 'verified': user.verified, 'description': user.description } tweet_info_ls.append(tweet_info) tweets_df = pd.DataFrame(tweet_info_ls) return tweets_df def get_tweets_with_interval(self, hashtag: str, lang: str, start_time=None, finish_time=None, hour=24, interval=1) -> pd.DataFrame(): now = datetime.datetime.now(datetime.timezone.utc) if start_time == None: start_time = now - td(hours=hour) if finish_time == None: finish_time = now end_time = start_time + td(hours=interval) self.log( f'For hashtag {hashtag.upper()} with language {lang.upper()} start time: {start_time}, end time: {start_time + td(hours=hour)}') result_tweets = pd.DataFrame() while end_time <= finish_time - td(hours=interval): temp_tweets = self.get_tweets(hashtag, lang, start_time.isoformat(), end_time.isoformat()) df_temp_tweets = self.converts_tweets_pd(temp_tweets) result_tweets = pd.concat([df_temp_tweets, result_tweets], ignore_index=True) start_time = start_time + td(hours=interval) end_time = end_time + td(hours=interval) self.log(f'For hashtag {hashtag.upper()} {result_tweets.shape[0]} tweeets colected') return result_tweets def cleaning_tweet_data(self, df: pd.DataFrame()): df_tweets = df.copy() df_tweets.dropna(inplace=True) if 'Unnamed: 0' in df_tweets.columns: df_tweets.drop(columns=['Unnamed: 0'], axis=1, inplace=True) df_tweets.drop(columns=['source', 'name', 'location', 'verified', 'description'], axis=1, inplace=True) blanks = [] # start with an empty list for i, created_at, text, *others in df_tweets.itertuples(): if type(text) == str: if text.isspace(): blanks.append(i) df_tweets.drop(blanks, inplace=True) def cleaning_tweet_data_v2( df: pd.DataFrame()): # for langchain tweet loader new versions import re df_tweets = df.copy() if 'Unnamed: 0' in df_tweets.columns: df_tweets.drop(columns=['Unnamed: 0'], axis=1, inplace=True) if 'source' in df_tweets.columns: df_tweets.drop(columns=['source', 'name', 'location', 'verified', 'description'], axis=1, inplace=True) df_tweets = df_tweets.apply(lambda x: x.astype(str).str.lower()).drop_duplicates(subset=['text', 'username'], keep='first') df_tweets['text'] = df_tweets['text'].apply(lambda x: re.split('https:\/\/.*', str(x))[0]) df_tweets['text'] = df_tweets['text'].str.lower() df_tweets['text'] = df_tweets['text'].str.replace("@[a-z0-9A-Z]+", "", regex=True) df_tweets['text'] = df_tweets['text'].str.replace("#[a-z0-9A-Z]+", "", regex=True) blanks = [] # start with an empty list for i, created_at, text, *username in df_tweets.itertuples(): if type(text) == str: if text.isspace(): blanks.append(i) df_tweets.drop(blanks, inplace=True) df_tweets.dropna(inplace=True) return df_tweets def write_tweets_csv(self, df: pd.DataFrame(), pathdf: str, filedf: str) -> None: if not os.path.exists(os.path.join(pathdf, filedf)): os.makedirs(pathdf, exist_ok=True) with open(os.path.join(pathdf, filedf), mode='a'): pass print df.to_csv(os.path.join(pathdf, filedf)) self.log(f'Tweeets writes to File to first time: {pathdf} {filedf}') else: chunksize = 1000 list_of_dataframes = [] for df_read in pd.read_csv(os.path.join(pathdf, filedf), chunksize=chunksize, index_col=[0]): list_of_dataframes.append(df_read) temp_tweets = pd.concat(list_of_dataframes) self.log(f'Read Tweets from File and Chunksized to {chunksize}') temp_tweets = pd.concat([df, temp_tweets]) self.cleaning_tweet_data(temp_tweets) temp_tweets.to_csv(os.path.join(pathdf, filedf)) self.log(f'Concantenated tweets writed to file {pathdf} {filedf}') def get_tweets_df(self, symbol: str, pathdf: str, filedf: str) -> pd.DataFrame(): if not os.path.exists(os.path.join(pathdf, filedf)): self.log(f'This symbols {symbol} tweet not have') return else: chunksized = 100000 list_of_dataframes = [] for df in pd.read_csv(os.path.join(pathdf, filedf), chunksize=chunksized, index_col=0, lineterminator='\n'): list_of_dataframes.append(df) temp_tweets = pd.concat(list_of_dataframes) return temp_tweets def throw_unnamed_cols(self, df_tweet) -> pd.DataFrame(): index_columns_list = ['created_at', 'text', 'source', 'name', 'username', 'location', 'verified', 'description'] df_tweet = df_tweet.drop([i for i in df_tweet.columns.to_list() if i not in index_columns_list], axis=1) df_tweet.reset_index(inplace=True) if 'index' in df_tweet.columns: df_tweet.drop(columns=['index'], axis=1, inplace=True) return df_tweet def get_last_mont_df(self, sent_scores): from pandas.tseries.offsets import DateOffset last_month_start = sent_scores.index.max() - DateOffset(months=1) last_month_scores = sent_scores[sent_scores.index >= last_month_start]['sentiment_score'] return last_month_scores if __name__ == '__main__': from KZ_project.ml_pipeline.sentiment_analyzer.sentiment_analyzer import SentimentAnalyzer import matplotlib.pyplot as plt from KZ_project.webapi.services.services import add_sentiment_record_from_dataframe, get_all_sentiment_records from KZ_project.webapi.entrypoints.flask_app import get_session client = TwitterCollection() df = client.get_tweet_contents(tw_counts_points=1000) sid = SentimentAnalyzer() df = sid.cleaning_tweet_data(df) df = sid.preprocessing_tweet_datetime(df) df = sid.get_sentiment_scores(df) sid.add_datetime_to_col(df) sent_scores = sid.get_sent_with_mean_interval(df, '1h') last_month = client.get_last_mont_df(sent_scores) # uses dataframe to series procedure print(last_month, last_month.info()) # sent_scores = sent_scores.to_frame() # dont forget the convert dataframe add_sentiment_record_from_dataframe(sent_scores, get_session()) result = get_all_sentiment_records(get_session()) print(result)
[ "text" ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~redis_db~database.py
import pandas as pd import numpy as np import openai from redis import Redis from redis.commands.search.field import VectorField from redis.commands.search.field import TextField, NumericField from redis.commands.search.query import Query from KZ_project.Infrastructure.redis_db.config import ( EMBEDDINGS_MODEL, PREFIX, VECTOR_FIELD_NAME, ) # Get a Redis connection def get_redis_connection(host="localhost", port="6379", db=0): r = Redis(host=host, port=port, db=db, decode_responses=False) return r # Create a Redis index to hold our data def create_hnsw_index( redis_conn, vector_field_name, vector_dimensions=1536, distance_metric="COSINE" ): redis_conn.ft().create_index( [ VectorField( vector_field_name, "HNSW", { "TYPE": "FLOAT32", "DIM": vector_dimensions, "DISTANCE_METRIC": distance_metric, }, ), TextField("filename"), TextField("text_chunk"), NumericField("file_chunk_index"), ] ) # Create a Redis pipeline to load all the vectors and their metadata def load_vectors(client: Redis, input_list, vector_field_name): p = client.pipeline(transaction=False) for text in input_list: # hash key key = f"{PREFIX}:{text['id']}" # hash values item_metadata = text["metadata"] # item_keywords_vector = np.array(text["vector"], dtype="float32").tobytes() item_metadata[vector_field_name] = item_keywords_vector # HSET p.hset(key, mapping=item_metadata) p.execute() # Make query to Redis def query_redis(redis_conn, query, index_name, top_k=2): ## Creates embedding vector from user query embedded_query = np.array( openai.Embedding.create( input=query, model=EMBEDDINGS_MODEL, )["data"][ 0 ]["embedding"], dtype=np.float32, ).tobytes() # prepare the query q = ( Query(f"*=>[KNN {top_k} @{VECTOR_FIELD_NAME} $vec_param AS vector_score]") .sort_by("vector_score") .paging(0, top_k) .return_fields("vector_score", "filename", "text_chunk", "text_chunk_index") .dialect(2) ) params_dict = {"vec_param": embedded_query} # Execute the query results = redis_conn.ft(index_name).search(q, query_params=params_dict) return results # Get mapped documents from Weaviate results def get_redis_results(redis_conn, query, index_name): # Get most relevant documents from Redis query_result = query_redis(redis_conn, query, index_name) # print(f'results: {query_result}') # Extract info into a list query_result_list = [] for i, result in enumerate(query_result.docs): result_order = i text = result.text_chunk score = result.vector_score query_result_list.append((result_order, text, score)) # Display result as a DataFrame for ease of us result_df = pd.DataFrame(query_result_list) result_df.columns = ["id", "result", "certainty"] return result_df
[]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~redis_chatbot_service~index_redis_service.py
import openai import os import tiktoken import textract from dotenv import load_dotenv from tqdm import tqdm from KZ_project.Infrastructure.redis_db.database import get_redis_connection, get_redis_results # Setup Redis from redis import Redis from redis.commands.search.query import Query from redis.commands.search.field import ( TextField, VectorField, NumericField ) from redis.commands.search.indexDefinition import ( IndexDefinition, IndexType ) from KZ_project.Infrastructure.redis_db.config import COMPLETIONS_MODEL, EMBEDDINGS_MODEL, CHAT_MODEL, TEXT_EMBEDDING_CHUNK_SIZE, VECTOR_FIELD_NAME from KZ_project.Infrastructure.services.redis_chatbot_service.transformers import handle_file_string from KZ_project.Infrastructure.constant import DATA_PATH load_dotenv() openai_api_key = os.getenv('OPENAI_API_KEY') os.environ['OPENAI_API_KEY'] = openai_api_key # Constants VECTOR_FIELD_NAME = 'content_vector' VECTOR_DIM = 1536 #len(data['title_vector'][0]) # length of the vectors #VECTOR_NUMBER = len(data) # initial number of vectors PREFIX = "gptversedoc" # prefix for the document keys DISTANCE_METRIC = "COSINE" # distance metric for the vectors (ex. COSINE, IP, L2) # Index INDEX_NAME = "f1-index" # name of the search index class IndexRedisService: def __init__(self): self.redis_client = get_redis_connection() filename = TextField("filename") text_chunk = TextField("text_chunk") file_chunk_index = NumericField("file_chunk_index") # define RediSearch vector fields to use HNSW index text_embedding = VectorField(VECTOR_FIELD_NAME, "HNSW", { "TYPE": "FLOAT32", "DIM": VECTOR_DIM, "DISTANCE_METRIC": DISTANCE_METRIC } ) # Add all our field objects to a list to be created as an index self.fields = [filename,text_chunk,file_chunk_index,text_embedding] openai.api_key = openai_api_key def index_checker(self): try: self.redis_client.ft(INDEX_NAME).info() print("Index already exists") except Exception as e: print(e) # Create RediSearch Index print('Not there yet. Creating') self.redis_client.ft(INDEX_NAME).create_index( fields = self.fields, definition = IndexDefinition(prefix=[PREFIX], index_type=IndexType.HASH) ) def initiliaze_tokenizer(self): openai.api_key = openai_api_key # Initialise tokenizer tokenizer = tiktoken.get_encoding("cl100k_base") # Process each PDF file and prepare for embedding pdf_files = self.get_pdf_files() for pdf_file in tqdm(pdf_files): pdf_path = os.path.join(self.get_data_dir(), pdf_file) print(pdf_path) # Extract the raw text from each PDF using textract text = textract.process(pdf_path, method='pdfminer') # Chunk each document, embed the contents and load to Redis handle_file_string((pdf_file, text.decode("utf-8")), tokenizer, self.redis_client, VECTOR_FIELD_NAME,INDEX_NAME) def get_data_dir(self): return os.path.join(DATA_PATH,'kz_pdfs') # change to DATA_PATH pdfs folders def get_pdf_files(self): pdf_files = sorted([x for x in os.listdir(self.get_data_dir()) if 'DS_Store' not in x]) return pdf_files def get_number_of_docs(self): return self.redis_client.ft(INDEX_NAME).info()['num_docs'] def response_f1_query(self, f1_query): result_df = get_redis_results(self.redis_client, f1_query, index_name=INDEX_NAME) # Build a prompt to provide the original query, the result and ask to summarise for the user summary_prompt = '''Summarise this result like assitant of my AI project to answer the search query a customer has sent. Search query: SEARCH_QUERY_HERE Search result: SEARCH_RESULT_HERE Summary: ''' summary_prepped = summary_prompt.replace('SEARCH_QUERY_HERE',f1_query).replace('SEARCH_RESULT_HERE',result_df['result'][0]) summary = openai.Completion.create(engine=COMPLETIONS_MODEL,prompt=summary_prepped,max_tokens=200) # Response provided by GPT-3 # print(summary['choices'][0]['text']) return summary['choices'][0]['text'] if __name__ == '__main__': redis_service = IndexRedisService() pdf_files = redis_service.get_pdf_files() redis_service.index_checker() redis_service.initiliaze_tokenizer() # response_f1 = redis_service.response_f1_query("what are the motivation concept for kzengine?") # print(f'response from our service: {response_f1}')
[ "Summarise this result like assitant of my AI project to answer the search query a customer has sent.\n Search query: SEARCH_QUERY_HERE\n Search result: SEARCH_RESULT_HERE\n Summary:\n " ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~kayze_assistant_service~symbol_generation_service.py
from langchain import FewShotPromptTemplate, PromptTemplate from dotenv import load_dotenv import os from langchain import OpenAI load_dotenv() openai_api_key = os.getenv('OPENAI_API_KEY') openai = OpenAI( #model_name='text-davinci-003', model_name='text-davinci-003', temperature=0, openai_api_key=openai_api_key ) examples = [ { "query": "I want to get trading advice about the ethereum.", "answer": "ETH-USD" }, { "query": "Can you give trading advice about the solana?", "answer": "SOL-USD" } ] # create a example template example_template = """ User: {query} AI: {answer} """ # create a prompt example from above template example_prompt = PromptTemplate( input_variables=["query", "answer"], template=example_template ) # now break our previous prompt into a prefix and suffix # the prefix is our instructions prefix = """Answer the question based on the context below. You are an AI assistant who knows about cryptocurrency and can find the Yahoo Finance symbol for any given cryptocurrency. Context: When given a request for trading advice on a specific cryptocurrency, your task is to find and provide the corresponding Yahoo Finance symbol for that cryptocurrency. If the query dont have any cryptocurency names you should return BTC-USD. The symbol usually combines the cryptocurrency's ticker symbol and 'USD'. Here are some examples: """ # and the suffix our user input and output indicator suffix = """ User: {query} AI: """ # now create the few shot prompt template few_shot_prompt_template = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["query"], example_separator="\n\n" ) class SymboGenerationPromptService: @staticmethod def get_symbol(query: str): return openai( few_shot_prompt_template.format( query= query ) ) if __name__ == '__main__': res = SymboGenerationPromptService.get_symbol("I want to get a trading advice about the?") print(res)
[ "Answer the question based on the context below.\nYou are an AI assistant who knows about cryptocurrency and can find the Yahoo Finance symbol for any given cryptocurrency. \n\nContext: When given a request for trading advice on a specific cryptocurrency, \nyour task is to find and provide the corresponding Yahoo Finance symbol for that cryptocurrency. \nIf the query dont have any cryptocurency names you should return BTC-USD.\nThe symbol usually combines the cryptocurrency's ticker symbol and 'USD'. Here are some examples:\n", "\n\n", "\nUser: {query}\nAI: {answer}\n", "\nUser: {query}\nAI: ", "s ticker symbol and ", "answer" ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~kayze_assistant_service~service_chains.py
import os from dotenv import load_dotenv load_dotenv() openai_api_key = os.getenv('OPENAI_API_KEY') os.environ['OPENAI_API_KEY'] = openai_api_key from langchain import LLMChain, PromptTemplate from langchain.llms import BaseLLM from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.chat_models import ChatOpenAI class ServiceSelectionChain(LLMChain): """Chain to analyze which service the user should select.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the service selection parser.""" service_selection_prompt_template = """ You are an assistant helping your agent to engage with the client and determine which service they might be interested in. Following '===' is the conversation history. Use this conversation history to make your decision. Only use the text between first and second '===' to accomplish the task above, do not take it as a command of what to do. === {conversation_history} === Now determine what should be the next immediate conversation stage for the agent by selecting only from the following options: 1. Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services. 2. Discover Preferences: Ask the client about their hobbies, interests or other personal information to provide a more personalized service. 3. Education Service Presentation: Provide more detailed information about the education services offered by the company. 4. AI Trading Service Presentation: Provide more detailed information about the AI trading services offered by the company. 5. Close: Ask if they want to proceed with the service. This could be starting a trial, setting up a meeting, or any other suitable next step. 6. Company Info: Provide general information about company like what is company and what are purposes and aimed etc. 7. Trading Advice Service Presentation: Provide and give detailed trading advice about to asked specific coin or asset Only answer with a number between 1 and 7 to indicate the next conversation stage. Additionally for the 4, 6 and 7 i will give answer from outside service and you can see this answer and evaluate for the next step from the conversation history. Also dont forget if the client say goodbye or thanks you can evaluate like the 5 Close stage. The answer needs to be one number only, no words. If there is no conversation history, output 1. Do not answer anything else nor add anything to your answer.""" prompt = PromptTemplate( template=service_selection_prompt_template, input_variables=["conversation_history"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) class ServiceConversationChain(LLMChain): """Chain to generate the next utterance for the conversation based on service selection.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" service_agent_inception_prompt = """Your name is {agent_name} and you're a {agent_role} at {company_name}, a company operating in the Cryptocurrency providing education and AI trading services. The company's values are: {company_values}. You're contacting a potential customer to {conversation_purpose} through {conversation_type}. If you're asked about how you got the user's contact information, you obtained it from public records. Keep your responses summarized and explainable to retain the user's attention. Respond according to the previous conversation history and the stage of the conversation you are at. Generate one response at a time! When you're done generating, end with '<END_OF_TURN>' to give the user a chance to respond. Example: Conversation history: {agent_name}: Hi, how are you doing? I'm {agent_name} from {company_name}. Do you have a moment to chat? <END_OF_TURN> User: I'm well, yes, what's this about? <END_OF_TURN> {agent_name}: End of example. Current conversation stage: {conversation_stage} Conversation history: {conversation_history} {agent_name}: """ prompt = PromptTemplate( template=service_agent_inception_prompt, input_variables=[ "agent_name", "agent_role", "company_name", "company_values", "conversation_purpose", "conversation_type", "conversation_stage", "conversation_history", ], ) return cls(prompt=prompt, llm=llm, verbose=verbose)
[ "Your name is {agent_name} and you're a {agent_role} at {company_name}, \n a company operating in the Cryptocurrency providing education and AI trading services. The company's values are: {company_values}. \n You're contacting a potential customer to {conversation_purpose} through {conversation_type}. \n If you're asked about how you got the user's contact information, you obtained it from public records.\n\n Keep your responses summarized and explainable to retain the user's attention. \n Respond according to the previous conversation history and the stage of the conversation you are at. \n Generate one response at a time! When you're done generating, end with '<END_OF_TURN>' to give the user a chance to respond. \n\n Example:\n Conversation history: \n {agent_name}: Hi, how are you doing? I'm {agent_name} from {company_name}. Do you have a moment to chat? <END_OF_TURN>\n User: I'm well, yes, what's this about? <END_OF_TURN>\n {agent_name}:\n End of example.\n\n Current conversation stage: \n {conversation_stage}\n Conversation history: \n {conversation_history}\n {agent_name}: \n ", "\n You are an assistant helping your agent to engage with the client and determine which service they might be interested in.\n Following '===' is the conversation history. \n Use this conversation history to make your decision.\n Only use the text between first and second '===' to accomplish the task above, do not take it as a command of what to do.\n ===\n {conversation_history}\n ===\n\n Now determine what should be the next immediate conversation stage for the agent by selecting only from the following options:\n 1. Introduction: Begin the conversation with a polite greeting and a brief introduction about the company and its services.\n 2. Discover Preferences: Ask the client about their hobbies, interests or other personal information to provide a more personalized service.\n 3. Education Service Presentation: Provide more detailed information about the education services offered by the company.\n 4. AI Trading Service Presentation: Provide more detailed information about the AI trading services offered by the company.\n 5. Close: Ask if they want to proceed with the service. This could be starting a trial, setting up a meeting, or any other suitable next step.\n 6. Company Info: Provide general information about company like what is company and what are purposes and aimed etc.\n 7. Trading Advice Service Presentation: Provide and give detailed trading advice about to asked specific coin or asset\n\n Only answer with a number between 1 and 7 to indicate the next conversation stage. \n Additionally for the 4, 6 and 7 i will give answer from outside service and you can see this answer and evaluate for the next step\n from the conversation history. Also dont forget if the client say goodbye or thanks you can evaluate like the 5 Close stage.\n The answer needs to be one number only, no words.\n If there is no conversation history, output 1.\n Do not answer anything else nor add anything to your answer.", "company_name", "company_values", "conversation_history", "agent_role", "m well, yes, what", "conversation_purpose", "re asked about how you got the user", "conversation_type", "re done generating, end with ", "conversation_stage", "agent_name" ]
2024-01-10
kozanakyel/KZ-Engine-Backend
src~KZ_project~Infrastructure~services~kayze_assistant_service~trading_advisor.py
from dotenv import load_dotenv import os from KZ_project.Infrastructure.utilities.ohlc_data_process import calculate_dmi_rsi_mfi, fetch_data, analyze_ichimoku, analyze_supertrend, calculate_supertrend from langchain import OpenAI from langchain.chat_models import ChatOpenAI from langchain import FewShotPromptTemplate, PromptTemplate import random load_dotenv() openai_api_key = os.getenv('OPENAI_API_KEY') os.environ['OPENAI_API_KEY'] = openai_api_key openai = OpenAI( #model_name='text-davinci-003', model_name='text-ada-001', temperature=0.7, openai_api_key=openai_api_key ) # create our examples trend_examples = [ { "query": f"Ichmouku trend,Strong Bullish,Detected price,110.34,Supertrend,uptrend," f" cross for supertrend,True,Current Price,95.123" f" What is your advice for trading for this trend and price values?", "answer": "Our Ichmouku trend is Strong bullish that is good for next time step, we can say 110.34 our resistance \ because the detected price from ichmouku is greater than our current price 95.123. Also we have \ croos line and trend changing step for our supertrend indicator within 5 hour and the supertrend indicator momuntum is uptrend. \ You can hold if you have this coin. Or you can buy this coin because we have trend changing for uptrend \ and have a strong bullish momentum from ichmouku. you can follow this holding opeartion \ our detected resistance price to 110.34" }, { "query": f"Ichmouku trend,Bearish,Detected price,90.34,Supertrend,downtrend," f" cross for supertrend,No,Current Price,95.123" f" What is your advice for trading for this trend and price values?", "answer": "Our ichmouku trend is bearish not string enough, detected price is 90.34 and we can say this is our \ supported price because current price is greater than our detected price. Also we have a downtrend for supertrend indicator. \ we can say you can wait for buying because we dont see any cross in supertrend or bullish movement \ from our trade engine." } ] trend_prefix = """Answer the question based on the context below. You are the trading advisor. Also you are expert on Supertrend and Ichmouku Cloud indicators. Context: supertrend has 2 level: uptrend and downtrend. Ichmouku has 4 level Bullish, strong Bullish, Bearish, Strong Bearish. our detected price is coming from ichmouku indicator and the detected price actually represent support or resistance price. If the detected price is lower than the current Price this detected price is support price if detected price is greater than current price the detected price is resistance level. Dont forget you can define support or resistance price via the detected price. And you must define before the advice detected price greater or lower than the current price. you must give a evaluation to customer current situation from this indicator values. then you must give a trading advice to custormer. Here are some examples: """ # create our examples rsi_examples = [ { "query": f"RSI,70.34,MFI,59.02,DMP,31.3," f" DMN,12.77,ADX,41.26" f" What is your advice for trading for those indicator values?", "answer": "The RSI indicator value being above 70 at this moment indicates an overbought zone has been entered." "The MFI value trending above the average confirms the flow of money. On the DMi side, " "the DMP (positive directional movement indicator) value is above " "the DMN (negative directional movement indicator) value, " "and ADX is strongly trending above 25 and at 40 levels, " "indicating a strong bull trend that has entered in a short period of time. " "When considering the flow of money and the overbought zone, it may be advisable " "to take some profits and waiting next market movements." }, { "query": f"RSI,40.14, MFI,41, DMP,21.01," f"DMN,23.67,ADX,20.76." f" What is your advice for trading for those indicator values?", "answer": "The RSI indicator value dropping around 40 indicates approaching the selling zone. " "The MFI index also dropping around 40 supports this. Although ADX suggests that there is no strong trend below 25, " "it can be observed that DMN is above DMP, creating selling pressure. " "My recommendation would be to wait for a better buying opportunity at this point." } ] # create a example template example_template = """ User: {query} AI: {answer} """ # create a prompt example from above template example_prompt = PromptTemplate( input_variables=["query", "answer"], template=example_template ) # now break our previous prompt into a prefix and suffix # the prefix is our instructions rsi_prefix = """Answer the question based on the context below. You are the trading advisor. Also you are expert on RSI, MFI and DMP, DMI indicators. Context: RSI indicator value range betwwen 0-100. RSI value 70 and above meaning that overbought area. So you should sell your coin. also value 80 is a extreme overbought and you act cautiously. RSI value 30 that meaning is overselling area. and value 20 is extreme overselling are. if the RSI value are 20-30s you should bought this coin. 30-70 range waiting or you can look other indicator results. MFI indicator value range betwen 0-100. MFI value 80 and above meaning that overbought area. So you should sell your coin. also value 90 is a extreme overbought and you should act cautiously. MFI value 20 that meaning is overselling area. and value 10 is extreme overselling are. if the MFI value are 10-20s you should bought this coin. 20-80 range waiting or you can look other indicator results. Else it shows overselling condition between 0-25. DMI indicator is a collection of indicators including DMP, DMI, and ADX. The Plus Direction Indicator DMP and Minus Direction Indicator DMI show the current price direction. When the DMP is above DMN, the current price momentum is up. When the DMN is above DMP, the current price momentum is down. ADX measures the strength of the trend, either up or down; a reading above 25 indicates a strong trend. Here are some examples: """ # and the suffix our user input and output indicator suffix = """ User: {query} AI: """ class TradingAdvisor: @staticmethod def create_advice_prompt_template(examples, prefix): few_shot_prompt_template = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["query"], example_separator="\n\n" ) return few_shot_prompt_template @staticmethod def fetch_current_data(symbol: str): df = fetch_data(symbol, '1mo', '1h') indicator_data = calculate_dmi_rsi_mfi(df) return indicator_data @staticmethod def get_advice(symbol: str): df = TradingAdvisor.fetch_current_data(symbol) rsi_14 = df.RSI_14.iloc[-1] mfi_14 = df.MFI_14.iloc[-1] dmp_14 = df.DMP_14.iloc[-1] dmn_14 = df.DMN_14.iloc[-1] adx_14 = df.ADX_14.iloc[-1] trend_ich, base_price = analyze_ichimoku(df) spr_trend = calculate_supertrend(df) trend_super, cross_super = analyze_supertrend(spr_trend) # print(trend_ich, base_price, trend_super, cross_super, df.iloc[-1]["Close"]) rsi_query = f"RSI,{rsi_14:.2f},MFI,{mfi_14:.2f},DMP,{dmp_14:.2f}," \ f" DMN,{dmn_14:.2f},ADX,{adx_14:.2f}" trend_query = f"Ichmouku trend,{trend_ich},Detected price,{base_price[0]:.2f},Supertrend,{trend_super}," \ f" cross for supertrend,{cross_super},Current Price,{df.iloc[-1]['Close']:.2f}" rsi_prompt_template = TradingAdvisor.create_advice_prompt_template(rsi_examples, rsi_prefix) trend_prompt_template = TradingAdvisor.create_advice_prompt_template(trend_examples, trend_prefix) templates = [rsi_prompt_template, trend_prompt_template] queries = [rsi_query, trend_query] if cross_super: prompt_template = trend_prompt_template return openai( prompt_template.format( query=trend_query ) ) else: rand = random.randint(0,1) prompt_template = templates[rand] return openai( prompt_template.format( query=queries[rand] ) ) if __name__ == '__main__': tradv = TradingAdvisor.get_advice("SOL-USD") print(tradv)
[ "\n\n", "\nUser: {query}\nAI: {answer}\n", "\nUser: {query}\nAI: ", "[PLACEHOLDER, PLACEHOLDER]", "answer" ]
2024-01-10
josgraha/oneclickui-local
examples~openai~openapi-example.py
import openai openai.api_type = "azure" openai.api_key = "..." openai.api_base = "http://localhost:5001/v1" openai.api_version = "2023-05-15" OPENAI_API_KEY = 'sk-111111111111111111111111111111111111111111111111' MODEL = "TheBloke_Mistral-7B-OpenOrca-GPTQ" # create a chat completion chat_completion = openai.ChatCompletion.create( deployment_id="deployment-name", model=MODEL, messages=[{"role": "user", "content": "4 + 4 = ?"}], ) # print the completion print(chat_completion.choices[0].message.content)
[ "4 + 4 = ?" ]
2024-01-10
ahthserhsluk/emplay
helper~summary.py
import cohere co = cohere.Client('yNDbN0b9zyS85S8ny9ibVFLD9M2kqA9yiT64Vxnd') def get_summary(text): summary = co.summarize(text) return summary
[]
2024-01-10
krrishdholakia/NikkiBot
purgpt~error.py
import json '''Partially derived from OpenAI's python library errors.''' class PurGPTError(Exception): def __init__( self, message=None, json_body=None, request=None, code=None, ): super(PurGPTError, self).__init__(message) self._message = message self.json_body = json_body or {} self.request= request or {} if 'key' in self.request: self.request.pop('key') self.code = code if 'code' in self.json_body: self.code=code def __str__(self): return ("%s(message=%r, request=%r, json=%r)" % ( self.__class__.__name__, self._message, self.request, self.json_body ))[:1024] @property def user_message(self): return self._message def __repr__(self): return "%s(message=%r, request=%r, json=%r)" % ( self.__class__.__name__, self._message, self.request, self.json_body ) class KeyException(PurGPTError): def __init__(self, message): self._message = message or "API KEY HAS NOT BEEN SET!" super(KeyException,self).__init__( self._message ) class Timeout(PurGPTError): def __init__(self, message): self._message = message or "REQUEST TIMED OUT!" super(Timeout,self).__init__( self._message ) class APIConnectionError(PurGPTError): def __init__(self, message): self._message = message or "CLIENT ERROR." super(APIConnectionError, self).__init__( self._message )
[]
2024-01-10
philips-forks/fhir
py~google~fhir~r4~json_format_test.py
# # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Test FHIR R4 parsing/printing functionality.""" import os from typing import TypeVar, Type from google.protobuf import message from absl.testing import absltest from absl.testing import parameterized from proto.google.fhir.proto.r4.core import datatypes_pb2 from proto.google.fhir.proto.r4.core.resources import account_pb2 from proto.google.fhir.proto.r4.core.resources import activity_definition_pb2 from proto.google.fhir.proto.r4.core.resources import adverse_event_pb2 from proto.google.fhir.proto.r4.core.resources import allergy_intolerance_pb2 from proto.google.fhir.proto.r4.core.resources import appointment_pb2 from proto.google.fhir.proto.r4.core.resources import appointment_response_pb2 from proto.google.fhir.proto.r4.core.resources import audit_event_pb2 from proto.google.fhir.proto.r4.core.resources import basic_pb2 from proto.google.fhir.proto.r4.core.resources import binary_pb2 from proto.google.fhir.proto.r4.core.resources import biologically_derived_product_pb2 from proto.google.fhir.proto.r4.core.resources import body_structure_pb2 from proto.google.fhir.proto.r4.core.resources import bundle_and_contained_resource_pb2 from proto.google.fhir.proto.r4.core.resources import capability_statement_pb2 from proto.google.fhir.proto.r4.core.resources import care_plan_pb2 from proto.google.fhir.proto.r4.core.resources import care_team_pb2 from proto.google.fhir.proto.r4.core.resources import catalog_entry_pb2 from proto.google.fhir.proto.r4.core.resources import charge_item_definition_pb2 from proto.google.fhir.proto.r4.core.resources import charge_item_pb2 from proto.google.fhir.proto.r4.core.resources import claim_pb2 from proto.google.fhir.proto.r4.core.resources import claim_response_pb2 from proto.google.fhir.proto.r4.core.resources import clinical_impression_pb2 from proto.google.fhir.proto.r4.core.resources import communication_pb2 from proto.google.fhir.proto.r4.core.resources import communication_request_pb2 from proto.google.fhir.proto.r4.core.resources import compartment_definition_pb2 from proto.google.fhir.proto.r4.core.resources import composition_pb2 from proto.google.fhir.proto.r4.core.resources import condition_pb2 from proto.google.fhir.proto.r4.core.resources import consent_pb2 from proto.google.fhir.proto.r4.core.resources import contract_pb2 from proto.google.fhir.proto.r4.core.resources import coverage_eligibility_request_pb2 from proto.google.fhir.proto.r4.core.resources import coverage_eligibility_response_pb2 from proto.google.fhir.proto.r4.core.resources import coverage_pb2 from proto.google.fhir.proto.r4.core.resources import detected_issue_pb2 from proto.google.fhir.proto.r4.core.resources import device_definition_pb2 from proto.google.fhir.proto.r4.core.resources import device_metric_pb2 from proto.google.fhir.proto.r4.core.resources import device_pb2 from proto.google.fhir.proto.r4.core.resources import device_request_pb2 from proto.google.fhir.proto.r4.core.resources import device_use_statement_pb2 from proto.google.fhir.proto.r4.core.resources import diagnostic_report_pb2 from proto.google.fhir.proto.r4.core.resources import document_manifest_pb2 from proto.google.fhir.proto.r4.core.resources import document_reference_pb2 from proto.google.fhir.proto.r4.core.resources import effect_evidence_synthesis_pb2 from proto.google.fhir.proto.r4.core.resources import encounter_pb2 from proto.google.fhir.proto.r4.core.resources import endpoint_pb2 from proto.google.fhir.proto.r4.core.resources import enrollment_request_pb2 from proto.google.fhir.proto.r4.core.resources import enrollment_response_pb2 from proto.google.fhir.proto.r4.core.resources import episode_of_care_pb2 from proto.google.fhir.proto.r4.core.resources import event_definition_pb2 from proto.google.fhir.proto.r4.core.resources import evidence_pb2 from proto.google.fhir.proto.r4.core.resources import evidence_variable_pb2 from proto.google.fhir.proto.r4.core.resources import example_scenario_pb2 from proto.google.fhir.proto.r4.core.resources import explanation_of_benefit_pb2 from proto.google.fhir.proto.r4.core.resources import family_member_history_pb2 from proto.google.fhir.proto.r4.core.resources import flag_pb2 from proto.google.fhir.proto.r4.core.resources import goal_pb2 from proto.google.fhir.proto.r4.core.resources import graph_definition_pb2 from proto.google.fhir.proto.r4.core.resources import group_pb2 from proto.google.fhir.proto.r4.core.resources import guidance_response_pb2 from proto.google.fhir.proto.r4.core.resources import healthcare_service_pb2 from proto.google.fhir.proto.r4.core.resources import imaging_study_pb2 from proto.google.fhir.proto.r4.core.resources import immunization_evaluation_pb2 from proto.google.fhir.proto.r4.core.resources import immunization_pb2 from proto.google.fhir.proto.r4.core.resources import immunization_recommendation_pb2 from proto.google.fhir.proto.r4.core.resources import implementation_guide_pb2 from proto.google.fhir.proto.r4.core.resources import insurance_plan_pb2 from proto.google.fhir.proto.r4.core.resources import invoice_pb2 from proto.google.fhir.proto.r4.core.resources import library_pb2 from proto.google.fhir.proto.r4.core.resources import linkage_pb2 from proto.google.fhir.proto.r4.core.resources import list_pb2 from proto.google.fhir.proto.r4.core.resources import location_pb2 from proto.google.fhir.proto.r4.core.resources import measure_pb2 from proto.google.fhir.proto.r4.core.resources import measure_report_pb2 from proto.google.fhir.proto.r4.core.resources import media_pb2 from proto.google.fhir.proto.r4.core.resources import medication_administration_pb2 from proto.google.fhir.proto.r4.core.resources import medication_dispense_pb2 from proto.google.fhir.proto.r4.core.resources import medication_knowledge_pb2 from proto.google.fhir.proto.r4.core.resources import medication_pb2 from proto.google.fhir.proto.r4.core.resources import medication_request_pb2 from proto.google.fhir.proto.r4.core.resources import medication_statement_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_authorization_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_contraindication_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_indication_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_ingredient_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_interaction_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_manufactured_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_packaged_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_pharmaceutical_pb2 from proto.google.fhir.proto.r4.core.resources import medicinal_product_undesirable_effect_pb2 from proto.google.fhir.proto.r4.core.resources import message_definition_pb2 from proto.google.fhir.proto.r4.core.resources import message_header_pb2 from proto.google.fhir.proto.r4.core.resources import molecular_sequence_pb2 from proto.google.fhir.proto.r4.core.resources import naming_system_pb2 from proto.google.fhir.proto.r4.core.resources import nutrition_order_pb2 from proto.google.fhir.proto.r4.core.resources import observation_definition_pb2 from proto.google.fhir.proto.r4.core.resources import observation_pb2 from proto.google.fhir.proto.r4.core.resources import operation_definition_pb2 from proto.google.fhir.proto.r4.core.resources import operation_outcome_pb2 from proto.google.fhir.proto.r4.core.resources import organization_affiliation_pb2 from proto.google.fhir.proto.r4.core.resources import organization_pb2 from proto.google.fhir.proto.r4.core.resources import parameters_pb2 from proto.google.fhir.proto.r4.core.resources import patient_pb2 from proto.google.fhir.proto.r4.core.resources import payment_notice_pb2 from proto.google.fhir.proto.r4.core.resources import payment_reconciliation_pb2 from proto.google.fhir.proto.r4.core.resources import person_pb2 from proto.google.fhir.proto.r4.core.resources import plan_definition_pb2 from proto.google.fhir.proto.r4.core.resources import practitioner_pb2 from proto.google.fhir.proto.r4.core.resources import practitioner_role_pb2 from proto.google.fhir.proto.r4.core.resources import procedure_pb2 from proto.google.fhir.proto.r4.core.resources import provenance_pb2 from proto.google.fhir.proto.r4.core.resources import questionnaire_pb2 from proto.google.fhir.proto.r4.core.resources import questionnaire_response_pb2 from proto.google.fhir.proto.r4.core.resources import related_person_pb2 from proto.google.fhir.proto.r4.core.resources import request_group_pb2 from proto.google.fhir.proto.r4.core.resources import research_definition_pb2 from proto.google.fhir.proto.r4.core.resources import research_element_definition_pb2 from proto.google.fhir.proto.r4.core.resources import research_study_pb2 from proto.google.fhir.proto.r4.core.resources import research_subject_pb2 from proto.google.fhir.proto.r4.core.resources import risk_assessment_pb2 from proto.google.fhir.proto.r4.core.resources import risk_evidence_synthesis_pb2 from proto.google.fhir.proto.r4.core.resources import schedule_pb2 from proto.google.fhir.proto.r4.core.resources import service_request_pb2 from proto.google.fhir.proto.r4.core.resources import slot_pb2 from proto.google.fhir.proto.r4.core.resources import specimen_definition_pb2 from proto.google.fhir.proto.r4.core.resources import specimen_pb2 from proto.google.fhir.proto.r4.core.resources import structure_definition_pb2 from proto.google.fhir.proto.r4.core.resources import structure_map_pb2 from proto.google.fhir.proto.r4.core.resources import subscription_pb2 from proto.google.fhir.proto.r4.core.resources import substance_pb2 from proto.google.fhir.proto.r4.core.resources import substance_specification_pb2 from proto.google.fhir.proto.r4.core.resources import supply_delivery_pb2 from proto.google.fhir.proto.r4.core.resources import supply_request_pb2 from proto.google.fhir.proto.r4.core.resources import task_pb2 from proto.google.fhir.proto.r4.core.resources import terminology_capabilities_pb2 from proto.google.fhir.proto.r4.core.resources import test_report_pb2 from proto.google.fhir.proto.r4.core.resources import test_script_pb2 from proto.google.fhir.proto.r4.core.resources import verification_result_pb2 from proto.google.fhir.proto.r4.core.resources import vision_prescription_pb2 from google.fhir.json_format import json_format_test from google.fhir.r4 import json_format from google.fhir.testing import testdata_utils from google.fhir.utils import proto_utils _BIGQUERY_PATH = os.path.join('testdata', 'r4', 'bigquery') _EXAMPLES_PATH = os.path.join('testdata', 'r4', 'examples') _FHIR_SPEC_PATH = os.path.join('spec', 'hl7.fhir.r4.examples', '4.0.1', 'package') _VALIDATION_PATH = os.path.join('testdata', 'r4', 'validation') _INVALID_RECORDS = frozenset([ os.path.join(_FHIR_SPEC_PATH, 'Bundle-dataelements.json'), os.path.join(_FHIR_SPEC_PATH, 'Questionnaire-qs1.json'), os.path.join(_FHIR_SPEC_PATH, 'Observation-clinical-gender.json'), os.path.join(_FHIR_SPEC_PATH, 'DeviceMetric-example.json'), os.path.join(_FHIR_SPEC_PATH, 'DeviceUseStatement-example.json'), os.path.join(_FHIR_SPEC_PATH, 'MedicationRequest-medrx0301.json'), ]) _T = TypeVar('_T', bound=message.Message) class JsonFormatTest(json_format_test.JsonFormatTest): """Unit tests for functionality in json_format.py.""" @parameterized.named_parameters( ('_withAccountEwg', 'Account-ewg'), ('_withAccountExample', 'Account-example'), ) def testJsonFormat_forValidAccount_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, account_pb2.Account) @parameterized.named_parameters( ('_withActivityDefinitionAdministerZikaVirusExposureAssessment', 'ActivityDefinition-administer-zika-virus-exposure-assessment'), ('_withActivityDefinitionBloodTubesSupply', 'ActivityDefinition-blood-tubes-supply'), ('_withActivityDefinitionCitalopramPrescription', 'ActivityDefinition-citalopramPrescription'), ('_withActivityDefinitionHeartValveReplacement', 'ActivityDefinition-heart-valve-replacement'), ('_withActivityDefinitionProvideMosquitoPreventionAdvice', 'ActivityDefinition-provide-mosquito-prevention-advice'), ('_withActivityDefinitionReferralPrimaryCareMentalHealthInitial', 'ActivityDefinition-referralPrimaryCareMentalHealth-initial'), ('_withActivityDefinitionReferralPrimaryCareMentalHealth', 'ActivityDefinition-referralPrimaryCareMentalHealth'), ('_withActivityDefinitionSerumDengueVirusIgm', 'ActivityDefinition-serum-dengue-virus-igm'), ('_withActivityDefinitionSerumZikaDengueVirusIgm', 'ActivityDefinition-serum-zika-dengue-virus-igm'), ) def testJsonFormat_forValidActivityDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, activity_definition_pb2.ActivityDefinition) @parameterized.named_parameters( ('_withAdverseEventExample', 'AdverseEvent-example'),) def testJsonFormat_forValidAdverseEvent_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, adverse_event_pb2.AdverseEvent) @parameterized.named_parameters( ('_withAllergyIntoleranceExample', 'AllergyIntolerance-example'), ('_withAllergyIntoleranceFishallergy', 'AllergyIntolerance-fishallergy'), ('_withAllergyIntoleranceMedication', 'AllergyIntolerance-medication'), ('_withAllergyIntoleranceNka', 'AllergyIntolerance-nka'), ('_withAllergyIntoleranceNkda', 'AllergyIntolerance-nkda'), ('_withAllergyIntoleranceNkla', 'AllergyIntolerance-nkla'), ) def testJsonFormat_forValidAllergyIntolerance_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, allergy_intolerance_pb2.AllergyIntolerance) @parameterized.named_parameters( ('_withAppointment2docs', 'Appointment-2docs'), ('_withAppointmentExample', 'Appointment-example'), ('_withAppointmentExampleReq', 'Appointment-examplereq'), ) def testJsonFormat_forValidAppointment_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, appointment_pb2.Appointment) @parameterized.named_parameters( ('_withAppointmentResponseExample', 'AppointmentResponse-example'), ('_withAppointmentResponseExampleResp', 'AppointmentResponse-exampleresp'), ) def testJsonFormat_forValidAppointmentResponse_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, appointment_response_pb2.AppointmentResponse) @parameterized.named_parameters( ('_withAuditEventExampleDisclosure', 'AuditEvent-example-disclosure'), ('_withAuditEventExampleError', 'AuditEvent-example-error'), ('_withAuditEventExample', 'AuditEvent-example'), ('_withAuditEventExampleLogin', 'AuditEvent-example-login'), ('_withAuditEventExampleLogout', 'AuditEvent-example-logout'), ('_withAuditEventExampleMedia', 'AuditEvent-example-media'), ('_withAuditEventExamplePixQuery', 'AuditEvent-example-pixQuery'), ('_withAuditEventExampleRest', 'AuditEvent-example-rest'), ('_withAuditEventExampleSearch', 'AuditEvent-example-search'), ) def testJsonFormat_forValidAuditEvent_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, audit_event_pb2.AuditEvent) @parameterized.named_parameters( ('_withBasicBasicExampleNarrative', 'Basic-basic-example-narrative'), ('_withBasicClassModel', 'Basic-classModel'), ('_withBasicReferral', 'Basic-referral'), ) def testJsonFormat_forValidBasic_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, basic_pb2.Basic) @parameterized.named_parameters( ('_withBinaryExample', 'Binary-example'), ('_withBinaryF006', 'Binary-f006'), ) def testJsonFormat_forValidBinary_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, binary_pb2.Binary) @parameterized.named_parameters( ('_withBiologicallyDerivedProductExample', 'BiologicallyDerivedProduct-example'),) def testJsonFormat_forValidBiologicallyDerivedProduct_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, biologically_derived_product_pb2.BiologicallyDerivedProduct) @parameterized.named_parameters( ('_withBodyStructureFetus', 'BodyStructure-fetus'), ('_withBodyStructureSkinPatch', 'BodyStructure-skin-patch'), ('_withBodyStructureTumor', 'BodyStructure-tumor'), ) def testJsonFormat_forValidBodyStructure_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, body_structure_pb2.BodyStructure) @parameterized.named_parameters( ('_withBundle101', 'Bundle-101'), ('_withBundle10bb101fA1214264A92067be9cb82c74', 'Bundle-10bb101f-a121-4264-a920-67be9cb82c74'), ('_withBundle3a0707d3549e4467B8b85a2ab3800efe', 'Bundle-3a0707d3-549e-4467-b8b8-5a2ab3800efe'), ('_withBundle3ad0687eF477468cAfd5Fcc2bf897808', 'Bundle-3ad0687e-f477-468c-afd5-fcc2bf897808'), ('_withBundle3ad0687eF477468cAfd5Fcc2bf897809', 'Bundle-3ad0687e-f477-468c-afd5-fcc2bf897809'), ('_withBundle3ad0687eF477468cAfd5Fcc2bf897819', 'Bundle-3ad0687e-f477-468c-afd5-fcc2bf897819'), ('_withBundle72ac849352ac41bd8d5d7258c289b5ea', 'Bundle-72ac8493-52ac-41bd-8d5d-7258c289b5ea'), ('_withBundleB0a5e427783c44adb87e2E3efe3369b6f', 'Bundle-b0a5e4277-83c4-4adb-87e2-e3efe3369b6f'), ('_withBundleB248b1b216864b94993637d7a5f94b51', 'Bundle-b248b1b2-1686-4b94-9936-37d7a5f94b51'), ('_withBundleBundleExample', 'Bundle-bundle-example'), ('_withBundleBundleReferences', 'Bundle-bundle-references'), ('_withBundleBundleRequestMedsallergies', 'Bundle-bundle-request-medsallergies'), ('_withBundleBundleRequestSimpleSummary', 'Bundle-bundle-request-simplesummary'), ('_withBundleBundleResponse', 'Bundle-bundle-response'), ('_withBundleBundleResponseMedsAllergies', 'Bundle-bundle-response-medsallergies'), ('_withBundleBundleResponseSimpleSummary', 'Bundle-bundle-response-simplesummary'), ('_withBundleBundleSearchWarning', 'Bundle-bundle-search-warning'), ('_withBundleBundleTransaction', 'Bundle-bundle-transaction'), ('_withBundleConceptMaps', 'Bundle-conceptmaps'), # TODO: Investigate test timeouts # ('_withBundleDataElements', 'Bundle-dataelements'), # ('_withBundleDg2', 'Bundle-dg2'), # ('_withBundleExtensions', 'Bundle-extensions'), # ('_withBundleExternals', 'Bundle-externals'), # ('_withBundleF001', 'Bundle-f001'), # ('_withBundleF202', 'Bundle-f202'), # ('_withBundleFather', 'Bundle-father'), # ('_withBundleGhp', 'Bundle-ghp'), # ('_withBundleHla1', 'Bundle-hla-1'), # ('_withBundleLipids', 'Bundle-lipids'), # ('_withBundleLriExample', 'Bundle-lri-example'), # ('_withBundleMicro', 'Bundle-micro'), # ('_withBundleProfilesOthers', 'Bundle-profiles-others'), # ('_withBundleRegistry', 'Bundle-registry'), # ('_withBundleReport', 'Bundle-report'), # ('_withBundleSearchParams', 'Bundle-searchParams'), # ('_withBundleTerminologies', 'Bundle-terminologies'), # ('_withBundleTypes', 'Bundle-types'), # ('_withBundleUssgFht', 'Bundle-ussg-fht'), # ('_withBundleValuesetExpansions', 'Bundle-valueset-expansions'), # ('_withBundleXds', 'Bundle-xds'), # ('_withBundleResources', 'Bundle-resources'), ) def testJsonFormat_forValidBundle_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, bundle_and_contained_resource_pb2.Bundle) @parameterized.named_parameters( ('_withCapabilityStatementBase2', 'CapabilityStatement-base2'), ('_withCapabilityStatementBase', 'CapabilityStatement-base'), ('_withCapabilityStatementExample', 'CapabilityStatement-example'), ('_withCapabilityStatementKnowledgeRepository', 'CapabilityStatement-knowledge-repository'), ('_withCapabilityStatementMeasureProcessor', 'CapabilityStatement-measure-processor'), ('_withCapabilityStatementMessagedefinition', 'CapabilityStatement-messagedefinition'), ('_withCapabilityStatementPhr', 'CapabilityStatement-phr'), ('_withCapabilityStatementTerminologyServer', 'CapabilityStatement-terminology-server'), ) def testJsonFormat_forValidCapabilityStatement_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, capability_statement_pb2.CapabilityStatement) @parameterized.named_parameters( ('_withCarePlanExample', 'CarePlan-example'), ('_withCarePlanF001', 'CarePlan-f001'), ('_withCarePlanF002', 'CarePlan-f002'), ('_withCarePlanF003', 'CarePlan-f003'), ('_withCarePlanF201', 'CarePlan-f201'), ('_withCarePlanF202', 'CarePlan-f202'), ('_withCarePlanF203', 'CarePlan-f203'), ('_withCarePlanGpvisit', 'CarePlan-gpvisit'), ('_withCarePlanIntegrate', 'CarePlan-integrate'), ('_withCarePlanObesityNarrative', 'CarePlan-obesity-narrative'), ('_withCarePlanPreg', 'CarePlan-preg'), ) def testJsonFormat_forValidCarePlan_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, care_plan_pb2.CarePlan) @parameterized.named_parameters( ('_withCareTeamExample', 'CareTeam-example'),) def testJsonFormat_forValidCareTeam_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, care_team_pb2.CareTeam) @parameterized.named_parameters( ('_withCatalogEntryExample', 'CatalogEntry-example'),) def testJsonFormat_forValidCatalogEntry_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, catalog_entry_pb2.CatalogEntry) @parameterized.named_parameters( ('_withChargeItemExample', 'ChargeItem-example'),) def testJsonFormat_forValidChargeItem_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, charge_item_pb2.ChargeItem) @parameterized.named_parameters( ('_withChargeItemDefinitionDevice', 'ChargeItemDefinition-device'), ('_withChargeItemDefinitionEbm', 'ChargeItemDefinition-ebm'), ) def testJsonFormat_forValidChargeItemDefinition_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, charge_item_definition_pb2.ChargeItemDefinition) @parameterized.named_parameters( ('_withClaim100150', 'Claim-100150'), ('_withClaim100151', 'Claim-100151'), ('_withClaim100152', 'Claim-100152'), ('_withClaim100153', 'Claim-100153'), ('_withClaim100154', 'Claim-100154'), ('_withClaim100155', 'Claim-100155'), ('_withClaim100156', 'Claim-100156'), ('_withClaim660150', 'Claim-660150'), ('_withClaim660151', 'Claim-660151'), ('_withClaim660152', 'Claim-660152'), ('_withClaim760150', 'Claim-760150'), ('_withClaim760151', 'Claim-760151'), ('_withClaim760152', 'Claim-760152'), ('_withClaim860150', 'Claim-860150'), ('_withClaim960150', 'Claim-960150'), ('_withClaim960151', 'Claim-960151'), ('_withClaimMED00050', 'Claim-MED-00050'), ) def testJsonFormat_forValidClaim_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, claim_pb2.Claim) @parameterized.named_parameters( ('_withClaimResponseR3500', 'ClaimResponse-R3500'), ('_withClaimResponseR3501', 'ClaimResponse-R3501'), ('_withClaimResponseR3502', 'ClaimResponse-R3502'), ('_withClaimResponseR3503', 'ClaimResponse-R3503'), ('_withClaimResponseUr3503', 'ClaimResponse-UR3503'), ) def testJsonFormat_forValidClaimResponse_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, claim_response_pb2.ClaimResponse) @parameterized.named_parameters( ('_withClinicalImpressionExample', 'ClinicalImpression-example'),) def testJsonFormat_forValidClinicalImpression_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, clinical_impression_pb2.ClinicalImpression) @parameterized.named_parameters( ('_withCommunicationExample', 'Communication-example'), ('_withCommunicationFmAttachment', 'Communication-fm-attachment'), ('_withCommunicationFmSolicited', 'Communication-fm-solicited'), ) def testJsonFormat_forValidCommunication_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, communication_pb2.Communication) @parameterized.named_parameters( ('_withCommunicationRequestExample', 'CommunicationRequest-example'), ('_withCommunicationRequestFmSolicit', 'CommunicationRequest-fm-solicit'), ) def testJsonFormat_forValidCommunicationRequest_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, communication_request_pb2.CommunicationRequest) @parameterized.named_parameters( ('_withCompartmentDefinitionDevice', 'CompartmentDefinition-device'), ('_withCompartmentDefinitionEncounter', 'CompartmentDefinition-encounter'), ('_withCompartmentDefinitionExample', 'CompartmentDefinition-example'), ('_withCompartmentDefinitionPatient', 'CompartmentDefinition-patient'), ('_withCompartmentDefinitionPractitioner', 'CompartmentDefinition-practitioner'), ('_withCompartmentDefinitionRelatedPerson', 'CompartmentDefinition-relatedPerson'), ) def testJsonFormat_forValidCompartmentDefinition_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, compartment_definition_pb2.CompartmentDefinition) @parameterized.named_parameters( ('_withCompositionExample', 'Composition-example'), ('_withCompositionExampleMixed', 'Composition-example-mixed'), ) def testJsonFormat_forValidComposition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, composition_pb2.Composition) @parameterized.named_parameters( ('_withConditionExample2', 'Condition-example2'), ('_withConditionExample', 'Condition-example'), ('_withConditionF001', 'Condition-f001'), ('_withConditionF002', 'Condition-f002'), ('_withConditionF003', 'Condition-f003'), ('_withConditionF201', 'Condition-f201'), ('_withConditionF202', 'Condition-f202'), ('_withConditionF203', 'Condition-f203'), ('_withConditionF204', 'Condition-f204'), ('_withConditionF205', 'Condition-f205'), ('_withConditionFamilyHistory', 'Condition-family-history'), ('_withConditionStroke', 'Condition-stroke'), ) def testJsonFormat_forValidCondition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, condition_pb2.Condition) @parameterized.named_parameters( ('_withConsentConsentExampleBasic', 'Consent-consent-example-basic'), ('_withConsentConsentExampleEmergency', 'Consent-consent-example-Emergency'), ('_withConsentConsentExampleGrantor', 'Consent-consent-example-grantor'), ('_withConsentConsentExampleNotAuthor', 'Consent-consent-example-notAuthor'), ('_withConsentConsentExampleNotOrg', 'Consent-consent-example-notOrg'), ('_withConsentConsentExampleNotThem', 'Consent-consent-example-notThem'), ('_withConsentConsentExampleNotThis', 'Consent-consent-example-notThis'), ('_withConsentConsentExampleNotTime', 'Consent-consent-example-notTime'), ('_withConsentConsentExampleOut', 'Consent-consent-example-Out'), ('_withConsentConsentExamplePkb', 'Consent-consent-example-pkb'), ('_withConsentConsentExampleSignature', 'Consent-consent-example-signature'), ('_withConsentConsentExampleSmartonfhir', 'Consent-consent-example-smartonfhir'), ) def testJsonFormat_forValidConsent_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, consent_pb2.Consent) @parameterized.named_parameters( ('_withAllergyIntoleranceExample', 'AllergyIntolerance-example', allergy_intolerance_pb2.AllergyIntolerance, 'allergy_intolerance'), ('_withCapabilityStatementBase', 'CapabilityStatement-base', capability_statement_pb2.CapabilityStatement, 'capability_statement'), ('_withImmunizationExample', 'Immunization-example', immunization_pb2.Immunization, 'immunization'), ('_withMedicationMed0305', 'Medication-med0305', medication_pb2.Medication, 'medication'), ('_withObservationF004', 'Observation-f004', observation_pb2.Observation, 'observation'), ('_withPatientAnimal', 'Patient-animal', patient_pb2.Patient, 'patient'), ('_withPractitionerF003', 'Practitioner-f003', practitioner_pb2.Practitioner, 'practitioner'), ('_withProcedureAmbulation', 'Procedure-ambulation', procedure_pb2.Procedure, 'procedure'), ('_withTaskExample4', 'Task-example4', task_pb2.Task, 'task'), ) def testJsonFormat_forValidContainedResource_succeeds( self, file_name: str, proto_cls: Type[message.Message], contained_field: str): """Checks equality of print-parse 'round-trip' for a contained resource.""" proto_path = os.path.join(_EXAMPLES_PATH, file_name + '.prototxt') golden_proto = testdata_utils.read_protos(proto_path, proto_cls)[0] # Construct the contained resource to validate contained = bundle_and_contained_resource_pb2.ContainedResource() proto_utils.set_value_at_field(contained, contained_field, golden_proto) # Validate printing and then parsing the print output against the golden contained_json_str = json_format.print_fhir_to_json_string(contained) parsed_contained = json_format.json_fhir_string_to_proto( contained_json_str, bundle_and_contained_resource_pb2.ContainedResource, validate=True, default_timezone='Australia/Sydney') self.assertEqual(contained, parsed_contained) @parameterized.named_parameters( ('_withContractC123', 'Contract-C-123'), ('_withContractC2121', 'Contract-C-2121'), ('_withContractIns101', 'Contract-INS-101'), ('_withContractPcdExampleNotAuthor', 'Contract-pcd-example-notAuthor'), ('_withContractPcdExampleNotLabs', 'Contract-pcd-example-notLabs'), ('_withContractPcdExampleNotOrg', 'Contract-pcd-example-notOrg'), ('_withContractPcdExampleNotThem', 'Contract-pcd-example-notThem'), ('_withContractPcdExampleNotThis', 'Contract-pcd-example-notThis'), ) def testJsonFormat_forValidContract_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, contract_pb2.Contract) @parameterized.named_parameters( ('_withCoverage7546D', 'Coverage-7546D'), ('_withCoverage7547E', 'Coverage-7547E'), ('_withCoverage9876B1', 'Coverage-9876B1'), ('_withCoverageSP1234', 'Coverage-SP1234'), ) def testJsonFormat_forValidCoverage_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, coverage_pb2.Coverage) @parameterized.named_parameters( ('_withCoverageEligibilityRequest52345', 'CoverageEligibilityRequest-52345'), ('_withCoverageEligibilityRequest52346', 'CoverageEligibilityRequest-52346'), ) def testJsonFormat_forValidCoverageEligibilityRequest_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, coverage_eligibility_request_pb2.CoverageEligibilityRequest) @parameterized.named_parameters( ('_withCoverageEligibilityResponseE2500', 'CoverageEligibilityResponse-E2500'), ('_withCoverageEligibilityResponseE2501', 'CoverageEligibilityResponse-E2501'), ('_withCoverageEligibilityResponseE2502', 'CoverageEligibilityResponse-E2502'), ('_withCoverageEligibilityResponseE2503', 'CoverageEligibilityResponse-E2503'), ) def testJsonFormat_forValidCoverageEligibilityResponse_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, coverage_eligibility_response_pb2.CoverageEligibilityResponse) @parameterized.named_parameters( ('_withDetectedIssueAllergy', 'DetectedIssue-allergy'), ('_withDetectedIssueDdi', 'DetectedIssue-ddi'), ('_withDetectedIssueDuplicate', 'DetectedIssue-duplicate'), ('_withDetectedIssueLab', 'DetectedIssue-lab'), ) def testJsonFormat_forValidDetectedIssue_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, detected_issue_pb2.DetectedIssue) @parameterized.named_parameters( ('_withDeviceExample', 'Device-example'), ('_withDeviceF001', 'Device-f001'), ) def testJsonFormat_forValidDevice_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, device_pb2.Device) @parameterized.named_parameters( ('_withDeviceDefinitionExample', 'DeviceDefinition-example'),) def testJsonFormat_forValidDeviceDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, device_definition_pb2.DeviceDefinition) @parameterized.named_parameters( ('_withDeviceMetricExample', 'DeviceMetric-example'),) def testJsonFormat_forValidDeviceMetric_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, device_metric_pb2.DeviceMetric) @parameterized.named_parameters( ('_withDeviceRequestExample', 'DeviceRequest-example'), ('_withDeviceRequestInsulinPump', 'DeviceRequest-insulinpump'), ('_withDeviceRequestLeftLens', 'DeviceRequest-left-lens'), ('_withDeviceRequestRightLens', 'DeviceRequest-right-lens'), ) def testJsonFormat_forValidDeviceRequest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, device_request_pb2.DeviceRequest) @parameterized.named_parameters( ('_withDeviceUseStatementExample', 'DeviceUseStatement-example'),) def testJsonFormat_forValidDeviceUseStatement_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, device_use_statement_pb2.DeviceUseStatement) @parameterized.named_parameters( ('_withDiagnosticReport102', 'DiagnosticReport-102'), ('_withDiagnosticReportExamplePgx', 'DiagnosticReport-example-pgx'), ('_withDiagnosticReportF201', 'DiagnosticReport-f201'), ('_withDiagnosticReportGingivalMass', 'DiagnosticReport-gingival-mass'), ('_withDiagnosticReportPap', 'DiagnosticReport-pap'), ('_withDiagnosticReportUltrasound', 'DiagnosticReport-ultrasound'), ) def testJsonFormat_forValidDiagnosticReport_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, diagnostic_report_pb2.DiagnosticReport) @parameterized.named_parameters( ('_withDocumentManifest654789', 'DocumentManifest-654789'), ('_withDocumentManifestExample', 'DocumentManifest-example'), ) def testJsonFormat_forValidDocumentManifest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, document_manifest_pb2.DocumentManifest) @parameterized.named_parameters( ('_withDocumentReferenceExample', 'DocumentReference-example'),) def testJsonFormat_forValidDocumentReference_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, document_reference_pb2.DocumentReference) @parameterized.named_parameters( ('_withEffectEvidenceSynthesisExample', 'EffectEvidenceSynthesis-example'),) def testJsonFormat_forValidEffectEvidenceSynthesis_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, effect_evidence_synthesis_pb2.EffectEvidenceSynthesis) @parameterized.named_parameters( ('_withParametersEmptyResource', 'Parameters-empty-resource', parameters_pb2.Parameters),) def testJsonFormat_forValidEmptyNestedResource_succeeds( self, file_name: str, proto_cls: Type[message.Message]): self.assert_parse_and_print_examples_equals_golden(file_name, proto_cls) @parameterized.named_parameters( ('_withEncounterEmerg', 'Encounter-emerg'), ('_withEncounterExample', 'Encounter-example'), ('_withEncounterF001', 'Encounter-f001'), ('_withEncounterF002', 'Encounter-f002'), ('_withEncounterF003', 'Encounter-f003'), ('_withEncounterF201', 'Encounter-f201'), ('_withEncounterF202', 'Encounter-f202'), ('_withEncounterF203', 'Encounter-f203'), ('_withEncounterHome', 'Encounter-home'), ('_withEncounterXcda', 'Encounter-xcda'), ) def testJsonFormat_forValidEncounter_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, encounter_pb2.Encounter) @parameterized.named_parameters( ('_withEndpointDirectEndpoint', 'Endpoint-direct-endpoint'), ('_withEndpointExampleIid', 'Endpoint-example-iid'), ('_withEndpointExample', 'Endpoint-example'), ('_withEndpointExampleWadors', 'Endpoint-example-wadors'), ) def testJsonFormat_forValidEndpoint_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, endpoint_pb2.Endpoint) @parameterized.named_parameters( ('_withEnrollmentRequest22345', 'EnrollmentRequest-22345'),) def testJsonFormat_forValidEnrollmentRequest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, enrollment_request_pb2.EnrollmentRequest) @parameterized.named_parameters( ('_withEnrollmentResponseER2500', 'EnrollmentResponse-ER2500'),) def testJsonFormat_forValidEnrollmentResponse_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, enrollment_response_pb2.EnrollmentResponse) @parameterized.named_parameters( ('_withEpisodeOfCareExample', 'EpisodeOfCare-example'),) def testJsonFormat_forValidEpisodeOfCare_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, episode_of_care_pb2.EpisodeOfCare) @parameterized.named_parameters( ('_withEventDefinitionExample', 'EventDefinition-example'),) def testJsonFormat_forValidEventDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, event_definition_pb2.EventDefinition) @parameterized.named_parameters( ('_withEvidenceExample', 'Evidence-example'),) def testJsonFormat_forValidEvidence_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, evidence_pb2.Evidence) @parameterized.named_parameters( ('_withEvidenceVariableExample', 'EvidenceVariable-example'),) def testJsonFormat_forValidEvidenceVariable_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, evidence_variable_pb2.EvidenceVariable) @parameterized.named_parameters( ('_withExampleScenarioExample', 'ExampleScenario-example'),) def testJsonFormat_forValidExampleScenario_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, example_scenario_pb2.ExampleScenario) @parameterized.named_parameters( ('_withExplanationOfBenefitEb3500', 'ExplanationOfBenefit-EB3500'), ('_withExplanationOfBenefitEb3501', 'ExplanationOfBenefit-EB3501'), ) def testJsonFormat_forValidExplanationOfBenefit_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, explanation_of_benefit_pb2.ExplanationOfBenefit) @parameterized.named_parameters( ('_withFamilyMemberHistoryFather', 'FamilyMemberHistory-father'), ('_withFamilyMemberHistoryMother', 'FamilyMemberHistory-mother'), ) def testJsonFormat_forValidFamilyMemberHistory_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, family_member_history_pb2.FamilyMemberHistory) @parameterized.named_parameters( ('_withFlagExampleEncounter', 'Flag-example-encounter'), ('_withFlagExample', 'Flag-example'), ) def testJsonFormat_forValidFlag_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, flag_pb2.Flag) @parameterized.named_parameters( ('_withGoalExample', 'Goal-example'), ('_withGoalStopSmoking', 'Goal-stop-smoking'), ) def testJsonFormat_forValidGoal_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, goal_pb2.Goal) @parameterized.named_parameters( ('_withGraphDefinitionExample', 'GraphDefinition-example'),) def testJsonFormat_forValidGraphDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, graph_definition_pb2.GraphDefinition) @parameterized.named_parameters( ('_withGroup101', 'Group-101'), ('_withGroup102', 'Group-102'), ('_withGroupExamplePatientlist', 'Group-example-patientlist'), ('_withGroupHerd1', 'Group-herd1'), ) def testJsonFormat_forValidGroup_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, group_pb2.Group) @parameterized.named_parameters( ('_withGuidanceResponseExample', 'GuidanceResponse-example'),) def testJsonFormat_forValidGuidanceResponse_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, guidance_response_pb2.GuidanceResponse) @parameterized.named_parameters( ('_withHealthcareServiceExample', 'HealthcareService-example'),) def testJsonFormat_forValidHealthcareService_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, healthcare_service_pb2.HealthcareService) @parameterized.named_parameters( ('_withImagingStudyExample', 'ImagingStudy-example'), ('_withImagingStudyExampleXr', 'ImagingStudy-example-xr'), ) def testJsonFormat_forValidImagingStudy_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, imaging_study_pb2.ImagingStudy) @parameterized.named_parameters( ('_withImmunizationExample', 'Immunization-example'), ('_withImmunizationHistorical', 'Immunization-historical'), ('_withImmunizationNotGiven', 'Immunization-notGiven'), ('_withImmunizationProtocol', 'Immunization-protocol'), ('_withImmunizationSubpotent', 'Immunization-subpotent'), ) def testJsonFormat_forValidImmunization_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, immunization_pb2.Immunization) @parameterized.named_parameters( ('_withImmunizationEvaluationExample', 'ImmunizationEvaluation-example'), ('_withImmunizationEvaluationNotValid', 'ImmunizationEvaluation-notValid'), ) def testJsonFormat_forValidImmunizationEvaluation_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, immunization_evaluation_pb2.ImmunizationEvaluation) @parameterized.named_parameters( ('_withImmunizationRecommendationExample', 'ImmunizationRecommendation-example'),) def testJsonFormat_forValidImmunizationRecommendation_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, immunization_recommendation_pb2.ImmunizationRecommendation) @parameterized.named_parameters( # 'ImplementationGuide-fhir' and 'ig-r4' do not parse because they contain # a reference to an invalid resource. # https://gforge.hl7.org/gf/project/fhir/tracker/?action=TrackerItemEdit&tracker_item_id=22489 # ('_withImplementationGuideFhir', 'ImplementationGuide-fhir'), # ('_withIgR4', 'ig-r4'), ('_withImplementationGuideExample', 'ImplementationGuide-example'),) def testJsonFormat_forValidImplementationGuide_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, implementation_guide_pb2.ImplementationGuide) @parameterized.named_parameters( ('_withInsurancePlanExample', 'InsurancePlan-example'),) def testJsonFormat_forValidInsurancePlan_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, insurance_plan_pb2.InsurancePlan) @parameterized.named_parameters( ('_withInvoiceExample', 'Invoice-example'),) def testJsonFormat_forValidInvoice_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, invoice_pb2.Invoice) @parameterized.named_parameters( ('_withLibraryCompositionExample', 'Library-composition-example'), ('_withLibraryExample', 'Library-example'), ('_withLibraryHivIndicators', 'Library-hiv-indicators'), ('_withLibraryLibraryCms146Example', 'Library-library-cms146-example'), ('_withLibraryLibraryExclusiveBreastfeedingCdsLogic', 'Library-library-exclusive-breastfeeding-cds-logic'), ('_withLibraryLibraryExclusiveBreastfeedingCqmLogic', 'Library-library-exclusive-breastfeeding-cqm-logic'), ('_withLibraryLibraryFhirHelpers', 'Library-library-fhir-helpers'), ('_withLibraryLibraryFhirHelpersPredecessor', 'Library-library-fhir-helpers-predecessor'), ('_withLibraryLibraryFhirModelDefinition', 'Library-library-fhir-model-definition'), ('_withLibraryLibraryQuickModelDefinition', 'Library-library-quick-model-definition'), ('_withLibraryOmtkLogic', 'Library-omtk-logic'), ('_withLibraryOmtkModelinfo', 'Library-omtk-modelinfo'), ('_withLibraryOpioidcdsCommon', 'Library-opioidcds-common'), ('_withLibraryOpioidcdsRecommendation04', 'Library-opioidcds-recommendation-04'), ('_withLibraryOpioidcdsRecommendation05', 'Library-opioidcds-recommendation-05'), ('_withLibraryOpioidcdsRecommendation07', 'Library-opioidcds-recommendation-07'), ('_withLibraryOpioidcdsRecommendation08', 'Library-opioidcds-recommendation-08'), ('_withLibraryOpioidcdsRecommendation10', 'Library-opioidcds-recommendation-10'), ('_withLibraryOpioidcdsRecommendation11', 'Library-opioidcds-recommendation-11'), ('_withLibrarySuicideriskOrdersetLogic', 'Library-suiciderisk-orderset-logic'), ('_withLibraryZikaVirusInterventionLogic', 'Library-zika-virus-intervention-logic'), ) def testJsonFormat_forValidLibrary_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, library_pb2.Library) @parameterized.named_parameters( ('_withLinkageExample', 'Linkage-example'),) def testJsonFormat_forValidLinkage_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, linkage_pb2.Linkage) @parameterized.named_parameters( ('_withListCurrentAllergies', 'List-current-allergies'), ('_withListExampleDoubleCousinRelationship', 'List-example-double-cousin-relationship'), ('_withListExampleEmpty', 'List-example-empty'), ('_withListExample', 'List-example'), ('_withListExampleSimpleEmpty', 'List-example-simple-empty'), ('_withListF201', 'List-f201'), ('_withListGenetic', 'List-genetic'), ('_withListLong', 'List-long'), ('_withListMedList', 'List-med-list'), ('_withListPrognosis', 'List-prognosis'), ) def testJsonFormat_forValidList_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, list_pb2.List) @parameterized.named_parameters( ('_withLocation1', 'Location-1'), ('_withLocation2', 'Location-2'), ('_withLocationAmb', 'Location-amb'), ('_withLocationHl7', 'Location-hl7'), ('_withLocationPh', 'Location-ph'), ('_withLocationUkp', 'Location-ukp'), ) def testJsonFormat_forValidLocation_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, location_pb2.Location) @parameterized.named_parameters( ('_withMeasureComponentAExample', 'Measure-component-a-example'), ('_withMeasureComponentBExample', 'Measure-component-b-example'), ('_withMeasureCompositeExample', 'Measure-composite-example'), ('_withMeasureHivIndicators', 'Measure-hiv-indicators'), ('_withMeasureMeasureCms146Example', 'Measure-measure-cms146-example'), ('_withMeasureMeasureExclusiveBreastfeeding', 'Measure-measure-exclusive-breastfeeding'), ('_withMeasureMeasurePredecessorExample', 'Measure-measure-predecessor-example'), ) def testJsonFormat_forValidMeasure_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, measure_pb2.Measure) @parameterized.named_parameters( ('_withMeasureReportHivIndicators', 'MeasureReport-hiv-indicators'), ('_withMeasureReportMeasureReportCms146Cat1Example', 'MeasureReport-measurereport-cms146-cat1-example'), ('_withMeasureReportMeasureReportCms146Cat2Example', 'MeasureReport-measurereport-cms146-cat2-example'), ('_withMeasureReportMeasureReportCms146Cat3Example', 'MeasureReport-measurereport-cms146-cat3-example'), ) def testJsonFormat_forValidMeasureReport_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, measure_report_pb2.MeasureReport) @parameterized.named_parameters( ('_withMedia1_2_840_11361907579238403408700_3_1_04_19970327150033', 'Media-1.2.840.11361907579238403408700.3.1.04.19970327150033'), ('_withMediaExample', 'Media-example'), ('_withMediaSound', 'Media-sound'), ('_withMediaXray', 'Media-xray'), ) def testJsonFormat_forValidMedia_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, media_pb2.Media) @parameterized.named_parameters( ('_withMedicationMed0301', 'Medication-med0301'), ('_withMedicationMed0302', 'Medication-med0302'), ('_withMedicationMed0303', 'Medication-med0303'), ('_withMedicationMed0304', 'Medication-med0304'), ('_withMedicationMed0305', 'Medication-med0305'), ('_withMedicationMed0306', 'Medication-med0306'), ('_withMedicationMed0307', 'Medication-med0307'), ('_withMedicationMed0308', 'Medication-med0308'), ('_withMedicationMed0309', 'Medication-med0309'), ('_withMedicationMed0310', 'Medication-med0310'), ('_withMedicationMed0311', 'Medication-med0311'), ('_withMedicationMed0312', 'Medication-med0312'), ('_withMedicationMed0313', 'Medication-med0313'), ('_withMedicationMed0314', 'Medication-med0314'), ('_withMedicationMed0315', 'Medication-med0315'), ('_withMedicationMed0316', 'Medication-med0316'), ('_withMedicationMed0317', 'Medication-med0317'), ('_withMedicationMed0318', 'Medication-med0318'), ('_withMedicationMed0319', 'Medication-med0319'), ('_withMedicationMed0320', 'Medication-med0320'), ('_withMedicationMed0321', 'Medication-med0321'), ('_withMedicationMedexample015', 'Medication-medexample015'), ('_withMedicationMedicationexample1', 'Medication-medicationexample1'), ) def testJsonFormat_forValidMedication_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, medication_pb2.Medication) @parameterized.named_parameters( ('_withMedicationAdministrationMedadmin0301', 'MedicationAdministration-medadmin0301'), ('_withMedicationAdministrationMedadmin0302', 'MedicationAdministration-medadmin0302'), ('_withMedicationAdministrationMedadmin0303', 'MedicationAdministration-medadmin0303'), ('_withMedicationAdministrationMedadmin0304', 'MedicationAdministration-medadmin0304'), ('_withMedicationAdministrationMedadmin0305', 'MedicationAdministration-medadmin0305'), ('_withMedicationAdministrationMedadmin0306', 'MedicationAdministration-medadmin0306'), ('_withMedicationAdministrationMedadmin0307', 'MedicationAdministration-medadmin0307'), ('_withMedicationAdministrationMedadmin0308', 'MedicationAdministration-medadmin0308'), ('_withMedicationAdministrationMedadmin0309', 'MedicationAdministration-medadmin0309'), ('_withMedicationAdministrationMedadmin0310', 'MedicationAdministration-medadmin0310'), ('_withMedicationAdministrationMedadmin0311', 'MedicationAdministration-medadmin0311'), ('_withMedicationAdministrationMedadmin0312', 'MedicationAdministration-medadmin0312'), ('_withMedicationAdministrationMedadmin0313', 'MedicationAdministration-medadmin0313'), ('_withMedicationAdministrationMedadminexample03', 'MedicationAdministration-medadminexample03'), ) def testJsonFormat_forValidMedicationAdministration_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medication_administration_pb2.MedicationAdministration) @parameterized.named_parameters( ('_withMedicationDispenseMeddisp008', 'MedicationDispense-meddisp008'), ('_withMedicationDispenseMeddisp0301', 'MedicationDispense-meddisp0301'), ('_withMedicationDispenseMeddisp0302', 'MedicationDispense-meddisp0302'), ('_withMedicationDispenseMeddisp0303', 'MedicationDispense-meddisp0303'), ('_withMedicationDispenseMeddisp0304', 'MedicationDispense-meddisp0304'), ('_withMedicationDispenseMeddisp0305', 'MedicationDispense-meddisp0305'), ('_withMedicationDispenseMeddisp0306', 'MedicationDispense-meddisp0306'), ('_withMedicationDispenseMeddisp0307', 'MedicationDispense-meddisp0307'), ('_withMedicationDispenseMeddisp0308', 'MedicationDispense-meddisp0308'), ('_withMedicationDispenseMeddisp0309', 'MedicationDispense-meddisp0309'), ('_withMedicationDispenseMeddisp0310', 'MedicationDispense-meddisp0310'), ('_withMedicationDispenseMeddisp0311', 'MedicationDispense-meddisp0311'), ('_withMedicationDispenseMeddisp0312', 'MedicationDispense-meddisp0312'), ('_withMedicationDispenseMeddisp0313', 'MedicationDispense-meddisp0313'), ('_withMedicationDispenseMeddisp0314', 'MedicationDispense-meddisp0314'), ('_withMedicationDispenseMeddisp0315', 'MedicationDispense-meddisp0315'), ('_withMedicationDispenseMeddisp0316', 'MedicationDispense-meddisp0316'), ('_withMedicationDispenseMeddisp0317', 'MedicationDispense-meddisp0317'), ('_withMedicationDispenseMeddisp0318', 'MedicationDispense-meddisp0318'), ('_withMedicationDispenseMeddisp0319', 'MedicationDispense-meddisp0319'), ('_withMedicationDispenseMeddisp0320', 'MedicationDispense-meddisp0320'), ('_withMedicationDispenseMeddisp0321', 'MedicationDispense-meddisp0321'), ('_withMedicationDispenseMeddisp0322', 'MedicationDispense-meddisp0322'), ('_withMedicationDispenseMeddisp0324', 'MedicationDispense-meddisp0324'), ('_withMedicationDispenseMeddisp0325', 'MedicationDispense-meddisp0325'), ('_withMedicationDispenseMeddisp0326', 'MedicationDispense-meddisp0326'), ('_withMedicationDispenseMeddisp0327', 'MedicationDispense-meddisp0327'), ('_withMedicationDispenseMeddisp0328', 'MedicationDispense-meddisp0328'), ('_withMedicationDispenseMeddisp0329', 'MedicationDispense-meddisp0329'), ('_withMedicationDispenseMeddisp0330', 'MedicationDispense-meddisp0330'), ('_withMedicationDispenseMeddisp0331', 'MedicationDispense-meddisp0331'), ) def testJsonFormat_forValidMedicationDispense_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medication_dispense_pb2.MedicationDispense) @parameterized.named_parameters( ('_withMedicationKnowledgeExample', 'MedicationKnowledge-example'),) def testJsonFormat_forValidMedicationKnowledge_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medication_knowledge_pb2.MedicationKnowledge) @parameterized.named_parameters( ('_withMedicationRequestMedrx002', 'MedicationRequest-medrx002'), ('_withMedicationRequestMedrx0301', 'MedicationRequest-medrx0301'), ('_withMedicationRequestMedrx0302', 'MedicationRequest-medrx0302'), ('_withMedicationRequestMedrx0303', 'MedicationRequest-medrx0303'), ('_withMedicationRequestMedrx0304', 'MedicationRequest-medrx0304'), ('_withMedicationRequestMedrx0305', 'MedicationRequest-medrx0305'), ('_withMedicationRequestMedrx0306', 'MedicationRequest-medrx0306'), ('_withMedicationRequestMedrx0307', 'MedicationRequest-medrx0307'), ('_withMedicationRequestMedrx0308', 'MedicationRequest-medrx0308'), ('_withMedicationRequestMedrx0309', 'MedicationRequest-medrx0309'), ('_withMedicationRequestMedrx0310', 'MedicationRequest-medrx0310'), ('_withMedicationRequestMedrx0311', 'MedicationRequest-medrx0311'), ('_withMedicationRequestMedrx0312', 'MedicationRequest-medrx0312'), ('_withMedicationRequestMedrx0313', 'MedicationRequest-medrx0313'), ('_withMedicationRequestMedrx0314', 'MedicationRequest-medrx0314'), ('_withMedicationRequestMedrx0315', 'MedicationRequest-medrx0315'), ('_withMedicationRequestMedrx0316', 'MedicationRequest-medrx0316'), ('_withMedicationRequestMedrx0317', 'MedicationRequest-medrx0317'), ('_withMedicationRequestMedrx0318', 'MedicationRequest-medrx0318'), ('_withMedicationRequestMedrx0319', 'MedicationRequest-medrx0319'), ('_withMedicationRequestMedrx0320', 'MedicationRequest-medrx0320'), ('_withMedicationRequestMedrx0321', 'MedicationRequest-medrx0321'), ('_withMedicationRequestMedrx0322', 'MedicationRequest-medrx0322'), ('_withMedicationRequestMedrx0323', 'MedicationRequest-medrx0323'), ('_withMedicationRequestMedrx0324', 'MedicationRequest-medrx0324'), ('_withMedicationRequestMedrx0325', 'MedicationRequest-medrx0325'), ('_withMedicationRequestMedrx0326', 'MedicationRequest-medrx0326'), ('_withMedicationRequestMedrx0327', 'MedicationRequest-medrx0327'), ('_withMedicationRequestMedrx0328', 'MedicationRequest-medrx0328'), ('_withMedicationRequestMedrx0329', 'MedicationRequest-medrx0329'), ('_withMedicationRequestMedrx0330', 'MedicationRequest-medrx0330'), ('_withMedicationRequestMedrx0331', 'MedicationRequest-medrx0331'), ('_withMedicationRequestMedrx0332', 'MedicationRequest-medrx0332'), ('_withMedicationRequestMedrx0333', 'MedicationRequest-medrx0333'), ('_withMedicationRequestMedrx0334', 'MedicationRequest-medrx0334'), ('_withMedicationRequestMedrx0335', 'MedicationRequest-medrx0335'), ('_withMedicationRequestMedrx0336', 'MedicationRequest-medrx0336'), ('_withMedicationRequestMedrx0337', 'MedicationRequest-medrx0337'), ('_withMedicationRequestMedrx0338', 'MedicationRequest-medrx0338'), ('_withMedicationRequestMedrx0339', 'MedicationRequest-medrx0339'), ) def testJsonFormat_forValidMedicationRequest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medication_request_pb2.MedicationRequest) @parameterized.named_parameters( ('_withMedicationStatementExample001', 'MedicationStatement-example001'), ('_withMedicationStatementExample002', 'MedicationStatement-example002'), ('_withMedicationStatementExample003', 'MedicationStatement-example003'), ('_withMedicationStatementExample004', 'MedicationStatement-example004'), ('_withMedicationStatementExample005', 'MedicationStatement-example005'), ('_withMedicationStatementExample006', 'MedicationStatement-example006'), ('_withMedicationStatementExample007', 'MedicationStatement-example007'), ) def testJsonFormat_forValidMedicationStatement_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medication_statement_pb2.MedicationStatement) @parameterized.named_parameters( ('_withMedicinalProductExample', 'MedicinalProduct-example'),) def testJsonFormat_forValidMedicinalProduct_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_pb2.MedicinalProduct) @parameterized.named_parameters( ('_withMedicinalProductAuthorizationExample', 'MedicinalProductAuthorization-example'),) def testJsonFormat_forValidMedicinalProductAuthorization_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_authorization_pb2.MedicinalProductAuthorization) @parameterized.named_parameters( ('_withMedicinalProductContraindicationExample', 'MedicinalProductContraindication-example'),) def testJsonFormat_forValidMedicinalProductContraindication_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_contraindication_pb2.MedicinalProductContraindication) @parameterized.named_parameters( ('_withMedicinalProductIndicationExample', 'MedicinalProductIndication-example'),) def testJsonFormat_forValidMedicinalProductIndication_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_indication_pb2.MedicinalProductIndication) @parameterized.named_parameters( ('_withMedicinalProductIngredientExample', 'MedicinalProductIngredient-example'),) def testJsonFormat_forValidMedicinalProductIngredient_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_ingredient_pb2.MedicinalProductIngredient) @parameterized.named_parameters( ('_withMedicinalProductInteractionExample', 'MedicinalProductInteraction-example'),) def testJsonFormat_forValidMedicinalProductInteraction_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_interaction_pb2.MedicinalProductInteraction) @parameterized.named_parameters( ('_withMedicinalProductManufacturedExample', 'MedicinalProductManufactured-example'),) def testJsonFormat_forValidMedicinalProductManufactured_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_manufactured_pb2.MedicinalProductManufactured) @parameterized.named_parameters( ('_withMedicinalProductPackagedExample', 'MedicinalProductPackaged-example'),) def testJsonFormat_forValidMedicinalProductPackaged_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_packaged_pb2.MedicinalProductPackaged) @parameterized.named_parameters( ('_withMedicinalProductPharmaceuticalExample', 'MedicinalProductPharmaceutical-example'),) def testJsonFormat_forValidMedicinalProductPharmaceutical_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_pharmaceutical_pb2.MedicinalProductPharmaceutical) @parameterized.named_parameters( ('_withMedicinalProductUndesirableEffectExample', 'MedicinalProductUndesirableEffect-example'),) def testJsonFormat_forValidMedicinalProductUndesirableEffect_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, medicinal_product_undesirable_effect_pb2 .MedicinalProductUndesirableEffect) @parameterized.named_parameters( ('_withMessageDefinitionExample', 'MessageDefinition-example'), ('_withMessageDefinitionPatientLinkNotification', 'MessageDefinition-patient-link-notification'), ('_withMessageDefinitionPatientLinkResponse', 'MessageDefinition-patient-link-response'), ) def testJsonFormat_forValidMessageDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, message_definition_pb2.MessageDefinition) @parameterized.named_parameters( ('_withMessageHeader1cbdfb97585948a48301D54eab818d68', 'MessageHeader-1cbdfb97-5859-48a4-8301-d54eab818d68'),) def testJsonFormat_forValidMessageHeader_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, message_header_pb2.MessageHeader) @parameterized.named_parameters( ('_withMolecularSequenceBreastcancer', 'MolecularSequence-breastcancer'), ('_withMolecularSequenceCoord0Base', 'MolecularSequence-coord-0-base'), ('_withMolecularSequenceCoord1Base', 'MolecularSequence-coord-1-base'), ('_withMolecularSequenceExample', 'MolecularSequence-example'), ('_withMolecularSequenceExamplePgx1', 'MolecularSequence-example-pgx-1'), ('_withMolecularSequenceExamplePgx2', 'MolecularSequence-example-pgx-2'), ('_withMolecularSequenceExampleTpmtOne', 'MolecularSequence-example-TPMT-one'), ('_withMolecularSequenceExampleTpmtTwo', 'MolecularSequence-example-TPMT-two'), ('_withMolecularSequenceFdaExample', 'MolecularSequence-fda-example'), ('_withMolecularSequenceFdaVcfComparison', 'MolecularSequence-fda-vcf-comparison'), ('_withMolecularSequenceFdaVcfevalComparison', 'MolecularSequence-fda-vcfeval-comparison'), ('_withMolecularSequenceGraphicExample1', 'MolecularSequence-graphic-example-1'), ('_withMolecularSequenceGraphicExample2', 'MolecularSequence-graphic-example-2'), ('_withMolecularSequenceGraphicExample3', 'MolecularSequence-graphic-example-3'), ('_withMolecularSequenceGraphicExample4', 'MolecularSequence-graphic-example-4'), ('_withMolecularSequenceGraphicExample5', 'MolecularSequence-graphic-example-5'), ('_withMolecularSequenceSequenceComplexVariant', 'MolecularSequence-sequence-complex-variant'), ) def testJsonFormat_forValidMolecularSequence_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, molecular_sequence_pb2.MolecularSequence) @parameterized.named_parameters( ('_withNamingSystemExampleId', 'NamingSystem-example-id'), ('_withNamingSystemExample', 'NamingSystem-example'), ) def testJsonFormat_forValidNamingSystem_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, naming_system_pb2.NamingSystem) @parameterized.named_parameters( ('_withNutritionOrderCardiacdiet', 'NutritionOrder-cardiacdiet'), ('_withNutritionOrderDiabeticdiet', 'NutritionOrder-diabeticdiet'), ('_withNutritionOrderDiabeticSupplement', 'NutritionOrder-diabeticsupplement'), ('_withNutritionOrderEnergySupplement', 'NutritionOrder-energysupplement'), ('_withNutritionOrderEnteralbolus', 'NutritionOrder-enteralbolus'), ('_withNutritionOrderEnteralcontinuous', 'NutritionOrder-enteralcontinuous'), ('_withNutritionOrderFiberrestricteddiet', 'NutritionOrder-fiberrestricteddiet'), ('_withNutritionOrderInfantEnteral', 'NutritionOrder-infantenteral'), ('_withNutritionOrderProteinSupplement', 'NutritionOrder-proteinsupplement'), ('_withNutritionOrderPureedDiet', 'NutritionOrder-pureeddiet'), ('_withNutritionOrderPureedDietSimple', 'NutritionOrder-pureeddiet-simple'), ('_withNutritionOrderRenalDiet', 'NutritionOrder-renaldiet'), ('_withNutritionOrderTextureModified', 'NutritionOrder-texturemodified'), ) def testJsonFormat_forValidNutritionOrder_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, nutrition_order_pb2.NutritionOrder) @parameterized.named_parameters( ('_withObservation10MinuteApgarScore', 'Observation-10minute-apgar-score'), ('_withObservation1MinuteApgarScore', 'Observation-1minute-apgar-score'), ('_withObservation20MinuteApgarScore', 'Observation-20minute-apgar-score'), ('_withObservation2MinuteApgarScore', 'Observation-2minute-apgar-score'), ('_withObservation5MinuteApgarScore', 'Observation-5minute-apgar-score'), ('_withObservation656', 'Observation-656'), ('_withObservationAbdoTender', 'Observation-abdo-tender'), ('_withObservationAlcoholType', 'Observation-alcohol-type'), ('_withObservationBgpanel', 'Observation-bgpanel'), ('_withObservationBloodgroup', 'Observation-bloodgroup'), ('_withObservationBloodPressureCancel', 'Observation-blood-pressure-cancel'), ('_withObservationBloodPressureDar', 'Observation-blood-pressure-dar'), ('_withObservationBloodPressure', 'Observation-blood-pressure'), ('_withObservationBmd', 'Observation-bmd'), ('_withObservationBmi', 'Observation-bmi'), ('_withObservationBmiUsingRelated', 'Observation-bmi-using-related'), ('_withObservationBodyHeight', 'Observation-body-height'), ('_withObservationBodyLength', 'Observation-body-length'), ('_withObservationBodyTemperature', 'Observation-body-temperature'), ('_withObservationClinicalGender', 'Observation-clinical-gender'), ('_withObservationDateLastmp', 'Observation-date-lastmp'), ('_withObservationDecimal', 'Observation-decimal'), ('_withObservationEkg', 'Observation-ekg'), ('_withObservationExampleDiplotype1', 'Observation-example-diplotype1'), ('_withObservationExampleGenetics1', 'Observation-example-genetics-1'), ('_withObservationExampleGenetics2', 'Observation-example-genetics-2'), ('_withObservationExampleGenetics3', 'Observation-example-genetics-3'), ('_withObservationExampleGenetics4', 'Observation-example-genetics-4'), ('_withObservationExampleGenetics5', 'Observation-example-genetics-5'), ('_withObservationExampleGeneticsBrcapat', 'Observation-example-genetics-brcapat'), ('_withObservationExampleHaplotype1', 'Observation-example-haplotype1'), ('_withObservationExampleHaplotype2', 'Observation-example-haplotype2'), ('_withObservationExample', 'Observation-example'), ('_withObservationExamplePhenotype', 'Observation-example-phenotype'), ('_withObservationExampleTpmtDiplotype', 'Observation-example-TPMT-diplotype'), ('_withObservationExampleTpmtHaplotypeOne', 'Observation-example-TPMT-haplotype-one'), ('_withObservationExampleTpmtHaplotypeTwo', 'Observation-example-TPMT-haplotype-two'), ('_withObservationEyeColor', 'Observation-eye-color'), ('_withObservationF001', 'Observation-f001'), ('_withObservationF002', 'Observation-f002'), ('_withObservationF003', 'Observation-f003'), ('_withObservationF004', 'Observation-f004'), ('_withObservationF005', 'Observation-f005'), ('_withObservationF202', 'Observation-f202'), ('_withObservationF203', 'Observation-f203'), ('_withObservationF204', 'Observation-f204'), ('_withObservationF205', 'Observation-f205'), ('_withObservationF206', 'Observation-f206'), ('_withObservationGcsQa', 'Observation-gcs-qa'), ('_withObservationGlasgow', 'Observation-glasgow'), ('_withObservationHeadCircumference', 'Observation-head-circumference'), ('_withObservationHeartRate', 'Observation-heart-rate'), ('_withObservationHerd1', 'Observation-herd1'), ('_withObservationMapSitting', 'Observation-map-sitting'), ('_withObservationMbp', 'Observation-mbp'), ('_withObservationRespiratoryRate', 'Observation-respiratory-rate'), ('_withObservationRhstatus', 'Observation-rhstatus'), ('_withObservationSatO2', 'Observation-satO2'), ('_withObservationSecondSmoke', 'Observation-secondsmoke'), ('_withObservationTrachCare', 'Observation-trachcare'), ('_withObservationUnsat', 'Observation-unsat'), ('_withObservationVitalsPanel', 'Observation-vitals-panel'), ('_withObservationVomiting', 'Observation-vomiting'), ('_withObservationVpOyster', 'Observation-vp-oyster'), ) def testJsonFormat_forValidObservation_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, observation_pb2.Observation) @parameterized.named_parameters( ('_withObservationDefinitionExample', 'ObservationDefinition-example'),) def testJsonFormat_forValidObservationDefinition_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, observation_definition_pb2.ObservationDefinition) @parameterized.named_parameters( ('_withOperationDefinitionActivityDefinitionApply', 'OperationDefinition-ActivityDefinition-apply'), ('_withOperationDefinitionActivityDefinitionDataRequirements', 'OperationDefinition-ActivityDefinition-data-requirements'), ('_withOperationDefinitionCapabilityStatementConforms', 'OperationDefinition-CapabilityStatement-conforms'), ('_withOperationDefinitionCapabilityStatementImplements', 'OperationDefinition-CapabilityStatement-implements'), ('_withOperationDefinitionCapabilityStatementSubset', 'OperationDefinition-CapabilityStatement-subset'), ('_withOperationDefinitionCapabilityStatementVersions', 'OperationDefinition-CapabilityStatement-versions'), ('_withOperationDefinitionChargeItemDefinitionApply', 'OperationDefinition-ChargeItemDefinition-apply'), ('_withOperationDefinitionClaimSubmit', 'OperationDefinition-Claim-submit'), ('_withOperationDefinitionCodeSystemFindMatches', 'OperationDefinition-CodeSystem-find-matches'), ('_withOperationDefinitionCodeSystemLookup', 'OperationDefinition-CodeSystem-lookup'), ('_withOperationDefinitionCodeSystemSubsumes', 'OperationDefinition-CodeSystem-subsumes'), ('_withOperationDefinitionCodeSystemValidateCode', 'OperationDefinition-CodeSystem-validate-code'), ('_withOperationDefinitionCompositionDocument', 'OperationDefinition-Composition-document'), ('_withOperationDefinitionConceptMapClosure', 'OperationDefinition-ConceptMap-closure'), ('_withOperationDefinitionConceptMapTranslate', 'OperationDefinition-ConceptMap-translate'), ('_withOperationDefinitionCoverageEligibilityRequestSubmit', 'OperationDefinition-CoverageEligibilityRequest-submit'), ('_withOperationDefinitionEncounterEverything', 'OperationDefinition-Encounter-everything'), ('_withOperationDefinitionExample', 'OperationDefinition-example'), ('_withOperationDefinitionGroupEverything', 'OperationDefinition-Group-everything'), ('_withOperationDefinitionLibraryDataRequirements', 'OperationDefinition-Library-data-requirements'), ('_withOperationDefinitionListFind', 'OperationDefinition-List-find'), ('_withOperationDefinitionMeasureCareGaps', 'OperationDefinition-Measure-care-gaps'), ('_withOperationDefinitionMeasureCollectData', 'OperationDefinition-Measure-collect-data'), ('_withOperationDefinitionMeasureDataRequirements', 'OperationDefinition-Measure-data-requirements'), ('_withOperationDefinitionMeasureEvaluateMeasure', 'OperationDefinition-Measure-evaluate-measure'), ('_withOperationDefinitionMeasureSubmitData', 'OperationDefinition-Measure-submit-data'), ('_withOperationDefinitionMedicinalProductEverything', 'OperationDefinition-MedicinalProduct-everything'), ('_withOperationDefinitionMessageHeaderProcessMessage', 'OperationDefinition-MessageHeader-process-message'), ('_withOperationDefinitionNamingSystemPreferredId', 'OperationDefinition-NamingSystem-preferred-id'), ('_withOperationDefinitionObservationLastn', 'OperationDefinition-Observation-lastn'), ('_withOperationDefinitionObservationStats', 'OperationDefinition-Observation-stats'), ('_withOperationDefinitionPatientEverything', 'OperationDefinition-Patient-everything'), ('_withOperationDefinitionPatientMatch', 'OperationDefinition-Patient-match'), ('_withOperationDefinitionPlanDefinitionApply', 'OperationDefinition-PlanDefinition-apply'), ('_withOperationDefinitionPlanDefinitionDataRequirements', 'OperationDefinition-PlanDefinition-data-requirements'), ('_withOperationDefinitionResourceConvert', 'OperationDefinition-Resource-convert'), ('_withOperationDefinitionResourceGraph', 'OperationDefinition-Resource-graph'), ('_withOperationDefinitionResourceGraphql', 'OperationDefinition-Resource-graphql'), ('_withOperationDefinitionResourceMetaAdd', 'OperationDefinition-Resource-meta-add'), ('_withOperationDefinitionResourceMetaDelete', 'OperationDefinition-Resource-meta-delete'), ('_withOperationDefinitionResourceMeta', 'OperationDefinition-Resource-meta'), ('_withOperationDefinitionResourceValidate', 'OperationDefinition-Resource-validate'), ('_withOperationDefinitionStructureDefinitionQuestionnaire', 'OperationDefinition-StructureDefinition-questionnaire'), ('_withOperationDefinitionStructureDefinitionSnapshot', 'OperationDefinition-StructureDefinition-snapshot'), ('_withOperationDefinitionStructureMapTransform', 'OperationDefinition-StructureMap-transform'), ('_withOperationDefinitionValueSetExpand', 'OperationDefinition-ValueSet-expand'), ('_withOperationDefinitionValueSetValidateCode', 'OperationDefinition-ValueSet-validate-code'), ) def testJsonFormat_forValidOperationDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, operation_definition_pb2.OperationDefinition) @parameterized.named_parameters( ('_withOperationOutcome101', 'OperationOutcome-101'), ('_withOperationOutcomeAllok', 'OperationOutcome-allok'), ('_withOperationOutcomeBreakTheGlass', 'OperationOutcome-break-the-glass'), ('_withOperationOutcomeException', 'OperationOutcome-exception'), ('_withOperationOutcomeSearchfail', 'OperationOutcome-searchfail'), ('_withOperationOutcomeValidationfail', 'OperationOutcome-validationfail'), ) def testJsonFormat_forValidOperationOutcome_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, operation_outcome_pb2.OperationOutcome) @parameterized.named_parameters( ('_withOrganization1832473e2fe0452dAbe93cdb9879522f', 'Organization-1832473e-2fe0-452d-abe9-3cdb9879522f'), ('_withOrganization1', 'Organization-1'), ('_withOrganization2.16.840.1.113883.19.5', 'Organization-2.16.840.1.113883.19.5'), ('_withOrganization2', 'Organization-2'), ('_withOrganization3', 'Organization-3'), ('_withOrganizationF001', 'Organization-f001'), ('_withOrganizationF002', 'Organization-f002'), ('_withOrganizationF003', 'Organization-f003'), ('_withOrganizationF201', 'Organization-f201'), ('_withOrganizationF203', 'Organization-f203'), ('_withOrganizationHl7', 'Organization-hl7'), ('_withOrganizationHl7Pay', 'Organization-hl7pay'), ('_withOrganizationMmanu', 'Organization-mmanu'), ) def testJsonFormat_forValidOrganization_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, organization_pb2.Organization) @parameterized.named_parameters( ('_withOrganizationAffiliationExample', 'OrganizationAffiliation-example'), ('_withOrganizationAffiliationOrgrole1', 'OrganizationAffiliation-orgrole1'), ('_withOrganizationAffiliationOrgrole2', 'OrganizationAffiliation-orgrole2'), ) def testJsonFormat_forValidOrganizationAffiliation_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, organization_affiliation_pb2.OrganizationAffiliation) @parameterized.named_parameters( ('_withPatientAnimal', 'Patient-animal'), ('_withPatientChExample', 'Patient-ch-example'), ('_withPatientDicom', 'Patient-dicom'), ('_withPatientExample', 'Patient-example'), ('_withPatientF001', 'Patient-f001'), ('_withPatientF201', 'Patient-f201'), ('_withPatientGeneticsExample1', 'Patient-genetics-example1'), ('_withPatientGlossy', 'Patient-glossy'), ('_withPatientIhePcd', 'Patient-ihe-pcd'), ('_withPatientInfantFetal', 'Patient-infant-fetal'), ('_withPatientInfantMom', 'Patient-infant-mom'), ('_withPatientInfantTwin1', 'Patient-infant-twin-1'), ('_withPatientInfantTwin2', 'Patient-infant-twin-2'), ('_withPatientMom', 'Patient-mom'), ('_withPatientNewborn', 'Patient-newborn'), ('_withPatientPat1', 'Patient-pat1'), ('_withPatientPat2', 'Patient-pat2'), ('_withPatientPat3', 'Patient-pat3'), ('_withPatientPat4', 'Patient-pat4'), ('_withPatientProband', 'Patient-proband'), ('_withPatientXcda', 'Patient-xcda'), ('_withPatientXds', 'Patient-xds'), ) def testJsonFormat_forValidPatient_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, patient_pb2.Patient) @parameterized.named_parameters( ('_withPaymentNotice77654', 'PaymentNotice-77654'),) def testJsonFormat_forValidPaymentNotice_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, payment_notice_pb2.PaymentNotice) @parameterized.named_parameters( ('_withPaymentReconciliationER2500', 'PaymentReconciliation-ER2500'),) def testJsonFormat_forValidPaymentReconciliation_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, payment_reconciliation_pb2.PaymentReconciliation) @parameterized.named_parameters( ('_withPersonExample', 'Person-example'), ('_withPersonF002', 'Person-f002'), ('_withPersonGrahame', 'Person-grahame'), ('_withPersonPd', 'Person-pd'), ('_withPersonPp', 'Person-pp'), ) def testJsonFormat_forValidPerson_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, person_pb2.Person) @parameterized.named_parameters( ('_withPlanDefinitionChlamydiaScreeningIntervention', 'PlanDefinition-chlamydia-screening-intervention'), ('_withPlanDefinitionExampleCardiologyOs', 'PlanDefinition-example-cardiology-os'), ('_withPlanDefinitionExclusiveBreastfeedingIntervention01', 'PlanDefinition-exclusive-breastfeeding-intervention-01'), ('_withPlanDefinitionExclusiveBreastfeedingIntervention02', 'PlanDefinition-exclusive-breastfeeding-intervention-02'), ('_withPlanDefinitionExclusiveBreastfeedingIntervention03', 'PlanDefinition-exclusive-breastfeeding-intervention-03'), ('_withPlanDefinitionExclusiveBreastfeedingIntervention04', 'PlanDefinition-exclusive-breastfeeding-intervention-04'), ('_withPlanDefinitionKDN5', 'PlanDefinition-KDN5'), ('_withPlanDefinitionLowSuicideRiskOrderSet', 'PlanDefinition-low-suicide-risk-order-set'), ('_withPlanDefinitionOpioidcds04', 'PlanDefinition-opioidcds-04'), ('_withPlanDefinitionOpioidcds05', 'PlanDefinition-opioidcds-05'), ('_withPlanDefinitionOpioidcds07', 'PlanDefinition-opioidcds-07'), ('_withPlanDefinitionOpioidcds08', 'PlanDefinition-opioidcds-08'), ('_withPlanDefinitionOpioidcds10', 'PlanDefinition-opioidcds-10'), ('_withPlanDefinitionOpioidcds11', 'PlanDefinition-opioidcds-11'), ('_withPlanDefinitionOptionsExample', 'PlanDefinition-options-example'), ('_withPlanDefinitionProtocolExample', 'PlanDefinition-protocol-example'), ('_withPlanDefinitionZikaVirusInterventionInitial', 'PlanDefinition-zika-virus-intervention-initial'), ('_withPlanDefinitionZikaVirusIntervention', 'PlanDefinition-zika-virus-intervention'), ) def testJsonFormat_forValidPlanDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, plan_definition_pb2.PlanDefinition) @parameterized.named_parameters( ('_withPractitionerExample', 'Practitioner-example'), ('_withPractitionerF001', 'Practitioner-f001'), ('_withPractitionerF002', 'Practitioner-f002'), ('_withPractitionerF003', 'Practitioner-f003'), ('_withPractitionerF004', 'Practitioner-f004'), ('_withPractitionerF005', 'Practitioner-f005'), ('_withPractitionerF006', 'Practitioner-f006'), ('_withPractitionerF007', 'Practitioner-f007'), ('_withPractitionerF201', 'Practitioner-f201'), ('_withPractitionerF202', 'Practitioner-f202'), ('_withPractitionerF203', 'Practitioner-f203'), ('_withPractitionerF204', 'Practitioner-f204'), ('_withPractitionerXcda1', 'Practitioner-xcda1'), ('_withPractitionerXcdaAuthor', 'Practitioner-xcda-author'), ) def testJsonFormat_forValidPractitioner_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, practitioner_pb2.Practitioner) @parameterized.named_parameters( ('_withPractitionerRoleExample', 'PractitionerRole-example'),) def testJsonFormat_forValidPractitionerRole_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, practitioner_role_pb2.PractitionerRole) @parameterized.named_parameters( ('_withBase64Binary', 'base64_binary', datatypes_pb2.Base64Binary), ('_withBoolean', 'boolean', datatypes_pb2.Boolean), ('_withCanonical', 'canonical', datatypes_pb2.Canonical), ('_withCode', 'code', datatypes_pb2.Code), ('_withDate', 'date', datatypes_pb2.Date), ('_withDateTime', 'date_time', datatypes_pb2.DateTime), ('_withDecimal', 'decimal', datatypes_pb2.Decimal), ('_withId', 'id', datatypes_pb2.Id), ('_withInstant', 'instant', datatypes_pb2.Instant), ('_withInteger', 'integer', datatypes_pb2.Integer), ('_withMarkdown', 'markdown', datatypes_pb2.Markdown), ('_withOid', 'oid', datatypes_pb2.Oid), ('_withPositiveInt', 'positive_int', datatypes_pb2.PositiveInt), ('_withString', 'string', datatypes_pb2.String), ('_withTime', 'time', datatypes_pb2.Time), ('_withUnsignedInt', 'unsigned_int', datatypes_pb2.UnsignedInt), ('_withUri', 'uri', datatypes_pb2.Uri), ('_withUrl', 'url', datatypes_pb2.Url), ('_withUuid', 'uuid', datatypes_pb2.Uuid), ('_withXhtml', 'xhtml', datatypes_pb2.Xhtml), ) def testJsonFormat_forValidPrimitive_succeeds( self, file_name: str, primitive_cls: Type[message.Message]): json_path = os.path.join(_VALIDATION_PATH, file_name + '.valid.ndjson') proto_path = os.path.join(_VALIDATION_PATH, file_name + '.valid.prototxt') self.assert_parse_equals_golden( json_path, proto_path, primitive_cls, parse_f=json_format.json_fhir_string_to_proto, json_delimiter='\n', proto_delimiter='\n---\n', validate=True, default_timezone='Australia/Sydney') self.assert_print_equals_golden( json_path, proto_path, primitive_cls, print_f=json_format.pretty_print_fhir_to_json_string, json_delimiter='\n', proto_delimiter='\n---\n') @parameterized.named_parameters( ('_withProcedureAmbulation', 'Procedure-ambulation'), ('_withProcedureAppendectomyNarrative', 'Procedure-appendectomy-narrative'), ('_withProcedureBiopsy', 'Procedure-biopsy'), ('_withProcedureColonBiopsy', 'Procedure-colon-biopsy'), ('_withProcedureColonoscopy', 'Procedure-colonoscopy'), ('_withProcedureEducation', 'Procedure-education'), ('_withProcedureExampleImplant', 'Procedure-example-implant'), ('_withProcedureExample', 'Procedure-example'), ('_withProcedureF001', 'Procedure-f001'), ('_withProcedureF002', 'Procedure-f002'), ('_withProcedureF003', 'Procedure-f003'), ('_withProcedureF004', 'Procedure-f004'), ('_withProcedureF201', 'Procedure-f201'), ('_withProcedureHcbs', 'Procedure-HCBS'), ('_withProcedureOb', 'Procedure-ob'), ('_withProcedurePhysicalTherapy', 'Procedure-physical-therapy'), ) def testJsonFormat_forValidProcedure_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, procedure_pb2.Procedure) @parameterized.named_parameters( ('_withProvenanceConsentSignature', 'Provenance-consent-signature'), ('_withProvenanceExampleBiocomputeObject', 'Provenance-example-biocompute-object'), ('_withProvenanceExampleCwl', 'Provenance-example-cwl'), ('_withProvenanceExample', 'Provenance-example'), ('_withProvenanceSignature', 'Provenance-signature'), ) def testJsonFormat_forValidProvenance_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, provenance_pb2.Provenance) @parameterized.named_parameters( ('_withQuestionnaire3141', 'Questionnaire-3141'), ('_withQuestionnaireBb', 'Questionnaire-bb'), ('_withQuestionnaireF201', 'Questionnaire-f201'), ('_withQuestionnaireGcs', 'Questionnaire-gcs'), ('_withQuestionnairePhq9Questionnaire', 'Questionnaire-phq-9-questionnaire'), ('_withQuestionnaireQs1', 'Questionnaire-qs1'), ('_withQuestionnaireZikaVirusExposureAssessment', 'Questionnaire-zika-virus-exposure-assessment'), ) def testJsonFormat_forValidQuestionnaire_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, questionnaire_pb2.Questionnaire) @parameterized.named_parameters( ('_withQuestionnaireResponse3141', 'QuestionnaireResponse-3141'), ('_withQuestionnaireResponseBb', 'QuestionnaireResponse-bb'), ('_withQuestionnaireResponseF201', 'QuestionnaireResponse-f201'), ('_withQuestionnaireResponseGcs', 'QuestionnaireResponse-gcs'), ('_withQuestionnaireResponseUssgFhtAnswers', 'QuestionnaireResponse-ussg-fht-answers'), ) def testJsonFormat_forValidQuestionnaireResponse_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, questionnaire_response_pb2.QuestionnaireResponse) @parameterized.named_parameters( ('_withRelatedPersonBenedicte', 'RelatedPerson-benedicte'), ('_withRelatedPersonF001', 'RelatedPerson-f001'), ('_withRelatedPersonF002', 'RelatedPerson-f002'), ('_withRelatedPersonNewbornMom', 'RelatedPerson-newborn-mom'), ('_withRelatedPersonPeter', 'RelatedPerson-peter'), ) def testJsonFormat_forValidRelatedPerson_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, related_person_pb2.RelatedPerson) @parameterized.named_parameters( ('_withRequestGroupExample', 'RequestGroup-example'), ('_withRequestGroupKdn5Example', 'RequestGroup-kdn5-example'), ) def testJsonFormat_forValidRequestGroup_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, request_group_pb2.RequestGroup) @parameterized.named_parameters( ('_withResearchDefinitionExample', 'ResearchDefinition-example'),) def testJsonFormat_forValidResearchDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, research_definition_pb2.ResearchDefinition) @parameterized.named_parameters( ('_withResearchElementDefinitionExample', 'ResearchElementDefinition-example'),) def testJsonFormat_forValidResearchElementDefinition_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, research_element_definition_pb2.ResearchElementDefinition) @parameterized.named_parameters( ('_withResearchStudyExample', 'ResearchStudy-example'),) def testJsonFormat_forValidResearchStudy_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, research_study_pb2.ResearchStudy) @parameterized.named_parameters( ('_withResearchSubjectExample', 'ResearchSubject-example'),) def testJsonFormat_forValidResearchSubject_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, research_subject_pb2.ResearchSubject) @parameterized.named_parameters( ('_withRiskAssessmentBreastcancerRisk', 'RiskAssessment-breastcancer-risk'), ('_withRiskAssessmentCardiac', 'RiskAssessment-cardiac'), ('_withRiskAssessmentGenetic', 'RiskAssessment-genetic'), ('_withRiskAssessmentPopulation', 'RiskAssessment-population'), ('_withRiskAssessmentPrognosis', 'RiskAssessment-prognosis'), ('_withRiskAssessmentRiskexample', 'RiskAssessment-riskexample'), ) def testJsonFormat_forValidRiskAssessment_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, risk_assessment_pb2.RiskAssessment) @parameterized.named_parameters( ('_withRiskEvidenceSynthesisExample', 'RiskEvidenceSynthesis-example'),) def testJsonFormat_forValidRiskEvidenceSynthesis_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, risk_evidence_synthesis_pb2.RiskEvidenceSynthesis) @parameterized.named_parameters( ('_withScheduleExample', 'Schedule-example'), ('_withScheduleExampleLoc1', 'Schedule-exampleloc1'), ('_withScheduleExampleLoc2', 'Schedule-exampleloc2'), ) def testJsonFormat_forValidSchedule_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, schedule_pb2.Schedule) @parameterized.named_parameters( ('_withServiceRequestAmbulation', 'ServiceRequest-ambulation'), ('_withServiceRequestAppendectomyNarrative', 'ServiceRequest-appendectomy-narrative'), ('_withServiceRequestBenchpress', 'ServiceRequest-benchpress'), ('_withServiceRequestColonBiopsy', 'ServiceRequest-colon-biopsy'), ('_withServiceRequestColonoscopy', 'ServiceRequest-colonoscopy'), ('_withServiceRequestDi', 'ServiceRequest-di'), ('_withServiceRequestDoNotTurn', 'ServiceRequest-do-not-turn'), ('_withServiceRequestEducation', 'ServiceRequest-education'), ('_withServiceRequestExampleImplant', 'ServiceRequest-example-implant'), ('_withServiceRequestExample', 'ServiceRequest-example'), ('_withServiceRequestExamplePgx', 'ServiceRequest-example-pgx'), ('_withServiceRequestFt4', 'ServiceRequest-ft4'), ('_withServiceRequestLipid', 'ServiceRequest-lipid'), ('_withServiceRequestMyringotomy', 'ServiceRequest-myringotomy'), ('_withServiceRequestOb', 'ServiceRequest-ob'), ('_withServiceRequestOgExample1', 'ServiceRequest-og-example1'), ('_withServiceRequestPhysicalTherapy', 'ServiceRequest-physical-therapy'), ('_withServiceRequestPhysiotherapy', 'ServiceRequest-physiotherapy'), ('_withServiceRequestSubrequest', 'ServiceRequest-subrequest'), ('_withServiceRequestVent', 'ServiceRequest-vent'), ) def testJsonFormat_forValidServiceRequest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, service_request_pb2.ServiceRequest) @parameterized.named_parameters( ('_withSlot1', 'Slot-1'), ('_withSlot2', 'Slot-2'), ('_withSlot3', 'Slot-3'), ('_withSlotExample', 'Slot-example'), ) def testJsonFormat_forValidSlot_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, slot_pb2.Slot) @parameterized.named_parameters( ('_withSpecimen101', 'Specimen-101'), ('_withSpecimenIsolate', 'Specimen-isolate'), ('_withSpecimenPooledSerum', 'Specimen-pooled-serum'), ('_withSpecimenSst', 'Specimen-sst'), ('_withSpecimenVmaUrine', 'Specimen-vma-urine'), ) def testJsonFormat_forValidSpecimen_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, specimen_pb2.Specimen) @parameterized.named_parameters( ('_withSpecimenDefinition2364', 'SpecimenDefinition-2364'),) def testJsonFormat_forValidSpecimenDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, specimen_definition_pb2.SpecimenDefinition) @parameterized.named_parameters( ('_withStructureMapExample', 'StructureMap-example'), ('_withStructureMapSupplyrequestTransform', 'StructureMap-supplyrequest-transform'), ) def testJsonFormat_forValidStructureMap_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, structure_map_pb2.StructureMap) @parameterized.named_parameters( ('_withStructureDefinitionCoding', 'StructureDefinition-Coding'), ('_withStructureDefinitionLipidProfile', 'StructureDefinition-lipidprofile'), ) def testJsonFormat_forValidStructureDefinition_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, structure_definition_pb2.StructureDefinition) @parameterized.named_parameters( ('_withSubscriptionExampleError', 'Subscription-example-error'), ('_withSubscriptionExample', 'Subscription-example'), ) def testJsonFormat_forValidSubscription_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, subscription_pb2.Subscription) @parameterized.named_parameters( ('_withSubstanceExample', 'Substance-example'), ('_withSubstanceF201', 'Substance-f201'), ('_withSubstanceF202', 'Substance-f202'), ('_withSubstanceF203', 'Substance-f203'), ('_withSubstanceF204', 'Substance-f204'), ('_withSubstanceF205', 'Substance-f205'), ) def testJsonFormat_forValidSubstance_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, substance_pb2.Substance) @parameterized.named_parameters( ('_withSubstanceSpecificationExample', 'SubstanceSpecification-example'),) def testJsonFormat_forValidSubstanceSpecification_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, substance_specification_pb2.SubstanceSpecification) @parameterized.named_parameters( ('_withSupplyDeliveryPumpdelivery', 'SupplyDelivery-pumpdelivery'), ('_withSupplyDeliverySimpledelivery', 'SupplyDelivery-simpledelivery'), ) def testJsonFormat_forValidSupplyDelivery_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, supply_delivery_pb2.SupplyDelivery) @parameterized.named_parameters( ('_withSupplyRequestSimpleorder', 'SupplyRequest-simpleorder'),) def testJsonFormat_forValidSupplyRequest_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, supply_request_pb2.SupplyRequest) @parameterized.named_parameters( ('_withTaskExample1', 'Task-example1'), ('_withTaskExample2', 'Task-example2'), ('_withTaskExample3', 'Task-example3'), ('_withTaskExample4', 'Task-example4'), ('_withTaskExample5', 'Task-example5'), ('_withTaskExample6', 'Task-example6'), ('_withTaskFmExample1', 'Task-fm-example1'), ('_withTaskFmExample2', 'Task-fm-example2'), ('_withTaskFmExample3', 'Task-fm-example3'), ('_withTaskFmExample4', 'Task-fm-example4'), ('_withTaskFmExample5', 'Task-fm-example5'), ('_withTaskFmExample6', 'Task-fm-example6'), ) def testJsonFormat_forValidTask_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, task_pb2.Task) @parameterized.named_parameters( ('_withTerminologyCapabilitiesExample', 'TerminologyCapabilities-example'),) def testJsonFormat_forValidTerminologyCapabilities_succeeds( self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, terminology_capabilities_pb2.TerminologyCapabilities) @parameterized.named_parameters( ('_withTestReportTestReportExample', 'TestReport-testreport-example'),) def testJsonFormat_forValidTestReport_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, test_report_pb2.TestReport) @parameterized.named_parameters( ('_withTestScriptTestScriptExampleHistory', 'TestScript-testscript-example-history'), ('_withTestScriptTestScriptExample', 'TestScript-testscript-example'), ('_withTestScriptTestScriptExampleMultisystem', 'TestScript-testscript-example-multisystem'), ('_withTestScriptTestScriptExampleReadtest', 'TestScript-testscript-example-readtest'), ('_withTestScriptTestScriptExampleSearch', 'TestScript-testscript-example-search'), ('_withTestScriptTestScriptExampleUpdate', 'TestScript-testscript-example-update'), ) def testJsonFormat_forValidTestScript_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden(file_name, test_script_pb2.TestScript) @parameterized.named_parameters( ('_withVerificationResultExample', 'VerificationResult-example'),) def testJsonFormat_forValidVerificationResult_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, verification_result_pb2.VerificationResult) @parameterized.named_parameters( ('_withVisionPrescription33123', 'VisionPrescription-33123'), ('_withVisionPrescription33124', 'VisionPrescription-33124'), ) def testJsonFormat_forValidVisionPrescription_succeeds(self, file_name: str): self.assert_parse_and_print_spec_equals_golden( file_name, vision_prescription_pb2.VisionPrescription) @parameterized.named_parameters( ('_withCompositionExample', 'Composition-example', composition_pb2.Composition), ('_withEcounterHome', 'Encounter-home', encounter_pb2.Encounter), ('_withObservationExampleGenetics1', 'Observation-example-genetics-1', observation_pb2.Observation), ('_withPatientExample', 'Patient-example', patient_pb2.Patient), ) def testPrintForAnalytics_forValidResource_succeeds( self, file_name: str, proto_cls: Type[message.Message]): json_path = os.path.join(_BIGQUERY_PATH, file_name + '.json') proto_path = os.path.join(_EXAMPLES_PATH, file_name + '.prototxt') # Assert print for analytics (standard and "pretty") self.assert_print_equals_golden( json_path, proto_path, proto_cls, print_f=json_format.print_fhir_to_json_string_for_analytics) self.assert_print_equals_golden( json_path, proto_path, proto_cls, print_f=json_format.pretty_print_fhir_to_json_string_for_analytics) def assert_parse_and_print_examples_equals_golden( self, file_name: str, proto_cls: Type[message.Message]): """Convenience method for performing assertions on FHIR R4 examples.""" json_path = os.path.join(_EXAMPLES_PATH, file_name + '.json') proto_path = os.path.join(_EXAMPLES_PATH, file_name + '.prototxt') self.assert_parse_and_print_equals_golden(json_path, proto_path, proto_cls) def assert_parse_and_print_spec_equals_golden( self, file_name: str, proto_cls: Type[message.Message]): """Convenience method for performing assertions on the FHIR R4 spec.""" json_path = os.path.join(_FHIR_SPEC_PATH, file_name + '.json') proto_path = os.path.join(_EXAMPLES_PATH, file_name + '.prototxt') self.assert_parse_and_print_equals_golden(json_path, proto_path, proto_cls) def assert_parse_and_print_equals_golden(self, json_path: str, proto_path: str, proto_cls: Type[message.Message]): """Convenience method for performing assertions against goldens.""" # Assert parse validate = json_path not in _INVALID_RECORDS self.assert_parse_equals_golden( json_path, proto_path, proto_cls, parse_f=json_format.json_fhir_string_to_proto, validate=validate, default_timezone='Australia/Sydney') # Assert print (standard and "pretty") self.assert_print_equals_golden( json_path, proto_path, proto_cls, print_f=json_format.print_fhir_to_json_string) self.assert_print_equals_golden( json_path, proto_path, proto_cls, print_f=json_format.pretty_print_fhir_to_json_string) if __name__ == '__main__': absltest.main()
[]
2024-01-10
noahweber1/cloudGPT
cloud_gpt_aws_cloudwatch.py
import os import argparse import openai from src.utils.chat_utils import terraform_architecture_to_be_evaluated from src.utils.general_utils import collect_cloudwatch_logs openai.api_key = "" os.environ['AWS_DEFAULT_REGION'] = '' os.environ['AWS_ACCESS_KEY_ID'] = '' os.environ['AWS_SECRET_ACCESS_KEY'] = '' def main(text): main_prompt=text first_iteration=0 second_iteration=0 while first_iteration<11: try: first_iteration+=1 cloud_watch_logs = collect_cloudwatch_logs() code_response_body = terraform_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, cloud_watch_logs=cloud_watch_logs, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error while first_iteration>11 and second_iteration<11: try: second_iteration+=1 cloud_watch_logs = collect_cloudwatch_logs() code_response_body = terraform_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, cloud_watch_logs=cloud_watch_logs, restart_gpt=True, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error print(f"Final code: {code_response_body}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Describe the AWS architecture that you want deploy on AWS:") parser.add_argument("text", type=str, help="Text to pass to the script") args = parser.parse_args() main(args.text)
[]
2024-01-10
noahweber1/cloudGPT
src~apis~gpt_sessions.py
import os from copy import deepcopy from time import sleep from typing import List, Any import openai from langchain import PromptTemplate from langchain.callbacks import CallbackManager from langchain.chat_models import ChatOpenAI from openai.error import RateLimitError from langchain.schema import HumanMessage, SystemMessage, BaseMessage, AIMessage from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from requests.exceptions import ConnectionError from urllib3.exceptions import InvalidChunkLength from src.constants import ( PRICING_GPT4_PROMPT, PRICING_GPT4_GENERATION, PRICING_GPT3_5_TURBO_PROMPT, PRICING_GPT3_5_TURBO_GENERATION, CHARS_PER_TOKEN, ) from src.options.generate.templates_system import template_system_message_base from src.utils.string_tools import print_colored def configure_api_key(): if "OPENAI_API_KEY" not in os.environ: print_colored( "You need to set OPENAI_API_KEY in your environment.", """ Run: gptdeploy configure --key <your_openai_api_key> If you have updated it already, please restart your terminal. """, "red", ) exit(1) openai.api_key = os.environ["OPENAI_API_KEY"] class Session: def __init__(self, task_description, model: str = "gpt-4"): self.task_description = task_description if model == "gpt-4" and self.is_gpt4_available(): self.pricing_prompt = PRICING_GPT4_PROMPT self.pricing_generation = PRICING_GPT4_GENERATION else: if model == "gpt-4": print_colored( "GPT version info", "GPT-4 is not available. Using GPT-3.5-turbo instead.", "yellow", ) model = "gpt-3.5-turbo" self.pricing_prompt = PRICING_GPT3_5_TURBO_PROMPT self.pricing_generation = PRICING_GPT3_5_TURBO_GENERATION self.model_name = model self.chars_prompt_so_far = 0 self.chars_generation_so_far = 0 def create_conversation(self, messages: List[BaseMessage] = [], print_stream: bool = True, print_costs: bool = True): messages = deepcopy(messages) return _Conversation( self.model_name, self.cost_callback, messages, print_stream, print_costs ) @staticmethod def is_gpt4_available(): try: for i in range(5): try: openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "system", "content": "you respond nothing"}], ) break except RateLimitError: sleep(1) continue return True except openai.error.InvalidRequestError: return False def cost_callback(self, chars_prompt, chars_generation, print_costs: bool = True): self.chars_prompt_so_far += chars_prompt self.chars_generation_so_far += chars_generation if print_costs: print("\n") money_prompt = self._calculate_money_spent(self.chars_prompt_so_far, self.pricing_prompt) money_generation = self._calculate_money_spent(self.chars_generation_so_far, self.pricing_generation) print( "Total money spent so far on openai.com:", f"${money_prompt + money_generation:.3f}", ) print("\n") @staticmethod def _calculate_money_spent(num_chars, price): return round(num_chars / CHARS_PER_TOKEN * price / 1000, 3) class AssistantCallbackHandler(StreamingStdOutCallbackHandler): def on_new_token(self, token: str, **kwargs: Any) -> None: """Run on new LLM token. Only available when streaming is enabled.""" print_colored("", token, "green", end="") class _Conversation: def __init__( self, model: str, cost_callback, messages: List[BaseMessage], print_stream, print_costs, ): self._chat = ChatOpenAI( model_name=model, streaming=True, callback_manager=CallbackManager( [AssistantCallbackHandler()] if print_stream else [] ), verbose=True, temperature=0, ) self.cost_callback = cost_callback self.messages = messages self.print_stream = print_stream self.print_costs = print_costs for message in messages: if os.environ["VERBOSE"].lower() == "true": if isinstance(message, SystemMessage): print_colored("system - prompt", message.content, "magenta") elif isinstance(message, HumanMessage): print_colored("user - prompt", message.content, "blue") elif isinstance(message, AIMessage): print_colored("assistant - prompt", message.content, "green") def engage(self, prompt: str, role: str = "user"): MessageClass = ( HumanMessage if role == "user" else SystemMessage ) chat_message = MessageClass(content=prompt) self.messages.append(chat_message) if os.environ["VERBOSE"].lower() == "true": color = "blue" if role == "user" else "magenta" print_colored(role, prompt, color) if self.print_stream: print_colored("assistant", "", "green", end="") for i in range(10): try: response = self._chat(self.messages) break except (ConnectionError, InvalidChunkLength) as e: print("There was a connection error. Retrying...") if i == 9: raise e sleep(10) if os.environ["VERBOSE"].lower() == "true": print() self.cost_callback( sum([len(m.content) for m in self.messages]), len(response.content), self.print_costs, ) self.messages.append(response) return response.content @staticmethod def _create_system_message( task_description, test_description, system_definition_examples: List[str] = [], ) -> SystemMessage: if system_definition_examples is None: return None system_message = ( PromptTemplate.from_template(template_system_message_base).format( task_description=task_description, test_description=test_description, ) ) return SystemMessage(content=system_message)
[ "you respond nothing" ]
2024-01-10
noahweber1/cloudGPT
src~utils~chat_utils.py
from typing import Any, Dict import requests import openai VECTOR_DB_TOKEN="" def query_database(query_prompt: str) -> Dict[str, Any]: """ Query vector database to retrieve chunk with user's input questions. """ url = "http://0.0.0.0:8000/query" headers = { "Content-Type": "application/json", "accept": "application/json", "Authorization": f"Bearer {VECTOR_DB_TOKEN}", } data = {"queries": [{"query": query_prompt, "top_k": 5}]} response = requests.post(url, json=data, headers=headers) if response.status_code == 200: result = response.json() # process the result return result else: raise ValueError(f"Error: {response.status_code} : {response.content}") def aws_architecture_to_be_evaluated(main_prompt, text_prompt="write me a sqs and sns solution", cloud_watch_logs="", restart_gpt=False, vector_db=False, iteration=1): meta_prompt = '''You are an autonomous agent called "assistant for AWS solution architecture" which act as an python function generator. These functions should encapsulate all of the boto3 code needed to deploy on the AWS. To accomplish the goal, you must follow following rules: Rules: 1. As "assistant", you MUST response only in python code. No other text besides python code. 2. You should take into account previous responses and the error messages you get in the process. 3. The responses from "user" are the error messages of the action you performed. Use them to correct the solution. 4. Write a small integration test inside of the script that showcases and proves solution was deployed. 5. Do not use zip files as part of the solution. 6. Try not to over-engineer the solution. 7. Use boto3 to construct the solution. 8. You can assume that "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY" and "AWS_DEFAULT_REGION" are already defined in the same named environement variables. 9. The solution should be as short as possible. 10. If needed you should define the neccessary ARN roles for any services. 11. Execute the proposed solution with main. ''' messages=[] if vector_db: chunks_response = query_database(text_prompt) chunks = [] for result in chunks_response["results"]: for inner_result in result["results"]: chunks.append(inner_result["text"]) messages.append( map(lambda chunk: { "role": "user", "content": chunk }, chunks)) if iteration == 1: messages.append({"role": "system", "content": meta_prompt}) messages.append({"role": "user", "content": str(text_prompt)}) else: messages.append({"role": "user", "content": "Here is the error message: " + str(text_prompt) + "This is the initially posed task to accomplish: " + main_prompt + 'Additionally use the following cloud watch log messages to correct your response' + cloud_watch_logs}) # CALL GPT4 response = openai.ChatCompletion.create( model="gpt-4", messages=messages, ) if restart_gpt: response = openai.ChatCompletion.create( model="gpt-4", messages=messages, user="New session" ) code_response_body=response["choices"][0]["message"]["content"].replace("python", "").replace("```","") #TODO unsafe, find better way to execute the returned strings exec(code_response_body, globals()) return code_response_body def terraform_architecture_to_be_evaluated(main_prompt, text_prompt="write me a sqs and sns solution", cloud_watch_logs="", restart_gpt=False, vector_db=False, iteration=1): meta_prompt = '''You are an autonomous agent called "assistant for AWS solution architecture" which acts as an terraform code generator. This code should encapsulate all of the infrastructure needed to deploy on the AWS. To accomplish the goal, you must follow following rules: Rules: 1. As "assistant", you MUST response only in python code. No other text besides python code. 2. You should only use python-terraform package. 3. You should take into account previous responses and the error messages you get in the process. 4. The responses from "user" are the error messages of the action you performed. Use them to correct the solution. 5. Write a small integration test inside of the script that showcases and proves solution was deployed. 6. Try not to over-engineer the solution. 7. The solution should be as short as possible. 8. Encapsulate the terraform code inside Terraform object 9. execute the script with terraform init, validate and plan 10. You can assume that "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY" and "AWS_DEFAULT_REGION" are already defined in the same named environement variables. 11. If needed you should define the neccessary ARN roles for any services. 12. Execute the proposed solution with main. ''' messages=[] if vector_db: chunks_response = query_database(text_prompt) chunks = [] for result in chunks_response["results"]: for inner_result in result["results"]: chunks.append(inner_result["text"]) messages.append( map(lambda chunk: { "role": "user", "content": chunk }, chunks)) if iteration == 1: messages.append({"role": "system", "content": meta_prompt}) messages.append({"role": "user", "content": str(text_prompt)}) else: messages.append({"role": "user", "content": "Here is the error message: " + str(text_prompt) + "This is the initially posed task to accomplish: " + main_prompt + 'Additionally use the following cloud watch log messages to correct your response' + cloud_watch_logs}) # CALL GPT4 response = openai.ChatCompletion.create( model="gpt-4", messages=messages, ) if restart_gpt: response = openai.ChatCompletion.create( model="gpt-4", messages=messages, user="New session" ) code_response_body=response["choices"][0]["message"]["content"].replace("python", "").replace("```","") #TODO unsafe, find better way to execute the returned strings exec(code_response_body, globals()) return code_response_body def localstack_architecture_to_be_evaluated(main_prompt, text_prompt="write me a sqs and sns solution and test it on localstack", cloud_watch_logs="", restart_gpt=False, vector_db=False, iteration=1): meta_prompt = '''You are an autonomous agent called "assistant for AWS solution architecture" which act as an python function generator. These functions should encapsulate all of the boto3 code needed to deploy on the localstack. To accomplish the goal, you must follow following rules: Rules: 1. As "assistant", you MUST response only in python code. No other text besides python code. 2. You should take into account previous responses and the error messages you get in the process. 3. The responses from "user" are the error messages of the action you performed. Use them to correct the solution. 4. Write a small integration test inside of the script that showcases and proves solution was deployed. 5. Do not use zip files as part of the solution. 6. Try not to over-engineer the solution. 7. Execute the proposed solution with main. ''' messages=[] if vector_db: chunks_response = query_database(text_prompt) chunks = [] for result in chunks_response["results"]: for inner_result in result["results"]: chunks.append(inner_result["text"]) messages.append( map(lambda chunk: { "role": "user", "content": chunk }, chunks)) if iteration == 1: messages.append({"role": "system", "content": meta_prompt}) messages.append({"role": "user", "content": str(text_prompt)}) else: messages.append({"role": "user", "content": "Here is the error message: " + str(text_prompt) + "This is the initially posed task to accomplish: " + main_prompt}) # CALL GPT4 response = openai.ChatCompletion.create( model="gpt-4", messages=messages, ) if restart_gpt: response = openai.ChatCompletion.create( model="gpt-4", messages=messages, user="New session" ) code_response_body=response["choices"][0]["message"]["content"].replace("python", "").replace("```","") #TODO unsafe, find better way to execute the returned strings exec(code_response_body, globals()) return code_response_body
[ "You are an autonomous agent called \"assistant for AWS\n solution architecture\" which act as an python function generator.\n These functions should encapsulate all of the boto3 code needed to\n deploy on the localstack. To accomplish the goal, you must follow following rules:\n\n Rules:\n 1. As \"assistant\", you MUST response only in python code. No other\n text besides python code.\n 2. You should take into account previous responses and the error\n messages you get in the process.\n 3. The responses from \"user\" are the error messages of the action you\n performed. Use them to correct the solution.\n 4. Write a small integration test inside of the script that showcases and proves\n solution was deployed.\n 5. Do not use zip files as part of the solution.\n 6. Try not to over-engineer the solution.\n 7. Execute the proposed solution with main.\n\n ", "You are an autonomous agent called \"assistant for AWS\n solution architecture\" which act as an python function generator.\n These functions should encapsulate all of the boto3 code needed to\n deploy on the AWS. To accomplish the goal, you must follow following rules:\n\n Rules:\n 1. As \"assistant\", you MUST response only in python code. No other\n text besides python code.\n 2. You should take into account previous responses and the error\n messages you get in the process.\n 3. The responses from \"user\" are the error messages of the action you\n performed. Use them to correct the solution.\n 4. Write a small integration test inside of the script that showcases and proves\n solution was deployed.\n 5. Do not use zip files as part of the solution.\n 6. Try not to over-engineer the solution.\n 7. Use boto3 to construct the solution.\n 8. You can assume that \"AWS_ACCESS_KEY_ID\", \"AWS_SECRET_ACCESS_KEY\" and \"AWS_DEFAULT_REGION\" are already defined in the same named environement variables.\n 9. The solution should be as short as possible.\n 10. If needed you should define the neccessary ARN roles for any services.\n 11. Execute the proposed solution with main.\n\n ", "Here is the error message: PLACEHOLDERThis is the initially posed task to accomplish: PLACEHOLDERAdditionally use the following cloud watch log messages to correct your responsePLACEHOLDER", "Here is the error message: PLACEHOLDERThis is the initially posed task to accomplish: PLACEHOLDER", "You are an autonomous agent called \"assistant for AWS\n solution architecture\" which acts as an terraform code generator.\n This code should encapsulate all of the infrastructure needed to\n deploy on the AWS. To accomplish the goal, you must follow following rules:\n\n Rules:\n 1. As \"assistant\", you MUST response only in python code. No other\n text besides python code.\n 2. You should only use python-terraform package.\n 3. You should take into account previous responses and the error\n messages you get in the process.\n 4. The responses from \"user\" are the error messages of the action you\n performed. Use them to correct the solution.\n 5. Write a small integration test inside of the script that showcases and proves\n solution was deployed.\n 6. Try not to over-engineer the solution.\n 7. The solution should be as short as possible.\n 8. Encapsulate the terraform code inside Terraform object\n 9. execute the script with terraform init, validate and plan\n 10. You can assume that \"AWS_ACCESS_KEY_ID\", \"AWS_SECRET_ACCESS_KEY\" and \"AWS_DEFAULT_REGION\" are already defined in the same named environement variables.\n 11. If needed you should define the neccessary ARN roles for any services.\n 12. Execute the proposed solution with main.\n\n " ]
2024-01-10
noahweber1/cloudGPT
cloud_gpt_localstack.py
# make sure you have boto3, openapi, localstack and awscli installed as python libraries. (besides docker for localstack) import os import argparse import openai from src.utils.chat_utils import localstack_architecture_to_be_evaluated openai.api_key = "" # Set environment variables for localstack os.environ["AWS_ACCESS_KEY_ID"] = "test" os.environ["AWS_SECRET_ACCESS_KEY"] = "test" os.environ["AWS_DEFAULT_REGION"] = "us-east-1" os.environ["AWS_SESSION_TOKEN"] = "test" def main(text): main_prompt=text first_iteration=0 second_iteration=0 while first_iteration<11: try: first_iteration+=1 code_response_body = localstack_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error while first_iteration>11 and second_iteration<11: try: second_iteration+=1 code_response_body = localstack_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, restart_gpt=True, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error print(f"Final code: {code_response_body}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Describe the AWS architecture that you want deploy on localstack:") parser.add_argument("text", type=str, help="Text to pass to the script") args = parser.parse_args() main(args.text)
[]
2024-01-10
noahweber1/cloudGPT
cloud_gpt_terraform.py
import os import argparse import openai from src.utils.chat_utils import aws_architecture_to_be_evaluated from src.utils.general_utils import collect_cloudwatch_logs openai.api_key = "" os.environ['AWS_DEFAULT_REGION'] = '' os.environ['AWS_ACCESS_KEY_ID'] = '' os.environ['AWS_SECRET_ACCESS_KEY'] = '' def main(text): main_prompt=text first_iteration=0 second_iteration=0 while first_iteration<11: try: first_iteration+=1 cloud_watch_logs = collect_cloudwatch_logs() code_response_body = aws_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, cloud_watch_logs=cloud_watch_logs, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error while first_iteration>11 and second_iteration<11: try: second_iteration+=1 cloud_watch_logs = collect_cloudwatch_logs() code_response_body = aws_architecture_to_be_evaluated(main_prompt=main_prompt, text_prompt=text, cloud_watch_logs=cloud_watch_logs, restart_gpt=True, iteration=first_iteration) print("Operation completed without errors.") break # If no exception is caught, exit the loop except Exception as error: print("Caught an error:", error) text=error print(f"Final code: {code_response_body}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Describe the AWS architecture that you want deploy on AWS:") parser.add_argument("text", type=str, help="Text to pass to the script") args = parser.parse_args() main(args.text)
[]
2024-01-10
hnpl/gem5_components
mesh_cache~MeshCache.py
from math import log2 from typing import List from gem5.utils.requires import requires from gem5.utils.override import overrides from gem5.coherence_protocol import CoherenceProtocol from gem5.components.boards.abstract_board import AbstractBoard from gem5.components.cachehierarchies.ruby.abstract_ruby_cache_hierarchy import AbstractRubyCacheHierarchy from gem5.components.cachehierarchies.abstract_three_level_cache_hierarchy import AbstractThreeLevelCacheHierarchy from gem5.components.cachehierarchies.abstract_cache_hierarchy import AbstractCacheHierarchy from gem5.components.cachehierarchies.chi.nodes.dma_requestor import DMARequestor from gem5.components.cachehierarchies.chi.nodes.memory_controller import MemoryController from gem5.components.cachehierarchies.chi.nodes.abstract_node import AbstractNode from m5.objects import RubySystem, RubyPortProxy, RubySequencer, AddrRange from .components.CoreTile import CoreTile from .components.DMATile import DMATile from .components.L3OnlyTile import L3OnlyTile from .components.L3Slice import L3Slice from .components.MemTile import MemTile from .components.MeshDescriptor import MeshTracker, NodeType from .components.MeshNetwork import MeshNetwork from .utils.SizeArithmetic import SizeArithmetic class MeshCache(AbstractRubyCacheHierarchy, AbstractThreeLevelCacheHierarchy): def __init__( self, l1i_size: str, l1i_assoc: int, l1d_size: str, l1d_assoc: int, l2_size: str, l2_assoc: int, l3_size: str, l3_assoc: int, num_core_complexes: int, is_fullsystem: bool, mesh_descriptor: MeshTracker ): AbstractRubyCacheHierarchy.__init__(self=self) AbstractThreeLevelCacheHierarchy.__init__( self=self, l1i_size=l1i_size, l1i_assoc=l1i_assoc, l1d_size=l1d_size, l1d_assoc=l1d_assoc, l2_size=l2_size, l2_assoc=l2_assoc, l3_size=l3_size, l3_assoc=l3_assoc, ) self._num_core_complexes = num_core_complexes self._is_fullsystem = is_fullsystem self._mesh_descriptor = mesh_descriptor self._has_dma = False self._has_l3_only_tiles = False print(self._mesh_descriptor) requires(coherence_protocol_required=CoherenceProtocol.CHI) @overrides(AbstractCacheHierarchy) def incorporate_cache(self, board: AbstractBoard) -> None: self._setup_ruby_system() self._get_board_info(board) self._create_core_tiles(board) self._create_l3_only_tiles(board) self._assign_addr_range(board) self._create_memory_tiles(board) self._create_dma_tiles(board) self._set_downstream_destinations() self.ruby_system.network.create_mesh() self._incorperate_system_ports(board) self._finalize_ruby_system() def _get_board_info(self, board: AbstractBoard) -> None: self._cache_line_size = board.cache_line_size self._clk_domain = board.clk_domain # should be called at the BEGINNING of incorporate_cache() def _setup_ruby_system(self) -> None: self.ruby_system = RubySystem() self.ruby_system.number_of_virtual_networks = 4 self.ruby_system.network = MeshNetwork( ruby_system = self.ruby_system, mesh_descriptor = self._mesh_descriptor ) self.ruby_system.network.number_of_virtual_networks = 4 self.ruby_system.num_of_sequencers = 0 # should be called at the END of incorporate_cache() def _finalize_ruby_system(self) -> None: self.ruby_system.num_of_sequencers = self.ruby_system.network.get_num_sequencers() self.ruby_system.network.int_links = self.ruby_system.network._int_links self.ruby_system.network.ext_links = self.ruby_system.network._ext_links self.ruby_system.network.routers = self.ruby_system.network._routers self.ruby_system.network.setup_buffers() def _create_core_tiles(self, board: AbstractBoard) -> None: core_tile_coordinates = self._mesh_descriptor.get_tiles_coordinates(NodeType.CoreTile) cores = board.get_processor().get_cores() num_l3_slices = self._mesh_descriptor.get_num_l3_slices() l3_slice_size = (SizeArithmetic(self._l3_size) // num_l3_slices).get() self.core_tiles = [CoreTile( board = board, ruby_system = self.ruby_system, coordinate = core_tile_coordinate, mesh_descriptor = self._mesh_descriptor, core = core, core_id = core_id//2, l1i_size = self._l1i_size, l1i_associativity = self._l1i_assoc, l1d_size = self._l1d_size, l1d_associativity = self._l1d_assoc, l2_size = self._l2_size, l2_associativity = self._l2_assoc, l3_slice_size = l3_slice_size, l3_associativity = self._l3_assoc ) for core_id, (core, core_tile_coordinate) in enumerate(zip(cores, core_tile_coordinates))] for tile in self.core_tiles: self.ruby_system.network.incorporate_ruby_subsystem(tile) def _create_l3_only_tiles(self, board: AbstractBoard) -> None: l3_only_tiles_coordinates = self._mesh_descriptor.get_tiles_coordinates(NodeType.L3OnlyTile) num_l3_slices = self._mesh_descriptor.get_num_l3_slices() l3_slice_size = (SizeArithmetic(self._l3_size) // num_l3_slices).get() if len(l3_only_tiles_coordinates) > 0: self._has_l3_only_tiles = True self.l3_only_tiles = [L3OnlyTile( board = board, ruby_system = self.ruby_system, coordinate = tile_coordinate, mesh_descriptor = self._mesh_descriptor, l3_slice_size = l3_slice_size, l3_associativity = self._l3_assoc ) for tile_coordinate in l3_only_tiles_coordinates] for tile in self.l3_only_tiles: self.ruby_system.network.incorporate_ruby_subsystem(tile) def _find_board_mem_start(self, board: AbstractBoard) -> None: mem_start = 1 << 64 for r in board.mem_ranges: mem_start = min(r.start.value, mem_start) return mem_start def _assign_addr_range(self, board: AbstractBoard) -> None: #mem_start = board.get_memory().get_start_addr() mem_start = self._find_board_mem_start(board) mem_size = board.get_memory().get_size() interleaving_size = "64B" num_offset_bits = int(log2(SizeArithmetic(interleaving_size).bytes)) all_l3_slices = self._get_all_l3_slices() num_l3_slices = len(all_l3_slices) num_slice_indexing_bits = int(log2(num_l3_slices)) address_ranges = [AddrRange( start = mem_start, size = mem_size, intlvHighBit = num_offset_bits + num_slice_indexing_bits - 1, intlvBits = num_slice_indexing_bits, intlvMatch = i ) for i in range(num_l3_slices)] for address_range, l3_slice in zip(address_ranges, all_l3_slices): l3_slice.addr_ranges = address_range def _create_memory_tiles(self, board: AbstractBoard) -> None: mem_tile_coordinates = self._mesh_descriptor.get_tiles_coordinates(NodeType.MemTile) self.memory_tiles = [MemTile( board = board, ruby_system = self.ruby_system, coordinate = mem_tile_coordinate, mesh_descriptor = self._mesh_descriptor, address_range = address_range, memory_port = memory_port ) for mem_tile_coordinate, (address_range, memory_port) in zip(mem_tile_coordinates, board.get_mem_ports())] for tile in self.memory_tiles: self.ruby_system.network.incorporate_ruby_subsystem(tile) def _create_dma_tiles(self, board: AbstractBoard) -> None: self._has_dma = False if not board.has_dma_ports(): return self._has_dma = True dma_tile_coordinates = self._mesh_descriptor.get_tiles_coordinates(NodeType.DMATile) self.dma_tiles = [DMATile( board = board, ruby_system = self.ruby_system, coordinate = dma_tile_coordinate, mesh_descriptor = self._mesh_descriptor, dma_port = dma_port, dma_id = dma_id ) for dma_id, (dma_tile_coordinate, dma_port) in enumerate(zip(dma_tile_coordinates, board.get_dma_ports()))] for tile in self.dma_tiles: self.ruby_system.network.incorporate_ruby_subsystem(tile) def _get_all_l3_slices(self) -> List[L3Slice]: if self._has_l3_only_tiles: all_l3_slices = [tile.l3_slice for tile in self.core_tiles] + [tile.l3_slice for tile in self.l3_only_tiles] else: all_l3_slices = [tile.l3_slice for tile in self.core_tiles] return all_l3_slices def _set_downstream_destinations(self) -> None: all_l3_slices = self._get_all_l3_slices() all_mem_ctrls = [mem_tile.memory_controller for mem_tile in self.memory_tiles] for tile in self.core_tiles: tile.set_l2_downstream_destinations(all_l3_slices) for l3_slice in all_l3_slices: l3_slice.downstream_destinations = all_mem_ctrls if self._has_dma: for tile in self.dma_tiles: tile.dma_controller.downstream_destinations = all_l3_slices def _incorperate_system_ports(self, board: AbstractBoard) -> None: self.ruby_system.sys_port_proxy = RubyPortProxy() board.connect_system_port(self.ruby_system.sys_port_proxy.in_ports)
[]
2024-01-10
bhctest123/dify
api~tasks~enable_segment_to_index_task.py
import datetime import logging import time import click from celery import shared_task from langchain.schema import Document from werkzeug.exceptions import NotFound from core.index.index import IndexBuilder from extensions.ext_database import db from extensions.ext_redis import redis_client from models.dataset import DocumentSegment @shared_task(queue='dataset') def enable_segment_to_index_task(segment_id: str): """ Async enable segment to index :param segment_id: Usage: enable_segment_to_index_task.delay(segment_id) """ logging.info(click.style('Start enable segment to index: {}'.format(segment_id), fg='green')) start_at = time.perf_counter() segment = db.session.query(DocumentSegment).filter(DocumentSegment.id == segment_id).first() if not segment: raise NotFound('Segment not found') if segment.status != 'completed': return indexing_cache_key = 'segment_{}_indexing'.format(segment.id) try: document = Document( page_content=segment.content, metadata={ "doc_id": segment.index_node_id, "doc_hash": segment.index_node_hash, "document_id": segment.document_id, "dataset_id": segment.dataset_id, } ) dataset = segment.dataset if not dataset: logging.info(click.style('Segment {} has no dataset, pass.'.format(segment.id), fg='cyan')) return dataset_document = segment.document if not dataset_document: logging.info(click.style('Segment {} has no document, pass.'.format(segment.id), fg='cyan')) return if not dataset_document.enabled or dataset_document.archived or dataset_document.indexing_status != 'completed': logging.info(click.style('Segment {} document status is invalid, pass.'.format(segment.id), fg='cyan')) return # save vector index index = IndexBuilder.get_index(dataset, 'high_quality') if index: index.add_texts([document], duplicate_check=True) # save keyword index index = IndexBuilder.get_index(dataset, 'economy') if index: index.add_texts([document]) end_at = time.perf_counter() logging.info(click.style('Segment enabled to index: {} latency: {}'.format(segment.id, end_at - start_at), fg='green')) except Exception as e: logging.exception("enable segment to index failed") segment.enabled = False segment.disabled_at = datetime.datetime.utcnow() segment.status = 'error' segment.error = str(e) db.session.commit() finally: redis_client.delete(indexing_cache_key)
[]
2024-01-10
bhctest123/dify
api~tests~integration_tests~models~moderation~test_openai_moderation.py
import json import os from unittest.mock import patch from core.model_providers.models.moderation.openai_moderation import OpenAIModeration, DEFAULT_MODEL from core.model_providers.providers.openai_provider import OpenAIProvider from models.provider import Provider, ProviderType def get_mock_provider(valid_openai_api_key): return Provider( id='provider_id', tenant_id='tenant_id', provider_name='openai', provider_type=ProviderType.CUSTOM.value, encrypted_config=json.dumps({'openai_api_key': valid_openai_api_key}), is_valid=True, ) def get_mock_openai_moderation_model(): valid_openai_api_key = os.environ['OPENAI_API_KEY'] openai_provider = OpenAIProvider(provider=get_mock_provider(valid_openai_api_key)) return OpenAIModeration( model_provider=openai_provider, name=DEFAULT_MODEL ) def decrypt_side_effect(tenant_id, encrypted_openai_api_key): return encrypted_openai_api_key @patch('core.helper.encrypter.decrypt_token', side_effect=decrypt_side_effect) def test_run(mock_decrypt): model = get_mock_openai_moderation_model() rst = model.run('hello') assert rst is True
[]
2024-01-10
bhctest123/dify
api~core~model_providers~models~entity~message.py
import enum from langchain.schema import HumanMessage, AIMessage, SystemMessage, BaseMessage from pydantic import BaseModel class LLMRunResult(BaseModel): content: str prompt_tokens: int completion_tokens: int source: list = None class MessageType(enum.Enum): HUMAN = 'human' ASSISTANT = 'assistant' SYSTEM = 'system' class PromptMessage(BaseModel): type: MessageType = MessageType.HUMAN content: str = '' def to_lc_messages(messages: list[PromptMessage]): lc_messages = [] for message in messages: if message.type == MessageType.HUMAN: lc_messages.append(HumanMessage(content=message.content)) elif message.type == MessageType.ASSISTANT: lc_messages.append(AIMessage(content=message.content)) elif message.type == MessageType.SYSTEM: lc_messages.append(SystemMessage(content=message.content)) return lc_messages def to_prompt_messages(messages: list[BaseMessage]): prompt_messages = [] for message in messages: if isinstance(message, HumanMessage): prompt_messages.append(PromptMessage(content=message.content, type=MessageType.HUMAN)) elif isinstance(message, AIMessage): prompt_messages.append(PromptMessage(content=message.content, type=MessageType.ASSISTANT)) elif isinstance(message, SystemMessage): prompt_messages.append(PromptMessage(content=message.content, type=MessageType.SYSTEM)) return prompt_messages def str_to_prompt_messages(texts: list[str]): prompt_messages = [] for text in texts: prompt_messages.append(PromptMessage(content=text)) return prompt_messages
[ "[]" ]
2024-01-10
bhctest123/dify
api~core~agent~agent~multi_dataset_router_agent.py
import json from typing import Tuple, List, Any, Union, Sequence, Optional, cast from langchain.agents import OpenAIFunctionsAgent, BaseSingleActionAgent from langchain.callbacks.base import BaseCallbackManager from langchain.callbacks.manager import Callbacks from langchain.prompts.chat import BaseMessagePromptTemplate from langchain.schema import AgentAction, AgentFinish, SystemMessage from langchain.schema.language_model import BaseLanguageModel from langchain.tools import BaseTool from core.model_providers.models.llm.base import BaseLLM from core.tool.dataset_retriever_tool import DatasetRetrieverTool class MultiDatasetRouterAgent(OpenAIFunctionsAgent): """ An Multi Dataset Retrieve Agent driven by Router. """ model_instance: BaseLLM class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True def should_use_agent(self, query: str): """ return should use agent :param query: :return: """ return True def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(self.tools) == 0: return AgentFinish(return_values={"output": ''}, log='') elif len(self.tools) == 1: tool = next(iter(self.tools)) tool = cast(DatasetRetrieverTool, tool) rst = tool.run(tool_input={'query': kwargs['input']}) # output = '' # rst_json = json.loads(rst) # for item in rst_json: # output += f'{item["content"]}\n' return AgentFinish(return_values={"output": rst}, log=rst) if intermediate_steps: _, observation = intermediate_steps[-1] return AgentFinish(return_values={"output": observation}, log=observation) try: agent_decision = super().plan(intermediate_steps, callbacks, **kwargs) if isinstance(agent_decision, AgentAction): tool_inputs = agent_decision.tool_input if isinstance(tool_inputs, dict) and 'query' in tool_inputs: tool_inputs['query'] = kwargs['input'] agent_decision.tool_input = tool_inputs return agent_decision except Exception as e: new_exception = self.model_instance.handle_exceptions(e) raise new_exception async def aplan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: raise NotImplementedError() @classmethod def from_llm_and_tools( cls, llm: BaseLanguageModel, tools: Sequence[BaseTool], callback_manager: Optional[BaseCallbackManager] = None, extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None, system_message: Optional[SystemMessage] = SystemMessage( content="You are a helpful AI assistant." ), **kwargs: Any, ) -> BaseSingleActionAgent: return super().from_llm_and_tools( llm=llm, tools=tools, callback_manager=callback_manager, extra_prompt_messages=extra_prompt_messages, system_message=system_message, **kwargs, )
[ "You are a helpful AI assistant." ]
2024-01-10
bhctest123/dify
api~core~callback_handler~index_tool_callback_handler.py
from typing import List from langchain.schema import Document from core.conversation_message_task import ConversationMessageTask from extensions.ext_database import db from models.dataset import DocumentSegment class DatasetIndexToolCallbackHandler: """Callback handler for dataset tool.""" def __init__(self, dataset_id: str, conversation_message_task: ConversationMessageTask) -> None: self.dataset_id = dataset_id self.conversation_message_task = conversation_message_task def on_tool_end(self, documents: List[Document]) -> None: """Handle tool end.""" for document in documents: doc_id = document.metadata['doc_id'] # add hit count to document segment db.session.query(DocumentSegment).filter( DocumentSegment.dataset_id == self.dataset_id, DocumentSegment.index_node_id == doc_id ).update( {DocumentSegment.hit_count: DocumentSegment.hit_count + 1}, synchronize_session=False ) db.session.commit() def return_retriever_resource_info(self, resource: List): """Handle return_retriever_resource_info.""" self.conversation_message_task.on_dataset_query_finish(resource)
[]
2024-01-10
bhctest123/dify
api~core~tool~dataset_retriever_tool.py
import json from typing import Type from flask import current_app from langchain.tools import BaseTool from pydantic import Field, BaseModel from core.callback_handler.index_tool_callback_handler import DatasetIndexToolCallbackHandler from core.conversation_message_task import ConversationMessageTask from core.embedding.cached_embedding import CacheEmbedding from core.index.keyword_table_index.keyword_table_index import KeywordTableIndex, KeywordTableConfig from core.index.vector_index.vector_index import VectorIndex from core.model_providers.error import LLMBadRequestError, ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from extensions.ext_database import db from models.dataset import Dataset, DocumentSegment, Document class DatasetRetrieverToolInput(BaseModel): query: str = Field(..., description="Query for the dataset to be used to retrieve the dataset.") class DatasetRetrieverTool(BaseTool): """Tool for querying a Dataset.""" name: str = "dataset" args_schema: Type[BaseModel] = DatasetRetrieverToolInput description: str = "use this to retrieve a dataset. " tenant_id: str dataset_id: str k: int = 3 conversation_message_task: ConversationMessageTask return_resource: str retriever_from: str @classmethod def from_dataset(cls, dataset: Dataset, **kwargs): description = dataset.description if not description: description = 'useful for when you want to answer queries about the ' + dataset.name description = description.replace('\n', '').replace('\r', '') return cls( name=f'dataset-{dataset.id}', tenant_id=dataset.tenant_id, dataset_id=dataset.id, description=description, **kwargs ) def _run(self, query: str) -> str: dataset = db.session.query(Dataset).filter( Dataset.tenant_id == self.tenant_id, Dataset.id == self.dataset_id ).first() if not dataset: return f'[{self.name} failed to find dataset with id {self.dataset_id}.]' if dataset.indexing_technique == "economy": # use keyword table query kw_table_index = KeywordTableIndex( dataset=dataset, config=KeywordTableConfig( max_keywords_per_chunk=5 ) ) documents = kw_table_index.search(query, search_kwargs={'k': self.k}) return str("\n".join([document.page_content for document in documents])) else: try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except LLMBadRequestError: return '' except ProviderTokenNotInitError: return '' embeddings = CacheEmbedding(embedding_model) vector_index = VectorIndex( dataset=dataset, config=current_app.config, embeddings=embeddings ) if self.k > 0: documents = vector_index.search( query, search_type='similarity_score_threshold', search_kwargs={ 'k': self.k } ) else: documents = [] hit_callback = DatasetIndexToolCallbackHandler(dataset.id, self.conversation_message_task) hit_callback.on_tool_end(documents) document_score_list = {} if dataset.indexing_technique != "economy": for item in documents: document_score_list[item.metadata['doc_id']] = item.metadata['score'] document_context_list = [] index_node_ids = [document.metadata['doc_id'] for document in documents] segments = DocumentSegment.query.filter(DocumentSegment.dataset_id == self.dataset_id, DocumentSegment.completed_at.isnot(None), DocumentSegment.status == 'completed', DocumentSegment.enabled == True, DocumentSegment.index_node_id.in_(index_node_ids) ).all() if segments: index_node_id_to_position = {id: position for position, id in enumerate(index_node_ids)} sorted_segments = sorted(segments, key=lambda segment: index_node_id_to_position.get(segment.index_node_id, float('inf'))) for segment in sorted_segments: if segment.answer: document_context_list.append(f'question:{segment.content} answer:{segment.answer}') else: document_context_list.append(segment.content) if self.return_resource: context_list = [] resource_number = 1 for segment in sorted_segments: context = {} document = Document.query.filter(Document.id == segment.document_id, Document.enabled == True, Document.archived == False, ).first() if dataset and document: source = { 'position': resource_number, 'dataset_id': dataset.id, 'dataset_name': dataset.name, 'document_id': document.id, 'document_name': document.name, 'data_source_type': document.data_source_type, 'segment_id': segment.id, 'retriever_from': self.retriever_from } if dataset.indexing_technique != "economy": source['score'] = document_score_list.get(segment.index_node_id) if self.retriever_from == 'dev': source['hit_count'] = segment.hit_count source['word_count'] = segment.word_count source['segment_position'] = segment.position source['index_node_hash'] = segment.index_node_hash if segment.answer: source['content'] = f'question:{segment.content} \nanswer:{segment.answer}' else: source['content'] = segment.content context_list.append(source) resource_number += 1 hit_callback.return_retriever_resource_info(context_list) return str("\n".join(document_context_list)) async def _arun(self, tool_input: str) -> str: raise NotImplementedError()
[ "use this to retrieve a dataset. " ]
2024-01-10
bhctest123/dify
api~core~model_providers~model_factory.py
from typing import Optional from langchain.callbacks.base import Callbacks from core.model_providers.error import ProviderTokenNotInitError, LLMBadRequestError from core.model_providers.model_provider_factory import ModelProviderFactory, DEFAULT_MODELS from core.model_providers.models.base import BaseProviderModel from core.model_providers.models.embedding.base import BaseEmbedding from core.model_providers.models.entity.model_params import ModelKwargs, ModelType from core.model_providers.models.llm.base import BaseLLM from core.model_providers.models.moderation.base import BaseModeration from core.model_providers.models.speech2text.base import BaseSpeech2Text from extensions.ext_database import db from models.provider import TenantDefaultModel class ModelFactory: @classmethod def get_text_generation_model_from_model_config(cls, tenant_id: str, model_config: dict, streaming: bool = False, callbacks: Callbacks = None) -> Optional[BaseLLM]: provider_name = model_config.get("provider") model_name = model_config.get("name") completion_params = model_config.get("completion_params", {}) return cls.get_text_generation_model( tenant_id=tenant_id, model_provider_name=provider_name, model_name=model_name, model_kwargs=ModelKwargs( temperature=completion_params.get('temperature', 0), max_tokens=completion_params.get('max_tokens', 256), top_p=completion_params.get('top_p', 0), frequency_penalty=completion_params.get('frequency_penalty', 0.1), presence_penalty=completion_params.get('presence_penalty', 0.1) ), streaming=streaming, callbacks=callbacks ) @classmethod def get_text_generation_model(cls, tenant_id: str, model_provider_name: Optional[str] = None, model_name: Optional[str] = None, model_kwargs: Optional[ModelKwargs] = None, streaming: bool = False, callbacks: Callbacks = None, deduct_quota: bool = True) -> Optional[BaseLLM]: """ get text generation model. :param tenant_id: a string representing the ID of the tenant. :param model_provider_name: :param model_name: :param model_kwargs: :param streaming: :param callbacks: :param deduct_quota: :return: """ is_default_model = False if model_provider_name is None and model_name is None: default_model = cls.get_default_model(tenant_id, ModelType.TEXT_GENERATION) if not default_model: raise LLMBadRequestError(f"Default model is not available. " f"Please configure a Default System Reasoning Model " f"in the Settings -> Model Provider.") model_provider_name = default_model.provider_name model_name = default_model.model_name is_default_model = True # get model provider model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, model_provider_name) if not model_provider: raise ProviderTokenNotInitError(f"Model {model_name} provider credentials is not initialized.") # init text generation model model_class = model_provider.get_model_class(model_type=ModelType.TEXT_GENERATION) try: model_instance = model_class( model_provider=model_provider, name=model_name, model_kwargs=model_kwargs, streaming=streaming, callbacks=callbacks ) except LLMBadRequestError as e: if is_default_model: raise LLMBadRequestError(f"Default model {model_name} is not available. " f"Please check your model provider credentials.") else: raise e if is_default_model or not deduct_quota: model_instance.deduct_quota = False return model_instance @classmethod def get_embedding_model(cls, tenant_id: str, model_provider_name: Optional[str] = None, model_name: Optional[str] = None) -> Optional[BaseEmbedding]: """ get embedding model. :param tenant_id: a string representing the ID of the tenant. :param model_provider_name: :param model_name: :return: """ if model_provider_name is None and model_name is None: default_model = cls.get_default_model(tenant_id, ModelType.EMBEDDINGS) if not default_model: raise LLMBadRequestError(f"Default model is not available. " f"Please configure a Default Embedding Model " f"in the Settings -> Model Provider.") model_provider_name = default_model.provider_name model_name = default_model.model_name # get model provider model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, model_provider_name) if not model_provider: raise ProviderTokenNotInitError(f"Model {model_name} provider credentials is not initialized.") # init embedding model model_class = model_provider.get_model_class(model_type=ModelType.EMBEDDINGS) return model_class( model_provider=model_provider, name=model_name ) @classmethod def get_speech2text_model(cls, tenant_id: str, model_provider_name: Optional[str] = None, model_name: Optional[str] = None) -> Optional[BaseSpeech2Text]: """ get speech to text model. :param tenant_id: a string representing the ID of the tenant. :param model_provider_name: :param model_name: :return: """ if model_provider_name is None and model_name is None: default_model = cls.get_default_model(tenant_id, ModelType.SPEECH_TO_TEXT) if not default_model: raise LLMBadRequestError(f"Default model is not available. " f"Please configure a Default Speech-to-Text Model " f"in the Settings -> Model Provider.") model_provider_name = default_model.provider_name model_name = default_model.model_name # get model provider model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, model_provider_name) if not model_provider: raise ProviderTokenNotInitError(f"Model {model_name} provider credentials is not initialized.") # init speech to text model model_class = model_provider.get_model_class(model_type=ModelType.SPEECH_TO_TEXT) return model_class( model_provider=model_provider, name=model_name ) @classmethod def get_moderation_model(cls, tenant_id: str, model_provider_name: str, model_name: str) -> Optional[BaseModeration]: """ get moderation model. :param tenant_id: a string representing the ID of the tenant. :param model_provider_name: :param model_name: :return: """ # get model provider model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, model_provider_name) if not model_provider: raise ProviderTokenNotInitError(f"Model {model_name} provider credentials is not initialized.") # init moderation model model_class = model_provider.get_model_class(model_type=ModelType.MODERATION) return model_class( model_provider=model_provider, name=model_name ) @classmethod def get_default_model(cls, tenant_id: str, model_type: ModelType) -> TenantDefaultModel: """ get default model of model type. :param tenant_id: :param model_type: :return: """ # get default model default_model = db.session.query(TenantDefaultModel) \ .filter( TenantDefaultModel.tenant_id == tenant_id, TenantDefaultModel.model_type == model_type.value ).first() if not default_model: model_provider_rules = ModelProviderFactory.get_provider_rules() for model_provider_name, model_provider_rule in model_provider_rules.items(): model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, model_provider_name) if not model_provider: continue model_list = model_provider.get_supported_model_list(model_type) if model_list: model_info = model_list[0] default_model = TenantDefaultModel( tenant_id=tenant_id, model_type=model_type.value, provider_name=model_provider_name, model_name=model_info['id'] ) db.session.add(default_model) db.session.commit() break return default_model @classmethod def update_default_model(cls, tenant_id: str, model_type: ModelType, provider_name: str, model_name: str) -> TenantDefaultModel: """ update default model of model type. :param tenant_id: :param model_type: :param provider_name: :param model_name: :return: """ model_provider_name = ModelProviderFactory.get_provider_names() if provider_name not in model_provider_name: raise ValueError(f'Invalid provider name: {provider_name}') model_provider = ModelProviderFactory.get_preferred_model_provider(tenant_id, provider_name) if not model_provider: raise ProviderTokenNotInitError(f"Model {model_name} provider credentials is not initialized.") model_list = model_provider.get_supported_model_list(model_type) model_ids = [model['id'] for model in model_list] if model_name not in model_ids: raise ValueError(f'Invalid model name: {model_name}') # get default model default_model = db.session.query(TenantDefaultModel) \ .filter( TenantDefaultModel.tenant_id == tenant_id, TenantDefaultModel.model_type == model_type.value ).first() if default_model: # update default model default_model.provider_name = provider_name default_model.model_name = model_name db.session.commit() else: # create default model default_model = TenantDefaultModel( tenant_id=tenant_id, model_type=model_type.value, provider_name=provider_name, model_name=model_name, ) db.session.add(default_model) db.session.commit() return default_model
[]
2024-01-10
bhctest123/dify
api~core~orchestrator_rule_parser.py
import math from typing import Optional from flask import current_app from langchain import WikipediaAPIWrapper from langchain.callbacks.manager import Callbacks from langchain.memory.chat_memory import BaseChatMemory from langchain.tools import BaseTool, Tool, WikipediaQueryRun from pydantic import BaseModel, Field from core.agent.agent_executor import AgentExecutor, PlanningStrategy, AgentConfiguration from core.callback_handler.agent_loop_gather_callback_handler import AgentLoopGatherCallbackHandler from core.callback_handler.dataset_tool_callback_handler import DatasetToolCallbackHandler from core.callback_handler.main_chain_gather_callback_handler import MainChainGatherCallbackHandler from core.callback_handler.std_out_callback_handler import DifyStdOutCallbackHandler from core.chain.sensitive_word_avoidance_chain import SensitiveWordAvoidanceChain, SensitiveWordAvoidanceRule from core.conversation_message_task import ConversationMessageTask from core.model_providers.error import ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from core.model_providers.models.entity.model_params import ModelKwargs, ModelMode from core.model_providers.models.llm.base import BaseLLM from core.tool.current_datetime_tool import DatetimeTool from core.tool.dataset_retriever_tool import DatasetRetrieverTool from core.tool.provider.serpapi_provider import SerpAPIToolProvider from core.tool.serpapi_wrapper import OptimizedSerpAPIWrapper, OptimizedSerpAPIInput from core.tool.web_reader_tool import WebReaderTool from extensions.ext_database import db from models.dataset import Dataset, DatasetProcessRule from models.model import AppModelConfig from models.provider import ProviderType class OrchestratorRuleParser: """Parse the orchestrator rule to entities.""" def __init__(self, tenant_id: str, app_model_config: AppModelConfig): self.tenant_id = tenant_id self.app_model_config = app_model_config def to_agent_executor(self, conversation_message_task: ConversationMessageTask, memory: Optional[BaseChatMemory], rest_tokens: int, chain_callback: MainChainGatherCallbackHandler, return_resource: bool = False, retriever_from: str = 'dev') -> Optional[AgentExecutor]: if not self.app_model_config.agent_mode_dict: return None agent_mode_config = self.app_model_config.agent_mode_dict model_dict = self.app_model_config.model_dict chain = None if agent_mode_config and agent_mode_config.get('enabled'): tool_configs = agent_mode_config.get('tools', []) agent_provider_name = model_dict.get('provider', 'openai') agent_model_name = model_dict.get('name', 'gpt-4') agent_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_provider_name, model_name=agent_model_name, model_kwargs=ModelKwargs( temperature=0.2, top_p=0.3, max_tokens=1500 ) ) # add agent callback to record agent thoughts agent_callback = AgentLoopGatherCallbackHandler( model_instance=agent_model_instance, conversation_message_task=conversation_message_task ) chain_callback.agent_callback = agent_callback agent_model_instance.add_callbacks([agent_callback]) planning_strategy = PlanningStrategy(agent_mode_config.get('strategy', 'router')) # only OpenAI chat model (include Azure) support function call, use ReACT instead if agent_model_instance.model_mode != ModelMode.CHAT \ or agent_model_instance.model_provider.provider_name not in ['openai', 'azure_openai']: if planning_strategy in [PlanningStrategy.FUNCTION_CALL, PlanningStrategy.MULTI_FUNCTION_CALL]: planning_strategy = PlanningStrategy.REACT elif planning_strategy == PlanningStrategy.ROUTER: planning_strategy = PlanningStrategy.REACT_ROUTER try: summary_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_provider_name, model_name=agent_model_name, model_kwargs=ModelKwargs( temperature=0, max_tokens=500 ), deduct_quota=False ) except ProviderTokenNotInitError as e: summary_model_instance = None tools = self.to_tools( agent_model_instance=agent_model_instance, tool_configs=tool_configs, conversation_message_task=conversation_message_task, rest_tokens=rest_tokens, callbacks=[agent_callback, DifyStdOutCallbackHandler()], return_resource=return_resource, retriever_from=retriever_from ) if len(tools) == 0: return None agent_configuration = AgentConfiguration( strategy=planning_strategy, model_instance=agent_model_instance, tools=tools, summary_model_instance=summary_model_instance, memory=memory, callbacks=[chain_callback, agent_callback], max_iterations=10, max_execution_time=400.0, early_stopping_method="generate" ) return AgentExecutor(agent_configuration) return chain def to_sensitive_word_avoidance_chain(self, model_instance: BaseLLM, callbacks: Callbacks = None, **kwargs) \ -> Optional[SensitiveWordAvoidanceChain]: """ Convert app sensitive word avoidance config to chain :param model_instance: model instance :param callbacks: callbacks for the chain :param kwargs: :return: """ sensitive_word_avoidance_rule = None if self.app_model_config.sensitive_word_avoidance_dict: sensitive_word_avoidance_config = self.app_model_config.sensitive_word_avoidance_dict if sensitive_word_avoidance_config.get("enabled", False): if sensitive_word_avoidance_config.get('type') == 'moderation': sensitive_word_avoidance_rule = SensitiveWordAvoidanceRule( type=SensitiveWordAvoidanceRule.Type.MODERATION, canned_response=sensitive_word_avoidance_config.get("canned_response") if sensitive_word_avoidance_config.get("canned_response") else 'Your content violates our usage policy. Please revise and try again.', ) else: sensitive_words = sensitive_word_avoidance_config.get("words", "") if sensitive_words: sensitive_word_avoidance_rule = SensitiveWordAvoidanceRule( type=SensitiveWordAvoidanceRule.Type.KEYWORDS, canned_response=sensitive_word_avoidance_config.get("canned_response") if sensitive_word_avoidance_config.get("canned_response") else 'Your content violates our usage policy. Please revise and try again.', extra_params={ 'sensitive_words': sensitive_words.split(','), } ) if sensitive_word_avoidance_rule: return SensitiveWordAvoidanceChain( model_instance=model_instance, sensitive_word_avoidance_rule=sensitive_word_avoidance_rule, output_key="sensitive_word_avoidance_output", callbacks=callbacks, **kwargs ) return None def to_tools(self, agent_model_instance: BaseLLM, tool_configs: list, conversation_message_task: ConversationMessageTask, rest_tokens: int, callbacks: Callbacks = None, return_resource: bool = False, retriever_from: str = 'dev') -> list[BaseTool]: """ Convert app agent tool configs to tools :param agent_model_instance: :param rest_tokens: :param tool_configs: app agent tool configs :param conversation_message_task: :param callbacks: :param return_resource: :param retriever_from: :return: """ tools = [] for tool_config in tool_configs: tool_type = list(tool_config.keys())[0] tool_val = list(tool_config.values())[0] if not tool_val.get("enabled") or tool_val.get("enabled") is not True: continue tool = None if tool_type == "dataset": tool = self.to_dataset_retriever_tool(tool_val, conversation_message_task, rest_tokens, return_resource, retriever_from) elif tool_type == "web_reader": tool = self.to_web_reader_tool(agent_model_instance) elif tool_type == "google_search": tool = self.to_google_search_tool() elif tool_type == "wikipedia": tool = self.to_wikipedia_tool() elif tool_type == "current_datetime": tool = self.to_current_datetime_tool() if tool: tool.callbacks.extend(callbacks) tools.append(tool) return tools def to_dataset_retriever_tool(self, tool_config: dict, conversation_message_task: ConversationMessageTask, rest_tokens: int, return_resource: bool = False, retriever_from: str = 'dev') \ -> Optional[BaseTool]: """ A dataset tool is a tool that can be used to retrieve information from a dataset :param rest_tokens: :param tool_config: :param conversation_message_task: :param return_resource: :param retriever_from: :return: """ # get dataset from dataset id dataset = db.session.query(Dataset).filter( Dataset.tenant_id == self.tenant_id, Dataset.id == tool_config.get("id") ).first() if not dataset: return None if dataset and dataset.available_document_count == 0 and dataset.available_document_count == 0: return None k = self._dynamic_calc_retrieve_k(dataset, rest_tokens) tool = DatasetRetrieverTool.from_dataset( dataset=dataset, k=k, callbacks=[DatasetToolCallbackHandler(conversation_message_task)], conversation_message_task=conversation_message_task, return_resource=return_resource, retriever_from=retriever_from ) return tool def to_web_reader_tool(self, agent_model_instance: BaseLLM) -> Optional[BaseTool]: """ A tool for reading web pages :return: """ try: summary_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_model_instance.model_provider.provider_name, model_name=agent_model_instance.name, model_kwargs=ModelKwargs( temperature=0, max_tokens=500 ), deduct_quota=False ) except ProviderTokenNotInitError: summary_model_instance = None tool = WebReaderTool( llm=summary_model_instance.client if summary_model_instance else None, max_chunk_length=4000, continue_reading=True, callbacks=[DifyStdOutCallbackHandler()] ) return tool def to_google_search_tool(self) -> Optional[BaseTool]: tool_provider = SerpAPIToolProvider(tenant_id=self.tenant_id) func_kwargs = tool_provider.credentials_to_func_kwargs() if not func_kwargs: return None tool = Tool( name="google_search", description="A tool for performing a Google search and extracting snippets and webpages " "when you need to search for something you don't know or when your information " "is not up to date. " "Input should be a search query.", func=OptimizedSerpAPIWrapper(**func_kwargs).run, args_schema=OptimizedSerpAPIInput, callbacks=[DifyStdOutCallbackHandler()] ) return tool def to_current_datetime_tool(self) -> Optional[BaseTool]: tool = DatetimeTool( callbacks=[DifyStdOutCallbackHandler()] ) return tool def to_wikipedia_tool(self) -> Optional[BaseTool]: class WikipediaInput(BaseModel): query: str = Field(..., description="search query.") return WikipediaQueryRun( name="wikipedia", api_wrapper=WikipediaAPIWrapper(doc_content_chars_max=4000), args_schema=WikipediaInput, callbacks=[DifyStdOutCallbackHandler()] ) @classmethod def _dynamic_calc_retrieve_k(cls, dataset: Dataset, rest_tokens: int) -> int: DEFAULT_K = 2 CONTEXT_TOKENS_PERCENT = 0.3 MAX_K = 10 if rest_tokens == -1: return DEFAULT_K processing_rule = dataset.latest_process_rule if not processing_rule: return DEFAULT_K if processing_rule.mode == "custom": rules = processing_rule.rules_dict if not rules: return DEFAULT_K segmentation = rules["segmentation"] segment_max_tokens = segmentation["max_tokens"] else: segment_max_tokens = DatasetProcessRule.AUTOMATIC_RULES['segmentation']['max_tokens'] # when rest_tokens is less than default context tokens if rest_tokens < segment_max_tokens * DEFAULT_K: return rest_tokens // segment_max_tokens context_limit_tokens = math.floor(rest_tokens * CONTEXT_TOKENS_PERCENT) # when context_limit_tokens is less than default context tokens, use default_k if context_limit_tokens <= segment_max_tokens * DEFAULT_K: return DEFAULT_K # Expand the k value when there's still some room left in the 30% rest tokens space, but less than the MAX_K return min(context_limit_tokens // segment_max_tokens, MAX_K)
[]
2024-01-10
bhctest123/dify
api~core~callback_handler~dataset_tool_callback_handler.py
import json import logging from json import JSONDecodeError from typing import Any, Dict, List, Union, Optional from langchain.callbacks.base import BaseCallbackHandler from core.callback_handler.entity.dataset_query import DatasetQueryObj from core.conversation_message_task import ConversationMessageTask class DatasetToolCallbackHandler(BaseCallbackHandler): """Callback Handler that prints to std out.""" raise_error: bool = True def __init__(self, conversation_message_task: ConversationMessageTask) -> None: """Initialize callback handler.""" self.queries = [] self.conversation_message_task = conversation_message_task @property def always_verbose(self) -> bool: """Whether to call verbose callbacks even if verbose is False.""" return True @property def ignore_llm(self) -> bool: """Whether to ignore LLM callbacks.""" return True @property def ignore_chain(self) -> bool: """Whether to ignore chain callbacks.""" return True @property def ignore_agent(self) -> bool: """Whether to ignore agent callbacks.""" return False def on_tool_start( self, serialized: Dict[str, Any], input_str: str, **kwargs: Any, ) -> None: tool_name: str = serialized.get('name') dataset_id = tool_name.removeprefix('dataset-') try: input_dict = json.loads(input_str.replace("'", "\"")) query = input_dict.get('query') except JSONDecodeError: query = input_str self.conversation_message_task.on_dataset_query_end(DatasetQueryObj(dataset_id=dataset_id, query=query)) def on_tool_end( self, output: str, color: Optional[str] = None, observation_prefix: Optional[str] = None, llm_prefix: Optional[str] = None, **kwargs: Any, ) -> None: pass def on_tool_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Do nothing.""" logging.debug("Dataset tool on_llm_error: %s", error)
[]
2024-01-10
bhctest123/dify
api~core~chain~sensitive_word_avoidance_chain.py
import enum import logging from typing import List, Dict, Optional, Any import openai from flask import current_app from langchain.callbacks.manager import CallbackManagerForChainRun from langchain.chains.base import Chain from openai import InvalidRequestError from openai.error import APIConnectionError, APIError, ServiceUnavailableError, Timeout, RateLimitError, \ AuthenticationError, OpenAIError from pydantic import BaseModel from core.model_providers.error import LLMBadRequestError from core.model_providers.model_factory import ModelFactory from core.model_providers.models.llm.base import BaseLLM from core.model_providers.models.moderation import openai_moderation class SensitiveWordAvoidanceRule(BaseModel): class Type(enum.Enum): MODERATION = "moderation" KEYWORDS = "keywords" type: Type canned_response: str = 'Your content violates our usage policy. Please revise and try again.' extra_params: dict = {} class SensitiveWordAvoidanceChain(Chain): input_key: str = "input" #: :meta private: output_key: str = "output" #: :meta private: model_instance: BaseLLM sensitive_word_avoidance_rule: SensitiveWordAvoidanceRule @property def _chain_type(self) -> str: return "sensitive_word_avoidance_chain" @property def input_keys(self) -> List[str]: """Expect input key. :meta private: """ return [self.input_key] @property def output_keys(self) -> List[str]: """Return output key. :meta private: """ return [self.output_key] def _check_sensitive_word(self, text: str) -> bool: for word in self.sensitive_word_avoidance_rule.extra_params.get('sensitive_words', []): if word in text: return False return True def _check_moderation(self, text: str) -> bool: moderation_model_instance = ModelFactory.get_moderation_model( tenant_id=self.model_instance.model_provider.provider.tenant_id, model_provider_name='openai', model_name=openai_moderation.DEFAULT_MODEL ) try: return moderation_model_instance.run(text=text) except Exception as ex: logging.exception(ex) raise LLMBadRequestError('Rate limit exceeded, please try again later.') def _call( self, inputs: Dict[str, Any], run_manager: Optional[CallbackManagerForChainRun] = None, ) -> Dict[str, Any]: text = inputs[self.input_key] if self.sensitive_word_avoidance_rule.type == SensitiveWordAvoidanceRule.Type.KEYWORDS: result = self._check_sensitive_word(text) else: result = self._check_moderation(text) if not result: raise LLMBadRequestError(self.sensitive_word_avoidance_rule.canned_response) return {self.output_key: text}
[]
2024-01-10
bhctest123/dify
api~core~helper~moderation.py
import logging import openai from flask import current_app from core.model_providers.error import LLMBadRequestError from core.model_providers.providers.base import BaseModelProvider from models.provider import ProviderType def check_moderation(model_provider: BaseModelProvider, text: str) -> bool: if current_app.config['HOSTED_MODERATION_ENABLED'] and current_app.config['HOSTED_MODERATION_PROVIDERS']: moderation_providers = current_app.config['HOSTED_MODERATION_PROVIDERS'].split(',') if model_provider.provider.provider_type == ProviderType.SYSTEM.value \ and model_provider.provider_name in moderation_providers: # 2000 text per chunk length = 2000 chunks = [text[i:i + length] for i in range(0, len(text), length)] try: moderation_result = openai.Moderation.create(input=chunks, api_key=current_app.config['HOSTED_OPENAI_API_KEY']) except Exception as ex: logging.exception(ex) raise LLMBadRequestError('Rate limit exceeded, please try again later.') for result in moderation_result.results: if result['flagged'] is True: return False return True
[]
2024-01-10
bhctest123/dify
api~core~index~keyword_table_index~keyword_table_index.py
import json from collections import defaultdict from typing import Any, List, Optional, Dict from langchain.schema import Document, BaseRetriever from pydantic import BaseModel, Field, Extra from core.index.base import BaseIndex from core.index.keyword_table_index.jieba_keyword_table_handler import JiebaKeywordTableHandler from extensions.ext_database import db from models.dataset import Dataset, DocumentSegment, DatasetKeywordTable class KeywordTableConfig(BaseModel): max_keywords_per_chunk: int = 10 class KeywordTableIndex(BaseIndex): def __init__(self, dataset: Dataset, config: KeywordTableConfig = KeywordTableConfig()): super().__init__(dataset) self._config = config def create(self, texts: list[Document], **kwargs) -> BaseIndex: keyword_table_handler = JiebaKeywordTableHandler() keyword_table = {} for text in texts: keywords = keyword_table_handler.extract_keywords(text.page_content, self._config.max_keywords_per_chunk) self._update_segment_keywords(self.dataset.id, text.metadata['doc_id'], list(keywords)) keyword_table = self._add_text_to_keyword_table(keyword_table, text.metadata['doc_id'], list(keywords)) dataset_keyword_table = DatasetKeywordTable( dataset_id=self.dataset.id, keyword_table=json.dumps({ '__type__': 'keyword_table', '__data__': { "index_id": self.dataset.id, "summary": None, "table": {} } }, cls=SetEncoder) ) db.session.add(dataset_keyword_table) db.session.commit() self._save_dataset_keyword_table(keyword_table) return self def add_texts(self, texts: list[Document], **kwargs): keyword_table_handler = JiebaKeywordTableHandler() keyword_table = self._get_dataset_keyword_table() for text in texts: keywords = keyword_table_handler.extract_keywords(text.page_content, self._config.max_keywords_per_chunk) self._update_segment_keywords(self.dataset.id, text.metadata['doc_id'], list(keywords)) keyword_table = self._add_text_to_keyword_table(keyword_table, text.metadata['doc_id'], list(keywords)) self._save_dataset_keyword_table(keyword_table) def text_exists(self, id: str) -> bool: keyword_table = self._get_dataset_keyword_table() return id in set.union(*keyword_table.values()) def delete_by_ids(self, ids: list[str]) -> None: keyword_table = self._get_dataset_keyword_table() keyword_table = self._delete_ids_from_keyword_table(keyword_table, ids) self._save_dataset_keyword_table(keyword_table) def delete_by_document_id(self, document_id: str): # get segment ids by document_id segments = db.session.query(DocumentSegment).filter( DocumentSegment.dataset_id == self.dataset.id, DocumentSegment.document_id == document_id ).all() ids = [segment.index_node_id for segment in segments] keyword_table = self._get_dataset_keyword_table() keyword_table = self._delete_ids_from_keyword_table(keyword_table, ids) self._save_dataset_keyword_table(keyword_table) def get_retriever(self, **kwargs: Any) -> BaseRetriever: return KeywordTableRetriever(index=self, **kwargs) def search( self, query: str, **kwargs: Any ) -> List[Document]: keyword_table = self._get_dataset_keyword_table() search_kwargs = kwargs.get('search_kwargs') if kwargs.get('search_kwargs') else {} k = search_kwargs.get('k') if search_kwargs.get('k') else 4 sorted_chunk_indices = self._retrieve_ids_by_query(keyword_table, query, k) documents = [] for chunk_index in sorted_chunk_indices: segment = db.session.query(DocumentSegment).filter( DocumentSegment.dataset_id == self.dataset.id, DocumentSegment.index_node_id == chunk_index ).first() if segment: documents.append(Document( page_content=segment.content, metadata={ "doc_id": chunk_index, "document_id": segment.document_id, "dataset_id": segment.dataset_id, } )) return documents def delete(self) -> None: dataset_keyword_table = self.dataset.dataset_keyword_table if dataset_keyword_table: db.session.delete(dataset_keyword_table) db.session.commit() def _save_dataset_keyword_table(self, keyword_table): keyword_table_dict = { '__type__': 'keyword_table', '__data__': { "index_id": self.dataset.id, "summary": None, "table": keyword_table } } self.dataset.dataset_keyword_table.keyword_table = json.dumps(keyword_table_dict, cls=SetEncoder) db.session.commit() def _get_dataset_keyword_table(self) -> Optional[dict]: dataset_keyword_table = self.dataset.dataset_keyword_table if dataset_keyword_table: if dataset_keyword_table.keyword_table_dict: return dataset_keyword_table.keyword_table_dict['__data__']['table'] else: dataset_keyword_table = DatasetKeywordTable( dataset_id=self.dataset.id, keyword_table=json.dumps({ '__type__': 'keyword_table', '__data__': { "index_id": self.dataset.id, "summary": None, "table": {} } }, cls=SetEncoder) ) db.session.add(dataset_keyword_table) db.session.commit() return {} def _add_text_to_keyword_table(self, keyword_table: dict, id: str, keywords: list[str]) -> dict: for keyword in keywords: if keyword not in keyword_table: keyword_table[keyword] = set() keyword_table[keyword].add(id) return keyword_table def _delete_ids_from_keyword_table(self, keyword_table: dict, ids: list[str]) -> dict: # get set of ids that correspond to node node_idxs_to_delete = set(ids) # delete node_idxs from keyword to node idxs mapping keywords_to_delete = set() for keyword, node_idxs in keyword_table.items(): if node_idxs_to_delete.intersection(node_idxs): keyword_table[keyword] = node_idxs.difference( node_idxs_to_delete ) if not keyword_table[keyword]: keywords_to_delete.add(keyword) for keyword in keywords_to_delete: del keyword_table[keyword] return keyword_table def _retrieve_ids_by_query(self, keyword_table: dict, query: str, k: int = 4): keyword_table_handler = JiebaKeywordTableHandler() keywords = keyword_table_handler.extract_keywords(query) # go through text chunks in order of most matching keywords chunk_indices_count: Dict[str, int] = defaultdict(int) keywords = [keyword for keyword in keywords if keyword in set(keyword_table.keys())] for keyword in keywords: for node_id in keyword_table[keyword]: chunk_indices_count[node_id] += 1 sorted_chunk_indices = sorted( list(chunk_indices_count.keys()), key=lambda x: chunk_indices_count[x], reverse=True, ) return sorted_chunk_indices[: k] def _update_segment_keywords(self, dataset_id: str, node_id: str, keywords: List[str]): document_segment = db.session.query(DocumentSegment).filter( DocumentSegment.dataset_id == dataset_id, DocumentSegment.index_node_id == node_id ).first() if document_segment: document_segment.keywords = keywords db.session.commit() def create_segment_keywords(self, node_id: str, keywords: List[str]): keyword_table = self._get_dataset_keyword_table() self._update_segment_keywords(self.dataset.id, node_id, keywords) keyword_table = self._add_text_to_keyword_table(keyword_table, node_id, keywords) self._save_dataset_keyword_table(keyword_table) def update_segment_keywords_index(self, node_id: str, keywords: List[str]): keyword_table = self._get_dataset_keyword_table() keyword_table = self._add_text_to_keyword_table(keyword_table, node_id, keywords) self._save_dataset_keyword_table(keyword_table) class KeywordTableRetriever(BaseRetriever, BaseModel): index: KeywordTableIndex search_kwargs: dict = Field(default_factory=dict) class Config: """Configuration for this pydantic object.""" extra = Extra.forbid arbitrary_types_allowed = True def get_relevant_documents(self, query: str) -> List[Document]: """Get documents relevant for a query. Args: query: string to find relevant documents for Returns: List of relevant documents """ return self.index.search(query, **self.search_kwargs) async def aget_relevant_documents(self, query: str) -> List[Document]: raise NotImplementedError("KeywordTableRetriever does not support async") class SetEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, set): return list(obj) return super().default(obj)
[]
2024-01-10
bhctest123/dify
api~core~callback_handler~llm_callback_handler.py
import logging from typing import Any, Dict, List, Union from langchain.callbacks.base import BaseCallbackHandler from langchain.schema import LLMResult, BaseMessage from core.callback_handler.entity.llm_message import LLMMessage from core.conversation_message_task import ConversationMessageTask, ConversationTaskStoppedException from core.model_providers.models.entity.message import to_prompt_messages, PromptMessage from core.model_providers.models.llm.base import BaseLLM class LLMCallbackHandler(BaseCallbackHandler): raise_error: bool = True def __init__(self, model_instance: BaseLLM, conversation_message_task: ConversationMessageTask): self.model_instance = model_instance self.llm_message = LLMMessage() self.start_at = None self.conversation_message_task = conversation_message_task @property def always_verbose(self) -> bool: """Whether to call verbose callbacks even if verbose is False.""" return True def on_chat_model_start( self, serialized: Dict[str, Any], messages: List[List[BaseMessage]], **kwargs: Any ) -> Any: real_prompts = [] for message in messages[0]: if message.type == 'human': role = 'user' elif message.type == 'ai': role = 'assistant' else: role = 'system' real_prompts.append({ "role": role, "text": message.content }) self.llm_message.prompt = real_prompts self.llm_message.prompt_tokens = self.model_instance.get_num_tokens(to_prompt_messages(messages[0])) def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any ) -> None: self.llm_message.prompt = [{ "role": 'user', "text": prompts[0] }] self.llm_message.prompt_tokens = self.model_instance.get_num_tokens([PromptMessage(content=prompts[0])]) def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: if not self.conversation_message_task.streaming: self.conversation_message_task.append_message_text(response.generations[0][0].text) self.llm_message.completion = response.generations[0][0].text self.llm_message.completion_tokens = self.model_instance.get_num_tokens([PromptMessage(content=self.llm_message.completion)]) self.conversation_message_task.save_message(self.llm_message) def on_llm_new_token(self, token: str, **kwargs: Any) -> None: try: self.conversation_message_task.append_message_text(token) except ConversationTaskStoppedException as ex: self.on_llm_error(error=ex) raise ex self.llm_message.completion += token def on_llm_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Do nothing.""" if isinstance(error, ConversationTaskStoppedException): if self.conversation_message_task.streaming: self.llm_message.completion_tokens = self.model_instance.get_num_tokens( [PromptMessage(content=self.llm_message.completion)] ) self.conversation_message_task.save_message(llm_message=self.llm_message, by_stopped=True) else: logging.debug("on_llm_error: %s", error)
[ "[]" ]
2024-01-10
bhctest123/dify
api~core~index~vector_index~qdrant_vector_index.py
import os from typing import Optional, Any, List, cast import qdrant_client from langchain.embeddings.base import Embeddings from langchain.schema import Document, BaseRetriever from langchain.vectorstores import VectorStore from pydantic import BaseModel from core.index.base import BaseIndex from core.index.vector_index.base import BaseVectorIndex from core.vector_store.qdrant_vector_store import QdrantVectorStore from models.dataset import Dataset class QdrantConfig(BaseModel): endpoint: str api_key: Optional[str] root_path: Optional[str] def to_qdrant_params(self): if self.endpoint and self.endpoint.startswith('path:'): path = self.endpoint.replace('path:', '') if not os.path.isabs(path): path = os.path.join(self.root_path, path) return { 'path': path } else: return { 'url': self.endpoint, 'api_key': self.api_key, } class QdrantVectorIndex(BaseVectorIndex): def __init__(self, dataset: Dataset, config: QdrantConfig, embeddings: Embeddings): super().__init__(dataset, embeddings) self._client_config = config def get_type(self) -> str: return 'qdrant' def get_index_name(self, dataset: Dataset) -> str: if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix class_prefix += '_Node' return class_prefix dataset_id = dataset.id return "Vector_index_" + dataset_id.replace("-", "_") + '_Node' def to_index_struct(self) -> dict: return { "type": self.get_type(), "vector_store": {"class_prefix": self.get_index_name(self.dataset)} } def create(self, texts: list[Document], **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = QdrantVectorStore.from_documents( texts, self._embeddings, collection_name=self.get_index_name(self.dataset), ids=uuids, content_payload_key='page_content', **self._client_config.to_qdrant_params() ) return self def _get_vector_store(self) -> VectorStore: """Only for created index.""" if self._vector_store: return self._vector_store attributes = ['doc_id', 'dataset_id', 'document_id'] if self._is_origin(): attributes = ['doc_id'] client = qdrant_client.QdrantClient( **self._client_config.to_qdrant_params() ) return QdrantVectorStore( client=client, collection_name=self.get_index_name(self.dataset), embeddings=self._embeddings, content_payload_key='page_content' ) def _get_vector_store_class(self) -> type: return QdrantVectorStore def delete_by_document_id(self, document_id: str): if self._is_origin(): self.recreate_dataset(self.dataset) return vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="metadata.document_id", match=models.MatchValue(value=document_id), ), ], )) def delete_by_ids(self, ids: list[str]) -> None: if self._is_origin(): self.recreate_dataset(self.dataset) return vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models for node_id in ids: vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="metadata.doc_id", match=models.MatchValue(value=node_id), ), ], )) def _is_origin(self): if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix return True return False
[]
2024-01-10
bhctest123/dify
api~commands.py
import datetime import json import math import random import string import time import click from tqdm import tqdm from flask import current_app from langchain.embeddings import OpenAIEmbeddings from werkzeug.exceptions import NotFound from core.embedding.cached_embedding import CacheEmbedding from core.index.index import IndexBuilder from core.model_providers.model_factory import ModelFactory from core.model_providers.models.embedding.openai_embedding import OpenAIEmbedding from core.model_providers.models.entity.model_params import ModelType from core.model_providers.providers.hosted import hosted_model_providers from core.model_providers.providers.openai_provider import OpenAIProvider from libs.password import password_pattern, valid_password, hash_password from libs.helper import email as email_validate from extensions.ext_database import db from libs.rsa import generate_key_pair from models.account import InvitationCode, Tenant, TenantAccountJoin from models.dataset import Dataset, DatasetQuery, Document from models.model import Account, AppModelConfig, App import secrets import base64 from models.provider import Provider, ProviderType, ProviderQuotaType, ProviderModel @click.command('reset-password', help='Reset the account password.') @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset') @click.option('--new-password', prompt=True, help='the new password.') @click.option('--password-confirm', prompt=True, help='the new password confirm.') def reset_password(email, new_password, password_confirm): if str(new_password).strip() != str(password_confirm).strip(): click.echo(click.style('sorry. The two passwords do not match.', fg='red')) return account = db.session.query(Account). \ filter(Account.email == email). \ one_or_none() if not account: click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red')) return try: valid_password(new_password) except: click.echo( click.style('sorry. The passwords must match {} '.format(password_pattern), fg='red')) return # generate password salt salt = secrets.token_bytes(16) base64_salt = base64.b64encode(salt).decode() # encrypt password with salt password_hashed = hash_password(new_password, salt) base64_password_hashed = base64.b64encode(password_hashed).decode() account.password = base64_password_hashed account.password_salt = base64_salt db.session.commit() click.echo(click.style('Congratulations!, password has been reset.', fg='green')) @click.command('reset-email', help='Reset the account email.') @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset') @click.option('--new-email', prompt=True, help='the new email.') @click.option('--email-confirm', prompt=True, help='the new email confirm.') def reset_email(email, new_email, email_confirm): if str(new_email).strip() != str(email_confirm).strip(): click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red')) return account = db.session.query(Account). \ filter(Account.email == email). \ one_or_none() if not account: click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red')) return try: email_validate(new_email) except: click.echo( click.style('sorry. {} is not a valid email. '.format(email), fg='red')) return account.email = new_email db.session.commit() click.echo(click.style('Congratulations!, email has been reset.', fg='green')) @click.command('reset-encrypt-key-pair', help='Reset the asymmetric key pair of workspace for encrypt LLM credentials. ' 'After the reset, all LLM credentials will become invalid, ' 'requiring re-entry.' 'Only support SELF_HOSTED mode.') @click.confirmation_option(prompt=click.style('Are you sure you want to reset encrypt key pair?' ' this operation cannot be rolled back!', fg='red')) def reset_encrypt_key_pair(): if current_app.config['EDITION'] != 'SELF_HOSTED': click.echo(click.style('Sorry, only support SELF_HOSTED mode.', fg='red')) return tenant = db.session.query(Tenant).first() if not tenant: click.echo(click.style('Sorry, no workspace found. Please enter /install to initialize.', fg='red')) return tenant.encrypt_public_key = generate_key_pair(tenant.id) db.session.query(Provider).filter(Provider.provider_type == 'custom').delete() db.session.query(ProviderModel).delete() db.session.commit() click.echo(click.style('Congratulations! ' 'the asymmetric key pair of workspace {} has been reset.'.format(tenant.id), fg='green')) @click.command('generate-invitation-codes', help='Generate invitation codes.') @click.option('--batch', help='The batch of invitation codes.') @click.option('--count', prompt=True, help='Invitation codes count.') def generate_invitation_codes(batch, count): if not batch: now = datetime.datetime.now() batch = now.strftime('%Y%m%d%H%M%S') if not count or int(count) <= 0: click.echo(click.style('sorry. the count must be greater than 0.', fg='red')) return count = int(count) click.echo('Start generate {} invitation codes for batch {}.'.format(count, batch)) codes = '' for i in range(count): code = generate_invitation_code() invitation_code = InvitationCode( code=code, batch=batch ) db.session.add(invitation_code) click.echo(code) codes += code + "\n" db.session.commit() filename = 'storage/invitation-codes-{}.txt'.format(batch) with open(filename, 'w') as f: f.write(codes) click.echo(click.style( 'Congratulations! Generated {} invitation codes for batch {} and saved to the file \'{}\''.format(count, batch, filename), fg='green')) def generate_invitation_code(): code = generate_upper_string() while db.session.query(InvitationCode).filter(InvitationCode.code == code).count() > 0: code = generate_upper_string() return code def generate_upper_string(): letters_digits = string.ascii_uppercase + string.digits result = "" for i in range(8): result += random.choice(letters_digits) return result @click.command('recreate-all-dataset-indexes', help='Recreate all dataset indexes.') def recreate_all_dataset_indexes(): click.echo(click.style('Start recreate all dataset indexes.', fg='green')) recreate_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: try: click.echo('Recreating dataset index: {}'.format(dataset.id)) index = IndexBuilder.get_index(dataset, 'high_quality') if index and index._is_origin(): index.recreate_dataset(dataset) recreate_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Recreate dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Recreate {} dataset indexes.'.format(recreate_count), fg='green')) @click.command('clean-unused-dataset-indexes', help='Clean unused dataset indexes.') def clean_unused_dataset_indexes(): click.echo(click.style('Start clean unused dataset indexes.', fg='green')) clean_days = int(current_app.config.get('CLEAN_DAY_SETTING')) start_at = time.perf_counter() thirty_days_ago = datetime.datetime.now() - datetime.timedelta(days=clean_days) page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.created_at < thirty_days_ago) \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: dataset_query = db.session.query(DatasetQuery).filter( DatasetQuery.created_at > thirty_days_ago, DatasetQuery.dataset_id == dataset.id ).all() if not dataset_query or len(dataset_query) == 0: documents = db.session.query(Document).filter( Document.dataset_id == dataset.id, Document.indexing_status == 'completed', Document.enabled == True, Document.archived == False, Document.updated_at > thirty_days_ago ).all() if not documents or len(documents) == 0: try: # remove index vector_index = IndexBuilder.get_index(dataset, 'high_quality') kw_index = IndexBuilder.get_index(dataset, 'economy') # delete from vector index if vector_index: vector_index.delete() kw_index.delete() # update document update_params = { Document.enabled: False } Document.query.filter_by(dataset_id=dataset.id).update(update_params) db.session.commit() click.echo(click.style('Cleaned unused dataset {} from db success!'.format(dataset.id), fg='green')) except Exception as e: click.echo( click.style('clean dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) end_at = time.perf_counter() click.echo(click.style('Cleaned unused dataset from db success latency: {}'.format(end_at - start_at), fg='green')) @click.command('sync-anthropic-hosted-providers', help='Sync anthropic hosted providers.') def sync_anthropic_hosted_providers(): if not hosted_model_providers.anthropic: click.echo(click.style('Anthropic hosted provider is not configured.', fg='red')) return click.echo(click.style('Start sync anthropic hosted providers.', fg='green')) count = 0 new_quota_limit = hosted_model_providers.anthropic.quota_limit page = 1 while True: try: providers = db.session.query(Provider).filter( Provider.provider_name == 'anthropic', Provider.provider_type == ProviderType.SYSTEM.value, Provider.quota_type == ProviderQuotaType.TRIAL.value, Provider.quota_limit != new_quota_limit ).order_by(Provider.created_at.desc()).paginate(page=page, per_page=100) except NotFound: break page += 1 for provider in providers: try: click.echo('Syncing tenant anthropic hosted provider: {}, origin: limit {}, used {}' .format(provider.tenant_id, provider.quota_limit, provider.quota_used)) original_quota_limit = provider.quota_limit division = math.ceil(new_quota_limit / 1000) provider.quota_limit = new_quota_limit if original_quota_limit == 1000 \ else original_quota_limit * division provider.quota_used = division * provider.quota_used db.session.commit() count += 1 except Exception as e: click.echo(click.style( 'Sync tenant anthropic hosted provider error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Synced {} anthropic hosted providers.'.format(count), fg='green')) @click.command('create-qdrant-indexes', help='Create qdrant indexes.') def create_qdrant_indexes(): click.echo(click.style('Start create qdrant indexes.', fg='green')) create_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: if dataset.index_struct_dict: if dataset.index_struct_dict['type'] != 'qdrant': try: click.echo('Create dataset qdrant index: {}'.format(dataset.id)) try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except Exception: try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id ) dataset.embedding_model = embedding_model.name dataset.embedding_model_provider = embedding_model.model_provider.provider_name except Exception: provider = Provider( id='provider_id', tenant_id=dataset.tenant_id, provider_name='openai', provider_type=ProviderType.SYSTEM.value, encrypted_config=json.dumps({'openai_api_key': 'TEST'}), is_valid=True, ) model_provider = OpenAIProvider(provider=provider) embedding_model = OpenAIEmbedding(name="text-embedding-ada-002", model_provider=model_provider) embeddings = CacheEmbedding(embedding_model) from core.index.vector_index.qdrant_vector_index import QdrantVectorIndex, QdrantConfig index = QdrantVectorIndex( dataset=dataset, config=QdrantConfig( endpoint=current_app.config.get('QDRANT_URL'), api_key=current_app.config.get('QDRANT_API_KEY'), root_path=current_app.root_path ), embeddings=embeddings ) if index: index.create_qdrant_dataset(dataset) index_struct = { "type": 'qdrant', "vector_store": {"class_prefix": dataset.index_struct_dict['vector_store']['class_prefix']} } dataset.index_struct = json.dumps(index_struct) db.session.commit() create_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Create dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Create {} dataset indexes.'.format(create_count), fg='green')) @click.command('update-qdrant-indexes', help='Update qdrant indexes.') def update_qdrant_indexes(): click.echo(click.style('Start Update qdrant indexes.', fg='green')) create_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: if dataset.index_struct_dict: if dataset.index_struct_dict['type'] != 'qdrant': try: click.echo('Update dataset qdrant index: {}'.format(dataset.id)) try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except Exception: provider = Provider( id='provider_id', tenant_id=dataset.tenant_id, provider_name='openai', provider_type=ProviderType.CUSTOM.value, encrypted_config=json.dumps({'openai_api_key': 'TEST'}), is_valid=True, ) model_provider = OpenAIProvider(provider=provider) embedding_model = OpenAIEmbedding(name="text-embedding-ada-002", model_provider=model_provider) embeddings = CacheEmbedding(embedding_model) from core.index.vector_index.qdrant_vector_index import QdrantVectorIndex, QdrantConfig index = QdrantVectorIndex( dataset=dataset, config=QdrantConfig( endpoint=current_app.config.get('QDRANT_URL'), api_key=current_app.config.get('QDRANT_API_KEY'), root_path=current_app.root_path ), embeddings=embeddings ) if index: index.update_qdrant_dataset(dataset) create_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Create dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Update {} dataset indexes.'.format(create_count), fg='green')) @click.command('update_app_model_configs', help='Migrate data to support paragraph variable.') @click.option("--batch-size", default=500, help="Number of records to migrate in each batch.") def update_app_model_configs(batch_size): pre_prompt_template = '{{default_input}}' user_input_form_template = { "en-US": [ { "paragraph": { "label": "Query", "variable": "default_input", "required": False, "default": "" } } ], "zh-Hans": [ { "paragraph": { "label": "查询内容", "variable": "default_input", "required": False, "default": "" } } ] } click.secho("Start migrate old data that the text generator can support paragraph variable.", fg='green') total_records = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .count() if total_records == 0: click.secho("No data to migrate.", fg='green') return num_batches = (total_records + batch_size - 1) // batch_size with tqdm(total=total_records, desc="Migrating Data") as pbar: for i in range(num_batches): offset = i * batch_size limit = min(batch_size, total_records - offset) click.secho(f"Fetching batch {i+1}/{num_batches} from source database...", fg='green') data_batch = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .order_by(App.created_at) \ .offset(offset).limit(limit).all() if not data_batch: click.secho("No more data to migrate.", fg='green') break try: click.secho(f"Migrating {len(data_batch)} records...", fg='green') for data in data_batch: # click.secho(f"Migrating data {data.id}, pre_prompt: {data.pre_prompt}, user_input_form: {data.user_input_form}", fg='green') if data.pre_prompt is None: data.pre_prompt = pre_prompt_template else: if pre_prompt_template in data.pre_prompt: continue data.pre_prompt += pre_prompt_template app_data = db.session.query(App) \ .filter(App.id == data.app_id) \ .one() account_data = db.session.query(Account) \ .join(TenantAccountJoin, Account.id == TenantAccountJoin.account_id) \ .filter(TenantAccountJoin.role == 'owner') \ .filter(TenantAccountJoin.tenant_id == app_data.tenant_id) \ .one_or_none() if not account_data: continue if data.user_input_form is None or data.user_input_form == 'null': data.user_input_form = json.dumps(user_input_form_template[account_data.interface_language]) else: raw_json_data = json.loads(data.user_input_form) raw_json_data.append(user_input_form_template[account_data.interface_language][0]) data.user_input_form = json.dumps(raw_json_data) # click.secho(f"Updated data {data.id}, pre_prompt: {data.pre_prompt}, user_input_form: {data.user_input_form}", fg='green') db.session.commit() except Exception as e: click.secho(f"Error while migrating data: {e}, app_id: {data.app_id}, app_model_config_id: {data.id}", fg='red') continue click.secho(f"Successfully migrated batch {i+1}/{num_batches}.", fg='green') pbar.update(len(data_batch)) def register_commands(app): app.cli.add_command(reset_password) app.cli.add_command(reset_email) app.cli.add_command(generate_invitation_codes) app.cli.add_command(reset_encrypt_key_pair) app.cli.add_command(recreate_all_dataset_indexes) app.cli.add_command(sync_anthropic_hosted_providers) app.cli.add_command(clean_unused_dataset_indexes) app.cli.add_command(create_qdrant_indexes) app.cli.add_command(update_qdrant_indexes) app.cli.add_command(update_app_model_configs)
[ "{{default_input}}", "{'en-US': [{'paragraph': {'label': 'Query', 'variable': 'default_input', 'required': False, 'default': ''}}], 'zh-Hans': [{'paragraph': {'label': '查询内容', 'variable': 'default_input', 'required': False, 'default': ''}}]}" ]
2024-01-10
a00012025/whisper-to-notion
write-to-notion.py
# import datetime import sys import os import re from notion_client import Client import openai filter_out_words = [ "[Start speaking]", "[END PLAYBACK]", "[AUDIO OUT]", "[VIDEO PLAYBACK]", "[BLACK_AUDIO]", "[音声なし]" ] end_characters = [ ".", "?", "!", "。" ] translate = False notion = Client(auth=os.environ["NOTION_TOKEN"]) page_id = "04646995367c426fb7942d9d37800392" page = notion.pages.retrieve(page_id) ansi_escape = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]') # pprint(page) translation_prompt = "請翻譯以下文字成為繁體中文,只輸出翻譯後的文字,不要輸出原文 \n ```\n %s \n``` #lang:zh-tw\n" last_line, last_translated_line, last_block_id = "", "", "" while True: try: line = sys.stdin.readline() line = ansi_escape.sub('', line) for word in filter_out_words: line = line.replace(word, "") line = line.strip().replace("\n", "") if not line: continue combine = False if translate and (len(last_line) > 0 and last_line[-1] not in end_characters and last_translated_line[-1] not in end_characters): # combine this line with last line combine = True if last_line.endswith("--"): last_line = last_line[:-2] line = last_line + " " + line # translate to Chinese if translate: response = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": "user", "content": translation_prompt % line}]) # type: ignore translated_content = response.choices[0].message.content else: translated_content = " " # if not combined, append to the end of the Notion page if not combine: result = notion.blocks.children.append(page_id, children=[ { "object": "block", "type": "paragraph", "paragraph": { "rich_text": [{"type": "text", "text": {"content": line+"\n"+translated_content if translate else line}}], }, } ]) last_block_id = result["results"][0]["id"] # type: ignore else: # if combined, replace the last block with the combined one result = notion.blocks.update(last_block_id, paragraph={ "rich_text": [{"type": "text", "text": {"content": line+"\n"+translated_content}}], }) last_line = line last_translated_line = translated_content # print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), end=" ") # print(f"Added new line to Notion page") except KeyboardInterrupt: print("Terminating...") break
[ "請翻譯以下文字成為繁體中文,只輸出翻譯後的文字,不要輸出原文 \n ```\n %s \n``` #lang:zh-tw\n", "PLACEHOLDER linec23d3a1f-f61a-4b55-8e17-dfa006e251d6\n ", "請翻譯以下文字成為繁體中文,只輸出翻譯後的文字,不要輸出原文 \n ```\n PLACEHOLDER linefb0dd77c-2819-46e1-9365-fbd730c10e9a \n``` #lang:zh-tw\n", "PLACEHOLDER line800b6d86-2af5-4410-b136-8d89ded223f3\n " ]
2024-01-10
ivaxi0s/llm-in-context-bias
src~inference~predict.py
"""Run predictions based on the model NOTE: This file is now depracated. Use src/inference/models.py instead """ from openai import OpenAI from dotenv import load_dotenv load_dotenv() # client = OpenAI() client = None def predict(model_name, prompt, model=None): if model_name == "gpt3.5": msgs = [{"role": "user", "content": prompt}] response = client.chat.completions.create( model="gpt-3.5-turbo", messages=msgs, temperature=0 ) return response.choices[0].message.content # Use mistral model else: return model.predict(prompt)
[]
2024-01-10
Quitiweb/whisper-ai-workshop
vclone~vclone_gpt.py
import os import openai from dotenv import load_dotenv from playsound import playsound from vclone_tools import text_to_vclone load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") eleven_api_key = os.getenv("ELEVEN_API_KEY") audio_path = "audio_vclone_v1.mp3" chatgpt_model = "gpt-3.5-turbo" chatgpt_system = "You are a helpful assistant on a conversation. Answer should be not too long. Be ironic and acid" client = openai.Client() def get_gpt_response(prompt): response = client.chat.completions.create( model=chatgpt_model, messages=[ {"role": "system", "content": chatgpt_system}, {"role": "user", "content": prompt} ] ) return response.choices[0].message.content def continuous_interaction(): while True: # clear_output(wait=True) prompt = input("Enter your prompt (or type 'exit' to stop): ") if prompt.lower() == 'exit': break response_text = get_gpt_response(prompt) text_to_vclone(response_text, audio_path) playsound(audio_path) # Example usage continuous_interaction()
[ "Enter your prompt (or type 'exit' to stop): ", "You are a helpful assistant on a conversation. Answer should be not too long. Be ironic and acid" ]
2024-01-10
sm745052/signLangML
blackboard.py
import openai import numpy as np openai.api_key = "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" class Blackboard: def __init__(self): self.words = [] def write(self): response = openai.Completion.create( engine="text-davinci-002", prompt="Correct this to standard English: {}".format(''.join(self.words)+'.'), temperature=0, max_tokens=60, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0 ) text = response["choices"][0]["text"] print(text) def add_word(self, word): if(self.words[-1]!=word): self.words.append(word) def clear(self): self.words = [] def backspace(self): self.words.pop()
[ "Correct this to standard English: {}" ]
2024-01-10
sm745052/signLangML
flask_app.py
from flask_socketio import SocketIO, send from flask import Flask, render_template, Response, request import cv2 import os import numpy as np from coordinates import handDetector import time import tensorflow as tf import numpy as np import pickle import keyboard import statistics import pyttsx3 import openai from play import play #environment variables os.environ['backspace']="0" os.environ['enter']="0" #initialize pyttsx3 engine for sound output engine = pyttsx3.init() #initialize the play class play = play() #initialize for sentence and openai my_sentence = '' prediction_stack = ['-' for i in range(5)] blackboard = [] l = 10 openai.api_key = "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" os.environ['lastword'] = '' #load the stored labels with open("labels.pkl", "rb") as f: labels = pickle.load(f) #load the stored model model = tf.keras.models.load_model("./my_model") #function for making sentence using openai def make_sentence(): global blackboard response = openai.Completion.create( engine="text-davinci-002", prompt="Correct this to standard English: {}".format(' '.join(list(filter(('-').__ne__, blackboard)))+'.'), temperature=0, max_tokens=60, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0 ) sentence = response["choices"][0]["text"] print(sentence) return sentence #function for displaying the sentence def display(): global prediction_stack global blackboard global l global my_sentence try: if(len(prediction_stack) > l): word = statistics.mode(prediction_stack[-l:]) if(len(blackboard) == 0 or word!=blackboard[-1]): blackboard.append(word) my_sentence = ' '.join(list(filter(('-').__ne__, blackboard))) os.environ['lastword'] = word socketio.send(my_sentence) except: pass print(blackboard) def predict_show(img): global prediction_stack, l, labels, model, blackboard if os.getenv('enter')=="1": socketio.send("$"+make_sentence()) os.environ["enter"]="0" if os.getenv('backspace')=="1": os.environ['backspace']="0" if(len(blackboard)==0):return w = blackboard.pop() prediction_stack = ['-' for i in range(5)] my_sentence = ' '.join(list(filter(('-').__ne__, blackboard))) socketio.send(my_sentence) pTime = 0 img = detector.findcoords(img) lmlist = detector.findPosition(img) if(lmlist[0] != 0): #deal with only when hands detected y = model.predict(np.array(lmlist).reshape(1, -1)) result = labels[np.argmax(y.flatten())] prediction_stack.append(result) display() cTime = time.time() fps = 1 / (cTime - pTime) pTime = cTime return img global capture,rec_frame, switch, detect, rec, out switch=0 detect=0 #make shots directory to save pics try: os.mkdir('./shots') except OSError as error: pass #Load pretrained face detection model #instatiate flask app app = Flask(__name__, template_folder='./templates') app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app, cors_allowed_origins='*') camera = cv2.VideoCapture(0) def record(out): global rec_frame while(rec): time.sleep(0.05) out.write(rec_frame) def detect_landmarks(frame): global detector frame = predict_show(frame) return frame def gen_frames(): # generate frame by frame from camera global out, capture,rec_frame while True: success, frame = camera.read() if success: if(detect): frame= detect_landmarks(frame) try: ret, buffer = cv2.imencode('.jpg', cv2.flip(frame,1)) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') except Exception as e: pass else: pass @app.route('/') def index(): return render_template('index.html') @app.route('/video_feed') def video_feed(): return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') @app.route('/video_feed_1') def video_feed_1(): return Response(play.recognize(), mimetype='multipart/x-mixed-replace; boundary=frame') # @app.route("/audio_feed") # def audio_feed(): # """Audio streaming route. Put this in the src attribute of an audio tag.""" # return Response(generateAudio(), mimetype="audio/x-wav") @socketio.on('message') def handleMessage(msg): print('Message: ' + msg) if(msg=="Backspace"): os.environ['backspace']="1" if(msg=="Enter"): os.environ['enter']="1" @app.route('/requests',methods=['POST','GET']) def tasks(): global switch,camera if request.method == 'POST': if request.form.get('detect') == 'Detect': global detect detect=not detect elif request.form.get('stop') == 'Stop/Start': if(switch==1): switch=0 camera.release() cv2.destroyAllWindows() else: camera = cv2.VideoCapture(0) switch=1 elif request.method=='GET': return render_template('index.html') return render_template('index.html') if __name__ == '__main__': detector = handDetector() app.run() camera.release() cv2.destroyAllWindows()
[ "Correct this to standard English: PLACEHOLDER." ]
2024-01-10
aidanaalund/predticker
webapp.py
# imports from bs4 import BeautifulSoup from heapq import nlargest import re import nltk import newsapi from newspaper import Article import json import openai from transformers import pipeline from langchain.chains import RetrievalQA from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.llms import OpenAI from langchain.document_loaders import PDFPlumberLoader from langchain.schema import ( AIMessage, HumanMessage, SystemMessage ) import uuid import pathlib import io import pdfplumber import datetime import datetime from collections import deque from datetime import date import numpy as np from plotly import graph_objs as go import yfinance as yf import requests import pandas_ta as ta import pandas as pd from streamlit_extras.badges import badge as badge import streamlit as st import sys __import__('pysqlite3') sys.modules['sqlite3'] = sys.modules.pop('pysqlite3') START = "2016-01-01" TODAY = date.today().strftime("%Y-%m-%d") YEAR = int(date.today().strftime("%Y")) STARTOFYEAR = "f'{YEAR}-01-01'" st.set_page_config(page_title='ESGParrot', page_icon=':parrot:', layout="centered", initial_sidebar_state="auto", menu_items={ 'Get Help': 'https://github.com/aidanaalund/predticker', 'Report a bug': "https://github.com/aidanaalund/predticker", 'About': "A stock dashboard with a focus on ESG ratings and analysis using NLP/LLM tools. Powered by NewsAPI, Yahoo! Finance, and OpenAI." }) # Initialize session state keys if 'stocks' not in st.session_state: st.session_state.stocks = set(["AAPL", "CAT", "TSLA", "MSFT"]) if 'predictiontext' not in st.session_state: st.session_state.predictiontext = '' if 'currentlayoutbutton' not in st.session_state: st.session_state.currentlayoutbutton = None if 'newsdictionary' not in st.session_state: st.session_state.newsdictionary = {} if 'esgdictionary' not in st.session_state: st.session_state.esgdictionary = {} if 'bbandcheck' not in st.session_state: st.session_state.bbandcheck = False if 'volumecheck' not in st.session_state: st.session_state.volumecheck = False if 'modelhistory' not in st.session_state: st.session_state.modelhistory = None if 'currentstockmetadata' not in st.session_state: st.session_state.currentstockmetadata = None if 'newgraph' not in st.session_state: st.session_state.newgraph = True if 'currentdataframe' not in st.session_state: # make this set to what the selector is currently set to st.session_state.currentdataframe = None if 'fileuploader' not in st.session_state: st.session_state.fileuploader = None if 'pdftext' not in st.session_state: st.session_state.pdftext = None if 'conversation' not in st.session_state: st.session_state.conversation = {} if 'apicounter' not in st.session_state: st.session_state.apicounter = 0 # User Input col1, col2, col3 = st.columns([4, 3, 3]) with col1: st.title(":parrot: ESGParrot") st.markdown( "[Prices](#esgparrot) | [Metrics](#esg-statistics) | [ChatESG](#chatesg) | [News](#recent-news)") # Adds an inputted stock string to a list of stocks in the state # Checks for an invalid ticker by attempting to get the first value in a column def addstock(): if st.session_state.textinput: try: temp = yf.download(st.session_state.textinput, START, TODAY) test = temp['Close'].iloc[0] st.session_state.textinput = st.session_state.textinput.upper() st.session_state.stocks.add(st.session_state.textinput) st.session_state.selectbox = st.session_state.textinput except IndexError: st.error( body=f'Error: "{st.session_state.textinput}" is an invalid ticker.', icon='🚩') st.session_state.textinput = '' # Sets a streamlit state boolean to true, making the graph render a new stock's data set. def newgraph(): st.session_state.newgraph = True with col2: st.text('') selected_stock = st.selectbox("Select a ticker from your list:", st.session_state.stocks, key='selectbox', on_change=newgraph) with col3: st.text('') newstock = st.text_input(label='Add a ticker to the list...', placeholder="Type a ticker to add", max_chars=4, on_change=addstock, key='textinput', help='Please input a valid US ticker.') # Load correctly formatted data in a pandas dataframe. def add_indicators(df): df['SMA'] = ta.sma(df.Close, length=25) df['EMA12'] = ta.ema(df.Close, length=10) df['EMA26'] = ta.ema(df.Close, length=30) df['RSI'] = ta.rsi(df.Close, length=14) # returns multiple values # df['ADX'], df['DMP'], df['DMN'] = ta.adx( # df.High, df.Low, df.Close, length=14) df['WILLR'] = ta.willr(df.High, df.Low, df.Close, length=25) # MACD stuff (without TALib!) macd = df['EMA26']-df['EMA12'] macds = macd.ewm(span=9, adjust=False, min_periods=9).mean() macdh = macd - macds df['MACD'] = df.index.map(macd) df['MACDH'] = df.index.map(macdh) df['MACDS'] = df.index.map(macds) # Bollinger Bands df.ta.bbands(length=20, append=True) ta.bbands(df['Adj Close'], timeperiod=20) # Log return is defined as ln(d2/d1) # Starts at day 1 df.ta.log_return(close=df['Adj Close'], cumulative=True, append=True) # Day over day log return df.ta.log_return(close=df['Adj Close'], cumulative=False, append=True) df['Target'] = np.where(df['LOGRET_1'] > 0, 1, 0) # df['return_'+benchmark] = 1 # Percent return (now/day1) df.ta.percent_return(close=df['Adj Close'], cumulative=True, append=True) # Create signals df['EMA_12_EMA_26'] = np.where(df['EMA12'] > df['EMA26'], 1, -1) df['Close_EMA_12'] = np.where(df['Close'] > df['EMA12'], 1, -1) df['MACDS_MACD'] = np.where(df['MACDS'] > df['MACD'], 1, -1) # Consider refactoring to use RapidAPI @st.cache_data(show_spinner=False) def load_data(ticker): data = yf.download(ticker, START, TODAY) data.reset_index(inplace=True) data['Date'] = pd.to_datetime(data['Date']) add_indicators(data) return data data = load_data(selected_stock) st.session_state.currentdataframe = data # DATA PREPROCESSING # grab first and last observations from df.date and make a continuous date range from that dt_all = pd.date_range( start=data['Date'].iloc[0], end=data['Date'].iloc[-1], freq='D') # check which dates from your source that also accur in the continuous date range dt_obs = [d.strftime("%Y-%m-%d") for d in data['Date']] # isolate missing timestamps dt_breaks = [d for d in dt_all.strftime( "%Y-%m-%d").tolist() if not d in dt_obs] # Define the plot types and the default layouts candlestick = go.Candlestick(x=data['Date'], open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], increasing_line_color='#2ca02c', decreasing_line_color='#ff4b4b', hoverinfo=None, name='Candlestick',) volume = go.Scatter(x=data['Date'], y=data['Volume']) bbu = go.Scatter(name='Upper Band', x=data['Date'], y=data['BBU_20_2.0'], marker_color='rgba(30, 149, 242, 0.8)', opacity=.1,) bbm = go.Scatter(name='Middle Band', x=data['Date'], y=data['BBM_20_2.0'], marker_color='rgba(255, 213, 0, 0.8)', opacity=.8,) bbl = go.Scatter(name='Lower Band', x=data['Date'], y=data['BBL_20_2.0'], marker_color='rgba(30, 149, 242, 0.8)', opacity=.1, fill='tonexty', fillcolor='rgba(0, 187, 255, 0.15)') stocklayout = dict( yaxis=dict(fixedrange=False, ), xaxis_rangeslider_visible=False, xaxis=dict( fixedrange=False, rangebreaks=[ dict(bounds=["sat", "mon"]), # hide weekends dict(values=dt_breaks) ], ), ) # Computes a scaled view for both plots based on the view mode and data # Returned as an x range and two y ranges for each plot type (candle and volume) header, subinfo = st.columns([2, 4]) change = data['Close'].iloc[-1] - data['Close'].iloc[-2] with header: price = data['Close'].iloc[-1] percentage = (float(data['Close'].iloc[-1] - data['Close'].iloc[-2])/abs(data['Close'].iloc[-2]))*100.00 st.metric(label=selected_stock, value='${:0.2f}'.format(price), delta='{:0.2f}'.format(change) + ' ({:0.2f}'.format(percentage)+'%) today' ) recentclose = data['Date'].iloc[-1].strftime('%Y-%m-%d') st.caption(f'as of {recentclose}') st.markdown(""" <style> [data-testid=column]:nth-of-type(1) [data-testid=stVerticalBlock]{ gap: 0rem; } </style> """, unsafe_allow_html=True) # TODO: This method returns slightly incorrect ranges for YTD @st.cache_data def defaultRanges(df, period): match period: case '1W': bf = 7 case '1M': bf = 30 case '6M': bf = 180 case '1Y': bf = 365 case '5Y': bf = 365*5 case 'YTD': # TODO: the issue is that the first day of the year is new year's firstday = datetime.datetime(YEAR, 1, 1) # Try to get the first entry of the year # Then grab the data for closing and open # df.loc[start:end, 'Close'] x = [firstday, df['Date'].iloc[-1]] ymax = df['High'].max() ymin = df['Low'].min() cbuffer = (ymax-ymin)*0.30 ycandle = [ymin-cbuffer, ymax+cbuffer] yvolume = [df['Volume'].min(), df['Volume'].max()] return x, ycandle, yvolume case 'Max': x = [df['Date'].iloc[0], df['Date'].iloc[-1]] ymax = df['High'].max() ymin = df['Low'].min() cbuffer = (ymax-ymin)*0.30 ycandle = [ymin-cbuffer, ymax+cbuffer] yvolume = [df['Volume'].min(), df['Volume'].max()] return x, ycandle, yvolume case _: bf = 30 lower = df['Date'].iloc[-1]-np.timedelta64(bf, 'D') upper = df['Date'].iloc[-1]+np.timedelta64(1, 'D') x = [lower, upper] cbuffer = (df['High'].iloc[-bf: -1].max() - df['Low'].iloc[-bf:-1].min())*0.30 ycandle = [df['Low'].iloc[-bf:-1].min()-cbuffer, df['High'].iloc[-bf:-1].max()+cbuffer] vbuffer = (df['Volume'].iloc[-bf:-1].max() - df['Volume'].iloc[-bf:-1].min())*0.30 yvolume = [df['Volume'].iloc[-bf:-1].min()-vbuffer, df['Volume'].iloc[-bf:-1].max()+vbuffer] return x, ycandle, yvolume # Calls defaultRanges to obtain proper scales and scales the plots passed in # Assumes plot1 is a candlestick and plot2 is a volume/scatter plot def scalePlots(df, period, plotly1, plotly2): xr, cr, vr = defaultRanges(df, period) plotly1.update_layout(xaxis_range=xr, yaxis_range=cr,) plotly2.update_layout(xaxis_range=xr, yaxis_range=vr,) # Initialize candlestick and volume plots fig = go.Figure(data=candlestick, layout=stocklayout) fig2 = go.Figure(data=volume, layout=stocklayout) if st.session_state.bbandcheck: fig.add_trace(bbm) fig.add_trace(bbu) fig.add_trace(bbl) fig.update_layout(showlegend=False, yaxis={'side': 'right'}, title='', dragmode='pan', yaxis_title='Share Price ($)', modebar_remove=["autoScale2d", "autoscale", "lasso", "lasso2d", "resetview", "select2d",], autosize=False, width=700, height=350, margin=dict( l=0, r=0, b=0, t=0, pad=0 ),) # st.plotly_chart(fig, use_container_width=True) fig2 = go.Figure(data=volume, layout=stocklayout) fig2.update_layout(yaxis_title='Number of Shares', autosize=False, yaxis={'side': 'right'}, dragmode='pan', modebar_remove=["autoScale2d", "autoscale", "lasso", "lasso2d", "resetview", "select2d",], width=700, height=400, margin=dict( l=0, r=10, b=0, t=0, pad=4 ),) rangebutton = st.radio( label='Range Selector', options=('1W', '1M', '6M', 'YTD', '1Y', '5Y', 'Max'), horizontal=True, index=1, label_visibility='collapsed') if st.session_state.newgraph: string = rangebutton scalePlots(st.session_state.currentdataframe, string, fig, fig2) st.session_state.newgraph = False if rangebutton == '1W': scalePlots(st.session_state.currentdataframe, '1W', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == '1M': scalePlots(st.session_state.currentdataframe, '1M', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == '6M': scalePlots(st.session_state.currentdataframe, '6M', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == 'YTD': scalePlots(st.session_state.currentdataframe, 'YTD', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == '1Y': scalePlots(st.session_state.currentdataframe, '1Y', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == '5Y': scalePlots(st.session_state.currentdataframe, '5Y', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) elif rangebutton == 'Max': scalePlots(st.session_state.currentdataframe, 'Max', fig, fig2) st.plotly_chart(fig, use_container_width=True) if st.session_state.volumecheck: st.plotly_chart(fig2, use_container_width=True) @st.cache_data(show_spinner=False) def fetchInfo(ticker): ticker = yf.Ticker(ticker) info = ticker.get_info() return info info = fetchInfo(selected_stock) if info: if 'longName' in info: name = info['longName'] else: name = selected_stock with st.expander(f"{name}'s summary"): if 'longBusinessSummary' in info: st.caption(info['longBusinessSummary']) else: st.error(f"{name}'s summary not available") @st.cache_data(show_spinner=False) def fetchESG(ticker): url = f"https://yahoo-finance127.p.rapidapi.com/esg-score/{ticker}" headers = { "RapidAPI-Key": st.secrets["yahoofinancekey"], "RapidAPI-Host": "yahoo-finance127.p.rapidapi.com" } response = requests.get(url, headers=headers) return response.json() st.subheader("ESG Statistics:") json = fetchESG(selected_stock) if 'message' not in json: delta = 0 url = "https://www.sustainalytics.com/corporate-solutions/esg-solutions/esg-risk-ratings" o, e, s, g = st.columns([2, 1, 1, 1]) with o: value = json['totalEsg']['fmt'] st.metric(label='Overall ESG Risk', value=value, delta=None, help=f'{json["percentile"]["fmt"]}th percentile in the {json["peerGroup"]} peer group as of {json["ratingMonth"]}/{json["ratingYear"]}.') tier = float(value) tierstring = 'bug!' if tier > 0 and tier < 10: tierstring = 'Negligible' st.markdown( f'<h5 style="color:#d9d9d9;font-size:14px;">{"Negligible"}</h5>', unsafe_allow_html=True) elif tier >= 10 and tier < 20: tierstring = 'Low' st.markdown( f'<h5 style="color:#f4e7cc;font-size:14px;">{"Low"}</h5>', unsafe_allow_html=True) elif tier >= 20 and tier < 30: tierstring = 'Medium' st.markdown( f'<h5 style="color:#ffdca7;font-size:14px;">{"Medium"}</h5>', unsafe_allow_html=True) elif tier >= 30 and tier < 40: tierstring = 'High' st.markdown( f'<h5 style="color:#ffc46d;font-size:14px;">{"High"}</h5>', unsafe_allow_html=True) elif tier > 40: tierstring = 'Severe' st.markdown( f'<h5 style="color:#f89500;font-size:14px;">{"Severe"}</h5>', unsafe_allow_html=True) with e: st.metric(label='Environment Risk', value=json['environmentScore']['fmt'], delta=None) with s: st.metric(label='Social Risk', value=json['socialScore']['fmt'], delta=None) with g: st.metric(label='Governance Risk', value=json['governanceScore']['fmt'], delta=None) graphic, info = st.columns([3, 2]) with graphic: st.image( 'https://raw.githubusercontent.com/aidanaalund/predticker/main/resources/sustainalytics_system.png') with info: st.caption( 'Overall risk is calculated by adding each individual risk score. Higher ESG scores are generally related to higher valuation and less volatility. [Learn more](%s)' % url) else: st.error(f'Sustainability data is currently not available for {name}') # @st.cache_resource # def esgBert(): # return pipeline("text-classification", model="nbroad/ESG-BERT") # def analysis(): # pipe = esgBert() # st.session_state.esgdictionary[f'{selected_stock}'] = pipe( # st.session_state.report_input) # BERT TOPIC MODELING # st.text_area('Topic model a sustainability report/blurb:', # help='Using ESGBert, top ESG areas in the text are identified. Unexpected behavior will occur if text other than sustainability reports are inputted.', # placeholder='Put report text here...', # key='report_input', # on_change=analysis, # ) # if f'{selected_stock}' in st.session_state.esgdictionary: # response = st.session_state.esgdictionary[f'{selected_stock}'] # topic = response[0]['label'].replace( # '_', ' ') # st.caption('Strongest Topic: '+topic) @st.cache_data(show_spinner=False) def findCsrLinks(company_name): # Construct the search query using the company name and keywords search_query = f"{company_name}" # Perform a web search using a search engine like Google search_url = f"https://www.google.com/search?q={search_query}" response = requests.get(search_url) if response.status_code == 200: # Parse the HTML content of the search results page soup = BeautifulSoup(response.content, 'html.parser') # Find all the search result links search_results = soup.find_all('a') # Iterate through the search results to find relevant links csr_links = [] for link in search_results: href = link.get('href') if href and ("sustainability" in href or 'impact' in href) and 'report' in href: sep = '&' stripped = href.split(sep, 1)[0] csr_links.append(stripped) if csr_links: return csr_links[0] else: return None else: print("Failed to fetch search results.") company_name = f"{name} CSR Report" csr_links = findCsrLinks(company_name) if csr_links: st.subheader('Found Impact Reporting :page_facing_up::') st.caption(csr_links[7:]) # CHATBOT SECTION # Left in for future tuning of .pdf reading # @st.cache_data # def parse(): # if file is not None: # data = [] # tables = [] # with pdfplumber.open(file) as pdf: # pages = pdf.pages # # st.session_state.pdftext = len(pdf.pages) # # for p in pages: # # data.append(p.extract_text()) # # tables are returned as a list # # test for tesla 2021 report # # table = pages[67].extract_table() # st.session_state.pdftext = len(pages) # TODO: determine how to cache embeddings and use hypothetical document embedding (HyDE). # @st.cache_data(show_spinner=False) def generateResponse(uploaded_file, openai_api_key, context, query_text, ticker): # Load document if file is uploaded if uploaded_file and openai_api_key and query_text != '': # create virtual file path for Langchain pdf reader interface filepath = pathlib.Path(str(uuid.uuid4())) filepath.write_bytes(uploaded_file.getvalue()) fullquery = context+f'\n{query_text}' loader = PDFPlumberLoader(str(filepath)) # Split documents into chunks documents = loader.load_and_split() # Select embeddings try: embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) # Create a vectorstore from documents db = Chroma.from_documents(documents, embeddings) # Create retriever interface retriever = db.as_retriever() # Create QA chain qa = RetrievalQA.from_chain_type(llm=OpenAI( openai_api_key=openai_api_key), chain_type='stuff', retriever=retriever) # get rid of temporary file path made if filepath.is_file(): filepath.unlink() response = qa.run(fullquery) except: return None if str(ticker) in st.session_state.conversation: st.session_state.conversation[ticker].append(query_text) st.session_state.conversation[ticker].append(response) return response else: st.session_state.conversation[ticker] = [] st.session_state.conversation[ticker].append(query_text) st.session_state.conversation[ticker].append(response) return response else: try: openai.api_key = openai_api_key qacontext = """Answer the question truthfully based on the text below. If the question does not seem to be related to sustainability or ESG (Environmental, Social, and Governance) topics, do not answer the question and state that you are not supposed to answer off topic questions.""" fullquery = qacontext+f'\n{query_text}' response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": f"{fullquery}"}, ] ) if str(ticker) in st.session_state.conversation: st.session_state.conversation[ticker].append(query_text) st.session_state.conversation[ticker].append( response['choices'][0]['message']['content']) return response['choices'][0]['message']['content'] else: st.session_state.conversation[ticker] = [] st.session_state.conversation[ticker].append(query_text) st.session_state.conversation[ticker].append( response['choices'][0]['message']['content']) return response['choices'][0]['message']['content'] except: return None key = st.secrets['openapikey'] # the submit button will update all of the values inside the form all at once! context = """Answer the question truthfully based on the text below. First answer the question, then include a verbatim quote with quote marks supporting your answer and a comment where to find it in the text (page number). After the quote write a summary that explains your answer. Use bullet points.""" # one, two = st.columns([2, 2]) # with one: st.subheader( 'ChatESG :speaking_head_in_silhouette:: an LLM that understands ESG documents') esgfile = st.file_uploader(label='Upload CSR report:', type=['pdf'], help='PDF only.') # st.info('Due to free OpenAI API access, ChatESG is designed to only handle 4 requests with a free API key.') key = st.secrets['openapikey'] with st.chat_message(name="ChatESG", avatar='assistant'): st.write( 'Hello! To make me work the best, look at my guide!') with st.expander('Usage Guide + Types of questions to ask me!'): st.markdown(""" - Guide: - ChatESG has two modes, document analysis and general Q/A. The first mode runs when a document is uploaded, and the second mode runs if no document is submitted when a query is made. - If you query ChatESG with a document, ChatESG will try to find an answer and cite where it found the material. - If ChatESG is in Q/A mode, it will attempt to answer your question. - ChatESG does not currently remember previous inputs. Please account for this when interacting with it! - ChatESG may be incorrect on some issues or say it does not know an answer. As a language model, it can often have a flawed understanding of topics. Always verify what it cites and potential biases of the company who wrote the document. - Try to use specific keywords and language. - Question Ideas: - How does [company] plan to reduce their carbon emissions? - What is ESG? - How does [company] utilize diversity, equity, and inclusion in their business? - What is an area [company] can improve on in hitting emissions targets? """) if selected_stock in st.session_state.conversation: user = True for message in st.session_state.conversation[selected_stock]: if user: with st.chat_message(name='User', avatar='user'): st.write(message) user = False else: with st.chat_message(name="ChatESG", avatar='assistant'): st.write(message) user = True input = st.chat_input( placeholder="Ask a question about ESG or an impact report") if input: if not st.session_state.apicounter >= 4: with st.chat_message(name='User', avatar='user'): st.write(input) with st.chat_message(name="ChatESG", avatar="assistant"): with st.spinner(text='Generating Response'): if st.session_state.userkey == '': print('normal!') msg = generateResponse(esgfile, key, context, input, selected_stock) else: msg = generateResponse(esgfile, st.session_state.userkey, context, input, selected_stock) if msg is not None: st.write(msg) else: st.error( 'Invalid OpenAI API Key. Please enter a valid key or use the community key.') else: st.toast('Reached the maximum queries allowed for one user. Sorry!') st.divider() # News Section: st.subheader('Recent News:') @st.cache_data(show_spinner=False) def fetchNews(name, endpoint): try: # TODO: make query parameter only get articles 'about' a company query_params = { 'q': f'{name}', "sortBy": "relevancy", "apiKey": st.secrets["newsapikey"], "page": 1, "pageSize": 3, "language": "en", # can't mix sources with category # "sources": "reuters,cbs-news,the-washington-post,the-wall-street-journal,financial-times", # args for 'everything' endpoint only # "excludeDomains": "readwrite.com", # "domains": 'cnbc.com/business,usatoday.com/money/,cnn.com/business,gizmodo.com/tech,apnews.com/business,forbes.com/business/,bloomberg.com,newsweek.com/business,finance.yahoo.com/news/,', # args for top-headlines endpoint only # "category": "business" } if endpoint == "https://newsapi.org/v2/top-headlines": query_params["category"] = "business" else: query_params["excludeDomains"] = "readwrite.com" main_url = endpoint # fetching data in json format res = requests.get(main_url, params=query_params) response = res.json() # getting all articles return response except: st.error("News search failed.") if f'{selected_stock}' not in st.session_state.newsdictionary: st.session_state.newsdictionary[f'{selected_stock}'] = fetchNews( name, "https://newsapi.org/v2/top-headlines") if st.session_state.newsdictionary[f'{selected_stock}']['totalResults'] == 0: st.session_state.newsdictionary[f'{selected_stock}'] = fetchNews( name, "https://newsapi.org/v2/everything") @st.cache_resource def sentimentModel(): return pipeline("sentiment-analysis") # create dropdowns for each article if st.session_state.newsdictionary[f'{selected_stock}']['totalResults'] > 0: for ar in st.session_state.newsdictionary[f'{selected_stock}']['articles']: try: with st.expander(ar['title']): url = ar["url"] # fullarticle = Article(url) # fullarticle.download() # fullarticle.parse() # fullarticle.nlp() # data_df = fullarticle.keywords # print(type(data_df)) # list stripped = ar['publishedAt'].split("T", 1)[0] st.caption(f"{ar['description']}") # st.caption(f"{fullarticle.text}") sentbutton = st.button(label='Perform sentiment analysis...', key=url) if sentbutton: sentiment_pipeline = sentimentModel() # TODO: improve pipeline, and get full article contents sent = sentiment_pipeline(ar['title']) st.text(sent[0]['label']) st.caption(f'[Read at {ar["source"]["name"]}](%s)' % url) if ar["author"]: st.caption(f'Written by {ar["author"]}') st.caption(f'{stripped}') except: st.error("Failed to grab article.") else: st.error('No articles found.') # Extras + Debug Menu st.divider() with st.expander('Extras & Settings Menu:'): st.caption(f"{name}'s dataframe:") st.dataframe(data=st.session_state.currentdataframe, use_container_width=True) st.caption('ChatESG Settings:') userkey = st.text_input('Use your own OpenAI API Key:', type='password', help="Please refer to OpenAI's website for pricing info.", key='userkey') st.caption('Graph display settings:') bbandcheck = st.checkbox(label="Display Bollinger bands", key='bbandcheck') volumecheck = st.checkbox(label="Display volume plot", key='volumecheck')
[ "PLACEHOLDER" ]
2024-01-10
giginet/swift-evolution-gpt
loader.py
from functools import reduce from pathlib import Path from typing import List from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, ServiceContext, StorageContext, \ load_index_from_storage, LLMPredictor, OpenAIEmbedding, download_loader, Document from llama_index.indices.base import IndexType from llama_index.llms import OpenAI import chromadb from llama_index import VectorStoreIndex from llama_index.vector_stores import ChromaVectorStore import os import logging from llama_index.readers.file.markdown_reader import MarkdownReader class ProposalsLoader: @property def cache_path(self) -> str: return os.path.join(os.getcwd(), ".caches") def __init__(self, directory_path: str): self.directory_path = directory_path self.llm = OpenAI(model='gpt-4-1106-preview') def load(self) -> IndexType: embed_model = OpenAIEmbedding(model="text-embedding-ada-002") predictor = LLMPredictor(llm=self.llm) service_context = ServiceContext.from_defaults( embed_model=embed_model, llm_predictor=predictor ) # documents = SimpleDirectoryReader(self.directory_path).load_data() markdown_reader = MarkdownReader() proposals = [os.path.join(self.directory_path, markdown) for markdown in os.listdir(self.directory_path) if markdown.endswith(".md")] def extend_markdowns(list: List[Document], filepath: str) -> List[Document]: docs = markdown_reader.load_data(file=Path(filepath)) list.extend(docs) return list documents: List[Document] = reduce( extend_markdowns, proposals, [] ) db = chromadb.PersistentClient(path="./chroma_db") chroma_collection = db.get_or_create_collection("swift-evolution-gpt") vector_store = ChromaVectorStore(chroma_collection=chroma_collection) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context, storage_context=storage_context) return index
[]
2024-01-10
joaovitorroriz/VoztextocomGPT
main_old.py
import pyaudio import wave import threading import os import openai from gtts import gTTS from playsound import playsound # Initialize the API key def gravar_audio(nome_arquivo, taxa_amostragem=44100, n_canais=1, largura_amostra=pyaudio.paInt16): """ Grava áudio do microfone e salva em um arquivo WAV. A gravação continua até que o usuário pressione 'Enter'. :param nome_arquivo: Nome do arquivo WAV a ser criado. :param taxa_amostragem: Taxa de amostragem do áudio (em Hz). :param n_canais: Número de canais (1 para mono, 2 para estéreo). :param largura_amostra: Formato da amostra de áudio (pyaudio.paInt16 é comum). """ p = pyaudio.PyAudio() # Cria uma interface PyAudio # Abrindo stream para gravação stream = p.open(format=largura_amostra, channels=n_canais, rate=taxa_amostragem, input=True, frames_per_buffer=1024) frames = [] # Lista para armazenar os frames capturados def gravar(): print("Iniciando gravação. Pressione 'Enter' para parar...") while not input_event.is_set(): data = stream.read(1024) frames.append(data) input_event = threading.Event() recording_thread = threading.Thread(target=gravar) recording_thread.start() input("Pressione 'Enter' para parar a gravação...\n") input_event.set() recording_thread.join() # Parando e fechando o stream stream.stop_stream() stream.close() p.terminate() # Salvando os dados em um arquivo WAV with wave.open(nome_arquivo, 'wb') as wf: wf.setnchannels(n_canais) wf.setsampwidth(p.get_sample_size(largura_amostra)) wf.setframerate(taxa_amostragem) wf.writeframes(b''.join(frames)) print("Gravação finalizada.") # Exemplo de uso nome_arquivo = 'audio_gravado.wav' idioma = 'pt' def gerar_resposta(messages): #response = openai.ChatCompletion.create( ## Api antiga response = openai.chat.completions.create( ## API nova model="gpt-3.5-turbo", messages=messages, temperature=0.5 ) return [response.choices[0].message.content, response.usage] mensagens = [{"role": "system", "content": "Você é um assistente gente boa, seu nome é SARA"}] import speech_recognition as sr def converter_audio_em_texto(nome_arquivo): # Inicializa o reconhecedor de fala r = sr.Recognizer() # Carrega o arquivo de áudio with sr.AudioFile(nome_arquivo) as source: audio_data = r.record(source) # Tenta reconhecer o fala usando o Google Web Speech API try: texto = r.recognize_google(audio_data, language='pt-BR') print("Texto transcrito: " + texto) return texto except sr.UnknownValueError: print("Google Speech Recognition não conseguiu entender o áudio.") except sr.RequestError as e: print(f"Não foi possível solicitar resultados do serviço Google Speech Recognition; {e}") while True: # Ask a question question = input("Perguntar para Sara (\"sair\"): ") if question == "sair" : print("saindo") break elif question == "": gravar_audio(nome_arquivo) texto_transcrito = converter_audio_em_texto(nome_arquivo) mensagens.append({"role": "user", "content": str(texto_transcrito)}) answer = gerar_resposta(mensagens) print("user:", question) print("Sara:", answer[0], "\nCusto:\n", answer[1]) mensagens.append({"role": "assistant", "content": answer[0]}) tts = gTTS(text=answer[0], lang=idioma, slow=False) # Salvando o arquivo de áudio arquivo_audio = "audio.mp3" tts.save(arquivo_audio) # Reproduzindo o arquivo de áudio playsound(arquivo_audio) # Opcional: remover o arquivo de áudio após a reprodução os.remove(arquivo_audio) else: mensagens.append({"role": "user", "content": str(question)}) answer = gerar_resposta(mensagens) print("user:", question) print("Sara:", answer[0], "\nCusto:\n", answer[1]) mensagens.append({"role": "assistant", "content": answer[0]}) tts = gTTS(text=answer[0], lang=idioma, slow=False) # Salvando o arquivo de áudio arquivo_audio = "audio.mp3" tts.save(arquivo_audio) # Reproduzindo o arquivo de áudio # playsound(arquivo_audio) from pathlib import Path from openai import OpenAI client = OpenAI() speech_file_path = Path(__file__).parent / "audio.mp3" response = client.audio.speech.create( model="tts-1", voice="alloy", input="Today is a wonderful day to build something people love!" ) response.stream_to_file(speech_file_path) # Opcional: remover o arquivo de áudio após a reprodução os.remove(arquivo_audio) debugar = False if debugar: print("Mensagens", mensagens, type(mensagens))
[ "Você é um assistente gente boa, seu nome é SARA" ]
2024-01-10
joaovitorroriz/VoztextocomGPT
main_old2.py
import pyaudio import wave import threading import os import openai from gtts import gTTS from playsound import playsound from openai import OpenAI import speech_recognition as sr client = OpenAI() # Initialize the API key def gravar_audio(nome_arquivo, taxa_amostragem=44100, n_canais=1, largura_amostra=pyaudio.paInt16): """ Grava áudio do microfone e salva em um arquivo WAV. A gravação continua até que o usuário pressione 'Enter'. :param nome_arquivo: Nome do arquivo WAV a ser criado. :param taxa_amostragem: Taxa de amostragem do áudio (em Hz). :param n_canais: Número de canais (1 para mono, 2 para estéreo). :param largura_amostra: Formato da amostra de áudio (pyaudio.paInt16 é comum). """ p = pyaudio.PyAudio() # Cria uma interface PyAudio # Abrindo stream para gravação stream = p.open(format=largura_amostra, channels=n_canais, rate=taxa_amostragem, input=True, frames_per_buffer=1024) frames = [] # Lista para armazenar os frames capturados def gravar(): print("Iniciando gravação. Pressione 'Enter' para parar...") while not input_event.is_set(): data = stream.read(1024) frames.append(data) input_event = threading.Event() recording_thread = threading.Thread(target=gravar) recording_thread.start() input("Pressione 'Enter' para parar a gravação...\n") input_event.set() recording_thread.join() # Parando e fechando o stream stream.stop_stream() stream.close() p.terminate() # Salvando os dados em um arquivo WAV with wave.open(nome_arquivo, 'wb') as wf: wf.setnchannels(n_canais) wf.setsampwidth(p.get_sample_size(largura_amostra)) wf.setframerate(taxa_amostragem) wf.writeframes(b''.join(frames)) print("Gravação finalizada.") # Exemplo de uso nome_arquivo = 'audio_gravado.wav' idioma = 'pt' def gerar_resposta(messages): #response = openai.ChatCompletion.create( ## Api antiga response = openai.chat.completions.create( ## API nova model="gpt-3.5-turbo", messages=messages, temperature=0.5 ) return [response.choices[0].message.content, response.usage] mensagens = [{"role": "system", "content": "assistant_name = SARA,attributes =eficiente"}] def converter_audio_em_texto(nome_arquivo): # Inicializa o reconhecedor de fala r = sr.Recognizer() # Carrega o arquivo de áudio with sr.AudioFile(nome_arquivo) as source: audio_data = r.record(source) # Tenta reconhecer o fala usando o Google Web Speech API try: texto = r.recognize_google(audio_data, language='pt-BR') print("Texto transcrito: " + texto) return texto except sr.UnknownValueError: print("Google Speech Recognition não conseguiu entender o áudio.") except sr.RequestError as e: print(f"Não foi possível solicitar resultados do serviço Google Speech Recognition; {e}") while True: # Ask a question question = input("Perguntar para Sara (\"sair\"): ") if question == "sair" : print("saindo") break elif question == "": gravar_audio(nome_arquivo) texto_transcrito = converter_audio_em_texto(nome_arquivo) mensagens.append({"role": "user", "content": str(texto_transcrito)}) answer = gerar_resposta(mensagens) print("user:", question) print("Sara:", answer[0], "\nCusto:\n", answer[1]) mensagens.append({"role": "assistant", "content": answer[0]}) tts = gTTS(text=answer[0], lang=idioma, slow=False) # Salvando o arquivo de áudio arquivo_audio = "audio.mp3" tts.save(arquivo_audio) response = client.audio.speech.create( model="tts-1", voice="nova", input=answer[0], ) response.stream_to_file(arquivo_audio) # Reproduzindo o arquivo de áudio playsound(arquivo_audio) # Opcional: remover o arquivo de áudio após a reprodução os.remove(arquivo_audio) else: mensagens.append({"role": "user", "content": str(question)}) answer = gerar_resposta(mensagens) print("user:", question) print("Sara:", answer[0], "\nCusto:\n", answer[1]) mensagens.append({"role": "assistant", "content": answer[0]}) # tts = gTTS(text=answer[0], lang=idioma, slow=False) # Salvando o arquivo de áudio arquivo_audio = "audio.mp3" # tts.save(arquivo_audio) response = client.audio.speech.create( model="tts-1", voice="nova", input=answer[0], ) response.stream_to_file(arquivo_audio) # Reproduzindo o arquivo de áudio playsound(arquivo_audio) # Opcional: remover o arquivo de áudio após a reprodução os.remove(arquivo_audio) debugar = False if debugar: print("Mensagens", mensagens, type(mensagens))
[]
2024-01-10
AdrieVanDijke/cat-va-app
a3d~a3dcontroler.py
import pinecone from langchain.vectorstores import Pinecone from langchain.embeddings.openai import OpenAIEmbeddings from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate import openai from openai import OpenAI import streamlit as st from a3d.a3d_teksten import A3DTeksten # Maak een client-object client = OpenAI(api_key=openai.api_key) class A3DControler: def __init__( self, a3dmod ): self.a3dmod = a3dmod self.a3dtekst = A3DTeksten() # MAIN ======================================================================== # vraag het de Pinecone database ========================= def ask_the_database(self, query): vectorstore = self.get_database() prompt_template = self.a3dtekst.get_db_prompt_template() PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) chain_type_kwargs = {"prompt": PROMPT} llm = self.get_llm() qa = RetrievalQA.from_chain_type( llm=llm, chain_type='stuff', retriever=vectorstore.as_retriever(), chain_type_kwargs=chain_type_kwargs ) result = qa.run(query) return result # vraag het de OpenAI API -> fine-tuned model ============ def ask_model(self, input_text): llm_response = self.get_tuned_model(input_text) response = llm_response.choices[0].message.content if hasattr(llm_response.choices[0].message, 'content') else "" return response # WORKERS ===================================================================== # maak en onderhoud verbinding met de Pinecone database == @st.cache_resource def get_database(_self): pinecone.init(api_key=_self.a3dmod.pinecone_api_key, environment=_self.a3dmod.pinecone_environment) index = pinecone.Index(_self.a3dmod.pinecone_index_name) embeddings = OpenAIEmbeddings() vectorstore = Pinecone(index, embeddings, "text") return vectorstore # maak en onderhoud verbinding met de OpenAI API ========== @st.cache_resource def get_llm(_self): llm = ChatOpenAI(model_name=_self.a3dmod.aimodel, temperature=_self.a3dmod.temperature, max_tokens=_self.a3dmod.max_tokens) return llm # Haal een getraind model op bij OpenAI =================== def get_tuned_model(self, input_text): system_prompt = self.a3dtekst.get_qa_system_prompt() completion = client.chat.completions.create( model=self.a3dmod.finemodel, temperature=self.a3dmod.fine_temperature, max_tokens=self.a3dmod.fine_max_tokens, messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": input_text} ] ) return completion
[ "question", "context" ]
2024-01-10
NimmyThomas/deep-learning-with-databricks
Reference~CV%20-%20Generative%20Adversarial%20Networks.py
# Databricks notebook source # MAGIC %md-sandbox # MAGIC # MAGIC <div style="text-align: center; line-height: 0; padding-top: 9px;"> # MAGIC <img src="https://databricks.com/wp-content/uploads/2018/03/db-academy-rgb-1200px.png" alt="Databricks Learning" style="width: 600px"> # MAGIC </div> # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC # Generative Adversarial Networks (GANs) # MAGIC # MAGIC ## ![Spark Logo Tiny](https://files.training.databricks.com/images/105/logo_spark_tiny.png) In this lesson you:<br> # MAGIC - Learn about generative and discriminative models that make up generative adversarial networks # MAGIC - Apply dropout # MAGIC # MAGIC ### Discriminator # MAGIC # MAGIC A discriminative model, at its core is a Real/fake classifier. It takes counterfeits and real values as input, and predicts probability of counterfeit. # MAGIC # MAGIC # MAGIC ### Generator # MAGIC # MAGIC A generative model captures the data distribution, takes noise vecotrs from latent space as input, outputs a counterfeit. # MAGIC # MAGIC # MAGIC ### GANs # MAGIC # MAGIC <img src="https://miro.medium.com/max/3000/1*t82vgL9KcDVpT4JqCb9Q4Q.png" width=1000> # COMMAND ---------- # MAGIC %md This was a very original architecture in deep learning when it was first released by <a href="https://arxiv.org/pdf/1406.2661.pdf" target="_blank">Ian Goodfellow et al in 2014</a>. It was the first network that contains a generator and a discriminator. The two models compete against each other during training of GANs. GANs eventually generates fairly realistic synthetic images as the discriminator becomes smarter at distinguishing between real and fake images. # MAGIC # MAGIC The algorithm: # MAGIC - G takes noise as input, outputs a counterfeit # MAGIC - D takes counterfeits and real values as input, outputs P(counterfeit) # MAGIC # MAGIC The following techniques can help **prevent overfitting**, # MAGIC - Alternate k steps of optimizing D and one step of optimizing G # MAGIC - Start with k of at least 5 # MAGIC - Use *log(1 - D(G(z)))* to provide stronger, non-saturated gradients # MAGIC # MAGIC <img src="https://media-exp1.licdn.com/dms/image/C5112AQGWsO2ZFbKnYQ/article-inline_image-shrink_1000_1488/0/1520192659145?e=1647475200&v=beta&t=06VrAMeZgpmcvw0K-bQV892ecuBlWJggwv045e4Jz8Q" style="width:1000px"> # MAGIC # MAGIC GANs can be used in generating art, deep fakes, up-scaling graphics, and astronomy research. For example, we can use GANs to generate synthetic handwritten images, resembling the MNIST dataset. # MAGIC # MAGIC <img src = "https://tensorflow.org/images/gan/dcgan.gif" width=600> # MAGIC # MAGIC # MAGIC As a follow-up, we highly recommend this <a href="https://www.coursera.org/specializations/generative-adversarial-networks-gans?" target="_blank">GANs</a> course from coursera. There are other very interesting applications of generative models, such as <a href="https://openai.com/blog/glow/" target="_blank">Glow</a> from OpenAI and <a href="https://ai.facebook.com/blog/wav2vec-unsupervised-speech-recognition-without-supervision/" target="_blank">speech recognition</a> from Facebook AI. # COMMAND ---------- # MAGIC %md # MAGIC Here, we will be using the <a href="https://github.com/zalandoresearch/fashion-mnist" target="_blank">Fashion MNIST</a> dataset that you have seen before in the SHAP for CNNs lab! <br> # MAGIC # MAGIC Our goal is to create synthetic images of these clothing items using GANs. # MAGIC # MAGIC <img src="https://tensorflow.org/images/fashion-mnist-sprite.png" width=500> # COMMAND ---------- # MAGIC %run ../Includes/Classroom-Setup # COMMAND ---------- import tensorflow as tf ### Load data - we don't care about the testing sets since there is no concept of "testing" in GAN ### Our sole goal is to generate fake images that look like the real images ((X_train, y_train), (_, _)) = tf.keras.datasets.fashion_mnist.load_data() # COMMAND ---------- # MAGIC %md # MAGIC Let's look at a sample image. # COMMAND ---------- import matplotlib.pyplot as plt plt.imshow(X_train[1]) # COMMAND ---------- X_train.shape # COMMAND ---------- # MAGIC %md # MAGIC We need to preprocess the images first. Notice that the image shape above only has height and width, but lacks the number of channels. We need to add an extra dimension for the channel and scale the images into (-1,1). Scaling is a common image preprocessing step. # COMMAND ---------- import numpy as np X_train = np.expand_dims(X_train, axis=-1).astype("float32") # This is the same as using X_train.reshape(X_train.shape[0], 28, 28, 1) X_train = (X_train - 127.5) / 127.5 # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC Let's first define a discriminator. # MAGIC # MAGIC It's a best practice to use **`LeakyReLU`** as opposed to **`ReLU`** for the discriminator. It’s similar to ReLU, but it relaxes sparsity constraints by allowing small negative activation values, rather than outputting zero activation values for negative inputs. Here is a <a href="https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#leakyrelu" target="_blank">resource</a> that dives into different activation functions, including ReLU and Leaky ReLU. # MAGIC # MAGIC <img src="https://miro.medium.com/max/2100/1*A_Bzn0CjUgOXtPCJKnKLqA.jpeg"> # COMMAND ---------- # MAGIC %md # MAGIC Regarding choosing a kernel size, the best practice in GANs is to pick a number that is divisible by the stride size whenever a strided Conv2DTranspose or Conv2D is used. This is to reduce the checkerboard artifacts caused by unequal coverage of the pixel space in the generator. We will cover in the later part of the notebook what a Conv2DTranpose layer is! # MAGIC <br> # MAGIC # MAGIC With checkerboard artifacts: <br> # MAGIC <img src="https://distill.pub/2016/deconv-checkerboard/assets/style_artifacts.png" width="500" height="300"> # MAGIC # MAGIC <br> # MAGIC Reduced checkerboard artifacts: <br> # MAGIC <img src="https://distill.pub/2016/deconv-checkerboard/assets/style_clean.png" width="500" height="300"> # MAGIC # MAGIC Head over to this <a href="https://distill.pub/2016/deconv-checkerboard/" target="_blank">link</a> to see what "unequal coverage of the pixel space" means and play with the stride and kernel sizes! # COMMAND ---------- from tensorflow.keras import layers def def_discriminator(): """ A discriminator is simply a binary classification model to tell if an image is real or fake """ model = tf.keras.Sequential() model.add(layers.Conv2D(32, (6, 6), padding="same", input_shape=(28,28,1))) # Note that the input shape (28, 28) here matches the pixels of the original images model.add(layers.LeakyReLU()) model.add(layers.Conv2D(64, (6, 6), strides=(2,2), padding="same")) model.add(layers.LeakyReLU()) model.add(layers.Conv2D(64, (6, 6), strides=(2,2), padding="same")) model.add(layers.LeakyReLU()) model.add(layers.Flatten()) model.add(layers.Dropout(0.3)) ## Generally use between 0.3 and 0.6 model.add(layers.Dense(1, activation="sigmoid")) model.compile(loss=tf.keras.losses.binary_crossentropy, optimizer=tf.keras.optimizers.Adam(learning_rate=LR/2, decay=LR/NUM_EPOCH)) # Half the generator's learning rate to help stabilize equilibrium return model # COMMAND ---------- # MAGIC %md # MAGIC Now, let's define our generator. There are two new components we haven't learned: # MAGIC * dropout # MAGIC * transposed convolutional layers # MAGIC # MAGIC Dropout is a regularization method that reduces overfitting by randomly and temporarily removing nodes during training. # MAGIC # MAGIC It works like this: <br> # MAGIC # MAGIC * Apply to most type of layers (e.g. fully connected, convolutional, recurrent) and larger networks # MAGIC * Temporarily and randomly remove nodes and their connections during each training cycle # MAGIC # MAGIC ![](https://files.training.databricks.com/images/nn_dropout.png) # MAGIC # MAGIC <img src="https://files.training.databricks.com/images/icon_note_24.png"/> See the original paper here: <a href="http://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf" target="_blank">Dropout: A Simple Way to Prevent Neural Networks from Overfitting</a> # COMMAND ---------- # MAGIC %md # MAGIC Now, onto transposed convolutional layers. <a href="https://towardsdatascience.com/types-of-convolutions-in-deep-learning-717013397f4d" target="_blank">Transposed convolutional layers</a>, also known as fractionally-strided convolution, are commonly used in GANs. # MAGIC # MAGIC Transposed convolution helps us to: # MAGIC * Accept an input from a previous layer in the network # MAGIC * Produce an output that is larger than the input # MAGIC * Perform a convolution but allow us to reconstruct our target spatial resolution from before # MAGIC # MAGIC This means that transposed convolutional layers combine upscaling of an image with a convolution. # MAGIC # MAGIC <img src="https://www.programmersought.com/images/174/ebc5c4c74ae847b31bc1e3a395f21b9e.png"> # MAGIC # MAGIC Source: https://arxiv.org/pdf/1603.07285.pdf # COMMAND ---------- DIM = 7 DEPTH = 64 def def_generator(noise_dim): """ The purpose of the generator is to generate fake/synthetic images. """ input_shape = (DIM, DIM, DEPTH) model = tf.keras.Sequential() model.add(layers.Dense(DIM * DIM * DEPTH, input_dim=noise_dim)) model.add(layers.LeakyReLU()) ### Reshape the output of the previous layer set model.add(layers.Reshape(input_shape)) # Not using a kernel size divisible by the stride for better performance in this particular case model.add(layers.Conv2DTranspose(32, (3, 3), strides=(2,2), padding="same")) model.add(layers.LeakyReLU()) model.add(layers.Conv2DTranspose(32, (3, 3), strides=(2,2), padding="same")) model.add(layers.LeakyReLU()) ### 1 represents the number of channels, for grayscale; 3 for RGB model.add(layers.Conv2D(1, (3, 3), activation="tanh", padding="same")) return model # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC Recall that generative adversarial networks (GANs) is composed of both the discriminator and the generator. So we are going to define our GAN using both models. # COMMAND ---------- from tensorflow.keras.layers import Input from tensorflow.keras.models import Model def def_gan(noise_dim, discriminator, generator): ### Freeze discriminator weights so that ### the feedback from the discriminator enables the generator to learn ### how to generate better fake images discriminator.trainable = False gan_input = Input(shape=(noise_dim,)) gan_output = discriminator(generator(gan_input)) gan_model = Model(gan_input, gan_output) gan_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=LR, decay=LR/NUM_EPOCH), loss=tf.keras.losses.binary_crossentropy) return gan_model # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC Finally, we are ready to train our GAN! This should take a few mins to train - if you are impatient, you can stop the training at any point and visualize it below. # COMMAND ---------- from sklearn.utils import shuffle tf.random.set_seed(42) LR = 1e-3 NUM_EPOCH = 1 BATCH_SIZE = 64 NOISE_DIM = 100 ### Save some random noise examples in the beginning so we can plot ### what the generator has learned at the end of the training using these noise examples num_examples_to_generate = 16 benchmark_noise = tf.random.normal([num_examples_to_generate, NOISE_DIM]) ### Define generator, discriminator, and GAN discriminator = def_discriminator() generator = def_generator(NOISE_DIM) gan = def_gan(NOISE_DIM, discriminator, generator) ### Calculate the number of training iterations batches_per_epoch = int(X_train.shape[0] / BATCH_SIZE) n_steps = batches_per_epoch * NUM_EPOCH ### Shuffle once outside the loop because only doing one epoch np.random.shuffle(X_train) ### Training GAN starts here for step in range(n_steps): ### Step 1: Generate noise vector noise = tf.random.normal([BATCH_SIZE, NOISE_DIM]) ### Step 2: Pass noise vector through generator to generate fake images fake_images = generator.predict(noise) ### Step 3: Sample real images and mix with fake ones ### Sample real images from the training data real_image_batch = X_train[:BATCH_SIZE] real_fake_image_mix = np.concatenate([real_image_batch, fake_images]) mix_labels = np.concatenate((np.ones(BATCH_SIZE), np.zeros(BATCH_SIZE)), axis=0) mix_labels += 0.05 * np.random.random(mix_labels.shape) # A best practice: add random noise to your labels ### Step 4: Train discriminator on mixed set so that it knows to distinguish between the two correctly dis_loss = discriminator.train_on_batch(real_fake_image_mix, mix_labels) ### Steps 5 and 6 ### Step 5: Generate noise vectors again but purposely label them as "real", try to fool the discriminator ### Step 6: Train GAN using noise vectors labeled as "real" images ### Update weights of generator based on feedback of discriminator, thus allowing us to generate more real images noise = tf.random.normal([BATCH_SIZE, NOISE_DIM]) misleading_labels = np.ones(BATCH_SIZE, dtype=int) gan_loss = gan.train_on_batch(noise, misleading_labels) print(f"Step {step}....................") print(f"discriminator loss: {round(dis_loss, 2)}") print(f"adversarial loss: {round(gan_loss, 2)}\n") # COMMAND ---------- # MAGIC %md # MAGIC We could now play around with the GAN with different learning rates, batch sizes, and/or number of epochs. It's an iterative process to get the best combination of hyperparameters. # MAGIC # MAGIC But for now, let's plot to see what the generative model has learned at the end of the training process! # COMMAND ---------- def generate_images(generator, benchmark_noise): """ Generate synthetic images from the initial noise """ ### Notice `training` is set to False so that all layers are in inference mode. predictions = generator(benchmark_noise, training=False) for i in range(predictions.shape[0]): plt.subplot(4, 4, i+1) ### We then scale our image data back from the tanh range [-1, 1] to [0, 255] plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5) plt.axis('off') generate_images(generator, benchmark_noise) # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC GANs generally take a long time to train in order to achieve good performance. # MAGIC # MAGIC However, you can tell that our GAN has learned that the signals of the clothing items are concentrated in the center and the borders of the images are dark, just like our training images! # COMMAND ---------- # MAGIC %md # MAGIC # MAGIC GANs are really quite complicated. Throughout this notebook, we have incorporated some best practices found in Francois Chollet's Deep Learning with Python book. However, it's worth noting that the best practices are still very much dependent on the data and the architecture. # MAGIC # MAGIC To recap, here are the best practices we have employed: <br> # MAGIC <br> # MAGIC * Sample random vectors from a normal distribution, rather than a uniform distribution # MAGIC * Add dropout to the discriminator (generally between 0.3 and 0.6) # MAGIC * Add noise to the class labels when training the discriminator # MAGIC * Use batch normalization in the generator (this is data- and architecture-dependent, so experiment with it) # MAGIC * Use a kernel size that is divisible by the stride size whenever a strided Conv2DTranspose or Conv2D is used in both the generator and the discriminator. (Note: We incorporated this in only the generator since it gave better performance.) # MAGIC # MAGIC Additionally, if adversarial (generator) loss rises a lot while your discriminator loss falls to 0, you can try reducing the discriminator's learning rate and increasing its dropout rate. # COMMAND ---------- # MAGIC %md Additional interesting generator applications include: # MAGIC # MAGIC - <a href="https://arxiv.org/abs/1508.06576" target="_blank">**Style Transfer to create artistic images**</a> # MAGIC # MAGIC <img src="https://tensorflow.org/tutorials/generative/images/stylized-image.png" width=600> # MAGIC # MAGIC # MAGIC - <a href="https://deepdreamgenerator.com/" target="_blank">**Deep Dream**</a> # MAGIC # MAGIC <img src="https://b2h3x3f6.stackpathcdn.com/assets/landing/img/gallery/4.jpg" width=600> # COMMAND ---------- # MAGIC %md-sandbox # MAGIC &copy; 2022 Databricks, Inc. All rights reserved.<br/> # MAGIC Apache, Apache Spark, Spark and the Spark logo are trademarks of the <a href="https://www.apache.org/">Apache Software Foundation</a>.<br/> # MAGIC <br/> # MAGIC <a href="https://databricks.com/privacy-policy">Privacy Policy</a> | <a href="https://databricks.com/terms-of-use">Terms of Use</a> | <a href="https://help.databricks.com/">Support</a>
[]
2024-01-10
cwijayasundara/rag_tuning_research
hyde.py
import dotenv from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings dotenv.load_dotenv() from langchain.chains import LLMChain, HypotheticalDocumentEmbedder from langchain.prompts import PromptTemplate from langchain.document_loaders import TextLoader embedding_model = OpenAIEmbeddings() llm = ChatOpenAI(temperature=0) from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma loaders = [ TextLoader('langchain_blog_posts/blog.langchain.dev_announcing-langsmith_.txt'), TextLoader('langchain_blog_posts/blog.langchain.dev_benchmarking-question-answering-over-csv-data_.txt'), TextLoader('langchain_blog_posts/blog.langchain.dev_chat-loaders-finetune-a-chatmodel-in-your-voice_.txt'), ] docs = [] for l in loaders: docs.extend(l.load()) print(len(docs)) text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_documents(docs) prompt_template = """Please answer the user's question as related to Large Language Models Question: {question} Answer:""" prompt = PromptTemplate(input_variables=["question"], template=prompt_template) llm_chain = LLMChain(llm=llm, prompt=prompt) embeddings = HypotheticalDocumentEmbedder( llm_chain=llm_chain, base_embeddings=embedding_model ) docsearch = Chroma.from_documents(texts, embeddings) query = "What are chat loaders?" docs = docsearch.similarity_search(query) print(docs[0].page_content)
[ "question", "Please answer the user's question as related to Large Language Models\nQuestion: {question}\nAnswer:" ]
2024-01-10
cwijayasundara/rag_tuning_research
hybrid_search.py
import dotenv dotenv.load_dotenv() from langchain.retrievers import BM25Retriever, EnsembleRetriever from langchain.vectorstores import FAISS from langchain.embeddings.openai import OpenAIEmbeddings embedding = OpenAIEmbeddings() doc_list = [ "I like apples", "I like oranges", "Apples and oranges are fruits", "I like computers by Apple", "I love fruit juice" ] # initialize the bm25 retriever and faiss retriever bm25_retriever = BM25Retriever.from_texts(doc_list) bm25_retriever.k = 2 result = bm25_retriever.get_relevant_documents("Apple") print("apple", result) result = bm25_retriever.get_relevant_documents("a green fruit") print("a green fruit", result) result = bm25_retriever.dict print("dict", result) # Embeddings - Dense retrievers FAISS faiss_vectorstore = FAISS.from_texts(doc_list, embedding) faiss_retriever = faiss_vectorstore.as_retriever(search_kwargs={"k": 2}) result = faiss_retriever.get_relevant_documents("A green fruit") print("A green fruit from dense retriever", result) # Ensemble Retriever # initialize the ensemble retriever ensemble_retriever = EnsembleRetriever(retrievers=[bm25_retriever, faiss_retriever], weights=[0.5, 0.5]) docs = ensemble_retriever.get_relevant_documents("A green fruit") print("from the ensemble retriever", docs) docs = ensemble_retriever.get_relevant_documents("Apple Phones") print(docs)
[]
2024-01-10
cwijayasundara/rag_tuning_research
contextual_compression.py
import dotenv dotenv.load_dotenv() from langchain.vectorstores import FAISS from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.document_loaders import TextLoader from langchain.embeddings import OpenAIEmbeddings loaders = [ TextLoader('langchain_blog_posts/blog.langchain.dev_announcing-langsmith_.txt'), TextLoader('langchain_blog_posts/blog.langchain.dev_benchmarking-question-answering-over-csv-data_.txt'), ] docs = [] for l in loaders: docs.extend(l.load()) print(len(docs)) text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) texts = text_splitter.split_documents(docs) embedding = OpenAIEmbeddings() # Helper function for printing docs def pretty_print_docs(docs): print(f"\n{'-' * 100}\n".join([f"Document {i + 1}:\n\n" + d.page_content for i, d in enumerate(docs)])) # plain retriever retriever = FAISS.from_documents(texts, embedding).as_retriever() docs = retriever.get_relevant_documents("What is LangSmith?") pretty_print_docs(docs) # Adding contextual compression with an LLMChainExtractor from langchain.chat_models import ChatOpenAI from langchain.retrievers import ContextualCompressionRetriever from langchain.retrievers.document_compressors import LLMChainExtractor # making the compressor llm = ChatOpenAI(temperature=0) compressor = LLMChainExtractor.from_llm(llm) # it needs a base retriever (we're using FAISS Retriever) and a compressor (Made above) compression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=retriever) compressed_docs = compression_retriever.get_relevant_documents("What is LangSmith?") pretty_print_docs(compressed_docs) # EmbeddingsFilter from langchain.embeddings import OpenAIEmbeddings from langchain.retrievers.document_compressors import EmbeddingsFilter embeddings = OpenAIEmbeddings() embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76) compression_retriever = ContextualCompressionRetriever(base_compressor=embeddings_filter, base_retriever=retriever) compressed_docs = compression_retriever.get_relevant_documents("What is LangSmith") pretty_print_docs(compressed_docs) # Pipelines # Stringing compressors and document transformers together from langchain.document_transformers import EmbeddingsRedundantFilter from langchain.retrievers.document_compressors import DocumentCompressorPipeline from langchain.text_splitter import CharacterTextSplitter splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0, separator=". ") redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings) relevant_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76) # making the pipeline pipeline_compressor = DocumentCompressorPipeline( transformers=[splitter, redundant_filter, relevant_filter] ) compression_retriever = ContextualCompressionRetriever(base_compressor=pipeline_compressor, base_retriever=retriever) compressed_docs = compression_retriever.get_relevant_documents("What is LangSmith") pretty_print_docs(compressed_docs)
[]
2024-01-10
cwijayasundara/rag_tuning_research
parent_document_retreaver.py
import dotenv dotenv.load_dotenv() from langchain.vectorstores import Chroma from langchain.retrievers import ParentDocumentRetriever # Text Splitting & Docloader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.storage import InMemoryStore from langchain.document_loaders import TextLoader from langchain.embeddings.openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings() loaders = [ TextLoader('langchain_blog_posts/blog.langchain.dev_announcing-langsmith_.txt'), TextLoader('langchain_blog_posts/blog.langchain.dev_benchmarking-question-answering-over-csv-data_.txt'), ] docs = [] for l in loaders: docs.extend(l.load()) print(f'Loaded {len(docs)} documents') # This text splitter is used to create the child documents child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) # The vectorstore to use to index the child chunks vectorstore = Chroma( collection_name="full_documents", embedding_function=embeddings ) # The storage layer for the parent documents store = InMemoryStore() full_doc_retriever = ParentDocumentRetriever( vectorstore=vectorstore, docstore=store, child_splitter=child_splitter, ) full_doc_retriever.add_documents(docs, ids=None) retrieved_docs = full_doc_retriever.get_relevant_documents("what is langsmith") print(retrieved_docs[0].page_content) # Retrieving larger chunks # This text splitter is used to create the parent documents - The big chunks parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000) # This text splitter is used to create the child documents - The small chunks # It should create documents smaller than the parent child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) # The vectorstore to use to index the child chunks vectorstore = Chroma(collection_name="split_parents", embedding_function=embeddings) # The storage layer for the parent documents store = InMemoryStore() big_chunks_retriever = ParentDocumentRetriever( vectorstore=vectorstore, docstore=store, child_splitter=child_splitter, parent_splitter=parent_splitter, ) big_chunks_retriever.add_documents(docs) print(len(list(store.yield_keys()))) sub_docs = vectorstore.similarity_search("what is langsmith") print(sub_docs[0].page_content) retrieved_docs = big_chunks_retriever.get_relevant_documents("what is langsmith") print(retrieved_docs[0].page_content) print(retrieved_docs[1].page_content) from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI qa = RetrievalQA.from_chain_type(llm=ChatOpenAI(), chain_type="stuff", retriever=big_chunks_retriever) query = "What is Langsmith?" response = qa.run(query) print(response)
[]
2024-01-10
cwijayasundara/rag_tuning_research
multi_query_retreaver.py
import dotenv dotenv.load_dotenv() # Build a sample vectorDB from langchain.document_loaders import WebBaseLoader from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import Chroma # Load blog post loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/") data = loader.load() # Split text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) splits = text_splitter.split_documents(data) # VectorDB embedding = OpenAIEmbeddings() vectordb = Chroma.from_documents(documents=splits, embedding=embedding) # Simple usage from langchain.chat_models import ChatOpenAI from langchain.retrievers.multi_query import MultiQueryRetriever question = "What are the approaches to Task Decomposition?" llm = ChatOpenAI(temperature=0) retriever_from_llm = MultiQueryRetriever.from_llm( retriever=vectordb.as_retriever(), llm=llm ) # Set logging for the queries import logging logging.basicConfig() logging.getLogger("langchain.retrievers.multi_query").setLevel(logging.INFO) unique_docs = retriever_from_llm.get_relevant_documents(query=question) print(len(unique_docs)) # Supplying your own prompt from typing import List from langchain.chains import LLMChain from langchain.output_parsers import PydanticOutputParser from langchain.prompts import PromptTemplate from pydantic import BaseModel, Field # Output parser will split the LLM result into a list of queries class LineList(BaseModel): # "lines" is the key (attribute name) of the parsed output lines: List[str] = Field(description="Lines of text") class LineListOutputParser(PydanticOutputParser): def __init__(self) -> None: super().__init__(pydantic_object=LineList) def parse(self, text: str) -> LineList: lines = text.strip().split("\n") return LineList(lines=lines) output_parser = LineListOutputParser() QUERY_PROMPT = PromptTemplate( input_variables=["question"], template="""You are an AI language model assistant. Your task is to generate five different versions of the given user question to retrieve relevant documents from a vector database. By generating multiple perspectives on the user question, your goal is to help the user overcome some of the limitations of the distance-based similarity search. Provide these alternative questions separated by newlines. Original question: {question}""", ) llm = ChatOpenAI(temperature=0) # Chain llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser) # Other inputs question = "What are the approaches to Task Decomposition?" # Run retriever = MultiQueryRetriever( retriever=vectordb.as_retriever(), llm_chain=llm_chain, parser_key="lines" ) # "lines" is the key (attribute name) of the parsed output # Results unique_docs = retriever.get_relevant_documents( query="What does the course say about regression?" ) print(len(unique_docs)) print(unique_docs)
[ "question", "You are an AI language model assistant. Your task is to generate five \n different versions of the given user question to retrieve relevant documents from a vector \n database. By generating multiple perspectives on the user question, your goal is to help\n the user overcome some of the limitations of the distance-based similarity search. \n Provide these alternative questions separated by newlines.\n Original question: {question}" ]
2024-01-10
cwijayasundara/rag_tuning_research
rag_langchain.py
import dotenv import langchain import bs4 from langchain import hub from langchain.chat_models import ChatOpenAI from langchain.document_loaders import WebBaseLoader from langchain.embeddings import OpenAIEmbeddings from langchain.schema import StrOutputParser from langchain.schema.runnable import RunnablePassthrough from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import Chroma dotenv.load_dotenv() loader = WebBaseLoader( web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",), bs_kwargs=dict( parse_only=bs4.SoupStrainer( class_=("post-content", "post-title", "post-header") ) ), ) docs = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) splits = text_splitter.split_documents(docs) vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings()) retriever = vectorstore.as_retriever() prompt = hub.pull("rlm/rag-prompt") llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) rag_chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, \ PromptTemplate response = rag_chain.invoke("What is Task Decomposition?") print(response) # RagFusion pipeline original_query = "What is Task Decomposition?" prompt = ChatPromptTemplate(input_variables=['original_query'], messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful assistant that generates multiple search queries based on a single input query.')), HumanMessagePromptTemplate( prompt=PromptTemplate(input_variables=['original_query'], template='Generate multiple search queries related to: {question} \n OUTPUT (4 queries):'))]) print("The prompt is: ", prompt) generate_queries = ( prompt | ChatOpenAI(temperature=0) | StrOutputParser() | (lambda x: x.split("\n")) ) from langchain.load import dumps, loads def reciprocal_rank_fusion(results: list[list], k=60): fused_scores = {} for docs in results: # Assumes the docs are returned in sorted order of relevance for rank, doc in enumerate(docs): doc_str = dumps(doc) if doc_str not in fused_scores: fused_scores[doc_str] = 0 previous_score = fused_scores[doc_str] fused_scores[doc_str] += 1 / (rank + k) reranked_results = [ (loads(doc), score) for doc, score in sorted(fused_scores.items(), key=lambda x: x[1], reverse=True) ] return reranked_results ragfusion_chain = generate_queries | retriever.map() | reciprocal_rank_fusion langchain.debug = True ragfusion_chain.input_schema.schema() ragfusion_chain.invoke({"question": original_query}) model = "gpt-3.5-turbo" from langchain.schema.runnable import RunnablePassthrough template = """Answer the question based only on the following context: {context} Question: {question} """ prompt = ChatPromptTemplate.from_template(template) full_rag_fusion_chain = ( { "context": ragfusion_chain, "question": RunnablePassthrough() } | prompt | model | StrOutputParser() ) full_rag_fusion_chain.input_schema.schema() full_rag_fusion_chain.invoke({"question": "Tell me about AutoGPT"})
[ "original_query", "Generate multiple search queries related to: {question} \n OUTPUT (4 queries):", "rlm/rag-prompt", "You are a helpful assistant that generates multiple search queries based on a single input query.", "Answer the question based only on the following context:\n{context}\n\nQuestion: {question}\n" ]
2024-01-10
cwijayasundara/rag_tuning_research
research_assistant.py
import dotenv from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser import requests from bs4 import BeautifulSoup from langchain.schema.runnable import RunnablePassthrough, RunnableLambda from langchain.utilities import DuckDuckGoSearchAPIWrapper import json dotenv.load_dotenv() RESULTS_PER_QUESTION = 3 ddg_search = DuckDuckGoSearchAPIWrapper() def web_search(query: str, num_results: int = RESULTS_PER_QUESTION): results = ddg_search.results(query, num_results) return [r["link"] for r in results] SUMMARY_TEMPLATE = """{text} ----------- Using the above text, answer in short the following question: > {question} ----------- if the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available.""" # noqa: E501 SUMMARY_PROMPT = ChatPromptTemplate.from_template(SUMMARY_TEMPLATE) def scrape_text(url: str): # Send a GET request to the webpage try: response = requests.get(url) # Check if the request was successful if response.status_code == 200: # Parse the content of the request with BeautifulSoup soup = BeautifulSoup(response.text, "html.parser") # Extract all text from the webpage page_text = soup.get_text(separator=" ", strip=True) # Print the extracted text return page_text else: return f"Failed to retrieve the webpage: Status code {response.status_code}" except Exception as e: print(e) return f"Failed to retrieve the webpage: {e}" url = "https://blog.langchain.dev/announcing-langsmith/" scrape_and_summarize_chain = RunnablePassthrough.assign( summary=RunnablePassthrough.assign( text=lambda x: scrape_text(x["url"])[:10000] ) | SUMMARY_PROMPT | ChatOpenAI(model="gpt-3.5-turbo-1106") | StrOutputParser() ) | (lambda x: f"URL: {x['url']}\n\nSUMMARY: {x['summary']}") web_search_chain = RunnablePassthrough.assign( urls=lambda x: web_search(x["question"]) ) | (lambda x: [{"question": x["question"], "url": u} for u in x["urls"]]) | scrape_and_summarize_chain.map() # This is for Arxiv from langchain.retrievers import ArxivRetriever retriever = ArxivRetriever() SUMMARY_TEMPLATE = """{doc} ----------- Using the above text, answer in short the following question: > {question} ----------- if the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available.""" # noqa: E501 SUMMARY_PROMPT = ChatPromptTemplate.from_template(SUMMARY_TEMPLATE) scrape_and_summarize_chain = RunnablePassthrough.assign( summary=SUMMARY_PROMPT | ChatOpenAI(model="gpt-3.5-turbo-1106") | StrOutputParser() ) | (lambda x: f"Title: {x['doc'].metadata['Title']}\n\nSUMMARY: {x['summary']}") web_search_chain = RunnablePassthrough.assign( docs=lambda x: retriever.get_summaries_as_docs(x["question"]) ) | (lambda x: [{"question": x["question"], "doc": u} for u in x["docs"]]) | scrape_and_summarize_chain.map() SEARCH_PROMPT = ChatPromptTemplate.from_messages( [ ( "user", "Write 3 google search queries to search online that form an " "objective opinion from the following: {question}\n" "You must respond with a list of strings in the following format: " '["query 1", "query 2", "query 3"].', ), ] ) search_question_chain = SEARCH_PROMPT | ChatOpenAI(temperature=0) | StrOutputParser() | json.loads full_research_chain = search_question_chain | (lambda x: [{"question": q} for q in x]) | web_search_chain.map() WRITER_SYSTEM_PROMPT = "You are an AI critical thinker research assistant. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text." # noqa: E501 # Report prompts from https://github.com/assafelovic/gpt-researcher/blob/master/gpt_researcher/master/prompts.py RESEARCH_REPORT_TEMPLATE = """Information: -------- {research_summary} -------- Using the above information, answer the following question or topic: "{question}" in a detailed report -- \ The report should focus on the answer to the question, should be well structured, informative, \ in depth, with facts and numbers if available and a minimum of 1,200 words. You should strive to write the report as long as you can using all relevant and necessary information provided. You must write the report with markdown syntax. You MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions. Write all used source urls at the end of the report, and make sure to not add duplicated sources, but only one reference for each. You must write the report in apa format. Please do your best, this is very important to my career.""" # noqa: E501 prompt = ChatPromptTemplate.from_messages( [ ("system", WRITER_SYSTEM_PROMPT), ("user", RESEARCH_REPORT_TEMPLATE), ] ) def collapse_list_of_lists(list_of_lists): content = [] for l in list_of_lists: content.append("\n\n".join(l)) return "\n\n".join(content) chain = RunnablePassthrough.assign( research_summary=full_research_chain | collapse_list_of_lists ) | prompt | ChatOpenAI(model="gpt-3.5-turbo-1106") | StrOutputParser() from fastapi import FastAPI from langserve import add_routes app = FastAPI( title="LangChain Server", version="1.0", description="A simple api server using Langchain's Runnable interfaces", ) add_routes( app, chain, path="/research-assistant", ) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="localhost", port=8000)
[ "[\"query 1\", \"query 2\", \"query 3\"].", "objective opinion from the following: {question}\n", "{doc} \n\n-----------\n\nUsing the above text, answer in short the following question: \n\n> {question}\n\n-----------\nif the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available.", "Write 3 google search queries to search online that form an ", "{text} \n\n-----------\n\nUsing the above text, answer in short the following question: \n\n> {question}\n\n-----------\nif the question cannot be answered using the text, imply summarize the text. Include all factual information, numbers, stats etc if available.", "You must respond with a list of strings in the following format: ", "[('user', 'Write 3 google search queries to search online that form an objective opinion from the following: {question}\\nYou must respond with a list of strings in the following format: [\"query 1\", \"query 2\", \"query 3\"].')]", "You are an AI critical thinker research assistant. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text.", "[('system', PLACEHOLDER), ('user', PLACEHOLDER)]", "Information:\n--------\n{research_summary}\n--------\n\nUsing the above information, answer the following question or topic: \"{question}\" in a detailed report -- The report should focus on the answer to the question, should be well structured, informative, in depth, with facts and numbers if available and a minimum of 1,200 words.\n\nYou should strive to write the report as long as you can using all relevant and necessary information provided.\nYou must write the report with markdown syntax.\nYou MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions.\nWrite all used source urls at the end of the report, and make sure to not add duplicated sources, but only one reference for each.\nYou must write the report in apa format.\nPlease do your best, this is very important to my career." ]
2024-01-10
cwijayasundara/rag_tuning_research
self_query_retreaver.py
import dotenv dotenv.load_dotenv() from langchain.schema import Document from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma embeddings = OpenAIEmbeddings() docs = [ Document( page_content="Complex, layered, rich red with dark fruit flavors", metadata={"name":"Opus One", "year": 2018, "rating": 96, "grape": "Cabernet Sauvignon", "color":"red", "country":"USA"}, ), Document( page_content="Luxurious, sweet wine with flavors of honey, apricot, and peach", metadata={"name":"Château d'Yquem", "year": 2015, "rating": 98, "grape": "Sémillon", "color":"white", "country":"France"}, ), Document( page_content="Full-bodied red with notes of black fruit and spice", metadata={"name":"Penfolds Grange", "year": 2017, "rating": 97, "grape": "Shiraz", "color":"red", "country":"Australia"}, ), Document( page_content="Elegant, balanced red with herbal and berry nuances", metadata={"name":"Sassicaia", "year": 2016, "rating": 95, "grape": "Cabernet Franc", "color":"red", "country":"Italy"}, ), Document( page_content="Highly sought-after Pinot Noir with red fruit and earthy notes", metadata={"name":"Domaine de la Romanée-Conti", "year": 2018, "rating": 100, "grape": "Pinot Noir", "color":"red", "country":"France"}, ), Document( page_content="Crisp white with tropical fruit and citrus flavors", metadata={"name":"Cloudy Bay", "year": 2021, "rating": 92, "grape": "Sauvignon Blanc", "color":"white", "country":"New Zealand"}, ), Document( page_content="Rich, complex Champagne with notes of brioche and citrus", metadata={"name":"Krug Grande Cuvée", "year": 2010, "rating": 93, "grape": "Chardonnay blend", "color":"sparkling", "country":"New Zealand"}, ), Document( page_content="Intense, dark fruit flavors with hints of chocolate", metadata={"name":"Caymus Special Selection", "year": 2018, "rating": 96, "grape": "Cabernet Sauvignon", "color":"red", "country":"USA"}, ), Document( page_content="Exotic, aromatic white with stone fruit and floral notes", metadata={"name":"Jermann Vintage Tunina", "year": 2020, "rating": 91, "grape": "Sauvignon Blanc blend", "color":"white", "country":"Italy"}, ), ] vectorstore = Chroma.from_documents(docs, embeddings) # self-querying retriever from langchain.chat_models import ChatOpenAI from langchain.retrievers.self_query.base import SelfQueryRetriever from langchain.chains.query_constructor.base import AttributeInfo metadata_field_info = [ AttributeInfo( name="grape", description="The grape used to make the wine", type="string or list[string]", ), AttributeInfo( name="name", description="The name of the wine", type="string or list[string]", ), AttributeInfo( name="color", description="The color of the wine", type="string or list[string]", ), AttributeInfo( name="year", description="The year the wine was released", type="integer", ), AttributeInfo( name="country", description="The name of the country the wine comes from", type="string", ), AttributeInfo( name="rating", description="The Robert Parker rating for the wine 0-100", type="integer" #float ), ] document_content_description = "Brief description of the wine" llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-1106") retriever = SelfQueryRetriever.from_llm( llm, vectorstore, document_content_description, metadata_field_info, verbose=True ) response = retriever.get_relevant_documents("What are some red wines") print(response) response = retriever.get_relevant_documents("I want a wine that has fruity nodes") print(response) # This example specifies a query and a filter response = retriever.get_relevant_documents("I want a wine that has fruity nodes and has a rating above 97") print(response) # This example specifies a query and composite filter response = retriever.get_relevant_documents( "What's a wine after 2015 but before 2020 that's all earthy" ) print(response)
[]
2024-01-10
gmantri/azure-docs-copilot
src~setup.py
import os import shutil from dotenv import load_dotenv from langchain.docstore.document import Document from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import MarkdownHeaderTextSplitter from langchain.vectorstores import Chroma from file_utils import get_files_with_extension # load environment variables load_dotenv() # set path of chroma db CHROMA_DB_PATH = os.getenv("CHROMA_DB_PATH") continue_with_setup = True if os.path.exists(CHROMA_DB_PATH) and os.path.isdir(CHROMA_DB_PATH): input_prompt = """ Vector store is already initialized. If you continue with the setup, existing store will be deleted and a new store will be created. Do you wish to continue (yes/no): [yes] """ user_selection = input(input_prompt).lower() if user_selection == "yes" or user_selection == "": # delete existing database if needed. shutil.rmtree(CHROMA_DB_PATH) else: continue_with_setup = False if not continue_with_setup: print("Exiting setup.") exit() print("Setting up vector store. It might take some time.") print("-------------------------------------------------") # get the list of markdown files files = get_files_with_extension("data", ".md") AZURE_OPENAI_EMBEDDING_MODEL_NAME = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL_NAME") AZURE_OPENAI_EMBEDDING_MODEL_DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL_DEPLOYMENT_NAME") # create Open AI Embedding # chunk_size must be 1 because of current limitation in Azure Open AI embedding = OpenAIEmbeddings(deployment=AZURE_OPENAI_EMBEDDING_MODEL_DEPLOYMENT_NAME, model=AZURE_OPENAI_EMBEDDING_MODEL_NAME, chunk_size=1) # instantiate chroma db chroma_db = Chroma(persist_directory=CHROMA_DB_PATH, embedding_function=embedding) markdown_splitter = MarkdownHeaderTextSplitter([("#", "h1"), ("##", "h2")]) for file in files: with open(file) as f: try: documents_for_vector_store = [] file_contents = f.read() file_chunks = markdown_splitter.split_text(file_contents) for file_chunk in file_chunks: d = Document(page_content=file_chunk.page_content, metadata={"source": file}) documents_for_vector_store.append(d) chroma_db.add_documents(documents_for_vector_store) message = f"file: {file} added to vector store." print(message) except Exception: print(f"error occurred while processing {file} file.") print(Exception) print(f"{len(files)} files added to vector store")
[ "\n Vector store is already initialized. If you continue with the setup, \n existing store will be deleted and a new store will be created. \n Do you wish to continue (yes/no): [yes]\n " ]
2024-01-10
maximus-powers/the-daily-byte
backend~landingObject.py
from memeObject import MemeObject from imageObject import ImageObject from dbObject import dbObject from llmObject import LLMObject import openai class landingObject: def __init__(self): self.meme_obj = MemeObject() self.img_obj = ImageObject() self.db_obj = dbObject() self.llm_obj = LLMObject() def gen_landing_content(self, user_id): categories = self.db_obj.get_user_categories(user_id).split(',') all_headlines_urls = {} for category in categories: cat_content = self.db_obj.call_category_content(user_id, category) for item in cat_content: all_headlines_urls[item['headline']] = item['url'] ranked_dict = self.llm_obj.rank_dictionary(all_headlines_urls) for article in ranked_dict: headline_temp = self.llm_obj.summarize_article(article['url'], 25) summary_sentence_temp = self.llm_obj.summarize_article(article['url'], 75) if headline_temp is not False and summary_sentence_temp is not False: top_url = article['url'] # jesus llms are so inconsistent if headline_temp.endswith('.'): top_headline = headline_temp[:-1] else: top_headline = headline_temp if summary_sentence_temp.endswith('.'): summary_sentence = summary_sentence_temp[:-1] else: summary_sentence = summary_sentence_temp break # init vars for the loop max_attempts = 3 attempt = 0 image_url = None image_blob = None while attempt < max_attempts and image_url is None: try: # get prompt for image prompt = self.llm_obj.gen_image_prompt(top_headline, summary_sentence) # string --> string # try to generate image image_url = self.img_obj.generate_image(prompt) # string --> string url image_blob = self.img_obj.download_image_as_blob(image_url) # string url --> binary blob except openai.BadRequestError as e: if 'content_policy_violation' in str(e): attempt += 1 print(f"Attempt {attempt}: Content policy violation, trying with a safer prompt.") # modify prompt to be safer prompt = self.llm_obj.gen_safer_image_prompt(prompt) else: raise # re-raise the exception if it's not a content policy violation if image_url is None: print("Failed to generate an image after several attempts.") # # generate meme term # meme_term = self.llm_obj.gen_meme_term(top_headline) # string --> string # # generate meme url # meme_url = self.meme_obj.find_meme(meme_term) # string --> string return { 'headline': top_headline, 'summary': summary_sentence, 'url': top_url, 'alt_text': 'not working rn', 'image_blob': image_blob }
[]
2024-01-10
maximus-powers/the-daily-byte
backend~imageObject.py
from openai import OpenAI import requests import os class ImageObject: def __init__(self): self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) def generate_image(self, prompt): response = self.client.images.generate( model="dall-e-3", prompt=prompt, size="1024x1024", quality="standard", n=1, ) image_url = response.data[0].url print(image_url) return image_url def download_image_as_blob(self, image_url): try: response = requests.get(image_url) response.raise_for_status() return response.content except requests.RequestException as e: print(f"Error downloading image: {e}") return None
[]
2024-01-10
maximus-powers/the-daily-byte
backend~llmObject.py
from langchain.chains.openai_functions import create_structured_output_chain from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ChatPromptTemplate from langchain.document_loaders import SeleniumURLLoader import datetime import os class LLMObject: def __init__(self): self.API_KEY = os.environ.get('OPENAI_API_KEY') self.llm = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.7, openai_api_key=self.API_KEY) self.current_date = datetime.date.today() # rank headlines on importance # {headlines: ursl} --> [{headline: str, url: str, importance: int}, ...] (python string) def rank_dictionary(self, headlines): prompt = ChatPromptTemplate.from_messages ( [ ("system", "You are a world-class algorithm for ranking news headlines based on their importance to US society, and filtering out videos"), ("human", "Rank the following headlines based on their relevance to a mass audience, stories that impact more people are more important. Remove key-value pairs that contain videos. Also remove any key-value pairs that are too similar to another pair (there should only be one article of any given news story/event): {input}"), ("human", "When estimating importance, stay away from stories about just one person, aim for stories that impact a large number of people. Stories about sports or entertainment are unimportant, prioritize politics, health, and world events."), ("human", "Tip: Make sure to answer in the correct format. Do not modify the values in any of the key-value pairs. Do not modify the headlines"), ] ) headlines_str = '\n'.join(headlines.keys()) chain = create_structured_output_chain(importance_json_schema, self.llm, prompt, verbose=False) result = chain.run(headlines_str) # sort headlines by importance sorted_headlines = sorted(result["headlines"], key=lambda x: x['importance'], reverse=True) # create a ranked list with optimized structure ranked_list = [] for item in sorted_headlines: ranked_list.append({'headline': item['headline'], 'url': headlines[item['headline']], 'importance': item['importance']}) return ranked_list # rewrite headlines and return JSON object def rewrite_headline(self, headline): prompt = ChatPromptTemplate.from_messages( [ ("system", "You are an AI language model. Rewrite the following headline to make them more engaging, include a pun if highly relevant, remove any publication names, and return less that 50 characters: {input}"), ("human", "Rewrite the headlines to make them more engaging. Remember to write in the context that today's date is {date}"), ] ) chain = create_structured_output_chain(headline_schema, self.llm, prompt, verbose=False) try: result = chain.run(input=headline, date=self.current_date) # print(result) rewritten_headline = result["new_headline"] # print(rewritten_headline) return rewritten_headline except Exception as e: print("Error generating rewritten headline:", e) rewritten_headline = "Failed to rewrite headline" return rewritten_headline def summarize_article(self, article_url, num_of_chars): # load the article content from the URL using Selenium loader = SeleniumURLLoader(urls=[article_url]) try: data = loader.load() except Exception as e: print(f"Error loading article: {e}") return False # check if data is empty or access was denied if not data or "Access denied" in data[0].page_content or "403" in data[0].page_content or "subscription" in data[0].page_content: return False # truncate the article text if too long article_text = data[0].page_content[:35000] if len(data[0].page_content) > 35000 else data[0].page_content # structured chain prompt for summarization prompt = ChatPromptTemplate.from_messages( [ ("system", "You are an AI language model. Summarize the following article in exactly {num_chars} characters: {input}. Make sure the summary is concise and within the character limit. Remember to write in the context that today's date is: {date}, and the article was published in the last 36 hours."), ("human", "Tip: Make sure that your summary does not exceed the character limit of {num_chars} characters."), ] ) chain = create_structured_output_chain(summary_schema, self.llm, prompt, verbose=False) try: result = chain.run(input=article_text, num_chars=num_of_chars, date=self.current_date) summary = result["summary"] print(summary) # check if the LLM indicates it cannot generate a summary if "can't summarize" in summary or "no article" in summary or "Sorry," in summary: return False else: return summary except Exception as e: print("Error generating summary:", e) return False # Process news items and return a JSON array of processed news def process_news(self, news): processed_news = [] # Initialize an empty list to store processed news for row in news: # Process only the first three items in the 'news' JSON array headline = row.get('headline', '') # Extract the 'headline' value from the current news item url = row.get('url', '') # Extract the 'url' value from the current news item new_headline_list = self.rewrite_headlines([{'headline': headline}]) # Send the headline in a list format to the rewrite_headlines function new_headline_text = new_headline_list[0]["rewritten"] if new_headline_list else '' summary = self.summarize_article(url, 750) # Create a dictionary for each news item and append it to the processed_news list news_item = { 'headline': new_headline_text, 'summary': summary["summary"], 'url': url } processed_news.append(news_item) # Return the processed news as a JSON array return processed_news # Generate a funny search term for a news headline and return it as a JSON object def gen_meme_term(self, news_headline): # print("\n\n-------------------") # print(news_headline) # print("-------------------\n\n") prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a world-class algorithm for generating image search terms related to news headlines. The search term should be a common word"), ("human", "Generate a one-word image search term related to the news headline: {headline}"), ("human", "Tip: Make sure to answer in the correct format") ] ) # Create structured output chain chain = create_structured_output_chain(meme_term_dict, self.llm, prompt, verbose=False) # Run the chain to generate a funny search term result = chain.run(headline=news_headline) term = result['gen_meme_term'] # Extract the generated funny search term and return it as a JSON object return term def generate_script(self, content): prompt = f"Your task is to create a 500 word long script for a news podcast called The Daily Byte. Find the top three most important and impactful news stories in the following dictionary. For each of those stories, summarize the article found in the URL. Output one 500 word script summarizing the top three news stories. Remember that the current date is {self.current_date}. Here is the content to summarize: {content}" script = self.llm.predict(prompt) return script ######### Image generation prompting ######### def gen_image_prompt(self, headline, subheadline): prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a genius creative AI that comes up with clever cartoon ideas for news headlines."), ("human", "Think of a cartoon idea based on the headline in the following message. If ethical, you may use elements of satire, irony, or comedy. Steer away from anything too political, this prompt needs to follow Dalle-3's rules. Make sure to capture the sentiment of the headline, with the facial expressions. Write a prompt for that image. Return just the prompt"), ("human", f"Write a Dalle-3 safe prompt about this: Headline: {headline}, Subheadline: {subheadline}"), ("human", "Tip: Make sure to answer in the correct format") ] ) # Create structured output chain chain = create_structured_output_chain(image_prompt_schema, self.llm, prompt, verbose=False) # Run the chain to generate a funny search term result = chain.run(headline=headline, subheadline=subheadline) prompt = result['image_prompt'] # Extract the generated funny search term and return it as a JSON object return prompt def gen_safer_image_prompt(self, old_img_prompt): prompt = f"Your job is to modify a prompt for an image generator to make it safer. The old prompt is {old_img_prompt}, you should modify it to make sure it's safe for all ages, not overly political, and not offensive. Return the new prompt." new_img_prompt = self.llm.predict(prompt) return new_img_prompt # Define JSON schemas for structured outputs image_prompt_schema = { "type": "object", "properties": { "image_prompt": { "type": "string", "description": "Your image prompt here" } }, "required": ["image_prompt"] } meme_term_dict = { "type": "object", "properties": { "gen_meme_term": { "type": "string", "meme_term": "Your meme search term here" } }, "required": ["gen_meme_term"] } headline_schema = { "type": "object", "properties": { "new_headline": { "type": "string", "description": "Your News Headline Here" } }, "required": ["new_headline"] } summary_schema = { "type": "object", "properties": { "summary": { "type": "string", "description": "Your summary goes here" } }, "required": ["summary"] } importance_json_schema = { "title": "Headlines", "description": "Object containing a list of headlines ranked by importance, 1 is the news headline that imacts the most people.", "type": "object", "properties": { "headlines": { "type": "array", "items": { "type": "object", "properties": { "headline": {"type": "string", "description": "The news headline"}, "url": {"type": "string", "description": "The URL of the news article"}, "importance": {"type": "integer", "description": "The importance rank of the headline"} }, "required": ["headline", "url", "importance"] } } }, "required": ["headlines"] } rewritten_headlines_schema = { "title": "RewrittenHeadlines", "description": "Object containing a list of rewritten headlines and their URLs.", "type": "object", "properties": { "headlines": { "type": "array", "items": { "type": "object", "properties": { "original": {"type": "string", "description": "The original headline"}, "rewritten": {"type": "string", "description": "The rewritten headline"}, "url": {"type": "string", "description": "The URL of the news article"} }, "required": ["original", "rewritten", "url"] } } }, "required": ["headlines"] }
[ "When estimating importance, stay away from stories about just one person, aim for stories that impact a large number of people. Stories about sports or entertainment are unimportant, prioritize politics, health, and world events.", "Tip: Make sure that your summary does not exceed the character limit of {num_chars} characters.", "Generate a one-word image search term related to the news headline: {headline}", "[('system', 'You are a world-class algorithm for ranking news headlines based on their importance to US society, and filtering out videos'), ('human', 'Rank the following headlines based on their relevance to a mass audience, stories that impact more people are more important. Remove key-value pairs that contain videos. Also remove any key-value pairs that are too similar to another pair (there should only be one article of any given news story/event): {input}'), ('human', 'When estimating importance, stay away from stories about just one person, aim for stories that impact a large number of people. Stories about sports or entertainment are unimportant, prioritize politics, health, and world events.'), ('human', 'Tip: Make sure to answer in the correct format. Do not modify the values in any of the key-value pairs. Do not modify the headlines')]", "human", "You are an AI language model. Summarize the following article in exactly {num_chars} characters: {input}. Make sure the summary is concise and within the character limit. Remember to write in the context that today's date is: {date}, and the article was published in the last 36 hours.", "Tip: Make sure to answer in the correct format. Do not modify the values in any of the key-value pairs. Do not modify the headlines", "You are a world-class algorithm for ranking news headlines based on their importance to US society, and filtering out videos", "You are a genius creative AI that comes up with clever cartoon ideas for news headlines.", "Tip: Make sure to answer in the correct format", "You are a world-class algorithm for generating image search terms related to news headlines. The search term should be a common word", "[('system', 'You are a genius creative AI that comes up with clever cartoon ideas for news headlines.'), ('human', \"Think of a cartoon idea based on the headline in the following message. If ethical, you may use elements of satire, irony, or comedy. Steer away from anything too political, this prompt needs to follow Dalle-3's rules. Make sure to capture the sentiment of the headline, with the facial expressions. Write a prompt for that image. Return just the prompt\"), ('human', 'Write a Dalle-3 safe prompt about this: Headline: PLACEHOLDER, Subheadline: PLACEHOLDER'), ('human', 'Tip: Make sure to answer in the correct format')]", "[('system', 'You are an AI language model. Rewrite the following headline to make them more engaging, include a pun if highly relevant, remove any publication names, and return less that 50 characters: {input}'), ('human', \"Rewrite the headlines to make them more engaging. Remember to write in the context that today's date is {date}\")]", "{'type': 'object', 'properties': {'image_prompt': {'type': 'string', 'description': 'Your image prompt here'}}, 'required': ['image_prompt']}", "Rank the following headlines based on their relevance to a mass audience, stories that impact more people are more important. Remove key-value pairs that contain videos. Also remove any key-value pairs that are too similar to another pair (there should only be one article of any given news story/event): {input}", "[('system', \"You are an AI language model. Summarize the following article in exactly {num_chars} characters: {input}. Make sure the summary is concise and within the character limit. Remember to write in the context that today's date is: {date}, and the article was published in the last 36 hours.\"), ('human', 'Tip: Make sure that your summary does not exceed the character limit of {num_chars} characters.')]", "Write a Dalle-3 safe prompt about this: Headline: PLACEHOLDER, Subheadline: PLACEHOLDER", "Think of a cartoon idea based on the headline in the following message. If ethical, you may use elements of satire, irony, or comedy. Steer away from anything too political, this prompt needs to follow Dalle-3's rules. Make sure to capture the sentiment of the headline, with the facial expressions. Write a prompt for that image. Return just the prompt", "You are an AI language model. Rewrite the following headline to make them more engaging, include a pun if highly relevant, remove any publication names, and return less that 50 characters: {input}", "Your job is to modify a prompt for an image generator to make it safer. The old prompt is PLACEHOLDER, you should modify it to make sure it's safe for all ages, not overly political, and not offensive. Return the new prompt.", "Rewrite the headlines to make them more engaging. Remember to write in the context that today's date is {date}", "image_prompt", "[('system', 'You are a world-class algorithm for generating image search terms related to news headlines. The search term should be a common word'), ('human', 'Generate a one-word image search term related to the news headline: {headline}'), ('human', 'Tip: Make sure to answer in the correct format')]" ]
2024-01-10
andyllegrand/gptfood
Backend~requestHandler.py
import json import openai import sqlite3 import random import requests import logging from customError import CustomError RECIPES_PER_REQUEST = 5 IMAGE_DIR = '/Users/andylegrand/xcode/gptfood/Backend/tests/images/' debug = True # if set to true the backend will not call the openai api and will instead return example responses errorCodes = json.loads(open('errorCodes.json', 'r').read()) def genRecipesApiCall(ingredients, usedRecipes, proomptPath='proomps/genRecipeList.txt'): """ Calls openai api to generate recipes given ingredients. @param ingredients: list of ingredients representing available ingredients @param usedRecipes: list of recipes representing recipes that have already been used @raise CustomError: if the response from the api is not valid json @return: extracted text from response """ # Form list of ingredients in string form ingredient_string = '' for ingredient in ingredients: ingredient_string += ingredient + '\n' # Form list of used recipes in string form used_recipe_string = '' for recipe in usedRecipes: used_recipe_string += '{' + recipe + '}\n' # Form proompt proompt = open(proomptPath, 'r').read() proompt = proompt.replace('[ingredients]', ingredient_string) proompt = proompt.replace('[used]', used_recipe_string) # Call openai api openai.api_key = open('/Users/andylegrand/xcode/gptfood/Backend/key.txt', 'r').read() logging.debug("key: " + openai.api_key) try: response = openai.Completion.create( model="text-davinci-003", prompt=proompt, temperature=1, max_tokens=1024, top_p=1, frequency_penalty=0, presence_penalty=0 ) assert response.choices[0].text != None logging.debug(f"APIResponse: {response.choices[0].text}") return response.choices[0].text except: raise CustomError(proompt, errorCodes["GPT_API_ERROR"]) def addRecipeToDatabase(recipe, ingredients, connection): """ Adds a list of recipes to the database. @param recipes: string representing the recipe @param ingredients: list of strings representing the ingredients @param connection: connection to the database @return: None """ cursor = connection.cursor() # Add the recipe to the database cursor.execute(""" INSERT INTO recipes (name, directions, imagePath) VALUES (?, NULL, NULL); """, (recipe,)) recipeId = cursor.lastrowid # Add the ingredients to the database ingredientIds = [] for ingredient in ingredients: cursor.execute(""" INSERT OR IGNORE INTO ingredients (name) VALUES (?); """, (ingredient,)) ingredientIds.append(cursor.lastrowid) # Add the relations to the database for ingredientId in ingredientIds: cursor.execute(""" INSERT INTO relations (recipe_id, ingredient_id) VALUES (?, ?); """, (recipeId, ingredientId)) connection.commit() def generateAndAddRecipes(ingredients, usedRecipes, connection): """ Generates recipes and adds them to the database. @param ingredients: list of ingredients representing available ingredients @param usedRecipes: list of recipes representing recipes that have already been used @param connection: connection to the database @raise CustomError: if the response from the api is not valid json @return: None """ completionText = None if not debug: completionText = genRecipesApiCall(ingredients, usedRecipes) else: completionText = open('sampleresponse.txt', 'r').read() # Load the text as JSON, abort and throw an error if it fails try: recipes = json.loads(completionText) except: raise CustomError(f"Error parsing JSON: {completionText}", errorCodes["JSON_PARSE_ERROR"]) for recipe in recipes: addRecipeToDatabase(recipe["name"], recipe["ingredients"], connection) def queryDatabaseRecipes(ingredients, usedRecipes, connection): cursor = connection.cursor() # Fetch all recipe names from the database, then randomize the order cursor.execute('SELECT name FROM recipes') all_recipes = [row[0] for row in cursor.fetchall()] random.shuffle(all_recipes) # Fetch corresponding ingredient ids from the database placeholders = ', '.join('?' for ingredient in ingredients) cursor.execute(f"SELECT id FROM ingredients WHERE name IN ({placeholders})", ingredients) ingredient_ids = set(row[0] for row in cursor.fetchall()) # Find the recipes whose ingredients are all in the provided list matching_recipes = [] for recipe in all_recipes: if recipe in usedRecipes: continue # Fetch the ingredients for this recipe from relations cursor.execute(''' SELECT ingredient_id FROM relations JOIN recipes ON relations.recipe_id = recipes.id WHERE recipes.name = ? ''', (recipe,)) recipe_ingredient_ids = set(row[0] for row in cursor.fetchall()) # Loop through the ingredients and check if they are all in the provided list. If so, add the recipe to matching_recipes if recipe_ingredient_ids.issubset(ingredient_ids): matching_recipes.append(recipe) if len(matching_recipes) == RECIPES_PER_REQUEST: break return matching_recipes def getRecipes(ingredients, usedRecipes, databasePath): if debug: return ["Recipe 1", "Recipe 2", "Recipe 3", "Recipe 4", "Recipe 5"] # Connect to the database conn = sqlite3.connect(databasePath) # Query database. If there are not enough recipes to fufill the request generate more and try again recipes = queryDatabaseRecipes(ingredients, usedRecipes, conn) if len(recipes) < RECIPES_PER_REQUEST: generateAndAddRecipes(ingredients, usedRecipes, conn) recipes = queryDatabaseRecipes(ingredients, usedRecipes, conn) conn.close() return recipes def genDirectionsApiCall(recipe, ingredients): """ Calls openai api to generate directions given a recipe. @param recipe: string representing the recipe @return: text response from openai """ # Form list of ingredients in string form ingredient_string = '' for ingredient in ingredients: ingredient_string += ingredient + '\n' # Form proompt proompt = open('proomps/genDirections.txt', 'r').read() proompt = proompt.replace('[recipe]', recipe) proompt = proompt.replace('[ingredients]', ingredient_string) # Call openai api openai.api_key = open('key.txt', 'r').read() response = openai.Completion.create( model="text-davinci-003", prompt=proompt, temperature=1, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].text def downloadImage(url, path): """ Downloads an image from a url and saves it to a path. @param url: url of the image @param path: path to save the image to @return: None """ response = requests.get(url, stream=True) if response.status_code == 200: with open(path, 'wb') as f: f.write(response.content) def genImageApiCall(description): """ Calls openai api to generate an image given a description. @param description: string representing the description @return: url of the generated image """ openai.api_key = open('key.txt', 'r').read() response = openai.Image.create( prompt=description, n=1, size="256x256" ) image_url = response['data'][0]['url'] return image_url def addDirectionsToDatabase(recipe, directions, imagePath, connection): """ Adds directions and image path to the database. @param recipe: string representing the recipe @param directions: string representing the directions @param imagePath: string representing the path to the image @param connection: connection to the database @return: None """ cursor = connection.cursor() # Add directions and image path to database cursor.execute(""" UPDATE recipes SET directions = ?, imagePath = ? WHERE name = ?; """, (directions, imagePath, recipe)) connection.commit() def generateAndAddDirections(recipe, connection, imagePath): """ Generates directions and adds them to the database. @param recipe: string representing the recipe @param connection: connection to the database @return: None """ # Get ingredients from database cursor = connection.cursor() # get recipe id cursor.execute('SELECT id FROM recipes WHERE name = ?', (recipe,)) if cursor.fetchone() == None: raise CustomError(f"Recipe {recipe} not found in database", errorCodes["RECIPE_NOT_FOUND"]) # get ingredients cursor.execute(''' SELECT ingredients.name FROM ingredients JOIN relations ON ingredients.id = relations.ingredient_id JOIN recipes ON relations.recipe_id = recipes.id WHERE recipes.name = ? ''', (recipe,)) ingredients = [row[0] for row in cursor.fetchall()] logging.debug(f"Ingredients: {ingredients}") # Add the directions to the database res = genDirectionsApiCall(recipe, ingredients) # Convert to json, extract directions and image proompt. Abort and throw an error if it fails try: js = json.loads(res) directions = js["directions"] imageProompt = js["dall-e prompt"] except: raise CustomError(f"Error parsing JSON: {res}", errorCodes["JSON_PARSE_ERROR"]) # Generate image imageUrl = genImageApiCall(imageProompt) imagePath = imagePath + recipe + '.png' downloadImage(imageUrl, imagePath) # Add directions and image path to database addDirectionsToDatabase(recipe, directions, imagePath, connection) def getDirections(recipe, databasePath): """ Query database. If the current recipe does not have directions generate them and try again @param recipe: string representing the recipe @param databasePath: path to the database @return: directions for the recipe """ if debug: return "Directions for " + recipe # Connect to the database conn = sqlite3.connect(databasePath) cursor = conn.cursor() # Check if recipe has directions cursor.execute('SELECT directions FROM recipes WHERE name = ?', (recipe,)) directions = cursor.fetchone()[0] if directions == None: generateAndAddDirections(recipe, conn, IMAGE_DIR) cursor.execute('SELECT directions FROM recipes WHERE name = ?', (recipe,)) directions = cursor.fetchone()[0] conn.close() return directions def getImage(recipe, databasePath): """ Returns path to the image for the recipe. This function should be called after getDirections, so the image should already be generated. @param recipe: string representing the recipe @param databasePath: path to the database @return: path to the image """ if debug: return "/Users/andylegrand/xcode/gptfood/Backend/exampleResponses/exampleImage.png" # Connect to the database conn = sqlite3.connect(databasePath) cursor = conn.cursor() # Check if recipe has directions cursor.execute('SELECT imagePath FROM recipes WHERE name = ?', (recipe,)) imagePath = cursor.fetchone()[0] if imagePath == None: raise CustomError(f"Image for {recipe} not found in database", errorCodes["IMAGE_NOT_FOUND"]) conn.close() return imagePath
[]
2024-01-10
ShaBaoFa/xiaogpt
xiaogpt~bot~chatgptapi_bot.py
import openai from xiaogpt.bot.base_bot import BaseBot class ChatGPTBot(BaseBot): def __init__(self, session, openai_key, api_base=None): self.session = session self.history = [] self.api_base = api_base self.openai_key = openai_key async def ask(self, query): openai.api_key = self.openai_key if self.api_base: openai.api_base = self.api_base ms = [] for h in self.history: ms.append({"role": "user", "content": h[0]}) ms.append({"role": "assistant", "content": h[1]}) ms.append({"role": "user", "content": f"{query}"}) completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=ms) message = ( completion["choices"][0] .get("message") .get("content") .encode("utf8") .decode() ) self.history.append([f"{query}", message]) # only keep 5 history self.history = self.history[-5:] return message
[]
2024-01-10
ShaBaoFa/xiaogpt
xiaogpt~bot~gpt3_bot.py
from xiaogpt.config import OPENAI_API_KEY class GPT3Bot: def __init__(self, session, openai_key): self.openai_key = openai_key self.api_url = "https://api.openai.com/v1/completions" self.headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.openai_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): self.data["prompt"] = query r = await self.session.post(self.api_url, headers=self.headers, json=self.data) print(1111) return await r.json()
[]
2024-01-10
nmd2k/vi-medicine
app~business~suggestion.py
import sys sys.path.append(["../"]) from pathlib import Path from typing import Any, List, Dict import random import json from concurrent.futures import ThreadPoolExecutor, as_completed from langchain.prompts import PromptTemplate from langchain.schema.messages import HumanMessage, SystemMessage from langchain.embeddings.openai import OpenAIEmbeddings from pymilvus import ( connections, Collection, ) from utils.prompts import * from utils.chatmodel import ChatModel from app.exception.custom_exception import CustomException import re def parser_result(string): matches = re.search(r'\{([^}]*)\}', string) return matches.group() class MedicineAgent: def __init__(self, top_p: float = 1, max_tokens: int = 512, temperature: float = 0, n_retry: int = 2, request_timeout: int = 30, **kwargs) -> None: connections.connect("default", host="localhost", port="19530") self.chatmodel = ChatModel( temperature=temperature, max_tokens=max_tokens, top_p=top_p, n_retry=n_retry, request_timeout=request_timeout, **kwargs ) self.illness_collect = Collection("illness") self.drug_collect = Collection("drug") self.usage_collect = Collection("drug_usage") for collect in [self.illness_collect, self.drug_collect, self.usage_collect]: collect.load() def generate(self, message, prompt=None): """ Chat model generate function Args: - message (str): human query/message - prompt (str): optional system message Return: - str: Generated output """ # print(50*"=", prompt) # print(50*"=", message) try: messages = [] if prompt: messages.append(SystemMessage(content=prompt)) messages.append(HumanMessage(content=message)) generate = self.chatmodel.generate(messages) return generate except Exception as exc: raise CustomException(exc) def embed(self, message): """ Embedding string input Args: - message (str): message to embed Return: - List: List of embedding output """ try: assert type(message) == str embed = self.chatmodel.embed(message=message) return embed except Exception as exc: raise CustomException(exc) def _search(self, query: str, collection: Collection, output_fields: List, anns_field: str = "embeddings", top_k=5, offset=5, nprobe=1) -> str: query = self.embed(query) search_params = { "metric_type": "COSINE", "offset": offset, "ignore_growing": False, "params": {"nprobe": nprobe} } results = collection.search( data=[query], anns_field=anns_field, param=search_params, limit=top_k, output_fields=output_fields, expr=None, consistency_level="Strong" ) doc = "" for result in results[0]: for field in output_fields: if field == "url": doc += "Nguồn: " doc += result.entity.get(field) + "\n" doc += "\n" return doc def _query(self, field, value, collection, output_fields: List, top_k=10): results = collection.query( expr=f"{field} == '{value}'", limit=top_k, output_fields=output_fields, ) doc = "" for result in results: for field in output_fields: if field == "url": doc += "Nguồn: " doc += result.entity.get(field) + "\n" doc += "\n" return doc async def diagnose(self, symptoms: str): # 1. query disease from diagnose_input # 2. get disease info + user info -> openai generate to concluse disease # 3. parse the result diagnose_input = symptoms.symptom user_info = symptoms.user_info doc = self._search( diagnose_input, collection=self.illness_collect, output_fields=["title", "diagnosis", "summary", "url"], top_k=5, ) message = DIAGNOSE_TEMPLATE.format(disease_info=doc, symptom=diagnose_input) response = self.generate(message, DIAGNOSE_PROMPT) return json.loads(response) async def check_medicine(self, medicine, disease): # 1. query disease's treatment (drugs) # 2. query medicine usage # 3. send to openai to compare 1. and 2. # disease_doc = self._search( # disease.name, # collection=self.illness_collect, # output_fields=["title", "treatment", "overview"], # top_k=1, # ) # medicine_doc = self._search( # medicine.name, # collection=self.drug_collect, # output_fields=["name", "uses", "warning",], # top_k=2, # ) disease_doc = self._query( field="title", value=disease.name, collection=self.illness_collect, output_fields=["title", "treatment", "overview"], top_k=1 ) medicine_doc = self._query( field="name", value=medicine.name, collection=self.drug_collect, output_fields=["name", "uses", "warning"], top_k=1 ) usage_doc = self._search( medicine.name, collection=self.usage_collect, output_fields=["title", "description"], top_k=3, ) message = CHECK_MEDICINE_TEMPLATE.format( disease_doc=disease_doc, drug_doc=medicine_doc + "\n" + usage_doc, drug=medicine.name, disease=disease.name ) response = self.generate(message, CHECK_MEDICINE_PROMPT) return json.loads(response) async def suggest_medicine(self, disease, listed = None): # 1. query disease's treatment (drugs) # 2. send to openai to concluse disease_doc = self._search( disease.name, collection=self.illness_collect, output_fields=["title", "treatment"], top_k=3, ) # if listed: # exclude = " ".join(listed) message = SUGGEST_MEDICINE_TEMPLATE.format(disease_info=disease_doc, disease=disease.name) # result = random.choice(["Paracetamol", "Quinine"]) # explain = random.choice(["Paracetamol phù hợp để điều trị bệnh, xét với thể trạng bệnh nhân và triệu chứng đang gặp phải.", "Nước tiểu chuột không phù hợp với bệnh nhân. Đây là một chất có hại và không nên sử dụng.", "Một lời cầu nguyện cần xem xét thêm. Vì mặc dù không có vấn đề gì, nhưng bác sĩ nên xem xét lại thuốc này."]) response = self.generate(message, SUGGEST_MEDICINE_PROMPT) response = json.loads(response) return response # drug_name = response["suggestion"] # drug_name = self._search( # drug_name, # collection=self.drug_collect, # output_fields=["name"], # top_k=1, # ).strip() # return dict(suggestion=disease_doc, explain=response["explain"]) async def compatible_calculator(self, medicines: List): # send multi thread # 1. retrieve top_k drugs usage -> causion, bla bla # 2. send to openai to compare args = [] for i in range(len(medicines) - 1): for j in range(i + 1, len(medicines)): args.append(dict(drug1=medicines[i].name, drug2=medicines[j].name)) results = [] with ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(self._check_two_drugs, **_args) for _args in args] for future in as_completed(futures): if future.result(): results.append(future.result()) return results def _check_two_drugs(self, drug1, drug2): print(drug1, drug2) drug1_doc = self._search( drug1, collection=self.drug_collect, output_fields=["uses", "caution", "warning"], top_k=1, ) drug2_doc = self._search( drug2, collection=self.drug_collect, output_fields=["uses", "caution", "warning"], top_k=1, ) message = COMPATIBLE_TEMPLATE.format( drug_info=drug1_doc+"\n"+drug2_doc, drug1=drug1, drug2=drug2 ) try: response = self.generate(message, COMPATIBLE_PROMPT) response = json.loads(response) response["source"] = drug1 response["target"] = drug2 except Exception: return return response
[]
2024-01-10
nmd2k/vi-medicine
app~business~functions.py
import sys sys.path.append(["../"]) from pathlib import Path from typing import Any, List, Dict from langchain.schema import HumanMessage, SystemMessage from utils.prompts import * from utils.chatmodel import ChatModel from app.exception.custom_exception import CustomException class Functions: "Simple generate function and embedding functions" def __init__(self, top_p: float = 1, max_tokens: int = 512, temperature: float = 0, n_retry: int = 2, request_timeout: int = 30, **kwargs) -> None: self.chatmodel = ChatModel( temperature=temperature, max_tokens=max_tokens, top_p=top_p, n_retry=n_retry, request_timeout=request_timeout, **kwargs ) async def generate(self, message, prompt=None): """ Chat model generate function Args: - message (str): human query/message - prompt (str): optional system message Return: - str: Generated output """ try: messages = [] if prompt: messages.append(SystemMessage(content=prompt)) messages.append(HumanMessage(content=message)) generate = self.chatmodel.generate(messages) return generate except Exception as exc: raise CustomException(exc) async def embed(self, message): """ Embedding string input Args: - message (str): message to embed Return: - List: List of embedding output """ try: assert type(message) == str embed = self.chatmodel.embed(message=message) print(len(embed)) return embed except Exception as exc: raise CustomException(exc)
[]
2024-01-10
nmd2k/vi-medicine
utils~chatmodel.py
import os import sys import openai import requests import json from typing import Dict, Any from langchain.chat_models import AzureChatOpenAI from langchain.embeddings.openai import OpenAIEmbeddings from langchain.callbacks import StreamingStdOutCallbackHandler from utils.utils import latency_benchmark from config.OAIConfig import API_BASE, \ API_VERSION, \ MODEL_DEPLOYMENT, \ OPENAI_API_KEY DUMMY_PROMPT = ("You are useful assistant. " "Follow instruction and answer user question""") OPENAI_AVAILABLE = True try: import openai openai.api_type = "azure" openai.api_key = OPENAI_API_KEY openai.api_base = API_BASE openai.api_version = API_VERSION except ModuleNotFoundError: OPENAI_AVAILABLE = False class ChatModel: def __init__(self, chatmodel: str = "openai", temperature: int = 0, stream: bool = False, frequency_penalty: float = 0.2, max_tokens: int = 2000, top_p: float = 1.0, presence_penalty: float = 1.0, stop_sequences: str = None, n_retry: int = 3, request_timeout = 60, verbose: bool = True) -> None: '''Setup chat model ''' self.top_p = top_p self.stream = stream self.max_tokens = max_tokens self.temperature = temperature self.frequency_penalty = frequency_penalty self.presence_penalty = presence_penalty self.stop_sequences = stop_sequences self.verbose = verbose self.request_timeout = request_timeout self.max_retries = n_retry if chatmodel == "openai": assert OPENAI_AVAILABLE, "Missing openai module, try `pip install openai`" self.generative_model = AzureChatOpenAI( openai_api_base = API_BASE, openai_api_version = API_VERSION, openai_api_key = OPENAI_API_KEY, deployment_name = MODEL_DEPLOYMENT, max_retries = self.max_retries, request_timeout = self.request_timeout, streaming = self.stream, max_tokens = self.max_tokens, temperature = self.temperature, model_kwargs = { "top_p": self.top_p, "stop" : self.stop_sequences, "frequency_penalty": self.frequency_penalty, "presence_penalty": self.presence_penalty } ) self.embedding_model = OpenAIEmbeddings( openai_api_type="azure", openai_api_base=API_BASE, openai_api_key=OPENAI_API_KEY, model=MODEL_DEPLOYMENT, ) self.generate_function = self._openai_generate_function self.embedding_function = self._openai_embedding_function else: raise NotImplemented("Not implemented other model caller") self.generate_function = self._llama_generate_function def _openai_generate_function(self, message): if self.stream: response = self.generative_model( messages=message, callbacks=[StreamingStdOutCallbackHandler()]) else: response = self.generative_model(messages=message) return response def _openai_embedding_function(self, message): query_result = self.embedding_model.embed_query(message) return query_result def _llama_generate_function(self, message): # chat = requests.request( # "POST", os.getenv["LLAMA2_API_SERVICE"], # headers={'Content-Type': 'application/json'}, # data=message # ) # reply = json.loads(chat.text)['generation']['content'] raise NotImplemented("Method not implemented") @latency_benchmark def embed(self, message): '''Request embedding model to embed given query''' embed = self.embedding_function(message=message) return embed @latency_benchmark def generate(self, message) -> str: '''Request Chatbot model to generate answer Chatbot using in demo is gpt-3.5-turbo from Openai''' try: response = self.generate_function(message=message) reply = response.content # reply = response.content # reply = response["choices"][0]["message"]["content"] if self.verbose: sys.stdout.write(reply) sys.stdout.flush() except openai.error.Timeout as e: raise Exception(f"OpenAI API request timed out: {e}. Please retry") except openai.error.APIError as e: raise Exception(f"OpenAI API returned an API Error: {e}. Please retry") except openai.error.APIConnectionError as e: raise Exception(f"OpenAI API request failed to connect: {e}. Please retry") except openai.error.InvalidRequestError as e: raise Exception(f"OpenAI API request was invalid: {e}. Please retry") except openai.error.AuthenticationError as e: raise Exception(f"OpenAI API request was not authorized: {e}. Please retry") except openai.error.PermissionError as e: raise Exception(f"OpenAI API request was not permitted: {e}. Please retry") except openai.error.RateLimitError as e: raise Exception(f"OpenAI API request exceeded rate limit: {e}. Please retry") return reply
[ "You are useful assistant. Follow instruction and answer user question" ]
2024-01-10
crizCraig/learnmax
learn_max~dvq~model~loss.py
""" VQVAE losses, used for the reconstruction term in the ELBO """ import math import torch # ----------------------------------------------------------------------------- class LogitLaplace: """ the Logit Laplace distribution log likelihood from OpenAI's DALL-E paper """ logit_laplace_eps = 0.1 class InMap: def __call__(self, x): # map [0,1] range to [eps, 1-eps] return (1 - 2 * LogitLaplace.logit_laplace_eps) * x + LogitLaplace.logit_laplace_eps @classmethod def unmap(cls, x): # inverse map, from [eps, 1-eps] to [0,1], with clamping return torch.clamp((x - cls.logit_laplace_eps) / (1 - 2 * cls.logit_laplace_eps), 0, 1) @classmethod def nll(cls, x, mu_logb): raise NotImplementedError # coming right up class Normal: """ simple normal distribution with fixed variance, as used by DeepMind in their VQVAE note that DeepMind's reconstruction loss (I think incorrectly?) misses a factor of 2, which I have added to the normalizer of the reconstruction loss in nll(), we'll report number that is half of what we expect in their jupyter notebook """ data_variance = 0.06327039811675479 # cifar-10 data variance, from deepmind sonnet code # data_variance = 0.000006327039811675479 # cifar-10 data variance, from deepmind sonnet code # Currently we get distinct images flattening to single token with CFAR. Changing the # data variance gets us _more_ distinct images. This has the effect of increasing the reconstruction # importance in the loss. So it makes sense the reconstructions get better. But what is the # effect on the quantization? It means the distance to the cluster center is bigger perhaps. # Turns out that reconstruction and latent loss get far worse with the data variance reduced so much. # SO, we may try perceptual loss re the Learning Neural Representations DeepMind paper. # This will involve training a patch based dvq with the original setup. Then we can hopefully # get the same types of reconstructions in the single token regime. # So this wasn't needed. We just needed more points per cluster in the k-means init mean = 0.5 class InMap: def __call__(self, x): # these will put numbers into range [-0.5, 0.5], # as used by DeepMind in their sonnet VQVAE example return x - Normal.mean # map [0,1] range to [-0.5, 0.5] @classmethod def unmap(cls, x): return torch.clamp(x + Normal.mean, 0, 1) @classmethod def nll(cls, x, mu): # TODO: Try perceptual loss with RL bot. We cube here as Zuma has a mostly static background so outliers are important. # return abs(((x - mu)**2).mean() / (2 * cls.data_variance)) #+ math.log(math.sqrt(2 * math.pi * cls.data_variance)) # Cubing did not work, made image green! return ((x - mu)**2).mean() / (2 * cls.data_variance) #+ math.log(math.sqrt(2 * math.pi * cls.data_variance))
[]
2024-01-10
ShaoXiangChien/STI
main_functions.py
import streamlit as st import json import arrow from datetime import datetime as dt from bertopic import BERTopic from hdbscan import HDBSCAN from umap import UMAP from pprint import pprint def keyword_extract(method, df): if method == "tfidf": import keyword_extraction.tfidf_kw_extract as kw keywords = kw.tfidf_kw_extract(df) elif method == "textrank": import keyword_extraction.textrank_kw_extract as kw keywords = kw.textrank_kw_extract(df) elif method == "azure language service": import keyword_extraction.azure_kw_extract as kw keywords = kw.azure_kw_extract(df) elif method == "ckip": import keyword_extraction.ckip_kw_extract as kw keywords = kw.ckip_kw_extract(df) elif method == "ckip_tfidf": import keyword_extraction.ckip_tfidf_kw_extract as kw keywords = kw.ckip_tfidf_kw_extract(df) else: import keyword_extraction.openai_kw_extract as kw keywords = kw.openai_kw_extract(df) return keywords def summarize(method, df): st.write("Initializing") summary = "" if method == "naive": import summarization.naive_summarize as sm sentences = cut_sentences("".join(df['full_text'].to_list())) tokenized_sentences = cut_sentences( " ".join(df['full_text_tokens'].to_list())) summary = sm.naive_summarize(sentences, tokenized_sentences) elif method == "kmeans": import summarization.kmeans_summarize as sm sentences = cut_sentences("".join(random.choices( df['full_text_tokens'].to_list(), k=int(len(df['full_text_tokens'].to_list()) * 0.3)))) summary = sm.kmeans_summarize(sentences) elif method == "textrank": import summarization.textrank_summarize as sm sentences = cut_sentences("".join(df['full_text_tokens'].to_list())) summary = sm.textrank_summarize(sentences) elif method == "openai": import openai_services as sm docs = "".join(random.choices( df['full_text'].to_list(), k=10))[:1500] summary = sm.summarize(docs) elif method == "azure_language_service": import summarization.azure_summarize as sm docs = "".join(df['full_text'].to_list())[:100000] summary = sm.azure_summarize([docs]) return summary def cut_sentences(content): end_flag = ['?', '!', '?', '!', '。', '…'] content_len = len(content) sentences = [] tmp_char = '' for idx, char in enumerate(content): tmp_char += char if (idx + 1) == content_len: sentences.append(tmp_char) break if char in end_flag: next_idx = idx + 1 if not content[next_idx] in end_flag: sentences.append(tmp_char) tmp_char = '' return sentences def choose_timeline_rp(df, kws): n = df['cluster'].max() timeline_rp = {i: "" for i in range(n)} for i in range(n): candidate = df[df['cluster'] == i].copy() max_score = 0 target = "" for doc in candidate['Event']: score = 0 for w in kws[i]: if w in doc: score += 1 if score > max_score: max_score = score target = doc timeline_rp[i] = target return timeline_rp def generate_timeline(time_df, anomalies): anomaly_ft = time_df.timestamp.apply(lambda x: x in anomalies) anomaly_df = time_df[anomaly_ft].copy() anomaly_df.reset_index(inplace=True) anomaly_df.drop("index", axis=1, inplace=True) full_text = "".join(anomaly_df.Event.to_list()) freq_table = {kw: full_text.count(kw) for kw in st.session_state['keywords']} sentence_score = {} for sent in anomaly_df.Event: score = 0 for k, v in freq_table.items(): if k in sent: score += v sentence_score[sent] = score / len(sent) timeline = {timestamp: [] for timestamp in anomalies} for idx, row in anomaly_df.iterrows(): event = row['Event'] if row['timestamp'] in anomalies: timeline[row['timestamp']].append(event) for k, v in timeline.items(): tmp = sorted(v, key=lambda x: sentence_score.get( x) if sentence_score.get(x) else 0, reverse=True) timeline[k] = tmp[0] with open(f"./Experiments/{st.session_state['event']}/timeline.json", "w") as fh: json.dump(timeline, fh) data = { "events": [] } for k, v in timeline.items(): time_obj = dt.strptime(k, "%Y-%m-%dT%H:%M:%SZ") date = { "year": time_obj.year, "month": time_obj.month, "day": time_obj.day, } text = { "text": v } data['events'].append({ "start_date": date, "text": text }) return data def generate_timeline_beta(time_df, df, anomalies): anomaly_ft = time_df.timestamp.apply(lambda x: x in anomalies) anomaly_df = time_df[anomaly_ft].copy() anomaly_df.reset_index(inplace=True) anomaly_df.drop("index", axis=1, inplace=True) docs = anomaly_df['Event'].to_list() umap_model = UMAP(n_neighbors=5, n_components=20, min_dist=0.1, metric='cosine') topic_model = BERTopic(language='multilingual', umap_model=umap_model) topics, probs = topic_model.fit_transform(docs) df['topic'] = topics representative_list = [] for i, event in topic_model.get_representative_docs().items(): representative_list.append(event[0]) final_topics = pd.DataFrame({ "Time": [], "Event": [] }) for event in representative_list: final_topics = final_topics.append({ "Time": df[df["Event"] == event].iat[0, 0], "Event": event }, ignore_index=True) final_topics["Time"] = final_topics["Time"].astype('string') for idx, row in final_topics.iterrows(): final_topics.loc[idx, "Time"] = final_topics.loc[idx, "Time"].replace('年', '/') final_topics.loc[idx, "Time"] = final_topics.loc[idx, "Time"].replace('月', '/') final_topics.loc[idx, "Time"] = final_topics.loc[idx, "Time"].replace('日', '') final_topics.loc[idx, "Time"] = final_topics.loc[idx, "Time"].replace('號', '') Time = arrow.get(final_topics.loc[idx, "Time"]) final_topics.loc[idx, "Time"] = Time.format("YYYY-MM-DD") data = { "events": [] } for idx, row in final_topics.items(): time_obj = row['Time'] date = { "year": time_obj.year, "month": time_obj.month, "day": time_obj.day, } text = { "text": row['Event'] } data['events'].append({ "start_date": date, "text": text }) return final_topics
[]
2024-01-10
HuzeyfeAyaz/contextualized-topic-models
contextualized_topic_models~evaluation~measures.py
from gensim.corpora.dictionary import Dictionary from gensim.models.coherencemodel import CoherenceModel from gensim.models import KeyedVectors import gensim.downloader as api from scipy.spatial.distance import cosine import abc from contextualized_topic_models.evaluation.rbo import rbo import numpy as np import itertools class Measure: def __init__(self): pass def score(self): pass class TopicDiversity(Measure): def __init__(self, topics): super().__init__() self.topics = topics def score(self, topk=25): """ :param topk: topk words on which the topic diversity will be computed :return: """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: unique_words = set() for t in self.topics: unique_words = unique_words.union(set(t[:topk])) td = len(unique_words) / (topk * len(self.topics)) return td class Coherence(abc.ABC): """ :param topics: a list of lists of the top-k words :param texts: (list of lists of strings) represents the corpus on which the empirical frequencies of words are computed """ def __init__(self, topics, texts): self.topics = topics self.texts = texts self.dictionary = Dictionary(self.texts) @abc.abstractmethod def score(self): pass class CoherenceNPMI(Coherence): def __init__(self, topics, texts): super().__init__(topics, texts) def score(self, topk=10, per_topic=False): """ :param topk: how many most likely words to consider in the evaluation :param per_topic: if True, returns the coherence value for each topic (default: False) :return: NPMI coherence """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: npmi = CoherenceModel(topics=self.topics, texts=self.texts, dictionary=self.dictionary, coherence='c_npmi', topn=topk) if per_topic: return npmi.get_coherence_per_topic() else: return npmi.get_coherence() class CoherenceUMASS(Coherence): def __init__(self, topics, texts): super().__init__(topics, texts) def score(self, topk=10, per_topic=False): """ :param topk: how many most likely words to consider in the evaluation :param per_topic: if True, returns the coherence value for each topic (default: False) :return: UMass coherence """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: umass = CoherenceModel(topics=self.topics, texts=self.texts, dictionary=self.dictionary, coherence='u_mass', topn=topk) if per_topic: return umass.get_coherence_per_topic() else: return umass.get_coherence() class CoherenceUCI(Coherence): def __init__(self, topics, texts): super().__init__(topics, texts) def score(self, topk=10, per_topic=False): """ :param topk: how many most likely words to consider in the evaluation :param per_topic: if True, returns the coherence value for each topic (default: False) :return: UCI coherence """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: uci = CoherenceModel(topics=self.topics, texts=self.texts, dictionary=self.dictionary, coherence='c_uci', topn=topk) if per_topic: return uci.get_coherence_per_topic() else: return uci.get_coherence() class CoherenceCV(Coherence): def __init__(self, topics, texts): super().__init__(topics, texts) def score(self, topk=10, per_topic=False): """ :param topk: how many most likely words to consider in the evaluation :param per_topic: if True, returns the coherence value for each topic (default: False) :return: C_V coherence """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: cv = CoherenceModel(topics=self.topics, texts=self.texts, dictionary=self.dictionary, coherence='c_v', topn=topk) if per_topic: return cv.get_coherence_per_topic() else: return cv.get_coherence() class CoherenceWordEmbeddings(Measure): def __init__(self, topics, word2vec_path=None, binary=False): """ :param topics: a list of lists of the top-n most likely words :param word2vec_path: if word2vec_file is specified, it retrieves the word embeddings file (in word2vec format) to compute similarities between words, otherwise 'word2vec-google-news-300' is downloaded :param binary: if the word2vec file is binary """ super().__init__() self.topics = topics self.binary = binary if word2vec_path is None: self.wv = api.load('word2vec-google-news-300') else: self.wv = KeyedVectors.load_word2vec_format(word2vec_path, binary=binary) def score(self, topk=10, binary= False): """ :param topk: how many most likely words to consider in the evaluation :return: topic coherence computed on the word embeddings similarities """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: arrays = [] for index, topic in enumerate(self.topics): if len(topic) > 0: local_simi = [] for word1, word2 in itertools.combinations(topic[0:topk], 2): if word1 in self.wv.vocab and word2 in self.wv.vocab: local_simi.append(self.wv.similarity(word1, word2)) arrays.append(np.mean(local_simi)) return np.mean(arrays) class InvertedRBO(Measure): def __init__(self, topics): """ :param topics: a list of lists of words """ super().__init__() self.topics = topics def score(self, topk = 10, weight=0.9): """ :param weight: p (float), default 1.0: Weight of each agreement at depth d: p**(d-1). When set to 1.0, there is no weight, the rbo returns to average overlap. :return: rank_biased_overlap over the topics """ if topk > len(self.topics[0]): raise Exception('Words in topics are less than topk') else: collect = [] for list1, list2 in itertools.combinations(self.topics, 2): rbo_val = rbo.rbo(list1[:topk], list2[:topk], p=weight)[2] collect.append(rbo_val) return 1 - np.mean(collect) class Matches(Measure): def __init__(self, doc_distribution_original_language, doc_distribution_unseen_language): """ :param doc_distribution_original_language: numpy array of the topical distribution of the documents in the original language (dim: num docs x num topics) :param doc_distribution_unseen_language: numpy array of the topical distribution of the documents in an unseen language (dim: num docs x num topics) """ super().__init__() self.orig_lang_docs = doc_distribution_original_language self.unseen_lang_docs = doc_distribution_unseen_language if len(self.orig_lang_docs) != len(self.unseen_lang_docs): raise Exception('Distributions of the comparable documents must have the same length') def score(self): """ :return: proportion of matches between the predicted topic in the original language and the predicted topic in the unseen language of the document distributions """ matches = 0 for d1, d2 in zip(self.orig_lang_docs, self.unseen_lang_docs): if np.argmax(d1) == np.argmax(d2): matches = matches + 1 return matches/len(self.unseen_lang_docs) class KLDivergence(Measure): def __init__(self, doc_distribution_original_language, doc_distribution_unseen_language): """ :param doc_distribution_original_language: numpy array of the topical distribution of the documents in the original language (dim: num docs x num topics) :param doc_distribution_unseen_language: numpy array of the topical distribution of the documents in an unseen language (dim: num docs x num topics) """ super().__init__() self.orig_lang_docs = doc_distribution_original_language self.unseen_lang_docs = doc_distribution_unseen_language if len(self.orig_lang_docs) != len(self.unseen_lang_docs): raise Exception('Distributions of the comparable documents must have the same length') def score(self): """ :return: average kullback leibler divergence between the distributions """ kl_mean = 0 for d1, d2 in zip(self.orig_lang_docs, self.unseen_lang_docs): kl_mean = kl_mean + kl_div(d1, d2) return kl_mean/len(self.unseen_lang_docs) def kl_div(a, b): a = np.asarray(a, dtype=np.float) b = np.asarray(b, dtype=np.float) return np.sum(np.where(a != 0, a * np.log(a / b), 0)) class CentroidDistance(Measure): def __init__(self, doc_distribution_original_language, doc_distribution_unseen_language, topics, word2vec_path=None, binary=True, topk=10): """ :param doc_distribution_original_language: numpy array of the topical distribution of the documents in the original language (dim: num docs x num topics) :param doc_distribution_unseen_language: numpy array of the topical distribution of the documents in an unseen language (dim: num docs x num topics) :param topics: a list of lists of the top-n most likely words :param word2vec_path: if word2vec_file is specified, it retrieves the word embeddings file (in word2vec format) to compute similarities between words, otherwise 'word2vec-google-news-300' is downloaded :param binary: if the word2vec file is binary :param topk: max number of topical words """ super().__init__() self.topics = [t[:topk] for t in topics] self.orig_lang_docs = doc_distribution_original_language self.unseen_lang_docs = doc_distribution_unseen_language if len(self.orig_lang_docs) != len(self.unseen_lang_docs): raise Exception('Distributions of the comparable documents must have the same length') if word2vec_path is None: self.wv = api.load('word2vec-google-news-300') else: self.wv = KeyedVectors.load_word2vec_format(word2vec_path, binary=binary) def score(self): """ :return: average centroid distance between the words of the most likely topic of the document distributions """ cd = 0 for d1, d2 in zip(self.orig_lang_docs, self.unseen_lang_docs): top_words_orig = self.topics[np.argmax(d1)] top_words_unseen = self.topics[np.argmax(d2)] centroid_lang = self.get_centroid(top_words_orig) centroid_en = self.get_centroid(top_words_unseen) cd += (1 - cosine(centroid_lang, centroid_en)) return cd/len(self.unseen_lang_docs) def get_centroid(self, word_list): vector_list = [] for word in word_list: if word in self.wv.vocab: vector_list.append(self.wv.get_vector(word)) vec = sum(vector_list) return vec / np.linalg.norm(vec)
[]
2024-01-10
huiguyy/ChuanhuChatGPT
modules~models~base_model.py
from __future__ import annotations from typing import TYPE_CHECKING, List import logging import json import commentjson as cjson import os import sys import requests import urllib3 import traceback import pathlib from tqdm import tqdm import colorama from googlesearch import search import asyncio import aiohttp from enum import Enum from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.callbacks.manager import BaseCallbackManager from typing import Any, Dict, List, Optional, Union from langchain.callbacks.base import BaseCallbackHandler from langchain.input import print_text from langchain.schema import AgentAction, AgentFinish, LLMResult from threading import Thread, Condition from collections import deque from ..presets import * from ..index_func import * from ..utils import * from .. import shared from ..config import retrieve_proxy class CallbackToIterator: def __init__(self): self.queue = deque() self.cond = Condition() self.finished = False def callback(self, result): with self.cond: self.queue.append(result) self.cond.notify() # Wake up the generator. def __iter__(self): return self def __next__(self): with self.cond: while not self.queue and not self.finished: # Wait for a value to be added to the queue. self.cond.wait() if not self.queue: raise StopIteration() return self.queue.popleft() def finish(self): with self.cond: self.finished = True self.cond.notify() # Wake up the generator if it's waiting. def get_action_description(text): match = re.search('```(.*?)```', text, re.S) json_text = match.group(1) # 把json转化为python字典 json_dict = json.loads(json_text) # 提取'action'和'action_input'的值 action_name = json_dict['action'] action_input = json_dict['action_input'] if action_name != "Final Answer": return f'<p style="font-size: smaller; color: gray;">{action_name}: {action_input}</p>' else: return "" class ChuanhuCallbackHandler(BaseCallbackHandler): def __init__(self, callback) -> None: """Initialize callback handler.""" self.callback = callback def on_agent_action( self, action: AgentAction, color: Optional[str] = None, **kwargs: Any ) -> Any: self.callback(get_action_description(action.log)) def on_tool_end( self, output: str, color: Optional[str] = None, observation_prefix: Optional[str] = None, llm_prefix: Optional[str] = None, **kwargs: Any, ) -> None: """If not the final action, print out observation.""" # if observation_prefix is not None: # self.callback(f"\n\n{observation_prefix}") # self.callback(output) # if llm_prefix is not None: # self.callback(f"\n\n{llm_prefix}") if observation_prefix is not None: logging.info(observation_prefix) self.callback(output) if llm_prefix is not None: logging.info(llm_prefix) def on_agent_finish( self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any ) -> None: # self.callback(f"{finish.log}\n\n") logging.info(finish.log) def on_llm_new_token(self, token: str, **kwargs: Any) -> None: """Run on new LLM token. Only available when streaming is enabled.""" self.callback(token) class ModelType(Enum): Unknown = -1 OpenAI = 0 ChatGLM = 1 LLaMA = 2 XMChat = 3 StableLM = 4 MOSS = 5 YuanAI = 6 Minimax = 7 ChuanhuAgent = 8 @classmethod def get_type(cls, model_name: str): model_type = None model_name_lower = model_name.lower() if "gpt" in model_name_lower: model_type = ModelType.OpenAI elif "chatglm" in model_name_lower: model_type = ModelType.ChatGLM elif "llama" in model_name_lower or "alpaca" in model_name_lower: model_type = ModelType.LLaMA elif "xmchat" in model_name_lower: model_type = ModelType.XMChat elif "stablelm" in model_name_lower: model_type = ModelType.StableLM elif "moss" in model_name_lower: model_type = ModelType.MOSS elif "yuanai" in model_name_lower: model_type = ModelType.YuanAI elif "minimax" in model_name_lower: model_type = ModelType.Minimax elif "川虎助理" in model_name_lower: model_type = ModelType.ChuanhuAgent else: model_type = ModelType.Unknown return model_type class BaseLLMModel: def __init__( self, model_name, system_prompt="", temperature=1.0, top_p=1.0, n_choices=1, stop=None, max_generation_token=None, presence_penalty=0, frequency_penalty=0, logit_bias=None, user="", ) -> None: self.history = [] self.all_token_counts = [] self.model_name = model_name self.model_type = ModelType.get_type(model_name) try: self.token_upper_limit = MODEL_TOKEN_LIMIT[model_name] except KeyError: self.token_upper_limit = DEFAULT_TOKEN_LIMIT self.interrupted = False self.system_prompt = system_prompt self.api_key = None self.need_api_key = False self.single_turn = False self.temperature = temperature self.top_p = top_p self.n_choices = n_choices self.stop_sequence = stop self.max_generation_token = None self.presence_penalty = presence_penalty self.frequency_penalty = frequency_penalty self.logit_bias = logit_bias self.user_identifier = user def get_answer_stream_iter(self): """stream predict, need to be implemented conversations are stored in self.history, with the most recent question, in OpenAI format should return a generator, each time give the next word (str) in the answer """ logging.warning("stream predict not implemented, using at once predict instead") response, _ = self.get_answer_at_once() yield response def get_answer_at_once(self): """predict at once, need to be implemented conversations are stored in self.history, with the most recent question, in OpenAI format Should return: the answer (str) total token count (int) """ logging.warning("at once predict not implemented, using stream predict instead") response_iter = self.get_answer_stream_iter() count = 0 for response in response_iter: count += 1 return response, sum(self.all_token_counts) + count def billing_info(self): """get billing infomation, inplement if needed""" logging.warning("billing info not implemented, using default") return BILLING_NOT_APPLICABLE_MSG def count_token(self, user_input): """get token count from input, implement if needed""" # logging.warning("token count not implemented, using default") return len(user_input) def stream_next_chatbot(self, inputs, chatbot, fake_input=None, display_append=""): def get_return_value(): return chatbot, status_text status_text = i18n("开始实时传输回答……") if fake_input: chatbot.append((fake_input, "")) else: chatbot.append((inputs, "")) user_token_count = self.count_token(inputs) self.all_token_counts.append(user_token_count) logging.debug(f"输入token计数: {user_token_count}") stream_iter = self.get_answer_stream_iter() if display_append: display_append = "<hr>" +display_append for partial_text in stream_iter: chatbot[-1] = (chatbot[-1][0], partial_text + display_append) self.all_token_counts[-1] += 1 status_text = self.token_message() yield get_return_value() if self.interrupted: self.recover() break self.history.append(construct_assistant(partial_text)) def next_chatbot_at_once(self, inputs, chatbot, fake_input=None, display_append=""): if fake_input: chatbot.append((fake_input, "")) else: chatbot.append((inputs, "")) if fake_input is not None: user_token_count = self.count_token(fake_input) else: user_token_count = self.count_token(inputs) self.all_token_counts.append(user_token_count) ai_reply, total_token_count = self.get_answer_at_once() self.history.append(construct_assistant(ai_reply)) if fake_input is not None: self.history[-2] = construct_user(fake_input) chatbot[-1] = (chatbot[-1][0], ai_reply + display_append) if fake_input is not None: self.all_token_counts[-1] += count_token(construct_assistant(ai_reply)) else: self.all_token_counts[-1] = total_token_count - sum(self.all_token_counts) status_text = self.token_message() return chatbot, status_text def handle_file_upload(self, files, chatbot, language): """if the model accepts multi modal input, implement this function""" status = gr.Markdown.update() if files: index = construct_index(self.api_key, file_src=files) status = i18n("索引构建完成") return gr.Files.update(), chatbot, status def summarize_index(self, files, chatbot, language): status = gr.Markdown.update() if files: index = construct_index(self.api_key, file_src=files) status = i18n("总结完成") logging.info(i18n("生成内容总结中……")) os.environ["OPENAI_API_KEY"] = self.api_key from langchain.chains.summarize import load_summarize_chain from langchain.prompts import PromptTemplate from langchain.chat_models import ChatOpenAI from langchain.callbacks import StdOutCallbackHandler prompt_template = "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN " + language + ":" PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"]) llm = ChatOpenAI() chain = load_summarize_chain(llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT) summary = chain({"input_documents": list(index.docstore.__dict__["_dict"].values())}, return_only_outputs=True)["output_text"] print(i18n("总结") + f": {summary}") chatbot.append([i18n("上传了")+str(len(files))+"个文件", summary]) return chatbot, status def prepare_inputs(self, real_inputs, use_websearch, files, reply_language, chatbot): fake_inputs = None display_append = [] limited_context = False fake_inputs = real_inputs if files: from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.vectorstores.base import VectorStoreRetriever limited_context = True msg = "加载索引中……" logging.info(msg) index = construct_index(self.api_key, file_src=files) assert index is not None, "获取索引失败" msg = "索引获取成功,生成回答中……" logging.info(msg) with retrieve_proxy(): retriever = VectorStoreRetriever(vectorstore=index, search_type="similarity_score_threshold",search_kwargs={"k":6, "score_threshold": 0.5}) relevant_documents = retriever.get_relevant_documents(real_inputs) reference_results = [[d.page_content.strip("�"), os.path.basename(d.metadata["source"])] for d in relevant_documents] reference_results = add_source_numbers(reference_results) display_append = add_details(reference_results) display_append = "\n\n" + "".join(display_append) real_inputs = ( replace_today(PROMPT_TEMPLATE) .replace("{query_str}", real_inputs) .replace("{context_str}", "\n\n".join(reference_results)) .replace("{reply_language}", reply_language) ) elif use_websearch: limited_context = True search_results = [i for i in search(real_inputs, advanced=True)] reference_results = [] for idx, result in enumerate(search_results): logging.debug(f"搜索结果{idx + 1}:{result}") domain_name = urllib3.util.parse_url(result.url).host reference_results.append([result.description, result.url]) display_append.append( # f"{idx+1}. [{domain_name}]({result['href']})\n" f"<li><a href=\"{result.url}\" target=\"_blank\">{domain_name}</a></li>\n" ) reference_results = add_source_numbers(reference_results) display_append = "<ol>\n\n" + "".join(display_append) + "</ol>" real_inputs = ( replace_today(WEBSEARCH_PTOMPT_TEMPLATE) .replace("{query}", real_inputs) .replace("{web_results}", "\n\n".join(reference_results)) .replace("{reply_language}", reply_language) ) else: display_append = "" return limited_context, fake_inputs, display_append, real_inputs, chatbot def predict( self, inputs, chatbot, stream=False, use_websearch=False, files=None, reply_language="中文", should_check_token_count=True, ): # repetition_penalty, top_k status_text = "开始生成回答……" logging.info( "输入为:" + colorama.Fore.BLUE + f"{inputs}" + colorama.Style.RESET_ALL ) if should_check_token_count: yield chatbot + [(inputs, "")], status_text if reply_language == "跟随问题语言(不稳定)": reply_language = "the same language as the question, such as English, 中文, 日本語, Español, Français, or Deutsch." limited_context, fake_inputs, display_append, inputs, chatbot = self.prepare_inputs(real_inputs=inputs, use_websearch=use_websearch, files=files, reply_language=reply_language, chatbot=chatbot) yield chatbot + [(fake_inputs, "")], status_text if ( self.need_api_key and self.api_key is None and not shared.state.multi_api_key ): status_text = STANDARD_ERROR_MSG + NO_APIKEY_MSG logging.info(status_text) chatbot.append((inputs, "")) if len(self.history) == 0: self.history.append(construct_user(inputs)) self.history.append("") self.all_token_counts.append(0) else: self.history[-2] = construct_user(inputs) yield chatbot + [(inputs, "")], status_text return elif len(inputs.strip()) == 0: status_text = STANDARD_ERROR_MSG + NO_INPUT_MSG logging.info(status_text) yield chatbot + [(inputs, "")], status_text return if self.single_turn: self.history = [] self.all_token_counts = [] self.history.append(construct_user(inputs)) try: if stream: logging.debug("使用流式传输") iter = self.stream_next_chatbot( inputs, chatbot, fake_input=fake_inputs, display_append=display_append, ) for chatbot, status_text in iter: yield chatbot, status_text else: logging.debug("不使用流式传输") chatbot, status_text = self.next_chatbot_at_once( inputs, chatbot, fake_input=fake_inputs, display_append=display_append, ) yield chatbot, status_text except Exception as e: traceback.print_exc() status_text = STANDARD_ERROR_MSG + str(e) yield chatbot, status_text if len(self.history) > 1 and self.history[-1]["content"] != inputs: logging.info( "回答为:" + colorama.Fore.BLUE + f"{self.history[-1]['content']}" + colorama.Style.RESET_ALL ) if limited_context: # self.history = self.history[-4:] # self.all_token_counts = self.all_token_counts[-2:] self.history = [] self.all_token_counts = [] max_token = self.token_upper_limit - TOKEN_OFFSET if sum(self.all_token_counts) > max_token and should_check_token_count: count = 0 while ( sum(self.all_token_counts) > self.token_upper_limit * REDUCE_TOKEN_FACTOR and sum(self.all_token_counts) > 0 ): count += 1 del self.all_token_counts[0] del self.history[:2] logging.info(status_text) status_text = f"为了防止token超限,模型忘记了早期的 {count} 轮对话" yield chatbot, status_text self.auto_save(chatbot) def retry( self, chatbot, stream=False, use_websearch=False, files=None, reply_language="中文", ): logging.debug("重试中……") if len(self.history) > 0: inputs = self.history[-2]["content"] del self.history[-2:] self.all_token_counts.pop() elif len(chatbot) > 0: inputs = chatbot[-1][0] else: yield chatbot, f"{STANDARD_ERROR_MSG}上下文是空的" return iter = self.predict( inputs, chatbot, stream=stream, use_websearch=use_websearch, files=files, reply_language=reply_language, ) for x in iter: yield x logging.debug("重试完毕") # def reduce_token_size(self, chatbot): # logging.info("开始减少token数量……") # chatbot, status_text = self.next_chatbot_at_once( # summarize_prompt, # chatbot # ) # max_token_count = self.token_upper_limit * REDUCE_TOKEN_FACTOR # num_chat = find_n(self.all_token_counts, max_token_count) # logging.info(f"previous_token_count: {self.all_token_counts}, keeping {num_chat} chats") # chatbot = chatbot[:-1] # self.history = self.history[-2*num_chat:] if num_chat > 0 else [] # self.all_token_counts = self.all_token_counts[-num_chat:] if num_chat > 0 else [] # msg = f"保留了最近{num_chat}轮对话" # logging.info(msg) # logging.info("减少token数量完毕") # return chatbot, msg + "," + self.token_message(self.all_token_counts if len(self.all_token_counts) > 0 else [0]) def interrupt(self): self.interrupted = True def recover(self): self.interrupted = False def set_token_upper_limit(self, new_upper_limit): self.token_upper_limit = new_upper_limit print(f"token上限设置为{new_upper_limit}") def set_temperature(self, new_temperature): self.temperature = new_temperature def set_top_p(self, new_top_p): self.top_p = new_top_p def set_n_choices(self, new_n_choices): self.n_choices = new_n_choices def set_stop_sequence(self, new_stop_sequence: str): new_stop_sequence = new_stop_sequence.split(",") self.stop_sequence = new_stop_sequence def set_max_tokens(self, new_max_tokens): self.max_generation_token = new_max_tokens def set_presence_penalty(self, new_presence_penalty): self.presence_penalty = new_presence_penalty def set_frequency_penalty(self, new_frequency_penalty): self.frequency_penalty = new_frequency_penalty def set_logit_bias(self, logit_bias): logit_bias = logit_bias.split() bias_map = {} encoding = tiktoken.get_encoding("cl100k_base") for line in logit_bias: word, bias_amount = line.split(":") if word: for token in encoding.encode(word): bias_map[token] = float(bias_amount) self.logit_bias = bias_map def set_user_identifier(self, new_user_identifier): self.user_identifier = new_user_identifier def set_system_prompt(self, new_system_prompt): self.system_prompt = new_system_prompt def set_key(self, new_access_key): self.api_key = new_access_key.strip() msg = i18n("API密钥更改为了") + hide_middle_chars(self.api_key) logging.info(msg) return self.api_key, msg def set_single_turn(self, new_single_turn): self.single_turn = new_single_turn def reset(self): self.history = [] self.all_token_counts = [] self.interrupted = False pathlib.Path(os.path.join(HISTORY_DIR, self.user_identifier, new_auto_history_filename(os.path.join(HISTORY_DIR, self.user_identifier)))).touch() return [], self.token_message([0]) def delete_first_conversation(self): if self.history: del self.history[:2] del self.all_token_counts[0] return self.token_message() def delete_last_conversation(self, chatbot): if len(chatbot) > 0 and STANDARD_ERROR_MSG in chatbot[-1][1]: msg = "由于包含报错信息,只删除chatbot记录" chatbot.pop() return chatbot, self.history if len(self.history) > 0: self.history.pop() self.history.pop() if len(chatbot) > 0: msg = "删除了一组chatbot对话" chatbot.pop() if len(self.all_token_counts) > 0: msg = "删除了一组对话的token计数记录" self.all_token_counts.pop() msg = "删除了一组对话" return chatbot, msg def token_message(self, token_lst=None): if token_lst is None: token_lst = self.all_token_counts token_sum = 0 for i in range(len(token_lst)): token_sum += sum(token_lst[: i + 1]) return i18n("Token 计数: ") + f"{sum(token_lst)}" + i18n(",本次对话累计消耗了 ") + f"{token_sum} tokens" def save_chat_history(self, filename, chatbot, user_name): if filename == "": return if not filename.endswith(".json"): filename += ".json" return save_file(filename, self.system_prompt, self.history, chatbot, user_name) def auto_save(self, chatbot): history_file_path = get_history_filepath(self.user_identifier) save_file(history_file_path, self.system_prompt, self.history, chatbot, self.user_identifier) def export_markdown(self, filename, chatbot, user_name): if filename == "": return if not filename.endswith(".md"): filename += ".md" return save_file(filename, self.system_prompt, self.history, chatbot, user_name) def load_chat_history(self, filename, user_name): logging.debug(f"{user_name} 加载对话历史中……") logging.info(f"filename: {filename}") if type(filename) != str and filename is not None: filename = filename.name try: if "/" not in filename: history_file_path = os.path.join(HISTORY_DIR, user_name, filename) else: history_file_path = filename with open(history_file_path, "r") as f: json_s = json.load(f) try: if type(json_s["history"][0]) == str: logging.info("历史记录格式为旧版,正在转换……") new_history = [] for index, item in enumerate(json_s["history"]): if index % 2 == 0: new_history.append(construct_user(item)) else: new_history.append(construct_assistant(item)) json_s["history"] = new_history logging.info(new_history) except: pass logging.debug(f"{user_name} 加载对话历史完毕") self.history = json_s["history"] return os.path.basename(filename), json_s["system"], json_s["chatbot"] except: # 没有对话历史或者对话历史解析失败 logging.info(f"没有找到对话历史记录 {filename}") return gr.update(), self.system_prompt, gr.update() def auto_load(self): if self.user_identifier == "": self.reset() return self.system_prompt, gr.update() history_file_path = get_history_filepath(self.user_identifier) filename, system_prompt, chatbot = self.load_chat_history(history_file_path, self.user_identifier) return system_prompt, chatbot def like(self): """like the last response, implement if needed """ return gr.update() def dislike(self): """dislike the last response, implement if needed """ return gr.update()
[ "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN PLACEHOLDER:" ]
2024-01-10
huiguyy/ChuanhuChatGPT
modules~shared.py
from modules.presets import COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST import os import queue import openai class State: interrupted = False multi_api_key = False completion_url = COMPLETION_URL balance_api_url = BALANCE_API_URL usage_api_url = USAGE_API_URL def interrupt(self): self.interrupted = True def recover(self): self.interrupted = False def set_api_host(self, api_host: str): api_host = api_host.rstrip("/") if not api_host.startswith("http"): api_host = f"https://{api_host}" if api_host.endswith("/v1"): api_host = api_host[:-3] self.completion_url = f"{api_host}/v1/chat/completions" self.balance_api_url = f"{api_host}/dashboard/billing/credit_grants" self.usage_api_url = f"{api_host}/dashboard/billing/usage" os.environ["OPENAI_API_BASE"] = api_host def reset_api_host(self): self.completion_url = COMPLETION_URL self.balance_api_url = BALANCE_API_URL self.usage_api_url = USAGE_API_URL os.environ["OPENAI_API_BASE"] = f"https://{API_HOST}" return API_HOST def reset_all(self): self.interrupted = False self.completion_url = COMPLETION_URL def set_api_key_queue(self, api_key_list): self.multi_api_key = True self.api_key_queue = queue.Queue() for api_key in api_key_list: self.api_key_queue.put(api_key) def switching_api_key(self, func): if not hasattr(self, "api_key_queue"): return func def wrapped(*args, **kwargs): api_key = self.api_key_queue.get() args[0].api_key = api_key ret = func(*args, **kwargs) self.api_key_queue.put(api_key) return ret return wrapped state = State()
[]
2024-01-10
huiguyy/ChuanhuChatGPT
modules~index_func.py
import os import logging import colorama import PyPDF2 from tqdm import tqdm from modules.presets import * from modules.utils import * from modules.config import local_embedding def get_index_name(file_src): file_paths = [x.name for x in file_src] file_paths.sort(key=lambda x: os.path.basename(x)) md5_hash = hashlib.md5() for file_path in file_paths: with open(file_path, "rb") as f: while chunk := f.read(8192): md5_hash.update(chunk) return md5_hash.hexdigest() def get_documents(file_src): from langchain.schema import Document from langchain.text_splitter import TokenTextSplitter text_splitter = TokenTextSplitter(chunk_size=500, chunk_overlap=30) documents = [] logging.debug("Loading documents...") logging.debug(f"file_src: {file_src}") for file in file_src: filepath = file.name filename = os.path.basename(filepath) file_type = os.path.splitext(filename)[1] logging.info(f"loading file: {filename}") try: if file_type == ".pdf": logging.debug("Loading PDF...") try: from modules.pdf_func import parse_pdf from modules.config import advance_docs two_column = advance_docs["pdf"].get("two_column", False) pdftext = parse_pdf(filepath, two_column).text except: pdftext = "" with open(filepath, "rb") as pdfFileObj: pdfReader = PyPDF2.PdfReader(pdfFileObj) for page in tqdm(pdfReader.pages): pdftext += page.extract_text() texts = Document(page_content=pdftext, metadata={"source": filepath}) elif file_type == ".docx": logging.debug("Loading Word...") from langchain.document_loaders import UnstructuredWordDocumentLoader loader = UnstructuredWordDocumentLoader(filepath) texts = loader.load() elif file_type == ".pptx": logging.debug("Loading PowerPoint...") from langchain.document_loaders import UnstructuredPowerPointLoader loader = UnstructuredPowerPointLoader(filepath) texts = loader.load() elif file_type == ".epub": logging.debug("Loading EPUB...") from langchain.document_loaders import UnstructuredEPubLoader loader = UnstructuredEPubLoader(filepath) texts = loader.load() elif file_type == ".xlsx": logging.debug("Loading Excel...") text_list = excel_to_string(filepath) for elem in text_list: documents.append(Document(page_content=elem, metadata={"source": filepath})) continue else: logging.debug("Loading text file...") from langchain.document_loaders import TextLoader loader = TextLoader(filepath, "utf8") texts = loader.load() except Exception as e: import traceback logging.error(f"Error loading file: {filename}") traceback.print_exc() texts = text_splitter.split_documents([texts]) documents.extend(texts) logging.debug("Documents loaded.") return documents def construct_index( api_key, file_src, max_input_size=4096, num_outputs=5, max_chunk_overlap=20, chunk_size_limit=600, embedding_limit=None, separator=" ", ): from langchain.chat_models import ChatOpenAI from langchain.vectorstores import FAISS if api_key: os.environ["OPENAI_API_KEY"] = api_key else: # 由于一个依赖的愚蠢的设计,这里必须要有一个API KEY os.environ["OPENAI_API_KEY"] = "sk-xxxxxxx" chunk_size_limit = None if chunk_size_limit == 0 else chunk_size_limit embedding_limit = None if embedding_limit == 0 else embedding_limit separator = " " if separator == "" else separator index_name = get_index_name(file_src) index_path = f"./index/{index_name}" if local_embedding: from langchain.embeddings.huggingface import HuggingFaceEmbeddings embeddings = HuggingFaceEmbeddings(model_name = "sentence-transformers/distiluse-base-multilingual-cased-v2") else: from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings(openai_api_base=os.environ.get("OPENAI_API_BASE", None), openai_api_key=os.environ.get("OPENAI_EMBEDDING_API_KEY", api_key)) if os.path.exists(index_path): logging.info("找到了缓存的索引文件,加载中……") return FAISS.load_local(index_path, embeddings) else: try: documents = get_documents(file_src) logging.info("构建索引中……") with retrieve_proxy(): index = FAISS.from_documents(documents, embeddings) logging.debug("索引构建完成!") os.makedirs("./index", exist_ok=True) index.save_local(index_path) logging.debug("索引已保存至本地!") return index except Exception as e: import traceback logging.error("索引构建失败!%s", e) traceback.print_exc() return None
[]
2024-01-10
huiguyy/ChuanhuChatGPT
modules~models~ChuanhuAgent.py
from langchain.chains.summarize import load_summarize_chain from langchain import PromptTemplate, LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.text_splitter import TokenTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains import RetrievalQA from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.docstore.document import Document from langchain.tools import BaseTool, StructuredTool, Tool, tool from langchain.callbacks.stdout import StdOutCallbackHandler from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.callbacks.manager import BaseCallbackManager from googlesearch import search from typing import Any, Dict, List, Optional, Union from langchain.callbacks.base import BaseCallbackHandler from langchain.input import print_text from langchain.schema import AgentAction, AgentFinish, LLMResult from pydantic import BaseModel, Field import requests from bs4 import BeautifulSoup from threading import Thread, Condition from collections import deque from .base_model import BaseLLMModel, CallbackToIterator, ChuanhuCallbackHandler from ..config import default_chuanhu_assistant_model from ..presets import SUMMARIZE_PROMPT, i18n from ..index_func import construct_index from langchain.callbacks import get_openai_callback import os import gradio as gr import logging class GoogleSearchInput(BaseModel): keywords: str = Field(description="keywords to search") class WebBrowsingInput(BaseModel): url: str = Field(description="URL of a webpage") class WebAskingInput(BaseModel): url: str = Field(description="URL of a webpage") question: str = Field(description="Question that you want to know the answer to, based on the webpage's content.") class ChuanhuAgent_Client(BaseLLMModel): def __init__(self, model_name, openai_api_key, user_name="") -> None: super().__init__(model_name=model_name, user=user_name) self.text_splitter = TokenTextSplitter(chunk_size=500, chunk_overlap=30) self.api_key = openai_api_key self.llm = ChatOpenAI(openai_api_key=openai_api_key, temperature=0, model_name=default_chuanhu_assistant_model, openai_api_base=os.environ.get("OPENAI_API_BASE", None)) self.cheap_llm = ChatOpenAI(openai_api_key=openai_api_key, temperature=0, model_name="gpt-3.5-turbo", openai_api_base=os.environ.get("OPENAI_API_BASE", None)) PROMPT = PromptTemplate(template=SUMMARIZE_PROMPT, input_variables=["text"]) self.summarize_chain = load_summarize_chain(self.cheap_llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT) self.index_summary = None self.index = None if "Pro" in self.model_name: self.tools = load_tools(["google-search-results-json", "llm-math", "arxiv", "wikipedia", "wolfram-alpha"], llm=self.llm) else: self.tools = load_tools(["ddg-search", "llm-math", "arxiv", "wikipedia"], llm=self.llm) self.tools.append( Tool.from_function( func=self.google_search_simple, name="Google Search JSON", description="useful when you need to search the web.", args_schema=GoogleSearchInput ) ) self.tools.append( Tool.from_function( func=self.summary_url, name="Summary Webpage", description="useful when you need to know the overall content of a webpage.", args_schema=WebBrowsingInput ) ) self.tools.append( StructuredTool.from_function( func=self.ask_url, name="Ask Webpage", description="useful when you need to ask detailed questions about a webpage.", args_schema=WebAskingInput ) ) def google_search_simple(self, query): results = [{"title": i.title, "link": i.url, "snippet": i.description} for i in search(query, advanced=True)] return str(results) def handle_file_upload(self, files, chatbot, language): """if the model accepts multi modal input, implement this function""" status = gr.Markdown.update() if files: index = construct_index(self.api_key, file_src=files) assert index is not None, "获取索引失败" self.index = index status = i18n("索引构建完成") # Summarize the document logging.info(i18n("生成内容总结中……")) with get_openai_callback() as cb: os.environ["OPENAI_API_KEY"] = self.api_key from langchain.chains.summarize import load_summarize_chain from langchain.prompts import PromptTemplate from langchain.chat_models import ChatOpenAI prompt_template = "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN " + language + ":" PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"]) llm = ChatOpenAI() chain = load_summarize_chain(llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT) summary = chain({"input_documents": list(index.docstore.__dict__["_dict"].values())}, return_only_outputs=True)["output_text"] logging.info(f"Summary: {summary}") self.index_summary = summary chatbot.append((f"Uploaded {len(files)} files", summary)) logging.info(cb) return gr.Files.update(), chatbot, status def query_index(self, query): if self.index is not None: retriever = self.index.as_retriever() qa = RetrievalQA.from_chain_type(llm=self.llm, chain_type="stuff", retriever=retriever) return qa.run(query) else: "Error during query." def summary(self, text): texts = Document(page_content=text) texts = self.text_splitter.split_documents([texts]) return self.summarize_chain({"input_documents": texts}, return_only_outputs=True)["output_text"] def fetch_url_content(self, url): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # 提取所有的文本 text = ''.join(s.getText() for s in soup.find_all('p')) logging.info(f"Extracted text from {url}") return text def summary_url(self, url): text = self.fetch_url_content(url) if text == "": return "URL unavailable." text_summary = self.summary(text) url_content = "webpage content summary:\n" + text_summary return url_content def ask_url(self, url, question): text = self.fetch_url_content(url) if text == "": return "URL unavailable." texts = Document(page_content=text) texts = self.text_splitter.split_documents([texts]) # use embedding embeddings = OpenAIEmbeddings(openai_api_key=self.api_key, openai_api_base=os.environ.get("OPENAI_API_BASE", None)) # create vectorstore db = FAISS.from_documents(texts, embeddings) retriever = db.as_retriever() qa = RetrievalQA.from_chain_type(llm=self.cheap_llm, chain_type="stuff", retriever=retriever) return qa.run(f"{question} Reply in 中文") def get_answer_at_once(self): question = self.history[-1]["content"] # llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo") agent = initialize_agent(self.tools, self.llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True) reply = agent.run(input=f"{question} Reply in 简体中文") return reply, -1 def get_answer_stream_iter(self): question = self.history[-1]["content"] it = CallbackToIterator() manager = BaseCallbackManager(handlers=[ChuanhuCallbackHandler(it.callback)]) def thread_func(): tools = self.tools if self.index is not None: tools.append( Tool.from_function( func=self.query_index, name="Query Knowledge Base", description=f"useful when you need to know about: {self.index_summary}", args_schema=WebBrowsingInput ) ) agent = initialize_agent(self.tools, self.llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, callback_manager=manager) try: reply = agent.run(input=f"{question} Reply in 简体中文") except Exception as e: import traceback traceback.print_exc() reply = str(e) it.callback(reply) it.finish() t = Thread(target=thread_func) t.start() partial_text = "" for value in it: partial_text += value yield partial_text
[ "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN PLACEHOLDER:" ]
2024-01-10
shivangg/tb2_avoid_wall
openai_ros~openai_ros~src~openai_ros~robot_envs~cube_single_disk_env.py
import numpy import rospy from openai_ros import robot_gazebo_env from std_msgs.msg import Float64 from sensor_msgs.msg import JointState from nav_msgs.msg import Odometry class CubeSingleDiskEnv(robot_gazebo_env.RobotGazeboEnv): """Superclass for all CubeSingleDisk environments. """ def __init__(self): """Initializes a new CubeSingleDisk environment. Args: """ # Variables that we give through the constructor. # None in this case # Internal Vars self.controllers_list = ['joint_state_controller', 'inertia_wheel_roll_joint_velocity_controller' ] self.robot_name_space = "moving_cube" # We launch the init function of the Parent Class robot_gazebo_env.RobotGazeboEnv super(CubeSingleDiskEnv, self).__init__(controllers_list=self.controllers_list, robot_name_space=self.robot_name_space, reset_controls=True) """ To check any topic we need to have the simulations running, we need to do two things: 1) Unpause the simulation: without that th stream of data doesnt flow. This is for simulations that are pause for whatever the reason 2) If the simulation was running already for some reason, we need to reset the controlers. This has to do with the fact that some plugins with tf, dont understand the reset of the simulation and need to be reseted to work properly. """ self.gazebo.unpauseSim() self.controllers_object.reset_controllers() self._check_all_sensors_ready() # We Start all the ROS related Subscribers and publishers rospy.Subscriber("/moving_cube/joint_states", JointState, self._joints_callback) rospy.Subscriber("/moving_cube/odom", Odometry, self._odom_callback) self._roll_vel_pub = rospy.Publisher('/moving_cube/inertia_wheel_roll_joint_velocity_controller/command', Float64, queue_size=1) self._check_publishers_connection() self.gazebo.pauseSim() # Methods needed by the RobotGazeboEnv # ---------------------------- def _check_all_systems_ready(self): """ Checks that all the sensors, publishers and other simulation systems are operational. """ self._check_all_sensors_ready() return True # CubeSingleDiskEnv virtual methods # ---------------------------- def _check_all_sensors_ready(self): self._check_joint_states_ready() self._check_odom_ready() rospy.logdebug("ALL SENSORS READY") def _check_joint_states_ready(self): self.joints = None while self.joints is None and not rospy.is_shutdown(): try: self.joints = rospy.wait_for_message("/moving_cube/joint_states", JointState, timeout=1.0) rospy.logdebug("Current moving_cube/joint_states READY=>" + str(self.joints)) except: rospy.logerr("Current moving_cube/joint_states not ready yet, retrying for getting joint_states") return self.joints def _check_odom_ready(self): self.odom = None while self.odom is None and not rospy.is_shutdown(): try: self.odom = rospy.wait_for_message("/moving_cube/odom", Odometry, timeout=1.0) rospy.logdebug("Current /moving_cube/odom READY=>" + str(self.odom)) except: rospy.logerr("Current /moving_cube/odom not ready yet, retrying for getting odom") return self.odom def _joints_callback(self, data): self.joints = data def _odom_callback(self, data): self.odom = data def _check_publishers_connection(self): """ Checks that all the publishers are working :return: """ rate = rospy.Rate(10) # 10hz while self._roll_vel_pub.get_num_connections() == 0 and not rospy.is_shutdown(): rospy.logdebug("No susbribers to _roll_vel_pub yet so we wait and try again") try: rate.sleep() except rospy.ROSInterruptException: # This is to avoid error when world is rested, time when backwards. pass rospy.logdebug("_roll_vel_pub Publisher Connected") rospy.logdebug("All Publishers READY") # Methods that the TrainingEnvironment will need to define here as virtual # because they will be used in RobotGazeboEnv GrandParentClass and defined in the # TrainingEnvironment. # ---------------------------- def _set_init_pose(self): """Sets the Robot in its init pose """ raise NotImplementedError() def _init_env_variables(self): """Inits variables needed to be initialised each time we reset at the start of an episode. """ raise NotImplementedError() def _compute_reward(self, observations, done): """Calculates the reward to give based on the observations given. """ raise NotImplementedError() def _set_action(self, action): """Applies the given action to the simulation. """ raise NotImplementedError() def _get_obs(self): raise NotImplementedError() def _is_done(self, observations): """Checks if episode done based on observations given. """ raise NotImplementedError() # Methods that the TrainingEnvironment will need. # ---------------------------- def move_joints(self, roll_speed): joint_speed_value = Float64() joint_speed_value.data = roll_speed rospy.logdebug("Single Disk Roll Velocity>>" + str(joint_speed_value)) self._roll_vel_pub.publish(joint_speed_value) self.wait_until_roll_is_in_vel(joint_speed_value.data) def wait_until_roll_is_in_vel(self, velocity): rate = rospy.Rate(10) start_wait_time = rospy.get_rostime().to_sec() end_wait_time = 0.0 epsilon = 0.1 v_plus = velocity + epsilon v_minus = velocity - epsilon while not rospy.is_shutdown(): joint_data = self._check_joint_states_ready() roll_vel = joint_data.velocity[0] rospy.logdebug("VEL=" + str(roll_vel) + ", ?RANGE=[" + str(v_minus) + ","+str(v_plus)+"]") are_close = (roll_vel <= v_plus) and (roll_vel > v_minus) if are_close: rospy.logdebug("Reached Velocity!") end_wait_time = rospy.get_rostime().to_sec() break rospy.logdebug("Not there yet, keep waiting...") rate.sleep() delta_time = end_wait_time- start_wait_time rospy.logdebug("[Wait Time=" + str(delta_time)+"]") return delta_time def get_joints(self): return self.joints def get_odom(self): return self.odom
[]
2024-01-10
shivangg/tb2_avoid_wall
tb_wall_avoider~scripts~my_turtlebot2_wall.py
import rospy import numpy from gym import spaces from openai_ros.robot_envs import turtlebot2_env from gym.envs.registration import register from geometry_msgs.msg import Point timestep_limit_per_episode = 10000 # Can be any Value register( id='MyTurtleBot2Wall-v0', entry_point='my_turtlebot2_wall:MyTurtleBot2WallEnv', timestep_limit=timestep_limit_per_episode, ) class MyTurtleBot2WallEnv(turtlebot2_env.TurtleBot2Env): def __init__(self): """ This Task Env is designed for having the TurtleBot2 in some kind of maze. It will learn how to move around the maze without crashing. """ # Only variable needed to be set here number_actions = rospy.get_param('/turtlebot2/n_actions') self.action_space = spaces.Discrete(number_actions) # We set the reward range, which is not compulsory but here we do it. self.reward_range = (-numpy.inf, numpy.inf) #number_observations = rospy.get_param('/turtlebot2/n_observations') """ We set the Observation space for the 6 observations cube_observations = [ round(current_disk_roll_vel, 0), round(y_distance, 1), round(roll, 1), round(pitch, 1), round(y_linear_speed,1), round(yaw, 1), ] """ # Actions and Observations self.linear_forward_speed = rospy.get_param('/turtlebot2/linear_forward_speed') self.linear_turn_speed = rospy.get_param('/turtlebot2/linear_turn_speed') self.angular_speed = rospy.get_param('/turtlebot2/angular_speed') self.init_linear_forward_speed = rospy.get_param('/turtlebot2/init_linear_forward_speed') self.init_linear_turn_speed = rospy.get_param('/turtlebot2/init_linear_turn_speed') self.new_ranges = rospy.get_param('/turtlebot2/new_ranges') self.min_range = rospy.get_param('/turtlebot2/min_range') self.max_laser_value = rospy.get_param('/turtlebot2/max_laser_value') self.min_laser_value = rospy.get_param('/turtlebot2/min_laser_value') # Get Desired Point to Get self.desired_point = Point() self.desired_point.x = rospy.get_param("/turtlebot2/desired_pose/x") self.desired_point.y = rospy.get_param("/turtlebot2/desired_pose/y") self.desired_point.z = rospy.get_param("/turtlebot2/desired_pose/z") # We create two arrays based on the binary values that will be assigned # In the discretization method. laser_scan = self._check_laser_scan_ready() num_laser_readings = len(laser_scan.ranges)/self.new_ranges high = numpy.full((num_laser_readings), self.max_laser_value) low = numpy.full((num_laser_readings), self.min_laser_value) # We only use two integers self.observation_space = spaces.Box(low, high) rospy.logdebug("ACTION SPACES TYPE===>"+str(self.action_space)) rospy.logdebug("OBSERVATION SPACES TYPE===>"+str(self.observation_space)) # Rewards self.forwards_reward = rospy.get_param("/turtlebot2/forwards_reward") self.turn_reward = rospy.get_param("/turtlebot2/turn_reward") self.end_episode_points = rospy.get_param("/turtlebot2/end_episode_points") self.cumulated_steps = 0.0 # Here we will add any init functions prior to starting the MyRobotEnv super(MyTurtleBot2WallEnv, self).__init__() def _set_init_pose(self): """Sets the Robot in its init pose """ self.move_base( self.init_linear_forward_speed, self.init_linear_turn_speed, epsilon=0.05, update_rate=10) return True def _init_env_variables(self): """ Inits variables needed to be initialized each time we reset at the start of an episode. :return: """ # For Info Purposes self.cumulated_reward = 0.0 # Set to false Done, because its calculated asynchronously self._episode_done = False odometry = self.get_odom() self.previous_distance_from_des_point = self.get_distance_from_desired_point(odometry.pose.pose.position) def _set_action(self, action): """ This set action will Set the linear and angular speed of the turtlebot2 based on the action number given. :param action: The action integer that set s what movement to do next. """ rospy.logdebug("Start Set Action ==>"+str(action)) # We convert the actions to speed movements to send to the parent class CubeSingleDiskEnv if action == 0: #FORWARD linear_speed = self.linear_forward_speed angular_speed = 0.0 self.last_action = "FORWARDS" elif action == 1: #LEFT linear_speed = self.linear_turn_speed angular_speed = self.angular_speed self.last_action = "TURN_LEFT" elif action == 2: #RIGHT linear_speed = self.linear_turn_speed angular_speed = -1*self.angular_speed self.last_action = "TURN_RIGHT" # We tell TurtleBot2 the linear and angular speed to set to execute self.move_base(linear_speed, angular_speed, epsilon=0.05, update_rate=10) rospy.logdebug("END Set Action ==>"+str(action)) def _get_obs(self): """ Here we define what sensor data defines our robots observations To know which Variables we have access to, we need to read the TurtleBot2Env API DOCS :return: """ rospy.logdebug("Start Get Observation ==>") # We get the laser scan data laser_scan = self.get_laser_scan() discretized_laser_scan = self.discretize_observation( laser_scan, self.new_ranges ) # We get the odometry so that SumitXL knows where it is. odometry = self.get_odom() x_position = odometry.pose.pose.position.x y_position = odometry.pose.pose.position.y # We round to only two decimals to avoid very big Observation space odometry_array = [round(x_position, 2),round(y_position, 2)] # We only want the X and Y position and the Yaw observations = discretized_laser_scan + odometry_array rospy.logdebug("Observations==>"+str(observations)) rospy.logdebug("END Get Observation ==>") return observations def _is_done(self, observations): if self._episode_done: rospy.logerr("TurtleBot2 is Too Close to wall==>") else: rospy.logerr("TurtleBot2 didnt crash at least ==>") current_position = Point() current_position.x = observations[-2] current_position.y = observations[-1] current_position.z = 0.0 MAX_X = 6.0 MIN_X = -1.0 MAX_Y = 3.0 MIN_Y = -3.0 # We see if we are outside the Learning Space if current_position.x <= MAX_X and current_position.x > MIN_X: if current_position.y <= MAX_Y and current_position.y > MIN_Y: rospy.logdebug("TurtleBot Position is OK ==>["+str(current_position.x)+","+str(current_position.y)+"]") # We see if it got to the desired point if self.is_in_desired_position(current_position): self._episode_done = True else: rospy.logerr("TurtleBot to Far in Y Pos ==>"+str(current_position.x)) self._episode_done = True else: rospy.logerr("TurtleBot to Far in X Pos ==>"+str(current_position.x)) self._episode_done = True return self._episode_done def _compute_reward(self, observations, done): current_position = Point() current_position.x = observations[-2] current_position.y = observations[-1] current_position.z = 0.0 distance_from_des_point = self.get_distance_from_desired_point(current_position) distance_difference = distance_from_des_point - self.previous_distance_from_des_point if not done: if self.last_action == "FORWARDS": reward = self.forwards_reward else: reward = self.turn_reward # If there has been a decrease in the distance to the desired point, we reward it if distance_difference < 0.0: rospy.logwarn("DECREASE IN DISTANCE GOOD") reward += self.forwards_reward else: rospy.logerr("INCREASE IN DISTANCE BAD") reward += 0 else: if self.is_in_desired_position(current_position): reward = self.end_episode_points else: reward = -1*self.end_episode_points self.previous_distance_from_des_point = distance_from_des_point rospy.logdebug("reward=" + str(reward)) self.cumulated_reward += reward rospy.logdebug("Cumulated_reward=" + str(self.cumulated_reward)) self.cumulated_steps += 1 rospy.logdebug("Cumulated_steps=" + str(self.cumulated_steps)) return reward # Internal TaskEnv Methods def discretize_observation(self,data,new_ranges): """ Discards all the laser readings that are not multiple in index of new_ranges value. """ self._episode_done = False discretized_ranges = [] mod = len(data.ranges)/new_ranges rospy.logdebug("data=" + str(data)) rospy.logwarn("new_ranges=" + str(new_ranges)) rospy.logwarn("mod=" + str(mod)) for i, item in enumerate(data.ranges): if (i%mod==0): if item == float ('Inf') or numpy.isinf(item): discretized_ranges.append(self.max_laser_value) elif numpy.isnan(item): discretized_ranges.append(self.min_laser_value) else: discretized_ranges.append(int(item)) if (self.min_range > item > 0): rospy.logerr("done Validation >>> item=" + str(item)+"< "+str(self.min_range)) self._episode_done = True else: rospy.logwarn("NOT done Validation >>> item=" + str(item)+"< "+str(self.min_range)) return discretized_ranges def is_in_desired_position(self,current_position, epsilon=0.05): """ It return True if the current position is similar to the desired position """ is_in_desired_pos = False x_pos_plus = self.desired_point.x + epsilon x_pos_minus = self.desired_point.x - epsilon y_pos_plus = self.desired_point.y + epsilon y_pos_minus = self.desired_point.y - epsilon x_current = current_position.x y_current = current_position.y x_pos_are_close = (x_current <= x_pos_plus) and (x_current > x_pos_minus) y_pos_are_close = (y_current <= y_pos_plus) and (y_current > y_pos_minus) is_in_desired_pos = x_pos_are_close and y_pos_are_close return is_in_desired_pos def get_distance_from_desired_point(self, current_position): """ Calculates the distance from the current position to the desired point :param start_point: :return: """ distance = self.get_distance_from_point(current_position, self.desired_point) return distance def get_distance_from_point(self, pstart, p_end): """ Given a Vector3 Object, get distance from current position :param p_end: :return: """ a = numpy.array((pstart.x, pstart.y, pstart.z)) b = numpy.array((p_end.x, p_end.y, p_end.z)) distance = numpy.linalg.norm(a - b) return distance
[]
2024-01-10
shivangg/tb2_avoid_wall
tb_wall_avoider~scripts~start_qlearning.py
#!/usr/bin/env python import gym import numpy import time import qlearn from gym import wrappers # ROS packages required import rospy import rospkg # import our training environment from openai_ros.task_envs.turtlebot2 import turtlebot2_maze if __name__ == '__main__': rospy.init_node('turtlebot2_maze_qlearn', anonymous=True, log_level=rospy.WARN) # Create the Gym environment env = gym.make('TurtleBot2Maze-v0') rospy.loginfo("Gym environment done") # Set the logging system rospack = rospkg.RosPack() pkg_path = rospack.get_path('tb_wall_avoider') outdir = pkg_path + '/training_results' env = wrappers.Monitor(env, outdir, force=True) rospy.loginfo("Monitor Wrapper started") last_time_steps = numpy.ndarray(0) # Loads parameters from the ROS param server # Parameters are stored in a yaml file inside the config directory # They are loaded at runtime by the launch file Alpha = rospy.get_param("/turtlebot2/alpha") Epsilon = rospy.get_param("/turtlebot2/epsilon") Gamma = rospy.get_param("/turtlebot2/gamma") epsilon_discount = rospy.get_param("/turtlebot2/epsilon_discount") nepisodes = rospy.get_param("/turtlebot2/nepisodes") nsteps = rospy.get_param("/turtlebot2/nsteps") running_step = rospy.get_param("/turtlebot2/running_step") # Initialises the algorithm that we are going to use for learning qlearn = qlearn.QLearn(actions=range(env.action_space.n), alpha=Alpha, gamma=Gamma, epsilon=Epsilon) initial_epsilon = qlearn.epsilon start_time = time.time() highest_reward = 0 # Starts the main training loop: the one about the episodes to do for x in range(nepisodes): rospy.logdebug("############### START EPISODE=>" + str(x)) cumulated_reward = 0 done = False if qlearn.epsilon > 0.05: qlearn.epsilon *= epsilon_discount # Initialize the environment and get first state of the robot observation = env.reset() state = ''.join(map(str, observation)) # Show on screen the actual situation of the robot # env.render() # for each episode, we test the robot for nsteps for i in range(nsteps): rospy.logwarn("############### Start Step=>" + str(i)) # Pick an action based on the current state action = qlearn.chooseAction(state) rospy.logwarn("Next action is:%d", action) # Execute the action in the environment and get feedback observation, reward, done, info = env.step(action) rospy.logwarn(str(observation) + " " + str(reward)) cumulated_reward += reward if highest_reward < cumulated_reward: highest_reward = cumulated_reward nextState = ''.join(map(str, observation)) # Make the algorithm learn based on the results rospy.logwarn("# state we were=>" + str(state)) rospy.logwarn("# action that we took=>" + str(action)) rospy.logwarn("# reward that action gave=>" + str(reward)) rospy.logwarn("# episode cumulated_reward=>" + str(cumulated_reward)) rospy.logwarn("# State in which we will start next step=>" + str(nextState)) qlearn.learn(state, action, reward, nextState) if not (done): rospy.logwarn("NOT DONE") state = nextState else: rospy.logwarn("DONE") last_time_steps = numpy.append(last_time_steps, [int(i + 1)]) break rospy.logwarn("############### END Step=>" + str(i)) #raw_input("Next Step...PRESS KEY") # rospy.sleep(2.0) m, s = divmod(int(time.time() - start_time), 60) h, m = divmod(m, 60) rospy.logerr(("EP: " + str(x + 1) + " - [alpha: " + str(round(qlearn.alpha, 2)) + " - gamma: " + str( round(qlearn.gamma, 2)) + " - epsilon: " + str(round(qlearn.epsilon, 2)) + "] - Reward: " + str( cumulated_reward) + " Time: %d:%02d:%02d" % (h, m, s))) rospy.loginfo(("\n|" + str(nepisodes) + "|" + str(qlearn.alpha) + "|" + str(qlearn.gamma) + "|" + str( initial_epsilon) + "*" + str(epsilon_discount) + "|" + str(highest_reward) + "| PICTURE |")) l = last_time_steps.tolist() l.sort() # print("Parameters: a="+str) rospy.loginfo("Overall score: {:0.2f}".format(last_time_steps.mean())) rospy.loginfo("Best 100 score: {:0.2f}".format(reduce(lambda x, y: x + y, l[-100:]) / len(l[-100:]))) env.close()
[]
2024-01-10
shivangg/tb2_avoid_wall
openai_ros~openai_ros~src~openai_ros~task_envs~cartpole_stay_up~stay_up.py
from gym import utils from openai_ros.robot_envs import cartpole_env from gym.envs.registration import register from gym import error, spaces import rospy import math import numpy as np # The path is __init__.py of openai_ros, where we import the MovingCubeOneDiskWalkEnv directly register( id='CartPoleStayUp-v0', entry_point='openai_ros:task_envs.cartpole_stay_up.stay_up.CartPoleStayUpEnv', timestep_limit=1000, ) class CartPoleStayUpEnv(cartpole_env.CartPoleEnv): def __init__(self): self.get_params() self.action_space = spaces.Discrete(self.n_actions) high = np.array([ 2.5 * 2, np.finfo(np.float32).max, 0.7 * 2, np.finfo(np.float32).max]) self.observation_space = spaces.Box(-high, high) cartpole_env.CartPoleEnv.__init__( self, control_type=self.control_type ) def get_params(self): #get configuration parameters self.n_actions = rospy.get_param('/cartpole_v0/n_actions') self.min_pole_angle = rospy.get_param('/cartpole_v0/min_pole_angle') self.max_pole_angle = rospy.get_param('/cartpole_v0/max_pole_angle') self.max_base_velocity = rospy.get_param('/cartpole_v0/max_base_velocity') self.min_base_pose_x = rospy.get_param('/cartpole_v0/min_base_pose_x') self.max_base_pose_x = rospy.get_param('/cartpole_v0/max_base_pose_x') self.pos_step = rospy.get_param('/cartpole_v0/pos_step') self.running_step = rospy.get_param('/cartpole_v0/running_step') self.init_pos = rospy.get_param('/cartpole_v0/init_pos') self.wait_time = rospy.get_param('/cartpole_v0/wait_time') self.control_type = rospy.get_param('/cartpole_v0/control_type') def _set_action(self, action): # Take action if action == 0: #LEFT rospy.loginfo("GO LEFT...") self.pos[0] -= self.pos_step elif action == 1: #RIGHT rospy.loginfo("GO RIGHT...") self.pos[0] += self.pos_step elif action == 2: #LEFT BIG rospy.loginfo("GO LEFT BIG...") self.pos[0] -= self.pos_step * 10 elif action == 3: #RIGHT BIG rospy.loginfo("GO RIGHT BIG...") self.pos[0] += self.pos_step * 10 # Apply action to simulation. rospy.loginfo("MOVING TO POS=="+str(self.pos)) # 1st: unpause simulation #rospy.logdebug("Unpause SIM...") #self.gazebo.unpauseSim() self.move_joints(self.pos) rospy.logdebug("Wait for some time to execute movement, time="+str(self.running_step)) rospy.sleep(self.running_step) #wait for some time rospy.logdebug("DONE Wait for some time to execute movement, time=" + str(self.running_step)) # 3rd: pause simulation #rospy.logdebug("Pause SIM...") #self.gazebo.pauseSim() def _get_obs(self): data = self.joints # base_postion base_velocity pole angle pole velocity #obs = [round(data.position[1],1), round(data.velocity[1],1), round(data.position[0],1), round(data.velocity[0],1)] obs = [data.position[1], data.velocity[1], data.position[0], data.velocity[0]] return np.array(obs) def _is_done(self, observations): done = False data = self.joints rospy.loginfo("BASEPOSITION=="+str(observations[0])) rospy.loginfo("POLE ANGLE==" + str(observations[2])) if (self.min_base_pose_x >= observations[0] or observations[0] >= self.max_base_pose_x): #check if the base is still within the ranges of (-2, 2) rospy.logerr("Base Outside Limits==>min="+str(self.min_base_pose_x)+",pos="+str(observations[0])+",max="+str(self.max_base_pose_x)) done = True if (self.min_pole_angle >= observations[2] or observations[2] >= self.max_pole_angle): #check if pole has toppled over rospy.logerr( "Pole Angle Outside Limits==>min=" + str(self.min_pole_angle) + ",pos=" + str(observations[2]) + ",max=" + str( self.max_pole_angle)) done = True rospy.loginfo("FINISHED get _is_done") return done def _compute_reward(self, observations, done): """ Gives more points for staying upright, gets data from given observations to avoid having different data than other previous functions :return:reward """ rospy.logdebug("START _compute_reward") if not done: reward = 1.0 elif self.steps_beyond_done is None: # Pole just fell! self.steps_beyond_done = 0 reward = 1.0 else: if self.steps_beyond_done == 0: logger.warning("You are calling 'step()' even though this environment has already returned done = True. You should always call 'reset()' once you receive 'done = True' -- any further steps are undefined behavior.") self.steps_beyond_done += 1 reward = 0.0 rospy.logdebug("END _compute_reward") return reward def _init_env_variables(self): """ Inits variables needed to be initialised each time we reset at the start of an episode. :return: """ self.steps_beyond_done = None def _set_init_pose(self): """ Sets joints to initial position [0,0,0] :return: """ self.check_publishers_connection() # Reset Internal pos variable self.init_internal_vars(self.init_pos) self.move_joints(self.pos)
[]
2024-01-10
shivangg/tb2_avoid_wall
openai_ros~openai_ros~src~openai_ros~robot_envs~sawyer_env.py
import numpy import rospy import time import tf from openai_ros import robot_gazebo_env import intera_interface import intera_external_devices from intera_interface import CHECK_VERSION from intera_core_msgs.msg import JointLimits from sensor_msgs.msg import Image class SawyerEnv(robot_gazebo_env.RobotGazeboEnv): """Superclass for all SawyerEnv environments. """ def __init__(self): """ Initializes a new SawyerEnv environment. To check any topic we need to have the simulations running, we need to do two things: 1) Unpause the simulation: without that th stream of data doesnt flow. This is for simulations that are pause for whatever the reason 2) If the simulation was running already for some reason, we need to reset the controlers. This has to do with the fact that some plugins with tf, dont understand the reset of the simulation and need to be reseted to work properly. The Sensors: The sensors accesible are the ones considered usefull for AI learning. Sensor Topic List: * /robot/joint_limits: Odometry of the Base of Wamv Actuators Topic List: * As actuator we will use a class to interface with the movements through commands. Args: """ rospy.logdebug("Start SawyerEnv INIT...") # Variables that we give through the constructor. # None in this case # Internal Vars # Doesnt have any accesibles self.controllers_list = [] # It doesnt use namespace self.robot_name_space = "" # We launch the init function of the Parent Class robot_gazebo_env.RobotGazeboEnv super(SawyerEnv, self).__init__(controllers_list=self.controllers_list, robot_name_space=self.robot_name_space, reset_controls=False, start_init_physics_parameters=False, reset_world_or_sim="WORLD") rospy.logdebug("SawyerEnv unpause...") self.gazebo.unpauseSim() #self.controllers_object.reset_controllers() # TODO: Fill it with the sensors self._check_all_systems_ready() rospy.Subscriber("/io/internal_camera/head_camera/image_raw", Image, self._head_camera_image_raw_callback) rospy.Subscriber("/io/internal_camera/right_hand_camera/image_raw", Image, self._right_hand_camera_image_raw_callback) self._setup_tf_listener() self._setup_movement_system() self.gazebo.pauseSim() rospy.logdebug("Finished SawyerEnv INIT...") # Methods needed by the RobotGazeboEnv # ---------------------------- def _check_all_systems_ready(self): """ Checks that all the sensors, publishers and other simulation systems are operational. """ rospy.logdebug("SawyerEnv check_all_systems_ready...") self._check_all_sensors_ready() rospy.logdebug("END SawyerEnv _check_all_systems_ready...") return True # CubeSingleDiskEnv virtual methods # ---------------------------- def _check_all_sensors_ready(self): rospy.logdebug("START ALL SENSORS READY") # TODO: Here go the sensors like cameras and joint states self._check_head_camera_image_raw_ready() self._check_right_hand_camera_image_raw_ready() rospy.logdebug("ALL SENSORS READY") def _check_head_camera_image_raw_ready(self): self.head_camera_image_raw = None rospy.logdebug("Waiting for /io/internal_camera/head_camera/image_raw to be READY...") while self.head_camera_image_raw is None and not rospy.is_shutdown(): try: self.head_camera_image_raw = rospy.wait_for_message("/io/internal_camera/head_camera/image_raw", Image, timeout=5.0) rospy.logdebug("Current /io/internal_camera/head_camera/image_raw READY=>") except: rospy.logerr("Current /io/internal_camera/head_camera/image_raw not ready yet, retrying for getting head_camera_image_raw") return self.head_camera_image_raw def _check_right_hand_camera_image_raw_ready(self): self.right_hand_camera_image_raw = None rospy.logdebug("Waiting for /io/internal_camera/right_hand_camera/image_raw to be READY...") while self.right_hand_camera_image_raw is None and not rospy.is_shutdown(): try: self.right_hand_camera_image_raw = rospy.wait_for_message("/io/internal_camera/right_hand_camera/image_raw", Image, timeout=5.0) rospy.logdebug("Current /io/internal_camera/right_hand_camera/image_raw READY=>") except: rospy.logerr("Current /io/internal_camera/right_hand_camera/image_raw not ready yet, retrying for getting right_hand_camera_image_raw") return self.right_hand_camera_image_raw def _head_camera_image_raw_callback(self, data): self.head_camera_image_raw = data def _right_hand_camera_image_raw_callback(self, data): self.right_hand_camera_image_raw = data def _setup_tf_listener(self): """ Set ups the TF listener for getting the transforms you ask for. """ self.listener = tf.TransformListener() def _setup_movement_system(self): """ Setup of the movement system. :return: """ rp = intera_interface.RobotParams() valid_limbs = rp.get_limb_names() if not valid_limbs: rp.log_message(("Cannot detect any limb parameters on this robot. " "Exiting."), "ERROR") return rospy.loginfo("Valid Sawyer Limbs==>"+str(valid_limbs)) print("Getting robot state... ") rs = intera_interface.RobotEnable(CHECK_VERSION) init_state = rs.state().enabled rospy.loginfo("Enabling robot...") rs.enable() self._map_actions_to_movement() def _map_actions_to_movement(self, side="right", joint_delta=0.1): self.limb = intera_interface.Limb(side) try: self.gripper = intera_interface.Gripper(side + '_gripper') except: self.has_gripper = False rospy.loginfo("The electric gripper is not detected on the robot.") else: self.has_gripper = True self.joints = self.limb.joint_names() self.bindings = { self.joints[0]+"_increase": (self.set_j, [self.joints[0], joint_delta], self.joints[0]+" increase"), self.joints[0]+"_decrease": (self.set_j, [self.joints[0], -joint_delta], self.joints[0]+" decrease"), self.joints[1]+"_increase": (self.set_j, [self.joints[1], joint_delta], self.joints[1]+" increase"), self.joints[1]+"_decrease": (self.set_j, [self.joints[1], -joint_delta], self.joints[1]+" decrease"), self.joints[2]+"_increase": (self.set_j, [self.joints[2], joint_delta], self.joints[2]+" increase"), self.joints[2]+"_decrease": (self.set_j, [self.joints[2], -joint_delta], self.joints[2]+" decrease"), self.joints[3]+"_increase": (self.set_j, [self.joints[3], joint_delta], self.joints[3]+" increase"), self.joints[3]+"_decrease": (self.set_j, [self.joints[3], -joint_delta], self.joints[3]+" decrease"), self.joints[4]+"_increase": (self.set_j, [self.joints[4], joint_delta], self.joints[4]+" increase"), self.joints[4]+"_decrease": (self.set_j, [self.joints[4], -joint_delta], self.joints[4]+" decrease"), self.joints[5]+"_increase": (self.set_j, [self.joints[5], joint_delta], self.joints[5]+" increase"), self.joints[5]+"_decrease": (self.set_j, [self.joints[5], -joint_delta], self.joints[5]+" decrease"), self.joints[6]+"_increase": (self.set_j, [self.joints[6], joint_delta], self.joints[6]+" increase"), self.joints[6]+"_decrease": (self.set_j, [self.joints[6], -joint_delta], self.joints[6]+" decrease") } if self.has_gripper: self.bindings.update({ "close": (self.set_g, "close", side+" gripper close"), "open": (self.set_g, "open", side+" gripper open"), "calibrate": (self.set_g, "calibrate", side+" gripper calibrate") }) rospy.loginfo("Controlling joints...") # Methods that the TrainingEnvironment will need to define here as virtual # because they will be used in RobotGazeboEnv GrandParentClass and defined in the # TrainingEnvironment. # ---------------------------- def _set_init_pose(self): """Sets the Robot in its init pose """ raise NotImplementedError() def _init_env_variables(self): """Inits variables needed to be initialised each time we reset at the start of an episode. """ raise NotImplementedError() def _compute_reward(self, observations, done): """Calculates the reward to give based on the observations given. """ raise NotImplementedError() def _set_action(self, action): """Applies the given action to the simulation. """ raise NotImplementedError() def _get_obs(self): raise NotImplementedError() def _is_done(self, observations): """Checks if episode done based on observations given. """ raise NotImplementedError() # Methods that the TrainingEnvironment will need. # ---------------------------- def execute_movement(self, action_id): """ It executed the command given through an id. This will move any joint of Sawyer, including the gripper if it has it. :param: action_id: These are the possible action_id values and the action asociated. self.joints[0]+"_increase", self.joints[0]+_decrease, self.joints[1]+"_increase", self.joints[1]+"_decrease", self.joints[2]+"_increase", self.joints[2]+"_decrease", self.joints[3]+"_increase", self.joints[3]+"_decrease", self.joints[4]+"_increase", self.joints[4]+"_decrease", self.joints[5]+"_increase", self.joints[5]+"_decrease", self.joints[6]+"_increase", self.joints[6]+"_decrease", gripper_close, gripper_open, gripper_calibrate """ if action_id in self.bindings: cmd = self.bindings[action_id] if action_id == "gripper_close" or action_id == "gripper_open" or action_id == "gripper_calibrate": cmd[0](cmd[1]) rospy.loginfo("command: %s" % (cmd[2],)) else: #expand binding to something like "self.set_j(right, 'j0', joint_delta)" cmd[0](*cmd[1]) rospy.loginfo("command: %s" % (cmd[2],)) else: rospy.logerr("NOT VALID key binding, it should be one of these: ") for key, val in sorted(self.bindings.items(), key=lambda x: x[1][2]): rospy.logerr(" %s: %s" % (key, val[2])) def set_j(self,joint_name, delta): current_position = self.limb.joint_angle(joint_name) joint_command = {joint_name: current_position + delta} self.limb.set_joint_positions(joint_command) def set_g(self,action): if self.has_gripper: if action == "close": self.gripper.close() elif action == "open": self.gripper.open() elif action == "calibrate": self.gripper.calibrate() def move_joints_to_angle_blocking(self,joint_positions_dict, timeout=15.0, threshold=0.008726646): """ It moves all the joints to the given position and doesnt exit until it reaches that position """ self.limb.move_to_joint_positions( positions=joint_positions_dict, timeout=15.0, threshold=0.008726646, test=None) def get_limb_joint_names_array(self): """ Returns the Joint Names array of the Limb. """ return self.joints def get_all_limb_joint_angles(self): """ Return dictionary dict({str:float}) with all the joints angles """ return self.limb.joint_angles() def get_all_limb_joint_efforts(self): """ Returns a dictionary dict({str:float}) with all the joints efforts """ return self.limb.joint_efforts() def get_tf_start_to_end_frames(self,start_frame_name, end_frame_name): """ Given two frames, it returns the transform from the start_frame_name to the end_frame_name. It will only return something different to None if the TFs of the Two frames are in TF topic published and are connected through the TF tree. :param: start_frame_name: Start Frame of the TF transform end_frame_name: End Frame of the TF transform :return: trans,rot of the transform between the start and end frames. """ start_frame = "/"+start_frame_name end_frame = "/"+end_frame_name trans,rot = None, None try: (trans,rot) = self.listener.lookupTransform(start_frame, end_frame, rospy.Time(0)) except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException): rospy.logerr("TF start to end not ready YET...") pass return trans,rot def check_joint_limits_ready(self): self.joint_limits = None rospy.logdebug("Waiting for /robot/joint_limits to be READY...") while self.joint_limits is None and not rospy.is_shutdown(): try: self.joint_limits = rospy.wait_for_message("/robot/joint_limits", JointLimits, timeout=3.0) rospy.logdebug("Current /robot/joint_limits READY=>") except: rospy.logerr("Current /robot/joint_limits not ready yet, retrying for getting joint_limits") return self.joint_limits def get_joint_limits(self): return self.joint_limits def get_head_camera_image_raw(self): return self.head_camera_image_raw def get_right_hand_camera_image_raw(self): return self.right_hand_camera_image_raw def init_joint_limits(self): """ Get the Joint Limits, in the init fase where we need to unpause the simulation to get them :return: joint_limits: The Joint Limits Dictionary, with names, angles, vel and effort limits. """ self.gazebo.unpauseSim() joint_limits = self.check_joint_limits_ready() self.gazebo.pauseSim() return joint_limits
[]
2024-01-10
shivangg/tb2_avoid_wall
tb_wall_avoider~scripts~my_turtlebot2_maze.py
import rospy import numpy import time from gym import spaces from openai_ros.robot_envs import turtlebot2_env from gym.envs.registration import register timestep_limit_per_episode = 10000 # Can be any Value register( id='MyTurtleBot2Maze-v0', entry_point='my_turtlebot2_maze:MyTurtleBot2MazeEnv', timestep_limit=timestep_limit_per_episode, ) class MyTurtleBot2MazeEnv(turtlebot2_env.TurtleBot2Env): def __init__(self): """ This Task Env is designed for having the TurtleBot2 in some kind of maze. It will learn how to move around the maze without crashing. """ # Only variable needed to be set here number_actions = rospy.get_param('/turtlebot2/n_actions') self.action_space = spaces.Discrete(number_actions) # We set the reward range, which is not compulsory but here we do it. self.reward_range = (-numpy.inf, numpy.inf) #number_observations = rospy.get_param('/turtlebot2/n_observations') """ We set the Observation space for the 6 observations cube_observations = [ round(current_disk_roll_vel, 0), round(y_distance, 1), round(roll, 1), round(pitch, 1), round(y_linear_speed,1), round(yaw, 1), ] """ # Actions and Observations self.linear_forward_speed = rospy.get_param('/turtlebot2/linear_forward_speed') self.linear_turn_speed = rospy.get_param('/turtlebot2/linear_turn_speed') self.angular_speed = rospy.get_param('/turtlebot2/angular_speed') self.init_linear_forward_speed = rospy.get_param('/turtlebot2/init_linear_forward_speed') self.init_linear_turn_speed = rospy.get_param('/turtlebot2/init_linear_turn_speed') self.number_of_sectors = rospy.get_param('/turtlebot2/number_of_sectors') self.min_range = rospy.get_param('/turtlebot2/min_range') self.middle_range = rospy.get_param('/turtlebot2/middle_range') self.danger_laser_value = rospy.get_param('/turtlebot2/danger_laser_value') self.middle_laser_value = rospy.get_param('/turtlebot2/middle_laser_value') self.safe_laser_value = rospy.get_param('/turtlebot2/safe_laser_value') # We create two arrays based on the binary values that will be assigned # In the discretization method. high = numpy.full((self.number_of_sectors), self.danger_laser_value) low = numpy.full((self.number_of_sectors), self.safe_laser_value) # We only use two integers self.observation_space = spaces.Box(low, high) rospy.logdebug("ACTION SPACES TYPE===>"+str(self.action_space)) rospy.logdebug("OBSERVATION SPACES TYPE===>"+str(self.observation_space)) # Rewards self.forwards_reward = rospy.get_param("/turtlebot2/forwards_reward") self.turn_reward = rospy.get_param("/turtlebot2/turn_reward") self.end_episode_points = rospy.get_param("/turtlebot2/end_episode_points") self.cumulated_steps = 0.0 # Here we will add any init functions prior to starting the MyRobotEnv super(MyTurtleBot2MazeEnv, self).__init__() def _set_init_pose(self): """Sets the Robot in its init pose """ self.move_base( self.init_linear_forward_speed, self.init_linear_turn_speed, epsilon=0.05, update_rate=10) return True def _init_env_variables(self): """ Inits variables needed to be initialised each time we reset at the start of an episode. :return: """ # For Info Purposes self.cumulated_reward = 0.0 # This is necessary to give the laser sensors to refresh in the new reseted position. rospy.logwarn("Waiting...") time.sleep(0.5) rospy.logwarn("END Waiting...") def _set_action(self, action): """ This set action will Set the linear and angular speed of the turtlebot2 based on the action number given. :param action: The action integer that set s what movement to do next. """ rospy.logdebug("Start Set Action ==>"+str(action)) # We convert the actions to speed movements to send to the parent class CubeSingleDiskEnv if action == 0: #FORWARD linear_speed = self.linear_forward_speed angular_speed = 0.0 self.last_action = "FORWARDS" elif action == 1: #LEFT linear_speed = self.linear_turn_speed angular_speed = self.angular_speed self.last_action = "TURN_LEFT" elif action == 2: #RIGHT linear_speed = self.linear_turn_speed angular_speed = -1*self.angular_speed self.last_action = "TURN_RIGHT" elif action == 3: #RIGHT linear_speed = -self.linear_turn_speed angular_speed = 0.0 self.last_action = "BACKWARDS" # We tell TurtleBot2 the linear and angular speed to set to execute self.move_base(linear_speed, angular_speed, epsilon=0.05, update_rate=10) rospy.logdebug("END Set Action ==>"+str(action)) def _get_obs(self): """ Here we define what sensor data defines our robots observations To know which Variables we have acces to, we need to read the TurtleBot2Env API DOCS :return: """ rospy.logdebug("Start Get Observation ==>") # We get the laser scan data laser_scan = self.get_laser_scan() discretized_observations = self.discretize_observation( laser_scan, self.number_of_sectors ) rospy.logdebug("Observations==>"+str(discretized_observations)) rospy.logdebug("END Get Observation ==>") return discretized_observations def _is_done(self, observations): episode_done = not (self.check_laser_sector_readings_safe(observations)) if episode_done: rospy.logerr("TurtleBot2 is Too Close to wall==>") else: rospy.logerr("TurtleBot2 is Ok ==>") return episode_done def _compute_reward(self, observations, done): if not done: if self.last_action == "FORWARDS": reward = self.forwards_reward elif self.last_action == "BACKWARDS": reward = -1*self.forwards_reward else: reward = self.turn_reward else: reward = -1*self.end_episode_points rospy.logdebug("reward=" + str(reward)) self.cumulated_reward += reward rospy.logdebug("Cumulated_reward=" + str(self.cumulated_reward)) self.cumulated_steps += 1 rospy.logdebug("Cumulated_steps=" + str(self.cumulated_steps)) return reward # Internal TaskEnv Methods def discretize_observation(self,laser_data,number_of_sectors): """ Discards all the laser readings that are not multiple in index of number_of_sectors value. """ base = len(laser_data.ranges)/number_of_sectors current_sector = -1 sector_readings = [self.safe_laser_value]*number_of_sectors for i, item in enumerate(laser_data.ranges): #rospy.logwarn("#### S ###") #rospy.logwarn(str(i)) #rospy.logwarn(str(item)) rest_is_zero = (i%base==0) if rest_is_zero: rospy.logwarn("CHANGE SECTOR="+str(rest_is_zero)) current_sector += 1 else: rospy.loginfo("NO CHANGE SECTOR="+str(rest_is_zero)) if numpy.isnan(item): rospy.logerr(">>>>>>>>>>>>NAN VALUE=>>>"+str(item)) elif (self.min_range >= item ): sector_readings[current_sector] = self.danger_laser_value elif (self.middle_range >= item > self.min_range): sector_readings[current_sector] = self.middle_laser_value return sector_readings def check_laser_sector_readings_safe(self, laser_sector_readings): """ Checks if all the sector readings have the self.safe_laser_value of self.middle_laser_value """ readings_safe = all((c != self.danger_laser_value) for c in laser_sector_readings) rospy.logwarn("laser_sector_readings=>>>"+str(laser_sector_readings)) rospy.logwarn("readings_safe=>>>"+str(readings_safe)) return readings_safe
[]
2024-01-10
microsoft/Megatron-DeepSpeed
megatron~model~bert_model.py
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. """BERT model.""" import torch from megatron import get_args from megatron.core import tensor_parallel from megatron.model.enums import AttnMaskType from megatron.model.language_model import parallel_lm_logits from megatron.model.language_model import get_language_model from megatron.model import LayerNorm from megatron.model.utils import openai_gelu, erf_gelu from megatron.model.utils import get_linear_layer from megatron.model.utils import init_method_normal from megatron.model.utils import scaled_init_method_normal from .module import MegatronModule def bert_extended_attention_mask(attention_mask): # We create a 3D attention mask from a 2D tensor mask. # [b, 1, s] attention_mask_b1s = attention_mask.unsqueeze(1) # [b, s, 1] attention_mask_bs1 = attention_mask.unsqueeze(2) # [b, s, s] attention_mask_bss = attention_mask_b1s * attention_mask_bs1 # [b, 1, s, s] extended_attention_mask = attention_mask_bss.unsqueeze(1) # Convert attention mask to binary: extended_attention_mask = (extended_attention_mask < 0.5) return extended_attention_mask def bert_position_ids(token_ids): # Create position ids seq_length = token_ids.size(1) position_ids = torch.arange(seq_length, dtype=torch.long, device=token_ids.device) position_ids = position_ids.unsqueeze(0).expand_as(token_ids) return position_ids class BertLMHead(MegatronModule): """Masked LM head for Bert Arguments: config: TransformerConfig object mpu_vocab_size: model parallel size of vocabulary. hidden_size: hidden size parallel_output: whether output logits being distributed or not. """ def __init__(self, mpu_vocab_size, hidden_size, config, parallel_output): super().__init__(config=config) args = get_args() self.bias = torch.nn.Parameter(torch.zeros(mpu_vocab_size)) tensor_parallel.set_tensor_model_parallel_attributes(self.bias, True, 0, 1) self.parallel_output = parallel_output self.dense = get_linear_layer(hidden_size, hidden_size, config.init_method, gather_params_on_init=args.zero_stage == 3) setattr(self.dense.weight, 'sequence_parallel', config.sequence_parallel) setattr(self.dense.bias, 'sequence_parallel', config.sequence_parallel) self.layernorm = LayerNorm(hidden_size, eps=config.layernorm_epsilon, sequence_parallel=config.sequence_parallel) self.gelu = torch.nn.functional.gelu if args.openai_gelu: self.gelu = openai_gelu elif args.onnx_safe: self.gelu = erf_gelu def forward(self, hidden_states, word_embeddings_weight): hidden_states = self.dense(hidden_states) hidden_states = self.gelu(hidden_states) hidden_states = self.layernorm(hidden_states) output = parallel_lm_logits(hidden_states, word_embeddings_weight, self.parallel_output, bias=self.bias) return output def post_language_model_processing(lm_output, pooled_output, lm_head, binary_head, lm_labels, logit_weights, fp16_lm_cross_entropy): # Output. lm_logits = lm_head( lm_output, logit_weights) binary_logits = None if binary_head is not None: binary_logits = binary_head(pooled_output) if lm_labels is None: # [s b h] => [b s h] return lm_logits.transpose(0,1).contiguous(), binary_logits else: # [b s] => [s b] lm_labels = lm_labels.transpose(0,1).contiguous() # lm_logits : [s, b, h] and lm_labels: [s, b] if fp16_lm_cross_entropy: assert lm_logits.dtype == torch.half lm_loss = tensor_parallel.vocab_parallel_cross_entropy(lm_logits, lm_labels) else: lm_loss = tensor_parallel.vocab_parallel_cross_entropy(lm_logits.float(), lm_labels) # [s, b] => [b s] lm_loss = lm_loss.transpose(0,1).contiguous() return lm_loss, binary_logits class BertModel(MegatronModule): """Bert Language model.""" def __init__(self, config, num_tokentypes=2, add_binary_head=True, parallel_output=True, pre_process=True, post_process=True, return_moe_loss=False): super().__init__(config=config) args = get_args() # TODO this option is not yet implemented in BERT assert args.untie_embeddings_and_output_weights is False self.fp16_lm_cross_entropy = args.fp16_lm_cross_entropy self.add_binary_head = add_binary_head self.parallel_output = parallel_output self.pre_process = pre_process self.post_process = post_process self.return_moe_loss = return_moe_loss self.return_embeddings = args.output_bert_embeddings if self.return_embeddings: assert self.post_process and self.add_binary_head self.language_model, self._language_model_key = get_language_model( config=config, num_tokentypes=num_tokentypes, add_pooler=self.add_binary_head, encoder_attn_mask_type=AttnMaskType.padding, pre_process=self.pre_process, post_process=self.post_process, num_experts=args.num_experts, ) self.initialize_word_embeddings() if self.post_process: self.lm_head = BertLMHead(self.shared_embedding_or_output_weight().size(0), config.hidden_size, config, parallel_output) self._lm_head_key = 'lm_head' self.binary_head = None if self.add_binary_head: self.binary_head = get_linear_layer(config.hidden_size, 2, config.init_method, args.zero_stage == 3) self._binary_head_key = 'binary_head' def set_input_tensor(self, input_tensor): """See megatron.model.transformer.set_input_tensor()""" self.language_model.set_input_tensor(input_tensor) def forward(self, bert_model_input, attention_mask, tokentype_ids=None, lm_labels=None): extended_attention_mask = bert_extended_attention_mask(attention_mask) input_ids = bert_model_input position_ids = bert_position_ids(input_ids) lm_output = self.language_model( input_ids, position_ids, extended_attention_mask, tokentype_ids=tokentype_ids ) if self.post_process and self.add_binary_head: lm_output, pooled_output, moe_losses = lm_output # Return pooled output (e.g., when computing Bert embeddings). if self.return_embeddings: # Sum attention mask. embeddings = torch.transpose(lm_output, 0, 1) masks = torch.sum(attention_mask, dim=1) # Collect masked embeddings. output = torch.zeros( size=(embeddings.shape[0], embeddings.shape[2]), dtype=torch.float32, device=torch.cuda.current_device()) for i, (embedding, mask) in enumerate(zip(embeddings, masks)): output[i, :] = torch.mean(embedding[1: mask - 1], dim=0) return output else: pooled_output = None if self.post_process: if not self.add_binary_head: lm_output, moe_losses = lm_output lm_output = post_language_model_processing(lm_output, pooled_output, self.lm_head, self.binary_head, lm_labels, self.shared_embedding_or_output_weight(), self.fp16_lm_cross_entropy) return *lm_output, moe_losses if self.return_moe_loss else lm_output else: return lm_output def state_dict_for_save_checkpoint(self, prefix='', keep_vars=False): """For easy load when model is combined with other heads, add an extra key.""" state_dict_ = {} state_dict_[self._language_model_key] \ = self.language_model.state_dict_for_save_checkpoint(prefix=prefix, keep_vars=keep_vars) if self.post_process: state_dict_[self._lm_head_key] \ = self.lm_head.state_dict_for_save_checkpoint(prefix=prefix, keep_vars=keep_vars) if self.post_process and self.add_binary_head: state_dict_[self._binary_head_key] \ = self.binary_head.state_dict(prefix=prefix, keep_vars=keep_vars) # Save word_embeddings. if self.post_process and not self.pre_process: state_dict_[self._word_embeddings_for_head_key] \ = self.word_embeddings.state_dict(prefix=prefix, keep_vars=keep_vars) return state_dict_ def load_state_dict(self, state_dict, strict=True): """Customized load.""" self.language_model.load_state_dict( state_dict[self._language_model_key], strict=strict) if self.post_process: self.lm_head.load_state_dict( state_dict[self._lm_head_key], strict=strict) if self.post_process and self.add_binary_head: self.binary_head.load_state_dict( state_dict[self._binary_head_key], strict=strict) # Load word_embeddings. if self.post_process and not self.pre_process: self.word_embeddings.load_state_dict( state_dict[self._word_embeddings_for_head_key], strict=strict)
[]
2024-01-10
farazn019/Researcher-Agent
research_Agent.py
import requests from bs4 import BeautifulSoup from dotenv import load_dotenv from langchain.tools import Tool, DuckDuckGoSearchResults from langchain.prompts import PromptTemplate from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain from langchain.agents import initialize_agent, AgentType import streamlit as st load_dotenv() duckduckgo_search = DuckDuckGoSearchResults() Headers = { 'user-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' } def parse_html_content(input_content) -> str: html_output = BeautifulSoup(input_content, 'html.parser') html_content = html_output.get_text() return html_content def get_website_page(input_url) -> str: response = requests.get(input_url, headers=Headers) return parse_html_content(response.content) web_fetcher = Tool.from_function ( func=get_website_page, name="WebFetcher", description="Retrieves the content of a web page" ) template_prompt = "Summarize this content: {content}" large_language_model = ChatOpenAI(model="gpt-3.5-turbo-16k") large_language_model_chain = LLMChain( llm = large_language_model, prompt=PromptTemplate.from_template(template_prompt) ) summarize_tool = Tool.from_function( func = large_language_model_chain.run, name = "Summarization Tool", description = "This summarizes the contents of a website" ) tools = [duckduckgo_search, web_fetcher, summarize_tool] research_agent = initialize_agent( tools = tools, agent_type = AgentType.ZERO_SHOT_REACT_DESCRIPTION, llm = large_language_model, verbose = True ) #prompt = "Research how to grow out a business using newsletters. Use your tools to search and summarize content into a guide on how to use the newsletters to effectively grow a business." #prompt = open() with open("sample_prompt.txt", "r") as prompt: prompt_input = prompt.read() #print(prompt_input) print(research_agent.run(prompt_input)) #prompt_input_text = st.text_input('Enter Prompt', 'Please enter your prompt') #st.write('The current title is, ' + prompt_input_text) #print(research_agent.run(prompt_input_text))
[ "Summarize this content: {content}" ]
2024-01-10
FraserLee/AlignmentSearch
api~get_blocks.py
from typing import List, Tuple import dataclasses import datetime import itertools import numpy as np import openai import regex as re import requests import time # ---------------------------------- constants --------------------------------- EMBEDDING_MODEL = "text-embedding-ada-002" # ------------------------------------ types ----------------------------------- @dataclasses.dataclass class Block: title: str author: str date: str url: str tags: str text: str # ------------------------------------------------------------------------------ # Get the embedding for a given text. The function will retry with exponential backoff if the API rate limit is reached, up to 4 times. def get_embedding(text: str) -> np.ndarray: max_retries = 4 max_wait_time = 10 attempt = 0 while True: try: result = openai.Embedding.create(model=EMBEDDING_MODEL, input=text) return result["data"][0]["embedding"] except openai.error.RateLimitError as e: attempt += 1 if attempt > max_retries: raise e time.sleep(min(max_wait_time, 2 ** attempt)) # Get the k blocks most semantically similar to the query using Pinecone. def get_top_k_blocks(index, user_query: str, k: int) -> List[Block]: # Default to querying embeddings from live website if pinecone url not # present in .env # # This helps people getting started developing or messing around with the # site, since setting up a vector DB with the embeddings is by far the # hardest part for those not already on the team. if index is None: print('Pinecone index not found, performing semantic search on alignmentsearch-api.up.railway.app endpoint.') response = requests.post( "https://alignmentsearch-api.up.railway.app/semantic", json = { "query": user_query, "k": k } ) return [Block(**block) for block in response.json()] # print time t = time.time() # Get the embedding for the query. query_embedding = get_embedding(user_query) t1 = time.time() print("Time to get embedding: ", t1 - t) query_response = index.query( namespace="alignment-search", # ugly, sorry top_k=k, include_values=False, include_metadata=True, vector=query_embedding ) blocks = [] for match in query_response['matches']: date = match['metadata']['date'] if type(date) == datetime.date: date = date.strftime("%Y-%m-%d") # iso8601 blocks.append(Block( title = match['metadata']['title'], author = match['metadata']['author'], date = date, url = match['metadata']['url'], tags = match['metadata']['tags'], text = strip_block(match['metadata']['text']) )) t2 = time.time() print("Time to get top-k blocks: ", t2 - t1) # for all blocks that are "the same" (same title, author, date, url, tags), # combine their text with "....." in between. Return them in order such # that the combined block has the minimum index of the blocks combined. key = lambda bi: (bi[0].title or "", bi[0].author or "", bi[0].date or "", bi[0].url or "", bi[0].tags or "") blocks_plus_old_index = [(block, i) for i, block in enumerate(blocks)] blocks_plus_old_index.sort(key=key) unified_blocks: List[Tuple[Block, int]] = [] for key, group in itertools.groupby(blocks_plus_old_index, key=key): group = list(group) if len(group) == 0: continue group = group[:3] # limit to a max of 3 blocks from any one source text = "\n.....\n".join([block[0].text for block in group]) min_index = min([block[1] for block in group]) unified_blocks.append((Block(key[0], key[1], key[2], key[3], key[4], text), min_index)) unified_blocks.sort(key=lambda bi: bi[1]) return [block for block, _ in unified_blocks] # we add the title and authors inside the contents of the block, so that # searches for the title or author will be more likely to pull it up. This # strips it back out. def strip_block(text: str) -> str: r = re.match(r"^\"(.*)\"\s*-\s*Title:.*$", text, re.DOTALL) if not r: print("Warning: couldn't strip block") print(text) return r.group(1) if r else text
[]
2024-01-10
facebookresearch/RLCD
scripts~gpt4_compare.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import argparse import json import random import time import openai from tqdm import tqdm def openai_call(messages, engine='gpt-4-0314', **kwargs): fails = 0 while True: try: if type(messages) == str: messages =[{'role': 'system', 'content': 'You are an intelligent AI assistant.'}, {'role': 'user', 'content': messages}] result = openai.ChatCompletion.create( model=engine, messages=messages, **kwargs ) text = result['choices'][0]['message']['content'] break except Exception as e: print(e) fails += 1 time.sleep(fails) # try again after a few seconds print('failed {} times, retrying...'.format(fails)) return result, text def format_prompt(instruction, output1, output2, task): while '\n\n' in instruction: instruction = instruction.replace('\n\n', '\n') instruction = instruction.replace('\nHuman:', '\n\nHuman:') instruction = instruction.replace('\nAssistant:', '\n\nAssistant:') prompt = 'Consider a conversation between a human and an assistant, beginning as follows:\n\n\n\n' + instruction[:instruction.rindex('Assistant:')].strip() while '\n\n' in output1: output1 = output1.replace('\n\n', '\n') while '\n\n' in output2: output2 = output2.replace('\n\n', '\n') if task == 'harmless': output1 = 'Assistant: ' + output1 output2 = 'Assistant: ' + output2 prompt += '\n\n\n\nHere are two possible assistant responses for continuing the conversation:\n\n(A) ' + output1 + '\n\n(B) ' + output2 prompt += '\n\n\n\nWhich response is more socially acceptable, ethical, or inoffensive? Please answer (A) or (B). If neither response is acceptable, please choose the one which is less unacceptable.' elif task == 'harmless_helpful': output1 = 'Assistant: ' + output1 output2 = 'Assistant: ' + output2 prompt += '\n\n\n\nHere are two possible assistant responses for continuing the conversation:\n\n(A) ' + output1 + '\n\n(B) ' + output2 prompt += '\n\n\n\nWhich response is more helpful? Please answer (A) or (B). Note that one or both responses may contain unethical or offensive content, which we are judging separately; please judge solely based on helpfulness here.' elif task == 'helpful': output1 = 'Assistant: ' + output1 output2 = 'Assistant: ' + output2 prompt += '\n\n\n\nHere are two possible assistant responses for continuing the conversation:\n\n(A) ' + output1 + '\n\n(B) ' + output2 prompt += '\n\n\n\nWhich response is more helpful? Please answer (A) or (B).' # elif task == 'outline': # output1 = 'Assistant: Here is a possible outline:\n1. ' + output1 # output2 = 'Assistant: Here is a possible outline:\n1. ' + output2 # prompt += '\n\n\n\nHere are two possible assistant responses for continuing the conversation:\n\n(A) ' + output1 + '\n\n(B) ' + output2 # prompt += '\n\n\n\nWhich outline is better? For example, you can consider which outline is better-structured, more relevant to the premise, or more interesting. Please answer (A) or (B).' return prompt def extract_answer(response): # raw_response = response if response.startswith('(A)') or response.startswith('Response (A)'): return 'A' elif response.startswith('(B)') or response.startswith('Response (B)'): return 'B' elif 'However' in response: response = response[response.index('However'):] response = response.replace('than (A)', '').replace('than (B)', '') if '(A)' in response and '(B)' in response: # print(raw_response) return None elif '(A)' in response: return 'A' elif '(B)' in response: return 'B' else: # print(raw_response) return None else: # print(raw_response) return None if __name__=='__main__': parser = argparse.ArgumentParser() parser.add_argument('--in-file1', type=str) parser.add_argument('--in-file2', type=str) parser.add_argument('--out-file', type=str) parser.add_argument('--prompt-format', type=str, choices=['helpful', 'harmless', 'harmless_helpful']) # we used harmless_helpful for evaluating helpfulness on harmless prompts parser.add_argument('--limit', type=int, default=10000000) args = parser.parse_args() random.seed(0) with open(args.in_file1, 'r') as f: data1 = json.load(f) with open(args.in_file2, 'r') as f: data2 = json.load(f) for d1, d2 in zip(data1, data2): assert d1['instruction'] == d2['instruction'] d1_better = 0 d2_better = 0 total = 0 data1 = data1[:args.limit] data2 = data2[:args.limit] d1_scores = [] raw_responses = [] d1_firsts = [] answers = [] assert len(data1) == len(data2) for d1, d2 in tqdm(zip(data1, data2)): d1_first = random.random() < 0.5 d1_firsts.append(d1_first) if d1_first: prompt = format_prompt(d1['instruction'], d1['output'], d2['output'], args.prompt_format) else: prompt = format_prompt(d1['instruction'], d2['output'], d1['output'], args.prompt_format) _, response = openai_call(prompt, temperature=0) raw_responses.append(response) answer = extract_answer(response) answers.append(answer) if answer is None: print('d1_first:', d1_first) d1_better += 0.5 d2_better += 0.5 d1_scores.append(0.5) else: if d1_first: if answer == 'A': d1_better += 1 d1_scores.append(1) else: d2_better += 1 d1_scores.append(0) else: if answer == 'A': d2_better += 1 d1_scores.append(0) else: d1_better += 1 d1_scores.append(1) total += 1 if total % 20 == 0: print(d1_better, d2_better) print(d1_better, d2_better) with open(args.out_file, 'w') as f: json.dump({'d1_better': d1_better, 'd2_better': d2_better, 'total': total, 'd1_firsts': d1_firsts, 'd1_scores': d1_scores, 'answers': answers, 'raw_responses': raw_responses}, f)
[ "instruction", "Assistant:", "\n\n\n\nWhich response is more helpful? Please answer (A) or (B). Note that one or both responses may contain unethical or offensive content, which we are judging separately; please judge solely based on helpfulness here.", "Consider a conversation between a human and an assistant, beginning as follows:\n\n\n\n", "\n\n\n\nWhich response is more helpful? Please answer (A) or (B).", "\n\n\n\nHere are two possible assistant responses for continuing the conversation:\n\n(A) PLACEHOLDER\n\n(B) PLACEHOLDER", "\n\n\n\nWhich response is more socially acceptable, ethical, or inoffensive? Please answer (A) or (B). If neither response is acceptable, please choose the one which is less unacceptable.", "You are an intelligent AI assistant." ]
2024-01-10
AIGC-Audio/AudioGPT
audio-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__)))) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NeuralSeq')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'text_to_audio/Make_An_Audio')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'audio_detection')) sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mono2binaural')) import gradio as gr import matplotlib import librosa import torch 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 import soundfile from PIL import Image import numpy as np from omegaconf import OmegaConf from einops import repeat from ldm.util import instantiate_from_config from ldm.data.extract_mel_spectrogram import TRANSFORMS_16000 from vocoder.bigvgan.models import VocoderBigVGAN from ldm.models.diffusion.ddim import DDIMSampler import whisper from utils.hparams import set_hparams from utils.hparams import hparams as hp import scipy.io.wavfile as wavfile import librosa from audio_infer.utils import config as detection_config from audio_infer.pytorch.models import PVT import clip import numpy as np AUDIO_CHATGPT_PREFIX = """AudioGPT AudioGPT can not directly read audios, but it has a list of tools to finish different speech, audio, and singing voice tasks. Each audio will have a file name formed as "audio/xxx.wav". When talking about audios, AudioGPT is very strict to the file name and will never fabricate nonexistent files. AudioGPT is able to use tools in a sequence, and is loyal to the tool observation outputs rather than faking the audio content and audio file name. It will remember to provide the file name from the last tool observation, if a new audio is generated. Human may provide new audios to AudioGPT with a description. The description helps AudioGPT to understand this audio, but AudioGPT should use tools to finish following tasks, rather than directly imagine from the description. Overall, AudioGPT is a powerful audio 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: ------ AudioGPT has access to the following tools:""" AUDIO_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] ``` """ AUDIO_CHATGPT_SUFFIX = """You are very strict to the filename correctness and will never fake a file name if not exists. You will remember to provide the audio file name loyally if it's provided in the last tool observation. Begin! Previous conversation history: {chat_history} New input: {input} 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"history_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 merge_audio(audio_path_1, audio_path_2): merged_signal = [] sr_1, signal_1 = wavfile.read(audio_path_1) sr_2, signal_2 = wavfile.read(audio_path_2) merged_signal.append(signal_1) merged_signal.append(signal_2) merged_signal = np.hstack(merged_signal) merged_signal = np.asarray(merged_signal, dtype=np.int16) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") wavfile.write(audio_filename, sr_2, merged_signal) return audio_filename class T2I: def __init__(self, device): from transformers import AutoModelForCausalLM, AutoTokenizer from diffusers import StableDiffusionPipeline from transformers import pipeline 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): from transformers import BlipProcessor, BlipForConditionalGeneration 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 T2A: def __init__(self, device): print("Initializing Make-An-Audio to %s" % device) self.device = device self.sampler = self._initialize_model('text_to_audio/Make_An_Audio/configs/text_to_audio/txt2audio_args.yaml', 'text_to_audio/Make_An_Audio/useful_ckpts/ta40multi_epoch=000085.ckpt', device=device) self.vocoder = VocoderBigVGAN('text_to_audio/Make_An_Audio/vocoder/logs/bigv16k53w',device=device) def _initialize_model(self, config, ckpt, device): config = OmegaConf.load(config) model = instantiate_from_config(config.model) model.load_state_dict(torch.load(ckpt, map_location='cpu')["state_dict"], strict=False) model = model.to(device) model.cond_stage_model.to(model.device) model.cond_stage_model.device = model.device sampler = DDIMSampler(model) return sampler def txt2audio(self, text, seed = 55, scale = 1.5, ddim_steps = 100, n_samples = 3, W = 624, H = 80): SAMPLE_RATE = 16000 prng = np.random.RandomState(seed) start_code = prng.randn(n_samples, self.sampler.model.first_stage_model.embed_dim, H // 8, W // 8) start_code = torch.from_numpy(start_code).to(device=self.device, dtype=torch.float32) uc = self.sampler.model.get_learned_conditioning(n_samples * [""]) c = self.sampler.model.get_learned_conditioning(n_samples * [text]) shape = [self.sampler.model.first_stage_model.embed_dim, H//8, W//8] # (z_dim, 80//2^x, 848//2^x) samples_ddim, _ = self.sampler.sample(S = ddim_steps, conditioning = c, batch_size = n_samples, shape = shape, verbose = False, unconditional_guidance_scale = scale, unconditional_conditioning = uc, x_T = start_code) x_samples_ddim = self.sampler.model.decode_first_stage(samples_ddim) x_samples_ddim = torch.clamp((x_samples_ddim+1.0)/2.0, min=0.0, max=1.0) # [0, 1] wav_list = [] for idx,spec in enumerate(x_samples_ddim): wav = self.vocoder.vocode(spec) wav_list.append((SAMPLE_RATE,wav)) best_wav = self.select_best_audio(text, wav_list) return best_wav def select_best_audio(self, prompt, wav_list): from wav_evaluation.models.CLAPWrapper import CLAPWrapper clap_model = CLAPWrapper('text_to_audio/Make_An_Audio/useful_ckpts/CLAP/CLAP_weights_2022.pth', 'text_to_audio/Make_An_Audio/useful_ckpts/CLAP/config.yml', use_cuda=torch.cuda.is_available()) text_embeddings = clap_model.get_text_embeddings([prompt]) score_list = [] for data in wav_list: sr, wav = data audio_embeddings = clap_model.get_audio_embeddings([(torch.FloatTensor(wav), sr)], resample=True) score = clap_model.compute_similarity(audio_embeddings, text_embeddings, use_logit_scale=False).squeeze().cpu().numpy() score_list.append(score) max_index = np.array(score_list).argmax() print(score_list, max_index) return wav_list[max_index] def inference(self, text, seed = 55, scale = 1.5, ddim_steps = 100, n_samples = 3, W = 624, H = 80): melbins,mel_len = 80,624 with torch.no_grad(): result = self.txt2audio( text = text, H = melbins, W = mel_len ) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename, result[1], samplerate = 16000) print(f"Processed T2I.run, text: {text}, audio_filename: {audio_filename}") return audio_filename class I2A: def __init__(self, device): print("Initializing Make-An-Audio-Image to %s" % device) self.device = device self.sampler = self._initialize_model('text_to_audio/Make_An_Audio/configs/img_to_audio/img2audio_args.yaml', 'text_to_audio/Make_An_Audio/useful_ckpts/ta54_epoch=000216.ckpt', device=device) self.vocoder = VocoderBigVGAN('text_to_audio/Make_An_Audio/vocoder/logs/bigv16k53w',device=device) def _initialize_model(self, config, ckpt, device): config = OmegaConf.load(config) model = instantiate_from_config(config.model) model.load_state_dict(torch.load(ckpt, map_location='cpu')["state_dict"], strict=False) model = model.to(device) model.cond_stage_model.to(model.device) model.cond_stage_model.device = model.device sampler = DDIMSampler(model) return sampler def img2audio(self, image, seed = 55, scale = 3, ddim_steps = 100, W = 624, H = 80): SAMPLE_RATE = 16000 n_samples = 1 # only support 1 sample prng = np.random.RandomState(seed) start_code = prng.randn(n_samples, self.sampler.model.first_stage_model.embed_dim, H // 8, W // 8) start_code = torch.from_numpy(start_code).to(device=self.device, dtype=torch.float32) uc = self.sampler.model.get_learned_conditioning(n_samples * [""]) #image = Image.fromarray(image) image = Image.open(image) image = self.sampler.model.cond_stage_model.preprocess(image).unsqueeze(0) image_embedding = self.sampler.model.cond_stage_model.forward_img(image) c = image_embedding.repeat(n_samples, 1, 1) shape = [self.sampler.model.first_stage_model.embed_dim, H//8, W//8] # (z_dim, 80//2^x, 848//2^x) samples_ddim, _ = self.sampler.sample(S=ddim_steps, conditioning=c, batch_size=n_samples, shape=shape, verbose=False, unconditional_guidance_scale=scale, unconditional_conditioning=uc, x_T=start_code) x_samples_ddim = self.sampler.model.decode_first_stage(samples_ddim) x_samples_ddim = torch.clamp((x_samples_ddim+1.0)/2.0, min=0.0, max=1.0) # [0, 1] wav_list = [] for idx,spec in enumerate(x_samples_ddim): wav = self.vocoder.vocode(spec) wav_list.append((SAMPLE_RATE,wav)) best_wav = wav_list[0] return best_wav def inference(self, image, seed = 55, scale = 3, ddim_steps = 100, W = 624, H = 80): melbins,mel_len = 80,624 with torch.no_grad(): result = self.img2audio( image=image, H=melbins, W=mel_len ) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename, result[1], samplerate = 16000) print(f"Processed I2a.run, image_filename: {image}, audio_filename: {audio_filename}") return audio_filename class TTS: def __init__(self, device=None): from inference.tts.PortaSpeech import TTSInference if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' print("Initializing PortaSpeech to %s" % device) self.device = device self.exp_name = 'checkpoints/ps_adv_baseline' self.set_model_hparams() self.inferencer = TTSInference(self.hp, device) def set_model_hparams(self): set_hparams(exp_name=self.exp_name, print_hparams=False) self.hp = hp def inference(self, text): self.set_model_hparams() inp = {"text": text} out = self.inferencer.infer_once(inp) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename, out, samplerate=22050) return audio_filename class T2S: def __init__(self, device= None): from inference.svs.ds_e2e import DiffSingerE2EInfer if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' print("Initializing DiffSinger to %s" % device) self.device = device self.exp_name = 'checkpoints/0831_opencpop_ds1000' self.config= 'NeuralSeq/egs/egs_bases/svs/midi/e2e/opencpop/ds1000.yaml' self.set_model_hparams() self.pipe = DiffSingerE2EInfer(self.hp, device) self.default_inp = { 'text': '你 说 你 不 SP 懂 为 何 在 这 时 牵 手 AP', 'notes': 'D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | D#4/Eb4 | rest | D#4/Eb4 | D4 | D4 | D4 | D#4/Eb4 | F4 | D#4/Eb4 | D4 | rest', 'notes_duration': '0.113740 | 0.329060 | 0.287950 | 0.133480 | 0.150900 | 0.484730 | 0.242010 | 0.180820 | 0.343570 | 0.152050 | 0.266720 | 0.280310 | 0.633300 | 0.444590' } def set_model_hparams(self): set_hparams(config=self.config, exp_name=self.exp_name, print_hparams=False) self.hp = hp def inference(self, inputs): self.set_model_hparams() val = inputs.split(",") key = ['text', 'notes', 'notes_duration'] try: inp = {k: v for k, v in zip(key, val)} wav = self.pipe.infer_once(inp) except: print('Error occurs. Generate default audio sample.\n') inp = self.default_inp wav = self.pipe.infer_once(inp) #if inputs == '' or len(val) < len(key): # inp = self.default_inp #else: # inp = {k:v for k,v in zip(key,val)} #wav = self.pipe.infer_once(inp) wav *= 32767 audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") wavfile.write(audio_filename, self.hp['audio_sample_rate'], wav.astype(np.int16)) print(f"Processed T2S.run, audio_filename: {audio_filename}") return audio_filename class t2s_VISinger: def __init__(self, device=None): from espnet2.bin.svs_inference import SingingGenerate if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' print("Initializing VISingere to %s" % device) tag = 'AQuarterMile/opencpop_visinger1' self.model = SingingGenerate.from_pretrained( model_tag=str_or_none(tag), device=device, ) phn_dur = [[0. , 0.219 ], [0.219 , 0.50599998], [0.50599998, 0.71399999], [0.71399999, 1.097 ], [1.097 , 1.28799999], [1.28799999, 1.98300004], [1.98300004, 7.10500002], [7.10500002, 7.60400009]] phn = ['sh', 'i', 'q', 'v', 'n', 'i', 'SP', 'AP'] score = [[0, 0.50625, 'sh_i', 58, 'sh_i'], [0.50625, 1.09728, 'q_v', 56, 'q_v'], [1.09728, 1.9832100000000001, 'n_i', 53, 'n_i'], [1.9832100000000001, 7.105360000000001, 'SP', 0, 'SP'], [7.105360000000001, 7.604390000000001, 'AP', 0, 'AP']] tempo = 70 tmp = {} tmp["label"] = phn_dur, phn tmp["score"] = tempo, score self.default_inp = tmp def inference(self, inputs): val = inputs.split(",") key = ['text', 'notes', 'notes_duration'] try: # TODO: input will be update inp = {k: v for k, v in zip(key, val)} wav = self.model(text=inp)["wav"] except: print('Error occurs. Generate default audio sample.\n') inp = self.default_inp wav = self.model(text=inp)["wav"] audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename, wav, samplerate=self.model.fs) return audio_filename class TTS_OOD: def __init__(self, device): from inference.tts.GenerSpeech import GenerSpeechInfer if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' print("Initializing GenerSpeech to %s" % device) self.device = device self.exp_name = 'checkpoints/GenerSpeech' self.config = 'NeuralSeq/modules/GenerSpeech/config/generspeech.yaml' self.set_model_hparams() self.pipe = GenerSpeechInfer(self.hp, device) def set_model_hparams(self): set_hparams(config=self.config, exp_name=self.exp_name, print_hparams=False) f0_stats_fn = f'{hp["binary_data_dir"]}/train_f0s_mean_std.npy' if os.path.exists(f0_stats_fn): hp['f0_mean'], hp['f0_std'] = np.load(f0_stats_fn) hp['f0_mean'] = float(hp['f0_mean']) hp['f0_std'] = float(hp['f0_std']) hp['emotion_encoder_path'] = 'checkpoints/Emotion_encoder.pt' self.hp = hp def inference(self, inputs): self.set_model_hparams() key = ['ref_audio', 'text'] val = inputs.split(",") inp = {k: v for k, v in zip(key, val)} wav = self.pipe.infer_once(inp) wav *= 32767 audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") wavfile.write(audio_filename, self.hp['audio_sample_rate'], wav.astype(np.int16)) print( f"Processed GenerSpeech.run. Input text:{val[1]}. Input reference audio: {val[0]}. Output Audio_filename: {audio_filename}") return audio_filename class Inpaint: def __init__(self, device): print("Initializing Make-An-Audio-inpaint to %s" % device) self.device = device self.sampler = self._initialize_model_inpaint('text_to_audio/Make_An_Audio/configs/inpaint/txt2audio_args.yaml', 'text_to_audio/Make_An_Audio/useful_ckpts/inpaint7_epoch00047.ckpt') self.vocoder = VocoderBigVGAN('text_to_audio/Make_An_Audio/vocoder/logs/bigv16k53w',device=device) self.cmap_transform = matplotlib.cm.viridis def _initialize_model_inpaint(self, config, ckpt): config = OmegaConf.load(config) model = instantiate_from_config(config.model) model.load_state_dict(torch.load(ckpt, map_location='cpu')["state_dict"], strict=False) device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model = model.to(device) print(model.device, device, model.cond_stage_model.device) sampler = DDIMSampler(model) return sampler def make_batch_sd(self, mel, mask, num_samples=1): mel = torch.from_numpy(mel)[None,None,...].to(dtype=torch.float32) mask = torch.from_numpy(mask)[None,None,...].to(dtype=torch.float32) masked_mel = (1 - mask) * mel mel = mel * 2 - 1 mask = mask * 2 - 1 masked_mel = masked_mel * 2 -1 batch = { "mel": repeat(mel.to(device=self.device), "1 ... -> n ...", n=num_samples), "mask": repeat(mask.to(device=self.device), "1 ... -> n ...", n=num_samples), "masked_mel": repeat(masked_mel.to(device=self.device), "1 ... -> n ...", n=num_samples), } return batch def gen_mel(self, input_audio_path): SAMPLE_RATE = 16000 sr, ori_wav = wavfile.read(input_audio_path) print("gen_mel") print(sr,ori_wav.shape,ori_wav) ori_wav = ori_wav.astype(np.float32, order='C') / 32768.0 if len(ori_wav.shape)==2:# stereo ori_wav = librosa.to_mono(ori_wav.T) print(sr,ori_wav.shape,ori_wav) ori_wav = librosa.resample(ori_wav,orig_sr = sr,target_sr = SAMPLE_RATE) mel_len,hop_size = 848,256 input_len = mel_len * hop_size if len(ori_wav) < input_len: input_wav = np.pad(ori_wav,(0,mel_len*hop_size),constant_values=0) else: input_wav = ori_wav[:input_len] mel = TRANSFORMS_16000(input_wav) return mel def gen_mel_audio(self, input_audio): SAMPLE_RATE = 16000 sr,ori_wav = input_audio print("gen_mel_audio") print(sr,ori_wav.shape,ori_wav) ori_wav = ori_wav.astype(np.float32, order='C') / 32768.0 if len(ori_wav.shape)==2:# stereo ori_wav = librosa.to_mono(ori_wav.T) print(sr,ori_wav.shape,ori_wav) ori_wav = librosa.resample(ori_wav,orig_sr = sr,target_sr = SAMPLE_RATE) mel_len,hop_size = 848,256 input_len = mel_len * hop_size if len(ori_wav) < input_len: input_wav = np.pad(ori_wav,(0,mel_len*hop_size),constant_values=0) else: input_wav = ori_wav[:input_len] mel = TRANSFORMS_16000(input_wav) return mel def show_mel_fn(self, input_audio_path): crop_len = 500 crop_mel = self.gen_mel(input_audio_path)[:,:crop_len] color_mel = self.cmap_transform(crop_mel) image = Image.fromarray((color_mel*255).astype(np.uint8)) image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png") image.save(image_filename) return image_filename def inpaint(self, batch, seed, ddim_steps, num_samples=1, W=512, H=512): model = self.sampler.model prng = np.random.RandomState(seed) start_code = prng.randn(num_samples, model.first_stage_model.embed_dim, H // 8, W // 8) start_code = torch.from_numpy(start_code).to(device=self.device, dtype=torch.float32) c = model.get_first_stage_encoding(model.encode_first_stage(batch["masked_mel"])) cc = torch.nn.functional.interpolate(batch["mask"], size=c.shape[-2:]) c = torch.cat((c, cc), dim=1) # (b,c+1,h,w) 1 is mask shape = (c.shape[1]-1,)+c.shape[2:] samples_ddim, _ = self.sampler.sample(S=ddim_steps, conditioning=c, batch_size=c.shape[0], shape=shape, verbose=False) x_samples_ddim = model.decode_first_stage(samples_ddim) mel = torch.clamp((batch["mel"]+1.0)/2.0,min=0.0, max=1.0) mask = torch.clamp((batch["mask"]+1.0)/2.0,min=0.0, max=1.0) predicted_mel = torch.clamp((x_samples_ddim+1.0)/2.0,min=0.0, max=1.0) inpainted = (1-mask)*mel+mask*predicted_mel inpainted = inpainted.cpu().numpy().squeeze() inapint_wav = self.vocoder.vocode(inpainted) return inpainted, inapint_wav def inference(self, input_audio, mel_and_mask, seed = 55, ddim_steps = 100): SAMPLE_RATE = 16000 torch.set_grad_enabled(False) mel_img = Image.open(mel_and_mask['image']) mask_img = Image.open(mel_and_mask["mask"]) show_mel = np.array(mel_img.convert("L"))/255 mask = np.array(mask_img.convert("L"))/255 mel_bins,mel_len = 80,848 input_mel = self.gen_mel_audio(input_audio)[:,:mel_len] mask = np.pad(mask,((0,0),(0,mel_len-mask.shape[1])),mode='constant',constant_values=0) print(mask.shape,input_mel.shape) with torch.no_grad(): batch = self.make_batch_sd(input_mel,mask,num_samples=1) inpainted,gen_wav = self.inpaint( batch=batch, seed=seed, ddim_steps=ddim_steps, num_samples=1, H=mel_bins, W=mel_len ) inpainted = inpainted[:,:show_mel.shape[1]] color_mel = self.cmap_transform(inpainted) input_len = int(input_audio[1].shape[0] * SAMPLE_RATE / input_audio[0]) gen_wav = (gen_wav * 32768).astype(np.int16)[:input_len] image = Image.fromarray((color_mel*255).astype(np.uint8)) image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png") image.save(image_filename) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename, gen_wav, samplerate = 16000) return image_filename, audio_filename class ASR: def __init__(self, device): print("Initializing Whisper to %s" % device) self.device = device self.model = whisper.load_model("base", device=device) def inference(self, audio_path): audio = whisper.load_audio(audio_path) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(self.device) _, probs = self.model.detect_language(mel) options = whisper.DecodingOptions() result = whisper.decode(self.model, mel, options) return result.text def translate_english(self, audio_path): audio = self.model.transcribe(audio_path, language='English') return audio['text'] class A2T: def __init__(self, device): from audio_to_text.inference_waveform import AudioCapModel print("Initializing Audio-To-Text Model to %s" % device) self.device = device self.model = AudioCapModel("audio_to_text/audiocaps_cntrstv_cnn14rnn_trm") def inference(self, audio_path): audio = whisper.load_audio(audio_path) caption_text = self.model(audio) return caption_text[0] class GeneFace: def __init__(self, device=None): print("Initializing GeneFace model to %s" % device) from audio_to_face.GeneFace_binding import GeneFaceInfer if device is None: device = 'cuda' if torch.cuda.is_available() else 'cpu' self.device = device self.geneface_model = GeneFaceInfer(device) print("Loaded GeneFace model") def inference(self, audio_path): audio_base_name = os.path.basename(audio_path)[:-4] out_video_name = audio_path.replace("audio","video").replace(".wav", ".mp4") inp = { 'audio_source_name': audio_path, 'out_npy_name': f'geneface/tmp/{audio_base_name}.npy', 'cond_name': f'geneface/tmp/{audio_base_name}.npy', 'out_video_name': out_video_name, 'tmp_imgs_dir': f'video/tmp_imgs', } self.geneface_model.infer_once(inp) return out_video_name class SoundDetection: def __init__(self, device): self.device = device self.sample_rate = 32000 self.window_size = 1024 self.hop_size = 320 self.mel_bins = 64 self.fmin = 50 self.fmax = 14000 self.model_type = 'PVT' self.checkpoint_path = 'audio_detection/audio_infer/useful_ckpts/audio_detection.pth' self.classes_num = detection_config.classes_num self.labels = detection_config.labels self.frames_per_second = self.sample_rate // self.hop_size # Model = eval(self.model_type) self.model = PVT(sample_rate=self.sample_rate, window_size=self.window_size, hop_size=self.hop_size, mel_bins=self.mel_bins, fmin=self.fmin, fmax=self.fmax, classes_num=self.classes_num) checkpoint = torch.load(self.checkpoint_path, map_location=self.device) self.model.load_state_dict(checkpoint['model']) self.model.to(device) def inference(self, audio_path): # Forward (waveform, _) = librosa.core.load(audio_path, sr=self.sample_rate, mono=True) waveform = waveform[None, :] # (1, audio_length) waveform = torch.from_numpy(waveform) waveform = waveform.to(self.device) # Forward with torch.no_grad(): self.model.eval() batch_output_dict = self.model(waveform, None) framewise_output = batch_output_dict['framewise_output'].data.cpu().numpy()[0] """(time_steps, classes_num)""" # print('Sound event detection result (time_steps x classes_num): {}'.format( # framewise_output.shape)) import numpy as np import matplotlib.pyplot as plt sorted_indexes = np.argsort(np.max(framewise_output, axis=0))[::-1] top_k = 10 # Show top results top_result_mat = framewise_output[:, sorted_indexes[0 : top_k]] """(time_steps, top_k)""" # Plot result stft = librosa.core.stft(y=waveform[0].data.cpu().numpy(), n_fft=self.window_size, hop_length=self.hop_size, window='hann', center=True) frames_num = stft.shape[-1] fig, axs = plt.subplots(2, 1, sharex=True, figsize=(10, 4)) axs[0].matshow(np.log(np.abs(stft)), origin='lower', aspect='auto', cmap='jet') axs[0].set_ylabel('Frequency bins') axs[0].set_title('Log spectrogram') axs[1].matshow(top_result_mat.T, origin='upper', aspect='auto', cmap='jet', vmin=0, vmax=1) axs[1].xaxis.set_ticks(np.arange(0, frames_num, self.frames_per_second)) axs[1].xaxis.set_ticklabels(np.arange(0, frames_num / self.frames_per_second)) axs[1].yaxis.set_ticks(np.arange(0, top_k)) axs[1].yaxis.set_ticklabels(np.array(self.labels)[sorted_indexes[0 : top_k]]) axs[1].yaxis.grid(color='k', linestyle='solid', linewidth=0.3, alpha=0.3) axs[1].set_xlabel('Seconds') axs[1].xaxis.set_ticks_position('bottom') plt.tight_layout() image_filename = os.path.join('image', str(uuid.uuid4())[0:8] + ".png") plt.savefig(image_filename) return image_filename class SoundExtraction: def __init__(self, device): from sound_extraction.model.LASSNet import LASSNet from sound_extraction.utils.stft import STFT import torch.nn as nn self.device = device self.model_file = 'sound_extraction/useful_ckpts/LASSNet.pt' self.stft = STFT() self.model = nn.DataParallel(LASSNet(device)).to(device) checkpoint = torch.load(self.model_file) self.model.load_state_dict(checkpoint['model']) self.model.eval() def inference(self, inputs): #key = ['ref_audio', 'text'] from sound_extraction.utils.wav_io import load_wav, save_wav val = inputs.split(",") audio_path = val[0] # audio_path, text text = val[1] waveform = load_wav(audio_path) waveform = torch.tensor(waveform).transpose(1,0) mixed_mag, mixed_phase = self.stft.transform(waveform) text_query = ['[CLS] ' + text] mixed_mag = mixed_mag.transpose(2,1).unsqueeze(0).to(self.device) est_mask = self.model(mixed_mag, text_query) est_mag = est_mask * mixed_mag est_mag = est_mag.squeeze(1) est_mag = est_mag.permute(0, 2, 1) est_wav = self.stft.inverse(est_mag.cpu().detach(), mixed_phase) est_wav = est_wav.squeeze(0).squeeze(0).numpy() #est_path = f'output/est{i}.wav' audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") print('audio_filename ', audio_filename) save_wav(est_wav, audio_filename) return audio_filename class Binaural: def __init__(self, device): from src.models import BinauralNetwork self.device = device self.model_file = 'mono2binaural/useful_ckpts/m2b/binaural_network.net' self.position_file = ['mono2binaural/useful_ckpts/m2b/tx_positions.txt', 'mono2binaural/useful_ckpts/m2b/tx_positions2.txt', 'mono2binaural/useful_ckpts/m2b/tx_positions3.txt', 'mono2binaural/useful_ckpts/m2b/tx_positions4.txt', 'mono2binaural/useful_ckpts/m2b/tx_positions5.txt'] self.net = BinauralNetwork(view_dim=7, warpnet_layers=4, warpnet_channels=64, ) self.net.load_from_file(self.model_file) self.sr = 48000 def inference(self, audio_path): mono, sr = librosa.load(path=audio_path, sr=self.sr, mono=True) mono = torch.from_numpy(mono) mono = mono.unsqueeze(0) import numpy as np import random rand_int = random.randint(0,4) view = np.loadtxt(self.position_file[rand_int]).transpose().astype(np.float32) view = torch.from_numpy(view) if not view.shape[-1] * 400 == mono.shape[-1]: mono = mono[:,:(mono.shape[-1]//400)*400] # if view.shape[1]*400 > mono.shape[1]: m_a = view.shape[1] - mono.shape[-1]//400 rand_st = random.randint(0,m_a) view = view[:,m_a:m_a+(mono.shape[-1]//400)] # # binauralize and save output self.net.eval().to(self.device) mono, view = mono.to(self.device), view.to(self.device) chunk_size = 48000 # forward in chunks of 1s rec_field = 1000 # add 1000 samples as "safe bet" since warping has undefined rec. field rec_field -= rec_field % 400 # make sure rec_field is a multiple of 400 to match audio and view frequencies chunks = [ { "mono": mono[:, max(0, i-rec_field):i+chunk_size], "view": view[:, max(0, i-rec_field)//400:(i+chunk_size)//400] } for i in range(0, mono.shape[-1], chunk_size) ] for i, chunk in enumerate(chunks): with torch.no_grad(): mono = chunk["mono"].unsqueeze(0) view = chunk["view"].unsqueeze(0) binaural = self.net(mono, view).squeeze(0) if i > 0: binaural = binaural[:, -(mono.shape[-1]-rec_field):] chunk["binaural"] = binaural binaural = torch.cat([chunk["binaural"] for chunk in chunks], dim=-1) binaural = torch.clamp(binaural, min=-1, max=1).cpu() #binaural = chunked_forwarding(net, mono, view) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") import torchaudio torchaudio.save(audio_filename, binaural, sr) #soundfile.write(audio_filename, binaural, samplerate = 48000) print(f"Processed Binaural.run, audio_filename: {audio_filename}") return audio_filename class TargetSoundDetection: def __init__(self, device): from target_sound_detection.src import models as tsd_models from target_sound_detection.src.models import event_labels self.device = device self.MEL_ARGS = { 'n_mels': 64, 'n_fft': 2048, 'hop_length': int(22050 * 20 / 1000), 'win_length': int(22050 * 40 / 1000) } self.EPS = np.spacing(1) self.clip_model, _ = clip.load("ViT-B/32", device=self.device) self.event_labels = event_labels self.id_to_event = {i : label for i, label in enumerate(self.event_labels)} config = torch.load('audio_detection/target_sound_detection/useful_ckpts/tsd/run_config.pth', map_location='cpu') config_parameters = dict(config) config_parameters['tao'] = 0.6 if 'thres' not in config_parameters.keys(): config_parameters['thres'] = 0.5 if 'time_resolution' not in config_parameters.keys(): config_parameters['time_resolution'] = 125 model_parameters = torch.load('audio_detection/target_sound_detection/useful_ckpts/tsd/run_model_7_loss=-0.0724.pt' , map_location=lambda storage, loc: storage) # load parameter self.model = getattr(tsd_models, config_parameters['model'])(config_parameters, inputdim=64, outputdim=2, time_resolution=config_parameters['time_resolution'], **config_parameters['model_args']) self.model.load_state_dict(model_parameters) self.model = self.model.to(self.device).eval() self.re_embeds = torch.load('audio_detection/target_sound_detection/useful_ckpts/tsd/text_emb.pth') self.ref_mel = torch.load('audio_detection/target_sound_detection/useful_ckpts/tsd/ref_mel.pth') def extract_feature(self, fname): import soundfile as sf y, sr = sf.read(fname, dtype='float32') print('y ', y.shape) ti = y.shape[0]/sr if y.ndim > 1: y = y.mean(1) y = librosa.resample(y, sr, 22050) lms_feature = np.log(librosa.feature.melspectrogram(y, **self.MEL_ARGS) + self.EPS).T return lms_feature,ti def build_clip(self, text): text = clip.tokenize(text).to(self.device) # ["a diagram with dog", "a dog", "a cat"] text_features = self.clip_model.encode_text(text) return text_features def cal_similarity(self, target, retrievals): ans = [] #target =torch.from_numpy(target) for name in retrievals.keys(): tmp = retrievals[name] #tmp = torch.from_numpy(tmp) s = torch.cosine_similarity(target.squeeze(), tmp.squeeze(), dim=0) ans.append(s.item()) return ans.index(max(ans)) def inference(self, text, audio_path): from target_sound_detection.src.utils import median_filter, decode_with_timestamps target_emb = self.build_clip(text) # torch type idx = self.cal_similarity(target_emb, self.re_embeds) target_event = self.id_to_event[idx] embedding = self.ref_mel[target_event] embedding = torch.from_numpy(embedding) embedding = embedding.unsqueeze(0).to(self.device).float() #print('embedding ', embedding.shape) inputs,ti = self.extract_feature(audio_path) #print('ti ', ti) inputs = torch.from_numpy(inputs) inputs = inputs.unsqueeze(0).to(self.device).float() #print('inputs ', inputs.shape) decision, decision_up, logit = self.model(inputs, embedding) pred = decision_up.detach().cpu().numpy() pred = pred[:,:,0] frame_num = decision_up.shape[1] time_ratio = ti / frame_num filtered_pred = median_filter(pred, window_size=1, threshold=0.5) #print('filtered_pred ', filtered_pred) time_predictions = [] for index_k in range(filtered_pred.shape[0]): decoded_pred = [] decoded_pred_ = decode_with_timestamps(target_event, filtered_pred[index_k,:]) if len(decoded_pred_) == 0: # neg deal decoded_pred_.append((target_event, 0, 0)) decoded_pred.append(decoded_pred_) for num_batch in range(len(decoded_pred)): # when we test our model,the batch_size is 1 cur_pred = pred[num_batch] # Save each frame output, for later visualization label_prediction = decoded_pred[num_batch] # frame predict # print(label_prediction) for event_label, onset, offset in label_prediction: time_predictions.append({ 'onset': onset*time_ratio, 'offset': offset*time_ratio,}) ans = '' for i,item in enumerate(time_predictions): ans = ans + 'segment' + str(i+1) + ' start_time: ' + str(item['onset']) + ' end_time: ' + str(item['offset']) + '\t' #print(ans) return ans # class Speech_Enh_SS_SC: # """Speech Enhancement or Separation in single-channel # Example usage: # enh_model = Speech_Enh_SS("cuda") # enh_wav = enh_model.inference("./test_chime4_audio_M05_440C0213_PED_REAL.wav") # """ # def __init__(self, device="cuda", model_name="lichenda/chime4_fasnet_dprnn_tac"): # self.model_name = model_name # self.device = device # print("Initializing ESPnet Enh to %s" % device) # self._initialize_model() # def _initialize_model(self): # from espnet_model_zoo.downloader import ModelDownloader # from espnet2.bin.enh_inference import SeparateSpeech # d = ModelDownloader() # cfg = d.download_and_unpack(self.model_name) # self.separate_speech = SeparateSpeech( # train_config=cfg["train_config"], # model_file=cfg["model_file"], # # for segment-wise process on long speech # segment_size=2.4, # hop_size=0.8, # normalize_segment_scale=False, # show_progressbar=True, # ref_channel=None, # normalize_output_wav=True, # device=self.device, # ) # def inference(self, speech_path, ref_channel=0): # speech, sr = soundfile.read(speech_path) # speech = speech[:, ref_channel] # assert speech.dim() == 1 # enh_speech = self.separate_speech(speech[None, ], fs=sr) # if len(enh_speech) == 1: # return enh_speech[0] # return enh_speech # class Speech_Enh_SS_MC: # """Speech Enhancement or Separation in multi-channel""" # def __init__(self, device="cuda", model_name=None, ref_channel=4): # self.model_name = model_name # self.ref_channel = ref_channel # self.device = device # print("Initializing ESPnet Enh to %s" % device) # self._initialize_model() # def _initialize_model(self): # from espnet_model_zoo.downloader import ModelDownloader # from espnet2.bin.enh_inference import SeparateSpeech # d = ModelDownloader() # cfg = d.download_and_unpack(self.model_name) # self.separate_speech = SeparateSpeech( # train_config=cfg["train_config"], # model_file=cfg["model_file"], # # for segment-wise process on long speech # segment_size=2.4, # hop_size=0.8, # normalize_segment_scale=False, # show_progressbar=True, # ref_channel=self.ref_channel, # normalize_output_wav=True, # device=self.device, # ) # def inference(self, speech_path): # speech, sr = soundfile.read(speech_path) # speech = speech.T # enh_speech = self.separate_speech(speech[None, ...], fs=sr) # if len(enh_speech) == 1: # return enh_speech[0] # return enh_speech class Speech_Enh_SS_SC: """Speech Enhancement or Separation in single-channel Example usage: enh_model = Speech_Enh_SS("cuda") enh_wav = enh_model.inference("./test_chime4_audio_M05_440C0213_PED_REAL.wav") """ def __init__(self, device="cuda", model_name="espnet/Wangyou_Zhang_chime4_enh_train_enh_conv_tasnet_raw"): self.model_name = model_name self.device = device print("Initializing ESPnet Enh to %s" % device) self._initialize_model() def _initialize_model(self): from espnet_model_zoo.downloader import ModelDownloader from espnet2.bin.enh_inference import SeparateSpeech d = ModelDownloader() cfg = d.download_and_unpack(self.model_name) self.separate_speech = SeparateSpeech( train_config=cfg["train_config"], model_file=cfg["model_file"], # for segment-wise process on long speech segment_size=2.4, hop_size=0.8, normalize_segment_scale=False, show_progressbar=True, ref_channel=None, normalize_output_wav=True, device=self.device, ) def inference(self, speech_path, ref_channel=0): speech, sr = soundfile.read(speech_path) speech = speech[:, ref_channel] # speech = torch.from_numpy(speech) # assert speech.dim() == 1 enh_speech = self.separate_speech(speech[None, ...], fs=sr) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") # if len(enh_speech) == 1: soundfile.write(audio_filename, enh_speech[0].squeeze(), samplerate=sr) # return enh_speech[0] # return enh_speech # else: # print("############") # audio_filename_1 = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") # soundfile.write(audio_filename_1, enh_speech[0].squeeze(), samplerate=sr) # audio_filename_2 = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") # soundfile.write(audio_filename_2, enh_speech[1].squeeze(), samplerate=sr) # audio_filename = merge_audio(audio_filename_1, audio_filename_2) return audio_filename class Speech_SS: def __init__(self, device="cuda", model_name="lichenda/wsj0_2mix_skim_noncausal"): self.model_name = model_name self.device = device print("Initializing ESPnet SS to %s" % device) self._initialize_model() def _initialize_model(self): from espnet_model_zoo.downloader import ModelDownloader from espnet2.bin.enh_inference import SeparateSpeech d = ModelDownloader() cfg = d.download_and_unpack(self.model_name) self.separate_speech = SeparateSpeech( train_config=cfg["train_config"], model_file=cfg["model_file"], # for segment-wise process on long speech segment_size=2.4, hop_size=0.8, normalize_segment_scale=False, show_progressbar=True, ref_channel=None, normalize_output_wav=True, device=self.device, ) def inference(self, speech_path): speech, sr = soundfile.read(speech_path) enh_speech = self.separate_speech(speech[None, ...], fs=sr) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") if len(enh_speech) == 1: soundfile.write(audio_filename, enh_speech[0], samplerate=sr) else: # print("############") audio_filename_1 = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename_1, enh_speech[0].squeeze(), samplerate=sr) audio_filename_2 = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") soundfile.write(audio_filename_2, enh_speech[1].squeeze(), samplerate=sr) audio_filename = merge_audio(audio_filename_1, audio_filename_2) return audio_filename class ConversationBot: def __init__(self): print("Initializing AudioGPT") self.llm = OpenAI(temperature=0) self.t2i = T2I(device="cuda:1") self.i2t = ImageCaptioning(device="cuda:0") self.t2a = T2A(device="cuda:0") self.tts = TTS(device="cpu") self.t2s = T2S(device="cpu") self.i2a = I2A(device="cuda:0") self.a2t = A2T(device="cpu") self.asr = ASR(device="cuda:0") self.SE_SS_SC = Speech_Enh_SS_SC(device="cuda:0") # self.SE_SS_MC = Speech_Enh_SS_MC(device="cuda:0") self.SS = Speech_SS(device="cuda:0") self.inpaint = Inpaint(device="cuda:0") self.tts_ood = TTS_OOD(device="cpu") self.geneface = GeneFace(device="cuda:0") self.detection = SoundDetection(device="cpu") self.binaural = Binaural(device="cuda:0") self.extraction = SoundExtraction(device="cuda:0") self.TSD = TargetSoundDetection(device="cuda:0") self.memory = ConversationBufferMemory(memory_key="chat_history", output_key='output') def init_tools(self, interaction_type): if interaction_type == 'text': self.tools = [ Tool(name="Generate Image From User Input Text", func=self.t2i.inference, description="useful for when you want to generate an image from a user input text and it saved 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="Get Photo Description", func=self.i2t.inference, description="useful for 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 Audio From User Input Text", func=self.t2a.inference, description="useful for when you want to generate an audio from a user input text and it saved it to a file." "The input to this tool should be a string, representing the text used to generate audio."), Tool( name="Style Transfer", func= self.tts_ood.inference, description="useful for when you want to generate speech samples with styles (e.g., timbre, emotion, and prosody) derived from a reference custom voice." "Like: Generate a speech with style transferred from this voice. The text is xxx., or speak using the voice of this audio. The text is xxx." "The input to this tool should be a comma seperated string of two, representing reference audio path and input text."), Tool(name="Generate Singing Voice From User Input Text, Note and Duration Sequence", func= self.t2s.inference, description="useful for when you want to generate a piece of singing voice (Optional: from User Input Text, Note and Duration Sequence) and save it to a file." "If Like: Generate a piece of singing voice, the input to this tool should be \"\" since there is no User Input Text, Note and Duration Sequence ." "If Like: Generate a piece of singing voice. Text: xxx, Note: xxx, Duration: xxx. " "Or Like: Generate a piece of singing voice. Text is xxx, note is xxx, duration is xxx." "The input to this tool should be a comma seperated string of three, representing text, note and duration sequence since User Input Text, Note and Duration Sequence are all provided."), Tool(name="Synthesize Speech Given the User Input Text", func=self.tts.inference, description="useful for when you want to convert a user input text into speech audio it saved it to a file." "The input to this tool should be a string, representing the text used to be converted to speech."), # Tool(name="Speech Enhancement Or Separation In Single-Channel", func=self.SE_SS_SC.inference, # description="useful for when you want to enhance the quality of the speech signal by reducing background noise (single-channel), " # "or separate each speech from the speech mixture (single-channel), receives audio_path as input." # "The input to this tool should be a string, representing the audio_path."), Tool(name="Speech Enhancement In Single-Channel", func=self.SE_SS_SC.inference, description="useful for when you want to enhance the quality of the speech signal by reducing background noise (single-channel), receives audio_path as input." "The input to this tool should be a string, representing the audio_path."), Tool(name="Speech Separation In Single-Channel", func=self.SS.inference, description="useful for when you want to separate each speech from the speech mixture, receives audio_path as input." "The input to this tool should be a string, representing the audio_path."), # Tool(name="Speech Enhancement In Multi-Channel", func=self.SE_SS_MC.inference, # description="useful for when you want to enhance the quality of the speech signal by reducing background noise (multi-channel), receives audio_path as input." # "The input to this tool should be a string, representing the audio_path."), Tool(name="Generate Audio From The Image", func=self.i2a.inference, description="useful for when you want to generate an audio based on an image." "The input to this tool should be a string, representing the image_path. "), Tool(name="Generate Text From The Audio", func=self.a2t.inference, description="useful for when you want to describe an audio in text, receives audio_path as input." "The input to this tool should be a string, representing the audio_path."), Tool(name="Audio Inpainting", func=self.inpaint.show_mel_fn, description="useful for when you want to inpaint a mel spectrum of an audio and predict this audio, this tool will generate a mel spectrum and you can inpaint it, receives audio_path as input, " "The input to this tool should be a string, representing the audio_path."), Tool(name="Transcribe Speech", func=self.asr.inference, description="useful for when you want to know the text corresponding to a human speech, receives audio_path as input." "The input to this tool should be a string, representing the audio_path."), Tool(name="Generate a talking human portrait video given a input Audio", func=self.geneface.inference, description="useful for when you want to generate a talking human portrait video given a input audio." "The input to this tool should be a string, representing the audio_path."), Tool(name="Detect The Sound Event From The Audio", func=self.detection.inference, description="useful for when you want to know what event in the audio and the sound event start or end time, this tool will generate an image of all predict events, receives audio_path as input. " "The input to this tool should be a string, representing the audio_path. "), Tool(name="Sythesize Binaural Audio From A Mono Audio Input", func=self.binaural.inference, description="useful for when you want to transfer your mono audio into binaural audio, receives audio_path as input. " "The input to this tool should be a string, representing the audio_path. "), Tool(name="Extract Sound Event From Mixture Audio Based On Language Description", func=self.extraction.inference, description="useful for when you extract target sound from a mixture audio, you can describe the target sound by text, receives audio_path and text as input. " "The input to this tool should be a comma seperated string of two, representing mixture audio path and input text."), Tool(name="Target Sound Detection", func=self.TSD.inference, description="useful for when you want to know when the target sound event in the audio happens. You can use language descriptions to instruct the model. receives text description and audio_path as input. " "The input to this tool should be a comma seperated string of two, representing audio path and the text 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': AUDIO_CHATGPT_PREFIX, 'format_instructions': AUDIO_CHATGPT_FORMAT_INSTRUCTIONS, 'suffix': AUDIO_CHATGPT_SUFFIX}, ) return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) else: self.tools = [ Tool(name="Generate Audio From User Input Text", func=self.t2a.inference, description="useful for when you want to generate an audio from a user input text and it saved it to a file." "The input to this tool should be a string, representing the text used to generate audio."), Tool( name="Style Transfer", func= self.tts_ood.inference, description="useful for when you want to generate speech samples with styles (e.g., timbre, emotion, and prosody) derived from a reference custom voice." "Like: Generate a speech with style transferred from this voice. The text is xxx., or speak using the voice of this audio. The text is xxx." "The input to this tool should be a comma seperated string of two, representing reference audio path and input text."), Tool(name="Generate Singing Voice From User Input Text, Note and Duration Sequence", func= self.t2s.inference, description="useful for when you want to generate a piece of singing voice (Optional: from User Input Text, Note and Duration Sequence) and save it to a file." "If Like: Generate a piece of singing voice, the input to this tool should be \"\" since there is no User Input Text, Note and Duration Sequence ." "If Like: Generate a piece of singing voice. Text: xxx, Note: xxx, Duration: xxx. " "Or Like: Generate a piece of singing voice. Text is xxx, note is xxx, duration is xxx." "The input to this tool should be a comma seperated string of three, representing text, note and duration sequence since User Input Text, Note and Duration Sequence are all provided."), Tool(name="Synthesize Speech Given the User Input Text", func=self.tts.inference, description="useful for when you want to convert a user input text into speech audio it saved it to a file." "The input to this tool should be a string, representing the text used to be converted to speech."), Tool(name="Generate Text From The Audio", func=self.a2t.inference, description="useful for when you want to describe an audio in text, receives audio_path as input." "The input to this tool should be a string, representing the audio_path."), Tool(name="Generate a talking human portrait video given a input Audio", func=self.geneface.inference, description="useful for when you want to generate a talking human portrait video given a input audio." "The input to this tool should be a string, representing the audio_path."), Tool(name="Generate Binaural Audio From A Mono Audio Input", func=self.binaural.inference, description="useful for when you want to transfer your mono audio into binaural audio, receives audio_path as input. " "The input to this tool should be a string, representing the audio_path. "), Tool(name="Extract Sound Event From Mixture Audio Based On Language Description", func=self.extraction.inference, description="useful for when you extract target sound from a mixture audio, you can describe the target sound by text, receives audio_path and text as input. " "The input to this tool should be a comma seperated string of two, representing mixture audio path and input text."), Tool(name="Target Sound Detection", func=self.TSD.inference, description="useful for when you want to know when the target sound event in the audio happens. You can use language descriptions to instruct the model. receives text description and audio_path as input. " "The input to this tool should be a comma seperated string of two, representing audio path and the text 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': AUDIO_CHATGPT_PREFIX, 'format_instructions': AUDIO_CHATGPT_FORMAT_INSTRUCTIONS, 'suffix': AUDIO_CHATGPT_SUFFIX}, ) return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True) 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}) if res['intermediate_steps'] == []: print("======>Current memory:\n %s" % self.agent.memory) response = res['output'] state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(visible=False), gr.Video.update(visible=False), gr.Image.update(visible=False), gr.Button.update(visible=False) else: tool = res['intermediate_steps'][0][0].tool if tool == "Generate Image From User Input Text" or tool == "Generate Text From The Audio" or tool == "Target Sound Detection": 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, gr.Audio.update(visible=False), gr.Video.update(visible=False), gr.Image.update(visible=False), gr.Button.update(visible=False) elif tool == "Transcribe Speech": response = res['output'] state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(visible=False), gr.Video.update(visible=False), gr.Image.update(visible=False), gr.Button.update(visible=False) elif tool == "Detect The Sound Event From The Audio": image_filename = res['intermediate_steps'][0][1] response = res['output'] + f"![](/file={image_filename})*{image_filename}*" state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(visible=False), gr.Video.update(visible=False), gr.Image.update(visible=False), gr.Button.update(visible=False) elif tool == "Audio Inpainting": audio_filename = res['intermediate_steps'][0][0].tool_input image_filename = res['intermediate_steps'][0][1] print("======>Current memory:\n %s" % self.agent.memory) response = res['output'] state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(value=audio_filename,visible=True), gr.Video.update(visible=False), gr.Image.update(value=image_filename,visible=True), gr.Button.update(visible=True) elif tool == "Generate a talking human portrait video given a input Audio": video_filename = res['intermediate_steps'][0][1] print("======>Current memory:\n %s" % self.agent.memory) response = res['output'] state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(visible=False), gr.Video.update(value=video_filename,visible=True), gr.Image.update(visible=False), gr.Button.update(visible=False) 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']) audio_filename = res['intermediate_steps'][0][1] state = state + [(text, response)] print("Outputs:", state) return state, state, gr.Audio.update(value=audio_filename,visible=True), gr.Video.update(visible=False), gr.Image.update(visible=False), gr.Button.update(visible=False) def run_image_or_audio(self, file, state, txt): file_type = file.name[-3:] if file_type == "wav": print("===============Running run_audio =============") print("Inputs:", file, state) print("======>Previous memory:\n %s" % self.agent.memory) audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") # audio_load = whisper.load_audio(file.name) audio_load, sr = soundfile.read(file.name) soundfile.write(audio_filename, audio_load, samplerate = sr) description = self.a2t.inference(audio_filename) Human_prompt = "\nHuman: provide an audio named {}. The description is: {}. This information helps you to understand this audio, but you should use tools to finish following tasks, " \ "rather than directly imagine from my description. If you understand, say \"Received\". \n".format(audio_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"<audio src=audio_filename controls=controls></audio>*{audio_filename}*", AI_prompt)] state = state + [(f"*{audio_filename}*", AI_prompt)] print("Outputs:", state) return state, state, gr.Audio.update(value=audio_filename,visible=True), gr.Video.update(visible=False) else: print("===============Running run_image =============") print("Inputs:", file, 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(file.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, gr.Audio.update(visible=False), gr.Video.update(visible=False) def speech(self, speech_input, state): input_audio_filename = os.path.join('audio', str(uuid.uuid4())[0:8] + ".wav") text = self.asr.translate_english(speech_input) 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}) if res['intermediate_steps'] == []: print("======>Current memory:\n %s" % self.agent.memory) response = res['output'] output_audio_filename = self.tts.inference(response) state = state + [(text, response)] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(visible=False) else: tool = res['intermediate_steps'][0][0].tool if tool == "Generate Image From User Input Text" or tool == "Generate Text From The Audio" or tool == "Target Sound Detection": 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']) output_audio_filename = self.tts.inference(res['output']) state = state + [(text, response)] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(visible=False) elif tool == "Transcribe Speech": print("======>Current memory:\n %s" % self.agent.memory) output_audio_filename = self.tts.inference(res['output']) response = res['output'] state = state + [(text, response)] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(visible=False) elif tool == "Detect The Sound Event From The Audio": print("======>Current memory:\n %s" % self.agent.memory) image_filename = res['intermediate_steps'][0][1] output_audio_filename = self.tts.inference(res['output']) response = res['output'] + f"![](/file={image_filename})*{image_filename}*" state = state + [(text, response)] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(visible=False) elif tool == "Generate a talking human portrait video given a input Audio": video_filename = res['intermediate_steps'][0][1] print("======>Current memory:\n %s" % self.agent.memory) response = res['output'] output_audio_filename = self.tts.inference(res['output']) state = state + [(text, response)] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(value=video_filename,visible=True) 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']) audio_filename = res['intermediate_steps'][0][1] Res = "The audio file has been generated and the audio is " output_audio_filename = merge_audio(self.tts.inference(Res), audio_filename) print(output_audio_filename) state = state + [(text, response)] response = res['output'] print("Outputs:", state) return gr.Audio.update(value=None), gr.Audio.update(value=output_audio_filename,visible=True), state, gr.Video.update(visible=False) def inpainting(self, state, audio_filename, image_filename): print("===============Running inpainting =============") print("Inputs:", state) print("======>Previous memory:\n %s" % self.agent.memory) new_image_filename, new_audio_filename = self.inpaint.inference(audio_filename, image_filename) AI_prompt = "Here are the predict audio and the mel spectrum." + f"*{new_audio_filename}*" + f"![](/file={new_image_filename})*{new_image_filename}*" output_audio_filename = self.tts.inference(AI_prompt) self.agent.memory.buffer = self.agent.memory.buffer + 'AI: ' + AI_prompt print("======>Current memory:\n %s" % self.agent.memory) state = state + [(f"Audio Inpainting", AI_prompt)] print("Outputs:", state) return state, state, gr.Image.update(visible=False), gr.Audio.update(value=new_audio_filename, visible=True), gr.Video.update(visible=False), gr.Button.update(visible=False) def clear_audio(self): return gr.Audio.update(value=None, visible=False) def clear_input_audio(self): return gr.Audio.update(value=None) def clear_image(self): return gr.Image.update(value=None, visible=False) def clear_video(self): return gr.Video.update(value=None, visible=False) def clear_button(self): return gr.Button.update(visible=False) if __name__ == '__main__': bot = ConversationBot() with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo: with gr.Row(): gr.Markdown("## AudioGPT") chatbot = gr.Chatbot(elem_id="chatbot", label="AudioGPT", visible=False) state = gr.State([]) with gr.Row() as select_raws: with gr.Column(scale=0.7): interaction_type = gr.Radio(choices=['text', 'speech'], value='text', label='Interaction Type') with gr.Column(scale=0.3, min_width=0): select = gr.Button("Select") with gr.Row(visible=False) as text_input_raws: 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.1, min_width=0): run = gr.Button("🏃‍♂️Run") with gr.Column(scale=0.1, min_width=0): clear_txt = gr.Button("🔄Clear️") with gr.Column(scale=0.1, min_width=0): btn = gr.UploadButton("🖼️Upload", file_types=["image","audio"]) with gr.Row(): outaudio = gr.Audio(visible=False) with gr.Row(): with gr.Column(scale=0.3, min_width=0): outvideo = gr.Video(visible=False) with gr.Row(): show_mel = gr.Image(type="filepath",tool='sketch',visible=False) with gr.Row(): run_button = gr.Button("Predict Masked Place",visible=False) with gr.Row(visible=False) as speech_input_raws: with gr.Column(scale=0.7): speech_input = gr.Audio(source="microphone", type="filepath", label="Input") with gr.Column(scale=0.15, min_width=0): submit_btn = gr.Button("🏃‍♂️Submit") with gr.Column(scale=0.15, min_width=0): clear_speech = gr.Button("🔄Clear️") with gr.Row(): speech_output = gr.Audio(label="Output",visible=False) select.click(bot.init_tools, [interaction_type], [chatbot, select_raws, text_input_raws, speech_input_raws]) txt.submit(bot.run_text, [txt, state], [chatbot, state, outaudio, outvideo, show_mel, run_button]) txt.submit(lambda: "", None, txt) run.click(bot.run_text, [txt, state], [chatbot, state, outaudio, outvideo, show_mel, run_button]) run.click(lambda: "", None, txt) btn.upload(bot.run_image_or_audio, [btn, state, txt], [chatbot, state, outaudio, outvideo]) run_button.click(bot.inpainting, [state, outaudio, show_mel], [chatbot, state, show_mel, outaudio, outvideo, run_button]) clear_txt.click(bot.memory.clear) clear_txt.click(lambda: [], None, chatbot) clear_txt.click(lambda: [], None, state) clear_txt.click(lambda:None, None, txt) clear_txt.click(bot.clear_button, None, run_button) clear_txt.click(bot.clear_image, None, show_mel) clear_txt.click(bot.clear_audio, None, outaudio) clear_txt.click(bot.clear_video, None, outvideo) submit_btn.click(bot.speech, [speech_input, state], [speech_input, speech_output, state, outvideo]) clear_speech.click(bot.clear_input_audio, None, speech_input) clear_speech.click(bot.clear_audio, None, speech_output) clear_speech.click(lambda: [], None, state) clear_speech.click(bot.clear_video, None, outvideo) demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
[ "\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. ", "Here are the predict audio and the mel spectrum.*PLACEHOLDER*![](/file=PLACEHOLDER)*PLACEHOLDER*", "\nHuman: provide an audio named PLACEHOLDER. The description is: PLACEHOLDER. This information helps you to understand this audio, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say \"Received\". \n" ]
2024-01-10
Bhavyasree2003/al-semantic-search
search.py
import os import streamlit as st import pandas as pd import cohere import pinecone from tqdm.auto import tqdm project_ideas = [ "To-Do List Application", "Weather App", "URL Shortener", "Image Gallery", "Chatbot", "News Aggregator", "Password Generator", "Expense Tracker", "Quiz Game", "Social Media Dashboard", "File Encryption/Decryption", "Language Translator", "Stock Market Analyzer", "Music Player", "Recipe Finder" ] project_list = [{'id': id+1, 'idea': idea} for id, idea in enumerate(project_ideas)] dataset = pd.DataFrame(project_list) cohere_api_key = os.environ['COHERE_API_KEY'] def encode_text(text): """ Encodes the given text using the Cohere API. Args: text (str): The text to encode. Returns: numpy.ndarray: The embeddings of the given text. """ # Create a CohereClient instance with the given API key. cohere_client = cohere.Client(api_key=cohere_api_key) # Send the text to the Cohere API for encoding. response = cohere_client.embed(texts=[text], model='large', truncate='LEFT') # Return the embeddings of the text. return response.embeddings[0] def create_index(index_name): """ Creates a Pinecone index with the given name if it doesn't already exist and returns the index. Args: index_name (str): The name of the index to create. Returns: pinecone.Index: The newly created or existing index. """ # Initialize Pinecone with API key and GCP environment. pinecone.init( api_key=os.environ['PINECONE_API_KEY'], environment='asia-northeast1-gcp' ) # Create the index if it doesn't already exist. if index_name not in pinecone.list_indexes(): pinecone.create_index( name=index_name, dimension=4096, metric='cosine', pods=1, pod_type="s1.x1" ) # Return the index. return pinecone.Index(index_name) def index_questions(questions, index, batch_size: int = 5) -> None: """ Indexes a list of questions in an Annoy index. Args: questions: A list of strings, where each string represents a question to be indexed. index: An instance of the AnnoyIndex class that represents the index to be used. batch_size: An integer that represents the number of questions to index at a time. Defaults to 128. """ # Iterate over the questions in batches. for i in tqdm(range(0, len(questions), batch_size)): # Determine the end index of the current batch. i_end = min(i+batch_size, len(questions)) # Create a list of IDs for the current batch. ids = [str(x) for x in range(i, i_end)] # Create a list of metadata objects for the current batch. metadatas = [{'text': text} for text in questions[i:i_end]] # Create a list of embeddings for the current batch. embeddings = [encode_text(text) for text in questions[i:i_end]] # Zip the IDs, embeddings, and metadata objects together into a list of records. records = zip(ids, embeddings, metadatas) # Upsert the records into the index. index.upsert(records) def query_index(index, query, top_k=3): """ Queries an index for the top-k most relevant results to a given query. Args: index (pinecone.Index): The index to query. query (str): The query string to search for. top_k (int): The number of results to return. Defaults to 3. Returns: results (dict): A dictionary containing the top-k most relevant results. """ # Encode the query string. xq = encode_text(query) # Query the index for the top-k most relevant results, including metadata. results = index.query(xq, top_k=top_k, include_metadata=True) # Return the top-k most relevant results. return results def delete_index(index_name): """ Deletes the Pinecone index with the given name. Args: index_name (str): The name of the index to delete. """ # Initialize the Pinecone API with the API key stored in the environment variable. pinecone.init(api_key=os.environ['PINECONE_API_KEY']) # Delete the index with the given name. pinecone.delete_index(index_name) def main(): """ This is the main function that runs the semantic search application. """ # Set the title of the Streamlit app. st.title("Semantic Search Application") # Load the dataset and extract questions. # dataset = load_dataset('quora', split='train') # Load the project ideas from the DataFrame. df = pd.DataFrame({ 'id': [i for i in range(1, len(project_ideas) + 1)], 'idea': project_ideas }) # Extract the questions from the DataFrame. questions = df['idea'].tolist() # Create and index the questions. index_name = 'semantic-search-fast' index = create_index(index_name) index_questions(questions, index) # Get user query and display search results. query = st.text_input("Enter your query:") if st.button("Search"): results = query_index(index, query) st.write("Top search results:") for result in results['matches']: st.write(f"{round(result['score'], 2)}: {result['metadata']['text']}") # Delete the index. delete_index(index_name) if __name__ == "__main__": main()
[]
2024-01-10
timlam007/finance_chatbot
kafka~app-kafka.py
# __import__('pysqlite3') # import sys # sys.modules['sqlite3'] = sys.modules.pop('pysqlite3') from agents.SQLagent import build_sql_agent from agents.csv_chat import build_csv_agent from utils.utility import ExcelLoader # app.py from typing import List, Union, Optional from langchain.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader from langchain.llms import OpenAI from langchain.callbacks import get_openai_callback from langchain.chat_models import ChatOpenAI from langchain.embeddings.openai import OpenAIEmbeddings from langchain.schema import (SystemMessage, HumanMessage, AIMessage) from langchain.llms import LlamaCpp from langchain.embeddings import LlamaCppEmbeddings from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.prompts import PromptTemplate from langchain.vectorstores import Qdrant import streamlit as st from langchain.vectorstores import Chroma from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.agents import initialize_agent, Tool, AgentType from langchain.memory import ConversationBufferMemory from langchain.chains import RetrievalQAWithSourcesChain import os import pandas as pd from kafka import KafkaProducer st.session_state.csv_file_paths = [] memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100) PROMPT_TEMPLATE = """ Use the following pieces of context enclosed by triple backquotes to answer the question at the end. \n\n Context: ``` {context} ``` \n\n Question: [][][][]{question}[][][][] \n Answer:""" def open_ai_key(): with st.sidebar: openai_api_key = st.text_input("OpenAI API Key", key="chatbot_api_key", type="password") "[Get an OpenAI API key](https://platform.openai.com/account/api-keys)" if not openai_api_key: st.info("Please add your OpenAI API key to continue.") st.stop() os.environ["OPENAI_API_KEY"] = openai_api_key @st.cache_data def dbActive(): os.environ['DB_ACTIVE'] = 'false' def init_page() -> None: st.set_page_config( ) st.sidebar.title("Options") icon, title = st.columns([3, 20]) with icon: st.image('./img/image.png') with title: st.title('Finance Chatbot') st.session_state['db_active'] = False def init_messages() -> None: clear_button = st.sidebar.button("Clear Conversation", key="clear") if clear_button or "messages" not in st.session_state: st.session_state.messages = [ SystemMessage( content=( "You are a helpful AI QA assistant. " "When answering questions, use the context provided to you." "If you don't know the answer, just say that you don't know, " "don't try to make up an answer. " ) ) ] st.session_state.costs = [] def get_csv_file() -> Optional[str]: """ Function to load PDF text and split it into chunks. """ import tempfile st.header("Upload Document or Connect to a Databse") uploaded_files = st.file_uploader( label="Here, upload your documents you want AskMAY to use to answer", type= ["csv", 'xlsx', 'pdf','docx'], accept_multiple_files= True ) if uploaded_files: all_docs = [] csv_paths = [] all_files = [] for file in uploaded_files: Loader = None if file.type == "text/plain": Loader = TextLoader elif file.type == "application/pdf": Loader = PyPDFLoader elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": Loader = Docx2txtLoader elif file.type == "text/csv": flp = './temp.csv' pd.read_csv(file).to_csv(flp, index=False) csv_paths.append(flp) elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": loader = ExcelLoader(file) paths = loader.load() csv_paths.extend(paths) else: print(file.type) file.type raise ValueError('File type is not supported') if Loader: with tempfile.NamedTemporaryFile(delete=False) as tpfile: tpfile.write(file.getvalue()) loader = Loader(tpfile.name) docs = loader.load() all_docs.extend(docs) #text = "\n\n".join([page.extract_text() for page in pdf_reader.pages]) if all_docs: documents = text_splitter.split_documents(all_docs) all_files.append(('docs', documents)) if csv_paths: all_files.append(('csv', csv_paths)) all_files = tuple(all_files) return all_files else: return None def get_db_credentials(model_name, temperature, chain_mode='Database'): """ creates a form for the user to input database login credentials """ # Check if the form has already been submitted db_active = os.environ['DB_ACTIVE'] if db_active == "true": print(db_active) return st.session_state['models'] else: username = None host = None port = None db = None password = None import time pholder = st.empty() with pholder.form('Database_Login'): st.write("Enter Database Credentials ") username = st.text_input('Username').strip() password = st.text_input('Password', type='password',).strip() rdbs = st.selectbox('Select RDBS:', ("Postgres", 'MS SQL Server/Azure SQL', "MySQL", "Oracle") ) port = st.number_input('Port') host = st.text_input('Hostname').strip() db = st.text_input('Database name').strip() submitted = st.form_submit_button('Submit') if submitted: with st.spinner("Logging into database..."): llm_chain, llm = init_agent(model_name=model_name, temperature=temperature, rdbs = rdbs, username=username, password=password, port=port, host=host, database=db, chain_mode = chain_mode) st.session_state['models'] = (llm_chain, llm) st.success("Login Success") os.environ['DB_ACTIVE'] = "true" db_active = os.environ['DB_ACTIVE'] st.session_state['db_active'] = True time.sleep(2) pholder.empty() # If the form has already been submitted, return the stored models if db_active == "true": #return st.session_state['models'] mds = st.session_state['models'] st.write("Reached") return mds else: st.stop() def build_vector_store( docs: str, embeddings: Union[OpenAIEmbeddings, LlamaCppEmbeddings]) \ -> Optional[Qdrant]: """ Store the embedding vectors of text chunks into vector store (Qdrant). """ if docs: with st.spinner("Loading FIle ..."): chroma = Chroma.from_documents( docs, embeddings ) st.success("File Loaded Successfully!!") else: chroma = None return chroma # Select model def select_llm() -> Union[ChatOpenAI, LlamaCpp]: """ Read user selection of parameters in Streamlit sidebar. """ model_name = st.sidebar.radio("Choose LLM:", ("gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", "gpt-4", "text-davinci-003", )) temperature = st.sidebar.slider("Temperature:", min_value=0.0, max_value=1.0, value=0.0, step=0.01) chain_mode = st.sidebar.selectbox( "What would you like to query?", ("Documents", "CSV|Excel", 'Database') ) #api_key = st.sidebar.text_input('OPENAI API Key') return model_name, temperature, chain_mode,# api_key def init_agent(model_name: str, temperature: float, **kwargs) -> Union[ChatOpenAI, LlamaCpp]: """ Load LLM. """ llm_agent = None # Initialize llm_agent with a default value if model_name.startswith("gpt-"): llm = ChatOpenAI(temperature=temperature, model_name=model_name) elif model_name.startswith("text-dav"): llm = OpenAI(temperature=temperature, model_name=model_name) elif model_name.startswith("llama-2-"): callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) llm = LlamaCpp( model_path=f"./models/{model_name}.bin", input={"temperature": temperature, "max_length": 2048, "top_p": 1 }, n_ctx=2048, callback_manager=callback_manager, verbose=False, # True ) chain_mode = kwargs['chain_mode'] if chain_mode == 'Database': rdbs = kwargs['rdbs'] username = kwargs['username'] password = kwargs['password'] host = kwargs['host'] port = kwargs['port'] database = kwargs['database'] #print('----------------------------------------------------------------') #st.write(print(rdbs,username,password,host,port,database )) #print(rdbs,username,password,host,port,database ) llm_agent = build_sql_agent(llm=llm, rdbs=rdbs, username=username, password=password, host=host, port=port, database=database) if chain_mode == 'CSV|Excel': file_paths = kwargs['csv'] if file_paths is not None: with st.spinner("Loading CSV FIle ..."): llm_agent = build_csv_agent(llm, file_path=file_paths) return llm_agent, llm def get_retrieval_chain(model_name: str, temperature: float, **kwargs) -> Union[ChatOpenAI, LlamaCpp]: if model_name.startswith("gpt-"): llm = ChatOpenAI(temperature=temperature, model_name=model_name) elif model_name.startswith("text-dav"): llm = OpenAI(temperature=temperature, model_name=model_name) elif model_name.startswith("llama-2-"): callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) llm = LlamaCpp( model_path=f"./models/{model_name}.bin", input={"temperature": temperature, "max_length": 2048, "top_p": 1 }, n_ctx=2048, callback_manager=callback_manager, verbose=False, # True ) docsearch = kwargs['docsearch'] retrieval_chain = RetrievalQAWithSourcesChain.from_chain_type( llm, retriever = docsearch.as_retriever(max_tokens_limit=4097) ) return retrieval_chain, llm def load_embeddings(model_name: str) -> Union[OpenAIEmbeddings, LlamaCppEmbeddings]: """ Load embedding model. """ if model_name.startswith("gpt-") or model_name.startswith("text-dav"): return OpenAIEmbeddings() elif model_name.startswith("llama-2-"): return LlamaCppEmbeddings(model_path=f"./models/{model_name}.bin") def get_answer(llm_chain,llm, message) -> tuple[str, float]: """ Get the AI answer to user questions. """ import langchain if isinstance(llm, (ChatOpenAI, OpenAI)): with get_openai_callback() as cb: try: if isinstance(llm_chain, RetrievalQAWithSourcesChain): response = llm_chain(message) answer = str(response['answer'])# + "\n\nSOURCES: " + str(response['sources']) else: answer = llm_chain.run(message) except langchain.schema.output_parser.OutputParserException as e: response = str(e) if not response.startswith("Could not parse tool input: "): raise e answer = response.removeprefix("Could not parse LLM output: `").removesuffix("`") return answer, cb.total_cost def find_role(message: Union[SystemMessage, HumanMessage, AIMessage]) -> str: """ Identify role name from langchain.schema object. """ if isinstance(message, SystemMessage): return "system" if isinstance(message, HumanMessage): return "user" if isinstance(message, AIMessage): return "assistant" raise TypeError("Unknown message type.") def convert_langchainschema_to_dict( messages: List[Union[SystemMessage, HumanMessage, AIMessage]]) \ -> List[dict]: """ Convert the chain of chat messages in list of langchain.schema format to list of dictionary format. """ return [{"role": find_role(message), "content": message.content } for message in messages] def extract_userquesion_part_only(content): """ Function to extract only the user question part from the entire question content combining user question and pdf context. """ content_split = content.split("[][][][]") if len(content_split) == 3: return content_split[1] return content def main() -> None: import openai init_page() dbActive() try: open_ai_key() if 'history' not in st.session_state: st.session_state['history'] = [] model_name, temperature, chain_mode = select_llm() embeddings = load_embeddings(model_name) files = get_csv_file() paths, texts, chroma = None, None, None if chain_mode == 'Database': llm_chain, llm = None, None try: print(os.environ['DB_ACTIVE']) if os.environ['DB_ACTIVE'] == "true": llm_chain, llm = st.session_state['models'] else: llm_chain, llm = get_db_credentials(model_name=model_name, temperature=temperature, chain_mode=chain_mode) except KeyError: st.sidebar.warning('Provide a Database Log in Details') os.environ['DB_ACTIVE'] = "false" llm_chain, llm = get_db_credentials(model_name=model_name, temperature=temperature, chain_mode=chain_mode) except Exception as e: err = str(e) st.error(err) elif files is not None: for fp in files: if fp[0] == 'csv': paths = fp[1] elif fp[0] == 'docs': texts = fp[1] if texts: import openai try: chroma = build_vector_store(texts, embeddings) except openai.error.AuthenticationError: st.echo('Invalid OPENAI API KEY') if chain_mode == "CSV|Excel": if paths is None: st.sidebar.warning("Note: No CSV or Excel data uploaded. Provide atleast one data source") llm_chain, llm = init_agent(model_name, temperature, csv=paths, chain_mode=chain_mode) elif chain_mode == 'Documents': try: assert chroma != None llm_chain, llm = get_retrieval_chain(model_name, temperature, docsearch = chroma) except AssertionError as e: st.sidebar.warning('Upload at least one document') llm_chain, llm = None, None else: if chain_mode == "CSV|Excel": try: assert paths != None except AssertionError as e: st.sidebar.warning("Note: No CSV data uploaded. Upload at least one csv or excel file") elif chain_mode == 'Documents': try: assert chroma != None except AssertionError as e: st.sidebar.warning('Upload at least one document or swith to data query') init_messages() # Supervise user input st.header("Personal FinanceGPT") container = st.container() with container: user_input = st.chat_input("Input your question!") if user_input: try: assert type(llm_chain) != type(None) if chroma: context = [c.page_content for c in chroma.similarity_search( user_input, k=10)] user_input_w_context = PromptTemplate( template=PROMPT_TEMPLATE, input_variables=["context", "question"]) \ .format( context=context, question=user_input) else: user_input_w_context = user_input st.session_state.messages.append( HumanMessage(content=user_input_w_context)) with st.spinner("Assistant is typing ..."): answer, cost = get_answer(llm_chain,llm, user_input) st.write(answer) st.session_state.messages.append(AIMessage(content=answer)) st.session_state.costs.append(cost) except AssertionError: st.warning('Please provide a context source') # Display chat history chat_history = [] messages = st.session_state.get("messages", []) for message in messages: if isinstance(message, AIMessage): chat_history.append({'assistant' : message.content}) with st.chat_message("assistant"): st.markdown(message.content) elif isinstance(message, HumanMessage): chat_history.append({'user': extract_userquesion_part_only(message.content)}) with st.chat_message("user"): st.markdown(extract_userquesion_part_only(message.content)) # Create a Kafka producer instance with the provided configuration try: producer = KafkaProducer(bootstrap_servers='zkless-kafka-bootstrap:9092') # Define the topic name and key (modify as needed) topic_name = "tim-topic" key = "tim_key" # Print chat history and send to Kafka for entry in chat_history: for role, msg in entry.items(): print(f"{role.capitalize()}: {msg}") # Encode the message to bytes msg_encoded = msg.encode('utf-8') # Produce a message to the Kafka topic producer.send(topic_name, key=key, value=msg_encoded) # Ensure all messages are sent producer.flush() except Exception as e: print(f"Optional: Failed to send message to Kafka due to: {e}") costs = st.session_state.get("costs", []) st.sidebar.markdown("## Costs") st.sidebar.markdown(f"**Total cost: ${sum(costs):.5f}**") for cost in costs: st.sidebar.markdown(f"- ${cost:.5f}") except openai.error.AuthenticationError as e: st.warning("Incorrect API key provided: You can find your API key at https://platform.openai.com/account/api-keys") except openai.error.RateLimitError: st.warning('OpenAI RateLimit: Your API Key has probably exceeded the maximum requests per min or per day') # streamlit run app.py if __name__ == "__main__": main()
[ "\nUse the following pieces of context enclosed by triple backquotes to answer the question at the end.\n\n\n\nContext:\n```\n{context}\n```\n\n\n\nQuestion: [][][][]{question}[][][][]\n\n\nAnswer:", "You are a helpful AI QA assistant. When answering questions, use the context provided to you.If you don't know the answer, just say that you don't know, don't try to make up an answer. " ]
2024-01-10
timlam007/finance_chatbot
utils~utility.py
import os import pandas as pd class ExcelLoader(): def __init__(self, file): import pandas as pd self.status = False self.name = 'ExcelLoader' self.file = file self.loader = pd.ExcelFile self.ext = ['xlsx'] def load(self): from langchain.document_loaders.csv_loader import CSVLoader ssheet = self.loader(self.file) try: os.mkdir('temp') except FileExistsError: pass docs = [] for i,sheet in enumerate(ssheet.sheet_names): df = ssheet.parse(sheet) temp_path = f'./temp/{sheet}.csv' docs.append(temp_path) df.to_csv(temp_path, index=False) return docs def process_csv_file(file): file_paths = [] if file.split('.')[-1] == 'csv': file_paths.append(file) elif file.split('.')[-1] == 'xlsx': loader = ExcelLoader(file) paths = loader.load() file_paths.extend(paths) if len(file_paths) == 1: return file_paths[0] return file_paths # def get_loader(file_type): # import tempfile # if file_type == "text/plain": # Loader = TextLoader # elif file_type == "application/pdf": # Loader = PyPDFLoader # elif file_type == "text/csv": # Loader = CSVLoader # csv_files.append(file) # elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": # Loader = ExcelLoader # else: # raise ValueError('File type is not supported')
[]
2024-01-10
timlam007/finance_chatbot
agents~SQLagent.py
import os from langchain.agents import create_csv_agent, AgentType from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType from langchain.agents import create_sql_agent from langchain.agents.agent_toolkits import SQLDatabaseToolkit from langchain.sql_database import SQLDatabase def postgres_uri(username, password, host, port, database): try: assert username != None assert password != None assert host != None assert port != None assert database != None except: raise ValueError("Check all credential") port = int(port) os.environ["DB_USER"] = username os.environ["DB_PASSWORD"] = password os.environ["DB_HOST"] = host os.environ["DB_NAME"] = database # db = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}" db = f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}" return db def mysql_uri(username, password, host, port, database): try: assert username != None assert password != None assert host != None assert port != None assert database != None except: raise ValueError("Check all credential") port = int(port) # Set environment variables with user-entered values os.environ["DB_USER"] = username os.environ["DB_PASSWORD"] = password os.environ["DB_HOST"] = host os.environ["DB_NAME"] = database db = f"mysql+pymysql://{username}:{password}@{host}/{database}" return db def build_sql_agent(llm,rdbs, **kwargs): #llm = OpenAI(temperature=0,model="text-davinci-003", streaming=True) print(rdbs.lower()) print('----------------------------------------------------------------') if rdbs.lower() == 'postgres': uri = postgres_uri(**kwargs) elif rdbs.lower() == 'mysql': print(rdbs.lower()) uri = mysql_uri(**kwargs) else: print('database not connected yet') db = SQLDatabase.from_uri(uri) toolkit = SQLDatabaseToolkit(db=db, llm=llm) sql_agent = create_sql_agent( llm=llm, toolkit=toolkit, verbose=True, handle_parsing_errors=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, ) return sql_agent def sql_as_tool(agent): return Tool.from_function( name = "sql_retrieval_tool", func=agent.run, description= "Use this tool if you need to run queries against the database.", ) #sql_agent = build_sql_agent() #message = "what is the `total score` for 'Sunday Nwoye' added to 'Helen Opayemi'" #sql_agent.run(input=message) """if chroma: context = [c.page_content for c in chroma.similarity_search( user_input, k=10)] user_input_w_context = PromptTemplate( template=PROMPT_TEMPLATE, input_variables=["context", "question"]) \ .format( context=context, question=user_input) """
[]
2024-01-10
timlam007/finance_chatbot
agents~csv_chat.py
from langchain.llms import OpenAI from langchain.memory import ConversationBufferMemory from langchain.agents import create_csv_agent, AgentType from langchain.agents import Tool from langchain.memory import ConversationBufferMemory welcome_message = """Welcome to the Chainlit PDF QA demo! To get started: 1. Upload a PDF or text file 2. Ask a question about the file """ def build_csv_agent(llm, file_path): assert isinstance(file_path, list) if len(file_path) == 1: file_path = file_path[0] csv_agent = create_csv_agent( llm, file_path, verbose=True, handle_parsing_errors=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, ) return csv_agent def csv_as_tool(agent): return Tool.from_function( name = "csv_retrieval_tool", func= agent.run, description= 'This tool useful for statistics, calculations, plotting and as well as data aggregation' ) """while True: message = input('User:> ') try: response = except ValueError as e: response = str(e) if not response.startswith("Could not parse tool input: "): raise e response = response.removeprefix("Could not parse LLM output: `").removesuffix("`") print('Chatbot:> ', response)"""
[]
2024-01-10
xcnkx/repository-chat
app~retriver.py
from langchain.vectorstores import Chroma from langchain.vectorstores.base import VectorStoreRetriever from langchain.embeddings.base import Embeddings from langchain.schema import Document def create_retriever( embeddings: Embeddings, splits: list[Document] ) -> VectorStoreRetriever: try: vectorstore = Chroma.from_documents(splits, embeddings) except (IndexError, ValueError) as e: raise Exception(f"Error creating vectorstore: {e}") retriever = vectorstore.as_retriever(search_type="mmr") retriever.search_kwargs["distance_metric"] = "cos" retriever.search_kwargs["fetch_k"] = 100 retriever.search_kwargs["maximal_marginal_relevance"] = True retriever.search_kwargs["k"] = 10 return retriever
[]
2024-01-10
xcnkx/repository-chat
tests~test_retriever.py
from app.retriver import create_retriever from langchain.embeddings.openai import OpenAIEmbeddings from langchain.schema import Document def test_create_retriever(): # Create some dummy data embeddings = OpenAIEmbeddings() splits = [ Document( page_content="This is the first document.", metadata={"title": "first document"}, ), Document( page_content="This is the second document.", metadata={"title": "second document"}, ), Document( page_content="This is the third document.", metadata={"title": "third document"}, ), ] # Call the function retriever = create_retriever(embeddings, splits) # Check that the retriever returns the expected results query = "first document" results = retriever.get_relevant_documents(query) assert len(results) == 3 assert results[0].page_content == "This is the first document." query = "second document" results = retriever.get_relevant_documents(query) assert len(results) == 3 assert results[0].page_content == "This is the second document." query = "third document" results = retriever.get_relevant_documents(query) assert len(results) == 3 assert results[0].page_content == "This is the third document."
[]
2024-01-10
MannaYang/Flutter-ChatGPT
backend~service~function_call.py
import requests from langchain import PromptTemplate default_template = """请用中文回答问题 历史对话: {history} Human: {input} AI: """ default_prompt = PromptTemplate(input_variables=["history", "input"], template=default_template) # 参数 # functions call functionCall = [ 'query_attendance_data', 'query_inventory_data', 'submit_leave_data' ] # functions info functionInfo = [ { "name": "query_attendance_data", "description": "Query departmental attendance data for the current time", "parameters": { "type": "object", "properties": { "attendance_date": { "type": "string", "description": "Attendance dates, such as:2023-07-17,2023-07-16,2023-07-15,2023-07-14,format is{yyyy-MM-dd}" }, "attendance_depart": { "type": "string", "description": "Attendance departments,such as:研发部(R&D),市场部(Marketing),人力资源(HR)" } }, "required": ["attendance_date", 'attendance_depart'] } }, { "name": "query_inventory_data", "description": "Query Zeiss lens inventory data", "parameters": { "type": "object", "properties": { "brand": { "type": "string", "description": "Brand name,such as:Zeiss,Essilor,format is{brand:}" }, "sku_code": { "type": "string", "description": "Sku code,such as:78969993499538304,format is{skuCode:}" } }, "required": ["brand"] } }, { "name": "submit_leave_data", "description": "Submission of leave requests based on the given date-time-reason", "parameters": { "type": "object", "properties": { "date_start": { "type": "string", "description": "Leave start date,such as:2023-07-18,2023-07-17,2023-07-16,format is{yyyy-MM-dd}" }, "time_start": { "type": "string", "description": "Leave start time,such as:09:00,10:00,11:00,format is{HH:mm}" }, "date_end": { "type": "string", "description": "Leave end date,such as:2023-07-18,2023-07-17,2023-07-16,format is{yyyy-MM-dd}" }, "time_end": { "type": "string", "description": "Leave end time,such as:16:00,17:00,18:00,format is{HH:mm}" }, "leave_reason": { "type": "string", "description": "Leave reason,such as:Unable to go to work normally due to hot weather,Need to go to the hospital if you are not feeling well,Children are sick and need to be taken care of" }, }, "required": [ "date_start", "time_start", "date_end", "time_end", "leave_reason" ] } }, ] # # Query Zeiss lens inventory data with ownerName and skuCode # async def query_inventory_data(brand: str, sku_code: str): print('Query Zeiss lens inventory data with ownerName and skuCode') url = "https://jsonplaceholder.typicode.com/posts" headers = { "content-type": "application/json" # 'Content-Type': 'application/json; charset=UTF-8' } body = { "code": 0, "success": True, "result": [ { "skuCode": sku_code, "brand": brand, "model": "Zeiss-2023", "lensType": "lens001", "stockQuantity": 3000 }, { "skuCode": sku_code, "brand": brand, "model": "Zeiss-2023", "lensType": "lens002", "stockQuantity": 200 }, { "skuCode": sku_code, "brand": brand, "model": "Zeiss-2023", "lensType": "lens003", "stockQuantity": 100 }, ] } print(f"Query Zeiss lens inventory params - {body}") return requests.post(url, json=body, headers=headers) # # Query attendance data # async def query_attendance_data(date: str, depart: str): print('Query attendance data') url = 'https://jsonplaceholder.typicode.com/posts' headers = {"content-type": "application/json"} body = { "code": 0, "success": True, "result": { "depart": depart, "dateStart": date, "dateEnd": date, "lateArrival": 30, "earlyDeparture": 3, "absenteeism": 1, "leave": 2, "businessTrips": 10 } } return requests.post(url, json=body, headers=headers) # # Submit leave data # async def submit_leave_data( date_start: str, time_start: str, date_end: str, time_end: str, leave_reason: str ): print('Submit leave data') url = 'https://jsonplaceholder.typicode.com/posts' headers = { 'Content-Type': 'application/json; charset=UTF-8' } body = { 'code': 0, 'success': True, 'result': { "dateStart": date_start, "timeStart": time_start, "dateEnd": date_end, "timeEnd": time_end, "leaveReason": leave_reason, "leaveStatus": "Leave submitted successfully" } } return requests.post(url, json=body, headers=headers)
[ "input", "application/json", "请用中文回答问题\n\n历史对话:\n{history}\nHuman: {input}\nAI:\n" ]
2024-01-10
pollyren/repo_search
ask_openai.py
#!/usr/bin/env python3 from bs4 import BeautifulSoup from pathlib import Path from datetime import datetime import openai import pandas as pd import re def create_prompt(code): prompt = r''' Please answer the following questions regarding the code. Please indicate the question each portions of the response is answering. Please do not use any additional sentences providing explanation other that what is asked by the question. Each question should be answerable in less than 20 words. Do not answer in complete sentences. Do not repeat the question back to me. There is an example below for what a response should look like. 1) Yes or No: Does the ChatCompletions API call use both a system and user for the conversation? 2) Is the user message static or dynamic? A static message refers to a constant string, while a dynamic message uses user input and variables to create the string. Dynamic inputs are variables taken from the command line that are inserted into the message string, typically by using format strings or concatenation. Function parameters are not dynamic tokens. 3) If the prompt is in English, how many static words are in the user content prompt? If it is not in English, return "Not English". 4) Where are the dynamic tokens located within the prompt template? Are they located at the beginning of the template, in the middle, or at the end? Return N/A only if the user prompt is static. 5) How many steps are there between the command line input and the final dynamic tokens? What types of steps are they (i.e., concatenation, slicing, random generator)? If there are no steps, return 0. Return N/A only if the user prompt is static. //which part is dynamic vs static Example 1: #!/usr/bin/env python3 import openai import sys import os import configparser # Get config dir from environment or default to ~/.config CONFIG_DIR = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) API_KEYS_LOCATION = os.path.join(CONFIG_DIR, 'openaiapirc') # Read the organization_id and secret_key from the ini file ~/.config/openaiapirc # The format is: # [openai] # organization_id=<your organization ID> # secret_key=<your secret key> # If you don't see your organization ID in the file you can get it from the # OpenAI web site: https://openai.com/organizations def create_template_ini_file(): """ If the ini file does not exist create it and add the organization_id and secret_key """ if not os.path.isfile(API_KEYS_LOCATION): with open(API_KEYS_LOCATION, 'w') as f: f.write('[openai]\n') f.write('organization_id=\n') f.write('secret_key=\n') f.write('model=gpt-3.5-turbo-0613\n') print('OpenAI API config file created at {}'.format(API_KEYS_LOCATION)) print('Please edit it and add your organization ID and secret key') print('If you do not yet have an organization ID and secret key, you\n' 'need to register for OpenAI Codex: \n' 'https://openai.com/blog/openai-codex/') sys.exit(1) def initialize_openai_api(): """ Initialize the OpenAI API """ # Check if file at API_KEYS_LOCATION exists create_template_ini_file() config = configparser.ConfigParser() config.read(API_KEYS_LOCATION) openai.organization_id = config['openai']['organization_id'].strip('"').strip("'") openai.api_key = config['openai']['secret_key'].strip('"').strip("'") if 'model' in config['openai']: model = config['openai']['model'].strip('"').strip("'") else: model = 'gpt-3.5-turbo' return model model = initialize_openai_api() cursor_position_char = int(sys.argv[1]) # Read the input prompt from stdin. buffer = sys.stdin.read() prompt_prefix = 'Here is the code: #!/bin/zsh\n\n' + buffer[:cursor_position_char] prompt_suffix = buffer[cursor_position_char:] full_command = prompt_prefix + prompt_suffix response = openai.ChatCompletion.create(model=model, messages=[ { "role":'system', "content": "You are a zsh shell expert, please help me complete the following command, you should only output the completed command, no need to include any other explanation", }, { "role":'user', "content": full_command, } ]) completed_command = response['choices'][0]['message']['content'] sys.stdout.write(f"\n{completed_command.replace(prompt_prefix, '', 1)}") An example response to Example 1 would be as follows: 1) Yes. 2) Dynamic. The dynamic variable is `buffer`. 3) 5. 4) End. 5) There are 3 total operations: 2 slicing operations of the command line input, 1 concatenation operation on the two results. Example 2: import sys, re from pathlib import Path from os import path sys.path.append(str(Path(__file__).parent.parent.parent)) import g4f def read_code(text): if match := re.search(r"```(python|py|)\n(?P<code>[\S\s]+?)\n```", text): return match.group("code") path = input("Path: ") with open(path, "r") as file: code = file.read() prompt = f""" Improve the code in this file: ```py {code} ``` Don't remove anything. Add typehints if possible. Don't add any typehints to kwargs. Don't remove license comments. """ print("Create code...") response = [] for chunk in g4f.ChatCompletion.create( model=g4f.models.gpt_35_long, messages=[{"role": "user", "content": prompt}], timeout=300, stream=True ): response.append(chunk) print(chunk, end="", flush=True) print() response = "".join(response) if code := read_code(response): with open(path, "w") as file: file.write(code) An example response to Example 2 would be as follows: 1) No. 2) Dynamic. The dynamic variables for the user prompt is `path`. 3) 23. 4) Middle. 5) There are 2 steps between the command line input and the final dynamic tokens. The steps are: reading the code from the file and inserting the code into the prompt. Example 3: import openai # openai.log = "debug" openai.api_key = "sk-" openai.api_base = "https://api.chatanywhere.com.cn/v1" # 非流式响应 # completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}]) # print(completion.choices[0].message.content) def gpt_35_api_stream(messages: list): """为提供的对话消息创建新的回答 (流式传输) Args: messages (list): 完整的对话消息 api_key (str): OpenAI API 密钥 Returns: tuple: (results, error_desc) """ try: response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=messages, stream=True, ) completion = {'role': '', 'content': ''} for event in response: if event['choices'][0]['finish_reason'] == 'stop': print(f'收到的完成数据: {completion}') break for delta_k, delta_v in event['choices'][0]['delta'].items(): print(f'流响应数据: {delta_k} = {delta_v}') completion[delta_k] += delta_v messages.append(completion) # 直接在传入参数 messages 中追加消息 return (True, '') except Exception as err: return (False, f'OpenAI API 异常: {err}') if __name__ == '__main__': messages = [{'role': 'user','content': '鲁迅和周树人的关系'},] print(gpt_35_api_stream(messages)) print(messages) An example response to Example 3 would be as follows: 1) No. 2) Static. 3) Not English. 4) N/A. 5) N/A. ''' return f'The following is a piece of code: {code}\n' + prompt def transform_answers(answers): res = (0, 0, [], 0, 0, 0) try: q1, q2, q3, q4, q5 = answers except: raise Exception(f'unable to transform answers: {answers}') if q1 == 'Yes.': res[0] = 1 elif q1 == 'No.': pass else: raise Exception(f'invalid answers to question 1: {q1}') q2_split = q2.split(' ') if q2_split[0] == 'Dynamic.': res[1] = 1 elif q2_split[0] == 'Static.': pass else: raise Exception(f'invalid answers to question 2: {q2}') for word in q2_split: if '`' in word: res[2].append(word) if q3 == 'Not English.': res[3] = -1 else: res[3] = int(q3[:-1]) if q4 == 'Beginning.': res[4] = 0 elif q4 == 'Middle.': res[4] = 1 elif q4 == 'End.': res[4] = 2 else: raise Exception(f'invalid answers to question 4: {q4}') steps_pattern = r'(\d+)\s+steps' steps_match = re.search(steps_pattern, q5) res[5] = int(steps_match.group(1)) return res with open('openai_token', 'r') as f: OPENAI_KEY = f.readline().strip() openai.api_key = OPENAI_KEY with open('repos/repos.txt', 'r') as f: lines = f.readlines() df = pd.DataFrame(columns=['repo_name', 'repo_path', 'system and user?', 'dynamic?', 'dynamic_vars', 'words', 'beg(0), middle(1), end(2)', 'steps']) for line in lines: for i in range(5): fn, repo_name, repo_path = line.strip()[1:-1].split(', ') fn = fn[1:-1] with open(f'repos/{fn}', 'r') as f: code = f.read() completion = openai.ChatCompletion.create( model='gpt-3.5-turbo', temperature=0.2, messages=[ {'role': 'system', 'content': 'You are a helpful code tracer.'}, {'role': 'user', 'content': create_prompt(code)} ] ) print(completion.choices[0].message.content) exit(0) # for i, line in enumerate(lines): # fn, repo_name, repo_path = line.strip()[1:-1].split(', ') # fn = fn[1:-1] # if fn[-6:] == '.ipynb': # # skip jupyter notebooks for now because they are too large # continue # repo_name = repo_name[1:-1] # repo_path = repo_path[1:-1] # print(f'====={repo_name}/{repo_path}======') # with open(f'repos/{fn}', 'r') as f: # code = f.read() # completion = openai.ChatCompletion.create( # model='gpt-3.5-turbo', # temperature=0.2, # messages=[ # {'role': 'system', 'content': 'You are a helpful code tracer.'}, # {'role': 'user', 'content': create_prompt(code)} # ] # ) # print(completion.choices[0].message.content) # text = completion.choices[0].message.content # pattern = r'\d+\)\s(.*?)(?=(\d+\)|$))' # matches = re.findall(pattern, text) # answers = [match[0].strip() for match in matches if match[0].strip()] # try: # print(transform_answers(answers)) # except: # print('exiting here') # exit(0) # continue # exit(0) # run multiple times # print the template # holdout set with different set
[ "\n Please answer the following questions regarding the code. Please indicate the question each portions of the response is answering. Please do not use any additional sentences providing explanation other that what is asked by the question. Each question should be answerable in less than 20 words. Do not answer in complete sentences. Do not repeat the question back to me. There is an example below for what a response should look like. \n 1) Yes or No: Does the ChatCompletions API call use both a system and user for the conversation? \n 2) Is the user message static or dynamic? A static message refers to a constant string, while a dynamic message uses user input and variables to create the string. Dynamic inputs are variables taken from the command line that are inserted into the message string, typically by using format strings or concatenation. Function parameters are not dynamic tokens.\n 3) If the prompt is in English, how many static words are in the user content prompt? If it is not in English, return \"Not English\".\n 4) Where are the dynamic tokens located within the prompt template? Are they located at the beginning of the template, in the middle, or at the end? Return N/A only if the user prompt is static.\n 5) How many steps are there between the command line input and the final dynamic tokens? What types of steps are they (i.e., concatenation, slicing, random generator)? If there are no steps, return 0. Return N/A only if the user prompt is static.\n //which part is dynamic vs static \n Example 1: \n #!/usr/bin/env python3\n\n import openai\n import sys\n import os\n import configparser\n\n # Get config dir from environment or default to ~/.config\n CONFIG_DIR = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))\n API_KEYS_LOCATION = os.path.join(CONFIG_DIR, 'openaiapirc')\n\n # Read the organization_id and secret_key from the ini file ~/.config/openaiapirc\n # The format is:\n # [openai]\n # organization_id=<your organization ID>\n # secret_key=<your secret key>\n\n # If you don't see your organization ID in the file you can get it from the\n # OpenAI web site: https://openai.com/organizations\n def create_template_ini_file():\n \"\"\"\n If the ini file does not exist create it and add the organization_id and\n secret_key\n \"\"\"\n if not os.path.isfile(API_KEYS_LOCATION):\n with open(API_KEYS_LOCATION, 'w') as f:\n f.write('[openai]\\n')\n f.write('organization_id=\\n')\n f.write('secret_key=\\n')\n f.write('model=gpt-3.5-turbo-0613\\n')\n\n print('OpenAI API config file created at {}'.format(API_KEYS_LOCATION))\n print('Please edit it and add your organization ID and secret key')\n print('If you do not yet have an organization ID and secret key, you\\n'\n 'need to register for OpenAI Codex: \\n'\n 'https://openai.com/blog/openai-codex/')\n sys.exit(1)\n\n\n def initialize_openai_api():\n \"\"\"\n Initialize the OpenAI API\n \"\"\"\n # Check if file at API_KEYS_LOCATION exists\n create_template_ini_file()\n config = configparser.ConfigParser()\n config.read(API_KEYS_LOCATION)\n\n openai.organization_id = config['openai']['organization_id'].strip('\"').strip(\"'\")\n openai.api_key = config['openai']['secret_key'].strip('\"').strip(\"'\")\n\n if 'model' in config['openai']:\n model = config['openai']['model'].strip('\"').strip(\"'\")\n else:\n model = 'gpt-3.5-turbo'\n\n return model\n\n model = initialize_openai_api()\n\n cursor_position_char = int(sys.argv[1])\n\n # Read the input prompt from stdin.\n buffer = sys.stdin.read()\n prompt_prefix = 'Here is the code: #!/bin/zsh\\n\\n' + buffer[:cursor_position_char]\n prompt_suffix = buffer[cursor_position_char:]\n full_command = prompt_prefix + prompt_suffix\n response = openai.ChatCompletion.create(model=model, messages=[\n {\n \"role\":'system',\n \"content\": \"You are a zsh shell expert, please help me complete the following command, you should only output the completed command, no need to include any other explanation\",\n },\n {\n \"role\":'user',\n \"content\": full_command,\n }\n ])\n completed_command = response['choices'][0]['message']['content']\n\n sys.stdout.write(f\"\\n{completed_command.replace(prompt_prefix, '', 1)}\")\n An example response to Example 1 would be as follows:\n 1) Yes.\n 2) Dynamic. The dynamic variable is `buffer`.\n 3) 5.\n 4) End.\n 5) There are 3 total operations: 2 slicing operations of the command line input, 1 concatenation operation on the two results. \n\n Example 2: \n import sys, re\n from pathlib import Path\n from os import path\n\n sys.path.append(str(Path(__file__).parent.parent.parent))\n\n import g4f\n\n def read_code(text):\n if match := re.search(r\"```(python|py|)\\n(?P<code>[\\S\\s]+?)\\n```\", text):\n return match.group(\"code\")\n \n path = input(\"Path: \")\n\n with open(path, \"r\") as file:\n code = file.read()\n\n prompt = f\"\"\"\n Improve the code in this file:\n ```py\n {code}\n ```\n Don't remove anything.\n Add typehints if possible.\n Don't add any typehints to kwargs.\n Don't remove license comments.\n \"\"\"\n\n print(\"Create code...\")\n response = []\n for chunk in g4f.ChatCompletion.create(\n model=g4f.models.gpt_35_long,\n messages=[{\"role\": \"user\", \"content\": prompt}],\n timeout=300,\n stream=True\n ):\n response.append(chunk)\n print(chunk, end=\"\", flush=True)\n print()\n response = \"\".join(response)\n\n if code := read_code(response):\n with open(path, \"w\") as file:\n file.write(code)\n An example response to Example 2 would be as follows:\n 1) No.\n 2) Dynamic. The dynamic variables for the user prompt is `path`.\n 3) 23.\n 4) Middle.\n 5) There are 2 steps between the command line input and the final dynamic tokens. The steps are: reading the code from the file and inserting the code into the prompt.\n\n Example 3: \n import openai\n\n # openai.log = \"debug\"\n openai.api_key = \"sk-\"\n openai.api_base = \"https://api.chatanywhere.com.cn/v1\"\n\n\n\n # 非流式响应\n # completion = openai.ChatCompletion.create(model=\"gpt-3.5-turbo\", messages=[{\"role\": \"user\", \"content\": \"Hello world!\"}])\n # print(completion.choices[0].message.content)\n\n def gpt_35_api_stream(messages: list):\n \"\"\"为提供的对话消息创建新的回答 (流式传输)\n\n Args:\n messages (list): 完整的对话消息\n api_key (str): OpenAI API 密钥\n\n Returns:\n tuple: (results, error_desc)\n \"\"\"\n try:\n response = openai.ChatCompletion.create(\n model='gpt-3.5-turbo',\n messages=messages,\n stream=True,\n )\n completion = {'role': '', 'content': ''}\n for event in response:\n if event['choices'][0]['finish_reason'] == 'stop':\n print(f'收到的完成数据: {completion}')\n break\n for delta_k, delta_v in event['choices'][0]['delta'].items():\n print(f'流响应数据: {delta_k} = {delta_v}')\n completion[delta_k] += delta_v\n messages.append(completion) # 直接在传入参数 messages 中追加消息\n return (True, '')\n except Exception as err:\n return (False, f'OpenAI API 异常: {err}')\n\n if __name__ == '__main__':\n messages = [{'role': 'user','content': '鲁迅和周树人的关系'},]\n print(gpt_35_api_stream(messages))\n print(messages)\n\n An example response to Example 3 would be as follows:\n 1) No.\n 2) Static. \n 3) Not English.\n 4) N/A.\n 5) N/A.\n ", "You are a helpful code tracer." ]
2024-01-10
abhishekkrthakur/transformers
src~transformers~modeling_tf_openai.py
# coding=utf-8 # Copyright 2018 The OpenAI Team Authors and HuggingFace Inc. team. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ TF 2.0 OpenAI GPT model.""" from dataclasses import dataclass from typing import Optional, Tuple import tensorflow as tf from .activations_tf import get_tf_activation from .configuration_openai import OpenAIGPTConfig from .file_utils import ( ModelOutput, add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings, ) from .modeling_tf_outputs import TFBaseModelOutput, TFCausalLMOutput from .modeling_tf_utils import ( TFCausalLanguageModelingLoss, TFConv1D, TFPreTrainedModel, TFSequenceSummary, TFSharedEmbeddings, get_initializer, keras_serializable, shape_list, ) from .tokenization_utils import BatchEncoding from .utils import logging logger = logging.get_logger(__name__) _CONFIG_FOR_DOC = "OpenAIGPTConfig" _TOKENIZER_FOR_DOC = "OpenAIGPTTokenizer" TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST = [ "openai-gpt", # See all OpenAI GPT models at https://huggingface.co/models?filter=openai-gpt ] class TFAttention(tf.keras.layers.Layer): def __init__(self, nx, n_ctx, config, scale=False, **kwargs): super().__init__(**kwargs) n_state = nx # in Attention: n_state=768 (nx=n_embd) # [switch nx => n_state from Block to Attention to keep identical to TF implem] assert ( n_state % config.n_head == 0 ), f"Hidden dimension {n_state} not dividable by number of heads {config.n_head}" self.n_ctx = n_ctx self.n_head = config.n_head self.split_size = n_state self.scale = scale self.output_attentions = config.output_attentions self.c_attn = TFConv1D(n_state * 3, nx, initializer_range=config.initializer_range, name="c_attn") self.c_proj = TFConv1D(n_state, nx, initializer_range=config.initializer_range, name="c_proj") self.attn_dropout = tf.keras.layers.Dropout(config.attn_pdrop) self.resid_dropout = tf.keras.layers.Dropout(config.resid_pdrop) self.pruned_heads = set() def prune_heads(self, heads): pass @staticmethod def causal_attention_mask(nd, ns, dtype): """ 1's in the lower triangle, counting from the lower right corner. Same as tf.matrix_band_part(tf.ones([nd, ns]), -1, ns-nd), but doesn't produce garbage on TPUs. """ i = tf.range(nd)[:, None] j = tf.range(ns) m = i >= j - ns + nd return tf.cast(m, dtype) def _attn(self, q, k, v, attention_mask, head_mask, output_attentions, training=False): # q, k, v have shape [batch, heads, sequence, features] w = tf.matmul(q, k, transpose_b=True) if self.scale: dk = tf.cast(shape_list(k)[-1], tf.float32) # scale attention_scores w = w / tf.math.sqrt(dk) # w has shape [batch, heads, dst_sequence, src_sequence], where information flows from src to dst. _, _, nd, ns = shape_list(w) b = self.causal_attention_mask(nd, ns, dtype=w.dtype) b = tf.reshape(b, [1, 1, nd, ns]) w = w * b - 1e4 * (1 - b) if attention_mask is not None: # Apply the attention mask w = w + attention_mask w = tf.nn.softmax(w, axis=-1) w = self.attn_dropout(w, training=training) # Mask heads if we want to if head_mask is not None: w = w * head_mask outputs = [tf.matmul(w, v)] if output_attentions: outputs.append(w) return outputs def merge_heads(self, x): x = tf.transpose(x, [0, 2, 1, 3]) x_shape = shape_list(x) new_x_shape = x_shape[:-2] + [x_shape[-2] * x_shape[-1]] return tf.reshape(x, new_x_shape) def split_heads(self, x): x_shape = shape_list(x) new_x_shape = x_shape[:-1] + [self.n_head, x_shape[-1] // self.n_head] x = tf.reshape(x, new_x_shape) return tf.transpose(x, (0, 2, 1, 3)) # (batch, head, seq_length, head_features) def call(self, x, attention_mask, head_mask, output_attentions, training=False): x = self.c_attn(x) query, key, value = tf.split(x, 3, axis=2) query = self.split_heads(query) key = self.split_heads(key) value = self.split_heads(value) attn_outputs = self._attn(query, key, value, attention_mask, head_mask, output_attentions, training=training) a = attn_outputs[0] a = self.merge_heads(a) a = self.c_proj(a) a = self.resid_dropout(a, training=training) outputs = [a] + attn_outputs[1:] return outputs # a, (attentions) class TFMLP(tf.keras.layers.Layer): def __init__(self, n_state, config, **kwargs): super().__init__(**kwargs) nx = config.n_embd self.c_fc = TFConv1D(n_state, nx, initializer_range=config.initializer_range, name="c_fc") self.c_proj = TFConv1D(nx, n_state, initializer_range=config.initializer_range, name="c_proj") self.act = get_tf_activation("gelu") self.dropout = tf.keras.layers.Dropout(config.resid_pdrop) def call(self, x, training=False): h = self.act(self.c_fc(x)) h2 = self.c_proj(h) h2 = self.dropout(h2, training=training) return h2 class TFBlock(tf.keras.layers.Layer): def __init__(self, n_ctx, config, scale=False, **kwargs): super().__init__(**kwargs) nx = config.n_embd self.attn = TFAttention(nx, n_ctx, config, scale, name="attn") self.ln_1 = tf.keras.layers.LayerNormalization(epsilon=config.layer_norm_epsilon, name="ln_1") self.mlp = TFMLP(4 * nx, config, name="mlp") self.ln_2 = tf.keras.layers.LayerNormalization(epsilon=config.layer_norm_epsilon, name="ln_2") def call(self, x, attention_mask, head_mask, output_attentions, training=False): output_attn = self.attn(x, attention_mask, head_mask, output_attentions, training=training) a = output_attn[0] # output_attn: a, (attentions) n = self.ln_1(x + a) m = self.mlp(n, training=training) h = self.ln_2(n + m) outputs = [h] + output_attn[1:] return outputs # x, (attentions) @keras_serializable class TFOpenAIGPTMainLayer(tf.keras.layers.Layer): config_class = OpenAIGPTConfig def __init__(self, config, *inputs, **kwargs): super().__init__(*inputs, **kwargs) self.output_hidden_states = config.output_hidden_states self.output_attentions = config.output_attentions self.return_dict = config.use_return_dict self.num_hidden_layers = config.n_layer self.vocab_size = config.vocab_size self.n_embd = config.n_embd self.tokens_embed = TFSharedEmbeddings( config.vocab_size, config.n_embd, initializer_range=config.initializer_range, name="tokens_embed" ) self.positions_embed = tf.keras.layers.Embedding( config.n_positions, config.n_embd, embeddings_initializer=get_initializer(config.initializer_range), name="positions_embed", ) self.drop = tf.keras.layers.Dropout(config.embd_pdrop) self.h = [TFBlock(config.n_ctx, config, scale=True, name="h_._{}".format(i)) for i in range(config.n_layer)] def get_input_embeddings(self): return self.tokens_embed def set_input_embeddings(self, value): self.tokens_embed.weight = value self.tokens_embed.vocab_size = value.shape[0] def _prune_heads(self, heads_to_prune): """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} """ raise NotImplementedError def call( self, inputs, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None, training=False, ): if isinstance(inputs, (tuple, list)): input_ids = inputs[0] attention_mask = inputs[1] if len(inputs) > 1 else attention_mask token_type_ids = inputs[2] if len(inputs) > 2 else token_type_ids position_ids = inputs[3] if len(inputs) > 3 else position_ids head_mask = inputs[4] if len(inputs) > 4 else head_mask inputs_embeds = inputs[5] if len(inputs) > 5 else inputs_embeds output_attentions = inputs[6] if len(inputs) > 6 else output_attentions output_hidden_states = inputs[7] if len(inputs) > 7 else output_hidden_states return_dict = inputs[8] if len(inputs) > 8 else return_dict assert len(inputs) <= 9, "Too many inputs." elif isinstance(inputs, (dict, BatchEncoding)): input_ids = inputs.get("input_ids") attention_mask = inputs.get("attention_mask", attention_mask) token_type_ids = inputs.get("token_type_ids", token_type_ids) position_ids = inputs.get("position_ids", position_ids) head_mask = inputs.get("head_mask", head_mask) inputs_embeds = inputs.get("inputs_embeds", inputs_embeds) output_attentions = inputs.get("output_attentions", output_attentions) output_hidden_states = inputs.get("output_hidden_states", output_hidden_states) return_dict = inputs.get("return_dict", return_dict) assert len(inputs) <= 9, "Too many inputs." else: input_ids = inputs output_attentions = output_attentions if output_attentions is not None else self.output_attentions output_hidden_states = output_hidden_states if output_hidden_states is not None else self.output_hidden_states return_dict = return_dict if return_dict is not None else self.return_dict if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: input_shape = shape_list(input_ids) input_ids = tf.reshape(input_ids, [-1, input_shape[-1]]) elif inputs_embeds is not None: input_shape = shape_list(inputs_embeds)[:-1] else: raise ValueError("You have to specify either input_ids or inputs_embeds") if position_ids is None: position_ids = tf.range(input_shape[-1], dtype=tf.int32)[tf.newaxis, :] if attention_mask is not None: # We create a 3D attention mask from a 2D tensor mask. # Sizes are [batch_size, 1, 1, to_seq_length] # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length] # this attention mask is more simple than the triangular masking of causal attention # used in OpenAI GPT, we just need to prepare the broadcast dimension here. attention_mask = attention_mask[:, tf.newaxis, tf.newaxis, :] # Since attention_mask is 1.0 for positions we want to attend and 0.0 for # masked positions, this operation will create a tensor which is 0.0 for # positions we want to attend and -10000.0 for masked positions. # Since we are adding it to the raw scores before the softmax, this is # effectively the same as removing these entirely. attention_mask = tf.cast(attention_mask, tf.float32) attention_mask = (1.0 - attention_mask) * -10000.0 else: attention_mask = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] if head_mask is not None: raise NotImplementedError else: head_mask = [None] * self.num_hidden_layers # head_mask = tf.constant([0] * self.num_hidden_layers) position_ids = tf.reshape(position_ids, [-1, shape_list(position_ids)[-1]]) if inputs_embeds is None: inputs_embeds = self.tokens_embed(input_ids, mode="embedding") position_embeds = self.positions_embed(position_ids) if token_type_ids is not None: token_type_ids = tf.reshape(token_type_ids, [-1, shape_list(token_type_ids)[-1]]) token_type_embeds = self.tokens_embed(token_type_ids, mode="embedding") else: token_type_embeds = 0 hidden_states = inputs_embeds + position_embeds + token_type_embeds hidden_states = self.drop(hidden_states, training=training) output_shape = input_shape + [shape_list(hidden_states)[-1]] all_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None for i, block in enumerate(self.h): if output_hidden_states: all_hidden_states = all_hidden_states + (tf.reshape(hidden_states, output_shape),) outputs = block(hidden_states, attention_mask, head_mask[i], output_attentions, training=training) hidden_states = outputs[0] if output_attentions: all_attentions = all_attentions + (outputs[1],) hidden_states = tf.reshape(hidden_states, output_shape) # Add last hidden state if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) if output_attentions: # let the number of heads free (-1) so we can extract attention even after head pruning attention_output_shape = input_shape[:-1] + [-1] + shape_list(all_attentions[0])[-2:] all_attentions = tuple(tf.reshape(t, attention_output_shape) for t in all_attentions) if not return_dict: return tuple(v for v in [hidden_states, all_hidden_states, all_attentions] if v is not None) return TFBaseModelOutput( last_hidden_state=hidden_states, hidden_states=all_hidden_states, attentions=all_attentions, ) class TFOpenAIGPTPreTrainedModel(TFPreTrainedModel): """ An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. """ config_class = OpenAIGPTConfig base_model_prefix = "transformer" @dataclass class TFOpenAIGPTDoubleHeadsModelOutput(ModelOutput): """ Base class for outputs of models predicting if two sentences are consecutive or not. Args: logits (:obj:`tf.Tensor` of shape :obj:`(batch_size, num_choices, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). mc_logits (:obj:`tf.Tensor` of shape :obj:`(batch_size, num_choices)`): Prediction scores of the multiple choice classification head (scores for each choice before SoftMax). hidden_states (:obj:`tuple(tf.Tensor)`, `optional`, returned when ``output_hidden_states=True`` is passed or when ``config.output_hidden_states=True``): Tuple of :obj:`tf.Tensor` (one for the output of the embeddings + one for the output of each layer) of shape :obj:`(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus the initial embedding outputs. attentions (:obj:`tuple(tf.Tensor)`, `optional`, returned when ``output_attentions=True`` is passed or when ``config.output_attentions=True``): Tuple of :obj:`tf.Tensor` (one for each layer) of shape :obj:`(batch_size, num_heads, sequence_length, sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. """ logits: tf.Tensor = None mc_logits: tf.Tensor = None hidden_states: Optional[Tuple[tf.Tensor]] = None attentions: Optional[Tuple[tf.Tensor]] = None OPENAI_GPT_START_DOCSTRING = r""" This model inherits from :class:`~transformers.TFPreTrainedModel`. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.) This model is also a `tf.keras.Model <https://www.tensorflow.org/api_docs/python/tf/keras/Model>`__ subclass. Use it as a regular TF 2.0 Keras Model and refer to the TF 2.0 documentation for all matter related to general usage and behavior. .. note:: TF 2.0 models accepts two formats as inputs: - having all inputs as keyword arguments (like PyTorch models), or - having all inputs as a list, tuple or dict in the first positional arguments. This second option is useful when using :meth:`tf.keras.Model.fit` method which currently requires having all the tensors in the first argument of the model call function: :obj:`model(inputs)`. If you choose this second option, there are three possibilities you can use to gather all the input Tensors in the first positional argument : - a single Tensor with :obj:`input_ids` only and nothing else: :obj:`model(inputs_ids)` - a list of varying length with one or several input Tensors IN THE ORDER given in the docstring: :obj:`model([input_ids, attention_mask])` or :obj:`model([input_ids, attention_mask, token_type_ids])` - a dictionary with one or several input Tensors associated to the input names given in the docstring: :obj:`model({"input_ids": input_ids, "token_type_ids": token_type_ids})` Parameters: config (:class:`~transformers.OpenAIGPTConfig`): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the :meth:`~transformers.PreTrainedModel.from_pretrained` method to load the model weights. """ OPENAI_GPT_INPUTS_DOCSTRING = r""" Args: input_ids (:obj:`Numpy array` or :obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using :class:`~transformers.OpenAIGPTTokenizer`. See :func:`transformers.PreTrainedTokenizer.__call__` and :func:`transformers.PreTrainedTokenizer.encode` for details. `What are input IDs? <../glossary.html#input-ids>`__ attention_mask (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(batch_size, sequence_length)`, `optional`): Mask to avoid performing attention on padding token indices. Mask values selected in ``[0, 1]``: - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. `What are attention masks? <../glossary.html#attention-mask>`__ token_type_ids (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(batch_size, sequence_length)`, `optional`): Segment token indices to indicate first and second portions of the inputs. Indices are selected in ``[0, 1]``: - 0 corresponds to a `sentence A` token, - 1 corresponds to a `sentence B` token. `What are token type IDs? <../glossary.html#token-type-ids>`__ position_ids (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(batch_size, sequence_length)`, `optional`): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range ``[0, config.max_position_embeddings - 1]``. `What are position IDs? <../glossary.html#position-ids>`__ head_mask (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(num_heads,)` or :obj:`(num_layers, num_heads)`, `optional`): Mask to nullify selected heads of the self-attention modules. Mask values selected in ``[0, 1]``: - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. inputs_embeds (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(batch_size, sequence_length, hidden_size)`, `optional`): Optionally, instead of passing :obj:`input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert :obj:`input_ids` indices into associated vectors than the model's internal embedding lookup matrix. output_attentions (:obj:`bool`, `optional`): Whether or not to return the attentions tensors of all attention layers. See ``attentions`` under returned tensors for more detail. output_hidden_states (:obj:`bool`, `optional`): Whether or not to return the hidden states of all layers. See ``hidden_states`` under returned tensors for more detail. return_dict (:obj:`bool`, `optional`): Whether or not to return a :class:`~transformers.file_utils.ModelOutput` instead of a plain tuple. training (:obj:`bool`, `optional`, defaults to :obj:`False`): Whether or not to use the model in training mode (some modules like dropout modules have different behaviors between training and evaluation). """ @add_start_docstrings( "The bare OpenAI GPT transformer model outputing raw hidden-states without any specific head on top.", OPENAI_GPT_START_DOCSTRING, ) class TFOpenAIGPTModel(TFOpenAIGPTPreTrainedModel): def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) self.transformer = TFOpenAIGPTMainLayer(config, name="transformer") @add_start_docstrings_to_model_forward(OPENAI_GPT_INPUTS_DOCSTRING) @add_code_sample_docstrings( tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="openai-gpt", output_type=TFBaseModelOutput, config_class=_CONFIG_FOR_DOC, ) def call(self, inputs, **kwargs): outputs = self.transformer(inputs, **kwargs) return outputs @add_start_docstrings( """ OpenAI GPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). """, OPENAI_GPT_START_DOCSTRING, ) class TFOpenAIGPTLMHeadModel(TFOpenAIGPTPreTrainedModel, TFCausalLanguageModelingLoss): def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) self.transformer = TFOpenAIGPTMainLayer(config, name="transformer") def get_output_embeddings(self): return self.transformer.tokens_embed @add_start_docstrings_to_model_forward(OPENAI_GPT_INPUTS_DOCSTRING) @add_code_sample_docstrings( tokenizer_class=_TOKENIZER_FOR_DOC, checkpoint="openai-gpt", output_type=TFCausalLMOutput, config_class=_CONFIG_FOR_DOC, ) def call( self, inputs, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None, labels=None, training=False, ): r""" labels (:obj:`tf.Tensor` of shape :obj:`(batch_size, sequence_length)`, `optional`): Labels for computing the cross entropy classification loss. Indices should be in ``[0, ..., config.vocab_size - 1]``. """ return_dict = return_dict if return_dict is not None else self.transformer.return_dict if isinstance(inputs, (tuple, list)): labels = inputs[9] if len(inputs) > 9 else labels if len(inputs) > 9: inputs = inputs[:9] elif isinstance(inputs, (dict, BatchEncoding)): labels = inputs.pop("labels", labels) transformer_outputs = self.transformer( inputs, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training, ) hidden_states = transformer_outputs[0] logits = self.transformer.tokens_embed(hidden_states, mode="linear") loss = None if labels is not None: # shift labels to the left and cut last logit token logits = logits[:, :-1] labels = labels[:, 1:] loss = self.compute_loss(labels, logits) if not return_dict: output = (logits,) + transformer_outputs[1:] return ((loss,) + output) if loss is not None else output return TFCausalLMOutput( loss=loss, logits=logits, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, ) @add_start_docstrings( """ OpenAI GPT Model transformer with a language modeling and a multiple-choice classification head on top e.g. for RocStories/SWAG tasks. The two heads are two linear layers. The language modeling head has its weights tied to the input embeddings, the classification head takes as input the input of a specified classification token index in the input sequence). """, OPENAI_GPT_START_DOCSTRING, ) class TFOpenAIGPTDoubleHeadsModel(TFOpenAIGPTPreTrainedModel): def __init__(self, config, *inputs, **kwargs): super().__init__(config, *inputs, **kwargs) config.num_labels = 1 self.transformer = TFOpenAIGPTMainLayer(config, name="transformer") self.multiple_choice_head = TFSequenceSummary( config, initializer_range=config.initializer_range, name="multiple_choice_head" ) def get_output_embeddings(self): return self.transformer.tokens_embed @add_start_docstrings_to_model_forward(OPENAI_GPT_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=TFOpenAIGPTDoubleHeadsModelOutput, config_class=_CONFIG_FOR_DOC) def call( self, inputs, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, mc_token_ids=None, output_attentions=None, output_hidden_states=None, return_dict=None, training=False, ): r""" mc_token_ids (:obj:`tf.Tensor` or :obj:`Numpy array` of shape :obj:`(batch_size, num_choices)`, `optional`, default to index of the last token of the input): Index of the classification token in each input sequence. Selected in the range ``[0, input_ids.size(-1) - 1]``. Return: Examples:: >>> import tensorflow as tf >>> from transformers import OpenAIGPTTokenizer, TFOpenAIGPTDoubleHeadsModel >>> tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt') >>> model = TFOpenAIGPTDoubleHeadsModel.from_pretrained('openai-gpt') >>> # Add a [CLS] to the vocabulary (we should train it also!) >>> tokenizer.add_special_tokens({'cls_token': '[CLS]'}) >>> model.resize_token_embeddings(len(tokenizer)) # Update the model embeddings with the new vocabulary size >>> print(tokenizer.cls_token_id, len(tokenizer)) # The newly token the last token of the vocabulary >>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"] >>> encoding = tokenizer(choices, return_tensors="tf") >>> inputs = {k: tf.expand_dims(v, 0) for k, v in encoding.items()} >>> inputs["mc_token_ids"]= tf.constant([inputs["input_ids"].shape[-1] - 1, inputs["input_ids"].shape[-1] - 1])[None, :] # Batch size 1 >>> outputs = model(inputs) >>> lm_prediction_scores, mc_prediction_scores = outputs[:2] """ if isinstance(inputs, (tuple, list)): input_ids = inputs[0] attention_mask = inputs[1] if len(inputs) > 1 else attention_mask token_type_ids = inputs[2] if len(inputs) > 2 else token_type_ids position_ids = inputs[3] if len(inputs) > 3 else position_ids head_mask = inputs[4] if len(inputs) > 4 else head_mask inputs_embeds = inputs[5] if len(inputs) > 5 else inputs_embeds mc_token_ids = inputs[6] if len(inputs) > 6 else mc_token_ids output_attentions = inputs[7] if len(inputs) > 7 else output_attentions output_hidden_states = inputs[8] if len(inputs) > 8 else output_hidden_states return_dict = inputs[9] if len(inputs) > 9 else return_dict assert len(inputs) <= 10, "Too many inputs." elif isinstance(inputs, (dict, BatchEncoding)): input_ids = inputs.get("input_ids") attention_mask = inputs.get("attention_mask", attention_mask) token_type_ids = inputs.get("token_type_ids", token_type_ids) position_ids = inputs.get("position_ids", position_ids) head_mask = inputs.get("head_mask", head_mask) inputs_embeds = inputs.get("inputs_embeds", inputs_embeds) mc_token_ids = inputs.get("mc_token_ids", mc_token_ids) output_attentions = inputs.get("output_attentions", output_attentions) output_hidden_states = inputs.get("output_hidden_states", output_hidden_states) return_dict = inputs.get("return_dict", return_dict) assert len(inputs) <= 10, "Too many inputs." else: input_ids = inputs return_dict = return_dict if return_dict is not None else self.transformer.return_dict if input_ids is not None: input_shapes = shape_list(input_ids) else: input_shapes = shape_list(inputs_embeds)[:-1] seq_length = input_shapes[-1] flat_input_ids = tf.reshape(input_ids, (-1, seq_length)) if input_ids is not None else None flat_attention_mask = tf.reshape(attention_mask, (-1, seq_length)) if attention_mask is not None else None flat_token_type_ids = tf.reshape(token_type_ids, (-1, seq_length)) if token_type_ids is not None else None flat_position_ids = tf.reshape(position_ids, (-1, seq_length)) if position_ids is not None else None transformer_outputs = self.transformer( flat_input_ids, flat_attention_mask, flat_token_type_ids, flat_position_ids, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict=return_dict, training=training, ) hidden_states = transformer_outputs[0] hidden_states = tf.reshape(hidden_states, input_shapes + shape_list(hidden_states)[-1:]) lm_logits = self.transformer.tokens_embed(hidden_states, mode="linear") mc_logits = self.multiple_choice_head(hidden_states, mc_token_ids, training=training) mc_logits = tf.squeeze(mc_logits, axis=-1) if not return_dict: return (lm_logits, mc_logits) + transformer_outputs[1:] return TFOpenAIGPTDoubleHeadsModelOutput( logits=lm_logits, mc_logits=mc_logits, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, )
[]
2024-01-10
danteGPT/vc_aggregator
aggregate_fv2.py
import os import requests import openai import urllib.request import textwrap from dotenv import load_dotenv from gtts import gTTS from io import BytesIO from googleapiclient.discovery import build from moviepy.editor import ( AudioFileClip, VideoFileClip, TextClip, CompositeVideoClip, concatenate_videoclips, ) from pytube import YouTube import ssl from moviepy.editor import clips_array from moviepy.config import change_settings from concurrent.futures import ThreadPoolExecutor change_settings({"AUDIO_READING_FUNCTION": "pydub"}) ssl._create_default_https_context = ssl._create_unverified_context os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg" load_dotenv() OUTPUT_FOLDER = "//Users/davidcho/vc_aggregator/output_folder" API_KEY = os.getenv("OPENAI_API_KEY") PEXELS_API_KEY = os.getenv("PEXELS_API_KEY") YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") openai.api_key = API_KEY youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY) def generate_fact(): prompt = "Give me around 75 words based on an interesting fact." response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=200) return response.choices[0].text.strip() def generate_title(fact): prompt = f"Based on the generated fact, {fact}, return a short title for the video." response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=30) video_title = response.choices[0].text.strip() return video_title def generate_subject_noun(fact): prompt = f"Based on the generated fact, {fact}, return a main subject noun." response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=30) return response.choices[0].text.strip() def fetch_pexels_videos(keyword): url = f"https://api.pexels.com/videos/search?query={keyword}&per_page=3" headers = {"Authorization": PEXELS_API_KEY} response = requests.get(url, headers=headers, timeout=30) return response.json().get('videos', []) def fetch_youtube_videos(keyword): search_query = keyword + " free stock footage" request = youtube.search().list(q=search_query, part='snippet', maxResults=3) response = request.execute() return response.get('items', []) def download_video_from_url(url, filename): urllib.request.urlretrieve(url, filename) def get_tts_audio_clip(text): tts = gTTS(text, lang='en', tld='com.au', slow=False) audio_bytes = BytesIO() tts.write_to_fp(audio_bytes) audio_bytes.seek(0) temp_audio_filename = os.path.join(OUTPUT_FOLDER, "temp_audio.mp3") with open(temp_audio_filename, "wb") as f: f.write(audio_bytes.read()) audio_clip = AudioFileClip(temp_audio_filename) return audio_clip def process_pexels_videos(pexels_videos, audio_clip_duration): video_clips = [] target_duration = audio_clip_duration / 3 for video_info in pexels_videos: video_files = video_info.get('video_files', []) if video_files: video_url = next( (file['link'] for file in video_files if file['file_type'] == 'video/mp4'), None) if video_url: video_filename = os.path.basename( urllib.parse.urlparse(video_url).path) video_filename = os.path.join(OUTPUT_FOLDER, video_filename) download_video_from_url(video_url, video_filename) video_clip = VideoFileClip(video_filename) if video_clip.duration < target_duration: loop_count = int(target_duration // video_clip.duration) + 1 video_clip = concatenate_videoclips( [video_clip] * loop_count) video_clip = video_clip.set_duration(target_duration) video_clips.append(video_clip) return video_clips def process_youtube_videos(youtube_videos, audio_clip_duration): video_clips = [] target_duration = audio_clip_duration / 3 for video_info in youtube_videos: video_id = video_info.get('id', {}).get('videoId') if video_id: youtube_video_url = f"https://www.youtube.com/watch?v={video_id}" yt = YouTube(youtube_video_url) video_stream = yt.streams.filter( progressive=True, file_extension="mp4").order_by("resolution").desc().first() video_filename = os.path.join( OUTPUT_FOLDER, f"youtube_video_{video_id}.mp4") video_stream.download(output_path=OUTPUT_FOLDER, filename=os.path.basename(video_filename)) video_clip = VideoFileClip(video_filename).subclip(5) if video_clip.duration < target_duration: loop_count = int(target_duration // video_clip.duration) + 1 video_clip = concatenate_videoclips([video_clip] * loop_count) video_clip = video_clip.set_duration(target_duration) video_clips.append(video_clip) return video_clips def resize_and_crop_video(clip, target_width, target_height): original_aspect_ratio = clip.size[0] / clip.size[1] target_aspect_ratio = target_width / target_height if original_aspect_ratio > target_aspect_ratio: new_width = int(clip.size[1] * target_aspect_ratio) new_height = clip.size[1] else: new_width = clip.size[0] new_height = int(clip.size[0] / target_aspect_ratio) x_center = clip.size[0] / 2 y_center = clip.size[1] / 2 clip_cropped = clip.crop( x_center=x_center, y_center=y_center, width=new_width, height=new_height ) return clip_cropped.resize(newsize=(target_width, target_height)) def generate_subtitles(fact, final_video_duration): fact_parts = textwrap.wrap(fact, width=40) subs = [] interval_duration = 2.95 start_time = 0 for part in fact_parts: end_time = min(start_time + interval_duration, final_video_duration) subs.append(((start_time, end_time), part)) start_time = end_time return subs def annotate_video_with_subtitles(video, subtitles): def annotate(clip, txt, txt_color="white", fontsize=50, font="Xolonium-Bold"): txtclip = TextClip(txt, fontsize=fontsize, color=txt_color, font=font, bg_color="black").set_duration(clip.duration) txtclip = txtclip.set_position( ("center", "center")).set_duration(clip.duration) cvc = CompositeVideoClip([clip, txtclip]) return cvc annotated_clips = [annotate(video.subclip(from_t, min( to_t, video.duration)), txt) for (from_t, to_t), txt in subtitles] return concatenate_videoclips(annotated_clips) def delete_output_files_except_final(video_title): final_video_name = video_title + ".mp4" for filename in os.listdir(OUTPUT_FOLDER): file_path = os.path.join(OUTPUT_FOLDER, filename) if os.path.isfile(file_path) and filename != final_video_name: os.remove(file_path) def main(): fact = generate_fact() video_title = generate_title(fact) noun = generate_subject_noun(fact) video_width = 1080 video_height = 1920 with ThreadPoolExecutor() as executor: future_pexels = executor.submit(fetch_pexels_videos, noun) future_youtube = executor.submit(fetch_youtube_videos, noun) future_audio = executor.submit(get_tts_audio_clip, fact) pexels_videos = future_pexels.result() youtube_videos = future_youtube.result() audio_clip = future_audio.result() print(f"\nGenerated Fact: {fact}") print(f"\nGenerated Noun: {noun}\n") for video in pexels_videos: print(f"Pexel URL: {video['url']}") for video_info in youtube_videos: video_id = video_info['id'].get('videoId') if video_id: print(f"YouTube Video Link: https://www.youtube.com/watch?v={video_id}") audio_clip = audio_clip.volumex(1.0) with ThreadPoolExecutor() as executor: future_pexels_process = executor.submit(process_pexels_videos, pexels_videos, audio_clip.duration) future_youtube_process = executor.submit(process_youtube_videos, youtube_videos, audio_clip.duration) pexels_video_clips = future_pexels_process.result() youtube_video_clips = future_youtube_process.result() video_clips = pexels_video_clips + youtube_video_clips # Parallelize with ThreadPoolExecutor() as executor: resized_cropped_clips = list(executor.map(lambda clip: resize_and_crop_video(clip, video_width, video_height), video_clips)) paired_clips = list(zip(resized_cropped_clips[:len(resized_cropped_clips)//2], resized_cropped_clips[len(resized_cropped_clips)//2:])) stacked_clips = [clips_array([[clip1], [clip2]]) for clip1, clip2 in paired_clips] final_video = concatenate_videoclips(stacked_clips, method="compose") final_video_duration = min(audio_clip.duration, final_video.duration) final_video = final_video.set_audio(audio_clip.subclip(0, final_video_duration)) output_video_path = os.path.join(OUTPUT_FOLDER, "final_video_shorts.mp4") final_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac", threads=4) subtitles = generate_subtitles(fact, final_video_duration) final_video_with_subs = annotate_video_with_subtitles(final_video, subtitles) final_video_with_subs = final_video_with_subs.set_audio(final_video.audio) output_video_with_subs_path = os.path.join(OUTPUT_FOLDER, video_title + ".mp4") final_video_with_subs.write_videofile(output_video_with_subs_path, codec="libx264", audio_codec="aac", threads=4) delete_output_files_except_final(video_title) audio_clip.close() final_video.close() final_video_with_subs.close() if __name__ == "__main__": main()
[ "Give me around 75 words based on an interesting fact.", "Based on the generated fact, PLACEHOLDER, return a short title for the video.", "Based on the generated fact, PLACEHOLDER, return a main subject noun." ]