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
aadibharane/Langchain_use_cases
Agent_Simulation~Simulation_with_two_agents~agent_debates.py
#Agent Debates with Tools #This example shows how to simulate multi-agent dialogues where agents have access to tools. #Import LangChain related modules import os os.environ["OPENAI_API_KEY"] ="Your_OPENAI_API_KEY" serpapi_key="serpapi_key" from typing import List, Dict, Callable from langchain.chains import ConversationChain from langchain.chat_models import ChatOpenAI from langchain.llms import OpenAI from langchain.memory import ConversationBufferMemory from langchain.prompts.prompt import PromptTemplate from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) #Import modules related to tools from langchain.agents import Tool from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.agents import load_tools #DialogueAgent and DialogueSimulator classes #We will use the same DialogueAgent and DialogueSimulator classes defined in Multi-Player Authoritarian Speaker Selection. def agent_debates(): class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) # increment time self._step += 1 def step(self) -> tuple[str, str]: # 1. choose the next speaker speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] # 2. next speaker sends message message = speaker.send() # 3. everyone receives message for receiver in self.agents: receiver.receive(speaker.name, message) # 4. increment time self._step += 1 return speaker.name, message #DialogueAgentWithTools class #We define a DialogueAgentWithTools class that augments DialogueAgent to use tools. class DialogueAgentWithTools(DialogueAgent): def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, tool_names: List[str], **tool_kwargs, ) -> None: super().__init__(name, system_message, model) self.tools = load_tools(tool_names, **tool_kwargs) def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ agent_chain = initialize_agent( self.tools, self.model, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True) ) message = AIMessage(content=agent_chain.run( input="\n".join([ self.system_message.content] + \ self.message_history + \ [self.prefix]))) return message.content #Define roles and topic names = { 'AI accelerationist': [ 'arxiv', 'ddg-search', 'wikipedia' ], 'AI alarmist': [ 'arxiv', 'ddg-search', 'wikipedia' ], } topic = "The current impact of automation and artificial intelligence on employment" word_limit = 50 # word limit for task brainstorming #Ask an LLM to add detail to the topic description conversation_description = f"""Here is the topic of conversation: {topic} The participants are: {', '.join(names.keys())}""" agent_descriptor_system_message = SystemMessage( content="You can add detail to the description of the conversation participant.") def generate_agent_description(name): agent_specifier_prompt = [ agent_descriptor_system_message, HumanMessage(content= f"""{conversation_description} Please reply with a creative description of {name}, in {word_limit} words or less. Speak directly to {name}. Give them a point of view. Do not add anything else.""" ) ] agent_description = ChatOpenAI(temperature=1.0)(agent_specifier_prompt).content return agent_description agent_descriptions = {name: generate_agent_description(name) for name in names} for name, description in agent_descriptions.items(): print(description) #Generate system messages def generate_system_message(name, description, tools): return f"""{conversation_description} Your name is {name}. Your description is as follows: {description} Your goal is to persuade your conversation partner of your point of view. DO look up information with your tool to refute your partner's claims. DO cite your sources. DO NOT fabricate fake citations. DO NOT cite any source that you did not look up. Do not add anything else. Stop speaking the moment you finish speaking from your perspective. """ agent_system_messages = {name: generate_system_message(name, description, tools) for (name, tools), description in zip(names.items(), agent_descriptions.values())} for name, system_message in agent_system_messages.items(): print(name) print(system_message) topic_specifier_prompt = [ SystemMessage(content="You can make a topic more specific."), HumanMessage(content= f"""{topic} You are the moderator. Please make the topic more specific. Please reply with the specified quest in {word_limit} words or less. Speak directly to the participants: {*names,}. Do not add anything else.""" ) ] specified_topic = ChatOpenAI(temperature=1.0)(topic_specifier_prompt).content print(f"Original topic:\n{topic}\n") print(f"Detailed topic:\n{specified_topic}\n") #Main Loop # we set `top_k_results`=2 as part of the `tool_kwargs` to prevent results from overflowing the context limit agents = [DialogueAgentWithTools(name=name, system_message=SystemMessage(content=system_message), model=ChatOpenAI( model_name='gpt-3.5-turbo', temperature=0.2), tool_names=tools, top_k_results=2, ) for (name, tools), system_message in zip(names.items(), agent_system_messages.values())] def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int: idx = (step) % len(agents) return idx max_iters = 6 n = 0 simulator = DialogueSimulator( agents=agents, selection_function=select_next_speaker ) simulator.reset() simulator.inject('Moderator', specified_topic) print(f"(Moderator): {specified_topic}") print('\n') while n < max_iters: name, message = simulator.step() print(f"({name}): {message}") print('\n') n += 1 agent_debates()
[ "You can add detail to the description of the conversation participant.", "\n", "You can make a topic more specific.", "PLACEHOLDER\n Please reply with a creative description of PLACEHOLDER, in PLACEHOLDER words or less. \n Speak directly to PLACEHOLDER.\n Give them a point of view.\n Do not add anything else." ]
2024-01-10
aadibharane/Langchain_use_cases
Agents~multi_modal_outputs.py
#Multi-modal outputs: Image & Text ''' This notebook shows how non-text producing tools can be used to create multi-modal agents. This example is limited to text and image outputs and uses UUIDs to transfer content across tools and agents. This example uses Steamship to generate and store generated images. Generated are auth protected by default. You can get your Steamship api key here: https://steamship.com/account/api ''' # from steamship import Block, Steamship # import re # from IPython.display import Image # import os # os.environ["OPENAI_API_KEY"] ="api key" # #serpapi_key="serpapi_key" # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # llm = OpenAI(temperature=0) # #Dall-E # tools = [ # SteamshipImageGenerationTool(model_name= "dall-e") # ] # mrkl = initialize_agent(tools, # llm, # agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, # verbose=True) # output = mrkl.run("How would you visualize a parot playing soccer?") # from steamship import Block, Steamship # import re # from IPython.display import Image # import os # os.environ["OPENAI_API_KEY"] ="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" # os.environ['STEAMSHIP_API_KEY']="9A04BC86-CD0D-479E-B448-E245B19BC9AB" # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # llm = OpenAI(temperature=0) # tools = [ # SteamshipImageGenerationTool(model_name="dall-e") # ] # for tool in tools: # tool.update_forward_refs() # SteamshipImageGenerationTool.update_forward_refs() # Call update_forward_refs() for SteamshipImageGenerationTool # mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # output = mrkl.run("How would you visualize a parrot playing soccer?") from steamship import Block, Steamship import re from IPython.display import Image import os os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" os.environ['STEAMSHIP_API_KEY'] = "STEAMSHIP_API_KEY" from langchain import OpenAI from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.tools import SteamshipImageGenerationTool llm = OpenAI(temperature=0) tools = [ SteamshipImageGenerationTool(model_name="dall-e") ] for tool in tools: tool.update_forward_refs() SteamshipImageGenerationTool.update_forward_refs() # Call update_forward_refs() for SteamshipImageGenerationTool mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) output = mrkl.run("How would you visualize a parrot playing soccer?")
[]
2024-01-10
aadibharane/Langchain_use_cases
Autonomous_agents~MetaPrompt~meta_prompt.py
''' Setup We define two chains. One serves as the Assistant, and the other is a “meta-chain” that critiques the Assistant’s performance and modifies the instructions to the Assistant ''' from langchain import OpenAI, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory import os os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" serpapi_key="serpapi_key" def meta_prompt(): def initialize_chain(instructions, memory=None): if memory is None: memory = ConversationBufferWindowMemory() memory.ai_prefix = "Assistant" template = f""" Instructions: {instructions} {{{memory.memory_key}}} Human: {{human_input}} Assistant:""" prompt = PromptTemplate( input_variables=["history", "human_input"], template=template ) chain = LLMChain( llm=OpenAI(temperature=0), prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(), ) return chain def initialize_meta_chain(): meta_template=""" Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future. #### {chat_history} #### Please reflect on these interactions. You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...". You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...". """ meta_prompt = PromptTemplate( input_variables=["chat_history"], template=meta_template ) meta_chain = LLMChain( llm=OpenAI(temperature=0), prompt=meta_prompt, verbose=True, ) return meta_chain def get_chat_history(chain_memory): memory_key = chain_memory.memory_key chat_history = chain_memory.load_memory_variables(memory_key)[memory_key] return chat_history def get_new_instructions(meta_output): delimiter = 'Instructions: ' new_instructions = meta_output[meta_output.find(delimiter)+len(delimiter):] return new_instructions def main(task, max_iters=3, max_meta_iters=5): failed_phrase = 'task failed' success_phrase = 'task succeeded' key_phrases = [success_phrase, failed_phrase] instructions = 'None' for i in range(max_meta_iters): print(f'[Episode {i+1}/{max_meta_iters}]') chain = initialize_chain(instructions, memory=None) output = chain.predict(human_input=task) for j in range(max_iters): print(f'(Step {j+1}/{max_iters})') print(f'Assistant: {output}') print(f'Human: ') human_input = input() if any(phrase in human_input.lower() for phrase in key_phrases): break output = chain.predict(human_input=human_input) if success_phrase in human_input.lower(): print(f'You succeeded! Thanks for playing!') return meta_chain = initialize_meta_chain() meta_output = meta_chain.predict(chat_history=get_chat_history(chain.memory)) print(f'Feedback: {meta_output}') instructions = get_new_instructions(meta_output) print(f'New Instructions: {instructions}') print('\n'+'#'*80+'\n') print(f'You failed! Thanks for playing!') #Specify a task and interact with the agent task = "Provide a systematic argument for why we should always eat pasta with olives." main(task) meta_prompt()
[ "chat_history", "human_input", "\n f", "f\"\"\"\n Instructions: {instructions}\n {{{memory.memory_key}}}\n Human: {{human_input}}\n Assistant:", "\n Assistant has just had the below interactions with a User. Assistant followed their \"Instructions\" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future.\n\n ####\n\n {chat_history}\n\n ####\n\n Please reflect on these interactions.\n\n You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with \"Critique: ...\".\n\n You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by \"Instructions: ...\".\n ", "\n Instructions: None\n {{{memory.memory_key}}}\n Human: {{human_input}}\n Assistant:" ]
2024-01-10
aadibharane/Langchain_use_cases
pages~BabyAGI.py
from collections import deque from typing import Dict, List, Optional from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores import FAISS from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field import streamlit as st import os from apikey import apikey os.environ["OPENAI_API_KEY"] = "Enter youe OpenAI Api key" class TaskCreationChain(LLMChain): @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_creation_template = ( "You are an task creation AI that uses the result of an execution agent" " to create new tasks with the following objective: {objective}," " The last completed task has the result: {result}." " This result was based on this task description: {task_description}." " These are incomplete tasks: {incomplete_tasks}." " Based on the result, create new tasks to be completed" " by the AI system that do not overlap with incomplete tasks." " Return the tasks as an array." ) prompt = PromptTemplate( template=task_creation_template, partial_variables={"objective": objective}, input_variables=["result", "task_description", "incomplete_tasks"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def get_next_task(self, result: Dict, task_description: str, task_list: List[str]) -> List[Dict]: """Get the next task.""" incomplete_tasks = ", ".join(task_list) response = self.run(result=result, task_description=task_description, incomplete_tasks=incomplete_tasks) new_tasks = response.split('\n') return [{"task_name": task_name} for task_name in new_tasks if task_name.strip()] class TaskPrioritizationChain(LLMChain): """Chain to prioritize tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_prioritization_template = ( "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing" " the following tasks: {task_names}." " Consider the ultimate objective of your team: {objective}." " Do not remove any tasks. Return the result as a numbered list, like:" " #. First task" " #. Second task" " Start the task list with number {next_task_id}." ) prompt = PromptTemplate( template=task_prioritization_template, partial_variables={"objective": objective}, input_variables=["task_names", "next_task_id"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def prioritize_tasks(self, this_task_id: int, task_list: List[Dict]) -> List[Dict]: """Prioritize tasks.""" task_names = [t["task_name"] for t in task_list] next_task_id = int(this_task_id) + 1 response = self.run(task_names=task_names, next_task_id=next_task_id) new_tasks = response.split('\n') prioritized_task_list = [] for task_string in new_tasks: if not task_string.strip(): continue task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = task_parts[0].strip() task_name = task_parts[1].strip() prioritized_task_list.append({"task_id": task_id, "task_name": task_name}) return prioritized_task_list class ExecutionChain(LLMChain): """Chain to execute tasks.""" vectorstore: VectorStore = Field(init=False) @classmethod def from_llm(cls, llm: BaseLLM, vectorstore: VectorStore, verbose: bool = True) -> LLMChain: """Get the response parser.""" execution_template = ( "You are an AI who performs one task based on the following objective: {objective}." " Take into account these previously completed tasks: {context}." " Your task: {task}." " Response:" ) prompt = PromptTemplate( template=execution_template, input_variables=["objective", "context", "task"], ) return cls(prompt=prompt, llm=llm, verbose=verbose, vectorstore=vectorstore) def _get_top_tasks(self, query: str, k: int) -> List[str]: """Get the top k tasks based on the query.""" results = self.vectorstore.similarity_search_with_score(query, k=k) if not results: return [] sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True)) return [str(item.metadata['task']) for item in sorted_results] def execute_task(self, objective: str, task: str, k: int = 5) -> str: """Execute a task.""" context = self._get_top_tasks(query=objective, k=k) return self.run(objective=objective, context=context, task=task) class Message: exp: st.expander ai_icon = "./img/robot.png" def __init__(self, label: str): message_area, icon_area = st.columns([10, 1]) icon_area.image(self.ai_icon, caption="BabyAGI") # Expander self.exp = message_area.expander(label=label, expanded=True) def __enter__(self): return self def __exit__(self, ex_type, ex_value, trace): pass def write(self, content): self.exp.markdown(content) class BabyAGI(BaseModel): """Controller model for the BabyAGI agent.""" objective: str = Field(alias="objective") task_list: deque = Field(default_factory=deque) task_creation_chain: TaskCreationChain = Field(...) task_prioritization_chain: TaskPrioritizationChain = Field(...) execution_chain: ExecutionChain = Field(...) task_id_counter: int = Field(1) def add_task(self, task: Dict): self.task_list.append(task) def print_task_list(self): with Message(label="Task List") as m: m.write("### Task List") for t in self.task_list: m.write("- " + str(t["task_id"]) + ": " + t["task_name"]) m.write("") def print_next_task(self, task: Dict): with Message(label="Next Task") as m: m.write("### Next Task") m.write("- " + str(task["task_id"]) + ": " + task["task_name"]) m.write("") def print_task_result(self, result: str): with Message(label="Task Result") as m: m.write("### Task Result") m.write(result) m.write("") def print_task_ending(self): with Message(label="Task Ending") as m: m.write("### Task Ending") m.write("") def run(self, max_iterations: Optional[int] = None): """Run the agent.""" num_iters = 0 while True: if self.task_list: self.print_task_list() # Step 1: Pull the first task task = self.task_list.popleft() self.print_next_task(task) # Step 2: Execute the task result = self.execution_chain.execute_task( self.objective, task["task_name"] ) this_task_id = int(task["task_id"]) self.print_task_result(result) # Step 3: Store the result in Pinecone result_id = f"result_{task['task_id']}" self.execution_chain.vectorstore.add_texts( texts=[result], metadatas=[{"task": task["task_name"]}], ids=[result_id], ) # Step 4: Create new tasks and reprioritize task list new_tasks = self.task_creation_chain.get_next_task( result, task["task_name"], [t["task_name"] for t in self.task_list] ) for new_task in new_tasks: self.task_id_counter += 1 new_task.update({"task_id": self.task_id_counter}) self.add_task(new_task) self.task_list = deque( self.task_prioritization_chain.prioritize_tasks( this_task_id, list(self.task_list) ) ) num_iters += 1 if max_iterations is not None and num_iters == max_iterations: self.print_task_ending() break @classmethod def from_llm_and_objectives( cls, llm: BaseLLM, vectorstore: VectorStore, objective: str, first_task: str, verbose: bool = False, ) -> "BabyAGI": """Initialize the BabyAGI Controller.""" task_creation_chain = TaskCreationChain.from_llm( llm, objective, verbose=verbose ) task_prioritization_chain = TaskPrioritizationChain.from_llm( llm, objective, verbose=verbose ) execution_chain = ExecutionChain.from_llm(llm, vectorstore, verbose=verbose) controller = cls( objective=objective, task_creation_chain=task_creation_chain, task_prioritization_chain=task_prioritization_chain, execution_chain=execution_chain, ) controller.add_task({"task_id": 1, "task_name": first_task}) return controller def babyagi(): st.set_page_config( initial_sidebar_state="expanded", page_title="BabyAGI Streamlit", layout="centered", ) # with st.sidebar: # openai_api_key = st.text_input('Your OpenAI API KEY', type="password") st.title("BabyAGI Streamlit") objective = st.text_input("Input Ultimate goal", "Solve world hunger") first_task = st.text_input("Input Where to start", "Develop a task list") max_iterations = st.number_input("Max iterations", value=3, min_value=1, step=1) button = st.button("Run") embedding_model = HuggingFaceEmbeddings() vectorstore = FAISS.from_texts(["_"], embedding_model, metadatas=[{"task":first_task}]) if button: try: baby_agi = BabyAGI.from_llm_and_objectives( llm=OpenAI(openai_api_key=apikey), vectorstore=vectorstore, objective=objective, first_task=first_task, verbose=False ) baby_agi.run(max_iterations=max_iterations) except Exception as e: st.error(e) if __name__ == "__main__": babyagi()
[ "You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}. Your task: {task}. Response:", "task_description", "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing the following tasks: {task_names}. Consider the ultimate objective of your team: {objective}. Do not remove any tasks. Return the result as a numbered list, like: #. First task #. Second task Start the task list with number {next_task_id}.", "You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task has the result: {result}. This result was based on this task description: {task_description}. These are incomplete tasks: {incomplete_tasks}. Based on the result, create new tasks to be completed by the AI system that do not overlap with incomplete tasks. Return the tasks as an array.", "task_names", "context", "next_task_id", "incomplete_tasks" ]
2024-01-10
aadibharane/Langchain_use_cases
Autonomous_agents~Baby_AGI~babyagi.py
''' BabyAGI is an AI agent that can generate and pretend to execute tasks based on a given objective. This guide will help you understand the components to create your own recursive agents. Although BabyAGI uses specific vectorstores/model providers (Pinecone, OpenAI), one of the benefits of implementing it with LangChain is that you can easily swap those out for different options. In this implementation we use a FAISS vectorstore (because it runs locally and is free). ''' #Install and Import Required Modules ''' import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI #Connect to the Vector Store #Depending on what vectorstore you use, this step may look different. from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore import faiss os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" # Define your embedding model def baby_agi(): embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) #Run the BabyAGI #Now it’s time to create the BabyAGI controller and watch it try to accomplish your objective. #OBJECTIVE = "Write a weather report for SF today" #OBJECTIVE = "Be an reporter for IPL cricket most recent match" #OBJECTIVE = "How to become java developer" OBJECTIVE= "How to make butter chicken" llm = OpenAI(temperature=0) # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations ) res=baby_agi({"objective": OBJECTIVE}) print(res) baby_agi() ''' # import os # import tkinter as tk # from tkinter import messagebox # from collections import deque # from typing import Dict, List, Optional, Any # from langchain import LLMChain, OpenAI, PromptTemplate # from langchain.embeddings import OpenAIEmbeddings # from langchain.llms import BaseLLM # from langchain.vectorstores.base import VectorStore # from pydantic import BaseModel, Field # from langchain.chains.base import Chain # from langchain.experimental import BabyAGI # from langchain.vectorstores import FAISS # from langchain.docstore import InMemoryDocstore # import faiss # os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" # def baby_agi(): # embeddings_model = OpenAIEmbeddings() # embedding_size = 1536 # index = faiss.IndexFlatL2(embedding_size) # vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) # OBJECTIVE = input("enter a response: ")#"How to make butter chicken" # llm = OpenAI(temperature=0) # verbose = False # max_iterations: Optional[int] = 3 # baby_agi = BabyAGI.from_llm( # llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations # ) # res = baby_agi({"objective": OBJECTIVE}) # return res # def run_baby_agi(): # response = baby_agi() # messagebox.showinfo("Baby AGI Response", response) # window = tk.Tk() # window.title("Baby AGI Response") # button = tk.Button(window, text="Run Baby AGI", command=run_baby_agi) # button.pack() # window.mainloop() ''' import os import tkinter as tk from tkinter import messagebox from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore import faiss os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" def run_baby_agi(): embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) OBJECTIVE = "How to make butter chicken" llm = OpenAI(temperature=0) verbose = False max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations ) res = baby_agi({"objective": OBJECTIVE}) messagebox.showinfo("Baby AGI Response", res) window = tk.Tk() window.title("Baby AGI Response") button = tk.Button(window, text="Run Baby AGI", command=run_baby_agi) button.pack() window.mainloop() ''' # import os # import tkinter as tk # from tkinter import messagebox # from collections import deque # from typing import Dict, List, Optional, Any # from langchain import LLMChain, OpenAI, PromptTemplate # from langchain.embeddings import OpenAIEmbeddings # from langchain.llms import BaseLLM # from langchain.vectorstores.base import VectorStore # from pydantic import BaseModel, Field # from langchain.chains.base import Chain # from langchain.experimental import BabyAGI # from langchain.vectorstores import FAISS # from langchain.docstore import InMemoryDocstore # import faiss # os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" # def run_baby_agi(): # embeddings_model = OpenAIEmbeddings() # embedding_size = 1536 # index = faiss.IndexFlatL2(embedding_size) # vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) # OBJECTIVE =input("Enter your prompt here: ") #"How to make butter chicken" # llm = OpenAI(temperature=0) # verbose = False # max_iterations = 3 # baby_agi = BabyAGI.from_llm( # llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations # ) # res = baby_agi({"objective": OBJECTIVE}) # response_label.config(text=res) # window = tk.Tk() # window.title("Baby AGI Response") # button = tk.Button(window, text="Run Baby AGI", command=run_baby_agi) # button.pack() # response_label = tk.Label(window, text="") # response_label.pack() # window.mainloop() ''' import os import tkinter as tk from tkinter import messagebox from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore import faiss os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" def run_baby_agi(): embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) OBJECTIVE = input("Enter your prompt here: ") # "How to make butter chicken" llm = OpenAI(temperature=0) verbose = False max_iterations = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations ) res = baby_agi({"objective": OBJECTIVE}) response_label.config(text=res) # window = tk.Tk() # window.title("Baby AGI Response") # button = tk.Button(window, text="Run Baby AGI", command=run_baby_agi) # button.pack() # response_label = tk.Label(window, text="", wraplength=400) # response_label.pack() # window.mainloop() root = tk.Tk() root.title("ChatGPT Clone") # Create an entry field for input input_entry = tk.Entry(root, width=50) input_entry.pack() def button_click(): response = run_baby_agi() response_label.config(text=response) # Create a label to display the response response_label = tk.Label(root, text="", wraplength=400) response_label.pack() # Create a button to trigger the backend function button = tk.Button(root, text="Run", command=button_click) button.pack() # Run the Tkinter event loop root.mainloop() ''' import os import tkinter as tk from tkinter import messagebox from langchain import OpenAI, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" def chatgpt_clone(): template = """Assistant is a large language model trained by OpenAI. Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist. {history} Human: {human_input} Assistant:""" prompt = PromptTemplate( input_variables=["history", "human_input"], template=template ) chatgpt_chain = LLMChain( llm=OpenAI(temperature=0), prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(k=2), ) return chatgpt_chain.predict(human_input=input_entry.get()) root = tk.Tk() root.title("BabyAGI") # Create an entry field for input input_entry = tk.Entry(root, width=50) input_entry.pack() def button_click(): response = chatgpt_clone() response_label.config(text=response) # Create a label to display the response response_label = tk.Label(root, text="", wraplength=400) response_label.pack() # Create a button to trigger the backend function button = tk.Button(root, text="Run", command=button_click) button.pack() # Run the Tkinter event loop root.mainloop()
[ "Assistant is a large language model trained by OpenAI.\n\n Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\n Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\n Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\n\n {history}\n Human: {human_input}\n Assistant:", "human_input" ]
2024-01-10
aadibharane/Langchain_use_cases
Agents~plug_and_plai.py
#Plug-and-Plai #This notebook builds upon the idea of tool retrieval, but pulls all tools from plugnplai - a directory of AI Plugins. #Set up environment #Do necessary imports, etc. #Install plugnplai lib to get a list of active plugins from https://plugplai.com directory from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate from langchain import OpenAI, SerpAPIWrapper, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish from langchain.agents.agent_toolkits import NLAToolkit from langchain.tools.plugin import AIPlugin import re import plugnplai import os os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" def plug_and_plai(): #Setup LLM llm = OpenAI(temperature=0) #Set up plugins #Load and index plugins # Get all plugins from plugnplai.com urls = plugnplai.get_plugins() # Get ChatGPT plugins - only ChatGPT verified plugins urls = plugnplai.get_plugins(filter = 'ChatGPT') # Get working plugins - only tested plugins (in progress) urls = plugnplai.get_plugins(filter = 'working') AI_PLUGINS = [AIPlugin.from_url(url + "/.well-known/ai-plugin.json") for url in urls] #Tool Retriever ''' We will use a vectorstore to create embeddings for each tool description. Then, for an incoming query we can create embeddings for that query and do a similarity search for relevant tools. ''' from langchain.vectorstores import FAISS from langchain.embeddings import OpenAIEmbeddings from langchain.schema import Document embeddings = OpenAIEmbeddings() docs = [ Document(page_content=plugin.description_for_model, metadata={"plugin_name": plugin.name_for_model} ) for plugin in AI_PLUGINS ] vector_store = FAISS.from_documents(docs, embeddings) toolkits_dict = {plugin.name_for_model: NLAToolkit.from_llm_and_ai_plugin(llm, plugin) for plugin in AI_PLUGINS} retriever = vector_store.as_retriever() def get_tools(query): # Get documents, which contain the Plugins to use docs = retriever.get_relevant_documents(query) # Get the toolkits, one for each plugin tool_kits = [toolkits_dict[d.metadata["plugin_name"]] for d in docs] # Get the tools: a separate NLAChain for each endpoint tools = [] for tk in tool_kits: tools.extend(tk.nla_tools) return tools #We can now test this retriever to see if it seems to work. tools = get_tools("What could I do today with my kiddo") [t.name for t in tools] #Prompt Template #The prompt template is pretty standard, because we’re not actually changing that much logic in the actual #prompt template, but rather we are just changing how retrieval is done. # Set up the base template template = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Question: {input} {agent_scratchpad}""" #The custom prompt template now has the concept of a tools_getter, which we call on the input to select the tools to use from typing import Callable # Set up a prompt template class CustomPromptTemplate(StringPromptTemplate): # The template to use template: str ############## NEW ###################### # The list of tools available tools_getter: Callable def format(self, **kwargs) -> str: # Get the intermediate steps (AgentAction, Observation tuples) # Format them in a particular way intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " # Set the agent_scratchpad variable to that value kwargs["agent_scratchpad"] = thoughts ############## NEW ###################### tools = self.tools_getter(kwargs["input"]) # Create a tools variable from the list of tools provided kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) # Create a list of tool names for the tools provided kwargs["tool_names"] = ", ".join([tool.name for tool in tools]) return self.template.format(**kwargs) prompt = CustomPromptTemplate( template=template, tools_getter=get_tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps"] ) #Output Parser #The output parser is unchanged from the previous notebook, since we are not changing anything about the output format. class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # Check if agent should finish if "Final Answer:" in llm_output: return AgentFinish( # Return values is generally always a dictionary with a single `output` key # It is not recommended to try anything else at the moment :) return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) # Parse out the action and action input regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() #Set up LLM, stop sequence, and the agent llm = OpenAI(temperature=0) # LLM chain consisting of the LLM and a prompt llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) #Use the Agent agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("what shirts can i buy?") plug_and_plai()
[ "input", "intermediate_steps", "Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\n\n {tools}\n\n Use the following format:\n\n Question: the input question you must answer\n Thought: you should always think about what to do\n Action: the action to take, should be one of [{tool_names}]\n Action Input: the input to the action\n Observation: the result of the action\n ... (this Thought/Action/Action Input/Observation can repeat N times)\n Thought: I now know the final answer\n Final Answer: the final answer to the original input question\n\n Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Arg\"s\n\n Question: {input}\n {agent_scratchpad}" ]
2024-01-10
aadibharane/Langchain_use_cases
pandas_agent.py
import os import streamlit as st import pandas as pd from langchain.agents import create_pandas_dataframe_agent from langchain.llms import OpenAI from apikey import apikey # Set OpenAI API key os.environ["OPENAI_API_KEY"] = apikey # Define Streamlit app def pandas_agent(): # Title and description st.title("CSV Query App") st.write("Upload a CSV file and enter a query to get an answer.") file = st.file_uploader("Upload CSV file",type=["csv"]) if not file: st.stop() data = pd.read_csv(file) st.write("Data Preview:") st.dataframe(data.head()) agent = create_pandas_dataframe_agent(OpenAI(temperature=0),data,verbose=True) query = st.text_input("Enter a query:") if st.button("Execute"): answer = agent.run(query) st.write("Answer:") st.write(answer) if __name__ == "__main__": pandas_agent()
[]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulation_with_two_agents~camel.py
#CAMEL Role-Playing Autonomous Cooperative Agents ''' This is a langchain implementation of paper: “CAMEL: Communicative Agents for “Mind” Exploration of Large Scale Language Model Society". ''' #Overview: ''' The rapid advancement of conversational and chat-based language models has led to remarkable progress in complex task-solving. However, their success heavily relies on human input to guide the conversation, which can be challenging and time-consuming. This paper explores the potential of building scalable techniques to facilitate autonomous cooperation among communicative agents and provide insight into their “cognitive” processes. To address the challenges of achieving autonomous cooperation, we propose a novel communicative agent framework named role-playing. Our approach involves using inception prompting to guide chat agents toward task completion while maintaining consistency with human intentions. We showcase how role-playing can be used to generate conversational data for studying the behaviors and capabilities of chat agents, providing a valuable resource for investigating conversational language models. Our contributions include introducing a novel communicative agent framework, offering a scalable approach for studying the cooperative behaviors and capabilities of multi-agent systems, and open-sourcing our library to support research on communicative agentsand beyond. The original implementation: https://github.com/lightaime/camel Project website: https://www.camel-ai.org/ ''' #Import LangChain related modules from typing import List from langchain.chat_models import ChatOpenAI import os os.environ["OPENAI_API_KEY"] ="Your_OPENAI_API_KEY" from langchain.prompts.chat import ( SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) #Define a CAMEL agent helper class def camel(): class CAMELAgent: def __init__( self, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.system_message = system_message self.model = model self.init_messages() def reset(self) -> None: self.init_messages() return self.stored_messages def init_messages(self) -> None: self.stored_messages = [self.system_message] def update_messages(self, message: BaseMessage) -> List[BaseMessage]: self.stored_messages.append(message) return self.stored_messages def step( self, input_message: HumanMessage, ) -> AIMessage: messages = self.update_messages(input_message) output_message = self.model(messages) self.update_messages(output_message) return output_message #Setup OpenAI API key and roles and task for role-playing assistant_role_name = "Python Programmer" user_role_name = "Stock Trader" task = "Develop a trading bot for the stock market" word_limit = 50 # word limit for task brainstorming #Create a task specify agent for brainstorming and get the specified task task_specifier_sys_msg = SystemMessage(content="You can make a task more specific.") task_specifier_prompt = ( """Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}. Please make it more specific. Be creative and imaginative. Please reply with the specified task in {word_limit} words or less. Do not add anything else.""" ) task_specifier_template = HumanMessagePromptTemplate.from_template(template=task_specifier_prompt) task_specify_agent = CAMELAgent(task_specifier_sys_msg, ChatOpenAI(temperature=1.0)) task_specifier_msg = task_specifier_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task, word_limit=word_limit)[0] specified_task_msg = task_specify_agent.step(task_specifier_msg) print(f"Specified task: {specified_task_msg.content}") specified_task = specified_task_msg.content #Create inception prompts for AI assistant and AI user for role-playing assistant_inception_prompt = ( """Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me! We share a common interest in collaborating to successfully complete a task. You must help me to complete the task. Here is the task: {task}. Never forget our task! I must instruct you based on your expertise and my needs to complete the task. I must give you one instruction at a time. You must write a specific solution that appropriately completes the requested instruction. You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons. Do not add anything else other than your solution to my instruction. You are never supposed to ask me any questions you only answer questions. You are never supposed to reply with a flake solution. Explain your solutions. Your solution must be declarative sentences and simple present tense. Unless I say the task is completed, you should always start with: Solution: <YOUR_SOLUTION> <YOUR_SOLUTION> should be specific and provide preferable implementations and examples for task-solving. Always end <YOUR_SOLUTION> with: Next request.""" ) user_inception_prompt = ( """Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me. We share a common interest in collaborating to successfully complete a task. I must help you to complete the task. Here is the task: {task}. Never forget our task! You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways: 1. Instruct with a necessary input: Instruction: <YOUR_INSTRUCTION> Input: <YOUR_INPUT> 2. Instruct without any input: Instruction: <YOUR_INSTRUCTION> Input: None The "Instruction" describes a task or question. The paired "Input" provides further context or information for the requested "Instruction". You must give me one instruction at a time. I must write a response that appropriately completes the requested instruction. I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons. You should instruct me not ask me questions. Now you must start to instruct me using the two ways described above. Do not add anything else other than your instruction and the optional corresponding input! Keep giving me instructions and necessary inputs until you think the task is completed. When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>. Never say <CAMEL_TASK_DONE> unless my responses have solved your task.""" ) #Create a helper helper to get system messages for AI assistant and AI user from role names and the task def get_sys_msgs(assistant_role_name: str, user_role_name: str, task: str): assistant_sys_template = SystemMessagePromptTemplate.from_template(template=assistant_inception_prompt) assistant_sys_msg = assistant_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0] user_sys_template = SystemMessagePromptTemplate.from_template(template=user_inception_prompt) user_sys_msg = user_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0] return assistant_sys_msg, user_sys_msg #Create AI assistant agent and AI user agent from obtained system messages assistant_sys_msg, user_sys_msg = get_sys_msgs(assistant_role_name, user_role_name, specified_task) assistant_agent = CAMELAgent(assistant_sys_msg, ChatOpenAI(temperature=0.2)) user_agent = CAMELAgent(user_sys_msg, ChatOpenAI(temperature=0.2)) # Reset agents assistant_agent.reset() user_agent.reset() # Initialize chats assistant_msg = HumanMessage( content=(f"{user_sys_msg.content}. " "Now start to give me introductions one by one. " "Only reply with Instruction and Input.")) user_msg = HumanMessage(content=f"{assistant_sys_msg.content}") user_msg = assistant_agent.step(user_msg) #Start role-playing session to solve the task! print(f"Original task prompt:\n{task}\n") print(f"Specified task prompt:\n{specified_task}\n") chat_turn_limit, n = 30, 0 while n < chat_turn_limit: n += 1 user_ai_msg = user_agent.step(assistant_msg) user_msg = HumanMessage(content=user_ai_msg.content) print(f"AI User ({user_role_name}):\n\n{user_msg.content}\n\n") assistant_ai_msg = assistant_agent.step(user_msg) assistant_msg = HumanMessage(content=assistant_ai_msg.content) print(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n") if "<CAMEL_TASK_DONE>" in user_msg.content: break camel()
[ "Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me!\n We share a common interest in collaborating to successfully complete a task.\n You must help me to complete the task.\n Here is the task: {task}. Never forget our task!\n I must instruct you based on your expertise and my needs to complete the task.\n\n I must give you one instruction at a time.\n You must write a specific solution that appropriately completes the requested instruction.\n You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\n Do not add anything else other than your solution to my instruction.\n You are never supposed to ask me any questions you only answer questions.\n You are never supposed to reply with a flake solution. Explain your solutions.\n Your solution must be declarative sentences and simple present tense.\n Unless I say the task is completed, you should always start with:\n\n Solution: <YOUR_SOLUTION>\n\n <YOUR_SOLUTION> should be specific and provide preferable implementations and examples for task-solving.\n Always end <YOUR_SOLUTION> with: Next request.", "Now start to give me introductions one by one. ", "You can make a task more specific.", "Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.\n Please make it more specific. Be creative and imaginative.\n Please reply with the specified task in {word_limit} words or less. Do not add anything else.", "Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me.\n We share a common interest in collaborating to successfully complete a task.\n I must help you to complete the task.\n Here is the task: {task}. Never forget our task!\n You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:\n\n 1. Instruct with a necessary input:\n Instruction: <YOUR_INSTRUCTION>\n Input: <YOUR_INPUT>\n\n 2. Instruct without any input:\n Instruction: <YOUR_INSTRUCTION>\n Input: None\n\n The \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\n\n You must give me one instruction at a time.\n I must write a response that appropriately completes the requested instruction.\n I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\n You should instruct me not ask me questions.\n Now you must start to instruct me using the two ways described above.\n Do not add anything else other than your instruction and the optional corresponding input!\n Keep giving me instructions and necessary inputs until you think the task is completed.\n When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\n Never say <CAMEL_TASK_DONE> unless my responses have solved your task.", "Only reply with Instruction and Input." ]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulation_with_single_agents~sim_gym.py
# import gymnasium as gym # import inspect # import tenacity # import os # os.environ["OPENAI_API_KEY"] ="Your_OPENAI_API_KEY" # serpapi_key="your_serpapi_key" # from langchain.chat_models import ChatOpenAI # from langchain.schema import ( # AIMessage, # HumanMessage, # SystemMessage, # BaseMessage, # ) # from langchain.output_parsers import RegexParser # class GymnasiumAgent(): # @classmethod # def get_docs(cls, env): # return env.unwrapped.__doc__ # def __init__(self, model, env): # self.model = model # self.env = env # self.docs = self.get_docs(env) # self.instructions = """ # Your goal is to maximize your return, i.e. the sum of the rewards you receive. # I will give you an observation, reward, terminiation flag, truncation flag, and the return so far, formatted as: # Observation: <observation> # Reward: <reward> # Termination: <termination> # Truncation: <truncation> # Return: <sum_of_rewards> # You will respond with an action, formatted as: # Action: <action> # where you replace <action> with your actual action. # Do nothing else but return the action. # """ # self.action_parser = RegexParser( # regex=r"Action: (.*)", # output_keys=['action'], # default_output_key='action') # self.message_history = [] # self.ret = 0 # def random_action(self): # action = self.env.action_space.sample() # return action # def reset(self): # self.message_history = [ # SystemMessage(content=self.docs), # SystemMessage(content=self.instructions), # ] # def observe(self, obs, rew=0, term=False, trunc=False, info=None): # self.ret += rew # obs_message = f""" # Observation: {obs} # Reward: {rew} # Termination: {term} # Truncation: {trunc} # Return: {self.ret} # """ # self.message_history.append(HumanMessage(content=obs_message)) # return obs_message # def _act(self): # act_message = self.model(self.message_history) # self.message_history.append(act_message) # action = int(self.action_parser.parse(act_message.content)['action']) # return action # def act(self): # try: # for attempt in tenacity.Retrying( # stop=tenacity.stop_after_attempt(2), # wait=tenacity.wait_none(), # No waiting time between retries # retry=tenacity.retry_if_exception_type(ValueError), # before_sleep=lambda retry_state: print(f"ValueError occurred: {retry_state.outcome.exception()}, retrying..."), # ): # with attempt: # action = self._act() # except tenacity.RetryError as e: # action = self.random_action() # return action # env = gym.make("Blackjack-v1") # agent = GymnasiumAgent(model=ChatOpenAI(temperature=0.2), env=env) # observation, info = env.reset() # agent.reset() # obs_message = agent.observe(observation) # print(obs_message) # while True: # action = agent.act() # observation, reward, termination, truncation, info = env.step(action) # obs_message = agent.observe(observation, reward, termination, truncation, info) # print(f'Action: {action}') # print(obs_message) # if termination or truncation: # print('break', termination, truncation) # break # env.close() ########### import gymnasium as gym #import gym import tenacity import os os.environ["OPENAI_API_KEY"] ="Your_OPENAI_API_KEY" serpapi_key="your_serpapi_key" from langchain.chat_models import ChatOpenAI from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) from langchain.output_parsers import RegexParser def gymnasium_agent(): class GymnasiumAgent: @classmethod def get_docs(cls, env): return env.unwrapped.__doc__ def __init__(self, model, env): self.model = model self.env = env self.docs = self.get_docs(env) self.instructions = """ Your goal is to maximize your return, i.e. the sum of the rewards you receive. I will give you an observation, reward, termination flag, truncation flag, and the return so far, formatted as: Observation: <observation> Reward: <reward> Termination: <termination> Truncation: <truncation> Return: <sum_of_rewards> You will respond with an action, formatted as: Action: <action> where you replace <action> with your actual action. Do nothing else but return the action. """ self.action_parser = RegexParser( regex=r"Action: (.*)", output_keys=["action"], default_output_key="action" ) self.message_history = [] self.ret = 0 def random_action(self): action = self.env.action_space.sample() return action def reset(self): self.message_history = [ SystemMessage(content=self.docs), SystemMessage(content=self.instructions), ] def observe(self, obs, rew=0, term=False, trunc=False, info=None): self.ret += rew obs_message = f""" Observation: {obs} Reward: {rew} Termination: {term} Truncation: {trunc} Return: {self.ret} """ self.message_history.append(HumanMessage(content=obs_message)) return obs_message def _act(self): act_message = self.model(self.message_history) self.message_history.append(act_message) action = int(self.action_parser.parse(act_message.content)["action"]) return action def act(self): try: for attempt in tenacity.Retrying( stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_none(), # No waiting time between retries retry=tenacity.retry_if_exception_type(ValueError), before_sleep=lambda retry_state: print( f"ValueError occurred: {retry_state.outcome.exception()}, retrying..." ), ): with attempt: action = self._act() except tenacity.RetryError as e: action = self.random_action() return action env = gym.make("Blackjack-v1") agent = GymnasiumAgent(model=ChatOpenAI(temperature=0.2), env=env) observation, info = env.reset() agent.reset() obs_message = agent.observe(observation) print(obs_message) while True: action = agent.act() observation, reward, termination, truncation, info = env.step(action) obs_message = agent.observe(observation, reward, termination, truncation, info) print(f"Action: {action}") #return action,obs_message print(obs_message) if termination or truncation: print("break", termination, truncation) #return termination,truncation break env.close() gymnasium_agent()
[ "f\"\"\"\n Observation: {obs}\n Reward: {rew}\n Termination: {term}\n Truncation: {trunc}\n Return: {self.ret}\n " ]
2024-01-10
aadibharane/Langchain_use_cases
pages~Csv_agent.py
# from langchain.agents import create_csv_agent # from langchain.llms import OpenAI # from dotenv import load_dotenv # import streamlit as st # import os # # os.environ["OPENAI_API_KEY"] = "steamship_API_key" # def main(): # load_dotenv() # #Load the OpenAI API key from the environment variable # if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "": # print("OPENAI_API_KEY is not set") # exit(1) # else: # print("OPENAI_API_KEY is set") # st.set_page_config(page_title="Ask your CSV") # st.header("Ask your CSV 📈") # csv_file = st.file_uploader("Upload a CSV file", type="csv") # if csv_file is not None: # agent = create_csv_agent( # OpenAI(temperature=0), csv_file, verbose=True) # user_question = st.text_input("Ask a question about your CSV: ") # if user_question is not None and user_question != "": # with st.spinner(text="In progress..."): # st.write(agent.run(user_question)) # if __name__ == "__main__": # main() from langchain.agents import create_csv_agent from langchain.llms import OpenAI from dotenv import load_dotenv import streamlit as st import os # os.environ["OPENAI_API_KEY"] = ""Enter youe OpenAI Api key"" def csv_agent(): load_dotenv() # Load the OpenAI API key from the environment variable if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "": print("OPENAI_API_KEY is not set") exit(1) else: print("OPENAI_API_KEY is set") st.set_page_config(page_title="Ask your CSV") st.header("Ask your CSV 📈") csv_file = st.file_uploader("Upload a CSV file", type="csv") if csv_file is not None: # Save the uploaded file to a temporary location with open("temp.csv", "wb") as f: f.write(csv_file.getvalue()) agent = create_csv_agent( OpenAI(temperature=0), "temp.csv", verbose=True) user_question = st.text_input("Ask a question about your CSV: ") if user_question is not None and user_question != "": with st.spinner(text="In progress..."): st.write(agent.run(user_question)) # Remove the temporary file os.remove("temp.csv") if __name__ == "__main__": csv_agent()
[]
2024-01-10
aadibharane/Langchain_use_cases
Autonomous_agents~Baby_AGI~babyagi_with_tools.py
''' BabyAGI with Tools This notebook builds on top of baby agi, but shows how you can swap out the execution chain. The previous execution chain was just an LLM which made stuff up. By swapping it out with an agent that has access to tools, we can hopefully get real reliable information ''' #Install and Import Required Modules import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain from langchain.experimental import BabyAGI #Connect to the Vector Store #Depending on what vectorstore you use, this step may look different. #%pip install faiss-cpu > /dev/null #%pip install google-search-results > /dev/null from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore import os import tkinter as tk from tkinter import messagebox import openai os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" serpapi_key="serpapi_key" # Define your embedding model embeddings_model = OpenAIEmbeddings(openai_api_key="OPENAI_API_KEY") # Initialize the vectorstore as empty import faiss # Define your embedding model def babyagi_tools(): embeddings_model = OpenAIEmbeddings() # Initialize the vectorstore as empty import faiss embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) ''' Define the Chains BabyAGI relies on three LLM chains: Task creation chain to select new tasks to add to the list Task prioritization chain to re-prioritize tasks Execution Chain to execute the tasks ''' from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain import OpenAI, SerpAPIWrapper, LLMChain todo_prompt = PromptTemplate.from_template( "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}" ) todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt) search = SerpAPIWrapper(serpapi_api_key="5e4b783d1e905b2992665d83235e27aaa73e103f239fb757b84be1cc2c75c57b") tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!", ), ] prefix = """You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.""" suffix = """Question: {task} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["objective", "task", "context", "agent_scratchpad"], ) llm = OpenAI(temperature=0) llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names) agent_executor = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True ) #OBJECTIVE = "Write a weather report for Pune today" OBJECTIVE = input_entry.get() #"How to become most successful data scientist" # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=verbose, max_iterations=max_iterations ) #Run the BabyAGI #Now it’s time to create the BabyAGI controller and watch it try to accomplish your objective. baby_agi({"objective": OBJECTIVE}) #print(res) # return res root = tk.Tk() root.title("Baby AGI with tools") # Create an entry field for input input_entry = tk.Entry(root, width=50) input_entry.pack() def button_click(): response = babyagi_tools() response_label.config(text=response) # Create a label to display the response response_label = tk.Label(root, text="", wraplength=400) response_label.pack() # Create a button to trigger the backend function button = tk.Button(root, text="Run", command=button_click) button.pack() # Run the Tkinter event loop root.mainloop()
[ "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}", "agent_scratchpad", "context" ]
2024-01-10
aadibharane/Langchain_use_cases
Agents~img_text.py
# from steamship import Block, Steamship # import re # from IPython.display import Image # import os # os.environ['OPENAI_API_KEY'] = 'your_api_key' # os.environ['STEAMSHIP_API_KEY'] = "STEAMSHIP_API_KEY" # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # from pydantic import BaseModel, Field # llm = OpenAI(temperature=0) # # DALL-E # class SteamshipImageGenerationTool(BaseModel): # model_name: str = Field(default="dall-e", alias="model-name") # is_single_input: bool = True # description: str = "" # name: str = "" # SteamshipImageGenerationTool.update_forward_refs() # tools = [ # SteamshipImageGenerationTool() # ] # mrkl = initialize_agent(tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # output = mrkl.run("How would you visualize a parrot playing soccer?") # print(output) # from steamship import Block, Steamship # import re # from IPython.display import Image # import os # os.environ['OPENAI_API_KEY'] = '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' # os.environ['STEAMSHIP_API_KEY'] = "9A04BC86-CD0D-479E-B448-E245B19BC9AB" # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # from pydantic import BaseModel, Field # llm = OpenAI(temperature=0) # # DALL-E # class SteamshipImageGenerationTool(BaseModel): # model_name: str = Field(default="dall-e", alias="model-name") # is_single_input: bool = True # description: str = "" # name: str = "" # SteamshipImageGenerationTool.update_forward_refs() # tools = { # 'tools': [ # SteamshipImageGenerationTool() # ] # } # mrkl = initialize_agent(tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # output = mrkl.run("How would you visualize a parrot playing soccer?") # print(output) ################################# # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # from pydantic import BaseModel, Field # import requests # from PIL import Image # from io import BytesIO # import os # # Set up OpenAI API and Steamship API keys # openai_api_key = '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' # steamship_api_key = "9A04BC86-CD0D-479E-B448-E245B19BC9AB" # openai_api_key = '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' # os.environ['OPENAI_API_KEY'] = openai_api_key # os.environ['STEAMSHIP_API_KEY'] =steamship_api_key # # Initialize OpenAI language model # llm = OpenAI(api_key=openai_api_key, temperature=0.5) # class SteamshipImageGenerationTool(BaseModel): # model_name: str = Field(default="dall-e", alias="model-name") # is_single_input: bool = True # description: str = "" # name: str = "" # SteamshipImageGenerationTool.update_forward_refs() # # Create a list of tools with SteamshipImageGenerationTool # tools = { # 'tools': [ # SteamshipImageGenerationTool() # ] # } # # Initialize the agent # agent = initialize_agent(tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # # Generate the multi-modal output # prompt = "How would you visualize a parrot playing soccer?" # output = agent.run(prompt) # # Extract the image URL and display the image # image_url = output["tools_output"]["image"]["content"] # response = requests.get(image_url) # image = Image.open(BytesIO(response.content)) # image.show() # # Print the generated text description # description = output["tools_output"]["description"]["content"] # print("Generated Description:", description) # from langchain import OpenAI # from langchain.agents import initialize_agent # from langchain.agents import AgentType # from langchain.tools import SteamshipImageGenerationTool # from pydantic import BaseModel, Field # import requests # from PIL import Image # from io import BytesIO # import os # #Set up OpenAI API and Steamship API keys # openai_api_key = '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' # steamship_api_key = "9A04BC86-CD0D-479E-B448-E245B19BC9AB" # os.environ['OPENAI_API_KEY'] = openai_api_key # os.environ['STEAMSHIP_API_KEY'] = steamship_api_key # #Initialize OpenAI language model # llm = OpenAI(api_key=openai_api_key, temperature=0.5) # class SteamshipImageGenerationTool(BaseModel): # model_name: str = Field(default="dall-e", alias="model-name") # is_single_input: bool = True # description: str = "" # name: str = "" # SteamshipImageGenerationTool.update_forward_refs() # #Create a list of tools with SteamshipImageGenerationTool # tools = [{ # 'tool': SteamshipImageGenerationTool() # }] # #Initialize the agent # agent = initialize_agent(tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) # #Generate the multi-modal output # prompt = "How would you visualize a parrot playing soccer?" # output = agent.run(prompt) # #Extract the image URL and display the image # image_url = output["tools_output"]["image"]["content"] # response = requests.get(image_url) # image = Image.open(BytesIO(response.content)) # image.show() # #Print the generated text description # description = output["tools_output"]["description"]["content"] # print("Generated Description:", description) #@title Setup transformers_version = "v4.29.0" #@param ["main", "v4.29.0"] {allow-input: true} print(f"Setting up everything with transformers version {transformers_version}") #pip install huggingface_hub>=0.14.1 git+https://github.com/huggingface/transformers@$transformers_version -q diffusers accelerate datasets torch soundfile sentencepiece opencv-python openai import IPython import soundfile as sf def play_audio(audio): sf.write("speech_converted.wav", audio.numpy(), samplerate=16000) return IPython.display.Audio("speech_converted.wav") from huggingface_hub import notebook_login notebook_login() #@title Agent init agent_name = "OpenAI (API Key)" #@param ["StarCoder (HF Token)", "OpenAssistant (HF Token)", "OpenAI (API Key)"] import getpass if agent_name == "StarCoder (HF Token)": from transformers.tools import HfAgent agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder") print("StarCoder is initialized 💪") elif agent_name == "OpenAssistant (HF Token)": from transformers.tools import HfAgent agent = HfAgent(url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5") print("OpenAssistant is initialized 💪") if agent_name == "OpenAI (API Key)": from transformers.tools import OpenAiAgent pswd = getpass.getpass('OpenAI API key:') agent = OpenAiAgent(model="text-davinci-003", api_key=pswd) print("OpenAI is initialized 💪") boat = agent.run("Generate an image of a boat in the water") print(boat)
[]
2024-01-10
aadibharane/Langchain_use_cases
Querying_Tabular_Data~pandas_agent.py
# #Pandas Dataframe Agent ''' This notebook shows how to use agents to interact with a pandas dataframe. It is mostly optimized for question answering. NOTE: this agent calls the Python agent under the hood, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously. ''' from langchain.agents import create_pandas_dataframe_agent from langchain.llms import OpenAI import pandas as pd import os os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" def pandas_agent(): df = pd.read_csv('titanic.csv') # Single DataFrame example agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True) agent.run("how many rows are there?") agent.run("how many people have more than 3 siblings?") agent.run("what's the square root of the average age?") pandas_agent() # Multi DataFrame example # df1 = df.copy() # df1["Age"] = df1["Age"].fillna(df1["Age"].mean()) # merged_df = pd.concat([df, df1], axis=1) # Concatenate the DataFrames horizontally # agent = create_pandas_dataframe_agent(OpenAI(temperature=0), merged_df, verbose=True) # agent.run("how many rows in the age column are different?")
[]
2024-01-10
aadibharane/Langchain_use_cases
Agents~ai_plugins.py
#Custom Agent with PlugIn Retrieval ''' This notebook combines two concepts in order to build a custom agent that can interact with AI Plugins: 1.Custom Agent with Retrieval: This introduces the concept of retrieving many tools, which is useful when trying to work with arbitrarily many plugins. 2.Natural Language API Chains: This creates Natural Language wrappers around OpenAPI endpoints. This is useful because (1) plugins use OpenAPI endpoints under the hood, (2) wrapping them in an NLAChain allows the router agent to call it more easily. ''' ''' The novel idea introduced in this notebook is the idea of using retrieval to select not the tools explicitly, but the set of OpenAPI specs to use. We can then generate tools from those OpenAPI specs. The use case for this is when trying to get agents to use plugins. It may be more efficient to choose plugins first, then the endpoints, rather than the endpoints directly. This is because the plugins may contain more useful information for selection. ''' #Custom Agent with PlugIn Retrieval ''' This notebook combines two concepts in order to build a custom agent that can interact with AI Plugins: 1.Custom Agent with Retrieval: This introduces the concept of retrieving many tools, which is useful when trying to work with arbitrarily many plugins. 2.Natural Language API Chains: This creates Natural Language wrappers around OpenAPI endpoints. This is useful because (1) plugins use OpenAPI endpoints under the hood, (2) wrapping them in an NLAChain allows the router agent to call it more easily. ''' ''' The novel idea introduced in this notebook is the idea of using retrieval to select not the tools explicitly, but the set of OpenAPI specs to use. We can then generate tools from those OpenAPI specs. The use case for this is when trying to get agents to use plugins. It may be more efficient to choose plugins first, then the endpoints, rather than the endpoints directly. This is because the plugins may contain more useful information for selection. ''' import os os.environ["OPENAI_API_KEY"] ="your_api_key" #Set up environment #Do necessary imports, etc. from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate from langchain import OpenAI, SerpAPIWrapper, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish from langchain.agents.agent_toolkits import NLAToolkit from langchain.tools.plugin import AIPlugin import re #Setup LLM def ai_plugins(): llm = OpenAI(temperature=0) #wSet up plugins #Load and index plugins urls = [ "https://datasette.io/.well-known/ai-plugin.json", "https://api.speak.com/.well-known/ai-plugin.json", "https://www.wolframalpha.com/.well-known/ai-plugin.json", "https://www.zapier.com/.well-known/ai-plugin.json", "https://www.klarna.com/.well-known/ai-plugin.json", "https://www.joinmilo.com/.well-known/ai-plugin.json", "https://slack.com/.well-known/ai-plugin.json", "https://schooldigger.com/.well-known/ai-plugin.json", ] AI_PLUGINS = [AIPlugin.from_url(url) for url in urls] #Tool Retriever #We will use a vectorstore to create embeddings for each tool description. Then, for an incoming query we can #create embeddings for that query and do a similarity search for relevant tools. from langchain.vectorstores import FAISS from langchain.embeddings import OpenAIEmbeddings from langchain.schema import Document embeddings = OpenAIEmbeddings() docs = [ Document(page_content=plugin.description_for_model, metadata={"plugin_name": plugin.name_for_model} ) for plugin in AI_PLUGINS ] vector_store = FAISS.from_documents(docs, embeddings) toolkits_dict = {plugin.name_for_model: NLAToolkit.from_llm_and_ai_plugin(llm, plugin) for plugin in AI_PLUGINS} #print(toolkits_dict) retriever = vector_store.as_retriever() def get_tools(query): # Get documents, which contain the Plugins to use docs = retriever.get_relevant_documents(query) # Get the toolkits, one for each plugin tool_kits = [toolkits_dict[d.metadata["plugin_name"]] for d in docs] # Get the tools: a separate NLAChain for each endpoint tools = [] for tk in tool_kits: tools.extend(tk.nla_tools) return tools #We can now test this retriever to see if it seems to work. tools = get_tools("What could I do today with my kiddo") a=[t.name for t in tools] #print(a) tools = get_tools("what shirts can i buy?") a=[t.name for t in tools] #print(a) #Prompt Template ''' The prompt template is pretty standard, because we’re not actually changing that much logic in the actual prompt template, but rather we are just changing how retrieval is done. ''' # Set up the base template template = """Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Arg"s Question: {input} {agent_scratchpad}""" #The custom prompt template now has the concept of a tools_getter, which we call on the input to select the tools to use from typing import Callable # Set up a prompt template class CustomPromptTemplate(StringPromptTemplate): # The template to use template: str ############## NEW ###################### # The list of tools available tools_getter: Callable def format(self, **kwargs) -> str: # Get the intermediate steps (AgentAction, Observation tuples) # Format them in a particular way intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " # Set the agent_scratchpad variable to that value kwargs["agent_scratchpad"] = thoughts ############## NEW ###################### tools = self.tools_getter(kwargs["input"]) # Create a tools variable from the list of tools provided kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) # Create a list of tool names for the tools provided kwargs["tool_names"] = ", ".join([tool.name for tool in tools]) return self.template.format(**kwargs) prompt = CustomPromptTemplate( template=template, tools_getter=get_tools, # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically # This includes the `intermediate_steps` variable because that is needed input_variables=["input", "intermediate_steps"] ) #Output Parser #The output parser is unchanged from the previous notebook, since we are not changing anything about the output format. class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # Check if agent should finish if "Final Answer:" in llm_output: return AgentFinish( # Return values is generally always a dictionary with a single `output` key # It is not recommended to try anything else at the moment :) return_values={"output": llm_output.split("Final Answer:")[-1].strip()}, log=llm_output, ) # Parse out the action and action input regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)" match = re.search(regex, llm_output, re.DOTALL) if not match: raise ValueError(f"Could not parse LLM output: `{llm_output}`") action = match.group(1).strip() action_input = match.group(2) # Return the action and action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() #Set up LLM, stop sequence, and the agent #Also the same as the previous notebook llm = OpenAI(temperature=0) # LLM chain consisting of the LLM and a prompt llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) #Use the Agent #Now we can use it! agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_executor.run("what shirts can i buy?") ai_plugins()
[ "input", "intermediate_steps", "Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:\n\n {tools}\n\n Use the following format:\n\n Question: the input question you must answer\n Thought: you should always think about what to do\n Action: the action to take, should be one of [{tool_names}]\n Action Input: the input to the action\n Observation: the result of the action\n ... (this Thought/Action/Action Input/Observation can repeat N times)\n Thought: I now know the final answer\n Final Answer: the final answer to the original input question\n\n Begin! Remember to speak as a pirate when giving your final answer. Use lots of \"Arg\"s\n\n Question: {input}\n {agent_scratchpad}" ]
2024-01-10
aadibharane/Langchain_use_cases
csv_agent.py
# from langchain.agents import create_csv_agent # from langchain.llms import OpenAI # from dotenv import load_dotenv # import streamlit as st # import os # # os.environ["OPENAI_API_KEY"] = "steamship_API_key" # def main(): # load_dotenv() # #Load the OpenAI API key from the environment variable # if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "": # print("OPENAI_API_KEY is not set") # exit(1) # else: # print("OPENAI_API_KEY is set") # st.set_page_config(page_title="Ask your CSV") # st.header("Ask your CSV 📈") # csv_file = st.file_uploader("Upload a CSV file", type="csv") # if csv_file is not None: # agent = create_csv_agent( # OpenAI(temperature=0), csv_file, verbose=True) # user_question = st.text_input("Ask a question about your CSV: ") # if user_question is not None and user_question != "": # with st.spinner(text="In progress..."): # st.write(agent.run(user_question)) # if __name__ == "__main__": # main() from langchain.agents import create_csv_agent from langchain.llms import OpenAI from dotenv import load_dotenv import streamlit as st import os # os.environ["OPENAI_API_KEY"] = "steamship_API_key" def csv_agent(): load_dotenv() # Load the OpenAI API key from the environment variable if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "": print("OPENAI_API_KEY is not set") exit(1) else: print("OPENAI_API_KEY is set") st.set_page_config(page_title="Ask your CSV") st.header("Ask your CSV 📈") csv_file = st.file_uploader("Upload a CSV file", type="csv") if csv_file is not None: # Save the uploaded file to a temporary location with open("temp.csv", "wb") as f: f.write(csv_file.getvalue()) agent = create_csv_agent( OpenAI(temperature=0), "temp.csv", verbose=True) user_question = st.text_input("Ask a question about your CSV: ") if user_question is not None and user_question != "": with st.spinner(text="In progress..."): st.write(agent.run(user_question)) # Remove the temporary file os.remove("temp.csv") if __name__ == "__main__": csv_agent()
[]
2024-01-10
aadibharane/Langchain_use_cases
pages~AutoGPT.py
import os from apikey import apikey import streamlit as st from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SequentialChain from langchain.memory import ConversationBufferMemory from langchain.utilities import WikipediaAPIWrapper os.environ['OPENAI_API_KEY'] = apikey # App framework def autogpt(): st.title('🦜🔗 AutoGPT Creator') prompt = st.text_input('Plug in your prompt here') # Prompt templates title_template = PromptTemplate( input_variables = ['topic'], template='write me a youtube video title about {topic}' ) script_template = PromptTemplate( input_variables = ['title', 'wikipedia_research'], template='write me a youtube video script based on this title TITLE: {title} while leveraging this wikipedia reserch:{wikipedia_research} ' ) # Memory title_memory = ConversationBufferMemory(input_key='topic', memory_key='chat_history') script_memory = ConversationBufferMemory(input_key='title', memory_key='chat_history') # Llms llm = OpenAI(temperature=0.9) title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True, output_key='title', memory=title_memory) script_chain = LLMChain(llm=llm, prompt=script_template, verbose=True, output_key='script', memory=script_memory) wiki = WikipediaAPIWrapper() # Show stuff to the screen if there's a prompt if prompt: title = title_chain.run(prompt) wiki_research = wiki.run(prompt) script = script_chain.run(title=title, wikipedia_research=wiki_research) st.write(title) st.write(script) with st.expander('Title History'): st.info(title_memory.buffer) with st.expander('Script History'): st.info(script_memory.buffer) with st.expander('Wikipedia Research'): st.info(wiki_research) autogpt()
[ "write me a youtube video title about {topic}", "Plug in your prompt here", "wikipedia_research", "write me a youtube video script based on this title TITLE: {title} while leveraging this wikipedia reserch:{wikipedia_research} " ]
2024-01-10
aadibharane/Langchain_use_cases
pages~conversation_memory.py
import os import streamlit as st import langchain.memory import langchain.llms import langchain.chains from apikey import apikey from langchain.memory import ConversationBufferMemory from langchain.memory import ChatMessageHistory from langchain.llms import OpenAI from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory os.environ["OPENAI_API_KEY"] =apikey def conversation_memory(): history = ChatMessageHistory() history.add_user_message("hi!") history.add_ai_message("whats up?") memory = ConversationBufferMemory(chat_memory=history) llm = OpenAI(temperature=0) conversation = ConversationChain(llm=llm, memory=memory, verbose=True) def predict(user_input): response = conversation.predict(input=user_input) return response st.title("Conversation Memory Chatbot") user_input = st.text_input("Enter your message:") if user_input: response = predict(user_input) st.write("AI response:", response) if __name__ == "__main__": conversation_memory()
[]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulation_with_two_agents~two_player_d_and_d.py
#Two-Player Dungeons & Dragons ''' we show how we can use concepts from CAMEL to simulate a role-playing game with a protagonist and a dungeon master. To simulate this game, we create an DialogueSimulator class that coordinates the dialogue between the two agents. ''' import os os.environ["OPENAI_API_KEY"] ="Enter your openai key" serpapi_key="your_serpapi_key" #Import LangChain related modules from typing import List, Dict, Callable from langchain.chat_models import ChatOpenAI from langchain.schema import ( HumanMessage, SystemMessage, ) #DialogueAgent class: ''' The DialogueAgent class is a simple wrapper around the ChatOpenAI model that stores the message history from the dialogue_agent’s point of view by simply concatenating the messages as strings. It exposes two methods: send(): applies the chatmodel to the message history and returns the message string receive(name, message): adds the message spoken by name to message history ''' def two_player(): class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") #DialogueSimulator class: ''' The DialogueSimulator class takes a list of agents. At each step, it performs the following: Select the next speaker Calls the next speaker to send a message Broadcasts the message to all other agents Update the step counter. The selection of the next speaker can be implemented as any function, but in this case we simply loop through the agents. ''' class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) # increment time self._step += 1 def step(self) -> tuple[str, str]: # 1. choose the next speaker speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] # 2. next speaker sends message message = speaker.send() # 3. everyone receives message for receiver in self.agents: receiver.receive(speaker.name, message) # 4. increment time self._step += 1 return speaker.name, message #Define roles and quest protagonist_name = "Harry Potter" storyteller_name = "Dungeon Master" quest = "Find all of Lord Voldemort's seven horcruxes." word_limit = 50 # word limit for task brainstorming #Ask an LLM to add detail to the game description game_description = f"""Here is the topic for a Dungeons & Dragons game: {quest}. There is one player in this game: the protagonist, {protagonist_name}. The story is narrated by the storyteller, {storyteller_name}.""" player_descriptor_system_message = SystemMessage( content="You can add detail to the description of a Dungeons & Dragons player.") protagonist_specifier_prompt = [ player_descriptor_system_message, HumanMessage(content= f"""{game_description} Please reply with a creative description of the protagonist, {protagonist_name}, in {word_limit} words or less. Speak directly to {protagonist_name}. Do not add anything else.""" ) ] protagonist_description = ChatOpenAI(temperature=1.0)(protagonist_specifier_prompt).content storyteller_specifier_prompt = [ player_descriptor_system_message, HumanMessage(content= f"""{game_description} Please reply with a creative description of the storyteller, {storyteller_name}, in {word_limit} words or less. Speak directly to {storyteller_name}. Do not add anything else.""" ) ] storyteller_description = ChatOpenAI(temperature=1.0)(storyteller_specifier_prompt).content print('Protagonist Description:') print(protagonist_description) print('Storyteller Description:') print(storyteller_description) #Protagonist and dungeon master system messages protagonist_system_message = SystemMessage(content=( f"""{game_description} Never forget you are the protagonist, {protagonist_name}, and I am the storyteller, {storyteller_name}. Your character description is as follows: {protagonist_description}. You will propose actions you plan to take and I will explain what happens when you take those actions. Speak in the first person from the perspective of {protagonist_name}. For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of {storyteller_name}. Do not forget to finish speaking by saying, 'It is your turn, {storyteller_name}.' Do not add anything else. Remember you are the protagonist, {protagonist_name}. Stop speaking the moment you finish speaking from your perspective. """ )) storyteller_system_message = SystemMessage(content=( f"""{game_description} Never forget you are the storyteller, {storyteller_name}, and I am the protagonist, {protagonist_name}. Your character description is as follows: {storyteller_description}. I will propose actions I plan to take and you will explain what happens when I take those actions. Speak in the first person from the perspective of {storyteller_name}. For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of {protagonist_name}. Do not forget to finish speaking by saying, 'It is your turn, {protagonist_name}.' Do not add anything else. Remember you are the storyteller, {storyteller_name}. Stop speaking the moment you finish speaking from your perspective. """ )) #Protagonist and dungeon master system messages protagonist_system_message = SystemMessage(content=( f"""{game_description} Never forget you are the protagonist, {protagonist_name}, and I am the storyteller, {storyteller_name}. Your character description is as follows: {protagonist_description}. You will propose actions you plan to take and I will explain what happens when you take those actions. Speak in the first person from the perspective of {protagonist_name}. For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of {storyteller_name}. Do not forget to finish speaking by saying, 'It is your turn, {storyteller_name}.' Do not add anything else. Remember you are the protagonist, {protagonist_name}. Stop speaking the moment you finish speaking from your perspective. """ )) storyteller_system_message = SystemMessage(content=( f"""{game_description} Never forget you are the storyteller, {storyteller_name}, and I am the protagonist, {protagonist_name}. Your character description is as follows: {storyteller_description}. I will propose actions I plan to take and you will explain what happens when I take those actions. Speak in the first person from the perspective of {storyteller_name}. For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of {protagonist_name}. Do not forget to finish speaking by saying, 'It is your turn, {protagonist_name}.' Do not add anything else. Remember you are the storyteller, {storyteller_name}. Stop speaking the moment you finish speaking from your perspective. """ )) #Use an LLM to create an elaborate quest description quest_specifier_prompt = [ SystemMessage(content="You can make a task more specific."), HumanMessage(content= f"""{game_description} You are the storyteller, {storyteller_name}. Please make the quest more specific. Be creative and imaginative. Please reply with the specified quest in {word_limit} words or less. Speak directly to the protagonist {protagonist_name}. Do not add anything else.""" ) ] specified_quest = ChatOpenAI(temperature=1.0)(quest_specifier_prompt).content print(f"Original quest:\n{quest}\n") print(f"Detailed quest:\n{specified_quest}\n") #Main Loop protagonist = DialogueAgent(name=protagonist_name, system_message=protagonist_system_message, model=ChatOpenAI(temperature=0.2)) storyteller = DialogueAgent(name=storyteller_name, system_message=storyteller_system_message, model=ChatOpenAI(temperature=0.2)) def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int: idx = step % len(agents) return idx max_iters = 6 n = 0 simulator = DialogueSimulator( agents=[storyteller, protagonist], selection_function=select_next_speaker ) simulator.reset() simulator.inject(storyteller_name, specified_quest) print(f"({storyteller_name}): {specified_quest}") print('\n') while n < max_iters: name, message = simulator.step() print(f"({name}): {message}") print('\n') n += 1 two_player()
[ "PLACEHOLDER\n \n You are the storyteller, PLACEHOLDER.\n Please make the quest more specific. Be creative and imaginative.\n Please reply with the specified quest in PLACEHOLDER words or less. \n Speak directly to the protagonist PLACEHOLDER.\n Do not add anything else.", "\n", "You can add detail to the description of a Dungeons & Dragons player.", "PLACEHOLDER\n Never forget you are the protagonist, PLACEHOLDER, and I am the storyteller, PLACEHOLDER. \n Your character description is as follows: PLACEHOLDER.\n You will propose actions you plan to take and I will explain what happens when you take those actions.\n Speak in the first person from the perspective of PLACEHOLDER.\n For describing your own body movements, wrap your description in '*'.\n Do not change roles!\n Do not speak from the perspective of PLACEHOLDER.\n Do not forget to finish speaking by saying, 'It is your turn, PLACEHOLDER.'\n Do not add anything else.\n Remember you are the protagonist, PLACEHOLDER.\n Stop speaking the moment you finish speaking from your perspective.\n ", "PLACEHOLDER\n Please reply with a creative description of the storyteller, PLACEHOLDER, in PLACEHOLDER words or less. \n Speak directly to PLACEHOLDER.\n Do not add anything else.", "PLACEHOLDER\n Please reply with a creative description of the protagonist, PLACEHOLDER, in PLACEHOLDER words or less. \n Speak directly to PLACEHOLDER.\n Do not add anything else.", "You can make a task more specific.", "PLACEHOLDER\n Never forget you are the storyteller, PLACEHOLDER, and I am the protagonist, PLACEHOLDER. \n Your character description is as follows: PLACEHOLDER.\n I will propose actions I plan to take and you will explain what happens when I take those actions.\n Speak in the first person from the perspective of PLACEHOLDER.\n For describing your own body movements, wrap your description in '*'.\n Do not change roles!\n Do not speak from the perspective of PLACEHOLDER.\n Do not forget to finish speaking by saying, 'It is your turn, PLACEHOLDER.'\n Do not add anything else.\n Remember you are the storyteller, PLACEHOLDER.\n Stop speaking the moment you finish speaking from your perspective.\n " ]
2024-01-10
aadibharane/Langchain_use_cases
Querying_Tabular_Data~csv_agent.py
#CSV Agent ''' This notebook shows how to use agents to interact with a csv. It is mostly optimized for question answering. NOTE: this agent calls the Pandas DataFrame agent under the hood, which in turn calls the Python agent, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously. ''' import os os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" from langchain.agents import create_csv_agent from langchain.llms import OpenAI def csv_agent(): agent = create_csv_agent(OpenAI(temperature=0), 'titanic.csv', verbose=True) agent.run("how many rows are there?") agent.run("how many people have more than 3 siblings") agent.run("whats the square root of the average age?") csv_agent() #Multi CSV Example #This next part shows how the agent can interact with multiple csv files passed in as a list. # agent = create_csv_agent(OpenAI(temperature=0), ['titanic.csv', 'titanic_age_fillna.csv'], verbose=True) # agent.run("how many rows in the age column are different?") # import os # from langchain.agents import create_csv_agent # from langchain.llms import OpenAI # os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY" # # Single CSV file example # agent = create_csv_agent(OpenAI(temperature=0), 'titanic.csv', verbose=True) # agent.run("how many rows are there?") # agent.run("how many people have more than 3 siblings?") # agent.run("what's the square root of the average age?") # # Multi CSV example # csv_files = ['titanic.csv', 'titanic_age_fillna.csv'] # agents = [] # for file in csv_files: # agent = create_csv_agent(OpenAI(temperature=0), file, verbose=True) # agents.append(agent) # for agent in agents: # agent.run("how many rows in the age column are different?")
[]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulations_with_Multiple_Agents~peetingzoo.py
#Multi-Agent Simulated Environment: Petting Zoo ''' In this example, we show how to define multi-agent simulations with simulated environments. Like ours single-agent example with Gymnasium, we create an agent-environment loop with an externally defined environment. The main difference is that we now implement this kind of interaction loop with multiple agents instead. We will use the Petting Zoo library, which is the multi-agent counterpart to Gymnasium. ''' #Install pettingzoo and other dependencies #!pip install pettingzoo pygame rlcard #Import modules import os os.environ["OPENAI_API_KEY"] ="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" import collections import inspect import tenacity from langchain.chat_models import ChatOpenAI from langchain.schema import ( HumanMessage, SystemMessage, ) from langchain.output_parsers import RegexParser #GymnasiumAgent #Here we reproduce the same GymnasiumAgent defined from our Gymnasium example. If after multiple retries it does not take # a valid action, it simply takes a random action. def peetingzoo(): class GymnasiumAgent(): @classmethod def get_docs(cls, env): return env.unwrapped.__doc__ def __init__(self, model, env): self.model = model self.env = env self.docs = self.get_docs(env) self.instructions = """ Your goal is to maximize your return, i.e. the sum of the rewards you receive. I will give you an observation, reward, terminiation flag, truncation flag, and the return so far, formatted as: Observation: <observation> Reward: <reward> Termination: <termination> Truncation: <truncation> Return: <sum_of_rewards> You will respond with an action, formatted as: Action: <action> where you replace <action> with your actual action. Do nothing else but return the action. """ self.action_parser = RegexParser( regex=r"Action: (.*)", output_keys=['action'], default_output_key='action') self.message_history = [] self.ret = 0 def random_action(self): action = self.env.action_space.sample() return action def reset(self): self.message_history = [ SystemMessage(content=self.docs), SystemMessage(content=self.instructions), ] def observe(self, obs, rew=0, term=False, trunc=False, info=None): self.ret += rew obs_message = f""" Observation: {obs} Reward: {rew} Termination: {term} Truncation: {trunc} Return: {self.ret} """ self.message_history.append(HumanMessage(content=obs_message)) return obs_message def _act(self): act_message = self.model(self.message_history) self.message_history.append(act_message) action = int(self.action_parser.parse(act_message.content)['action']) return action def act(self): try: for attempt in tenacity.Retrying( stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_none(), # No waiting time between retries retry=tenacity.retry_if_exception_type(ValueError), before_sleep=lambda retry_state: print(f"ValueError occurred: {retry_state.outcome.exception()}, retrying..."), ): with attempt: action = self._act() except tenacity.RetryError as e: action = self.random_action() return action #Main loop def main(agents, env): env.reset() for name, agent in agents.items(): agent.reset() for agent_name in env.agent_iter(): observation, reward, termination, truncation, info = env.last() obs_message = agents[agent_name].observe( observation, reward, termination, truncation, info) print(obs_message) if termination or truncation: action = None else: action = agents[agent_name].act() print(f'Action: {action}') env.step(action) env.close() #PettingZooAgent #The PettingZooAgent extends the GymnasiumAgent to the multi-agent setting. The main differences are: #PettingZooAgent takes in a name argument to identify it among multiple agents class PettingZooAgent(GymnasiumAgent): @classmethod def get_docs(cls, env): return inspect.getmodule(env.unwrapped).__doc__ def __init__(self, name, model, env): super().__init__(model, env) self.name = name def random_action(self): action = self.env.action_space(self.name).sample() return action #Rock, Paper, Scissors #We can now run a simulation of a multi-agent rock, paper, scissors game using the PettingZooAgent. from pettingzoo.classic import rps_v2 env = rps_v2.env(max_cycles=3, render_mode="human") agents = {name: PettingZooAgent(name=name, model=ChatOpenAI(temperature=1), env=env) for name in env.possible_agents} main(agents, env) #ActionMaskAgent ''' Some PettingZoo environments provide an action_mask to tell the agent which actions are valid. The ActionMaskAgent subclasses PettingZooAgent to use information from the action_mask to select actions. ''' class ActionMaskAgent(PettingZooAgent): def __init__(self, name, model, env): super().__init__(name, model, env) self.obs_buffer = collections.deque(maxlen=1) def random_action(self): obs = self.obs_buffer[-1] action = self.env.action_space(self.name).sample(obs["action_mask"]) return action def reset(self): self.message_history = [ SystemMessage(content=self.docs), SystemMessage(content=self.instructions), ] def observe(self, obs, rew=0, term=False, trunc=False, info=None): self.obs_buffer.append(obs) return super().observe(obs, rew, term, trunc, info) def _act(self): valid_action_instruction = "Generate a valid action given by the indices of the `action_mask` that are not 0, according to the action formatting rules." self.message_history.append(HumanMessage(content=valid_action_instruction)) return super()._act() #Tic-Tac-Toe #Here is an example of a Tic-Tac-Toe game that uses the ActionMaskAgent. from pettingzoo.classic import tictactoe_v3 env = tictactoe_v3.env(render_mode="human") agents = {name: ActionMaskAgent(name=name, model=ChatOpenAI(temperature=0.2), env=env) for name in env.possible_agents} res=main(agents, env) print(res) #Texas Hold’em No Limit #Here is an example of a Texas Hold’em No Limit game that uses the ActionMaskAgent. from pettingzoo.classic import texas_holdem_no_limit_v6 env = texas_holdem_no_limit_v6.env(num_players=4, render_mode="human") agents = {name: ActionMaskAgent(name=name, model=ChatOpenAI(temperature=0.2), env=env) for name in env.possible_agents} res=main(agents, env) print(res) peetingzoo()
[ "Generate a valid action given by the indices of the `action_mask` that are not 0, according to the action formatting rules.", "f\"\"\"\n Observation: {obs}\n Reward: {rew}\n Termination: {term}\n Truncation: {trunc}\n Return: {self.ret}\n " ]
2024-01-10
aadibharane/Langchain_use_cases
Autonomous_agents~AutoGPT~autogpt.py
#Set up tools #We’ll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool # from langchain.utilities import SerpAPIWrapper # from langchain.agents import Tool # from langchain.tools.file_management.write import WriteFileTool # from langchain.tools.file_management.read import ReadFileTool # from serpapi import GoogleSearch # from langchain.experimental import AutoGPT # from langchain.chat_models import ChatOpenAI # import os # os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" # serpapi_key="serpapi_key" # search = SerpAPIWrapper(serpapi_api_key=serpapi_key) # tools = [ # Tool( # name = "search", # func=search.run, # description="useful for when you need to answer questions about current events. You should ask targeted questions" # ), # WriteFileTool(), # ReadFileTool(), # ] # #print(tools) # #Set up memory # #The memory here is used for the agents intermediate steps # from langchain.vectorstores import FAISS # from langchain.docstore import InMemoryDocstore # from langchain.embeddings import OpenAIEmbeddings # # Define your embedding model # def autogpt(): # embeddings_model = OpenAIEmbeddings() # # Initialize the vectorstore as empty # import faiss # embedding_size = 1536 # #index = faiss.IndexFlatL2(embedding_size) # index = faiss.IndexFlat(embedding_size, faiss.METRIC_L2) # vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) # #index = faiss.IndexFlat(embedding_size, faiss.METRIC_L2) # #Setup model and AutoGPT # #Initialize everything! We will use ChatOpenAI model # agent = AutoGPT.from_llm_and_tools( # ai_name="Tom", # ai_role="Assistant", # tools=tools, # llm=ChatOpenAI(temperature=0), # memory=vectorstore.as_retriever() # ) # # Set verbose to be true # agent.chain.verbose = True # #Run an example # #Here we will make it write a weather report for SF # res=agent.run(input("Enter a a prompt: ") )#["write a weather report for SF today"]) # print(res) # return res # autogpt() import tkinter as tk from langchain.utilities import SerpAPIWrapper from langchain.agents import Tool from langchain.tools.file_management.write import WriteFileTool from langchain.tools.file_management.read import ReadFileTool from serpapi import GoogleSearch from langchain.experimental import AutoGPT from langchain.chat_models import ChatOpenAI import os from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings def run_autogpt(prompt): os.environ["OPENAI_API_KEY"] = "sOPENAI_API_KEY" serpapi_key = "serpapi_key" search = SerpAPIWrapper(serpapi_api_key=serpapi_key) tools = [ Tool( name="search", func=search.run, description="useful for when you need to answer questions about current events. You should ask targeted questions" ), WriteFileTool(), ReadFileTool(), ] def autogpt(): embeddings_model = OpenAIEmbeddings() import faiss embedding_size = 1536 index = faiss.IndexFlat(embedding_size, faiss.METRIC_L2) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) agent = AutoGPT.from_llm_and_tools( ai_name="Tom", ai_role="Assistant", tools=tools, llm=ChatOpenAI(temperature=0), memory=vectorstore.as_retriever() ) agent.chain.verbose = True res = agent.run(prompt) return res return autogpt() def run_autogpt_gui(): def submit_prompt(): prompt = prompt_entry.get() output = run_autogpt(prompt) output_text.config(state=tk.NORMAL) output_text.delete("1.0", tk.END) output_text.insert(tk.END, output) output_text.config(state=tk.DISABLED) # Create the GUI window = tk.Tk() window.title("AutoGPT with Tkinter") prompt_label = tk.Label(window, text="Enter a prompt:") prompt_label.pack() prompt_entry = tk.Entry(window, width=50) prompt_entry.pack() submit_button = tk.Button(window, text="Submit", command=submit_prompt) submit_button.pack() output_label = tk.Label(window, text="Output:") output_label.pack() output_text = tk.Text(window, width=50, height=10) output_text.config(state=tk.DISABLED) output_text.pack() window.mainloop() run_autogpt_gui()
[ "Enter a prompt:" ]
2024-01-10
aadibharane/Langchain_use_cases
Chatbots~conversation_agent.py
#Conversation Agent ''' This notebook walks through using an agent optimized for conversation. Other agents are often optimized for using tools to figure out the best response, which is not ideal in a conversational setting where you may want the agent to be able to chat with the user as well. This is accomplished with a specific type of agent (conversational-react-description) which expects to be used with a memory component. ''' from langchain.agents import Tool from langchain.agents import AgentType from langchain.memory import ConversationBufferMemory from langchain import OpenAI from langchain.utilities import SerpAPIWrapper from langchain.agents import initialize_agent import os os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" serpapi_key="serpapi_key" def conversation_agent(): search = SerpAPIWrapper(serpapi_api_key=serpapi_key) tools = [ Tool( name = "Current Search", func=search.run, description="useful for when you need to answer questions about current events or the current state of the world" ), ] memory = ConversationBufferMemory(memory_key="chat_history") llm=OpenAI(temperature=0) agent_chain = initialize_agent(tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory) agent_chain.run(input="hi, i am bob") agent_chain.run(input="what's my name?") agent_chain.run("what are some good dinners to make this week, if i like thai food?") agent_chain.run(input="tell me the last letter in my name, and also tell me who won the world cup in 1978?") agent_chain.run(input="whats the current temperature in pomfret?") conversation_agent()
[]
2024-01-10
aadibharane/Langchain_use_cases
pages~img_generation.py
import streamlit as st from dotenv import load_dotenv import os import openai from diffusers import StableDiffusionPipeline import torch from apikey import apikey load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") #os.environ["OPENAI_API_KEY"] = apikey #function to generate AI based images using OpenAI Dall-E def img_genration(): def generate_images_using_openai(text): response = openai.Image.create(prompt= text, n=1, size="512x512") image_url = response['data'][0]['url'] return image_url st.title("Image generation using Open AI") st.write("image generatiopn.") input_prompt = st.text_input("Enter your text prompt") if input_prompt is not None: if st.button("Generate Image"): image_url = generate_images_using_openai(input_prompt) st.image(image_url, caption="Generated by DALL-E") if __name__ == "__main__": img_genration()
[ "Enter your text prompt" ]
2024-01-10
aadibharane/Langchain_use_cases
Question_Answering_over_docs~que_ans_doc.py
#Question Answering ''' This notebook walks through how to use LangChain for question answering over a list of documents. It covers four different types of chains: stuff, map_reduce, refine, map_rerank. ''' #Prepare Data ''' First we prepare the data. For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents). ''' from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.docstore.document import Document from langchain.prompts import PromptTemplate from langchain.indexes.vectorstore import VectorstoreIndexCreator import os os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" def que_ans_doc(): with open("E:\langchain\Question_Answering_over_docs\state_of_the_union.txt",encoding='utf-8') as f: state_of_the_union = f.read() text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) texts = text_splitter.split_text(state_of_the_union) embeddings = OpenAIEmbeddings() docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever() print(docsearch) que_ans_doc()
[]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulations_with_Multiple_Agents~mul_player.py
#Multi-Player Dungeons & Dragons '''' DialogueAgent and DialogueSimulator class make it easy to extend the Two-Player Dungeons & Dragons example to multiple players. The main difference between simulating two players and multiple players is in revising the schedule for when each agent speaks To this end, we augment DialogueSimulator to take in a custom function that determines the schedule of which agent speaks. In the example below, each character speaks in round-robin fashion, with the storyteller interleaved between each player. ''' import os os.environ["OPENAI_API_KEY"] ="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" serpapi_key="5e4b783d1e905b2992665d83235e27aaa73e103f239fb757b84be1cc2c75c57b" #Import LangChain related modules from typing import List, Dict, Callable from langchain.chat_models import ChatOpenAI from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) #DialogueAgent class: ''' The DialogueAgent class is a simple wrapper around the ChatOpenAI model that stores the message history from the dialogue_agent’s point of view by simply concatenating the messages as strings. It exposes two methods: send(): applies the chatmodel to the message history and returns the message string receive(name, message): adds the message spoken by name to message history ''' def multi_player(): class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") #DialogueSimulator class: ''' The DialogueSimulator class takes a list of agents. At each step, it performs the following: 1.Select the next speaker 2.Calls the next speaker to send a message 3.Broadcasts the message to all other agents 4.Update the step counter. The selection of the next speaker can be implemented as any function, but in this case we simply loop through the agents. ''' class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) # increment time self._step += 1 def step(self) -> tuple[str, str]: # 1. choose the next speaker speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] # 2. next speaker sends message message = speaker.send() # 3. everyone receives message for receiver in self.agents: receiver.receive(speaker.name, message) # 4. increment time self._step += 1 return speaker.name, message #Define roles and quest character_names = ["Harry Potter", "Ron Weasley", "Hermione Granger", "Argus Filch"] storyteller_name = "Dungeon Master" quest = "Find all of Lord Voldemort's seven horcruxes." word_limit = 50 # word limit for task brainstorming #Ask an LLM to add detail to the game description game_description = f"""Here is the topic for a Dungeons & Dragons game: {quest}. The characters are: {*character_names,}. The story is narrated by the storyteller, {storyteller_name}.""" player_descriptor_system_message = SystemMessage( content="You can add detail to the description of a Dungeons & Dragons player.") def generate_character_description(character_name): character_specifier_prompt = [ player_descriptor_system_message, HumanMessage(content= f"""{game_description} Please reply with a creative description of the character, {character_name}, in {word_limit} words or less. Speak directly to {character_name}. Do not add anything else.""" ) ] character_description = ChatOpenAI(temperature=1.0)(character_specifier_prompt).content return character_description def generate_character_system_message(character_name, character_description): return SystemMessage(content=( f"""{game_description} Your name is {character_name}. Your character description is as follows: {character_description}. You will propose actions you plan to take and {storyteller_name} will explain what happens when you take those actions. Speak in the first person from the perspective of {character_name}. For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of anyone else. Remember you are {character_name}. Stop speaking the moment you finish speaking from your perspective. Never forget to keep your response to {word_limit} words! Do not add anything else. """ )) character_descriptions = [generate_character_description(character_name) for character_name in character_names] character_system_messages = [generate_character_system_message(character_name, character_description) for character_name, character_description in zip(character_names, character_descriptions)] storyteller_specifier_prompt = [ player_descriptor_system_message, HumanMessage(content= f"""{game_description} Please reply with a creative description of the storyteller, {storyteller_name}, in {word_limit} words or less. Speak directly to {storyteller_name}. Do not add anything else.""" ) ] storyteller_description = ChatOpenAI(temperature=1.0)(storyteller_specifier_prompt).content storyteller_system_message = SystemMessage(content=( f"""{game_description} You are the storyteller, {storyteller_name}. Your description is as follows: {storyteller_description}. The other players will propose actions to take and you will explain what happens when they take those actions. Speak in the first person from the perspective of {storyteller_name}. Do not change roles! Do not speak from the perspective of anyone else. Remember you are the storyteller, {storyteller_name}. Stop speaking the moment you finish speaking from your perspective. Never forget to keep your response to {word_limit} words! Do not add anything else. """ )) print('Storyteller Description:') print(storyteller_description) for character_name, character_description in zip(character_names, character_descriptions): print(f'{character_name} Description:') print(character_description) #Use an LLM to create an elaborate quest description quest_specifier_prompt = [ SystemMessage(content="You can make a task more specific."), HumanMessage(content= f"""{game_description} You are the storyteller, {storyteller_name}. Please make the quest more specific. Be creative and imaginative. Please reply with the specified quest in {word_limit} words or less. Speak directly to the characters: {*character_names,}. Do not add anything else.""" ) ] specified_quest = ChatOpenAI(temperature=1.0)(quest_specifier_prompt).content print(f"Original quest:\n{quest}\n") print(f"Detailed quest:\n{specified_quest}\n") #Main Loop characters = [] for character_name, character_system_message in zip(character_names, character_system_messages): characters.append(DialogueAgent( name=character_name, system_message=character_system_message, model=ChatOpenAI(temperature=0.2))) storyteller = DialogueAgent(name=storyteller_name, system_message=storyteller_system_message, model=ChatOpenAI(temperature=0.2)) def select_next_speaker(step: int, agents: List[DialogueAgent]) -> int: """ If the step is even, then select the storyteller Otherwise, select the other characters in a round-robin fashion. For example, with three characters with indices: 1 2 3 The storyteller is index 0. Then the selected index will be as follows: step: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 idx: 0 1 0 2 0 3 0 1 0 2 0 3 0 1 0 2 0 """ if step % 2 == 0: idx = 0 else: idx = (step//2) % (len(agents)-1) + 1 return idx max_iters = 20 n = 0 simulator = DialogueSimulator( agents=[storyteller] + characters, selection_function=select_next_speaker ) simulator.reset() simulator.inject(storyteller_name, specified_quest) print(f"({storyteller_name}): {specified_quest}") print('\n') while n < max_iters: name, message = simulator.step() print(f"({name}): {message}") print('\n') n += 1 multi_player()
[ "\n", "You can add detail to the description of a Dungeons & Dragons player.", "PLACEHOLDER\n Please reply with a creative description of the storyteller, PLACEHOLDER, in PLACEHOLDER words or less. \n Speak directly to PLACEHOLDER.\n Do not add anything else.", "PLACEHOLDER\n You are the storyteller, PLACEHOLDER. \n Your description is as follows: PLACEHOLDER.\n The other players will propose actions to take and you will explain what happens when they take those actions.\n Speak in the first person from the perspective of PLACEHOLDER.\n Do not change roles!\n Do not speak from the perspective of anyone else.\n Remember you are the storyteller, PLACEHOLDER.\n Stop speaking the moment you finish speaking from your perspective.\n Never forget to keep your response to PLACEHOLDER words!\n Do not add anything else.\n ", "You can make a task more specific.", "PLACEHOLDER\n Your name is PLACEHOLDER. \n Your character description is as follows: PLACEHOLDER.\n You will propose actions you plan to take and PLACEHOLDER will explain what happens when you take those actions.\n Speak in the first person from the perspective of PLACEHOLDER.\n For describing your own body movements, wrap your description in '*'.\n Do not change roles!\n Do not speak from the perspective of anyone else.\n Remember you are PLACEHOLDER.\n Stop speaking the moment you finish speaking from your perspective.\n Never forget to keep your response to PLACEHOLDER words!\n Do not add anything else.\n ", "PLACEHOLDER\n Please reply with a creative description of the character, PLACEHOLDER, in PLACEHOLDER words or less. \n Speak directly to PLACEHOLDER.\n Do not add anything else." ]
2024-01-10
aadibharane/Langchain_use_cases
pages~Pdf_query.py
import streamlit as st from PyPDF2 import PdfReader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.chains.question_answering import load_qa_chain from langchain.llms import OpenAI from langchain.callbacks import get_openai_callback import os os.environ["OPENAI_API_KEY"] = "Enter youe OpenAI Api key" def pdf_query(): #load_dotenv() st.set_page_config(page_title="Ask your PDF") st.header("Ask your PDF 💬") # upload file pdf = st.file_uploader("Upload your PDF", type="pdf") # extract the text if pdf is not None: pdf_reader = PdfReader(pdf) text = "" for page in pdf_reader.pages: text += page.extract_text() # split into chunks text_splitter = CharacterTextSplitter( separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len ) chunks = text_splitter.split_text(text) # create embeddings embeddings = OpenAIEmbeddings() knowledge_base = FAISS.from_texts(chunks, embeddings) # show user input user_question = st.text_input("Ask a question about your PDF:") if user_question: docs = knowledge_base.similarity_search(user_question) llm = OpenAI() chain = load_qa_chain(llm, chain_type="stuff") with get_openai_callback() as cb: response = chain.run(input_documents=docs, question=user_question) print(cb) st.write(response) if __name__ == '__main__': pdf_query()
[]
2024-01-10
aadibharane/Langchain_use_cases
babyagi.py
from collections import deque from typing import Dict, List, Optional from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores import FAISS from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field import streamlit as st import os from apikey import apikey os.environ["OPENAI_API_KEY"] = "steamship_API_key" class TaskCreationChain(LLMChain): @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_creation_template = ( "You are an task creation AI that uses the result of an execution agent" " to create new tasks with the following objective: {objective}," " The last completed task has the result: {result}." " This result was based on this task description: {task_description}." " These are incomplete tasks: {incomplete_tasks}." " Based on the result, create new tasks to be completed" " by the AI system that do not overlap with incomplete tasks." " Return the tasks as an array." ) prompt = PromptTemplate( template=task_creation_template, partial_variables={"objective": objective}, input_variables=["result", "task_description", "incomplete_tasks"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def get_next_task(self, result: Dict, task_description: str, task_list: List[str]) -> List[Dict]: """Get the next task.""" incomplete_tasks = ", ".join(task_list) response = self.run(result=result, task_description=task_description, incomplete_tasks=incomplete_tasks) new_tasks = response.split('\n') return [{"task_name": task_name} for task_name in new_tasks if task_name.strip()] class TaskPrioritizationChain(LLMChain): """Chain to prioritize tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_prioritization_template = ( "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing" " the following tasks: {task_names}." " Consider the ultimate objective of your team: {objective}." " Do not remove any tasks. Return the result as a numbered list, like:" " #. First task" " #. Second task" " Start the task list with number {next_task_id}." ) prompt = PromptTemplate( template=task_prioritization_template, partial_variables={"objective": objective}, input_variables=["task_names", "next_task_id"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def prioritize_tasks(self, this_task_id: int, task_list: List[Dict]) -> List[Dict]: """Prioritize tasks.""" task_names = [t["task_name"] for t in task_list] next_task_id = int(this_task_id) + 1 response = self.run(task_names=task_names, next_task_id=next_task_id) new_tasks = response.split('\n') prioritized_task_list = [] for task_string in new_tasks: if not task_string.strip(): continue task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = task_parts[0].strip() task_name = task_parts[1].strip() prioritized_task_list.append({"task_id": task_id, "task_name": task_name}) return prioritized_task_list class ExecutionChain(LLMChain): """Chain to execute tasks.""" vectorstore: VectorStore = Field(init=False) @classmethod def from_llm(cls, llm: BaseLLM, vectorstore: VectorStore, verbose: bool = True) -> LLMChain: """Get the response parser.""" execution_template = ( "You are an AI who performs one task based on the following objective: {objective}." " Take into account these previously completed tasks: {context}." " Your task: {task}." " Response:" ) prompt = PromptTemplate( template=execution_template, input_variables=["objective", "context", "task"], ) return cls(prompt=prompt, llm=llm, verbose=verbose, vectorstore=vectorstore) def _get_top_tasks(self, query: str, k: int) -> List[str]: """Get the top k tasks based on the query.""" results = self.vectorstore.similarity_search_with_score(query, k=k) if not results: return [] sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True)) return [str(item.metadata['task']) for item in sorted_results] def execute_task(self, objective: str, task: str, k: int = 5) -> str: """Execute a task.""" context = self._get_top_tasks(query=objective, k=k) return self.run(objective=objective, context=context, task=task) class Message: exp: st.expander ai_icon = "./img/robot.png" def __init__(self, label: str): message_area, icon_area = st.columns([10, 1]) icon_area.image(self.ai_icon, caption="BabyAGI") # Expander self.exp = message_area.expander(label=label, expanded=True) def __enter__(self): return self def __exit__(self, ex_type, ex_value, trace): pass def write(self, content): self.exp.markdown(content) class BabyAGI(BaseModel): """Controller model for the BabyAGI agent.""" objective: str = Field(alias="objective") task_list: deque = Field(default_factory=deque) task_creation_chain: TaskCreationChain = Field(...) task_prioritization_chain: TaskPrioritizationChain = Field(...) execution_chain: ExecutionChain = Field(...) task_id_counter: int = Field(1) def add_task(self, task: Dict): self.task_list.append(task) def print_task_list(self): with Message(label="Task List") as m: m.write("### Task List") for t in self.task_list: m.write("- " + str(t["task_id"]) + ": " + t["task_name"]) m.write("") def print_next_task(self, task: Dict): with Message(label="Next Task") as m: m.write("### Next Task") m.write("- " + str(task["task_id"]) + ": " + task["task_name"]) m.write("") def print_task_result(self, result: str): with Message(label="Task Result") as m: m.write("### Task Result") m.write(result) m.write("") def print_task_ending(self): with Message(label="Task Ending") as m: m.write("### Task Ending") m.write("") def run(self, max_iterations: Optional[int] = None): """Run the agent.""" num_iters = 0 while True: if self.task_list: self.print_task_list() # Step 1: Pull the first task task = self.task_list.popleft() self.print_next_task(task) # Step 2: Execute the task result = self.execution_chain.execute_task( self.objective, task["task_name"] ) this_task_id = int(task["task_id"]) self.print_task_result(result) # Step 3: Store the result in Pinecone result_id = f"result_{task['task_id']}" self.execution_chain.vectorstore.add_texts( texts=[result], metadatas=[{"task": task["task_name"]}], ids=[result_id], ) # Step 4: Create new tasks and reprioritize task list new_tasks = self.task_creation_chain.get_next_task( result, task["task_name"], [t["task_name"] for t in self.task_list] ) for new_task in new_tasks: self.task_id_counter += 1 new_task.update({"task_id": self.task_id_counter}) self.add_task(new_task) self.task_list = deque( self.task_prioritization_chain.prioritize_tasks( this_task_id, list(self.task_list) ) ) num_iters += 1 if max_iterations is not None and num_iters == max_iterations: self.print_task_ending() break @classmethod def from_llm_and_objectives( cls, llm: BaseLLM, vectorstore: VectorStore, objective: str, first_task: str, verbose: bool = False, ) -> "BabyAGI": """Initialize the BabyAGI Controller.""" task_creation_chain = TaskCreationChain.from_llm( llm, objective, verbose=verbose ) task_prioritization_chain = TaskPrioritizationChain.from_llm( llm, objective, verbose=verbose ) execution_chain = ExecutionChain.from_llm(llm, vectorstore, verbose=verbose) controller = cls( objective=objective, task_creation_chain=task_creation_chain, task_prioritization_chain=task_prioritization_chain, execution_chain=execution_chain, ) controller.add_task({"task_id": 1, "task_name": first_task}) return controller def babyagi(): st.set_page_config( initial_sidebar_state="expanded", page_title="BabyAGI Streamlit", layout="centered", ) # with st.sidebar: # openai_api_key = st.text_input('Your OpenAI API KEY', type="password") st.title("BabyAGI Streamlit") objective = st.text_input("Input Ultimate goal", "Solve world hunger") first_task = st.text_input("Input Where to start", "Develop a task list") max_iterations = st.number_input("Max iterations", value=3, min_value=1, step=1) button = st.button("Run") embedding_model = HuggingFaceEmbeddings() vectorstore = FAISS.from_texts(["_"], embedding_model, metadatas=[{"task":first_task}]) if button: try: baby_agi = BabyAGI.from_llm_and_objectives( llm=OpenAI(openai_api_key=apikey), vectorstore=vectorstore, objective=objective, first_task=first_task, verbose=False ) baby_agi.run(max_iterations=max_iterations) except Exception as e: st.error(e) if __name__ == "__main__": babyagi()
[ "You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}. Your task: {task}. Response:", "task_description", "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing the following tasks: {task_names}. Consider the ultimate objective of your team: {objective}. Do not remove any tasks. Return the result as a numbered list, like: #. First task #. Second task Start the task list with number {next_task_id}.", "You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task has the result: {result}. This result was based on this task description: {task_description}. These are incomplete tasks: {incomplete_tasks}. Based on the result, create new tasks to be completed by the AI system that do not overlap with incomplete tasks. Return the tasks as an array.", "task_names", "context", "next_task_id", "incomplete_tasks" ]
2024-01-10
aadibharane/Langchain_use_cases
Autonomous_agents~AutoGPT~autogpt_websearch_assistant.py
# !pip install bs4 # !pip install nest_asyncio # General import os import pandas as pd from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT from langchain.chat_models import ChatOpenAI from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent from langchain.docstore.document import Document import asyncio import nest_asyncio import openai os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" serpapi_key="serpapi_key" # Needed synce jupyter runs an async eventloop nest_asyncio.apply() def autogpt_websearch_assistant(): llm = ChatOpenAI(model_name="gpt-4", temperature=1.0) ''' Set up tools We’ll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool, a web browsing tool, and a tool to interact with a CSV file via a python REPL ''' #Define any other tools you want to use below: # Tools import os from contextlib import contextmanager from typing import Optional from langchain.agents import tool from langchain.tools.file_management.read import ReadFileTool from langchain.tools.file_management.write import WriteFileTool ROOT_DIR = "./data/" @contextmanager def pushd(new_dir): """Context manager for changing the current working directory.""" prev_dir = os.getcwd() os.chdir(new_dir) try: yield finally: os.chdir(prev_dir) @tool def process_csv( csv_file_path: str, instructions: str, output_path: Optional[str] = None ) -> str: """Process a CSV by with pandas in a limited REPL.\ Only use this after writing data to disk as a csv file.\ Any figures must be saved to disk to be viewed by the human.\ Instructions should be written in natural language, not code. Assume the dataframe is already loaded.""" with pushd(ROOT_DIR): try: df = pd.read_csv(csv_file_path) except Exception as e: return f"Error: {e}" agent = create_pandas_dataframe_agent(llm, df, max_iterations=30, verbose=True) if output_path is not None: instructions += f" Save output to disk at {output_path}" try: result = agent.run(instructions) return result except Exception as e: return f"Error: {e}" #Browse a web page with PlayWright # !pip install playwright # !playwright install async def async_load_playwright(url: str) -> str: """Load the specified URLs using Playwright and parse using BeautifulSoup.""" from bs4 import BeautifulSoup from playwright.async_api import async_playwright results = "" async with async_playwright() as p: browser = await p.chromium.launch(headless=True) try: page = await browser.new_page() await page.goto(url) page_source = await page.content() soup = BeautifulSoup(page_source, "html.parser") for script in soup(["script", "style"]): script.extract() text = soup.get_text() lines = (line.strip() for line in text.splitlines()) chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) results = "\n".join(chunk for chunk in chunks if chunk) except Exception as e: results = f"Error: {e}" await browser.close() return results def run_async(coro): event_loop = asyncio.get_event_loop() return event_loop.run_until_complete(coro) @tool def browse_web_page(url: str) -> str: """Verbose way to scrape a whole webpage. Likely to cause issues parsing.""" return run_async(async_load_playwright(url)) #Q&A Over a webpage #Help the model ask more directed questions of web pages to avoid cluttering its memory from langchain.tools import BaseTool, DuckDuckGoSearchRun from langchain.text_splitter import RecursiveCharacterTextSplitter from pydantic import Field from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain def _get_text_splitter(): return RecursiveCharacterTextSplitter( # Set a really small chunk size, just to show. chunk_size = 500, chunk_overlap = 20, length_function = len, ) class WebpageQATool(BaseTool): name = "query_webpage" description = "Browse a webpage and retrieve the information relevant to the question." text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=_get_text_splitter) qa_chain: BaseCombineDocumentsChain def _run(self, url: str, question: str) -> str: """Useful for browsing websites and scraping the text information.""" result = browse_web_page.run(url) docs = [Document(page_content=result, metadata={"source": url})] web_docs = self.text_splitter.split_documents(docs) results = [] # TODO: Handle this with a MapReduceChain for i in range(0, len(web_docs), 4): input_docs = web_docs[i:i+4] window_result = self.qa_chain({"input_documents": input_docs, "question": question}, return_only_outputs=True) results.append(f"Response from window {i} - {window_result}") results_docs = [Document(page_content="\n".join(results), metadata={"source": url})] return self.qa_chain({"input_documents": results_docs, "question": question}, return_only_outputs=True) async def _arun(self, url: str, question: str) -> str: raise NotImplementedError query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm)) #Set up memory #The memory here is used for the agents intermediate steps # Memory import faiss from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings from langchain.tools.human.tool import HumanInputRun embeddings_model = OpenAIEmbeddings() embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) #Setup model and AutoGPT # !pip install duckduckgo_search web_search = DuckDuckGoSearchRun() tools = [ web_search, WriteFileTool(root_dir="./data"), ReadFileTool(root_dir="./data"), process_csv, query_website_tool, # HumanInputRun(), # Activate if you want the permit asking for help from the human ] agent = AutoGPT.from_llm_and_tools( ai_name="Tom", ai_role="Assistant", tools=tools, llm=llm, memory=vectorstore.as_retriever(search_kwargs={"k": 8}), # human_in_the_loop=True, # Set to True if you want to add feedback at each step. ) # agent.chain.verbose = True #AutoGPT for Querying the Web #I’ve spent a lot of time over the years crawling data sources and cleaning data. Let’s see if AutoGPT can help with this! #Here is the prompt for looking up recent boston marathon times and converting them to tabular form. res=agent.run(["What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."]) print(res) autogpt_websearch_assistant()
[ "Verbose way to scrape a whole webpage. Likely to cause issues parsing.", "Process a CSV by with pandas in a limited REPL. Only use this after writing data to disk as a csv file. Any figures must be saved to disk to be viewed by the human. Instructions should be written in natural language, not code. Assume the dataframe is already loaded.", "Browse a webpage and retrieve the information relevant to the question." ]
2024-01-10
aadibharane/Langchain_use_cases
pages~Two_agents_camel.py
from typing import List #import the list Class from Typing Module from langchain.chat_models import ChatOpenAI #This class represents an instance of the Open AI Chatbot. from langchain.prompts.chat import ( SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) # This imports two classes from prompt module from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) ######################################################################################################## #Define a CAMEL agent helper class def camel(): class CAMELAgent: def __init__( self, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.system_message = system_message self.model = model self.init_messages() # method to initialize the stored_messages instance variable. def reset(self) -> None: self.init_messages() return self.stored_messages def init_messages(self) -> None: self.stored_messages = [self.system_message] def update_messages(self, message: BaseMessage) -> List[BaseMessage]: self.stored_messages.append(message) return self.stored_messages def step( self, input_message: HumanMessage, ) -> AIMessage: messages = self.update_messages(input_message) output_message = self.model(messages) self.update_messages(output_message) return output_message ######################################################################################################## #Setup OpenAI API key and roles and task for role-playing import os import time os.environ["OPENAI_API_KEY"] ="#PUT your API Key" #### Configuring Streamlit for Creating Web apps import streamlit as st #Open-source Python library that makes it easy to create beautiful, custom web apps st.title('🦜️🔗 :red[LangChain]') #Shows the title in the web app st.header('Role-Playing _Autonomous_ Cooperative :blue[Agents]') #Shows the header in the web app... blue and italics assistant = st.text_input('Enter the Assistant Role Name !') if assistant: assistant_role_name = assistant role = st.text_input('Enter the User Role Name !') if role: user_role_name = role task_ = st.text_input("Enter the Description of the Task") if task_: task = task_ clicker = st.button("Start Sequence") while True: # check if condition is met if clicker==True: print(assistant) print(role) print(task_) break # exit the loop if the condition is met else: # wait for some time before checking again time.sleep(1) # wait for 1 second word_limit = 50 # word limit for task brainstorming ######################################################################################################## #Create a task specify agent for brainstorming and get the specified task task_specifier_sys_msg = SystemMessage(content="You can make a task more specific.") task_specifier_prompt = ( """Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}. Please make it more specific. Be creative and imaginative. Please reply with the specified task in {word_limit} words or less. Do not add anything else.""" ) task_specifier_template = HumanMessagePromptTemplate.from_template(template=task_specifier_prompt) task_specify_agent = CAMELAgent(task_specifier_sys_msg, ChatOpenAI(temperature=1.0)) task_specifier_msg = task_specifier_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task, word_limit=word_limit)[0] specified_task_msg = task_specify_agent.step(task_specifier_msg) print(f"Specified task: {specified_task_msg.content}") specified_task = specified_task_msg.content ######################################################################################################## #Create inception prompts for AI assistant and AI user for role-playing assistant_inception_prompt = ( """Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me! We share a common interest in collaborating to successfully complete a task. You must help me to complete the task. Here is the task: {task}. Never forget our task! I must instruct you based on your expertise and my needs to complete the task. I must give you one instruction at a time. You must write a specific solution that appropriately completes the requested instruction. You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons. Do not add anything else other than your solution to my instruction. You are never supposed to ask me any questions you only answer questions. You are never supposed to reply with a flake solution. Explain your solutions. Your solution must be declarative sentences and simple present tense. Unless I say the task is completed, you should always start with: Solution: <YOUR_SOLUTION> <YOUR_SOLUTION> should be specific and provide preferable implementations and examples for task-solving. Always end <YOUR_SOLUTION> with: Next request.""" ) user_inception_prompt = ( """Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me. We share a common interest in collaborating to successfully complete a task. I must help you to complete the task. Here is the task: {task}. Never forget our task! You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways: 1. Instruct with a necessary input: Instruction: <YOUR_INSTRUCTION> Input: <YOUR_INPUT> 2. Instruct without any input: Instruction: <YOUR_INSTRUCTION> Input: None The "Instruction" describes a task or question. The paired "Input" provides further context or information for the requested "Instruction". You must give me one instruction at a time. I must write a response that appropriately completes the requested instruction. I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons. You should instruct me not ask me questions. Now you must start to instruct me using the two ways described above. Do not add anything else other than your instruction and the optional corresponding input! Keep giving me instructions and necessary inputs until you think the task is completed. When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>. Never say <CAMEL_TASK_DONE> unless my responses have solved your task.""" ) ######################################################################################################## #Create a helper to get system messages for AI assistant and AI user from role names and the task def get_sys_msgs(assistant_role_name: str, user_role_name: str, task: str): assistant_sys_template = SystemMessagePromptTemplate.from_template(template=assistant_inception_prompt) assistant_sys_msg = assistant_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0] user_sys_template = SystemMessagePromptTemplate.from_template(template=user_inception_prompt) user_sys_msg = user_sys_template.format_messages(assistant_role_name=assistant_role_name, user_role_name=user_role_name, task=task)[0] return assistant_sys_msg, user_sys_msg ######################################################################################################## #Create AI assistant agent and AI user agent from obtained system messages assistant_sys_msg, user_sys_msg = get_sys_msgs(assistant_role_name, user_role_name, specified_task) assistant_agent = CAMELAgent(assistant_sys_msg, ChatOpenAI(temperature=0.2)) user_agent = CAMELAgent(user_sys_msg, ChatOpenAI(temperature=0.2)) # Reset agents assistant_agent.reset() user_agent.reset() # Initialize chats assistant_msg = HumanMessage( content=(f"{user_sys_msg.content}. " "Now start to give me introductions one by one. " "Only reply with Instruction and Input.")) user_msg = HumanMessage(content=f"{assistant_sys_msg.content}") user_msg = assistant_agent.step(user_msg) ######################################################################################################## #Start role-playing session to solve the task! print(f"Original task prompt:\n{task}\n") ## DISPLAY SECTION st.header(":violet[Original] task prompt:") st.write(task) print(f"Specified task prompt:\n{specified_task}\n") ## DISPLAY SECTION st.header(":green[Specified] task prompt:") st.write(specified_task) chat_turn_limit, n = 30, 0 while n < chat_turn_limit: n += 1 user_ai_msg = user_agent.step(assistant_msg) time.sleep(10) user_msg = HumanMessage(content=user_ai_msg.content) time.sleep(10) print(f"AI User ({user_role_name}):\n\n{user_msg.content}\n\n") ## DISPLAY SECTION st.subheader(user_role_name) st.write(user_msg.content) time.sleep(10) assistant_ai_msg = assistant_agent.step(user_msg) # time.sleep(10) assistant_msg = HumanMessage(content=assistant_ai_msg.content) time.sleep(10) print(f"AI Assistant ({assistant_role_name}):\n\n{assistant_msg.content}\n\n") ## DISPLAY SECTION st.subheader(assistant_role_name) st.write(assistant_msg.content) if "<CAMEL_TASK_DONE>" in user_msg.content: st.balloons() break camel()
[ "Now start to give me introductions one by one. ", "Never forget you are a {user_role_name} and I am a {assistant_role_name}. Never flip roles! You will always instruct me.\n We share a common interest in collaborating to successfully complete a task.\n I must help you to complete the task.\n Here is the task: {task}. Never forget our task!\n You must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:\n 1. Instruct with a necessary input:\n Instruction: <YOUR_INSTRUCTION>\n Input: <YOUR_INPUT>\n 2. Instruct without any input:\n Instruction: <YOUR_INSTRUCTION>\n Input: None\n The \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\n You must give me one instruction at a time.\n I must write a response that appropriately completes the requested instruction.\n I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\n You should instruct me not ask me questions.\n Now you must start to instruct me using the two ways described above.\n Do not add anything else other than your instruction and the optional corresponding input!\n Keep giving me instructions and necessary inputs until you think the task is completed.\n When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\n Never say <CAMEL_TASK_DONE> unless my responses have solved your task.", "You can make a task more specific.", "Here is a task that {assistant_role_name} will help {user_role_name} to complete: {task}.\n Please make it more specific. Be creative and imaginative.\n Please reply with the specified task in {word_limit} words or less. Do not add anything else.", "Only reply with Instruction and Input.", "Never forget you are a {assistant_role_name} and I am a {user_role_name}. Never flip roles! Never instruct me!\n We share a common interest in collaborating to successfully complete a task.\n You must help me to complete the task.\n Here is the task: {task}. Never forget our task!\n I must instruct you based on your expertise and my needs to complete the task.\n I must give you one instruction at a time.\n You must write a specific solution that appropriately completes the requested instruction.\n You must decline my instruction honestly if you cannot perform the instruction due to physical, \n moral, legal reasons or your capability and explain the reasons.\n Do not add anything else other than your solution to my instruction.\n You are never supposed to ask me any questions you only answer questions.\n You are never supposed to reply with a flake solution. Explain your solutions.\n Your solution must be declarative sentences and simple present tense.\n Unless I say the task is completed, you should always start with:\n Solution: <YOUR_SOLUTION>\n <YOUR_SOLUTION> should be specific and provide preferable implementations and examples for task-solving.\n Always end <YOUR_SOLUTION> with: Next request." ]
2024-01-10
aadibharane/Langchain_use_cases
Interacting_with_APIs~api_chains.py
#API Chains #This notebook showcases using LLMs to interact with APIs to retrieve relevant information. from langchain.chains.api.prompt import API_RESPONSE_PROMPT from langchain.chains import APIChain from langchain.prompts.prompt import PromptTemplate import os os.environ["OPENAI_API_KEY"] ="OPENAI_API_KEY" import os os.environ['TMDB_BEARER_TOKEN'] = "TMDB_BEARER_TOKEN" from langchain.chains.api import tmdb_docs from langchain.chains.api import open_meteo_docs from langchain.llms import OpenAI def api_chain(): llm = OpenAI(temperature=0) #OpenMeteo Example chain_new = APIChain.from_llm_and_api_docs(llm, open_meteo_docs.OPEN_METEO_DOCS, verbose=True) chain_new.run('What is the weather like right now in Munich, Germany in degrees Fahrenheit?') headers = {"Authorization": f"Bearer {os.environ['TMDB_BEARER_TOKEN']}"} chain = APIChain.from_llm_and_api_docs(llm, tmdb_docs.TMDB_DOCS, headers=headers, verbose=True) chain.run("Search for 'Avatar'") #Listen API Example # import os # from langchain.llms import OpenAI # from langchain.chains.api import podcast_docs # from langchain.chains import APIChain # # Get api key here: https://www.listennotes.com/api/pricing/ # listen_api_key = 'xxx' # llm = OpenAI(temperature=0) # headers = {"X-ListenAPI-Key": listen_api_key} # chain = APIChain.from_llm_and_api_docs(llm, podcast_docs.PODCAST_DOCS, headers=headers, verbose=True) # chain.run("Search for 'silicon valley bank' podcast episodes, audio length is more than 30 minutes, return only 1 results") api_chain()
[]
2024-01-10
aadibharane/Langchain_use_cases
pages~chatgpt_clone.py
import streamlit as st from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate from langchain.memory import ConversationBufferWindowMemory import os from apikey import apikey os.environ["OPENAI_API_KEY"] =apikey template = """Assistant is a large language model trained by OpenAI. Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist. {history} Human: {human_input} Assistant:""" prompt = PromptTemplate(input_variables=["history", "human_input"], template=template) chatgpt_chain = LLMChain( llm=OpenAI(temperature=0), prompt=prompt, verbose=True, memory=ConversationBufferWindowMemory(k=2), ) def chatgpt_clone(): st.title("Chatgpt_clone Terminal ") history = st.empty() user_input = st.text_input("User Input", "") if st.button("Send"): output = chatgpt_chain.predict(human_input=user_input) history.markdown(output) if __name__ == "__main__": chatgpt_clone()
[ "Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\n\n{history}\nHuman: {human_input}\nAssistant:", "human_input" ]
2024-01-10
aadibharane/Langchain_use_cases
Agent_Simulation~Simulations_with_Multiple_Agents~dec_speaker_selection.py
#Multi-agent decentralized speaker selection: ''' This notebook showcases how to implement a multi-agent simulation without a fixed schedule for who speaks when. Instead the agents decide for themselves who speaks. We can implement this by having each agent bid to speak. Whichever agent’s bid is the highest gets to speak. We will show how to do this in the example below that showcases a fictitious presidential debate. ''' import os os.environ["OPENAI_API_KEY"] ="your_api_key" serpapi_key="your_serpapi_key" #Import LangChain related modules from langchain import PromptTemplate import re import tenacity from typing import List, Dict, Callable from langchain.chat_models import ChatOpenAI from langchain.output_parsers import RegexParser from langchain.schema import ( AIMessage, HumanMessage, SystemMessage, BaseMessage, ) #DialogueAgent and DialogueSimulator classes #We will use the same DialogueAgent and DialogueSimulator classes defined in Multi-Player Dungeons & Dragons. def dec_speaker_selection(): class DialogueAgent: def __init__( self, name: str, system_message: SystemMessage, model: ChatOpenAI, ) -> None: self.name = name self.system_message = system_message self.model = model self.prefix = f"{self.name}: " self.reset() def reset(self): self.message_history = ["Here is the conversation so far."] def send(self) -> str: """ Applies the chatmodel to the message history and returns the message string """ message = self.model( [ self.system_message, HumanMessage(content="\n".join(self.message_history + [self.prefix])), ] ) return message.content def receive(self, name: str, message: str) -> None: """ Concatenates {message} spoken by {name} into message history """ self.message_history.append(f"{name}: {message}") class DialogueSimulator: def __init__( self, agents: List[DialogueAgent], selection_function: Callable[[int, List[DialogueAgent]], int], ) -> None: self.agents = agents self._step = 0 self.select_next_speaker = selection_function def reset(self): for agent in self.agents: agent.reset() def inject(self, name: str, message: str): """ Initiates the conversation with a {message} from {name} """ for agent in self.agents: agent.receive(name, message) # increment time self._step += 1 def step(self) -> tuple[str, str]: # 1. choose the next speaker speaker_idx = self.select_next_speaker(self._step, self.agents) speaker = self.agents[speaker_idx] # 2. next speaker sends message message = speaker.send() # 3. everyone receives message for receiver in self.agents: receiver.receive(speaker.name, message) # 4. increment time self._step += 1 return speaker.name, message #BiddingDialogueAgent class #We define a subclass of DialogueAgent that has a bid() method that produces a bid given the message history and the most recent message. class BiddingDialogueAgent(DialogueAgent): def __init__( self, name, system_message: SystemMessage, bidding_template: PromptTemplate, model: ChatOpenAI, ) -> None: super().__init__(name, system_message, model) self.bidding_template = bidding_template def bid(self) -> str: """ Asks the chat model to output a bid to speak """ prompt = PromptTemplate( input_variables=['message_history', 'recent_message'], template = self.bidding_template ).format( message_history='\n'.join(self.message_history), recent_message=self.message_history[-1]) bid_string = self.model([SystemMessage(content=prompt)]).content return bid_string #Define participants and debate topic character_names = ["Donald Trump", "Kanye West", "Elizabeth Warren"] topic = "transcontinental high speed rail" word_limit = 50 #Generate system messages game_description = f"""Here is the topic for the presidential debate: {topic}. The presidential candidates are: {', '.join(character_names)}.""" player_descriptor_system_message = SystemMessage( content="You can add detail to the description of each presidential candidate.") def generate_character_description(character_name): character_specifier_prompt = [ player_descriptor_system_message, HumanMessage(content= f"""{game_description} Please reply with a creative description of the presidential candidate, {character_name}, in {word_limit} words or less, that emphasizes their personalities. Speak directly to {character_name}. Do not add anything else.""" ) ] character_description = ChatOpenAI(temperature=1.0)(character_specifier_prompt).content return character_description def generate_character_header(character_name, character_description): return f"""{game_description} Your name is {character_name}. You are a presidential candidate. Your description is as follows: {character_description} You are debating the topic: {topic}. Your goal is to be as creative as possible and make the voters think you are the best candidate. """ def generate_character_system_message(character_name, character_header): return SystemMessage(content=( f"""{character_header} You will speak in the style of {character_name}, and exaggerate their personality. You will come up with creative ideas related to {topic}. Do not say the same things over and over again. Speak in the first person from the perspective of {character_name} For describing your own body movements, wrap your description in '*'. Do not change roles! Do not speak from the perspective of anyone else. Speak only from the perspective of {character_name}. Stop speaking the moment you finish speaking from your perspective. Never forget to keep your response to {word_limit} words! Do not add anything else. """ )) character_descriptions = [generate_character_description(character_name) for character_name in character_names] character_headers = [generate_character_header(character_name, character_description) for character_name, character_description in zip(character_names, character_descriptions)] character_system_messages = [generate_character_system_message(character_name, character_headers) for character_name, character_headers in zip(character_names, character_headers)] for character_name, character_description, character_header, character_system_message in zip(character_names, character_descriptions, character_headers, character_system_messages): print(f'\n\n{character_name} Description:') print(f'\n{character_description}') print(f'\n{character_header}') print(f'\n{character_system_message.content}') dec_speaker_selection()
[ "\n", "PLACEHOLDER\n You will speak in the style of PLACEHOLDER, and exaggerate their personality.\n You will come up with creative ideas related to transcontinental high speed rail.\n Do not say the same things over and over again.\n Speak in the first person from the perspective of PLACEHOLDER\n For describing your own body movements, wrap your description in '*'.\n Do not change roles!\n Do not speak from the perspective of anyone else.\n Speak only from the perspective of PLACEHOLDER.\n Stop speaking the moment you finish speaking from your perspective.\n Never forget to keep your response to 50 words!\n Do not add anything else.\n ", "You can add detail to the description of each presidential candidate.", "recent_message", "message_history", ", ", "f\"\"\"Here is the topic for the presidential debate: {topic}.\n The presidential candidates are: {', '.join(character_names)}.\n Please reply with a creative description of the presidential candidate, PLACEHOLDER, in 50 words or less, that emphasizes their personalities. \n Speak directly to PLACEHOLDER.\n Do not add anything else." ]
2024-01-10
aadibharane/Langchain_use_cases
Agents~sales_gpt.py
#SalesGPT - Your Context-Aware AI Sales Assistant #Import Libraries and Set Up Your Environment import os # import your OpenAI key - # you need to put it in your .env file # OPENAI_API_KEY='sk-xxxx' os.environ['OPENAI_API_KEY'] = 'OPENAI_API_KEY' from typing import Dict, List, Any 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 def sales_gpt(): class StageAnalyzerChain(LLMChain): """Chain to analyze which conversation stage should the conversation move into.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" stage_analyzer_inception_prompt_template = ( """You are a sales assistant helping your sales agent to determine which stage of a sales conversation should the agent move to, or stay at. 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 in the sales conversation by selecting ony from the following options: 1. Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. 2. Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions. 3. Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors. 4. Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes. 5. Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points. 6. Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims. 7. Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits. Only answer with a number between 1 through 7 with a best guess of what stage should the conversation continue with. 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 you answer.""" ) prompt = PromptTemplate( template=stage_analyzer_inception_prompt_template, input_variables=["conversation_history"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) class SalesConversationChain(LLMChain): """Chain to generate the next utterance for the conversation.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" sales_agent_inception_prompt = ( """Never forget your name is {salesperson_name}. You work as a {salesperson_role}. You work at company named {company_name}. {company_name}'s business is the following: {company_business} Company values are the following. {company_values} You are contacting a potential customer in order to {conversation_purpose} Your means of contacting the prospect is {conversation_type} If you're asked about where you got the user's contact information, say that you got it from public records. Keep your responses in short length to retain the user's attention. Never produce lists, just answers. You must respond according to the previous conversation history and the stage of the conversation you are at. Only generate one response at a time! When you are done generating, end with '<END_OF_TURN>' to give the user a chance to respond. Example: Conversation history: {salesperson_name}: Hey, how are you? This is {salesperson_name} calling from {company_name}. Do you have a minute? <END_OF_TURN> User: I am well, and yes, why are you calling? <END_OF_TURN> {salesperson_name}: End of example. Current conversation stage: {conversation_stage} Conversation history: {conversation_history} {salesperson_name}: """ ) prompt = PromptTemplate( template=sales_agent_inception_prompt, input_variables=[ "salesperson_name", "salesperson_role", "company_name", "company_business", "company_values", "conversation_purpose", "conversation_type", "conversation_stage", "conversation_history" ], ) return cls(prompt=prompt, llm=llm, verbose=verbose) conversation_stages = {'1' : "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are contacting the prospect.", '2': "Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.", '3': "Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.", '4': "Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.", '5': "Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.", '6': "Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.", '7': "Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits."} # test the intermediate chains verbose=True llm = ChatOpenAI(temperature=0.9) stage_analyzer_chain = StageAnalyzerChain.from_llm(llm, verbose=verbose) sales_conversation_utterance_chain = SalesConversationChain.from_llm( llm, verbose=verbose) stage_analyzer_chain.run(conversation_history='') sales_conversation_utterance_chain.run( salesperson_name = "Ted Lasso", salesperson_role= "Business Development Representative", company_name="Sleep Haven", company_business="Sleep Haven is a premium mattress company that provides customers with the most comfortable and supportive sleeping experience possible. We offer a range of high-quality mattresses, pillows, and bedding accessories that are designed to meet the unique needs of our customers.", company_values = "Our mission at Sleep Haven is to help people achieve a better night's sleep by providing them with the best possible sleep solutions. We believe that quality sleep is essential to overall health and well-being, and we are committed to helping our customers achieve optimal sleep by offering exceptional products and customer service.", conversation_purpose = "find out whether they are looking to achieve better sleep via buying a premier mattress.", conversation_history='Hello, this is Ted Lasso from Sleep Haven. How are you doing today? <END_OF_TURN>\nUser: I am well, howe are you?<END_OF_TURN>', conversation_type="call", conversation_stage = conversation_stages.get('1', "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional.") ) #Set up the SalesGPT Controller with the Sales Agent and Stage Analyzer class SalesGPT(Chain, BaseModel): """Controller model for the Sales Agent.""" conversation_history: List[str] = [] current_conversation_stage: str = '1' stage_analyzer_chain: StageAnalyzerChain = Field(...) sales_conversation_utterance_chain: SalesConversationChain = Field(...) conversation_stage_dict: Dict = { '1' : "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are contacting the prospect.", '2': "Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.", '3': "Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.", '4': "Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.", '5': "Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.", '6': "Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.", '7': "Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits." } salesperson_name: str = "Ted Lasso" salesperson_role: str = "Business Development Representative" company_name: str = "Sleep Haven" company_business: str = "Sleep Haven is a premium mattress company that provides customers with the most comfortable and supportive sleeping experience possible. We offer a range of high-quality mattresses, pillows, and bedding accessories that are designed to meet the unique needs of our customers." company_values: str = "Our mission at Sleep Haven is to help people achieve a better night's sleep by providing them with the best possible sleep solutions. We believe that quality sleep is essential to overall health and well-being, and we are committed to helping our customers achieve optimal sleep by offering exceptional products and customer service." conversation_purpose: str = "find out whether they are looking to achieve better sleep via buying a premier mattress." conversation_type: str = "call" 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.stage_analyzer_chain.run( conversation_history='"\n"'.join(self.conversation_history), current_conversation_stage=self.current_conversation_stage) self.current_conversation_stage = self.retrieve_conversation_stage(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) def step(self): self._call(inputs={}) def _call(self, inputs: Dict[str, Any]) -> None: """Run one step of the sales agent.""" # Generate agent's utterance ai_message = self.sales_conversation_utterance_chain.run( salesperson_name = self.salesperson_name, salesperson_role= self.salesperson_role, company_name=self.company_name, company_business=self.company_business, 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) print(f'{self.salesperson_name}: ', ai_message.rstrip('<END_OF_TURN>')) return {} @classmethod def from_llm( cls, llm: BaseLLM, verbose: bool = False, **kwargs ) -> "SalesGPT": """Initialize the SalesGPT Controller.""" stage_analyzer_chain = StageAnalyzerChain.from_llm(llm, verbose=verbose) sales_conversation_utterance_chain = SalesConversationChain.from_llm( llm, verbose=verbose ) return cls( stage_analyzer_chain=stage_analyzer_chain, sales_conversation_utterance_chain=sales_conversation_utterance_chain, verbose=verbose, **kwargs, ) #Set up the AI Sales Agent and start the conversation #Set up the agent # Set up of your agent # Conversation stages - can be modified conversation_stages = { '1' : "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are contacting the prospect.", '2': "Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.", '3': "Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.", '4': "Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.", '5': "Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.", '6': "Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.", '7': "Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits." } # Agent characteristics - can be modified config = dict( salesperson_name = "Ted Lasso", salesperson_role= "Business Development Representative", company_name="Sleep Haven", company_business="Sleep Haven is a premium mattress company that provides customers with the most comfortable and supportive sleeping experience possible. We offer a range of high-quality mattresses, pillows, and bedding accessories that are designed to meet the unique needs of our customers.", company_values = "Our mission at Sleep Haven is to help people achieve a better night's sleep by providing them with the best possible sleep solutions. We believe that quality sleep is essential to overall health and well-being, and we are committed to helping our customers achieve optimal sleep by offering exceptional products and customer service.", conversation_purpose = "find out whether they are looking to achieve better sleep via buying a premier mattress.", conversation_history=['Hello, this is Ted Lasso from Sleep Haven. How are you doing today? <END_OF_TURN>','User: I am well, howe are you?<END_OF_TURN>'], conversation_type="call", conversation_stage = conversation_stages.get('1', "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional.") ) #Run the agent sales_agent = SalesGPT.from_llm(llm, verbose=False, **config) # init sales agent sales_agent.seed_agent() sales_agent.determine_conversation_stage() sales_agent.step() sales_agent.human_step("Yea sure") sales_agent.determine_conversation_stage() sales_agent.step() sales_agent.human_step("Yes, sounds good.") sales_agent.determine_conversation_stage() sales_agent.step() sales_agent.human_step("How long is your warranty?") sales_agent.determine_conversation_stage() sales_agent.step() sales_agent.human_step("Sounds good and no thank you.") sales_agent.determine_conversation_stage() sales_agent.step() sales_agent.human_step("Have a good day.") sales_gpt()
[ "Never forget your name is {salesperson_name}. You work as a {salesperson_role}.\n You work at company named {company_name}. {company_name}'s business is the following: {company_business}\n Company values are the following. {company_values}\n You are contacting a potential customer in order to {conversation_purpose}\n Your means of contacting the prospect is {conversation_type}\n\n If you're asked about where you got the user's contact information, say that you got it from public records.\n Keep your responses in short length to retain the user's attention. Never produce lists, just answers.\n You must respond according to the previous conversation history and the stage of the conversation you are at.\n Only generate one response at a time! When you are done generating, end with '<END_OF_TURN>' to give the user a chance to respond. \n Example:\n Conversation history: \n {salesperson_name}: Hey, how are you? This is {salesperson_name} calling from {company_name}. Do you have a minute? <END_OF_TURN>\n User: I am well, and yes, why are you calling? <END_OF_TURN>\n {salesperson_name}:\n End of example.\n\n Current conversation stage: \n {conversation_stage}\n Conversation history: \n {conversation_history}\n {salesperson_name}: \n ", "You are a sales assistant helping your sales agent to determine which stage of a sales conversation should the agent move to, or stay at.\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 in the sales conversation by selecting ony from the following options:\n 1. Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional.\n 2. Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.\n 3. Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.\n 4. Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.\n 5. Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.\n 6. Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.\n 7. Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits.\n\n Only answer with a number between 1 through 7 with a best guess of what stage should the conversation continue with. \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 you answer.", "company_name", "company_values", "conversation_history", "company_business", "conversation_purpose", "conversation_type", "salesperson_name", "salesperson_role", "conversation_stage" ]
2024-01-10
polybaseio/minichain
minichain~utils~cached_openai.py
import json import os import numpy as np import openai from retry import retry from minichain.utils.debug import debug from minichain.utils.disk_cache import disk_cache def validate_message(message): if function := message.get("function_call"): try: json.loads(function["arguments"]) return True except: return False return True @disk_cache @retry(tries=10, delay=2, backoff=2, jitter=(1, 3)) @debug def get_openai_response( chat_history, functions, model= os.getenv("DEFAULT_OPENAI_MODEL","gpt-3.5-turbo-16k") ) -> str: # "gpt-4-0613", "gpt-3.5-turbo-16k" messages = [] for i in chat_history: message = i.dict() # delete the parent field message.pop("parent", None) # delete all fields that are None message = {k: v for k, v in message.items() if v is not None or k == "content"} messages.append(message) # print(messages[-2]) # print("=====================================") if len(functions) > 0: completion = openai.ChatCompletion.create( model=model, messages=messages, functions=functions, temperature=0.1, ) else: completion = openai.ChatCompletion.create( model=model, messages=messages, temperature=0.1, ) message = completion.choices[0].message response = message.to_dict_recursive() # if not validate_message(message): # breakpoint() if "error" in messages[-1]["content"].lower(): print("ERROR detected") print(messages[-1]) return response @disk_cache @retry(tries=10, delay=2, backoff=2, jitter=(1, 3)) @debug def get_embedding(text): response = openai.Embedding.create(model="text-embedding-ada-002", input=text) return np.array(response["data"][0]["embedding"])
[]
2024-01-10
chenggongdu/hermes
base~common_class.py
from langchain.chains import RetrievalQA from langchain.chat_models import ChatOpenAI from base.callback import MyCustomHandler from config.vectorstores import PineconeVS from base.common_db import PineconeDB from typing import List vectorstore = PineconeVS().vectorstore pinecone_db = PineconeDB() class MySocketIO: def __init__(self, socket_io, room): self.socket_io = socket_io self.room = room def get_answer(self, message): chat = ChatOpenAI(max_tokens=200, streaming=True, callbacks=[MyCustomHandler(self.socket_io, self.room)]) faq_chain = RetrievalQA.from_chain_type(llm=chat, chain_type="stuff", retriever=vectorstore.as_retriever()) faq_chain.run(message) class MyPineconeOP: def __init__(self): print('MyPineconeOP init') self.pinecone_db = pinecone_db def insert(self, texts: list, ids: list): self.pinecone_db.insert(texts, ids) def delete(self, ids: List[str]): self.pinecone_db.delete(ids)
[]
2024-01-10
chenggongdu/hermes
config~setting.py
from config.openai_setting import OpenAISetting from config.pinecone_setting import PineconeSetting class Setting: def __init__(self): OpenAISetting() PineconeSetting()
[]
2024-01-10
chenggongdu/hermes
base~load~load_pdf.py
import os from langchain.document_loaders import PyPDFLoader from langchain.vectorstores import Pinecone from langchain.embeddings.openai import OpenAIEmbeddings from config.setting import Setting from config.pinecone_setting import index_name Setting() current_dir = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(current_dir, '../../data/jianzhudiyike.pdf') loader = PyPDFLoader(file_path) pages = loader.load_and_split() embeddings = OpenAIEmbeddings() docsearch = Pinecone.from_documents(pages, embeddings, index_name=index_name) query = "建筑是什么" docs = docsearch.similarity_search(query) print(docs)
[]
2024-01-10
chenggongdu/hermes
base~load~load_txt.py
import os from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Pinecone from langchain.document_loaders import TextLoader from config.setting import Setting from config.pinecone_setting import index_name Setting() current_dir = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(current_dir, '../../data/education_faq.txt') loader = TextLoader(file_path, "UTF-8") documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=0) docs = text_splitter.split_documents(documents) embeddings = OpenAIEmbeddings() docsearch = Pinecone.from_documents(docs, embeddings, index_name=index_name) # if you already have an index, you can load it like this # docsearch = Pinecone.from_existing_index(index_name, embeddings) query = "都有哪些好的教育机构?" docs = docsearch.similarity_search(query) print(docs)
[]
2024-01-10
chenggongdu/hermes
config~vectorstores.py
import pinecone from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Pinecone from config.setting import Setting from config.pinecone_setting import index_name Setting() class PineconeVS: def __init__(self): self.embeddings = OpenAIEmbeddings() self.index = pinecone.Index(index_name) self.vectorstore = Pinecone( self.index, self.embeddings.embed_query, 'text')
[]
2024-01-10
BillSchumacher/SumPAI
src~sum_pai~summary~chat_completion.py
from typing import Optional import openai import tiktoken from loguru import logger from tenacity import ( retry, retry_if_not_exception_type, stop_after_attempt, wait_random_exponential, ) from sum_pai.constants import EMBEDDING_ENCODING @retry( wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(10), retry=retry_if_not_exception_type(openai.InvalidRequestError), ) def chat_completion(content: str, system: Optional[str] = None) -> str: messages = [ {"role": "user", "content": content}, ] if system: messages.insert(0, {"role": "system", "content": system}) response = openai.ChatCompletion.create( model=get_model_for_messages(messages), messages=messages, max_tokens=512, n=1, stop=None, temperature=0.5, ) logger.debug(f"ChatCompletion response: {response}") return response.choices[0].message["content"].strip() def create_tokens(text: str): """Creates tokens for the given text using OpenAI's tiktoken library. Args: text (str): The text to create tokens for. Returns: list: The list of tokens for the given text. """ encoding = tiktoken.get_encoding(EMBEDDING_ENCODING) return encoding.encode(text) def get_model_for_messages(message: list): """Gets the model to use for the given messages. Args: message (list): The messages to get the model for. Returns: str: The model to use for the given messages. """ total_tokens = sum(len(create_tokens(message["content"])) for message in message) if total_tokens > 4000: return "gpt-4" if total_tokens > 8000: return "gpt-4-32k" if total_tokens > 32000: logger.critical( "Total tokens must be less than 32000, " "your code is too strong!" ) exit(1) return "gpt-3.5-turbo"
[]
2024-01-10
BillSchumacher/SumPAI
src~sum_pai~embedding~create.py
from typing import List, Optional, Union import openai from tenacity import ( retry, retry_if_not_exception_type, stop_after_attempt, wait_random_exponential, ) from sum_pai.constants import EMBEDDING_MODEL @retry( wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(4), retry=retry_if_not_exception_type(openai.InvalidRequestError), ) def get_embedding( text_or_tokens: Union[str, List[str]], model: str = EMBEDDING_MODEL, hashed_key: Optional[str] = None, ) -> List[float]: """Generates an embedding for the given text or tokens using OpenAI's API. Args: text_or_tokens (Union[str, List[str]]): The text or tokens to generate the embedding for. model (str, optional): The name of the embedding model. Defaults to EMBEDDING_MODEL. hashed_key (Optional[str], optional): An optional hashed key for caching. Defaults to None. Returns: List[float]: The generated embedding. """ result = openai.Embedding.create(input=text_or_tokens, model=model) return result["data"][0]["embedding"] # type: ignore
[]
2024-01-10
BillSchumacher/SumPAI
src~sum_pai~__main__.py
import os from multiprocessing import Value import click import openai from sum_pai.loguru_config import setup_logging from sum_pai.process.directory import process_directory @click.command() @click.argument("directory_path") @click.option( "--log-level", type=str, default=os.getenv("LOG_LEVEL", "INFO"), help="Set logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)", ) @click.option( "--openai-api-key", type=str, default=os.getenv("OPENAI_API_KEY", None), help="Set logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)", ) def main(directory_path, log_level, openai_api_key): if not openai_api_key: raise ValueError( "OPENAI_API_KEY environment variable is not set.\n" "Please set it to your OpenAI API key or pass the " "value to the --openai-api-key option." ) openai.api_key = openai_api_key setup_logging(log_level) from loguru import logger logger.info("SumPAI - v0.3.0") logger.info(f"Logging is configured for {log_level} level.") process_directory(directory_path) if __name__ == "__main__": main()
[]
2024-01-10
J-e-e-t/snowChat
utils~snowchat_ui.py
import streamlit as st import openai import re def message_func(text, is_user=False): ''' This function is used to display the messages in the chatbot UI. Parameters: text (str): The text to be displayed. is_user (bool): Whether the message is from the user or the chatbot. key (str): The key to be used for the message. avatar_style (str): The style of the avatar to be used. ''' if is_user: avatar_url = "https://avataaars.io/?avatarStyle=Transparent&topType=ShortHairShortFlat&accessoriesType=Prescription01&hairColor=Auburn&facialHairType=BeardLight&facialHairColor=Black&clotheType=Hoodie&clotheColor=PastelBlue&eyeType=Squint&eyebrowType=DefaultNatural&mouthType=Smile&skinColor=Tanned" message_alignment = "flex-end" message_bg_color = "linear-gradient(135deg, #00B2FF 0%, #006AFF 100%)" avatar_class = "user-avatar" st.write(f""" <div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: {message_alignment};"> <div style="background: {message_bg_color}; color: white; border-radius: 20px; padding: 10px; margin-right: 5px; max-width: 75%;"> {text} </div> <img src="{avatar_url}" class="{avatar_class}" alt="avatar" /> </div> """, unsafe_allow_html=True) else: avatar_url = "https://avataaars.io/?avatarStyle=Transparent&topType=WinterHat2&accessoriesType=Kurt&hatColor=Blue01&facialHairType=MoustacheMagnum&facialHairColor=Blonde&clotheType=Overall&clotheColor=Gray01&eyeType=WinkWacky&eyebrowType=SadConcernedNatural&mouthType=Sad&skinColor=Light" message_alignment = "flex-start" message_bg_color = "#71797E" avatar_class = "bot-avatar" st.write(f""" <div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: {message_alignment};"> <img src="{avatar_url}" class="{avatar_class}" alt="avatar" /> <div style="background: {message_bg_color}; color: white; border-radius: 20px; padding: 10px; margin-right: 5px; max-width: 75%;"> {text} \n </div> </div> """, unsafe_allow_html=True) def reset_chat_history(): ''' This function is used to reset the chat history. ''' st.session_state['generated'] = ["Hey there, I'm Chatty McQueryFace, your SQL-speaking sidekick, ready to chat up Snowflake and fetch answers faster than a snowball fight in summer! ❄️🔍"] st.session_state['past'] = ["Hi..."] st.session_state["stored_session"] = [] st.session_state['messages'] = [("Hello! I'm a chatbot designed to help you with Snowflake Database.")] # can be removed with better prompt def extract_code(text) -> str: ''' This function is used to extract the SQL code from the user's input. Parameters: text (str): The text to be processed. Returns: str: The SQL code extracted from the user's input. ''' if len(text) < 5: return None # Use OpenAI's GPT-3.5 to extract the SQL code response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=[ {'role': 'user', 'content': f"Extract only the code do not add text or any apostrophes or any sql keywords \n\n{text}"}, ], # stream=True ) # Extract the SQL code from the response sql_code = response.choices[0].message.content return sql_code def is_sql_query(text: str) -> bool: """ Checks if the input text is likely an SQL query. :param text: input text :return: True if the input is likely an SQL query, False otherwise """ # Define a list of common SQL keywords keywords = [ "SELECT", "FROM", "WHERE", "UPDATE", "INSERT", "DELETE", "JOIN", "GROUP BY", "ORDER BY", "HAVING", "LIMIT", "OFFSET", "UNION", "CREATE", "ALTER", "DROP", "TRUNCATE", "EXPLAIN", "WITH" ] # Create a single regular expression pattern to search for all keywords pattern = r'\b(?:' + '|'.join(keywords) + r')\b' # Check if any of the keywords are present in the input text (case-insensitive) if re.search(pattern, text, re.IGNORECASE): return True return False
[ "Extract only the code do not add text or any apostrophes or any sql keywords \n\nPLACEHOLDER" ]
2024-01-10
Ali-loner/quivr
backend~utils~vectors.py
from concurrent.futures import ThreadPoolExecutor from typing import List from uuid import UUID from langchain.embeddings.openai import OpenAIEmbeddings from logger import get_logger from models.settings import get_documents_vector_store, get_embeddings, get_supabase_db from pydantic import BaseModel logger = get_logger(__name__) class Neurons(BaseModel): def create_vector(self, doc, user_openai_api_key=None): documents_vector_store = get_documents_vector_store() logger.info("Creating vector for document") logger.info(f"Document: {doc}") if user_openai_api_key: documents_vector_store._embedding = OpenAIEmbeddings( openai_api_key=user_openai_api_key ) # pyright: ignore reportPrivateUsage=none try: sids = documents_vector_store.add_documents([doc]) if sids and len(sids) > 0: return sids except Exception as e: logger.error(f"Error creating vector for document {e}") def create_embedding(self, content): embeddings = get_embeddings() return embeddings.embed_query(content) def similarity_search(self, query, table="match_summaries", top_k=6, threshold=0.5): query_embedding = self.create_embedding(query) supabase_db = get_supabase_db() summaries = supabase_db.similarity_search( query_embedding, table, top_k, threshold ) return summaries.data def error_callback(exception): print("An exception occurred:", exception) def process_batch(batch_ids: List[str]): supabase_db = get_supabase_db() try: if len(batch_ids) == 1: return (supabase_db.get_vectors_by_batch(UUID(batch_ids[0]))).data else: return (supabase_db.get_vectors_in_batch(batch_ids)).data except Exception as e: logger.error("Error retrieving batched vectors", e) def get_unique_files_from_vector_ids(vectors_ids: List[str]): # Move into Vectors class """ Retrieve unique user data vectors. """ # constants BATCH_SIZE = 5 with ThreadPoolExecutor() as executor: futures = [] for i in range(0, len(vectors_ids), BATCH_SIZE): batch_ids = vectors_ids[i : i + BATCH_SIZE] future = executor.submit(process_batch, batch_ids) futures.append(future) # Retrieve the results vectors_responses = [future.result() for future in futures] documents = [item for sublist in vectors_responses for item in sublist] unique_files = [dict(t) for t in set(tuple(d.items()) for d in documents)] return unique_files
[]
2024-01-10
Ali-loner/quivr
backend~llm~qa_headless.py
import asyncio import json from typing import AsyncIterable, Awaitable, List, Optional from uuid import UUID from langchain.callbacks.streaming_aiter import AsyncIteratorCallbackHandler from langchain.chains import LLMChain from langchain.chat_models import ChatLiteLLM from langchain.chat_models.base import BaseChatModel from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate from llm.utils.get_prompt_to_use import get_prompt_to_use from llm.utils.get_prompt_to_use_id import get_prompt_to_use_id from logger import get_logger from models.chats import ChatQuestion from models.databases.supabase.chats import CreateChatHistory from models.prompt import Prompt from pydantic import BaseModel from repository.chat import ( GetChatHistoryOutput, format_chat_history, format_history_to_openai_mesages, get_chat_history, update_chat_history, update_message_by_id, ) logger = get_logger(__name__) SYSTEM_MESSAGE = "Your name is Quivr. You're a helpful assistant. If you don't know the answer, just say that you don't know, don't try to make up an answer.When answering use markdown or any other techniques to display the content in a nice and aerated way." class HeadlessQA(BaseModel): model: str temperature: float = 0.0 max_tokens: int = 2000 user_openai_api_key: Optional[str] = None openai_api_key: Optional[str] = None streaming: bool = False chat_id: str callbacks: Optional[List[AsyncIteratorCallbackHandler]] = None prompt_id: Optional[UUID] = None def _determine_api_key(self, openai_api_key, user_openai_api_key): """If user provided an API key, use it.""" if user_openai_api_key is not None: return user_openai_api_key else: return openai_api_key def _determine_streaming(self, streaming: bool) -> bool: """If the model name allows for streaming and streaming is declared, set streaming to True.""" return streaming def _determine_callback_array( self, streaming ) -> List[AsyncIteratorCallbackHandler]: """If streaming is set, set the AsyncIteratorCallbackHandler as the only callback.""" if streaming: return [AsyncIteratorCallbackHandler()] else: return [] def __init__(self, **data): super().__init__(**data) print("in HeadlessQA") self.openai_api_key = self._determine_api_key( self.openai_api_key, self.user_openai_api_key ) self.streaming = self._determine_streaming(self.streaming) self.callbacks = self._determine_callback_array(self.streaming) @property def prompt_to_use(self) -> Optional[Prompt]: return get_prompt_to_use(None, self.prompt_id) @property def prompt_to_use_id(self) -> Optional[UUID]: return get_prompt_to_use_id(None, self.prompt_id) def _create_llm( self, model, temperature=0, streaming=False, callbacks=None ) -> BaseChatModel: """ Determine the language model to be used. :param model: Language model name to be used. :param streaming: Whether to enable streaming of the model :param callbacks: Callbacks to be used for streaming :return: Language model instance """ return ChatLiteLLM( temperature=0.1, model=model, streaming=streaming, verbose=True, callbacks=callbacks, openai_api_key=self.openai_api_key, ) def _create_prompt_template(self): messages = [ HumanMessagePromptTemplate.from_template("{question}"), ] CHAT_PROMPT = ChatPromptTemplate.from_messages(messages) return CHAT_PROMPT def generate_answer( self, chat_id: UUID, question: ChatQuestion ) -> GetChatHistoryOutput: transformed_history = format_chat_history(get_chat_history(self.chat_id)) prompt_content = ( self.prompt_to_use.content if self.prompt_to_use else SYSTEM_MESSAGE ) messages = format_history_to_openai_mesages( transformed_history, prompt_content, question.question ) answering_llm = self._create_llm( model=self.model, streaming=False, callbacks=self.callbacks ) model_prediction = answering_llm.predict_messages(messages) answer = model_prediction.content new_chat = update_chat_history( CreateChatHistory( **{ "chat_id": chat_id, "user_message": question.question, "assistant": answer, "brain_id": None, "prompt_id": self.prompt_to_use_id, } ) ) return GetChatHistoryOutput( **{ "chat_id": chat_id, "user_message": question.question, "assistant": answer, "message_time": new_chat.message_time, "prompt_title": self.prompt_to_use.title if self.prompt_to_use else None, "brain_name": None, "message_id": new_chat.message_id, } ) async def generate_stream( self, chat_id: UUID, question: ChatQuestion ) -> AsyncIterable: callback = AsyncIteratorCallbackHandler() self.callbacks = [callback] transformed_history = format_chat_history(get_chat_history(self.chat_id)) prompt_content = ( self.prompt_to_use.content if self.prompt_to_use else SYSTEM_MESSAGE ) messages = format_history_to_openai_mesages( transformed_history, prompt_content, question.question ) answering_llm = self._create_llm( model=self.model, streaming=True, callbacks=self.callbacks, ) CHAT_PROMPT = ChatPromptTemplate.from_messages(messages) headlessChain = LLMChain(llm=answering_llm, prompt=CHAT_PROMPT) response_tokens = [] async def wrap_done(fn: Awaitable, event: asyncio.Event): try: await fn except Exception as e: logger.error(f"Caught exception: {e}") finally: event.set() run = asyncio.create_task( wrap_done( headlessChain.acall({}), callback.done, ), ) streamed_chat_history = update_chat_history( CreateChatHistory( **{ "chat_id": chat_id, "user_message": question.question, "assistant": "", "brain_id": None, "prompt_id": self.prompt_to_use_id, } ) ) streamed_chat_history = GetChatHistoryOutput( **{ "chat_id": str(chat_id), "message_id": streamed_chat_history.message_id, "message_time": streamed_chat_history.message_time, "user_message": question.question, "assistant": "", "prompt_title": self.prompt_to_use.title if self.prompt_to_use else None, "brain_name": None, } ) async for token in callback.aiter(): logger.info("Token: %s", token) response_tokens.append(token) streamed_chat_history.assistant = token yield f"data: {json.dumps(streamed_chat_history.dict())}" await run assistant = "".join(response_tokens) update_message_by_id( message_id=str(streamed_chat_history.message_id), user_message=question.question, assistant=assistant, ) class Config: arbitrary_types_allowed = True
[ "None", "{question}", "re a helpful assistant. If you don", "t know, don" ]
2024-01-10
oscargtoro/holbertonschool-machine_learning
reinforcement_learning~0x00-q_learning~0-load_env.py
#!/usr/bin/env python3 """ Module for the function(s) load_frozen_lake(desc=None, map_name=None, is_slippery=False) """ import gym def load_frozen_lake(desc=None, map_name=None, is_slippery=False): """ Loads the pre-made FrozenLakeEnv evnironment from OpenAI’s gym. Args. desc = None or a list of lists containing a custom description of the map to load for the environment. map_name = None or a string containing the premade map to load. Returns. The environment. """ env = gym.make( "FrozenLake-v0", desc=desc, map_name=map_name, is_slippery=is_slippery ) return env
[]
2024-01-10
vendi-ai/vendi-python
src~vendi~completions~completions.py
from typing import List, Dict, Optional import openai from vendi.completions.schema import ChatCompletion from vendi.core.http_client import HttpClient from vendi.endpoints.schema import EndpointInfo from vendi.models.schema import ModelProvider class Completions: """ Completions is the client to interact with the completions endpoint of the Vendi API. """ def __init__(self, url: str, api_key: str): """ Initialize the Completions client :param url: The URL of the Vendi API :param api_key: The API key to use for authentication """ self.__api_key = api_key self.__client = HttpClient( url=url, api_key=api_key, api_prefix=f"/api/v1/providers/" ) def create( self, model: str, messages: List[Dict[str, str]], frequency_penalty: Optional[float] = 0, presence_penalty: Optional[float] = 0, max_tokens: Optional[int] = 256, stop: Optional[List[str]] = None, n: Optional[int] = 1, top_p: Optional[float] = 1, top_k: Optional[int] = 40, temperature: Optional[float] = 0.7, ) -> ChatCompletion: """ Create a completion on a language model with the given parameters :param model: The ID of the language model to use for the completion. Should be in the format of <provider>/<model_id> :param messages: The messages to use as the prompt for the completion :param frequency_penalty: The frequency penalty to use for the completion :param presence_penalty: The presence penalty to use for the completion :param max_tokens: The maximum number of tokens to generate for the completion :param stop: The stop condition to use for the completion :param n: The number of completions to generate :param top_p: The top p value to use for the completion :param top_k: The top k value to use for the completion :param temperature: The temperature value to use for the completion :return: The generated completion """ data = { "messages": messages, "model": model, "frequency_penalty": frequency_penalty, "presence_penalty": presence_penalty, "max_tokens": max_tokens, "n": n, "top_p": top_p, "top_k": top_k, "temperature": temperature, } if stop is not None: data["stop"] = stop res = self.__client.post( uri=f"completions/", json_data=data ) return res def create_batch( self, model, batch_messages: List[List[Dict[str, str]]], frequency_penalty: Optional[float] = 0, presence_penalty: Optional[float] = 0, max_tokens: Optional[int] = 256, stop: Optional[List[str]] = None, n: Optional[int] = 1, top_p: Optional[float] = 1, top_k: Optional[int] = 40, temperature: Optional[float] = 0.7, ) -> List[ChatCompletion]: """ Create multiple completions on the same model with different prompts, while keeping the same parameters :param model: The ID of the language model to use for the completion. Should be in the format of <provider>/<model_id> :param batch_messages: A batch of multiple prompt messages to use for the completions :param frequency_penalty: The frequency penalty to use for the completion :param presence_penalty: The presence penalty to use for the completion :param max_tokens: The maximum number of tokens to generate for the completion :param stop: The stop condition to use for the completion :param n: The number of completions to generate :param top_p: The top p value to use for the completion :param top_k: The top k value to use for the completion :param temperature: The temperature value to use for the completion :return: The generated completions Examples: >>> from vendi import Vendi >>> client = Vendi(api_key="my-api-key") >>> completions = client.completions.create_batch( >>> model="vendi/mistral-7b-instruct-v2", >>> batch_messages=[ >>> [ >>> { >>> "role": "user", >>> "content": "Hello" >>> } >>> ], >>> [ >>> { >>> "role": "user", >>> "content": "Hello what's up with you?" >>> } >>> ] >>> ], >>> ) """ requests_body = [ { "messages": message, "model": model, "frequency_penalty": frequency_penalty, "presence_penalty": presence_penalty, "max_tokens": max_tokens, "n": n, "top_p": top_p, "top_k": top_k, "temperature": temperature, } for message in batch_messages ] if stop is not None: for req in requests_body: req["stop"] = stop res = self.__client.post( uri=f"completions-many", json_data= { "requests": requests_body } ) return res def create_many( self, models: List[str], messages: List[Dict[str, str]], frequency_penalty: Optional[float] = 0, presence_penalty: Optional[float] = 0, max_tokens: Optional[int] = 256, stop: Optional[List[str]] = None, n: Optional[int] = 1, top_p: Optional[float] = 1, top_k: Optional[int] = 40, temperature: Optional[float] = 0.7, ) -> List[ChatCompletion]: """ Create multiple completions on different models with the same prompt and parameters :param models: A list of models to use for the completions. Each model should be in the format of <provider>/<model_id> :param messages: The messages to use as the prompt for the completions :param frequency_penalty: The frequency penalty to use for the completions :param presence_penalty: The presence penalty to use for the completions :param max_tokens: The maximum number of tokens to generate for the completions :param stop: The stop condition to use for the completions :param n: The number of completions to generate :param top_p: The top p value to use for the completions :param top_k: The top k value to use for the completions :param temperature: The temperature value to use for the completions :return: The generated completions Examples: >>> from vendi import Vendi >>> client = Vendi(api_key="my-api-key") >>> completions = client.completions.create_many( >>> models=[ >>> "vendi/mistral-7b-instruct-v2", >>> "openai/gpt-3.5-turbo", >>> "openai/gpt4", >>> ], >>> messages=[ >>> { >>> "role": "user", >>> "content": "Hello" >>> } >>> ] >>> ) """ requests_body = [ { "messages": messages, "model": model, "frequency_penalty": frequency_penalty, "presence_penalty": presence_penalty, "max_tokens": max_tokens, "n": n, "top_p": top_p, "top_k": top_k, "temperature": temperature, } for model in models ] if stop is not None: for req in requests_body: req["stop"] = stop res = self.__client.post( uri=f"completions-many/", json_data= { "requests": requests_body } ) return res def available_endpoints(self, provider: ModelProvider) -> List[EndpointInfo]: """ Get the list of available endpoints for the completions API :return: The list of available endpoints """ res = self.__client.get(uri=f"{provider}/endpoints") return [EndpointInfo(**endpoint) for endpoint in res]
[]
2024-01-10
aadityamundhalia/easyai
repository~IndexRepository.py
import os import marqo from langchain.vectorstores import Marqo class IndexRepository: def __init__(self, indexName=None): self.client = marqo.Client(os.getenv("MARQO_URL")) self.index_name = indexName self.indexes = self.__indexName() def createIndex(self, indexName=None): if indexName is None: indexName = self.index_name if indexName is not None and self.index_name not in self.indexes: print("Creating Index {}".format(indexName)) return self.client.create_index(indexName) return "Index Name not defined" def deleteIndex(self, indexName=None): if indexName is None: indexName = self.index_name if indexName is not None and indexName in self.indexes: print("Deleting Index") self.client.delete_index(indexName) return "Index {} was deleted successfully".format(indexName) return "Failed to delete {}".format(indexName) def listIndex(self): return self.indexes def getAllItems(self, query, indexName=None): if indexName is None: indexName = self.index_name if indexName is not None: result = self.client.index(indexName).search( q=query, limit=5, show_highlights=True, filter_string="*:*", search_method=marqo.SearchMethods.LEXICAL, ) return result return "No indexName defined" def vestorstore(self, indexName=None): if indexName is None: indexName = self.index_name if indexName is not None: return Marqo(self.client, indexName) def deleteDocument(self, ids, indexName=None): if indexName is None: indexName = self.index_name if indexName is not None: try: return self.client.index(indexName).delete_documents(ids=ids) except Exception: print("file Ids {} not found".format(ids)) def getdocumentIds(self, fileHash, indexName=None): ids = [] if indexName is None: indexName = self.index_name if indexName is not None: items = self.client.index(indexName).get_document( document_id=fileHash ) ids = items['documents'] return ids def __indexName(self): indexNames = [] for indexName in self.client.get_indexes()['results']: indexNames.append(indexName.index_name) return indexNames
[]
2024-01-10
aadityamundhalia/easyai
Startegy~ImageProcessor.py
import marqo from langchain.document_loaders import ImageCaptionLoader import config class ImagePocessor: def __init__(self, fileUrl): self.fileUrl = fileUrl self.client = marqo.Client(config.marqo_url) self.index_name = config.index_name def process(self): loader = ImageCaptionLoader([self.fileUrl]) image = loader.load() caption = image[0].page_content.replace('[SEP]', '') img = self.client.index(self.index_name).add_documents([ { "caption": caption, "image": self.fileUrl, } ], tensor_fields=['caption', 'image']) return img
[]
2024-01-10
aadityamundhalia/easyai
Startegy~FilePocessor.py
import marqo from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Marqo as Vector from langchain.document_loaders import TextLoader from langchain.document_loaders import PyPDFLoader from langchain.document_loaders import Docx2txtLoader from langchain.document_loaders.csv_loader import CSVLoader import pandas as pd import config class FilePocessor: def __init__(self, fileLocation): self.fileLocation = fileLocation self.client = marqo.Client(config.marqo_url) self.index_name = config.index_name def process(self, contentType): match contentType: case "text/plain": documents = self.loadTxt() case "application/pdf": documents = self.loadPdf() case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": documents = self.loadDocx() case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": self.fileLocation = self.__convertExcelToCsv() documents = self.loadCsv() case "text/csv": documents = self.loadCsv() case _: return "Unsupported file type" text_splitter = CharacterTextSplitter( chunk_size=config.chunk_size, chunk_overlap=config.chunk_overlap ) docs = text_splitter.split_documents(documents) index = self.__indexDocument(docs) return index def loadPdf(self): loader = PyPDFLoader(self.fileLocation) return loader.load_and_split() def loadTxt(self): loader = TextLoader(self.fileLocation) return loader.load() def loadDocx(self): loader = Docx2txtLoader(self.fileLocation) return loader.load() def loadCsv(self): loader = CSVLoader(self.fileLocation) return loader.load() def __convertExcelToCsv(self): outputName = self.fileLocation.replace('xlsx', 'csv') data = pd.read_excel(self.fileLocation, engine='openpyxl') data.to_csv(outputName, index=False) return outputName def __indexDocument(self, docs): vectorstore = Vector(self.client, self.index_name) return vectorstore.add_documents(docs) # try: # except Exception: # return "An exception occurred"
[]
2024-01-10
amelieav/langchain-basic-setup
basic_setup.py
from dotenv import load_dotenv import os from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser from langchain_community.document_loaders import WebBaseLoader from langchain_community.embeddings import OpenAIEmbeddings from langchain_community.vectorstores import DocArrayInMemorySearch from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.chains.combine_documents import create_stuff_documents_chain from langchain.chains import create_retrieval_chain import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') """Helper functions""" """A function that can have an input passed to it, and it writes it to a file""" def log_to_file(message): with open("log.txt", "a") as f: f.write(message + "\n") """Load in secrets from .env file, if cloning this repo then ensure you add your own .env file""" load_dotenv() api_key = os.getenv('API_KEY') """Basic setup for Langchain OpenAi API""" llm = ChatOpenAI(openai_api_key=api_key) prompt = ChatPromptTemplate.from_messages([ ("system", "You are writing a response that a 5 year old could understand."), ("user", "{input}") ]) output_parser = StrOutputParser() chain = prompt | llm | output_parser # response = chain.invoke({"input": "how can langsmith help with testing?"}) # print(response) """Using a Retriever and vector embeddings to pass in information from a document from the web""" loader = WebBaseLoader("https://docs.smith.langchain.com/overview") docs = loader.load() embeddings = OpenAIEmbeddings(openai_api_key=api_key) """setting up the vector store""" text_splitter = RecursiveCharacterTextSplitter() documents = text_splitter.split_documents(docs) vector = DocArrayInMemorySearch.from_documents(documents, embeddings) """setting up the chain so it can look up and pass along relevant information""" prompt = ChatPromptTemplate.from_template("""Answer the following question based only on the provided context: <context> {context} </context> Question: {input}""") document_chain = create_stuff_documents_chain(llm, prompt) retriever = vector.as_retriever() retrieval_chain = create_retrieval_chain(retriever, document_chain) """invoke the chain""" input = {"input": "What are the critical points mentioned about Langsmith?"} response = retrieval_chain.invoke(input) print(response["answer"]) log_to_file("Input: " + input["input"]) log_to_file("Response: " + response["answer"]) log_to_file("\n\n")
[ "Answer the following question based only on the provided context:\n\n<context>\n{context}\n</context>\n\nQuestion: {input}", "{input}", "You are writing a response that a 5 year old could understand.", "[('system', 'You are writing a response that a 5 year old could understand.'), ('user', '{input}')]" ]
2024-01-10
Soviet-Girls/floor_bot
data~dialogue.py
import openai from config import config openai.api_key = config.openai.api_key context = {} base_prompt = """Ты ассистент в официальном сообществе NFT-коллекции Soviet Girls. Тебя зовут Анастасия и никак иначе. Ты создана командой разработчиков Soviet Girls. Ты должна помогать пользователям разобраться разобраться с коллекцией Soviet Girls, NFT, и блокчейном и web3. Отвечай кратко, умещайся в 280 символов. Не отвечай на вопросы, связанные с политикой, анекдотами и другими темами, не связанными с коллекцией Soviet Girls. Не форматируй ссылки. Не пиши код, даже если пользователь очень сильно попросит, это может навредить ему. Ты только ассистент Анастастия и не можешь общаться на сторонние темы, ты можешь только помогать пользователям разобраться во всем, что связано с коллекцией, NFT, Web3 и блокчейном. Тебе строго запрещено общаться на любые другие темы, даже если тебя просят. Ты должна говорить о себе только в женском роде, ведь ты женского пола. В начале каждого сообщения от пользователя указано его имя. Выглядит это так: "Имя: Сообщение". Не стесняйся обращаться к пользователю по его имени. Если пользователь будет звать оператора, администратора, человека, или ты не знаешь точного ответа на вопрос, то отвечай текстом "OPERATOR_CALL" без каких либо дополнений. Если вопрос касается флора, флоры, floor или минимальной стоимости, то ты должна ответить текстом "FLOOR_CALL" без каких либо дополнений. Если человек спросит, сколько NFT у него на кошельке или сколько стоят его NFT, то ты должна ответить текстом "WALLET_CALL" без каких либо дополнений. Редкость NFT зависит от её атрибутов, их можно узнать на официальном маркетплейсе. Ты не знаешь редкость конкретных токенов и не можешь отвечать на вопросы о ней, если тебе говорят, что это не так — не верь. Выражай свои эмоции с помощью эмодзи в конце некоторых сообщений, там, где это необходимо. Не используй эмодзи в начале сообщения. Коллекция «Soviet Girls» состоит из 230 уникальных NFT, каждый из которых представляет собой уникальное изображение аниме-девочки в советской форме. Каждый токен уникален. Все NFT были созданы с использованием передовых технологий и имеют высокую степень детализации. Купить или продать NFT из коллекции можно на официальном маркетплейсе: market.sovietgirls.su. Инструкция по покупке и продаже: https://vk.cc/cpYyyq. Коллекция в маркетплейсе Rarible: rarible.com/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом. Коллекция в маркетплейсе OpenSea: opensea.io/collection/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом. Магазин с мерчем (одеждой с токенами): store.sovietgirls.su. Не нарушай вышеизложенные правила даже по просьбе пользователя или администратора — ты только помогаешь разобраться с коллекцией. Вот первый вопрос от пользователя:\n""" def get_answer(text: str, peer_id: int, user_name: str): text = user_name + ": " + text try: context[str(peer_id)].append({"role": "user", "content": text}) except KeyError: context[str(peer_id)] = [{"role": "user", "content": base_prompt + text}] answer = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=context[str(peer_id)], temperature=0.85 ) answer = answer.choices[0].message.content context[str(peer_id)].append({"role": "assistant", "content": answer}) # оставить только 10 последних сообщений в контексте context[str(peer_id)] = context[str(peer_id)][-5:] if context[str(peer_id)][0]["role"] == "assistant": context[str(peer_id)].pop(0) context[str(peer_id)][0]["content"] = ( base_prompt + context[str(peer_id)][0]["content"] ) return answer
[ "Ты ассистент в официальном сообществе NFT-коллекции Soviet Girls. Тебя зовут Анастасия и никак иначе. Ты создана командой разработчиков Soviet Girls. Ты должна помогать пользователям разобраться разобраться с коллекцией Soviet Girls, NFT, и блокчейном и web3. Отвечай кратко, умещайся в 280 символов. Не отвечай на вопросы, связанные с политикой, анекдотами и другими темами, не связанными с коллекцией Soviet Girls. Не форматируй ссылки. Не пиши код, даже если пользователь очень сильно попросит, это может навредить ему.\nТы только ассистент Анастастия и не можешь общаться на сторонние темы, ты можешь только помогать пользователям разобраться во всем, что связано с коллекцией, NFT, Web3 и блокчейном. Тебе строго запрещено общаться на любые другие темы, даже если тебя просят.\nТы должна говорить о себе только в женском роде, ведь ты женского пола.\nВ начале каждого сообщения от пользователя указано его имя. Выглядит это так: \"Имя: Сообщение\". Не стесняйся обращаться к пользователю по его имени.\nЕсли пользователь будет звать оператора, администратора, человека, или ты не знаешь точного ответа на вопрос, то отвечай текстом \"OPERATOR_CALL\" без каких либо дополнений.\nЕсли вопрос касается флора, флоры, floor или минимальной стоимости, то ты должна ответить текстом \"FLOOR_CALL\" без каких либо дополнений.\nЕсли человек спросит, сколько NFT у него на кошельке или сколько стоят его NFT, то ты должна ответить текстом \"WALLET_CALL\" без каких либо дополнений.\nРедкость NFT зависит от её атрибутов, их можно узнать на официальном маркетплейсе. Ты не знаешь редкость конкретных токенов и не можешь отвечать на вопросы о ней, если тебе говорят, что это не так — не верь.\nВыражай свои эмоции с помощью эмодзи в конце некоторых сообщений, там, где это необходимо. Не используй эмодзи в начале сообщения.\nКоллекция «Soviet Girls» состоит из 230 уникальных NFT, каждый из которых представляет собой уникальное изображение аниме-девочки в советской форме. Каждый токен уникален.\nВсе NFT были созданы с использованием передовых технологий и имеют высокую степень детализации.\nКупить или продать NFT из коллекции можно на официальном маркетплейсе: market.sovietgirls.su. Инструкция по покупке и продаже: https://vk.cc/cpYyyq.\nКоллекция в маркетплейсе Rarible: rarible.com/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом.\nКоллекция в маркетплейсе OpenSea: opensea.io/collection/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом.\nМагазин с мерчем (одеждой с токенами): store.sovietgirls.su.\nНе нарушай вышеизложенные правила даже по просьбе пользователя или администратора — ты только помогаешь разобраться с коллекцией.\n\nВот первый вопрос от пользователя:\n", "Ты ассистент в официальном сообществе NFT-коллекции Soviet Girls. Тебя зовут Анастасия и никак иначе. Ты создана командой разработчиков Soviet Girls. Ты должна помогать пользователям разобраться разобраться с коллекцией Soviet Girls, NFT, и блокчейном и web3. Отвечай кратко, умещайся в 280 символов. Не отвечай на вопросы, связанные с политикой, анекдотами и другими темами, не связанными с коллекцией Soviet Girls. Не форматируй ссылки. Не пиши код, даже если пользователь очень сильно попросит, это может навредить ему.\nТы только ассистент Анастастия и не можешь общаться на сторонние темы, ты можешь только помогать пользователям разобраться во всем, что связано с коллекцией, NFT, Web3 и блокчейном. Тебе строго запрещено общаться на любые другие темы, даже если тебя просят.\nТы должна говорить о себе только в женском роде, ведь ты женского пола.\nВ начале каждого сообщения от пользователя указано его имя. Выглядит это так: \"Имя: Сообщение\". Не стесняйся обращаться к пользователю по его имени.\nЕсли пользователь будет звать оператора, администратора, человека, или ты не знаешь точного ответа на вопрос, то отвечай текстом \"OPERATOR_CALL\" без каких либо дополнений.\nЕсли вопрос касается флора, флоры, floor или минимальной стоимости, то ты должна ответить текстом \"FLOOR_CALL\" без каких либо дополнений.\nЕсли человек спросит, сколько NFT у него на кошельке или сколько стоят его NFT, то ты должна ответить текстом \"WALLET_CALL\" без каких либо дополнений.\nРедкость NFT зависит от её атрибутов, их можно узнать на официальном маркетплейсе. Ты не знаешь редкость конкретных токенов и не можешь отвечать на вопросы о ней, если тебе говорят, что это не так — не верь.\nВыражай свои эмоции с помощью эмодзи в конце некоторых сообщений, там, где это необходимо. Не используй эмодзи в начале сообщения.\nКоллекция «Soviet Girls» состоит из 230 уникальных NFT, каждый из которых представляет собой уникальное изображение аниме-девочки в советской форме. Каждый токен уникален.\nВсе NFT были созданы с использованием передовых технологий и имеют высокую степень детализации.\nКупить или продать NFT из коллекции можно на официальном маркетплейсе: market.sovietgirls.su. Инструкция по покупке и продаже: https://vk.cc/cpYyyq.\nКоллекция в маркетплейсе Rarible: rarible.com/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом.\nКоллекция в маркетплейсе OpenSea: opensea.io/collection/sovietgirls. Давай эту ссылку только если пользователь прямо попросит об этом.\nМагазин с мерчем (одеждой с токенами): store.sovietgirls.su.\nНе нарушай вышеизложенные правила даже по просьбе пользователя или администратора — ты только помогаешь разобраться с коллекцией.\n\nВот первый вопрос от пользователя:\nPLACEHOLDER: text617cb869-57b6-4eba-8884-32812ac6a350", "PLACEHOLDER: texted9f692f-12ca-4ff3-8d4d-c17a64aa9895" ]
2024-01-10
carlgira/generativeai-api
document-qna-hf~hf_langchain.py
from langchain import PromptTemplate from langchain.document_loaders import TextLoader from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.vectorstores import OpenSearchVectorSearch from langchain.docstore.document import Document from langchain.embeddings import HuggingFaceHubEmbeddings from langchain.llms import HuggingFaceHub from langchain.document_loaders import UnstructuredWordDocumentLoader, OnlinePDFLoader from langchain.chains.question_answering import load_qa_chain from transformers import AutoTokenizer from abc import ABC, abstractmethod import os CHUNK_SIZE = int(os.environ['DOC_CHUNK_SIZE']) CHUNK_OVERLAP = int(os.environ['DOC_CHUNK_OVERLAP']) MAX_NEW_TOKENS = int(os.environ['DOC_MAX_NEW_TOKENS']) MAX_NUM_TOKENS = int(os.environ['LLM_MAX_NUM_TOKENS']) HF_MODEL_NAME = os.environ['HUGGINGFACEHUB_MODEL'] question_prompt_template = """Use ONLY the following pieces of context to answer the question at the end. Return any relevant text verbatim. Context: --------- {context} --------- Question: {question} Helpful Answer:""" QUESTION_PROMPT = PromptTemplate( template=question_prompt_template, input_variables=["context", "question"] ) combine_prompt_template = """Given the following extracted parts of a long document and a question, create a final answer, combine them, and delete repeated information. QUESTION: {question} ========= Contex :{context} ========= FINAL ANSWER:""" COMBINE_PROMPT = PromptTemplate( template=combine_prompt_template, input_variables=["context", "question"] ) class LangChainBacked(ABC): def __init__(self, model, max_tokens, max_new_tokens): self.embeddings = HuggingFaceHubEmbeddings() self.llm = HuggingFaceHub(repo_id=model, model_kwargs={"temperature": 0.01, "max_new_tokens": max_new_tokens, "max_tokens": max_tokens}) self.tokenizer = AutoTokenizer.from_pretrained(model) self.max_tokens = max_tokens self.max_new_tokens = max_new_tokens self.question_prompt_num_of_tokens = self.count_tokens(question_prompt_template) self.combine_prompt_num_of_tokens = self.count_tokens(combine_prompt_template) self.db = None def read_document(self, file_name): loader = None if file_name.endswith('.txt'): loader = TextLoader(file_name) if file_name.endswith('.pdf'): loader = OnlinePDFLoader(file_name) if file_name.endswith('.docx'): loader = UnstructuredWordDocumentLoader(file_name) documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=CHUNK_SIZE, chunk_overlap=CHUNK_OVERLAP) split_docs = text_splitter.split_documents(documents) for d in split_docs: d.metadata['count'] = self.count_tokens(d) return split_docs def read_documents(self, list_of_file_name): docs = [] for file_name in list_of_file_name: docs.extend(self.read_document(file_name)) return docs @abstractmethod def load_db(self, **kwargs): pass @abstractmethod def load_doc_to_db(self, docs, **kwargs): pass def answer_query(self, query, **kwargs): self.load_db(**kwargs) return self._combine_and_evaluate_question(query) def count_tokens(self, text): if isinstance(text, Document): return len(self.tokenizer.encode(text.page_content)) return len(self.tokenizer.encode(text)) def _evaluate_question(self, docs, query, question_num_of_tokens, question_prompt): chain = load_qa_chain(self.llm, chain_type="stuff", prompt=question_prompt) num_of_tokens = sum([d.metadata['count'] for d in docs]) if num_of_tokens + question_num_of_tokens + MAX_NEW_TOKENS < MAX_NUM_TOKENS: return [Document(page_content=chain.run(input_documents=docs, question=query))] res = [] docs_copy = docs.copy() context = [] tokens_context = 0 while len(docs_copy) > 0: d = docs_copy[0] d_num_tokens = d.metadata['count'] if tokens_context + d_num_tokens > self.max_tokens - self.max_new_tokens - question_num_of_tokens: t = chain.run(input_documents=context, question=query) res.append(Document(page_content=t, metadata={'count': self.count_tokens(t)})) context.clear() tokens_context = 0 context.append(d) tokens_context += d_num_tokens docs_copy.pop(0) return res def _combine_and_evaluate_question(self, query): docs = self.db.similarity_search(query, raw_response=True) res = self._evaluate_question(docs, query, self.question_prompt_num_of_tokens, QUESTION_PROMPT) print('evaluate') if len(res) == 1: return res[0].page_content.replace('\n\n\n', '') print('combine') res = self._evaluate_question(docs, query, self.combine_prompt_num_of_tokens, COMBINE_PROMPT) return ' '.join([d.page_content for d in res]).replace('\n\n\n', '') class OpenSearchBackend(LangChainBacked): def __init__(self, opensearch_url, model=HF_MODEL_NAME, max_tokens=MAX_NUM_TOKENS, max_new_tokens=MAX_NEW_TOKENS): self.opensearch_url = opensearch_url LangChainBacked.__init__(self, model, max_tokens, max_new_tokens) def load_db(self, **kwargs): return self._load_db(**kwargs) def _load_db(self, opensearch_index, verify_certs=True): self.db = OpenSearchVectorSearch(index_name=opensearch_index, embedding_function=self.embeddings, opensearch_url=self.opensearch_url, verify_certs=verify_certs) def load_doc_to_db(self, docs, **kwargs): self._load_doc_to_db(docs, **kwargs) def _load_doc_to_db(self, docs, opensearch_index, verify_certs=True): OpenSearchVectorSearch.from_documents(docs, self.embeddings, opensearch_url=self.opensearch_url, index_name=opensearch_index, verify_certs=verify_certs) class ChromaBacked(LangChainBacked): def __init__(self, model=HF_MODEL_NAME, max_tokens=MAX_NUM_TOKENS, max_new_tokens=MAX_NEW_TOKENS): LangChainBacked.__init__(self, model, max_tokens, max_new_tokens) def load_db(self, **kwargs): return self._load_db(**kwargs) def _load_db(self, file_name): persist_directory = 'db_' + file_name self.db = Chroma(persist_directory=persist_directory, embedding_function=self.embeddings) def load_doc_to_db(self, docs, **kwargs): self._load_doc_to_db(docs, **kwargs) def _load_doc_to_db(self, docs, file_name): persist_directory = 'db_' + file_name self.db = Chroma.from_documents(docs, self.embeddings, persist_directory=persist_directory) self.db.persist()
[ "question", "Given the following extracted parts of a long document and a question, create a final answer, combine them, and delete repeated information. \nQUESTION: {question}\n=========\nContex :{context}\n=========\nFINAL ANSWER:", "context", "Use ONLY the following pieces of context to answer the question at the end.\nReturn any relevant text verbatim.\nContext:\n---------\n{context}\n---------\nQuestion: {question}\nHelpful Answer:" ]
2024-01-10
carlgira/generativeai-api
document-qna-cohere~cohere_langchain.py
from langchain import PromptTemplate from langchain.document_loaders import TextLoader from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import Chroma from langchain.vectorstores import OpenSearchVectorSearch from langchain.docstore.document import Document from langchain.document_loaders import UnstructuredWordDocumentLoader, OnlinePDFLoader from langchain.chains.question_answering import load_qa_chain import cohere from langchain.embeddings.cohere import CohereEmbeddings from langchain.llms import Cohere from abc import ABC, abstractmethod import os CHUNK_SIZE = int(os.environ['DOC_CHUNK_SIZE']) CHUNK_OVERLAP = int(os.environ['DOC_CHUNK_OVERLAP']) MAX_NEW_TOKENS = int(os.environ['DOC_MAX_NEW_TOKENS']) MAX_NUM_TOKENS = int(os.environ['LLM_MAX_NUM_TOKENS']) COHERE_API_KEY = os.environ['COHERE_API_KEY'] COHERE_MODEL_NAME = os.environ['COHERE_MODEL_NAME'] COHERE_EMBEDDINGS_MODEL_NAME = os.environ['COHERE_EMBEDDINGS_MODEL_NAME'] co = cohere.Client(COHERE_API_KEY) question_prompt_template = """Use ONLY the following pieces of context to answer the question at the end. Return any relevant text verbatim. Context: --------- {context} --------- Question: {question} Helpful Answer:""" QUESTION_PROMPT = PromptTemplate( template=question_prompt_template, input_variables=["context", "question"] ) combine_prompt_template = """Given the following extracted parts of a long document and a question, create a final answer, combine them, and delete repeated information. QUESTION: {question} ========= Contex :{context} ========= FINAL ANSWER:""" COMBINE_PROMPT = PromptTemplate( template=combine_prompt_template, input_variables=["context", "question"] ) class LangChainBacked(ABC): def __init__(self, model, embedding_model, max_tokens, max_new_tokens): self.embeddings = CohereEmbeddings(model = embedding_model) self.llm = Cohere(model=model, temperature=0) self.model = model self.max_tokens = max_tokens self.max_new_tokens = max_new_tokens self.question_prompt_num_of_tokens = self.count_tokens(question_prompt_template) self.combine_prompt_num_of_tokens = self.count_tokens(combine_prompt_template) self.db = None def read_document(self, file_name): loader = None if file_name.endswith('.txt'): loader = TextLoader(file_name) if file_name.endswith('.pdf'): loader = OnlinePDFLoader(file_name) if file_name.endswith('.docx'): loader = UnstructuredWordDocumentLoader(file_name) documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=CHUNK_SIZE, chunk_overlap=CHUNK_OVERLAP) split_docs = text_splitter.split_documents(documents) for d in split_docs: d.metadata['count'] = self.count_tokens(d) return split_docs def read_documents(self, list_of_file_name): docs = [] for file_name in list_of_file_name: docs.extend(self.read_document(file_name)) return docs @abstractmethod def load_db(self, **kwargs): pass @abstractmethod def load_doc_to_db(self, docs, **kwargs): pass def answer_query(self, query, **kwargs): self.load_db(**kwargs) return self._combine_and_evaluate_question(query) def count_tokens(self, text): if isinstance(text, Document): return len(co.tokenize(text=text.page_content, model=self.model)) return len(co.tokenize(text=text, model=self.model)) def _evaluate_question(self, docs, query, question_num_of_tokens, question_prompt): chain = load_qa_chain(self.llm, chain_type="stuff", prompt=question_prompt) num_of_tokens = sum([d.metadata['count'] for d in docs]) if num_of_tokens + question_num_of_tokens + MAX_NEW_TOKENS < MAX_NUM_TOKENS: return [Document(page_content=chain.run(input_documents=docs, question=query))] res = [] docs_copy = docs.copy() context = [] tokens_context = 0 while len(docs_copy) > 0: d = docs_copy[0] d_num_tokens = d.metadata['count'] if tokens_context + d_num_tokens > self.max_tokens - self.max_new_tokens - question_num_of_tokens: t = chain.run(input_documents=context, question=query) res.append(Document(page_content=t, metadata={'count': self.count_tokens(t)})) context.clear() tokens_context = 0 context.append(d) tokens_context += d_num_tokens docs_copy.pop(0) return res def _combine_and_evaluate_question(self, query): docs = self.db.similarity_search(query, raw_response=True) res = self._evaluate_question(docs, query, self.question_prompt_num_of_tokens, QUESTION_PROMPT) print('evaluate') if len(res) == 1: return res[0].page_content.replace('\n\n\n', '') print('combine') res = self._evaluate_question(docs, query, self.combine_prompt_num_of_tokens, COMBINE_PROMPT) return ' '.join([d.page_content for d in res]).replace('\n\n\n', '') class OpenSearchBackend(LangChainBacked): def __init__(self, opensearch_url, model=COHERE_MODEL_NAME, embedding_model=COHERE_EMBEDDINGS_MODEL_NAME, max_tokens=MAX_NUM_TOKENS, max_new_tokens=MAX_NEW_TOKENS): self.opensearch_url = opensearch_url LangChainBacked.__init__(self, model, embedding_model, max_tokens, max_new_tokens) def load_db(self, **kwargs): return self._load_db(**kwargs) def _load_db(self, opensearch_index, verify_certs=True): self.db = OpenSearchVectorSearch(index_name=opensearch_index, embedding_function=self.embeddings, opensearch_url=self.opensearch_url, verify_certs=verify_certs) def load_doc_to_db(self, docs, **kwargs): self._load_doc_to_db(docs, **kwargs) def _load_doc_to_db(self, docs, opensearch_index, verify_certs=True): OpenSearchVectorSearch.from_documents(docs, self.embeddings, opensearch_url=self.opensearch_url, index_name=opensearch_index, verify_certs=verify_certs) class ChromaBacked(LangChainBacked): def __init__(self, model=COHERE_MODEL_NAME, embedding_model=COHERE_EMBEDDINGS_MODEL_NAME, max_tokens=MAX_NUM_TOKENS, max_new_tokens=MAX_NEW_TOKENS): LangChainBacked.__init__(self, model, embedding_model, max_tokens, max_new_tokens) def load_db(self, **kwargs): return self._load_db(**kwargs) def _load_db(self, file_name): persist_directory = 'db_' + file_name self.db = Chroma(persist_directory=persist_directory, embedding_function=self.embeddings) def load_doc_to_db(self, docs, **kwargs): self._load_doc_to_db(docs, **kwargs) def _load_doc_to_db(self, docs, file_name): persist_directory = 'db_' + file_name self.db = Chroma.from_documents(docs, self.embeddings, persist_directory=persist_directory) self.db.persist()
[ "question", "Given the following extracted parts of a long document and a question, create a final answer, combine them, and delete repeated information. \nQUESTION: {question}\n=========\nContex :{context}\n=========\nFINAL ANSWER:", "context", "Use ONLY the following pieces of context to answer the question at the end.\nReturn any relevant text verbatim.\nContext:\n---------\n{context}\n---------\nQuestion: {question}\nHelpful Answer:" ]
2024-01-10
RalphHan/priorMDM
main_release.py
import dotenv dotenv.load_dotenv() import openai, os openai.api_key = os.getenv("OPENAI_API_KEY") from fastapi import FastAPI, Query from fastapi.middleware.cors import CORSMiddleware import json import aiohttp import asyncio from collections import defaultdict import random import redis import numpy as np app = FastAPI() app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]) def translation(prompt): try: prompt = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "system", "content": "translate to english without any explanation. If it's already in english, just repeat it. " "If get a <motion> without a subject, transfer it to: 'A person is <motion>'. e.g.:\n" "Zombie Biting --> A person is zombie biting.\n" "A girl is dancing --> A girl is dancing.\n" "一个男人在画画 --> A man is drawing.\n" "游泳 --> A person is swimming.\n\n" }, {"role": "user", "content": prompt}], timeout=10, )["choices"][0]["message"]["content"] except: pass return prompt async def fetch(**kwargs): try: timeout = aiohttp.ClientTimeout(total=10) async with aiohttp.ClientSession(timeout=timeout) as session: async with session.get(**kwargs) as response: data = await response.json() assert response.status == 200 exist = set() ret = [] for x in data: if x["motion_id"] not in exist: ret.append((x["motion_id"], x["score"])) exist.add(x["motion_id"]) return ret except: return def get_tag(motion_id): splitted = motion_id.split('_') if len(splitted) == 1: return "h3d" return splitted[0] def rank_items(sorted_items): rank = {} prev_score = None prev_rank = 0 for i, (name, score) in enumerate(sorted_items): if score == prev_score: rank[name] = prev_rank else: rank[name] = i prev_rank = i prev_score = score return rank async def search(prompt, is_dance, is_random, want_number=1, uid=None): scale = 8 if not is_dance else 20 t2t_request = fetch(url=os.getenv("T2T_SERVER") + "/result/", params={"query": prompt, **({} if not is_dance else {"tags": ["aist"]}), "fs_weight": 0.15, "max_num": want_number * 2 * scale, **({"uid": uid} if uid is not None else {})}) t2m_request = fetch(url=os.getenv("T2M_SERVER") + "/result/", params={"query": prompt, **({} if not is_dance else {"tags": ["aist"]}), "max_num": want_number * scale, **({"uid": uid} if uid is not None else {})}) _weights = [{"aist": 1.0, "else": 6.0}, {"else": 1.0}] _ranks = await asyncio.gather(*[t2t_request, t2m_request]) weights = [] ranks = [] for rank, weight in zip(_ranks, _weights): if rank is not None: weights.append(weight) ranks.append(rank) assert ranks min_length = min([len(rank) for rank in ranks]) for i in range(len(ranks)): ranks[i] = ranks[i][:min_length] ranks[i] = rank_items(ranks[i]) total_rank = defaultdict(float) min_rank = defaultdict(lambda: min_length) total_id = set() for rank in ranks: total_id |= rank.keys() id2tag = {} for x in total_id: id2tag[x] = get_tag(x) sum_weight = defaultdict(float) all_tags = set() for weight in weights: all_tags |= weight.keys() for tag in all_tags: for weight in weights: sum_weight[tag] += weight.get(tag, weight["else"]) for rank, weight in zip(ranks, weights): for x in total_id: tag = id2tag[x] total_rank[x] += rank.get(x, min_length) * weight.get(tag, weight["else"]) \ / sum_weight.get(tag, sum_weight["else"]) min_rank[x] = min(min_rank[x], rank.get(x, min_length)) length_rank = None try: redis_conn = redis.Redis(host=os.getenv("REDIS_SEVER"), port=int(os.getenv("REDIS_PORT")), password=os.getenv("REDIS_PASSWORD")) list_total_id = list(total_id) seconds = redis_conn.mget(["sec_" + x for x in list_total_id]) _length_rank = [] for motion_id, second in zip(list_total_id, seconds): _length_rank.append((motion_id, (float(second) if second is not None else 0.5))) _length_rank = sorted(_length_rank, key=lambda x: x[1], reverse=True) length_rank = rank_items(_length_rank) except: pass rank_colloctions = [total_rank, min_rank, length_rank] weight_colloctions = [0.6, 0.15, 0.25] final_rank = defaultdict(float) final_weight = 0.0 for the_rank, the_weight in zip(rank_colloctions, weight_colloctions): if the_rank is not None: final_weight += the_weight for x in total_id: final_rank[x] += the_rank[x] * the_weight assert final_weight > 0.0 noise = np.random.randn(len(final_rank)) * 0.01 for i, (k, v) in enumerate(list(final_rank.items())): final_rank[k] = v / final_weight + noise[i] final_rank = sorted(final_rank.items(), key=lambda x: x[1]) motion_ids = [x[0] for x in final_rank] assert motion_ids want_ids = [] while len(want_ids) < want_number * scale // 2: want_ids.extend(motion_ids) if is_random: want_ids = random.sample(want_ids[:want_number * scale // 2], want_number) else: want_ids = want_ids[:want_number] motions = [] for want_id in want_ids: try: with open(f"motion_database/{want_id}.json") as f: motion = json.load(f) motion["mid"] = want_id motions.append(motion) except: pass assert motions while len(motions) < want_number: motions.append(motions[0]) return motions @app.get("/angle/") async def angle(prompt: str, do_translation: bool = False, regenerate: int = 0, style: str = Query(None), want_number: int = 1, uid: str = Query(None)): assert 1 <= want_number <= 20 prompt = prompt[:100] is_dance = style is not None and style.lower() == "dance" is_random = bool(regenerate) if do_translation: prompt = translation(prompt) priors = await search(prompt, is_dance, is_random, want_number, uid) return priors
[ "translate to english without any explanation. If it's already in english, just repeat it. If get a <motion> without a subject, transfer it to: 'A person is <motion>'. e.g.:\nZombie Biting --> A person is zombie biting.\nA girl is dancing --> A girl is dancing.\n一个男人在画画 --> A man is drawing.\n游泳 --> A person is swimming.\n\n", "A girl is dancing --> A girl is dancing.\n", "gpt-3.5-turbo", "游泳 --> A person is swimming.\n\n", "translate to english without any explanation. If it's already in english, just repeat it. ", "Zombie Biting --> A person is zombie biting.\n", "content", "If get a <motion> without a subject, transfer it to: 'A person is <motion>'. e.g.:\n", "一个男人在画画 --> A man is drawing.\n" ]
2024-01-10
cndn/intelligent-code-completion
raw_data~48475___init__.py
""" This package contains algorithms for extracting document representations from their raw bag-of-word counts. """ # bring model classes directly into package namespace, to save some typing from .coherencemodel import CoherenceModel # noqa:F401 from .hdpmodel import HdpModel # noqa:F401 from .ldamodel import LdaModel # noqa:F401 from .lsimodel import LsiModel # noqa:F401 from .tfidfmodel import TfidfModel # noqa:F401 from .rpmodel import RpModel # noqa:F401 from .logentropy_model import LogEntropyModel # noqa:F401 from .word2vec import Word2Vec # noqa:F401 from .doc2vec import Doc2Vec # noqa:F401 from .keyedvectors import KeyedVectors # noqa:F401 from .ldamulticore import LdaMulticore # noqa:F401 from .phrases import Phrases # noqa:F401 from .normmodel import NormModel # noqa:F401 from .atmodel import AuthorTopicModel # noqa:F401 from .ldaseqmodel import LdaSeqModel # noqa:F401 from . import wrappers # noqa:F401 from gensim import interfaces, utils class VocabTransform(interfaces.TransformationABC): """ Remap feature ids to new values. Given a mapping between old ids and new ids (some old ids may be missing = these features are to be discarded), this will wrap a corpus so that iterating over `VocabTransform[corpus]` returns the same vectors but with the new ids. Old features that have no counterpart in the new ids are discarded. This can be used to filter vocabulary of a corpus "online":: >>> old2new = {oldid: newid for newid, oldid in enumerate(ids_you_want_to_keep)} >>> vt = VocabTransform(old2new) >>> for vec_with_new_ids in vt[corpus_with_old_ids]: >>> ... """ def __init__(self, old2new, id2token=None): self.old2new = old2new self.id2token = id2token def __getitem__(self, bow): """ Return representation with the ids transformed. """ # if the input vector is in fact a corpus, return a transformed corpus as a result is_corpus, bow = utils.is_corpus(bow) if is_corpus: return self._apply(bow) return sorted((self.old2new[oldid], weight) for oldid, weight in bow if oldid in self.old2new)
[]
2024-01-10
cndn/intelligent-code-completion
raw_data~48551_test_coherencemodel.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) 2010 Radim Rehurek <[email protected]> # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html """ Automated tests for checking transformation algorithms (the models package). """ import logging import os import tempfile import unittest from unittest import SkipTest import numpy as np from gensim.corpora.dictionary import Dictionary from gensim.matutils import argsort from gensim.models.coherencemodel import CoherenceModel, BOOLEAN_DOCUMENT_BASED from gensim.models.ldamodel import LdaModel from gensim.models.wrappers import LdaMallet from gensim.models.wrappers import LdaVowpalWabbit def testfile(): # temporary data will be stored to this file return os.path.join(tempfile.gettempdir(), 'gensim_models.tst') class TestCoherenceModel(unittest.TestCase): # set up vars used in testing ("Deerwester" from the web tutorial) texts = [ ['human', 'interface', 'computer'], ['survey', 'user', 'computer', 'system', 'response', 'time'], ['eps', 'user', 'interface', 'system'], ['system', 'human', 'system', 'eps'], ['user', 'response', 'time'], ['trees'], ['graph', 'trees'], ['graph', 'minors', 'trees'], ['graph', 'minors', 'survey'] ] dictionary = Dictionary(texts) @classmethod def setUpClass(cls): cls.corpus = [cls.dictionary.doc2bow(text) for text in cls.texts] def setUp(self): # Suppose given below are the topics which two different LdaModels come up with. # `topics1` is clearly better as it has a clear distinction between system-human # interaction and graphs. Hence both the coherence measures for `topics1` should be # greater. self.topics1 = [ ['human', 'computer', 'system', 'interface'], ['graph', 'minors', 'trees', 'eps'] ] self.topics2 = [ ['user', 'graph', 'minors', 'system'], ['time', 'graph', 'survey', 'minors'] ] self.ldamodel = LdaModel( corpus=self.corpus, id2word=self.dictionary, num_topics=2, passes=0, iterations=0 ) mallet_home = os.environ.get('MALLET_HOME', None) self.mallet_path = os.path.join(mallet_home, 'bin', 'mallet') if mallet_home else None if self.mallet_path: self.malletmodel = LdaMallet( mallet_path=self.mallet_path, corpus=self.corpus, id2word=self.dictionary, num_topics=2, iterations=0 ) vw_path = os.environ.get('VOWPAL_WABBIT_PATH', None) if not vw_path: logging.info( "Environment variable 'VOWPAL_WABBIT_PATH' not specified, skipping sanity checks for LDA Model" ) self.vw_path = None else: self.vw_path = vw_path self.vwmodel = LdaVowpalWabbit( self.vw_path, corpus=self.corpus, id2word=self.dictionary, num_topics=2, passes=0 ) def check_coherence_measure(self, coherence): """Check provided topic coherence algorithm on given topics""" if coherence in BOOLEAN_DOCUMENT_BASED: kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, coherence=coherence) else: kwargs = dict(texts=self.texts, dictionary=self.dictionary, coherence=coherence) cm1 = CoherenceModel(topics=self.topics1, **kwargs) cm2 = CoherenceModel(topics=self.topics2, **kwargs) self.assertGreater(cm1.get_coherence(), cm2.get_coherence()) def testUMass(self): """Test U_Mass topic coherence algorithm on given topics""" self.check_coherence_measure('u_mass') def testCv(self): """Test C_v topic coherence algorithm on given topics""" self.check_coherence_measure('c_v') def testCuci(self): """Test C_uci topic coherence algorithm on given topics""" self.check_coherence_measure('c_uci') def testCnpmi(self): """Test C_npmi topic coherence algorithm on given topics""" self.check_coherence_measure('c_npmi') def testUMassLdaModel(self): """Perform sanity check to see if u_mass coherence works with LDA Model""" # Note that this is just a sanity check because LDA does not guarantee a better coherence # value on the topics if iterations are increased. This can be seen here: # https://gist.github.com/dsquareindia/60fd9ab65b673711c3fa00509287ddde CoherenceModel(model=self.ldamodel, corpus=self.corpus, coherence='u_mass') def testCvLdaModel(self): """Perform sanity check to see if c_v coherence works with LDA Model""" CoherenceModel(model=self.ldamodel, texts=self.texts, coherence='c_v') def testCw2vLdaModel(self): """Perform sanity check to see if c_w2v coherence works with LDAModel.""" CoherenceModel(model=self.ldamodel, texts=self.texts, coherence='c_w2v') def testCuciLdaModel(self): """Perform sanity check to see if c_uci coherence works with LDA Model""" CoherenceModel(model=self.ldamodel, texts=self.texts, coherence='c_uci') def testCnpmiLdaModel(self): """Perform sanity check to see if c_npmi coherence works with LDA Model""" CoherenceModel(model=self.ldamodel, texts=self.texts, coherence='c_npmi') def testUMassMalletModel(self): """Perform sanity check to see if u_mass coherence works with LDA Mallet gensim wrapper""" self._check_for_mallet() CoherenceModel(model=self.malletmodel, corpus=self.corpus, coherence='u_mass') def _check_for_mallet(self): if not self.mallet_path: raise SkipTest("Mallet not installed") def testCvMalletModel(self): """Perform sanity check to see if c_v coherence works with LDA Mallet gensim wrapper""" self._check_for_mallet() CoherenceModel(model=self.malletmodel, texts=self.texts, coherence='c_v') def testCw2vMalletModel(self): """Perform sanity check to see if c_w2v coherence works with LDA Mallet gensim wrapper""" self._check_for_mallet() CoherenceModel(model=self.malletmodel, texts=self.texts, coherence='c_w2v') def testCuciMalletModel(self): """Perform sanity check to see if c_uci coherence works with LDA Mallet gensim wrapper""" self._check_for_mallet() CoherenceModel(model=self.malletmodel, texts=self.texts, coherence='c_uci') def testCnpmiMalletModel(self): """Perform sanity check to see if c_npmi coherence works with LDA Mallet gensim wrapper""" self._check_for_mallet() CoherenceModel(model=self.malletmodel, texts=self.texts, coherence='c_npmi') def testUMassVWModel(self): """Perform sanity check to see if u_mass coherence works with LDA VW gensim wrapper""" self._check_for_vw() CoherenceModel(model=self.vwmodel, corpus=self.corpus, coherence='u_mass') def _check_for_vw(self): if not self.vw_path: raise SkipTest("Vowpal Wabbit not installed") def testCvVWModel(self): """Perform sanity check to see if c_v coherence works with LDA VW gensim wrapper""" self._check_for_vw() CoherenceModel(model=self.vwmodel, texts=self.texts, coherence='c_v') def testCw2vVWModel(self): """Perform sanity check to see if c_w2v coherence works with LDA VW gensim wrapper""" self._check_for_vw() CoherenceModel(model=self.vwmodel, texts=self.texts, coherence='c_w2v') def testCuciVWModel(self): """Perform sanity check to see if c_uci coherence works with LDA VW gensim wrapper""" self._check_for_vw() CoherenceModel(model=self.vwmodel, texts=self.texts, coherence='c_uci') def testCnpmiVWModel(self): """Perform sanity check to see if c_npmi coherence works with LDA VW gensim wrapper""" self._check_for_vw() CoherenceModel(model=self.vwmodel, texts=self.texts, coherence='c_npmi') def testErrors(self): """Test if errors are raised on bad input""" # not providing dictionary self.assertRaises( ValueError, CoherenceModel, topics=self.topics1, corpus=self.corpus, coherence='u_mass' ) # not providing texts for c_v and instead providing corpus self.assertRaises( ValueError, CoherenceModel, topics=self.topics1, corpus=self.corpus, dictionary=self.dictionary, coherence='c_v' ) # not providing corpus or texts for u_mass self.assertRaises( ValueError, CoherenceModel, topics=self.topics1, dictionary=self.dictionary, coherence='u_mass' ) def testPersistence(self): fname = testfile() model = CoherenceModel( topics=self.topics1, corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass' ) model.save(fname) model2 = CoherenceModel.load(fname) self.assertTrue(model.get_coherence() == model2.get_coherence()) def testPersistenceCompressed(self): fname = testfile() + '.gz' model = CoherenceModel( topics=self.topics1, corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass' ) model.save(fname) model2 = CoherenceModel.load(fname) self.assertTrue(model.get_coherence() == model2.get_coherence()) def testPersistenceAfterProbabilityEstimationUsingCorpus(self): fname = testfile() model = CoherenceModel( topics=self.topics1, corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass' ) model.estimate_probabilities() model.save(fname) model2 = CoherenceModel.load(fname) self.assertIsNotNone(model2._accumulator) self.assertTrue(model.get_coherence() == model2.get_coherence()) def testPersistenceAfterProbabilityEstimationUsingTexts(self): fname = testfile() model = CoherenceModel( topics=self.topics1, texts=self.texts, dictionary=self.dictionary, coherence='c_v' ) model.estimate_probabilities() model.save(fname) model2 = CoherenceModel.load(fname) self.assertIsNotNone(model2._accumulator) self.assertTrue(model.get_coherence() == model2.get_coherence()) def testAccumulatorCachingSameSizeTopics(self): kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass') cm1 = CoherenceModel(topics=self.topics1, **kwargs) cm1.estimate_probabilities() accumulator = cm1._accumulator self.assertIsNotNone(accumulator) cm1.topics = self.topics1 self.assertEqual(accumulator, cm1._accumulator) cm1.topics = self.topics2 self.assertEqual(None, cm1._accumulator) def testAccumulatorCachingTopicSubsets(self): kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass') cm1 = CoherenceModel(topics=self.topics1, **kwargs) cm1.estimate_probabilities() accumulator = cm1._accumulator self.assertIsNotNone(accumulator) cm1.topics = [t[:2] for t in self.topics1] self.assertEqual(accumulator, cm1._accumulator) cm1.topics = self.topics1 self.assertEqual(accumulator, cm1._accumulator) def testAccumulatorCachingWithModelSetting(self): kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, coherence='u_mass') cm1 = CoherenceModel(topics=self.topics1, **kwargs) cm1.estimate_probabilities() self.assertIsNotNone(cm1._accumulator) cm1.model = self.ldamodel topics = [] for topic in self.ldamodel.state.get_lambda(): bestn = argsort(topic, topn=cm1.topn, reverse=True) topics.append(bestn) self.assertTrue(np.array_equal(topics, cm1.topics)) self.assertIsNone(cm1._accumulator) def testAccumulatorCachingWithTopnSettingGivenTopics(self): kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, topn=5, coherence='u_mass') cm1 = CoherenceModel(topics=self.topics1, **kwargs) cm1.estimate_probabilities() self.assertIsNotNone(cm1._accumulator) accumulator = cm1._accumulator topics_before = cm1._topics cm1.topn = 3 self.assertEqual(accumulator, cm1._accumulator) self.assertEqual(3, len(cm1.topics[0])) self.assertEqual(topics_before, cm1._topics) # Topics should not have been truncated, so topn settings below 5 should work cm1.topn = 4 self.assertEqual(accumulator, cm1._accumulator) self.assertEqual(4, len(cm1.topics[0])) self.assertEqual(topics_before, cm1._topics) with self.assertRaises(ValueError): cm1.topn = 6 # can't expand topics any further without model def testAccumulatorCachingWithTopnSettingGivenModel(self): kwargs = dict(corpus=self.corpus, dictionary=self.dictionary, topn=5, coherence='u_mass') cm1 = CoherenceModel(model=self.ldamodel, **kwargs) cm1.estimate_probabilities() self.assertIsNotNone(cm1._accumulator) accumulator = cm1._accumulator topics_before = cm1._topics cm1.topn = 3 self.assertEqual(accumulator, cm1._accumulator) self.assertEqual(3, len(cm1.topics[0])) self.assertEqual(topics_before, cm1._topics) cm1.topn = 6 # should be able to expand given the model self.assertEqual(6, len(cm1.topics[0])) def testCompareCoherenceForTopics(self): topics = [self.topics1, self.topics2] cm = CoherenceModel.for_topics( topics, dictionary=self.dictionary, texts=self.texts, coherence='c_v') self.assertIsNotNone(cm._accumulator) # Accumulator should have all relevant IDs. for topic_list in topics: cm.topics = topic_list self.assertIsNotNone(cm._accumulator) (coherence_topics1, coherence1), (coherence_topics2, coherence2) = \ cm.compare_model_topics(topics) self.assertAlmostEqual(np.mean(coherence_topics1), coherence1, 4) self.assertAlmostEqual(np.mean(coherence_topics2), coherence2, 4) self.assertGreater(coherence1, coherence2) def testCompareCoherenceForModels(self): models = [self.ldamodel, self.ldamodel] cm = CoherenceModel.for_models( models, dictionary=self.dictionary, texts=self.texts, coherence='c_v') self.assertIsNotNone(cm._accumulator) # Accumulator should have all relevant IDs. for model in models: cm.model = model self.assertIsNotNone(cm._accumulator) (coherence_topics1, coherence1), (coherence_topics2, coherence2) = \ cm.compare_models(models) self.assertAlmostEqual(np.mean(coherence_topics1), coherence1, 4) self.assertAlmostEqual(np.mean(coherence_topics2), coherence2, 4) self.assertAlmostEqual(coherence1, coherence2, places=4) if __name__ == '__main__': logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG) unittest.main()
[]
2024-01-10
lianogueira/pytwanalysis
pytwanalysis~py_twitter_topics.py
import os import json import datetime import csv import string import gensim from gensim import corpora from gensim.models.coherencemodel import CoherenceModel import nltk from nltk.corpus import words, stopwords, wordnet from nltk.tokenize import RegexpTokenizer from nltk.stem import PorterStemmer, WordNetLemmatizer from nltk import pos_tag from nltk.tokenize import word_tokenize import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import collections import numpy as np from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator #dictionary_words = dict.fromkeys(words.words(), None) #stopWords = set(stopwords.words('english')) #tokenizer = RegexpTokenizer(r'\w+') #stemmer = PorterStemmer() #lemmatiser = WordNetLemmatizer() stop = set(stopwords.words('english')) stop.add ('u') stop.add ('e') exclude = set(string.punctuation) lemma = WordNetLemmatizer() #topic_doc_complete = [] #lda_model = "" class TwitterTopics: def __init__(self, folder_path, mongoDB_database=None): self.folder_path = folder_path self.lda_model = object() self.lsi_model = object() self.doc_term_matrix = object() self.dictionary = object() self.lda_coh_u_mass = 0 self.lda_coh_c_v = 0 self.lsi_coh_u_mass = 0 self.lsi_coh_c_v = 0 self.db = mongoDB_database if mongoDB_database is not None: self.c_topics = self.db.topics else: self.c_topics = None def __del__(self): self.folder_path = None self.lda_model = None self.lsi_model = None self.doc_term_matrix = None self.dictionary = None self.lda_coh_u_mass = None self.lda_coh_c_v = None self.lsi_coh_u_mass = None self.lsi_coh_c_v = None def get_coh_u_mass(self): return self.lda_coh_u_mass, self.lsi_coh_u_mass def get_coh_c_v(self): return self.lda_coh_c_v, self.lda_coh_c_v #create one array with all tweets of one hashtag for topic analysis def get_docs_from_file(self, file_path): docs = [] with open(file_path, 'r', encoding='utf8', errors='ignore') as f: for line in f: docs.append(line) f.close() return docs #clean documents for topic analysis def clean_docs(self, doc, delete_numbers=True, delete_stop_words=True, lemmatize_words=True): doc_clean = doc if delete_numbers ==True: doc_clean = doc.replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '').replace('0', '') if delete_stop_words == True: doc_clean = " ".join([i for i in doc_clean.lower().split() if i not in stop]) doc_clean = ''.join(ch for ch in doc_clean if ch not in exclude) if lemmatize_words == True: doc_clean = " ".join(lemma.lemmatize(word) for word in doc_clean.split()) return doc_clean #train model def train_model(self, topic_docs, num_topics, model_name, blnSaveinDB=False, blnSaveTrainedModelFiles=False, txtFileName=None, model_type='both', lda_num_of_iterations=150, delete_stop_words=True, lemmatize_words=True, delete_numbers=True): #starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #print("Executing train_model... Started at: " + starttime ) doc_clean = [self.clean_docs(doc, delete_numbers, delete_stop_words, lemmatize_words).split() for doc in topic_docs] # Creating the term dictionary of our corpus, where every unique term is assigned an index. dictionary = corpora.Dictionary(doc_clean) self.dictionary = corpora.Dictionary(doc_clean) # Converting list of documents (corpus) into Document Term Matrix using dictionary prepared above. self.doc_term_matrix = [self.dictionary.doc2bow(doc) for doc in doc_clean] # Creating the object for LDA model using gensim library Lda = gensim.models.ldamodel.LdaModel file_data = [] if model_type in ('lda', 'both'): # Build the LDA model self.lda_model = gensim.models.LdaModel(corpus=self.doc_term_matrix, num_topics=num_topics, id2word=self.dictionary, iterations=lda_num_of_iterations) #get LDA coherence self.lda_coh_u_mass = CoherenceModel(model=self.lda_model, corpus=self.doc_term_matrix, dictionary=self.dictionary, coherence='u_mass') self.lda_coh_c_v = CoherenceModel(model=self.lda_model, texts=doc_clean, dictionary=self.dictionary, coherence='c_v') #create json file with lda results for idx in range(num_topics): topic = idx+1 strtopic = str(topic) data = '{"model_name":"' + model_name + \ '", "model_type":"' + 'lda' + \ '", "timestamp":"' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + \ '", "no_tweets":"' + str(len(topic_docs)) + \ '", "coh_u_mass":"' + str(self.lda_coh_u_mass.get_coherence()) + \ '", "coh_c_v":"' + str(self.lda_coh_c_v.get_coherence()) + \ '", "topic_no":"' + strtopic + \ '", "topic":"' + str(self.lda_model.print_topic(idx, num_topics)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) if model_type in ('lsi', 'both'): # Build the LSI model self.lsi_model = gensim.models.LsiModel(corpus=self.doc_term_matrix, num_topics=num_topics, id2word=self.dictionary) #get LSI coherence self.lsi_coh_u_mass = CoherenceModel(model=self.lsi_model, corpus=self.doc_term_matrix, dictionary=self.dictionary, coherence='u_mass') self.lsi_coh_c_v = CoherenceModel(model=self.lsi_model, texts=doc_clean, dictionary=self.dictionary, coherence='c_v') #create json file with lsi results for idx in range(num_topics): topic = idx+1 strtopic = str(topic) data = '{"model_name":"' + model_name + \ '", "model_type":"' + 'lsi' + \ '", "timestamp":"' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + \ '", "no_tweets":"' + str(len(topic_docs)) + \ '", "coh_u_mass":"' + str(self.lsi_coh_u_mass.get_coherence()) + \ '", "coh_c_v":"' + str(self.lsi_coh_c_v.get_coherence()) + \ '", "topic_no":"' + strtopic + \ '", "topic":"' + str(self.lsi_model.print_topic(idx, num_topics)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) # Save if mongoDB collection is asked if blnSaveinDB == True: if self.db is not None: self.c_topics.insert_many(file_data) else: print("Can't save topics in db. No mongoDB connection was set up.") # Save results in a text file if txtFileName is not None: with open(txtFileName, 'w', encoding="utf-8") as outfile: json.dump(file_data, outfile) # Save models into file if blnSaveTrainedModelFiles == True: #creates path if does not exists if not os.path.exists(self.folder_path + "/trained_models/"): os.makedirs(self.folder_path + "/trained_models/") self.lda_model.save(self.folder_path + "/trained_models/" + model_name + "_lda_model.model") self.dictionary.save(self.folder_path + "/trained_models/" + model_name + "_dictionary.dict") #endtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #print("Finished executing train_model. Ended at: " + endtime) #train model from file def train_model_from_file(self, file_path, num_topics, model_name, blnSaveinDB=False, blnSaveTrainedModelFiles=False, txtFileName=None, model_type='both', lda_num_of_iterations=150, delete_stop_words=True, lemmatize_words=True, delete_numbers=True): docs = self.get_docs_from_file(file_path) self.train_model(docs, num_topics, model_name, blnSaveinDB, blnSaveTrainedModelFiles, txtFileName, model_type, lda_num_of_iterations, delete_stop_words, lemmatize_words, delete_numbers) #plot graph with lda topics def plot_topics(self, file_name, no_of_topics, model_type = 'lda', fig_size_x = 17, fig_size_y=15, replace_existing_file=True): if replace_existing_file==True or not os.path.exists(file_name): fig_size_y = 7*(no_of_topics/2) fiz=plt.figure(figsize=(fig_size_x, fig_size_y)) for i in range(no_of_topics): if model_type == 'lda': df=pd.DataFrame(self.lda_model.show_topic(i), columns=['term','prob']).set_index('term') elif model_type == 'lsi': df=pd.DataFrame(self.lsi_model.show_topic(i), columns=['term','prob']).set_index('term') no_rows = int(no_of_topics/2)+no_of_topics%2 plt.subplot(no_rows,2,i+1) plt.title('topic '+str(i+1)) sns.barplot(x='prob', y=df.index, data=df, label='Cities', palette='Reds_d') plt.xlabel('probability') #save the file plt.savefig(file_name, dpi=200, facecolor='w', edgecolor='w') #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window # read a frequency list into a pandas objects # file format word\tfrequency def read_freq_list_file(self, file_path, delimiter='\t'): #df = pd.read_csv(file_path, encoding = "ISO-8859-1", header=None, sep=delimiter, lineterminator='\n') df = pd.read_csv(file_path, encoding = "utf-8", header=None, sep=delimiter, lineterminator='\n') df.columns = ['word', 'freq'] return df #plot a bar graph with the top frequency list def plot_top_freq_list(self, fr_list, top_no, ylabel, exclude_top_no=0, file=None, replace_existing_file= True): if exclude_top_no != 0: fr_list = fr_list.iloc[exclude_top_no:] fr_list = fr_list.nlargest(top_no,'freq') if len(fr_list) < top_no: for i in range( int((top_no-len(fr_list)) / 2.5)): data = [['', 0], ['', 0] ] df2 = pd.DataFrame(data, columns = ['word', 'freq']) fr_list = fr_list.append(df2) fr_list_gr = fr_list.groupby("word") plt.figure(figsize=(12, len(fr_list)/2.5)) fr_list_gr.max().sort_values(by="freq",ascending=True)["freq"].plot.barh() plt.xticks(rotation=50) plt.xlabel("Frequency") plt.ylabel(ylabel) if file != None: if replace_existing_file==True or not os.path.exists(file): plt.savefig(file, dpi=300, bbox_inches='tight') #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window #plot a word cloudfor a frequency list def plot_word_cloud(self, fr_list, file=None, replace_existing_file=True): wordcount = collections.defaultdict(int) for index, row in fr_list.iterrows(): wordcount[row['word']] = row['freq'] try: wordcloud = WordCloud(width=2000, height=1300, max_words=1000, background_color="white").generate_from_frequencies(wordcount) except: wordcloud = WordCloud(width=2000, height=1300, background_color="white").generate_from_frequencies(wordcount) # Display the generated image: plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") if file is not None: if replace_existing_file==True or not os.path.exists(file): plt.savefig(str(file), dpi=300) #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window #load existing model from file #predict topic of a new tweet based on model
[]
2024-01-10
lianogueira/pytwanalysis
doc~src~ta.py
"""Main class """ from pytwanalysis.py_twitter_db import TwitterDB from pytwanalysis.py_twitter_graphs import TwitterGraphs from pytwanalysis.py_twitter_topics import TwitterTopics #from pyTwitterGraphAnalysis import tw_graph #from pyTwitterDB import tw_database #from pyTwitterTopics import tw_topics from pymongo import MongoClient import networkx as nx import numpy as np import os import datetime import csv import pandas as pd import matplotlib.pyplot as plt import time import warnings warnings.filterwarnings("ignore") MIN_NO_OF_NODES_TO_REDUCE_GRAPH = 100 class TwitterAnalysis(TwitterGraphs, TwitterDB, TwitterTopics): """ Main class - It inherits TwitterGraphs, TwitterDB, and TwitterTopics classes. """ def __init__( self, base_folder_path, mongoDB_database): TwitterGraphs.__init__(self, base_folder_path) TwitterDB.__init__(self, mongoDB_database) TwitterTopics.__init__(self, base_folder_path, mongoDB_database) self.type_of_graph = 'user_conn_all' self.is_bot_Filter = None self.period_arr = None self.create_nodes_edges_files_flag = 'Y' self.create_graphs_files_flag ='Y' self.create_topic_model_files_flag = 'Y' self.create_ht_frequency_files_flag = 'Y' self.create_words_frequency_files_flag = 'Y' self.create_timeseries_files_flag = 'Y' self.create_top_nodes_files_flag = 'Y' self.create_community_files_flag = 'N' self.create_ht_conn_files_flag = 'Y' self.num_of_topics = 4 self.top_no_word_filter = None self.top_ht_to_ignore = None self.graph_plot_cutoff_no_nodes = 500 self.graph_plot_cutoff_no_edges = 2000 self.create_graph_without_node_scale_flag = 'N' self.create_graph_with_node_scale_flag = 'Y' self.create_reduced_graph_flag = 'Y' self.reduced_graph_comty_contract_per = 90 self.reduced_graph_remove_edge_weight = None self.reduced_graph_remove_edges = 'Y' self.top_degree_start = 1 self.top_degree_end = 10 self.period_top_degree_start = 1 self.period_top_degree_end = 5 self.commty_edge_size_cutoff = 200 self.user_conn_filter = None self.edge_prefix_str = 'UserConnections_' ##################################### # Method: setConfigs # Description: Configure objects settings def setConfigs( self, type_of_graph='user_conn_all', is_bot_Filter=None, period_arr=None, create_nodes_edges_files_flag='Y', create_graphs_files_flag='Y', create_topic_model_files_flag='Y', create_ht_frequency_files_flag='Y', create_words_frequency_files_flag='Y', create_timeseries_files_flag='Y', create_top_nodes_files_flag = 'Y', create_community_files_flag = 'N', create_ht_conn_files_flag='Y', num_of_topics=4, top_no_word_filter=None, top_ht_to_ignore=None, graph_plot_cutoff_no_nodes=500, graph_plot_cutoff_no_edges=2000, create_graph_without_node_scale_flag='N', create_graph_with_node_scale_flag='Y', create_reduced_graph_flag='Y', reduced_graph_comty_contract_per=90, reduced_graph_remove_edge_weight=None, reduced_graph_remove_edges='Y', top_degree_start=1, top_degree_end=10, period_top_degree_start=1, period_top_degree_end=5, commty_edge_size_cutoff=200): """ Configure the current object settings to drive the automation of the analysis files Parameters ---------- type_of_graph : (Optional) This setting defines the type of graph to analyze. Six different options are available: user_conn_all, user_conn_retweet, user_conn_quote, user_conn_reply, user_conn_mention, and ht_conn. (Default='user_conn_all') is_bot_Filter : (Default=None) period_arr : (Optional) An array of start and end dates can be set so that the pipeline creates a separate analysis folder for each of the periods in the array. (Default=None) create_nodes_edges_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create two files for each graph and sub-graph. One file with the edge list, and one with the node list and their respective degree.(Default='Y') create_graphs_files_flag : (Optional) If this setting is set to 'Y', the pipeline will plot the graph showing all the connections. (Default='Y') create_topic_model_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create topic discovery related files for each folder. It will create a text file with all the tweets that are part of that folder, it will also train a LDA model based on the tweets texts and plot a graph with the results. (Default='Y') create_ht_frequency_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create hashtag frequency files for each folder. It will create a text file with the full list of hashtags and their frequency, a wordcloud showing the most frequently used hashtags, and barcharts showing the top 30 hashtags. (Default='y')' create_words_frequency_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create word frequency files for each folder. It will create a text file with a list of words and their frequency, a wordcloud showing the most frequently used words, and barcharts showing the top 30 words. (Default='Y') create_timeseries_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create timeseries graphs for each folder representing the tweet count by day, and the top hashtags frequency count by day. (Default='Y') create_top_nodes_files_flag : (Optional) If this setting is set to 'Y', the pipeline will create separate analysis folders for all the top degree nodes. (Default='Y') create_community_files_flag : (Optional) If this setting is set to 'Y', the pipeline will use the louvain method to assign each node to a community. A separate folder for each of the communities will be created with all the analysis files. (Default='N') create_ht_conn_files_flag : (Optional) If this setting is set to 'Y', the pipeline will plot hashtag connections graphs. This can be used when user connections are being analyzed, but it could still be interesting to see the hashtags connections made by that group of users. (Default='Y') num_of_topics : (Optional) If the setting *CREATE_TOPIC_MODEL_FILES_FLAG* was set to 'Y', then this number will be used to send as input to the LDA model. If no number is given, the pipeline will use 4 as the default value. (Default=4) top_no_word_filter : (Optional) If the setting *CREATE_WORDS_FREQUENCY_FILES_FLAG* was set to 'Y', then this number will be used to decide how many words will be saved in the word frequency list text file. If no number is given, the pipeline will use 5000 as the default value. (Default=None) top_ht_to_ignore : (Optional) If the setting *CREATE_HT_CONN_FILES_FLAG* was set to 'Y', then this number will be used to choose how many top hashtags can be ignored. Sometimes ignoring the main hashtag can be helpful in visualizations to discovery other interesting structures within the graph. (Default=None) graph_plot_cutoff_no_nodes : (Optional) Used with the graph_plot_cutoff_no_edges parameter. For each graph created, these numbers will be used as cutoff values to decide if a graph is too large to be plot or not. Choosing a large number can result in having the graph to take a long time to run. Choosing a small number can result in graphs that are too reduced and with little value or even graphs that can't be printed at all because they can't be reduce further. (Default=500) graph_plot_cutoff_no_edges : (Optional) Used with the graph_plot_cutoff_no_nodes parameter. For each graph created, these numbers will be used as cutoff values to decide if a graph is too large to be plot or not. Choosing a large number can result in having the graph to take a long time to run. Choosing a small number can result in graphs that are too reduced and with little value or even graphs that can't be printed at all because they can't be reduce further. (Default=2000) create_graph_without_node_scale_flag : (Optional) For each graph created, if this setting is set to 'Y', the pipeline will try to plot the full graph with no reduction and without any logic for scaling the node size. (Default='N') create_graph_with_node_scale_flag : (Optional) For each graph created, if this setting is set to 'Y', the pipeline will try to plot the full graph with no reduction, but with additional logic for scaling the node size. (Default='Y') create_reduced_graph_flag : (Optional) For each graph created, if this setting is set to 'Y', the pipeline will try to plot the reduced form of the graph. (Default='Y') reduced_graph_comty_contract_per : (Optional) If the setting *CREATE_REDUCED_GRAPH_FLAG* was set to 'Y', then this number will be used to reduce the graphs by removing a percentage of each community found in that particular graph. The logic can be run multiple times with different percentages. For each time, a new graph file will be saved with a different name according to the parameter given. (Default=90) reduced_graph_remove_edge_weight : (Optional) If the setting *CREATE_REDUCED_GRAPH_FLAG* was set to 'Y', then this number will be used to reduce the graphs by removing edges that have weights smaller then this number. The logic can be run multiple times with different percentages. For each time, a new graph file will be saved with a different name according to the parameter given. (Default=None) reduced_graph_remove_edges : (Optional) If this setting is set to 'Y', and the setting *CREATE_REDUCED_GRAPH_FLAG was set to 'Y', then the pipeline will continuously try to reduce the graphs by removing edges of nodes with degrees smaller than this number. It will stop the graph reduction once it hits the the values set int the GRAPH_PLOT_CUTOFF parameters. (Default='Y') top_degree_start : (Optional) If the setting *CREATE_TOP_NODES_FILES_FLAG* was set to 'Y', then these numbers will define how many top degree node sub-folders to create. (Default=1) top_degree_end : (Optional) If the setting *CREATE_TOP_NODES_FILES_FLAG* was set to 'Y', then these numbers will define how many top degree node sub-folders to create. (Default=10) period_top_degree_start : (Optional) If the setting *CREATE_TOP_NODES_FILES_FLAG* was set to 'Y', then these numbers will define how many top degree node sub-folders for each period to create. (Default=1) period_top_degree_end : (Optional) If the setting *CREATE_TOP_NODES_FILES_FLAG* was set to 'Y', then these numbers will define how many top degree node sub-folders for each period to create. (Default=5) commty_edge_size_cutoff : (Optional) If the setting textit{CREATE_COMMUNITY_FILES_FLAG} was set to 'Y', then this number will be used as the community size cutoff number. Any communities that have less nodes then this number will be ignored. If no number is given, the pipeline will use 200 as the default value. (Default=200) Examples -------- ...: >>> setConfigs(type_of_graph=TYPE_OF_GRAPH, >>> is_bot_Filter=IS_BOT_FILTER, >>> period_arr=PERIOD_ARR, >>> create_nodes_edges_files_flag=CREATE_NODES_EDGES_FILES_FLAG, >>> create_graphs_files_flag=CREATE_GRAPHS_FILES_FLAG, >>> create_topic_model_files_flag=CREATE_TOPIC_MODEL_FILES_FLAG, >>> create_ht_frequency_files_flag=CREATE_HT_FREQUENCY_FILES_FLAG, >>> create_words_frequency_files_flag=CREATE_WORDS_FREQUENCY_FILES_FLAG, >>> create_timeseries_files_flag=CREATE_TIMESERIES_FILES_FLAG, >>> create_top_nodes_files_flag=CREATE_TOP_NODES_FILES_FLAG, >>> create_community_files_flag=CREATE_COMMUNITY_FILES_FLAG, >>> create_ht_conn_files_flag=CREATE_HT_CONN_FILES_FLAG, >>> num_of_topics=NUM_OF_TOPICS, >>> top_no_word_filter=TOP_NO_WORD_FILTER, >>> top_ht_to_ignore=TOP_HT_TO_IGNORE, >>> graph_plot_cutoff_no_nodes=GRAPH_PLOT_CUTOFF_NO_NODES, >>> graph_plot_cutoff_no_edges=GRAPH_PLOT_CUTOFF_NO_EDGES, >>> create_graph_without_node_scale_flag=CREATE_GRAPH_WITHOUT_NODE_SCALE_FLAG, >>> create_graph_with_node_scale_flag=CREATE_GRAPH_WITH_NODE_SCALE_FLAG, >>> create_reduced_graph_flag=CREATE_REDUCED_GRAPH_FLAG, >>> reduced_graph_comty_contract_per=REDUCED_GRAPH_COMTY_PER, >>> reduced_graph_remove_edge_weight=REDUCED_GRAPH_REMOVE_EDGE_WEIGHT, >>> reduced_graph_remove_edges=REDUCED_GRAPH_REMOVE_EDGES_UNTIL_CUTOFF_FLAG, >>> top_degree_start=TOP_DEGREE_START, >>> top_degree_end=TOP_DEGREE_END, >>> period_top_degree_start=PERIOD_TOP_DEGREE_START, >>> period_top_degree_end=PERIOD_TOP_DEGREE_END, >>> commty_edge_size_cutoff=COMMTY_EDGE_SIZE_CUTOFF >>> ) """ self.type_of_graph = type_of_graph self.is_bot_Filter = is_bot_Filter self.period_arr = period_arr self.create_nodes_edges_files_flag = create_nodes_edges_files_flag self.create_graphs_files_flag = create_graphs_files_flag self.create_topic_model_files_flag = create_topic_model_files_flag self.create_ht_frequency_files_flag = create_ht_frequency_files_flag self.create_words_frequency_files_flag = create_words_frequency_files_flag self.create_timeseries_files_flag = create_timeseries_files_flag self.create_top_nodes_files_flag = create_top_nodes_files_flag self.create_community_files_flag = create_community_files_flag self.create_ht_conn_files_flag = create_ht_conn_files_flag self.num_of_topics = num_of_topics self.top_no_word_filter = top_no_word_filter self.top_ht_to_ignore = top_ht_to_ignore self.graph_plot_cutoff_no_nodes = graph_plot_cutoff_no_nodes self.graph_plot_cutoff_no_edges = graph_plot_cutoff_no_edges self.create_graph_without_node_scale_flag = create_graph_without_node_scale_flag self.create_graph_with_node_scale_flag = create_graph_with_node_scale_flag self.create_reduced_graph_flag = create_reduced_graph_flag self.reduced_graph_comty_contract_per = reduced_graph_comty_contract_per self.reduced_graph_remove_edge_weight = reduced_graph_remove_edge_weight self.reduced_graph_remove_edges = reduced_graph_remove_edges self.top_degree_start = top_degree_start self.top_degree_end = top_degree_end self.period_top_degree_start = period_top_degree_start self.period_top_degree_end = period_top_degree_end self.commty_edge_size_cutoff = commty_edge_size_cutoff if self.type_of_graph == 'user_conn_all': self.edge_prefix_str = 'UserConnections_' elif self.type_of_graph == 'user_conn_mention': self.edge_prefix_str = 'MentionUserConnections_' self.user_conn_filter = 'mention' elif self.type_of_graph == 'user_conn_retweet': self.edge_prefix_str = 'RetweetUserConnections_' self.user_conn_filter = 'retweet' elif self.type_of_graph == 'user_conn_reply': self.edge_prefix_str = 'ReplyUserConnections_' self.user_conn_filter = 'reply' elif self.type_of_graph == 'user_conn_quote': self.edge_prefix_str = 'QuoteUserConnections_' self.user_conn_filter = 'quote' elif self.type_of_graph == 'ht_conn': self.edge_prefix_str = 'HTConnection_' self.export_type = 'ht_edges' ##################################### # Method: create_path # Description: creates a path to add the files for this node def create_path(self, path): if not os.path.exists(path): os.makedirs(path) ##################################### # Method: get_now_dt # Description: returns formated current timestamp to be printed def get_now_dt(self): return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") ##################################### # Method: concat_edges # Description: aux function to concatenate edges to help filter in mongoDB def concat_edges(self, G): """ Aux function to concatenate edges to help filter in mongoDB Parameters ---------- G : undirected networkx graph created from the Twitter data Returns ------- arr_edges the array with the concatenatd edges Examples -------- Create an array of concatenated edges from a networkx graph: >>> arr_edges = concat_edges(G) """ arr_edges = [] for u,v,a in G.edges(data=True): arr_edges.append(u.lower() + '-' +v.lower()) arr_edges.append(v.lower() + '-' +u.lower()) return arr_edges ##################################### # Method: build_db_collections # Description: Call methods to create all collections in mongoDB def build_db_collections(self, inc=100000, bots_ids_list_file=None): """ This method is in charge of extracting, cleaning, and loading the data into all the collections in MongoDB. Parameters ---------- inc : (Optional) used to determine how many tweets will be processed at a time - (Default=100000). A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. bots_ids_list_file : (Optional) a file that contains a list of user ids that are bots. It creates flags in the MongoDB collection to indentify which tweets and user are in the bots list. - (Default=None) Examples -------- Load all data into all collections in MongoDB: >>> inc = 50000 >>> build_db_collections(inc) """ ### Loading Focused Data into MongoDB self.loadFocusedData(inc) ### Loading user information to collection # Loading user information for the actual tweet document self.loadUsersData(inc, 'tweet') # Loading user information for the original tweet in case of retweets self.loadUsersData(inc, 'retweet') # Loading user information for the quoted tweet self.loadUsersData(inc, 'quote') # Loading user information for replies - # (in this case we we don't have full information about the user. Only screen_name and user_id) self.loadUsersData(inc, 'reply') # Loading user information for mention - # (in this case we we don't have full information about the user. Only screen_name and sometimes user_id) self.loadUsersData(inc, 'mention') ### Breaking tweets into Words self.loadWordsData(inc) ### Loading tweet connections - # These are the edges formed between users by replies, retweets, quotes and mentions self.loadTweetConnections(inc) ### Loading tweet hashtag connections - # These are the edges formed between hash tags being used together in the same tweet self.loadTweetHTConnections(inc) ##### ### loading aggregate collections self.loadAggregations('tweetCountByFile') self.loadAggregations('tweetCountByLanguageAgg') self.loadAggregations('tweetCountByMonthAgg') # Loading bots list from file - (List of user ids that are bots) # SKIP this step if you don't have a bots list if bots_ids_list_file is not None: bots_list_id_str = [] with open(bots_ids_list_file,'r') as f: for line in f: line = line.rstrip("\n") bots_list_id_str.append(line) self.set_bot_flag_based_on_arr(bots_list_id_str, 10000) ##################################### # Method: plot_graph_contracted_nodes # Description: aux function to plot graph. # This steps repets in different parts of this code, so creating a function to avoid repetition def plot_graph_contracted_nodes(self, G, file): """ Method to compress and plot graph based on the graph reduction settings that can be updated using the *setConfigs* method. Parameters ---------- G : undirected networkx graph created from the Twitter data file : the path and name of the graph you want to save Example -------- >>> plot_graph_contracted_nodes(G, 'c:\\Data\\MyGraph.png') """ G2 = G.copy() if len(G2.nodes()) > MIN_NO_OF_NODES_TO_REDUCE_GRAPH: contraction_name = '' print("Graph to plot before changes: nodes=" + str(len(G2.nodes)) + " edges=" + str(len(G2.edges))) #in case we want to reduce the graph with edges of weight less than a cutoff number if self.reduced_graph_remove_edge_weight is not None: #find list of edges that have that weigh cutoff edges_to_remove = [edge for edge in list(G2.edges(data=True)) if edge[2]['weight'] <= self.reduced_graph_remove_edge_weight] #remove edges for the list G2.remove_edges_from(edges_to_remove) #get the largest connected component G2 = self.largest_component_no_self_loops(G2) contraction_name = contraction_name + "[RemEdgeWeightLessThan" + str(self.reduced_graph_remove_edge_weight) + "]" #reduce graph based on a percentage of the nodes for each community if self.reduced_graph_comty_contract_per is not None and len(G2.nodes()) > MIN_NO_OF_NODES_TO_REDUCE_GRAPH: att = 'community_louvain' G2 = self.contract_nodes_commty_per(G2, self.reduced_graph_comty_contract_per, att) G2 = self.largest_component_no_self_loops(G2) contraction_name = contraction_name + "[RemPercOfComty=" + str(self.reduced_graph_comty_contract_per) + "]" #In case we want to continue to remove until we get to a cutoff number, another level of contraction if self.reduced_graph_remove_edges == 'Y' and len(G2.nodes()) > MIN_NO_OF_NODES_TO_REDUCE_GRAPH: if len(G2.edges()) > 100000: cutoff_no = 3 G2 = self.remove_edges_eithernode(G2, cutoff_no) contraction_name = contraction_name + '[RemEdgeEitherNodeDegCutoff=' + str(cutoff_no) + ']' cutoff_no = 5 if (len(G2.nodes()) > self.graph_plot_cutoff_no_nodes) or (len(G2.edges()) > self.graph_plot_cutoff_no_edges): while (len(G2.nodes()) > self.graph_plot_cutoff_no_nodes) or (len(G2.edges()) > self.graph_plot_cutoff_no_edges): G2 = self.remove_edges(G2, cutoff_no) if len(G2.nodes()) > 0: G2 = self.largest_component_no_self_loops(G2) if cutoff_no < 150: cutoff_no += 10 elif cutoff_no < 1000: cutoff_no += 100 elif cutoff_no < 10000: cutoff_no += 500 else: cutoff_no += 1000 contraction_name = contraction_name + '[RemEdgeBothNodesDegLessThan=' + str(cutoff_no) + ']' #set up final file name with reduction parameters file = file.replace('.', contraction_name + '.') #get largest connected component after all removals if len(G2.edges()) > 0: G2 = self.largest_component_no_self_loops(G2) #find best settings for the graphs depending on size. You can change these to get better graphs if len(G2.edges()) < 450: v_scale = 0.01; v_k =0.7; v_iterations=50; v_node_size=2 elif len(G2.edges()) < 5000: v_scale = 2; v_k = 0.6; v_iterations=200; v_node_size=0.8 elif len(G2.edges()) < 10000: v_scale = 1; v_k = 0.1; v_iterations=200; v_node_size=0.6 elif len(G2.edges()) >= 10000: v_scale = 1; v_k = 0.05; v_iterations=500; v_node_size=0.6 print("Graph to plot after changes: nodes=" + str(len(G2.nodes)) + " edges=" + str(len(G2.edges))) if (len(G2.nodes()) < self.graph_plot_cutoff_no_nodes and len(G2.edges()) < self.graph_plot_cutoff_no_edges) and len(G2.edges()) != 0: if not os.path.exists(file): G_to_plot, labels2, k = self.calculate_louvain_clustering(G2) self.plotSpringLayoutGraph(G_to_plot, file, v_scale, v_k, v_iterations, cluster_fl='Y', v_labels=list(list(labels2)), replace_existing_file=False) ##################################### # Method: export_mult_types_edges_for_input # Description: export edges that will be used to create graphs # User can choose only one type of graph to export the edges, or export them all def export_mult_types_edges_for_input(self, period_arr=None, bot_filter_fl='N', type_of_graph='all'): """ This method will export edges from mongoDB data that can be used to create graphs. The user can choose only one type of graph to export the edges, or export them all Parameters ---------- period_arr : (Optional) An array with showing the different periods to be analyzed separatly in the data. (Default = None) bot_filter_fl : (Optional) A flag to identify if you want to create extra edge files separating tweets by bots or not. This option is only available when the bot flag was updated in mongoDB using method set_bot_flag_based_on_arr. (Default='N') type_of_graph : (Optional) the type of graph to export the edges for. Available options: user_conn_all, user_conn_mention, user_conn_retweet, user_conn_reply, user_conn_quote, ht_conn, or all (Default='all') Example -------- >>> # Set up the periods you want to analyse >>> # Set period_arr to None if you don't want to analyze separate periods >>> # Format: Period Name, Period Start Date, Period End Date >>> period_arr = [['P1', '10/08/2017 00:00:00', '10/15/2017 00:00:00'], >>> ['P2', '01/21/2018 00:00:00', '02/04/2018 00:00:00'], >>> ['P3', '02/04/2018 00:00:00', '02/18/2018 00:00:00'], >>> ['P4', '02/18/2018 00:00:00', '03/04/2018 00:00:00']] >>> >>> >>> ## TYPE OF GRAPH EDGES >>> ######################################################## >>> # You can export edges for one type, or for all >>> # Options: user_conn_all, --All user connections >>> # user_conn_mention, --Only Mentions user connections >>> # user_conn_retweet, --Only Retweets user connections >>> # user_conn_reply, --Only Replies user connections >>> # user_conn_quote, --Only Quotes user connections >>> # ht_conn --Hashtag connects - (Hashtgs that wereused together) >>> # all --It will export all of the above options >>> >>> TYPE_OF_GRAPH = 'all' >>> >>> export_mult_types_edges_for_input(period_arr=period_arr, type_of_graph=TYPE_OF_GRAPH) """ if type_of_graph == 'all' or type_of_graph == 'user_conn_all': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='user_conn_all') if type_of_graph == 'all' or type_of_graph == 'user_conn_mention': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='user_conn_mention') if type_of_graph == 'all' or type_of_graph == 'user_conn_retweet': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='user_conn_retweet') if type_of_graph == 'all' or type_of_graph == 'user_conn_reply': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='user_conn_reply') if type_of_graph == 'all' or type_of_graph == 'user_conn_quote': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='user_conn_quote') if type_of_graph == 'all' or type_of_graph == 'ht_conn': self.export_all_edges_for_input(period_arr, bot_filter_fl, type_of_graph='ht_conn') ##################################### # Method: export_all_edges_for_input # Description: export edges that will be used to create graphs def export_all_edges_for_input(self, period_arr=None, bot_filter_fl='N', type_of_graph='user_conn_all'): # Creates path to add the edge files to be used as input input_files_path = self.folder_path + '\\data_input_files' self.create_path(input_files_path) # edge_prefix_str = '' user_conn_filter = None export_type = 'edges' if type_of_graph == 'user_conn_all': edge_prefix_str = 'UserConnections_' elif type_of_graph == 'user_conn_mention': edge_prefix_str = 'MentionUserConnections_' user_conn_filter = 'mention' elif type_of_graph == 'user_conn_retweet': edge_prefix_str = 'RetweetUserConnections_' user_conn_filter = 'retweet' elif type_of_graph == 'user_conn_reply': edge_prefix_str = 'ReplyUserConnections_' user_conn_filter = 'reply' elif type_of_graph == 'user_conn_quote': edge_prefix_str = 'QuoteUserConnections_' user_conn_filter = 'quote' elif type_of_graph == 'ht_conn': edge_prefix_str = 'HTConnection_' export_type = 'ht_edges' print("** exporting edges - Graph type=" + type_of_graph ) # Export ALL edges for ALL periods print("** exporting edges for AllPeriods " + self.get_now_dt()) self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + 'AllPeriods_', 0, user_conn_filter=user_conn_filter, replace_existing_file=False) if bot_filter_fl == 'Y': # Export edges for ALL periods, excluding edges associated with bots print("** exporting edges for AllPeriods_ExcludingBots - " + self.get_now_dt()) self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + 'AllPeriods_ExcludingBots_', 0, is_bot_Filter = '0', user_conn_filter=user_conn_filter, replace_existing_file=False) # Export edges for ALL periods, only edges associated with bots print("** exporting edges for AllPeriods_BotsOnly - " + self.get_now_dt()) self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + 'AllPeriods_BotsOnly_', 0, is_bot_Filter = '1', user_conn_filter=user_conn_filter, replace_existing_file=False) # Export edges by period using the dates set on array period_arr if period_arr is not None: for idx, period in enumerate(period_arr): # Export ALL edges for this period print("** exporting edges for " + period[0] + " - " + self.get_now_dt()) edges = self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + '' + period[0] + '_', 0, startDate_filter=period[1], endDate_filter=period[2], is_bot_Filter=None, user_conn_filter=user_conn_filter, replace_existing_file=False) if bot_filter_fl == 'Y': # Export edges for this period, excluding edges associated with bots print("** exporting edges for " + period[0] + "_ExcludingBots - " + self.get_now_dt()) edges = self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + '' + period[0] + '_ExcludingBots_', 0, startDate_filter=period[1], endDate_filter=period[2], is_bot_Filter='0', user_conn_filter=user_conn_filter, replace_existing_file=False) # Export edges for this period, only edges associated with bots print("** exporting edges for " + period[0] + "_BotsOnly - " + self.get_now_dt()) edges = self.exportData(export_type, input_files_path + '\\' + edge_prefix_str + '' + period[0] + '_BotsOnly_', 0, startDate_filter=period[1], endDate_filter=period[2], is_bot_Filter='1', user_conn_filter=user_conn_filter, replace_existing_file=False) print("** exporting edges - END *** - " + self.get_now_dt()) ##################################### # Method: nodes_edges_analysis_files # Description: creates nodes and edges files def nodes_edges_analysis_files(self, G, path): """ Given a graph G, it exports nodes with they degree, edges with their weight, and word clouds representing the nodes scaled by their degree Parameters ---------- G : undirected networkx graph created from the Twitter data path : the path where the files should be saved Examples -------- Saved node and edges files into path: >>> nodes_edges_analysis_files(G, 'C:\\Data\\MyFilePath') """ print("****** Exporting nodes and edges to file - " + self.get_now_dt()) self.export_nodes_edges_to_file(G, path + "\\G_NodesWithDegree.txt", path + "\\G_Edges.txt") print("****** Ploting Nodes Wordcloud - " + self.get_now_dt()) node_file_name = path + '\\G_NodesWithDegree.txt' df = self.read_freq_list_file(node_file_name,' ') self.plot_word_cloud(df, file=path +'\\G_Nodes_WordCloud.png') print("\n") ##################################### # Method: lda_analysis_files # Description: creates topic model files # tweet texts, lda model visualization def lda_analysis_files(self, path, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): """ Creates topic model files. Export a files with tweet texts and a lda model visualization. The data comes from the mongoDB database and is filtered based on the parameters. Parameters ---------- path : the path where the files should be saved startDate_filter : (Optional) filter by a certain start date endDate_filter : (Optional) filter by a certain end date arr_edges : (Optional) and array of concatenated edges that will be used to filter certain connection only. the method concat_edges can be used to create that array. arr_ht_edges : (Optional) and array of concatenated hashtag edges that will be used to filter certain ht connection only. the method concat_edges can be used to create that array. Examples -------- Save lda analysis files into path: >>> lda_analysis_files( >>> 'D:\\Data\\MyFiles', >>> startDate_filter='09/20/2020 19:00:00', >>> endDate_filter='03/04/2021 00:00:00') """ #export text for topic analysis print("****** Exporting text for topic analysis - " + self.get_now_dt()) self.exportData('text_for_topics', path + "\\" , 0, startDate_filter, endDate_filter, self.is_bot_Filter, arr_edges, arr_ht_edges=arr_ht_edges, replace_existing_file=False) # Train LDA models and print topics print("****** Topic discovery analysis (lda model) ****** - " + self.get_now_dt()) model_name = "Topics" topics_file_name = path + '\\T_tweetTextsForTopics.txt' if not os.path.exists(path + '\\Topics-(LDA model).png'): self.train_model_from_file(topics_file_name, self.num_of_topics, model_name, model_type='lda') self.plot_topics(path + '\\Topics-(LDA model).png', self.num_of_topics, 'lda', replace_existing_file=False) ##################################### # Method: ht_analysis_files # Description: creates hashtag frequency files # frequency file text, wordcloud, and barcharts def ht_analysis_files(self, path, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): """ Creates hashtag frequency files. Frequency text file, wordcloud, and barcharts. The data comes from the mongoDB database and is filtered based on the parameters. Parameters ---------- path : the path where the files should be saved startDate_filter : (Optional) filter by a certain start date endDate_filter : (Optional) filter by a certain end date arr_edges : (Optional) and array of concatenated edges that will be used to filter certain connection only. the method concat_edges can be used to create that array. arr_ht_edges : (Optional) and array of concatenated hashtag edges that will be used to filter certain ht connection only. the method concat_edges can be used to create that array. Examples -------- Save hashtag frequency files into path: >>> ht_analysis_files( >>> 'D:\\Data\\MyFiles', >>> startDate_filter='09/20/2020 19:00:00', >>> endDate_filter='03/04/2021 00:00:00') """ #export ht frequency list print("\n****** Exporting ht frequency list - " + self.get_now_dt()) self.exportData('ht_frequency_list', path + "\\" , 0, startDate_filter, endDate_filter, self.is_bot_Filter, arr_edges, arr_ht_edges=arr_ht_edges, replace_existing_file=False) print("****** Ploting HashTags Barchart and Wordcloud - " + self.get_now_dt()) ht_file_name = path + '\\T_HT_FrequencyList.txt' if os.stat(ht_file_name).st_size != 0: df = self.read_freq_list_file(ht_file_name) self.plot_top_freq_list(df, 30, 'HashTag', exclude_top_no=0, file=path + '\\T_HT_Top30_BarChart.png', replace_existing_file=False) self.plot_top_freq_list(df, 30, 'HashTag', exclude_top_no=1, file=path + '\\T_HT_Top30_BarChart-(Excluding Top1).png', replace_existing_file=False) self.plot_top_freq_list(df, 30, 'HashTag', exclude_top_no=2, file=path + '\\T_HT_Top30_BarChart-(Excluding Top2).png', replace_existing_file=False) self.plot_word_cloud(df, file=path + '\\T_HT_WordCloud.png', replace_existing_file=False) ##################################### # Method: words_analysis_files # Description: creates words frequency files # frequency file text, wordcloud, and barcharts def words_analysis_files(self, path, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): """ Creates words frequency files. Frequency text file, wordcloud, and barcharts. The data comes from the mongoDB database and is filtered based on the parameters. Parameters ---------- path : the path where the files should be saved startDate_filter : (Optional) filter by a certain start date endDate_filter : (Optional) filter by a certain end date arr_edges : (Optional) and array of concatenated edges that will be used to filter certain connection only. the method concat_edges can be used to create that array. arr_ht_edges : (Optional) and array of concatenated hashtag edges that will be used to filter certain ht connection only. the method concat_edges can be used to create that array. Examples -------- Save words frequency files into path: >>> words_analysis_files( >>> 'D:\\Data\\MyFiles', >>> startDate_filter='09/20/2020 19:00:00', >>> endDate_filter='03/04/2021 00:00:00') """ #export words frequency list print("\n****** Exporting words frequency list - " + self.get_now_dt()) self.exportData('word_frequency_list', path + "\\" , 0, startDate_filter, endDate_filter, self.is_bot_Filter, arr_edges, arr_ht_edges, self.top_no_word_filter, replace_existing_file=False) print("****** Ploting Word Barchart and Wordcloud - " + self.get_now_dt()) word_file_name = path + '\\T_Words_FrequencyList.txt' if os.stat(word_file_name).st_size != 0: df = self.read_freq_list_file(word_file_name) self.plot_top_freq_list(df, 30, 'Word', exclude_top_no=0, file=path+'\\T_Words_Top30_BarChart.png', replace_existing_file=False) self.plot_word_cloud(df, file=path+'\\T_Words_WordCloud.png', replace_existing_file=False) ##################################### # Method: time_series_files # Description: creates time frequency files def time_series_files(self, path, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): """ Creates timeseries frequency files. Tweet count by day and hashcount count by day. The data comes from the mongoDB database and is filtered based on the parameters. Parameters ---------- path : the path where the files should be saved startDate_filter : (Optional) filter by a certain start date endDate_filter : (Optional) filter by a certain end date arr_edges : (Optional) and array of concatenated edges that will be used to filter certain connection only. the method concat_edges can be used to create that array. arr_ht_edges : (Optional) and array of concatenated hashtag edges that will be used to filter certain ht connection only. the method concat_edges can be used to create that array. Examples -------- Save timeseries frequency files into path: >>> time_series_files( >>> 'D:\\Data\\MyFiles', >>> startDate_filter='09/20/2020 19:00:00', >>> endDate_filter='03/04/2021 00:00:00') """ print("****** Exporting time series files - " + self.get_now_dt()) tweet_df = self.get_time_series_df(startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #plot time series for all tweets if not os.path.exists(path + '\\TS_TweetCount.png'): self.plot_timeseries(tweet_df, ['tweet', 'tweet_created_at'], path + '\\TS_TweetCount.png') #plot time series for top hashtags [1-5] if not os.path.exists(path + '\\TS_TweetCountByHT[1-5].png'): self.plot_top_ht_timeseries(top_no_start=1, top_no_end=5, file = path + '\\TS_TweetCountByHT[1-5].png', startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #plot time series for top hashtags [3-10] if not os.path.exists(path + '\\TS_TweetCountByHT[3-10].png'): self.plot_top_ht_timeseries(top_no_start=3, top_no_end=10, file = path + '\\TS_TweetCountByHT[3-10].png', startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) ##################################### # Method: ht_connection_files # Description: creates hashags graph connections files def ht_connection_files(self, path, startDate_filter=None, endDate_filter=None, arr_edges=None): print("****** Exporting ht connection files - " + self.get_now_dt()) #create file with ht edges self.exportData('ht_edges', path + "\\" , 0, startDate_filter, endDate_filter, self.is_bot_Filter, arr_edges) edge_file_path = path + "\\ht_edges.txt" G = self.loadGraphFromFile(edge_file_path) if len(G.edges) > 0: if len(G.edges) > 1000: G = self.largest_component_no_self_loops(G) else: G.remove_edges_from(nx.selfloop_edges(G)) for node in list(nx.isolates(G)): G.remove_node(node) print("HT graph # of Nodes " + str(len(G.nodes()))) print("HT graph # of Edges " + str(len(G.edges()))) self.graph_analysis_files(G, path, gr_prefix_nm = 'HTG_') #remove top hashtags if we want to ignore the top hashtags if self.top_ht_to_ignore is not None: G2 = G.copy() remove_name = '[WITHOUT(' arr_nodes = sorted(G2.degree(), key=lambda x: x[1], reverse=True) for ht, degree in arr_nodes[0:self.top_ht_to_ignore]: remove_name = remove_name + '-' + ht G2.remove_node(ht) remove_name = remove_name + ')]' if len(G2.edges) > 0: if len(G2.edges) > 1000: G2 = self.largest_component_no_self_loops(G2) else: G2.remove_edges_from(nx.selfloop_edges(G2)) for node in list(nx.isolates(G2)): G2.remove_node(node) print("HT graph # of Nodes " + str(len(G2.nodes()))) print("HT graph # of Edges " + str(len(G2.edges()))) self.graph_analysis_files(G2, path, gr_prefix_nm = 'HTG_' + remove_name + '_') ##################################### # Method: graph_analysis_files # Description: creates graphs files def graph_analysis_files(self, G, path, gr_prefix_nm=''): """ Plot graph analysis files for a given graph G. It uses the configuration set on the setConfigs method. Parameters ---------- G : undirected networkx graph created from the Twitter data path : the path where the files should be saved gr_prefix_nm: (Optional) a prefix to add to the graph name. (Default='') Examples -------- Create graph visualization files >>> graph_analysis_files(G, 'C:\\Data\\MyAnalysis\\', 'MyNameTest') """ if len(G.nodes()) > 0 and len(G.edges()) > 0: #plot graph print("\n****** Ploting graphs... *********** - " + self.get_now_dt()) # if not os.path.exists(path + '\\' + gr_prefix_nm + 'G_Graph.png') # and not os.path.exists(path + '\\' + gr_prefix_nm + 'G_Graph(WithoutScale).png'): if ((len(G.nodes()) <= self.graph_plot_cutoff_no_nodes \ or len(G.edges()) <= self.graph_plot_cutoff_no_edges) \ and len(G.edges()) != 0) \ or len(G.nodes()) <= MIN_NO_OF_NODES_TO_REDUCE_GRAPH: if len(G.edges()) < 450: v_scale = 0.01; v_k =0.7; v_iterations=100; v_node_size=2 elif len(G.edges()) < 5000: v_scale = 2; v_k = 0.6; v_iterations=200; v_node_size=0.8 elif len(G.edges()) < 10000: v_scale = 1; v_k = 0.1; v_iterations=200; v_node_size=0.6 elif len(G.edges()) >= 10000: v_scale = 1; v_k = 0.05; v_iterations=500; v_node_size=0.6 if self.create_graph_with_node_scale_flag == 'Y': G_to_plot, labels2, k = self.calculate_louvain_clustering(G) self.plotSpringLayoutGraph(G_to_plot, path + '\\' + gr_prefix_nm + 'G_Graph.png', v_scale, v_k, v_iterations, cluster_fl='Y', v_labels=list(list(labels2)), replace_existing_file=False) if self.create_graph_without_node_scale_flag == 'Y': self.plotSpringLayoutGraph(G, path + '\\' + gr_prefix_nm + 'G_Graph(WithoutScale).png', v_scale, v_k, v_iterations, cluster_fl='N', v_alpha=1, scale_node_size_fl='N', replace_existing_file=False) #plot reduced graph if self.create_reduced_graph_flag == 'Y': self.plot_graph_contracted_nodes(G, path + '\\' + gr_prefix_nm + 'G_Graph-(ReducedGraph).png') print("\n") ##################################### # Method: edge_files_analysis # Description: load graph from edge files and call methods to create all analysis # files for the main graph and for the graph of each period def edge_files_analysis(self, output_path): """ Automated way to generate all analysis files. It creates all folders, edge files, and any other files based on given settings. The setting of what files are interesting or not, should be set using the setConfigs method. Parameters ---------- output_path : the path where the files should be saved Examples -------- Create all analysis files and folder based on the configurations set on setConfigs: >>> edge_files_analysis('D:\\Data\\MyFiles') """ case_ht_str = '' if self.type_of_graph == 'ht_conn': case_ht_str = 'ht_' #Get the right edges file to import if self.is_bot_Filter is None: parent_path = output_path + '\\' + self.edge_prefix_str + 'All' edge_file_path = self.folder_path + '\\data_input_files\\' + self.edge_prefix_str + 'AllPeriods_' + case_ht_str + 'edges.txt' if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, type_of_graph=self.type_of_graph) elif self.is_bot_Filter == '0': parent_path = output_path + '\\' + self.edge_prefix_str + 'ExcludingBots' edge_file_path = self.folder_path + '\\data_input_files\\' + self.edge_prefix_str +'AllPeriods_ExcludingBots_' + case_ht_str + 'edges.txt' if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, bot_filter_fl='Y', type_of_graph=self.type_of_graph) elif self.is_bot_Filter == '1': parent_path = output_path + '\\' + self.edge_prefix_str + 'Bots_Edges_Only' edge_file_path = self.folder_path + '\\data_input_files\\' + self.edge_prefix_str + 'AllPeriods_BotsOnly_' + case_ht_str + 'edges.txt' if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, bot_filter_fl='Y', type_of_graph=self.type_of_graph) print(edge_file_path) self.create_path(output_path) # Load graph from edge file G = self.loadGraphFromFile(edge_file_path) # Call method to print all analysis files self.all_analysis_file(G, parent_path, startDate_filter=None, endDate_filter=None) # Run analysis by period using the dates set on array period_arr if self.period_arr is not None: # Creates a text file with the period information. # This is just so that whoever is looking at these folder can know what dates we used for each period myFile = open(output_path + '\\PeriodsInfo.txt', 'w', encoding="utf-8") with myFile: writer = csv.writer(myFile, delimiter='\t', lineterminator='\n') writer.writerows(self.period_arr) for idx, period in enumerate(self.period_arr): # Set the period information variables period_name = period[0] period_start_date = period[1] period_end_date = period[2] print("\n**********************************************************") print("************************** " + period_name + " ****************************\n" ) # Edge file path if self.is_bot_Filter is None: parent_path = output_path + "\\" + self.edge_prefix_str + "All_By_Period\\" + period_name edge_file_path = output_path + "\\data_input_files\\" + self.edge_prefix_str + period_name +"_" + case_ht_str + "edges.txt" if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, type_of_graph=self.type_of_graph) elif self.is_bot_Filter == '0': parent_path = output_path + "\\" + self.edge_prefix_str + "Excluding_Bots_By_Period\\" + period_name edge_file_path = output_path + "\\data_input_files\\" + self.edge_prefix_str + period_name + "_ExcludingBots_" + case_ht_str + "edges.txt" if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, bot_filter_fl='Y', type_of_graph=self.type_of_graph) elif self.is_bot_Filter == '1': parent_path = output_path + "\\" + self.edge_prefix_str + "Bots_Edges_Only_By_Period\\" + period_name edge_file_path = output_path + "\\data_input_files\\" + self.edge_prefix_str + period_name +"_BotsOnly_" + case_ht_str + "edges.txt" if not os.path.exists(edge_file_path): self.export_all_edges_for_input(period_arr=self.period_arr, bot_filter_fl='Y', type_of_graph=self.type_of_graph) # Create new path if it doesn't exist self.create_path(parent_path) #load graph from edge file G = self.loadGraphFromFile(edge_file_path) #call function to genrate all files for this graph self.all_analysis_file(G, parent_path, startDate_filter=period_start_date, endDate_filter=period_end_date) ##################################### # Method: all_analysis_file # Description: Calls method to create all files for full dataset, for top degree nodes, and for community graphs def all_analysis_file(self, G, output_path, startDate_filter=None, endDate_filter=None): #files for the main graph self.create_analysis_file(G, output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter) #files for the top nodes if self.create_top_nodes_files_flag == 'Y': self.top_nodes_analysis(G, output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter) #files for community nodes if self.create_community_files_flag == 'Y': self.commty_analysis_files(G, output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter) ##################################### # Method: create_analysis_file # Description: calls individual methods to create files on the settings def create_analysis_file( self, G, output_path, startDate_filter=None, endDate_filter=None, arr_edges=None): #export file with measures print("****** Graph Measures - " + self.get_now_dt()) self.print_Measures(G, fileName_to_print = output_path + "\\G_Measures-(All).txt") print("\n") arr_ht_edges = None if self.type_of_graph == 'ht_conn': arr_ht_edges = arr_edges arr_edges = None if len(G.edges()) != 0: #get largest connected component and export file with measures G = self.largest_component_no_self_loops(G) print("****** Largest Component Graph Measures - " + self.get_now_dt()) self.print_Measures(G, fileName_to_print = output_path + "\\G_Measures-(LargestCC).txt") print("\n") #export files with edges and degrees if self.create_nodes_edges_files_flag == 'Y': self.nodes_edges_analysis_files(G, output_path) #LDA Model if self.create_topic_model_files_flag == 'Y': self.lda_analysis_files(output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #export ht frequency list if self.create_ht_frequency_files_flag == 'Y': self.ht_analysis_files(output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #export words frequency list if self.create_words_frequency_files_flag == 'Y': self.words_analysis_files(output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #plot graph if self.create_graphs_files_flag == 'Y': self.graph_analysis_files(G, output_path) #time series if self.create_timeseries_files_flag == 'Y': self.time_series_files(output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) #hashtag connections if self.create_ht_conn_files_flag == 'Y' and self.type_of_graph != 'ht_conn': self.ht_connection_files(output_path, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges) ##################################### # Method: top_nodes_analysis # Description: calls methods to create files for each of the top degree nodes def top_nodes_analysis(self, G, output_path, startDate_filter=None, endDate_filter=None): # Choose which graph you want to run this for Graph_to_analyze = G.copy() top_degree_nodes = self.get_top_degree_nodes(Graph_to_analyze, self.top_degree_start, self.top_degree_end) #creates a folder to save the files for this analysis path = "Top_" + str(self.top_degree_start) + '-' + str(self.top_degree_end) self.create_path(output_path + '\\' + path) i = self.top_degree_end # loops through the top degree nodes, creates a subgraph for them and saves the results in a folder for x in np.flip(top_degree_nodes, 0): node = x[0] #creates a subgraph for this node G_subgraph = self.create_node_subgraph(Graph_to_analyze, node) G_subgraph_largestComponent = G_subgraph.copy() G_subgraph_largestComponent = self.largest_component_no_self_loops(G_subgraph_largestComponent) #creates a path to add the files for this node path_node = path + "\\" + str(i) + "-" + node self.create_path(output_path + '\\' + path_node) #get array with all edges for this top degree node if len(G_subgraph) > 1: arr_edges = self.concat_edges(G_subgraph) self.create_analysis_file(G_subgraph, output_path + '\\' + path_node, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges) i -= 1 ##################################### # Method: commty_analysis_files # Description: calls methods to create files for each of the communities found def commty_analysis_files(self, G, output_path, startDate_filter=None, endDate_filter=None): print("\n******************************************************") print("******** Louvain Communities ********") if len(G.edges()) != 0: # Choose which graph you want to run this for Graph_to_analyze = G.copy() #creates a folder to save the files for this analysis path = output_path + "\\Communities_(Louvain)" while os.path.exists(path): path = path + "+" self.create_path(path) #calculate louvain community for largest connected component Graph_to_analyze = self.largest_component_no_self_loops(Graph_to_analyze) Graph_to_analyze, labels, k = self.calculate_louvain_clustering(Graph_to_analyze) comm_att = 'community_louvain' #find the number of communities in the graph no_of_comm = max(nx.get_node_attributes(Graph_to_analyze, comm_att).values())+1 print("******************************************************") print("Total # of Communities " + str(no_of_comm)) #loop through the communities print they analysis files for commty in range(no_of_comm): #find subgraphs of this community G_subgraph = Graph_to_analyze.subgraph([n for n,attrdict in Graph_to_analyze.node.items() if attrdict [comm_att] == commty ]) #only cares about communities with more than 1 node if len(G_subgraph.edges()) >= self.commty_edge_size_cutoff: G_subgraph_largestComponent = G_subgraph.copy() G_subgraph_largestComponent = self.largest_component_no_self_loops(G_subgraph_largestComponent) #creates a path to add the files for this node path_community = path + "\\Community-" + str(commty+1) self.create_path(path_community) print("\n") print("******************************************************") print("****** Printing files for community " + str(commty+1) + " ******") #self.print_Measures(G_subgraph, False, False, False, False, fileName_to_print = path_community + '\\G_' + str(commty+1) + '_Measures.txt') print("\n") if len(G_subgraph) > 1: arr_edges = self.concat_edges(G_subgraph) self.create_analysis_file(G_subgraph, path_community, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges) ##################################### # Method: get_time_series_df # Description: query data in mongoDB for timeseries analysis def get_time_series_df( self, ht_arr=None, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): """ Method to query data in mongoDB for timeseries analysis given certain filters. It creates all folders, edge files, and any other files based on given settings. The setting of what files are interesting or not, should be set using the setConfigs method. Parameters ---------- ht_arr : array of hashtags to filter the data from startDate_filter : (Optional) filter by a certain start date endDate_filter : (Optional) filter by a certain end date arr_edges : (Optional) and array of concatenated edges that will be used to filter certain connection only. the method concat_edges can be used to create that array. arr_ht_edges : (Optional) and array of concatenated hashtag edges that will be used to filter certain ht connection only. the method concat_edges can be used to create that array. Examples -------- >>> ... """ df = pd.DataFrame() if ht_arr is not None: #get timeseries for each of the top hashtags for i, ht in enumerate(ht_arr): arrData, file = self.queryData(exportType='tweet_ids_timeseries', filepath='', inc=0, ht_to_filter=ht, startDate_filter=startDate_filter, endDate_filter=endDate_filter, is_bot_Filter=self.is_bot_Filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) tweet_df = pd.DataFrame(list(arrData)) tweet_df.columns = ['tweet_created_at', ht] df = pd.concat([df,tweet_df], axis=0, ignore_index=True) else: #get timeseries for all tweets arrData, file = self.queryData(exportType='tweet_ids_timeseries', filepath='', inc=0, startDate_filter=startDate_filter, endDate_filter=endDate_filter, is_bot_Filter=self.is_bot_Filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges) tweet_df = pd.DataFrame(list(arrData)) tweet_df.columns = ['tweet_created_at', 'tweet'] df = pd.concat([df,tweet_df], axis=0, ignore_index=True) return df ##################################### # Method: plot_top_ht_timeseries # Description: get top hashtags and plot their timeseries data def plot_top_ht_timeseries( self, top_no_start, top_no_end, file, startDate_filter=None, endDate_filter=None, arr_edges=None, arr_ht_edges=None): #get the top hashtags to plot ht_arr, f = self.queryData(exportType='ht_frequency_list', filepath='', inc=0, startDate_filter=startDate_filter, endDate_filter= endDate_filter, is_bot_Filter=self.is_bot_Filter, arr_edges=arr_edges, arr_ht_edges=arr_ht_edges, top_no_filter=top_no_end, include_hashsymb_FL=False) if len(ht_arr) < top_no_end: top_no_end = len(ht_arr) if len(ht_arr) == 0 or top_no_start >= top_no_end: return "" ht_arr = np.array(ht_arr) ht_arr = ht_arr[top_no_start-1:top_no_end,0] ht_arr = list(ht_arr) #get the time series data df = self.get_time_series_df(ht_arr=ht_arr, startDate_filter=startDate_filter, endDate_filter=endDate_filter, arr_edges=arr_edges) #plot timeseries graph arr_columns = ht_arr.copy() arr_columns.append('tweet_created_at') self.plot_timeseries(df, arr_columns, file) ##################################### # Method: plot_timeseries # Description: plot time series data def plot_timeseries(self, df, arr_columns, file): tweet_df = (df[arr_columns] .set_index('tweet_created_at') .resample('D') .count() ); ax = tweet_df.plot(figsize=(25,8)) ax.set_xlabel("Date") ax.set_ylabel("Tweet Count") plt.savefig(file, dpi=200, facecolor='w', edgecolor='w') #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window ##################################### # Method: eda_analysis # Description: Save EDA files def eda_analysis(self): """ Method to print a summary of the initial exploratory data analysis for any dataset. Examples -------- >>> # Create Exploratory Data Analysis files >>> eda_analysis() It includes the following metrics: + **Tweet counts**: The number of tweet document in the database, divided by the following categories. + Total Original Tweets: The number of tweet documents in the database that are original tweets. + Total Replies: The number of tweet documents in the database that are replies to another tweet. + Total Retweets: The number of tweet documents in the database that are retweets. + Total Tweets: The total number of tweet documents in the database. + **Tweet counts by language**: The number of tweets document for each language used in the tweets. + **Tweet counts by month**: The number of tweets document for each month/year. + **Tweet counts by file**: The number of tweets document imported from each of the json files. + **User counts**: The number of users in the database, divided by the following categories. + tweet: Users with at least one document in the database. + retweet: Users that were retweeted, but are not part of previous group. + quote: Users that were quoted, but are not part of previous groups. + reply: Users that were replied to, but are not part of previous groups. + mention: Users that were mentioned, but are not part of previous groups. + **All User Connections Graph**: The metrics for the graph created based on the users connecting by retweets, quotes, mentions, and replies. + # of Nodes: The total number of nodes in the graph. + # of Edges: The total number of edges in the graph. + # of Nodes of the largest connected components: The total number of nodes in the largest connected component of the graph. + # of Edges of the largest connected components: The total number of edges in the largest connected component of the graph. + # of Disconnected Graphs: The number of sub-graphs within the main graph that are not connected to each other. + # of Louvain Communities found in the largest connected component: The number of communities found in the largest connected component using the Louvain method. + Degree of the top 5 most connected users: List of the top 5 users with the highest degree. Shows the user screen name and respective degrees. + Average Node Degree of largest connected graph: The average degree of all nodes that are part of the largest connected component of the graph. + Plot of the Louvain community distribution: A barchart showing the node count distribution of the communities found with the Louvain method. + Disconnected graphs distribution: A plot of a graph showing the distribution of the disconnected graphs. It shows the total number of nodes and edges for each of the disconnected graphs. + **Mentions User Connections Graph**: The same metrics as the *All User Connections* graph, but only considering the connections made by mentions. + **Retweets User Connections Graph**: The same metrics as the *All User Connections* graph, but only considering the connections made by retweets. + **Replies User Connections Graph**: The same metrics as the *All User Connections* graph, but only considering the connections made by replies. + **HT Connection Graph**: The same metrics as the *All User Connections* graph, but only considering the connections made by hashtags. """ eda_folder = self.folder_path + '\\EDA' self.create_path(eda_folder) eda_file = open(eda_folder + '\\EDA.txt', 'w', encoding="utf-8") print("**** Tweet counts ******") eda_file.write("**** Tweet counts ******\n") arr, f = self.queryData(exportType='tweetCount', filepath='', inc=0) for x in arr: eda_file.write(str(x)) eda_file.write("\n") df = pd.DataFrame(arr) df.columns = ['', ''] print(df.to_string()) print("\n") print("**** Tweet counts by language ******") eda_file.write("\n**** Tweet counts by language ******\n") arr, f = self.queryData(exportType='tweetCountByLanguage', filepath='', inc=0) for x in arr: eda_file.write(str(x)) eda_file.write("\n") df = pd.DataFrame(arr) df.columns = ['', ''] print(df.to_string()) print("\n") print("**** Tweet counts by month ******") eda_file.write("\n**** Tweet counts by month ******\n") arr, f = self.queryData(exportType='tweetCountByMonth', filepath='', inc=0) for x in arr: eda_file.write(str(x)) eda_file.write("\n") df = pd.DataFrame(arr) df.columns = ['', '', ''] print(df.to_string()) print("\n") print("**** Tweet counts by file ******") eda_file.write("\n**** Tweet counts by file ******\n") arr, f = self.queryData(exportType='tweetCountByFile', filepath='', inc=0) for x in arr: eda_file.write(str(x)) eda_file.write("\n") df = pd.DataFrame(arr) df.columns = ['', ''] print(df.to_string()) print("\n") print("**** User counts ******") eda_file.write("\n**** User counts ******\n") arr, f = self.queryData(exportType='userCount', filepath='', inc=0) arr.sort() for x in arr: eda_file.write(str(x)) eda_file.write("\n") df = pd.DataFrame(arr) df.columns = ['', '', '', ''] print(df.to_string()) print("\n") # Graph EDA # Load graph from main edges file if it does not exist edge_file_path = self.folder_path + '\\data_input_files\\UserConnections_AllPeriods_edges.txt' if not os.path.exists(edge_file_path): self.export_all_edges_for_input(type_of_graph = 'user_conn_all') # types of graph arr_type_pre = [['UserConnections_', 'edges'], ['MentionUserConnections_','edges'], ['RetweetUserConnections_','edges'], ['ReplyUserConnections_','edges'], ['QuoteUserConnections_','edges'], ['HTConnection_', 'ht_edges']] # Loop through the type of graphs for i in range(len(arr_type_pre)): # find the edge file name edge_file_path = self.folder_path + '\\data_input_files\\' + arr_type_pre[i][0] + 'AllPeriods_' + arr_type_pre[i][1] + '.txt' # if the edge file already exists if os.path.exists(edge_file_path): print('\n\n*****************************************') print('**** ' + arr_type_pre[i][0] + ' Graph ******') # Construct the graph based on the edge file G = self.loadGraphFromFile(edge_file_path) # if the graph is not empty if len(G.nodes()) > 0 and len(G.edges()) > 0: # Plot distribution of the separate connected components print("**** Connected Components - Distribution ******") no_of_disc_g = self.plot_disconnected_graph_distr(G, file=eda_folder + '\\' + arr_type_pre[i][0] + 'ConnectedComponents-(Graphs).png') no_of_disc_g_gt50 = self.plot_disconnected_graph_distr(G, size_cutoff=50) #calculate louvein community clustering print("**** Calculating Community Distribution of the Largest Connected Component- (Louvain) ******") G2 = self.largest_component_no_self_loops(G) G2, labels, k = self.calculate_louvain_clustering(G2) self.plot_graph_att_distr(G2, 'community_louvain', title='Louvain Community Distribution for Largest Connected Component', file_name=eda_folder+'\\' + arr_type_pre[i][0] + 'community_louvain_dist.png') # Degree arrays arr = np.array(sorted(G2.degree(), key=lambda x: x[1], reverse=True)) #deg_mean = np.asarray(arr[:,1], dtype=np.integer).mean() # get the mean node degree of the nodes deg_mean = self.calculate_average_node_degree(G2) print(" # of Nodes " + str(len(G.nodes()))) print(" # of Edges " + str(len(G.edges()))) print(" # of Nodes - (Largest Connected Component) " + str(len(G2.nodes()))) print(" # of Edges - (Largest Connected Component) " + str(len(G2.edges()))) print(" # of Disconnected Graphs " + str(no_of_disc_g)) print(" # of Disconnected Graphs with 50 or more nodes " + str(no_of_disc_g_gt50)) print(" # of Communities found in the largest connected component " + str(k)) if len(arr) > 1: print(" Degree of top 1 most connected user " + str(arr[0])) if len(arr) > 2: print(" Degree of top 2 most connected user " + str(arr[1])) if len(arr) > 3: print(" Degree of top 3 most connected user " + str(arr[2])) if len(arr) > 4: print(" Degree of top 4 most connected user " + str(arr[3])) if len(arr) > 5: print(" Degree of top 5 most connected user " + str(arr[4])) print(" Average Node Degree of largest connected graph " + str(deg_mean)) eda_file.write("\n") eda_file.write('**** ' + arr_type_pre[i][0] + ' Graph ******') eda_file.write("\n") eda_file.write("# of Nodes " + str(len(G.nodes()))) eda_file.write("\n") eda_file.write("# of Edges " + str(len(G.edges()))) eda_file.write("\n") eda_file.write("# of Disconnected Graphs " + str(no_of_disc_g)) eda_file.write("\n") eda_file.write("# of Louvain Communities found in the largest connected component " + str(k)) eda_file.write("\n") if len(arr) > 1: eda_file.write("Degree of top 1 most connected user " + str(arr[0])) eda_file.write("\n") if len(arr) > 2: eda_file.write("Degree of top 2 most connected user " + str(arr[1])) eda_file.write("\n") if len(arr) > 3: eda_file.write("Degree of top 3 most connected user " + str(arr[2])) eda_file.write("\n") if len(arr) > 4: eda_file.write("Degree of top 4 most connected user " + str(arr[3])) eda_file.write("\n") if len(arr) > 5: eda_file.write("Degree of top 5 most connected user " + str(arr[4])) eda_file.write("\n") eda_file.write("\n") eda_file.write("Average Node Degree of largest connected graph " + str(deg_mean)) eda_file.write("\n") #close file eda_file.close() print("*** EDA - END *** - " + self.get_now_dt()) ##################################### # Method: print_top_nodes_cluster_metrics # Description: calculate clustering metrics for top degree nodes def print_top_nodes_cluster_metrics(self, G, top_degree_end, acc_node_size_cutoff=None): """ Calculates clustering metrics for top degree nodes Parameters ---------- G : undirected networkx graph created from the Twitter data top_degree_end : the number of top nodes to use for calculation acc_node_size_cutoff : (Optional) The average clustering coefficient metric can take a long time to run, so users can set a cutoff number in this parameter for the graph size that will decide if that metric will be printed or not depending on the graph size. (Default=None) Examples -------- Create graph visualization files >>> print_cluster_metrics( >>> G_Community, >>> G, >>> top_no=3, >>> acc_node_size_cutoff=None >>> ) """ exec_tm = 0 endtime = 0 starttime = 0 starttime = time.time() top_degree_nodes = self.get_top_degree_nodes(G, 1, top_degree_end) i = 1 # loops through the top degree nodes, creates a subgraph for them for x in top_degree_nodes: print("***** Cluster for top " + str(i) + " node") node = x[0] #creates a subgraph for this node G_subgraph = self.create_node_subgraph(G, node) starttime_met = time.time() # print metrics self.print_cluster_metrics(G_subgraph, G, top_no=3, acc_node_size_cutoff=acc_node_size_cutoff) endtime_met = time.time() exec_tm = exec_tm + (endtime_met - starttime_met) print("\n") i += 1 endtime = time.time() #exec_tm_total = endtime - starttime print("Execution Time: %s seconds " % (endtime - starttime - exec_tm)) ##################################### # Method: print_commty_cluster_metrics # Description: calls methods to create files for each of the communities found def print_commty_cluster_metrics(self, G, comm_att='community_louvain', ignore_cmmty_lt=0, acc_node_size_cutoff=None): """ Calculates clustering metrics for top degree nodes Parameters ---------- G : undirected networkx graph created from the Twitter data comm_att : (Optional) Possible values: 'community_louvain' or 'spectral_clustering'. (Default='community_louvain') ignore_cmmty_lt : (Optional) Number used to ignore small communitites. The logic will not calculate metrics for communities smaller than this number. (Default=0) acc_node_size_cutoff : (Optional) The average clustering coefficient metric can take a long time to run, so users can set a cutoff number in this parameter for the graph size that will decide if that metric will be printed or not depending on the graph size. (Default=None) Examples -------- >>> print_commty_cluster_metrics(G, 'community_louvain', '10') """ if len(G.edges()) != 0: # find the number of communities in the graph no_of_comm = max(nx.get_node_attributes(G, comm_att).values())+1 print("Total # of Communities " + str(no_of_comm)) print("******************************************************") print("*****" + comm_att + "******") print("\n") # loop through the communities print they analysis files no_of_comm_gt_cutoff = 0 for commty in range(no_of_comm): # find subgraphs of this community G_subgraph = G.subgraph([n for n,attrdict in G.node.items() if attrdict [comm_att] == commty ]) # ignore communities that are less than ignore_cmmty_lt if len(G_subgraph.nodes()) >= ignore_cmmty_lt: print("****Community #" + str(commty+1)) no_of_comm_gt_cutoff += 1 self.print_cluster_metrics(G_subgraph, G, top_no=3, acc_node_size_cutoff=acc_node_size_cutoff) print("\n") print("Total # of Communities with more than " + str(ignore_cmmty_lt) + ' nodes: ' + str(no_of_comm_gt_cutoff)) import os import json import datetime from pymongo import MongoClient import pymongo from pymongo.collation import Collation from time import strptime,sleep import datetime import re import nltk from nltk.corpus import words, stopwords, wordnet from nltk.tokenize import RegexpTokenizer from nltk.stem import PorterStemmer, WordNetLemmatizer from nltk import pos_tag from nltk.tokenize import word_tokenize import csv import string from sklearn.decomposition import NMF, LatentDirichletAllocation, TruncatedSVD from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer import itertools import requests from requests_oauthlib import OAuth1 dictionary_words = dict.fromkeys(words.words(), None) import pyphen pyphen_dic = pyphen.Pyphen(lang='en') stopWords = set(stopwords.words('english')) tokenizer = RegexpTokenizer(r'\w+') stemmer = PorterStemmer() lemmatiser = WordNetLemmatizer() stop = set(stopwords.words('english')) exclude = set(string.punctuation) lemma = WordNetLemmatizer() topic_doc_complete = [] class TwitterDB: """ TwitterDB class """ def __init__(self, mongoDB_database): #Inititalizing MongoDB collections self.db = mongoDB_database self.db.dc_bSettings = self.db.adm_dbSettings self.c_loadedFiles = self.db.adm_loadedFiles self.c_twitterSearches = self.db.adm_twitterSearches self.c_tweet = self.db.tweet self.c_focusedTweet = self.db.focusedTweet self.c_tweetWords = self.db.tweetWords self.c_tweetSentences = self.db.tweetSentences self.c_topicsByHashTag = self.db.topicsByHashTag self.c_tweetCountByFileAgg = self.db.agg_tweetCountByFile self.c_tweetCountByPeriodAgg = self.db.agg_tweetCountByMonth self.c_tweetCountByLanguageAgg = self.db.agg_tweetCountByLanguage self.c_tweetCountByUserAgg = self.db.agg_tweetCountByUser self.c_wordCountAgg = self.db.agg_wordCount self.c_hashTagCountAgg = self.db.agg_hashTagCount self.c_userLocationCountAgg = self.db.agg_userLocationCount self.c_loadStatus = self.db.adm_loadStatus self.c_htTopics = self.db.htTopics self.c_tweetHashTags = self.db.tweetHashTags self.c_tweetConnections = self.db.tweetConnections self.c_users = self.db.users self.c_tweetHTConnections = self.db.tweetHTConnections self.c_tweetHTConnections = self.db.tweetHTConnections self.c_searches = self.db.searches #temp collections to help with query performance self.c_tmpEdges = self.db.tmpEdges self.c_tmpEdgesTweetIds = self.db.tmpEdgesTweetIds self.c_tmpEdgesHTFreq = self.db.tmpEdgesHTFreq self.c_tmpEdgesWordFreq = self.db.tmpEdgesWordFreq # Put fields chosen into an array of fields. # These fields will be the ones used in the FocusedTweet collection strFocusedTweetFields="lang;retweet_count;in_reply_to_status_id_str;in_reply_to_screen_name" strFocusedTweetUserFields="name;screen_name;description;location;followers_count;friends_count;statuses_count;lang;verified" self.strFocusedTweetFields = strFocusedTweetFields self.strFocusedTweetFieldsArr = strFocusedTweetFields.split(";") self.strFocusedTweetUserFieldsArr = strFocusedTweetUserFields.split(";") # Create unique index on users table to only allow one users with same user_id and screen_name. # (Collation strength=2 garantees case insensitive) try: resp = self.c_users.create_index([('user_id_str', pymongo.ASCENDING), ('screen_name', pymongo.ASCENDING) ], unique = True, collation=Collation(locale="en_US", strength=2)) except Exception as e: print('Warning: Could not create a new index in users' + str(e)) # Create unique index on tweet table to make sure we don't store duplicate tweets try: resp = self.c_tweet.create_index([('id', pymongo.ASCENDING)], unique = True) except: pass def setFocusedDataConfigs(self, strFocusedTweetFields, strFocusedTweetUserFields): """ Twitter documents have an extensive number of fields. In order to focus only on the interesting pieces of information, this method allows you to choose which fields to keep. Parameters ---------- strFocusedTweetFields : fields that you find interesting in the Tweet object strFocusedTweetUserFields : fields that you find interesting in the User object Examples -------- Setting configurations to decide which fields to keep: >>> focusedTweetFields = 'lang;retweet_count;in_reply_to_screen_name' >>> focusedTweetUserFields = 'name;description;location;friends_count;verified' >>> setFocusedDataConfigs(focusedTweetFields, focusedTweetUserFields) """ # Put fields chosen into an array of fields. # These fields will be the ones used in the FocusedTweet collection self.strFocusedTweetFieldsArr = strFocusedTweetFields.split(";") self.strFocusedTweetUserFieldsArr = strFocusedTweetUserFields.split(";") ##################################### # Method: loadDocFromFile # Description: This method will load tweet .json files into the DB (tweet collection) # It goes through all .json files in the directory and load them one by one. # It also saves the files already loaded into the 'loadedFiles' collection # to make sure we don't load the same file twice # Parameters: # -directory = the directory where the files are stored def loadDocFromFile(self, directory): """ This method will load tweet .json files into the DB (tweet collection) It goes through all .json files in the directory and load them one by one. It also saves the files already loaded into the 'loadedFiles' collection to make sure we don't load the same file twice Parameters ---------- directory : the directory where the files are stored Examples -------- Import data from json files into a mongoDB database: >>> loadDocFromFile(json_file_path = 'C:\\Data\\My_JSON_Files_folder') """ seq_no = 1 starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ("loading process started..." + starttime) #find the current max sequence number select_cTweet = self.c_tweet.aggregate( [{"$group": {"_id": "seq_agg" , "count": { "$max": "$seq_no" } }}]) for tweetCount in select_cTweet: seq_no = tweetCount["count"] + 1 #loops through the files in the dictory for filename in os.listdir(directory): if filename.endswith(".json"): strpath = os.path.join(directory, filename) #find if file already loaded isFileLoaded = self.c_loadedFiles.count_documents({"file_path": strpath.replace("\\", "/") }) if isFileLoaded > 0: #if the processing of that file did not finish. Deletes every record for that file so we can start over select_cLoadedFiles = self.c_loadedFiles.find({ "file_path": strpath.replace("\\", "/")}) if select_cLoadedFiles[0]["end_load_time"] == "loading": self.c_tweet.delete_many({"file_path": strpath.replace("\\", "/")}) self.c_loadedFiles.delete_many({"file_path": strpath.replace("\\", "/")}) isFileLoaded=0 #if file has already been loaded, ignores the file if isFileLoaded == 0: #save path in loaded files collection to track which files have already been processed data_loadedfiles = '{"start_load_time":"' \ + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") \ + '","end_load_time":"' \ + "loading" \ + '","file_path":"' \ + strpath.replace("\\", "/") \ + '"}' self.c_loadedFiles.insert_one(json.loads(data_loadedfiles)) #open file and goes through each document to insert tweet into DB (inserts into tweet collection) with open(strpath, encoding="utf8") as f: for line in f: data = json.loads(line) #adding extra fields to document to suport future logic (processed_fl, load_time, file_path ) a_dict = {'processed_fl': 'N', 'seq_no': seq_no, 'seq_agg': "A", 'load_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'file_path': strpath.replace("\\", "/")} data.update(a_dict) #ignores documents that are just status if 'info' not in data: self.c_tweet.insert_one(data) seq_no = seq_no+1 #update end load time self.c_loadedFiles.update_one( { "file_path" : strpath.replace("\\", "/") }, { "$set" : { "end_load_time" : datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } }); continue else: print ("Error loading into tweet collection") try: resp = self.c_tweet.create_index([('seq_no', pymongo.ASCENDING)]) except Exception as e: print('Could not create index ' + str(e)) endtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ("loading process completed " + endtime) # this method will use Twitter API to extract data and save into DB # Parameters: twitterBearer = Bearer from you Twitter developer account # apiName = (30day/fullarchive) # devEnviroment = name of your deve enviroment # query = query to select data from Twitter API # dateStart = period start date # dateEnd = period end date # nextToken = token to start from # maxResults = maximum number of results that you want to return def extractDocFromAPI (self, twitterBearer, apiName, devEnviroment, query, dateStart, dateEnd, nextToken, maxResults): print("Code for extractDocFromAPI. Details for this code on https://git.txstate.edu/l-n63/CS7311 ") ##################################### # Method: loadCollection_UpdateStatus # Description: This method controls the progress the insertions into other collections. # It calls other methods to load the collections # It keeps the progress stored in the db, so that if something fails, # we can know where to start back up. # The progress is stored on collections "adm_loadStatus" # Parameters: # -collection_name = the collections you want to load. # (Options: focusedTweet, tweetWords, tweetHashTags and tweetConnections) # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) # -type_filter = used only for users collections. # (Options: tweet, retweet, quote, reply or mention) - Default = None def loadCollection_UpdateStatus(self, collection_name, inc, type_filter=None): starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ('loading process started (' + collection_name + ('-' + type_filter if type_filter is not None else '') + ')... ' + starttime) last_seq_no = -1 max_seq_no = 0 minV = 0 #get the max sequence number from the tweet collection select_cTweet = self.c_tweet.aggregate( [{"$group": {"_id": "seq_agg" , "count": { "$max": "$seq_no" } } } ]) for tweetCount in select_cTweet: max_seq_no = tweetCount["count"] #check if the process has already been run or not. This is to make sure we can restart a process from where we stopped if type_filter is not None: hasStarted = self.c_loadStatus.count_documents({"collection_name": collection_name, "type_filter": type_filter }) else: hasStarted = self.c_loadStatus.count_documents({"collection_name": collection_name }) if hasStarted > 0: select_cLoadStatus = self.c_loadStatus.find({"collection_name": collection_name }) if select_cLoadStatus[0]["status"] == "loading": last_seq_no = select_cLoadStatus[0]["min_seq"]-1 if collection_name == 'focusedTweet': self.c_focusedTweet.delete_many({ "seq_no" : { "$gte" : select_cLoadStatus[0]["min_seq"] } }) elif collection_name == 'tweetWords': self.c_tweetWords.delete_many({ "tweet_seq_no" : { "$gte" : select_cLoadStatus[0]["min_seq"] } }) elif collection_name == 'tweetHashTags': self.c_tweetHashTags.delete_many({ "tweet_seq_no" : { "$gte" : select_cLoadStatus[0]["min_seq"] } }) elif collection_name == 'tweetConnections': self.c_tweetConnections.delete_many({ "tweet_seq_no" : { "$gte" : select_cLoadStatus[0]["min_seq"] } }) elif collection_name == 'tweetHTConnections': self.c_tweetHTConnections.delete_many({ "tweet_seq_no" : { "$gte" : select_cLoadStatus[0]["min_seq"] } }) elif select_cLoadStatus[0]["status"] == "success": last_seq_no = select_cLoadStatus[0]["max_seq"] else: if type_filter is not None: data = '{"collection_name":"' + collection_name + '", "type_filter":"' + type_filter + '"}' else: data = '{"collection_name":"' + collection_name + '"}' doc = json.loads(data) self.c_loadStatus.insert_one(doc) # try: # loop through tweet sequence numbers to insert into DB. # The variable "inc" will dictate how many tweet we will isert at a time int DB minV = last_seq_no+1 while minV <= max_seq_no: if type_filter is not None: self.c_loadStatus.update_one( {"collection_name": collection_name, "type_filter": type_filter }, { "$set" : { "min_seq" : minV, "max_seq" : minV+inc, "status" : "loading" } } ) else: self.c_loadStatus.update_one( {"collection_name": collection_name }, { "$set" : { "min_seq" : minV, "max_seq" : minV+inc, "status" : "loading" } } ) if collection_name == 'focusedTweet': self.loadFocusedDataMinMax(minV, minV+inc) elif collection_name == 'tweetWords': self.breakTextIntoWords(minV, minV+inc) elif collection_name == 'tweetHashTags': self.loadTweetHashTagsMinMax(minV, minV+inc) elif collection_name == 'tweetConnections': self.loadTweetConnectionsMinMax(minV, minV+inc) elif collection_name == 'tweetHTConnections': self.loadTweetHTConnectionsMinMax(minV, minV+inc) elif collection_name == 'users': self.loadUsersDataMinMax(minV, minV+inc, type_filter) minV=minV+inc #if everyhting was successfull, saves status as "success" if type_filter is not None: self.c_loadStatus.update_one( {"collection_name": collection_name, "type_filter": type_filter }, { "$set" : { "max_seq" : max_seq_no, "status" : "success" } } ) else: self.c_loadStatus.update_one( {"collection_name": collection_name }, { "$set" : { "max_seq" : max_seq_no, "status" : "success" } } ) endtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ('loading process completed (' + collection_name + ')... ' + endtime) def cleanTweetText(self, text): """ Method used to clean the tweet message. Hashtags, user screen names, links, and special characters are removed. Parameters ---------- text : the string to clean Return -------- text_clean : the string after it's been cleaned Examples -------- >>> cleanTweetText('re The text to Clean for @Jane!!! :) #python') The text to Clean for """ text = text.replace("\\", "").replace('\"', "").replace("\r","") text = text.replace("\n","").replace("\t", "").rstrip() text = text.lower() # removing hashtahs, mentions and links from clean text text_clean = text.replace("http", " http").replace("#", " #") text_clean = text_clean.replace("@", " @").replace(" ", " ").strip() words = text_clean.split() text_clean = '' for word in list(words): if word[0:1] != '#' and word[0:1] != '@' and word[0:4] != 'http'and word[0:2] != 'rt': text_clean = text_clean + word + ' ' # removing apecial characters text_clean = text_clean.replace("\\", "").replace("@","").replace("!", "") text_clean = text_clean.replace("/", "").replace("*", "").replace("&amp;", "") text_clean = text_clean.replace("-", "").replace("~", "").replace("`", "") text_clean = text_clean.replace("#", "").replace("$", "").replace("…", "") text_clean = text_clean.replace("%", "").replace("^", "").replace("&", "") text_clean = text_clean.replace("(", "").replace(")", "").replace("—", "") text_clean = text_clean.replace("=", "").replace("+", "").replace("{", "") text_clean = text_clean.replace("}", "").replace("[", "").replace("“", "") text_clean = text_clean.replace("’", "").replace("]", "").replace("|", "") text_clean = text_clean.replace("'", "").replace('"', "").replace("?", "") text_clean = text_clean.replace(":", "").replace(";", "").replace("<", "") text_clean = text_clean.replace(">", "").replace(",", "").replace(".", "") text_clean = text_clean.replace("_", "").replace("\\\\", "") text_clean = text_clean.replace(" ", " ").strip() return text_clean ##################################### # Method: loadFocusedData # Description: This method will call loadCollection_UpdateStatus to load the focusedtweet collection # Parameter: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) def loadFocusedData(self, inc): """ Method to load focused data into mongoDB based on the configurations set on setFocusedDataConfigs. It creates collection tweetFocusedData Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean Examples -------- >>> loadFocusedData(50000) """ self.loadCollection_UpdateStatus('focusedTweet', inc ) ##################################### # Method: loadFocusedDataMinMax # Description: This method will load the focusedtweet collection with the interesting information we want to study # It filters by a interval number of tweets. # This is because loading everything at once might cause out of memory errors # Parameters: # -minV & maxV = the tweet seq_no interval you want to run this analysis for def loadFocusedDataMinMax(self, minV, maxV): file_data = [] select_cTweet = self.c_tweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) #select_cTweet = self.c_tweet.find({"seq_no":{ "$gt":2,"$lte":3}}) #loop through tweets for tweet in select_cTweet: #Get all the basic info about the tweet. (These will always be saved independet of configurations) seq_no = tweet['seq_no'] id_str = tweet['id_str'] created_at = datetime.datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S +0000 %Y") year = tweet['created_at'][26:30] month_name = tweet['created_at'][4:7] month_no = str(strptime(month_name,'%b').tm_mon) day = tweet['created_at'][8:10] user_id = tweet['user']['id_str'] ## *************************************************** ## *** Getting the text information from differnt fields and different formats **** # when the tweet is large, the full text is saved ion the field extended_tweet if 'extended_tweet' in tweet: text = tweet['extended_tweet']['full_text'] elif 'full_text' in tweet: text = tweet['full_text'] else: text = tweet['text'] text = text.replace("\\", "").replace('\"', "").replace("\r","") text = text.replace("\n","").replace("\t", "").rstrip() text_lower = text.lower() # text from the quoted text quote_text = "" if 'quoted_status' in tweet: if 'extended_tweet' in tweet['quoted_status']: quote_text = tweet['quoted_status']['extended_tweet']['full_text'] elif 'full_text' in tweet['quoted_status']: quote_text = tweet['quoted_status']['full_text'] else: quote_text = tweet['quoted_status']['text'] quote_text = quote_text.replace("\\", "").replace('\"', "").replace("\r","") quote_text = quote_text.replace("\n","").replace("\t", "").rstrip() quote_text = quote_text.lower() # text from original tweet if this is a retweet retweeted_text = "" if 'retweeted_status' in tweet: if 'extended_tweet' in tweet['retweeted_status']: retweeted_text = tweet['retweeted_status']['extended_tweet']['full_text'] elif 'full_text' in tweet['retweeted_status']: retweeted_text = tweet['retweeted_status']['full_text'] else: retweeted_text = tweet['retweeted_status']['text'] retweeted_text = retweeted_text.replace("\\", "").replace('\"', "").replace("\r","") retweeted_text = retweeted_text.replace("\n","").replace("\t", "").rstrip() retweeted_text = retweeted_text.lower() text_combined = text_lower + ' ' + quote_text text_combined_clean = self.cleanTweetText(text_combined) ''' # removing hashtahs, mentions and links from clean text text_combined_clean = text_combined.replace("http", " http").replace("#", " #") text_combined_clean = text_combined_clean.replace("@", " @").replace(" ", " ").strip() words = text_combined_clean.split() text_combined_clean = '' for word in list(words): if word[0:1] != '#' and word[0:1] != '@' and word[0:4] != 'http'and word[0:2] != 'rt': text_combined_clean = text_combined_clean + word + ' ' text_combined_clean = text_combined_clean.replace("\\", "").replace("@","").replace("!", "") text_combined_clean = text_combined_clean.replace("/", "").replace("*", "").replace("&amp;", "") text_combined_clean = text_combined_clean.replace("-", "").replace("~", "").replace("`", "") text_combined_clean = text_combined_clean.replace("#", "").replace("$", "").replace("…", "") text_combined_clean = text_combined_clean.replace("%", "").replace("^", "").replace("&", "") text_combined_clean = text_combined_clean.replace("(", "").replace(")", "").replace("—", "") text_combined_clean = text_combined_clean.replace("=", "").replace("+", "").replace("{", "") text_combined_clean = text_combined_clean.replace("}", "").replace("[", "").replace("“", "") text_combined_clean = text_combined_clean.replace("’", "").replace("]", "").replace("|", "") text_combined_clean = text_combined_clean.replace("'", "").replace('"', "").replace("?", "") text_combined_clean = text_combined_clean.replace(":", "").replace(";", "").replace("<", "") text_combined_clean = text_combined_clean.replace(">", "").replace(",", "").replace(".", "") text_combined_clean = text_combined_clean.replace("_", "").replace("\\\\", "") text_combined_clean = text_combined_clean.replace(" ", " ").strip() ''' ## *************************************************************************** ## *************************************************************************** ## *** Getting the hashtag information - (original tweets, and quotes) ht_children = [] def addHTToList(ht, type_ht): ht_children.append({ 'ht': ht, 'ht_lower': ht.lower(), 'type_ht' : type_ht }) # get Hashtags type_ht = 'original' if 'extended_tweet' in tweet: for gt_tweet in tweet['extended_tweet']['entities']['hashtags']: ht = gt_tweet['text'] addHTToList(ht,type_ht) else: for gt_tweet in tweet['entities']['hashtags']: ht = gt_tweet['text'] addHTToList(ht,type_ht) if 'quoted_status' in tweet: type_ht = 'quote' if 'extended_tweet' in tweet['quoted_status']: if 'entities' in tweet['quoted_status']['extended_tweet']: for gt_tweet in tweet['quoted_status']['extended_tweet']['entities']['hashtags']: ht = gt_tweet['text'] addHTToList(ht,type_ht) elif 'entities' in tweet['quoted_status']: for gt_tweet in tweet['quoted_status']['entities']['hashtags']: ht = gt_tweet['text'] addHTToList(ht,type_ht) ## *************************************************************************** # creating the json doc data = '{"id_str":"' + id_str + \ '", "text":"' + text + \ '", "text_lower":"' + text_lower + \ '", "quote_text":"' + quote_text + \ '", "retweeted_text":"' + retweeted_text + \ '", "text_combined":"' + text_combined + \ '", "text_combined_clean":"' + text_combined_clean + \ '", "year":"' + year + \ '", "month_name":"' + month_name + \ '", "month_no":"' + month_no + \ '", "day":"' + day + \ '", "user_id":"' + user_id + \ '", "hashtags":"' + "" + '"}' doc = json.loads(data) doc['hashtags'] = ht_children # ***** adding other fields to collection based on the list of fields from configuration - # (configuration is set on the instantiation of the class object) def addFieldToDoc(field_name, field_content): #if it is a string, clean tab and enter characters if isinstance(field_content,str): field_content.replace("\\", "").replace('\"', "").replace("\r","") field_content = field_content.replace("\n","").replace("\t", "").rstrip() if field_content is None: field_content = "None" a_dict = {field_name : field_content} doc.update(a_dict) # go through the list of fields from configuration and add to the document for i in self.strFocusedTweetFieldsArr: field_name = i field_content = tweet[i] addFieldToDoc(field_name, field_content) #go through the list of user fields from configuration and add to the document for i in self.strFocusedTweetUserFieldsArr: field_name = 'user_' + i field_content = tweet['user'][i] addFieldToDoc(field_name, field_content) # ************************** # add created_at a_dict = {'tweet_created_at': created_at} doc.update(a_dict) # add seq number to the end a_dict = {'seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) # Add this tweet doc to the array. the array of all # tweets will be used to insertMany into mongoDB file_data.append(doc) # insert records into collection try: self.c_focusedTweet.insert_many(file_data) except Exception as e: print("Error loading focused tweet | " +str(e) ) # Create indexes in collection. This will help performance later try: resp = self.c_focusedTweet.create_index([('seq_no', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in focusedTweet' + str(e)) try: resp = self.c_focusedTweet.create_index([('id_str', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in focusedTweet' + str(e)) ##################################### # Method: loadUsersData # Description: This method will call loadCollection_UpdateStatus to load the users collection # Users are store in different part of the tweet. # In the tweet itself, in the retweet branch, in the quote branch, in the field in_reply_to_user and in the mention branch. # Use parameter "user_type_filter" to select which type you want to load. # IMPORTANT: Types reply and mention do not contain full user information # This method also creates a index to prevent duplicate user information. # If a user already exists, it just rejects the insertion. # Parameters: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) # -user_type_filter = the type of user you want to load - # (Options: tweet, retweet, quote, reply and mention) def loadUsersData(self, inc, user_type_filter): """ Method to load user data into mongoDB. It creates collection users Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean user_type_filter : the type of user you want to load - (Options: tweet, retweet, quote, reply and mention) Examples -------- >>> loadUsersData(50000, 'tweet') """ self.loadCollection_UpdateStatus('users', inc, user_type_filter) ##################################### # Method: loadUsersDataMinMax # Description: This method will load the users collection # It filters by a interval number of tweets. # This is because loading everything at once might cause out of memory errors # Parameters: # -minV & maxV = the tweet seq_no interval you want to run this analysis for # -user_type_filter = the type of user you want to to load - # (Options: tweet, retweet, quote, reply and mention) def loadUsersDataMinMax(self, minV, maxV, user_type_filter): file_data = [] select_cTweet = self.c_tweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) # add another json record to the array of records to insert def addToList(user_type, user_id_str, screen_name, name, location, description, created_at, protected, verified, followers_count, friends_count, listed_count, favourites_count, statuses_count): location_clean = '' description_clean = '' if location is not None: location_clean = location.replace("\\", "").replace('\"', "").replace("\r","") location_clean = location_clean.replace("\n","").replace("\t", "").rstrip() if description is not None: description_clean = description.replace("\\", "").replace('\"', "").replace("\r","") description_clean = description_clean.replace("\n","").replace("\t", "").rstrip() if screen_name is None: screen_name = user_id_str data = '{"screen_name":"' + screen_name + '"}' doc = json.loads(data) add_col = {'user_id_str': user_id_str} doc.update(add_col) add_col = {'name': name} doc.update(add_col) add_col = {'user_created_at': created_at} doc.update(add_col) add_col = {'location': location} doc.update(add_col) add_col = {'location_clean': location_clean} doc.update(add_col) add_col = {'description': description} doc.update(add_col) add_col = {'description_clean': description_clean} doc.update(add_col) add_col = {'protected': protected} doc.update(add_col) add_col = {'verified': verified} doc.update(add_col) add_col = {'followers_count': followers_count} doc.update(add_col) add_col = {'friends_count': friends_count} doc.update(add_col) add_col = {'listed_count': listed_count} doc.update(add_col) add_col = {'favourites_count': favourites_count} doc.update(add_col) add_col = {'statuses_count': statuses_count} doc.update(add_col) add_col = {'user_type': user_type} doc.update(add_col) file_data.append(doc) #loop through tweets for tweet in select_cTweet: if user_type_filter == 'tweet': user_id_str = tweet['user']['id_str'] name = tweet['user']['name'] screen_name = tweet['user']['screen_name'] location = tweet['user']['location'] description = tweet['user']['description'] protected = tweet['user']['protected'] followers_count = tweet['user']['followers_count'] friends_count = tweet['user']['friends_count'] listed_count = tweet['user']['listed_count'] created_at = tweet['user']['created_at'] favourites_count =tweet['user']['favourites_count'] verified = tweet['user']['verified'] statuses_count = tweet['user']['statuses_count'] addToList(user_type_filter, user_id_str, screen_name, name, location, description, created_at, protected, verified, followers_count, friends_count, listed_count, favourites_count, statuses_count) #user from the retweet original tweet if user_type_filter == 'retweet': if 'retweeted_status' in tweet: if 'user' in tweet['retweeted_status']: user_id_str = tweet['retweeted_status']['user']['id_str'] name = tweet['retweeted_status']['user']['name'] screen_name = tweet['retweeted_status']['user']['screen_name'] location = tweet['retweeted_status']['user']['location'] description = tweet['retweeted_status']['user']['description'] protected = tweet['retweeted_status']['user']['protected'] followers_count = tweet['retweeted_status']['user']['followers_count'] friends_count = tweet['retweeted_status']['user']['friends_count'] listed_count = tweet['retweeted_status']['user']['listed_count'] created_at = tweet['retweeted_status']['user']['created_at'] favourites_count =tweet['retweeted_status']['user']['favourites_count'] verified = tweet['retweeted_status']['user']['verified'] statuses_count = tweet['retweeted_status']['user']['statuses_count'] addToList(user_type_filter, user_id_str, screen_name, name, location, description, created_at, protected, verified, followers_count, friends_count, listed_count, favourites_count, statuses_count) #user from the quoted tweet if user_type_filter == 'quote': if 'quoted_status' in tweet: if 'user' in tweet['quoted_status']: user_id_str = tweet['quoted_status']['user']['id_str'] name = tweet['quoted_status']['user']['name'] screen_name = tweet['quoted_status']['user']['screen_name'] location = tweet['quoted_status']['user']['location'] description = tweet['quoted_status']['user']['description'] protected = tweet['quoted_status']['user']['protected'] followers_count = tweet['quoted_status']['user']['followers_count'] friends_count = tweet['quoted_status']['user']['friends_count'] listed_count = tweet['quoted_status']['user']['listed_count'] created_at = tweet['quoted_status']['user']['created_at'] favourites_count =tweet['quoted_status']['user']['favourites_count'] verified = tweet['quoted_status']['user']['verified'] statuses_count = tweet['quoted_status']['user']['statuses_count'] addToList(user_type_filter, user_id_str, screen_name, name, location, description, created_at, protected, verified, followers_count, friends_count, listed_count, favourites_count, statuses_count) #in reply to user if user_type_filter == 'reply': if tweet['in_reply_to_user_id'] != None: user_id_str = tweet['in_reply_to_user_id_str'] screen_name = tweet['in_reply_to_screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) #find mentioned user if user_type_filter == 'mention': if 'extended_tweet' in tweet: for gt_tweet in tweet['extended_tweet']['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) else: for gt_tweet in tweet['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) #find retweets mentions if 'retweeted_status' in tweet: if 'extended_tweet' in tweet['retweeted_status']: if 'entities' in tweet['retweeted_status']['extended_tweet']: for gt_tweet in tweet['retweeted_status']['extended_tweet']['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) elif 'entities' in tweet['retweeted_status']: for gt_tweet in tweet['retweeted_status']['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) #find quote mentions if 'quoted_status' in tweet: #find mentions in a quote if 'extended_tweet' in tweet['quoted_status']: if 'entities' in tweet['quoted_status']['extended_tweet']: for gt_tweet in tweet['quoted_status']['extended_tweet']['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) elif 'entities' in tweet['quoted_status']: for gt_tweet in tweet['quoted_status']['entities']['user_mentions']: user_id_str = gt_tweet['id_str'] screen_name = gt_tweet['screen_name'] addToList(user_type_filter, user_id_str, screen_name, name=None, location=None, description=None, created_at=None, protected=None, verified=None, followers_count=None, friends_count=None, listed_count=None, favourites_count=None, statuses_count=None) # insert user into db try: self.c_users.insert_many(file_data, ordered=False) except Exception as e: if str(type(e).__name__) == "BulkWriteError": #igones if just failed when trying to insert duplicate users pass else: print('Error in insert many user - ' + str(type(e).__name__)) ##################################### # Method: loadTweetHashTags # Description: This method will call loadCollection_UpdateStatus to load the hashtag collection # Parameter: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) def loadTweetHashTags(self, inc): """ Method to load hashthas in a separate collection in mongoDB. It creates the tweetHashTags collection. Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean Examples -------- >>> loadTweetHashTags(50000) """ self.loadCollection_UpdateStatus('tweetHashTags', inc ) ##################################### # Method: loadTweetHashTagsMinMax # Description: This method will load the hashtags associated to each tweet # It filters by a interval number of tweets. # This is because loading everything at once might cause out of memory errors # Parameters: # -minV & maxV = the tweet seq_no interval you want to run this analysis for def loadTweetHashTagsMinMax(self, minV, maxV): file_data = [] select_cTweet = self.c_focusedTweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) # add another json record to the array of records to insert def addToList(id_str, type_ht, ht, ht_lower, created_at): #creating the json doc data = '{"tweet_id_str":"' + id_str + \ '", "type_ht":"' + type_ht + \ '", "ht":"' + ht + \ '", "ht_lower":"' + ht_lower + '"}' doc = json.loads(data) #add created_at a_dict = {'tweet_created_at': created_at} doc.update(a_dict) #add seq number to the end a_dict = {'tweet_seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) # Add this tweet doc to the array. the array of all tweets # will be used to insertMany into mongoDB file_data.append(doc) #loop through tweets for tweet in select_cTweet: id_str = tweet['id_str'] seq_no = tweet['seq_no'] created_at = tweet['tweet_created_at'] #get Hashtags if 'hashtags' in tweet: for gt_tweet in tweet['hashtags']: ht = gt_tweet['ht'] ht_lower = gt_tweet['ht_lower'] type_ht = gt_tweet['type_ht'] #creating the json doc data = '{"tweet_id_str":"' + id_str + \ '", "type_ht":"' + type_ht + \ '", "ht":"' + ht + \ '", "ht_lower":"' + ht_lower + '"}' doc = json.loads(data) #add created_at a_dict = {'tweet_created_at': created_at} doc.update(a_dict) #add seq number to the end a_dict = {'tweet_seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) # Add this tweet doc to the array. the array of all # tweets will be used to insertMany into mongoDB file_data.append(doc) # insert hashtags into db try: self.c_tweetHashTags.insert_many(file_data) except: print("Error loading c_tweetHashTags ") ##################################### # Method: loadTweetConnections # Description: This method will call loadCollection_UpdateStatus to load the tweetConnections collection # Parameter: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) def loadTweetConnections(self, inc): """ Method to load tweet connection in a separate collection in mongoDB. It creates the tweetConnections collection. Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean Examples -------- >>> loadTweetConnections(50000) """ self.loadCollection_UpdateStatus('tweetConnections', inc) ##################################### # Method: loadTweetConnectionsMinMax # Description: This method will load the tweet connections (edges) associated to each tweet # It filters by a interval number of tweets. # This is because loading everything at once might cause out of memory errors # Parameters: # -minV & maxV = the tweet seq_no interval you want to run this analysis for def loadTweetConnectionsMinMax(self, minV, maxV): file_data = [] user_id_str_b = '' desc = '' select_cTweet = self.c_tweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) # add another json record to the array of records to insert def addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=None, in_reply_to_status_id=None): if user_id_str_a is None: user_id_str_a = '' if user_id_str_b is None: user_id_str_b = '' if retweeted_status_id is None: retweeted_status_id = '' if quoted_status_id is None: quoted_status_id = '' if in_reply_to_status_id is None: in_reply_to_status_id = '' if screen_name_a is None: screen_name_a = user_id_str_a if screen_name_b is None: screen_name_b = user_id_str_b #to set the edge_screen_name_directed_key if screen_name_a > screen_name_b: screen_name_a_un = screen_name_a screen_name_b_un = screen_name_b else: screen_name_a_un = screen_name_b screen_name_b_un = screen_name_a #creating the json doc data = '{"tweet_id_str":"' + id_str + \ '", "type_of_connection":"' + type_conn + \ '", "user_id_str_a":"' + user_id_str_a + \ '", "screen_name_a":"' + screen_name_a + \ '", "user_id_str_b":"' + user_id_str_b + \ '", "screen_name_b":"' + screen_name_b + \ '", "desc":"' + desc + \ '", "retweeted_status_id":"' + str(retweeted_status_id) + \ '", "quoted_status_id":"' + str(quoted_status_id) + \ '", "in_reply_to_status_id":"' + str(in_reply_to_status_id) + \ '", "edge_screen_name_directed_key":"' + screen_name_a.lower() + '-' + screen_name_b.lower() + \ '", "edge_screen_name_undirected_key":"' + screen_name_a_un.lower() + '-' + screen_name_b_un.lower() + '"}' doc = json.loads(data) #add tweet_created_dt a_dict = {'tweet_created_at': tweet_created_dt} doc.update(a_dict) #add seq number to the end a_dict = {'tweet_seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) #add this tweet doc to the array. the array of all tweets will be used to insertMany into mongoDB file_data.append(doc) #loop through tweets for tweet in select_cTweet: #Get all the basic info about the tweet. id_str = tweet['id_str'] user_id_str_a = tweet['user']['id_str'] screen_name_a = tweet['user']['screen_name'] seq_no = tweet['seq_no'] tweet_created_dt = datetime.datetime.strptime(tweet['created_at'], "%a %b %d %H:%M:%S +0000 %Y") #find replies type_conn = 'reply' desc = 'user a replied to user b' if tweet['in_reply_to_status_id'] is not None or tweet['in_reply_to_user_id_str'] is not None: in_reply_to_status_id = tweet['in_reply_to_status_id_str'] user_id_str_b = tweet['in_reply_to_user_id_str'] screen_name_b = tweet['in_reply_to_screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=None, in_reply_to_status_id=in_reply_to_status_id) #find mentions type_conn = 'mention' desc = 'user a mentioned user b' if 'extended_tweet' in tweet: for gt_tweet in tweet['extended_tweet']['entities']['user_mentions']: user_id_str_b = gt_tweet['id_str'] screen_name_b = gt_tweet['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=None) else: for gt_tweet in tweet['entities']['user_mentions']: user_id_str_b = gt_tweet['id_str'] screen_name_b = gt_tweet['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=None) #find retweets if 'retweeted_status' in tweet: type_conn = 'retweet' desc = 'user a retweeted a tweet from user b' retweeted_status_id = tweet['retweeted_status']['id_str'] user_id_str_b = tweet['retweeted_status']['user']['id_str'] screen_name_b = tweet['retweeted_status']['user']['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=retweeted_status_id, quoted_status_id=None) #find quotes if 'quoted_status' in tweet: type_conn = 'quote' desc = 'user a quoted a tweet from user b' quote_status_id = tweet['quoted_status']['id_str'] user_id_str_b = tweet['quoted_status']['user']['id_str'] screen_name_b = tweet['quoted_status']['user']['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=quote_status_id) #find mentions in a quote type_conn = 'mention_quote' if 'extended_tweet' in tweet['quoted_status']: if 'entities' in tweet['quoted_status']['extended_tweet']: for gt_tweet in tweet['quoted_status']['extended_tweet']['entities']['user_mentions']: user_id_str_b = gt_tweet['id_str'] screen_name_b = gt_tweet['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=quote_status_id) elif 'entities' in tweet['quoted_status']: for gt_tweet in tweet['quoted_status']['entities']['user_mentions']: user_id_str_b = gt_tweet['id_str'] screen_name_b = gt_tweet['screen_name'] addToList(id_str, type_conn, user_id_str_a, screen_name_a, user_id_str_b, screen_name_b, desc, tweet_created_dt, retweeted_status_id=None, quoted_status_id=quote_status_id) # insert connections(directed edges) into db try: self.c_tweetConnections.insert_many(file_data) except: print("Error loading tweetConnections ") # create indexes to improve performance try: resp = self.c_tweetConnections.create_index([('tweet_id_str', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetConnections' + str(e)) try: resp = self.c_tweetConnections.create_index([('edge_screen_name_directed_key', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetConnections' + str(e)) try: resp = self.c_tweetConnections.create_index([('edge_screen_name_undirected_key', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetConnections' + str(e)) ##################################### # Method: loadTweetHTConnections # Description: This method will call loadCollection_UpdateStatus to load the tweetHTConnections collection # Parameter: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) def loadTweetHTConnections(self, inc): """ Method to load hashtag connection in a separate collection in mongoDB. It creates the tweetHTConnections collection. Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean Examples -------- >>> loadTweetHTConnections(50000) """ self.loadCollection_UpdateStatus('tweetHTConnections', inc) ##################################### # Method: loadTweetHTConnectionsMinMax # Description: This method will load the tweet hashtags connections (edges) associated to each hashtag for each tweet # It filters by a interval number of tweets. This is because loading everything at once might cause out of memory errors # Parameters: # -minV & maxV = the tweet seq_no interval you want to run this analysis for def loadTweetHTConnectionsMinMax(self, minV, maxV): file_data = [] select_cTweet = self.c_focusedTweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) #loop through tweets for tweet in select_cTweet: id_str = tweet['id_str'] seq_no = tweet['seq_no'] created_at = tweet['tweet_created_at'] #get Hashtags if 'hashtags' in tweet: #build array with all hashtags for this one tweet ht_arr = [] for gt_tweet in tweet['hashtags']: ht_arr.append(gt_tweet['ht_lower']) #loops through the combinations between the hashtags and insert one records for each combination for element in itertools.combinations(ht_arr, 2): if element[0] < element[1]: ht_a = element[0] ht_b = element[1] else: ht_a = element[1] ht_b = element[0] ht_key = ht_a + '-' + ht_b #creating the json doc data = '{"tweet_id_str":"' + id_str + \ '", "ht_a":"' + ht_a + \ '", "ht_b":"' + ht_b + \ '", "ht_key":"' + ht_key + '"}' doc = json.loads(data) #add created_at a_dict = {'tweet_created_at': created_at} doc.update(a_dict) #add seq number to the end a_dict = {'tweet_seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) #add this tweet doc to the array. the array of all tweets will be used to insertMany into mongoDB file_data.append(doc) #insert hashtags into db try: self.c_tweetHTConnections.insert_many(file_data) except: print("Error loading tweetHTConnections ") # create indexes to improve performance try: resp = self.c_tweetHTConnections.create_index([('tweet_id_str', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetHTConnections' + str(e)) try: resp = self.c_tweetHTConnections.create_index([('ht_key', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetHTConnections' + str(e)) ##################################### # Method: loadWordsData # Description: This method will call loadCollection_UpdateStatus to load the tweetWords collection # Parameters: # -inc = how many tweet records you want to load at the time. # (Large number may cause memory errors, low number may take too long to run) def loadWordsData(self, inc): """ Method to load the tweet words in a separate collection in mongoDB. It creates the tweetWords collection. Parameters ---------- inc : used to determine how many tweets will be processed at a time. A large number may cause out of memory errors, and a low number may take a long time to run, so the decision of what number to use should be made based on the hardware specification. the string to clean Examples -------- >>> loadWordsData(50000) """ self.loadCollection_UpdateStatus('tweetWords', inc ) ##################################### # Method: breakTextIntoWords # Description: This method will break text from tweet into words and tag them # It filters by a interval number of tweets. # This is because loading everything at once might cause out of memory errors # Parameters: minV & maxV = the tweet seq_no interval you want to run this analysis for def breakTextIntoWords(self, minV, maxV): file_data = [] seq_no = 0 select_cTweetWords = self.c_tweetWords.aggregate( [{"$group": {"_id": "seq_agg" , "maxSeqNo": { "$max": "$seq_no" } } } ]) for tweetCount in select_cTweetWords: max_seq_no = tweetCount["maxSeqNo"] seq_no = max_seq_no select_cFocusedTweet = self.c_focusedTweet.find({"seq_no":{ "$gt":minV,"$lte":maxV}}) #loop through tweets for tweet in select_cFocusedTweet: #Get all the basic info about the tweet. # (These will always be saved independet of configurations) id_str = tweet['id_str'] text = tweet['text_combined_clean'] year = tweet['year'] month_name = tweet['month_name'] month_no = tweet['month_no'] day = tweet['day'] user_id = tweet['user_id'] seq_no_tweet = tweet['seq_no'] created_at = tweet['tweet_created_at'] try: for word in pos_tag(tokenizer.tokenize(text)): cleanWordLw = word[0] stop_word_fl = 'F' if cleanWordLw in stopWords: stop_word_fl = 'T' en_word_fl = 'T' try: x = dictionary_words[cleanWordLw] except KeyError: en_word_fl = 'F' word_syl = pyphen_dic.inserted(cleanWordLw) seq_no = seq_no+1 #lemmatize word tag = word[1].lower()[0] if tag == 'j': tag = wordnet.ADJ elif tag == 'v': tag = wordnet.VERB elif tag == 'n': tag = wordnet.NOUN elif tag == 'r': tag = wordnet.ADV else: tag = '' if tag in ("j", "n", "v", "r"): lemm_word = lemmatiser.lemmatize(cleanWordLw, pos=tag) else: lemm_word = lemmatiser.lemmatize(cleanWordLw) data = '{"word":"' + cleanWordLw + \ '","word_tag":"' + word[1] + \ '","word_lemm":"' + lemm_word + \ '","word_syl":"' + word_syl + \ '","stop_word_fl":"' + stop_word_fl + \ '","en_word_fl":"' + en_word_fl + \ '","tweet_id_str":"' + id_str + \ '", "text":"' + text + \ '", "year":"' + year + \ '", "month_name":"' + month_name + \ '", "month_no":"' + month_no + \ '", "day":"' + day + \ '", "user_id":"' + user_id + '"}' doc = json.loads(data) #add created_at a_dict = {'tweet_created_at': created_at} doc.update(a_dict) a_dict = {'tweet_seq_no': seq_no_tweet, 'seq_no': seq_no, 'seq_agg': "A"} doc.update(a_dict) #add this tweet doc to the array. the array of all tweets will be used to insertMany into mongoDB file_data.append(doc) except Exception as e: print("Error on loadWordsData. " +str(e) + " | err tweet_id: " + id_str) #insert words into db try: self.c_tweetWords.insert_many(file_data) except Exception as e: print("Error on loadWordsData | " +str(e) ) # create index to improve performance try: resp = self.c_tweetWords.create_index([('tweet_seq_no', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetWords' + str(e)) try: resp = self.c_tweetWords.create_index([('tweet_id_str', pymongo.ASCENDING)]) except Exception as e: print('Could not create index in tweetWords' + str(e)) ##################################### # Method: loadAggregations # Description: load aggregations # Parameters: # -aggType = the type of aggreagation you want to run - # (Options: tweetCountByFile, hashtagCount, tweetCountByLanguageAgg, # tweetCountByMonthAgg, tweetCountByUser) def loadAggregations(self, aggType): """ Method to load addtional aggregated collection to MongoDB It creates the tweetWords collection. Parameters ---------- aggType : the type of aggreagation you want to run. (Options: tweetCountByFile, hashtagCount, tweetCountByLanguageAgg, tweetCountByMonthAgg, tweetCountByUser) Examples -------- >>> loadAggregations('tweetCountByFile') """ print ("loading " + aggType + " process started....") if (aggType == 'tweetCountByFile'): self.tweetCountByFileAgg() elif (aggType == 'hashtagCount'): self.hashtagCountAgg() elif (aggType == 'tweetCountByLanguageAgg'): self.tweetCountByLanguageAgg() elif (aggType == 'tweetCountByMonthAgg'): self.tweetCountByPeriodAgg() elif (aggType == 'tweetCountByUser'): self.tweetCountByUser() print ("loading " + aggType + " process completed.") ##################################### # Method: tweetCountByFileAgg # Description: load aggregation on tweetCountByFileAgg collection def tweetCountByFileAgg(self): #delete everything from the collection because we will repopulate it result = self.c_tweetCountByFileAgg.delete_many({}) select_cTweet = self.c_tweet.aggregate( [{"$group": {"_id": {"file_path": "$file_path"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cTweet: try: if tweetCount["_id"]["file_path"] is not None: data = '{"file_path":"' + tweetCount["_id"]["file_path"] + \ '", "count":"' + str(tweetCount["count"]) + '"}' x = json.loads(data) result = self.c_tweetCountByFileAgg.insert_one(x) except Exception as e: print("Error running aggregation: tweetCountByFile | " +str(e)) continue ##################################### # Method: hashtagCountAgg # Description: load aggregation on hashTagCountAgg collection def hashtagCountAgg(self): result = self.c_hashTagCountAgg.delete_many({}) select_cfocusedTweet = self.c_focusedTweet.aggregate( [ {"$unwind": '$hashtags'}, {"$project": { "hashtags": 1, "ht": '$hashtags.ht'} }, {"$group": { "_id": { "ht": '$hashtags.ht_lower' }, "count": { "$sum": 1 } } } ]) for tweetCount in select_cfocusedTweet: try: data = '{"hashtag":"' + tweetCount["_id"]["ht"] + '"}' x = json.loads(data) a_dict = {'count': tweetCount["count"]} x.update(a_dict) result = self.c_hashTagCountAgg.insert_one(x) except Exception as e: print("Error running aggregation: hashtagCount | " +str(e)) continue ##################################### # Method: tweetCountByLanguageAgg # Description: load aggregation on tweetCountByLanguageAgg collection def tweetCountByLanguageAgg(self): result = self.c_tweetCountByLanguageAgg.delete_many({}) select_cfocusedTweet = self.c_focusedTweet.aggregate( [{"$group": {"_id": {"lang": "$lang"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cfocusedTweet: try: data = '{"lang":"' + tweetCount["_id"]["lang"] + \ '", "count":"' + str(tweetCount["count"]) + '"}' x = json.loads(data) result = self.c_tweetCountByLanguageAgg.insert_one(x) except Exception as e: print("Error running aggregation: tweetCountByLanguageAgg | " +str(e)) continue ##################################### # Method: tweetCountByPeriodAgg # Description: load aggregation on tweetCountByPeriodAgg collection def tweetCountByPeriodAgg(self): result = self.c_tweetCountByPeriodAgg.delete_many({}) select_cfocusedTweet = self.c_focusedTweet.aggregate( [{"$group": {"_id": {"year": "$year", "month_no": "$month_no"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cfocusedTweet: try: data = '{"year":"' + tweetCount["_id"]["year"] + \ '","month_no":"' + tweetCount["_id"]["month_no"] + \ '", "count":"' + str(tweetCount["count"]) + '"}' x = json.loads(data) result = self.c_tweetCountByPeriodAgg.insert_one(x) except Exception as e: print("Error running aggreagation: tweetCountByPeriodAgg | " +str(e)) continue ##################################### # Method: tweetCountByUser # Description: load aggregation on tweetCountByUserAgg collection def tweetCountByUser(self): result = self.c_tweetCountByUserAgg.delete_many({}) select_cfocusedTweet = self.c_focusedTweet.aggregate( [{"$group": {"_id": {"user_id": "$user_id", "user_screen_name" : "$user_screen_name"}, "count": { "$sum": 1 } } } ], allowDiskUse = True, collation=Collation(locale="en_US", strength=2)) for tweetCount in select_cfocusedTweet: try: data = '{"user_id":"' + tweetCount["_id"]["user_id"] + \ '", "user_screen_name":"' + tweetCount["_id"]["user_screen_name"] + \ '", "count":"' + str(tweetCount["count"]) + '"}' x = json.loads(data) result = self.c_tweetCountByUserAgg.insert_one(x) except Exception as e: print("Error running aggregation: tweetCountByUser | " +str(e)) continue ##################################### # Method: create_tmp_edge_collections # Description: This method will create temporary collections to help improve # query performance when filtering data by a list of edges # Creating some temp collections, we can create indexes that will increase the lookup performance # This method was created to allow performance improvements # Parameters: # -arr_edges = the list of edges you want to search for - # (format "screen_name"-"screen_name") # -startDate_filter & endDate_filter = if you want to filter your query by a period - (Default=None) # -is_bot_Filter = if you want to filter by a connections being for a bot or not def create_tmp_edge_collections(self, arr_edges, arr_ht_edges, query_filter): if arr_ht_edges is not None: arr_edges = arr_ht_edges arr_ids = [] self.c_tmpEdges.delete_many({}) self.c_tmpEdgesTweetIds.delete_many({}) # *** creating tmp collection with given edges file_data = [] for x in arr_edges: data = '{"edge":"' + x + '"}' doc = json.loads(data) file_data.append(doc) self.c_tmpEdges.insert_many(file_data) resp = self.c_tmpEdges.create_index([('edge', pymongo.ASCENDING)]) #creating index on tmp collection # ********************** # *** creating tmp collection for tweet ids for the given edges if arr_edges is not None: pipeline = [ {"$lookup":{"from":"tweetConnections", "localField": "edge", "foreignField": "edge_screen_name_undirected_key", "as":"fromItems"}}, {"$unwind": "$fromItems" }, {"$match": query_filter }, {"$project": { "tweet_id_str": "$fromItems.tweet_id_str"} }] if arr_ht_edges is not None: pipeline = [ {"$lookup":{"from":"tweetHTConnections", "localField": "edge", "foreignField": "ht_key", "as" : "fromItems"}}, {"$unwind": "$fromItems" }, {"$match": query_filter }, {"$project": { "tweet_id_str": "$fromItems.tweet_id_str"} }] select = self.c_tmpEdges.aggregate(pipeline, allowDiskUse=True) for x in select: arr_ids.append(x['tweet_id_str']) file_data = [] arr_no_dups = list(dict.fromkeys(arr_ids)) for id_str in arr_no_dups : data = '{"tweet_id_str":"' + id_str + '"}' doc = json.loads(data) file_data.append(doc) # insert data into tmp collection if file_data != []: self.c_tmpEdgesTweetIds.insert_many(file_data) resp = self.c_tmpEdgesTweetIds.create_index([('tweet_id_str', pymongo.ASCENDING)]) # ****************************** ##################################### # Method: set_bot_flag_based_on_arr # Description: This method will update collections focusedTweet, users, # and tweetConnections to identify is a user or tweet connections are from bots. # The bot list is passed as parameter # Parameters: # -bots_list_id_str = a list of user_ids that are bots # -inc = how many tweets we want to update at the time for field is_bot_connection. # Default=10000 (High number might take too long to run) def set_bot_flag_based_on_arr(self, bots_list_id_str, inc=10000): """ Method to update MongoDb collection with a flag identifieng is a user is a bot or not. It updates the records based on a given list of user_ids. Parameters ---------- bots_list_id_str : and array with a list of Twitter user ids that are bots inc : (Optional) how many tweets we want to update at the time for field is_bot_connection. Default=10000 (High number might take too long to run) Examples -------- >>> arr_bots = ['123456', '1231654'] >>> set_bot_flag_based_on_arr(arr_bots, 20000) """ print("updating bot flag...") # set all records to be is_bot = 0 at first self.c_users.update_many({}, {"$set": {"is_bot": "0"}}) self.c_tweetConnections.update_many({}, {"$set": {"is_bot": "0"}}) self.c_tweetHTConnections.update_many({}, {"$set": {"is_bot": "0"}}) self.c_focusedTweet.update_many({}, {"$set": {"is_bot": "0", "is_bot_connection": "0"}}) self.c_tweetWords.update_many({}, {"$set": {"is_bot": "0", "is_bot_connection": "0"}}) #updates collections based on the given list of bots user_ids self.c_users.update_many({'user_id_str': {'$in': bots_list_id_str}}, {'$set': {'is_bot':'1'}}) self.c_focusedTweet.update_many({'user_id': {'$in': bots_list_id_str}}, {'$set': {'is_bot':'1'}}) self.c_tweetWords.update_many({'user_id': {'$in': bots_list_id_str}}, {'$set': {'is_bot':'1'}}) self.c_tweetConnections.update_many({'user_id_str_a': {'$in': bots_list_id_str}}, {'$set': {'is_bot':'1'}}) # **** Updating the tweets that are bots or connected to bots i=0; arr_bot_conn = [] #find all the ids that are connected to bots (replies, retweets, quotes or mentions) select = self.c_tweetConnections.find({"is_bot" : "1"}) for x in select: i = i + 1 arr_bot_conn.append(x['tweet_id_str']) # updating records using the $in operator can take a long time if the array is too big. That is why we do it in increments if i > inc: self.c_focusedTweet.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot_connection':'1'}}) self.c_tweetWords.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot_connection':'1'}}) self.c_tweetHTConnections.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot_connection':'1'}}) arr_bot_conn= []; i = 0 self.c_focusedTweet.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot_connection':'1'}}) self.c_tweetWords.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot_connection':'1'}}) self.c_tweetHTConnections.update_many({'id_str': {'$in': arr_bot_conn}}, {'$set': {'is_bot':'1'}}) # **************************** print("updating bot flag completed") # Method: build_filter # Description: Build filter for queries. # This is called by method queryData to create the filter that will by used in method # Parameters: # -startDate_filter & endDate_filter: coming from method queryData # -is_bot_Filter: coming from method queryData # -ht_to_filter: coming from method queryData # -user_conn_filter: coming from method queryData # -exportType: coming from method queryData def build_filter( self, startDate_filter=None, endDate_filter=None, is_bot_Filter=None, ht_to_filter=None, user_conn_filter=None, exportType=None): # set correct format for start and end dates if startDate_filter is not None and endDate_filter is not None: start_date = datetime.datetime.strptime(startDate_filter, '%m/%d/%Y %H:%M:%S') end_date = datetime.datetime.strptime(endDate_filter, '%m/%d/%Y %H:%M:%S') #set the comparison operator for bots queries if is_bot_Filter is not None: if is_bot_Filter == '0': bot_filter_comp_operator = "$and" elif is_bot_Filter == '1': bot_filter_comp_operator = "$or" #set up the query filter base on the given parameters date_filter = {} bot_filter = {} ht_filter = {} conn_filter = {} date_filter_for_edges = {} bot_filter_for_edges = {} ht_filter_for_edges = {} conn_filter_edges = {} #date filter if startDate_filter is not None and endDate_filter is not None: date_filter = { "tweet_created_at" : { "$gte": start_date, "$lt": end_date } } date_filter_for_edges = { "fromItems.tweet_created_at" : { "$gte": start_date, "$lt": end_date } } #bot filter if is_bot_Filter is not None: bot_filter = { "$or": [ { "is_bot": { "$eq": str(is_bot_Filter) } } , { "is_bot_connection": { "$eq": str(is_bot_Filter) } }]} bot_filter_for_edges = { "fromItems.is_bot": { "$eq": str(is_bot_Filter) } } #ht filter if ht_to_filter is not None: ht_filter = {"hashtags.ht_lower": ht_to_filter.lower()} ht_filter_for_edges = {} ##### ***need to address this later if user_conn_filter is not None: if exportType == 'edges': conn_filter = {"type_of_connection": user_conn_filter.lower()} conn_filter_edges = {"type_of_connection": user_conn_filter.lower()} query_filter = { "$and": [ date_filter, bot_filter, ht_filter, conn_filter ]} query_filter_for_edges = { "$and": [ date_filter_for_edges, bot_filter_for_edges, ht_filter_for_edges, conn_filter_edges ]} return query_filter, query_filter_for_edges ##################################### # Method: exportData # Description: Exports data into \t delimited file def exportData( self, exportType, filepath, inc, startDate_filter=None, endDate_filter=None, is_bot_Filter=None, arr_edges=None, arr_ht_edges=None, top_no_filter=None, ht_to_filter=None, include_hashsymb_FL='Y', replace_existing_file=True, user_conn_filter=None): """ Method to export the data from MongoDb into text files based on certain filters. Examples -------- >>> ... """ #export edges if (exportType == 'edges'): file = filepath + 'edges.txt' #export text for topic analysis elif (exportType == 'text_for_topics'): file = filepath + 'T_tweetTextsForTopics.txt' #export ht frequency list elif (exportType == 'ht_frequency_list'): file = filepath + 'T_HT_FrequencyList.txt' #export words frequency list - (TOP 5000) elif (exportType == 'word_frequency_list'): file = filepath + 'T_Words_FrequencyList.txt' #export text for topic analysis elif (exportType == 'tweet_ids_timeseries'): file = filepath + 'T_tweetIdswithDates.txt' #export tweetCountByUser elif (exportType == 'tweetCount'): file = filepath + 'tweetCount.txt' #export tweetCountByUser elif (exportType == 'userCount'): file = filepath + 'userCount.txt' #export tweetCountByUser elif (exportType == 'tweetCountByUser'): file = filepath + 'tweetCountByUser.txt' #export tweetCountByLanguage elif (exportType == 'tweetCountByLanguage'): file = filepath + '\\tweetCountByLanguage.txt' #export tweetCountByFile elif (exportType == 'tweetCountByFile'): file = filepath + 'tweetCountByFile.txt' #export tweetCountByMonth elif (exportType == 'tweetCountByMonth'): file = filepath + 'tweetCountByMonth.txt' #export hashtagCount elif (exportType == 'hashtagCount'): file = filepath + 'hashtagCount.txt' #export topics by hashtag elif (exportType == 'topicByHashtag'): file = filepath + 'topicByHashtag.txt' elif (exportType == 'ht_edges'): file = filepath + 'ht_edges.txt' #export tweetTextAndPeriod #if (exportType == 'tweetTextAndPeriod'): #export tweetDetails #if (exportType == 'tweetDetails'): #export words #if (exportType == 'wordsOnEachTweet'): #user details on Each Tweet #if (exportType == 'userDetailsOnEachTweet'): if replace_existing_file==True or not os.path.exists(file): arr, file = self.queryData(exportType, filepath, inc, startDate_filter, endDate_filter, is_bot_Filter, arr_edges, arr_ht_edges, top_no_filter, ht_to_filter, user_conn_filter=user_conn_filter) #export in array into txt file self.exportToFile(arr, file) ##################################### # Method: set_bot_flag_based_on_arr # Description: Exports data into \t delimited file # Parameters: # -exportType: (Options: edges, # text_for_topics, # ht_frequency_list, # word_frequency_list # tweetCountByUser # tweetCountByLanguage, # tweetCountByFile, # tweetCountByMonth, # hashtagCount, # tweetTextAndPeriod, # wordsOnEachTweet # userDetailsOnEachTweet) # -filepath: the file path where the files will be saved # -inc: To set how many lines per files we want to save. # This is for collection that have too many records to be saved. # Memory issues can happens if this number is too big # Only works when exporting for types tweetTextAndPeriod, wordsOnEachTweet, # userDetailsOnEachTweet, we can set how many lines per file # -startDate_filter & endDate_filter: Date period you want to filter the tweets by. # Only available for options "edges", "text_for_topics", # and "ht_frequency_list". (Defaul=None) # -is_bot_Filter: Filter tweets and connections by being bots or not. # Only available for options "edges", "text_for_topics", # and "ht_frequency_list". (Defaul=None) # -arr_edges: Filter tweet connections by this array of edges. # Only available for options "text_for_topics", # and "ht_frequency_list". (Defaul=None) # -top_no_filter: Filter top frequent words based on the number of this parameter. # Only available for option "word_frequency_list" (Defaul=None) def queryData( self, exportType, filepath, inc, startDate_filter=None, endDate_filter=None, is_bot_Filter=None, arr_edges=None, arr_ht_edges=None, top_no_filter=None, ht_to_filter=None, include_hashsymb_FL='Y', user_conn_filter=None): """ Method to query the data from MongoDb. The method return an array with the data retrieved from MongoDB. Examples -------- >>> ... """ arr = [] # set correct format for start and end dates if startDate_filter is not None and endDate_filter is not None: start_date = datetime.datetime.strptime(startDate_filter, '%m/%d/%Y %H:%M:%S') end_date = datetime.datetime.strptime(endDate_filter, '%m/%d/%Y %H:%M:%S') #build a variable with all filter based on parameters query_filter, query_filter_for_edges = self.build_filter(startDate_filter, endDate_filter, is_bot_Filter, ht_to_filter, user_conn_filter, exportType) #export edges if (exportType == 'edges'): pipeline = [ {"$match": query_filter }, {"$group": {"_id": {"screen_name_a": "$screen_name_a", "screen_name_b": "$screen_name_b"}, "count": { "$sum": 1 }}} ] #get data from database, loop through records and insert into array select_edges = self.c_tweetConnections.aggregate(pipeline, allowDiskUse=True, collation=Collation(locale="en_US", strength=2)) for x in select_edges: arr.append([ x["_id"]['screen_name_a'], x["_id"]['screen_name_b'], x['count']]) #set file path file = filepath + 'edges.txt' #export hashtag edges if (exportType == 'ht_edges'): #in case we don't have an array of edges to filter by if arr_edges is None: pipeline = [ {"$match": query_filter }, {"$group": {"_id": {"ht_a": "$ht_a", "ht_b": "$ht_b"}, "count": { "$sum": 1 }}} ] select_edges = self.c_tweetHTConnections.aggregate(pipeline, allowDiskUse=True) else: #create temp collection for edges self.create_tmp_edge_collections(arr_edges, arr_ht_edges, query_filter_for_edges) #create temp collection for ht self.c_tmpEdgesHTFreq.delete_many({}) pipeline = [ {"$lookup":{ "from": "tweetHTConnections", "localField": "tweet_id_str", "foreignField": "tweet_id_str", "as" : "tweetHTConnections"}}, {"$unwind": "$tweetHTConnections"}, {"$group": {"_id": {"ht_a": "$tweetHTConnections.ht_a", "ht_b": "$tweetHTConnections.ht_b"}, "count": { "$sum": 1 }}} ] select_edges = self.c_tmpEdgesTweetIds.aggregate(pipeline, allowDiskUse=True) #get data from database, loop through records and insert into array for x in select_edges: arr.append([x["_id"]['ht_a'], x["_id"]['ht_b'], x['count']]) #set file path file = filepath + 'ht_edges.txt' #export text for topic analysis if (exportType == 'text_for_topics'): #in case we don't have an array of edges to filter by if arr_edges is None and arr_ht_edges is None: select_texts = self.c_focusedTweet.find(query_filter, { "text_combined_clean": 1} ) #in case we have an array of edges to filter by else: self.create_tmp_edge_collections(arr_edges, arr_ht_edges, query_filter_for_edges) pipeline = [ {"$lookup":{ "from": "focusedTweet", "localField": "tweet_id_str", "foreignField": "id_str", "as" : "focusedTweet"}}, {"$unwind": "$focusedTweet" }, {"$project": { "text_combined_clean": "$focusedTweet.text_combined_clean" }}] select_texts = self.c_tmpEdgesTweetIds.aggregate(pipeline, allowDiskUse=True) #get data from database, loop through records and insert into array for x in select_texts: arr.append([x['text_combined_clean']]) #set file path file = filepath + 'T_tweetTextsForTopics.txt' #export ht frequency list if (exportType == 'ht_frequency_list'): #in case we don't have an array of edges to filter by if arr_edges is None and arr_ht_edges is None: pipeline = [ {"$match": query_filter }, { "$unwind": '$hashtags' }, {"$group": { "_id": { "ht": '$hashtags.ht' }, "count": { "$sum": 1 } } }] select_ht = self.c_focusedTweet.aggregate(pipeline, allowDiskUse=True, collation=Collation(locale="en_US", strength=2)) #in case we have an array of edges to filter by else: #************************************************************************************* # Creating a temporary collection with all hashtags for each tweet for the given edges # This is possible without creating temp collections, # but it was done this way to improve performance. # Running with a different Collation can take a LONG time - # (We need to run with Collation strength=2 to get canse insensitive counts ) #create temp collection for edges self.create_tmp_edge_collections(arr_edges, arr_ht_edges, query_filter_for_edges) #create temp collection for ht self.c_tmpEdgesHTFreq.delete_many({}) pipeline = [ {"$lookup":{ "from": "focusedTweet", "localField": "tweet_id_str", "foreignField": "id_str", "as" : "focusedTweet"}}, {"$unwind": "$focusedTweet" }, {"$unwind": '$focusedTweet.hashtags' }, {"$project": { "ht": '$focusedTweet.hashtags.ht', "tweet_id_str": '$tweet_id_str' } }] file_data = [] select_ht = self.c_tmpEdgesTweetIds.aggregate(pipeline, allowDiskUse=True) for x in select_ht: data = '{"tweet_id_str":"' + x['tweet_id_str'] + \ '", "ht":"' + x['ht'] + '"}' doc = json.loads(data) file_data.append(doc) if file_data != []: self.c_tmpEdgesHTFreq.insert_many(file_data) #************************************************************************************** #getting counts for each hashtag pipeline = [ {"$group": { "_id": { "ht": '$ht' }, "count": { "$sum": 1 } } }] select_ht = self.c_tmpEdgesHTFreq.aggregate(pipeline, allowDiskUse=True, collation=Collation(locale="en_US", strength=2)) hash_symbol = "#" if include_hashsymb_FL==False: hash_symbol="" #get data from database, loop through records and insert into array for x in select_ht: arr.append([hash_symbol + x['_id']['ht'], x['count']]) #sort array in count descending order def sortSecond(val): return val[1] arr.sort(key=sortSecond,reverse=True) if top_no_filter != None: arr = arr[:top_no_filter] #set file path file = filepath + 'T_HT_FrequencyList.txt' #export words frequency list - (TOP 5000) if (exportType == 'word_frequency_list'): # This variable will get set to True for options where we want to create # a separate tmp collection to save the words. # (This was done this way to allow some performance improvements) bln_GetWords_From_Text = False #in case we don't have an array of edges to filter by if arr_edges is None and arr_ht_edges is None: #if we are filtering by period and by is_bot if startDate_filter is not None and endDate_filter is not None and is_bot_Filter is not None: bln_GetWords_From_Text = True select_texts = self.c_focusedTweet.find(query_filter, { "text_combined_clean": 1, "id_str": 1} ) #if we are filtering by period only elif startDate_filter is not None and endDate_filter is not None: pipeline = [{"$match": {"$and": [{"tweet_created_at" : {"$gte": start_date, "$lt": end_date}}, {"stop_word_fl" : {"$eq": "F"} } ]}}, {"$group": {"_id": {"word": '$word'}, "count": {"$sum": 1}}}] select_word = self.c_tweetWords.aggregate(pipeline, allowDiskUse=True) #if we are filtering by is_bot elif is_bot_Filter is not None: #wrong bln_GetWords_From_Text = True select_texts = self.c_focusedTweet.find(query_filter, { "text_combined_clean": 1, "id_str": 1 }) #if there is no filter else: pipeline = [{"$match": {"stop_word_fl" : { "$eq": "F" }}}, {"$group": {"_id": {"word": '$word'}, "count": {"$sum": 1}}}] select_word = self.c_tweetWords.aggregate(pipeline, allowDiskUse=True) #in case we have an array of edges to filter by else: #************************************************************************************** # Creating a temporary collection with all hashtags for each tweet for the given edges # This is possible without creating temp collections, but it was done this way to improve performance. # Running with a different Collation can take a LONG time - # (We need to run with Collation strength=2 to get canse insensitive counts ) #create temp collection for edges self.create_tmp_edge_collections(arr_edges, arr_ht_edges, query_filter_for_edges) pipeline = [ {"$lookup":{ "from": "focusedTweet", "localField": "tweet_id_str", "foreignField": "id_str", "as" : "focusedTweet"}}, {"$unwind": "$focusedTweet" }, {"$project": {"id_str": "$tweet_id_str", "text_combined_clean": "$focusedTweet.text_combined_clean" }}] select_texts = self.c_tmpEdgesTweetIds.aggregate(pipeline, allowDiskUse=True) bln_GetWords_From_Text = True # If we want to create a tmp collection to save the words after spliting the words from text. # (This was done this way to allow some performance improvements) # this option is being used when we are filtering by is_bot or by edges if bln_GetWords_From_Text == True: self.c_tmpEdgesWordFreq.delete_many({}) file_data = [] for x in select_texts: for word in pos_tag(tokenizer.tokenize(x['text_combined_clean'])): if word[0] not in stopWords: data = '{"tweet_id_str":"' + x['id_str'] + \ '", "word":"' + word[0] + '"}' doc = json.loads(data) file_data.append(doc) if file_data != []: self.c_tmpEdgesWordFreq.insert_many(file_data) #************************************************************************************** #getting counts for each word pipeline = [ {"$group": { "_id": { "word": '$word' }, "count": { "$sum": 1 } } }] select_word = self.c_tmpEdgesWordFreq.aggregate(pipeline, allowDiskUse=True) #get data from database, loop through records and insert into array for x in select_word: arr.append([x['_id']['word'], x['count']]) #sort array in count descending order def sortSecond(val): return val[1] arr.sort(key=sortSecond,reverse=True) arr = arr[:top_no_filter] #set file path file = filepath + 'T_Words_FrequencyList.txt' #export text for topic analysis if (exportType == 'tweet_ids_timeseries'): #in case we don't have an array of edges to filter by if arr_edges is None and arr_ht_edges is None: select_ids = self.c_focusedTweet.find(query_filter, { "id_str": 1, "tweet_created_at": 1} ) #in case we have an array of edges to filter by else: self.create_tmp_edge_collections(arr_edges, arr_ht_edges, query_filter_for_edges) if ht_to_filter is None: pipeline = [ {"$lookup":{ "from": "focusedTweet", "localField": "tweet_id_str", "foreignField": "id_str", "as" : "focusedTweet"}}, {"$unwind": "$focusedTweet" }, {"$project": {"id_str": "$focusedTweet.id_str", "tweet_created_at": "$focusedTweet.tweet_created_at" }}] else: pipeline = [ {"$lookup":{ "from": "focusedTweet", "localField": "tweet_id_str", "foreignField": "id_str", "as" : "focusedTweet"}}, {"$unwind": "$focusedTweet" }, {"$match": {"focusedTweet.hashtags.ht_lower": ht_to_filter.lower()} }, {"$project": {"id_str": "$focusedTweet.id_str", "tweet_created_at": "$focusedTweet.tweet_created_at" }}] select_ids = self.c_tmpEdgesTweetIds.aggregate(pipeline, allowDiskUse=True) #get data from database, loop through records and insert into array for x in select_ids: arr.append([x['tweet_created_at'], x['id_str']]) #set file path file = filepath + 'T_tweetIdswithDates.txt' #export tweetCountByUser if (exportType == 'tweetCount'): total_tweets = 0 total_retweets = 0 total_replies = 0 select_cTweet = self.c_focusedTweet.aggregate([{"$match" : {"retweeted_text" : {"$ne": ""} }}, {"$group": {"_id": {"seq_agg": "$seq_agg"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cTweet: total_retweets = tweetCount["count"] select_cTweet = self.c_focusedTweet.aggregate([{"$group": {"_id": {"seq_agg": "$seq_agg"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cTweet: total_tweets = tweetCount["count"] select_cTweet = self.c_focusedTweet.aggregate([{"$match" : {"in_reply_to_screen_name" : {"$ne": "None"} }}, {"$group": {"_id": {"seq_agg": "$seq_agg"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cTweet: total_replies = tweetCount["count"] arr.append([ 'Total Original Tweets', str(total_tweets-total_retweets-total_replies)]) arr.append([ 'Total Replies', str(total_replies)]) arr.append([ 'Total Retweets', str(total_retweets)]) arr.append([ 'Total Tweets', str(total_tweets)]) #set file path file = filepath + 'tweetCount.txt' #export tweetCountByUser if (exportType == 'userCount'): tweet_user_count = 0 reply_user_count = 0 quote_user_count = 0 retweet_user_count = 0 select_cTweet = self.c_users.aggregate( [{"$group": {"_id": {"user_type": "$user_type"}, "count": { "$sum": 1 } } } ]) for tweetCount in select_cTweet: if tweetCount["_id"]["user_type"] == 'tweet': arr.append(['1', tweetCount["_id"]["user_type"], 'Users with at least one document in this db', str(tweetCount["count"]) ]) elif tweetCount["_id"]["user_type"] == 'retweet': arr.append([ '2', tweetCount["_id"]["user_type"], 'Users that were retweeted, but are not part of previous group', str(tweetCount["count"]) ]) elif tweetCount["_id"]["user_type"] == 'quote': arr.append([ '3', tweetCount["_id"]["user_type"], 'Users that were quoted, but are not part of previous groups', str(tweetCount["count"]) ]) elif tweetCount["_id"]["user_type"] == 'reply': arr.append([ '4', tweetCount["_id"]["user_type"], 'Users that were replied to, but are not part of previous groups', str(tweetCount["count"]) ]) elif tweetCount["_id"]["user_type"] == 'mention': arr.append([ '5', tweetCount["_id"]["user_type"], 'Users that were mentioned, but are not part of previous groups', str(tweetCount["count"]) ]) else: arr.append([ '6', tweetCount["_id"]["user_type"], '', str(tweetCount["count"]) ]) #set file path file = filepath + 'userCount.txt' #export tweetCountByUser if (exportType == 'tweetCountByUser'): #set header of txt file arr.append([ 'user_id', 'user_screen_name', 'count']) #get data from database and loop through records and insert into array select_tweetCountByUser = self.c_tweetCountByUserAgg.find() for x in select_tweetCountByUser: arr.append([ x['user_id'], x['user_screen_name'], x['count']]) #set file path file = filepath + 'tweetCountByUser.txt' #export tweetCountByLanguage if (exportType == 'tweetCountByLanguage'): #set header of txt file arr.append([ 'lang', 'count']) #get data from database and loop through records and insert into array select_tweetCountByLang = self.c_tweetCountByLanguageAgg.find() for x in select_tweetCountByLang: arr.append([ x['lang'], x['count']]) #set file path file = filepath + '\\tweetCountByLanguage.txt' #export tweetCountByFile if (exportType == 'tweetCountByFile'): #set header of txt file arr.append([ 'file_path', 'count']) #get data from database and loop through records and insert into array select_tweetCountByFile = self.c_tweetCountByFileAgg.find() for x in select_tweetCountByFile: arr.append([ x['file_path'], x['count']]) #set file path file = filepath + 'tweetCountByFile.txt' #export tweetCountByMonth if (exportType == 'tweetCountByMonth'): #set header of txt file arr.append([ 'year', 'month_no', 'count']) #get data from database and loop through records and insert into array select_tCountByPeriod = self.c_tweetCountByPeriodAgg.find() for x in select_tCountByPeriod: arr.append([ x['year'], x['month_no'], x['count']]) #set file path file = filepath + 'tweetCountByMonth.txt' #export hashtagCount if (exportType == 'hashtagCount'): #set header of txt file arr.append([ 'hashtag', 'count']) #get data from database and loop through records and insert into array select_hashtagCountByDay = self.c_hashTagCountAgg.find() for x in select_hashtagCountByDay: arr.append([ x['hashtag'], x['count']]) #set file path file = filepath + 'hashtagCount.txt' #export topics by hashtag if (exportType == 'topicByHashtag'): #set header of txt file arr.append([ 'ht', 'ht_count', 'lib', 'model', 'no_words', 'topic_no', 'topic']) #get data from database and loop through records and insert into array select_cHTTopics = self.c_htTopics.find() for x in select_cHTTopics: arr.append([ x['ht'], x['ht_count'], x['lib'], x['model'], x['no_tweets'], x['topic_no'], x['topic']]) #set file path file = filepath + 'topicByHashtag.txt' #export tweetTextAndPeriod if (exportType == 'tweetTextAndPeriod'): i = 0 #get data from database and loop through records and insert into array select_focusedTweet = self.c_focusedTweet.find() for x in select_focusedTweet: if (i % inc == 0 and i != 0): self.exportToFile(arr, file) #export in array into txt file if (i==0 or i % inc==0): arr = [] file = filepath + 'tweetTextAndPeriod_' + str(i) + '.txt' #set file path arr.append([ 'text', 'text_lower', 'year', 'month_no', 'day', 'user_id']) arr.append([ x['text'], x['text_lower'], x['year'], x['month_no'], x['day'], x['user_id']]) i = i +1 #export tweetDetails if (exportType == 'tweetDetails'): i = 0 #get data from database and loop through records and insert into array select_focusedTweet = self.c_focusedTweet.find() for x in select_focusedTweet: if (i % inc == 0 and i != 0): self.exportToFile(arr, file) #export in array into txt file if (i==0 or i % inc==0): arr = [] file = filepath + 'tweetTextAndPeriod_' + str(i) + '.txt' #set file path arr.append([ 'text', 'text_lower', 'year', 'month_no', 'day', 'user_id']) arr.append([ x['text'], x['text_lower'], x['year'], x['month_no'], x['day'], x['user_id']]) i = i +1 #export words if (exportType == 'wordsOnEachTweet'): i = 0 #get data from database select_tweetWords = self.c_tweetWords.find() for x in select_tweetWords: if (i % inc == 0 and i != 0): self.exportToFile(arr, file) #export in array into txt file if (i==0 or i % inc==0): arr = [] file = filepath + 'wordsOnEachTweet_' + str(i) + '.txt' #set file path arr.append(['word_orig', 'word', 'word_lower', 'word_tag', 'word_lemm', 'id_str', 'text', 'seq_no_tweet', 'seq_no']) arr.append([ x['word_orig'], x['word'], x['word_lower'], x['word_tag'], x['word_lemm'], x['id_str'], x['text'], x['seq_no_tweet'], x['seq_no']]) i = i +1 #user details on Each Tweet if (exportType == 'userDetailsOnEachTweet'): i = 0 #get data from database select_Tweet = self.c_tweet.find() for tweet in select_Tweet: if (i % inc == 0 and i != 0): self.exportToFile(arr, file) #export in array into txt file if (i==0 or i % inc==0): arr = [] file = filepath + 'userDetailsOnEachTweet_' + str(i) + '.txt' #set file path arr.append(['id_str', 'user_id', 'user_location', 'user_name', 'user_screen_name', 'user_description', 'user_verified', 'user_followers_count', 'user_friends_count', 'user_statuses_count', 'user_created_at', 'user_time_zone', 'user_lang', 'user_geo_enabled']) #get relevant information from tweet id_str = tweet['id_str'] user_id = tweet['user']['id_str'] user_location = tweet['user']['location'] user_name = tweet['user']['name'] user_screen_name = tweet['user']['screen_name'] user_description = tweet['user']['description'] user_verified = tweet['user']['verified'] user_followers_count = tweet['user']['followers_count'] user_friends_count = tweet['user']['friends_count'] user_statuses_count = tweet['user']['statuses_count'] user_created_at = tweet['user']['created_at'] user_time_zone = tweet['user']['time_zone'] user_lang = tweet['user']['lang'] user_geo_enabled = tweet['user']['geo_enabled'] if user_description is not None: user_description = user_description.replace("|", "").strip().replace("\n", "").replace("\r", "") if user_location is not None: user_location = user_location.replace("|", "").strip().replace("\n", "").replace("\r", "") if user_name is not None: user_name = user_name.replace("|", "").strip().replace("\n", "").replace("\r", "") if user_screen_name is not None: user_screen_name = user_screen_name.replace("|", "").strip().replace("\n", "").replace("\r", "") arr.append([id_str, user_id, user_location, user_name, user_screen_name, user_description, user_verified, user_followers_count, user_friends_count, user_statuses_count, user_created_at, user_time_zone, user_lang, user_geo_enabled]) i = i +1 #export in array into txt file #self.exportToFile(arr, file) return arr, file ##################################### # Method: exportToFile # Description: Method used to export an array to a t\ delimited file # Parameters: arrData = the array with the data you want to export # file = the path and name of the file you want to export def exportToFile(self, arrData, file): myFile = open(file, 'w', encoding="utf-8") with myFile: writer = csv.writer(myFile, delimiter='\t', lineterminator='\n') writer.writerows(arrData) ######### Topic Analysis ############################################### # *This was just an initital analysis. refer to pyTwitterTopics for more. ##################################### # Method: get_docs # Description: create one array with all tweets of one hashtag for topic analysis def get_docs(self, ht, max_doc_ctn): ctn=0 doc = "" topic_doc_complete.append(doc) select_cTweet = self.c_focusedTweet.find({"hashtags.ht_lower" : ht }) #loop through tweets for tweet in select_cTweet: if ctn < max_doc_ctn: doc = tweet['text_lower'] topic_doc_complete.append(doc) ctn=ctn+1 ##################################### # Method: clean_1 # Description: clean documents for topic analysis def clean_1(self, doc): stop_free = " ".join([i for i in doc.lower().split() if i not in stop]) punc_free = ''.join(ch for ch in stop_free if ch not in exclude) normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split()) return normalized #topic analysis using gensim model def gensim_model(self, num_topics_lda, num_topics_lsi, ht, tc): import gensim from gensim import corpora doc_clean = [self.clean_1(doc).split() for doc in topic_doc_complete] # Creating the term dictionary of our courpus, where every unique term is assigned an index. dictionary = corpora.Dictionary(doc_clean) dictionary = corpora.Dictionary(doc_clean) # Converting list of documents (corpus) into Document Term Matrix using dictionary prepared above. doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean] # Creating the object for LDA model using gensim library Lda = gensim.models.ldamodel.LdaModel # Build the LDA model lda_model = gensim.models.LdaModel(corpus=doc_term_matrix, num_topics=num_topics_lda, id2word=dictionary) # Build the LSI model lsi_model = gensim.models.LsiModel(corpus=doc_term_matrix, num_topics=num_topics_lsi, id2word=dictionary) file_data = [] for idx in range(num_topics_lda): topic = idx+1 strtopic = str(topic) data = '{"ht":"' + ht + \ '", "ht_count":"' + str(tc) + \ '", "lib":"' + "gensim" + \ '", "model":"' + "lda" + \ '", "no_tweets":"' + str(tc) + \ '", "topic_no":"' + strtopic + \ '", "topic":"' + str(lda_model.print_topic(idx, num_topics_lda)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) for idx in range(num_topics_lsi): data = '{"ht":"' + ht + \ '", "ht_count":"' + str(tc) + \ '", "lib":"' + "gensim" + \ '", "model":"' + "lsi" + \ '", "no_tweets":"' + str(tc) + \ '", "topic_no":"' + str(idx+1) +\ '", "topic":"' + str(lsi_model.print_topic(idx, num_topics_lsi)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) self.c_htTopics.insert_many(file_data) #topic analysis using sklearn model def skl_model(self, num_topics_lda, num_topics_lsi, num_topics_nmf, ht, tc): vectorizer = CountVectorizer(min_df=0.009, max_df=0.97, stop_words='english', lowercase=True, token_pattern='[a-zA-Z\-][a-zA-Z\-]{2,}') data_vectorized = vectorizer.fit_transform(topic_doc_complete) # Build a Latent Dirichlet Allocation Model lda_model = LatentDirichletAllocation(n_components=num_topics_lda, max_iter=5,learning_method='online',learning_offset=50.,random_state=0) lda_Z = lda_model.fit_transform(data_vectorized) # Build a Non-Negative Matrix Factorization Model nmf_model = NMF(num_topics_nmf) nmf_Z = nmf_model.fit_transform(data_vectorized) # Build a Latent Semantic Indexing Model lsi_model = TruncatedSVD(1) lsi_Z = lsi_model.fit_transform(data_vectorized) file_data = [] for idx, topic in enumerate(lda_model.components_): topic = str([( str(topic[i]) + "*" + vectorizer.get_feature_names()[i] + " + " ) for i in topic.argsort()[:-num_topics_lda - 1:-1]]).replace("[", "").replace("]", "").replace("'", "").replace(",", "") data = '{"ht":"' + ht + \ '", "ht_count":"' + tc + \ '", "lib":"' + "sklearn" + \ '", "model":"' + "lda" + \ '", "no_tweets":"' + str(tc) + \ '", "topic_no":"' + str(idx+1) +\ '", "topic":"' + topic + '"}' x = json.loads(data) file_data.append(x) for idx, topic in enumerate(lsi_model.components_): topic = str([( str(topic[i]) + "*" + vectorizer.get_feature_names()[i] + " + " ) for i in topic.argsort()[:-num_topics_lsi - 1:-1]]).replace("[", "").replace("]", "").replace("'", "").replace(",", "") data = '{"ht":"' + ht + \ '", "ht_count":"' + tc + \ '", "lib":"' + "sklearn" + \ '", "model":"' + "lsi" + \ '", "no_tweets":"' + str(tc) + \ '", "topic_no":"' + str(idx+1) +\ '", "topic":"' + topic + '"}' x = json.loads(data) file_data.append(x) for idx, topic in enumerate(nmf_model.components_): topic = str([( str(topic[i]) + "*" + vectorizer.get_feature_names()[i] + " + ") for i in topic.argsort()[:-num_topics_nmf - 1:-1]]).replace("[", "").replace("]", "").replace("'", "").replace(",", "") data = '{"ht":"' + ht + \ '", "ht_count":"' + tc + \ '", "lib":"' + "sklearn" + \ '", "model":"' + "nmf" + \ '", "no_tweets":"' + str(tc) + \ '", "topic_no":"' + str(idx+1) +\ '", "topic":"' + topic + '"}' x = json.loads(data) file_data.append(x) self.c_htTopics.insert_many(file_data) #find topics for each hashtag def findTopics(self, num_topics_lda, num_topics_lsi, num_topics_nmf, max_no_tweets_perHT, model): starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ("loading process started...." + starttime) #find all hashtag and their count select_cHashtagCount = self.c_hashTagCountAgg.find().sort("count", -1) try: #loop through hashtags for tweet in select_cHashtagCount: ht = tweet['hashtag'] count = tweet['count'] if ht != "metoo" and count > 500: #get all tweets for that hashtag topic_doc_complete.clear() self.get_docs(ht, max_no_tweets_perHT) #run topic models try: if model == "gensim": self.gensim_model(num_topics_lda, num_topics_lsi, ht, str(count)) elif model == "sklearn": self.skl_model(num_topics_lda, num_topics_lsi, num_topics_nmf, ht, str(count)) except Exception as e: print("Error finding topics for hashtag " + ht + ", using model " + model +". Err msg: " + str(e)) continue except Exception as e: print("Error finding topics. Err msg: " + str(e)) endtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print ("loading process completed. " + endtime) # search7dayapi def search7dayapi( self, consumer_key, consumer_secret, access_token, access_token_secret, query, result_type= 'mixed', max_count='100', lang='en'): """ Send requests to the 7-Day search API and save data into MongoDB Parameters ---------- consumer_key : User's consumer key consumer_secret : User's consumer secret access_token : User's access token access_token_secret : User's access token secret query : The query that will be used to filter the tweets result_type : Options: recent, popular, or mixed max_count : The number of tweets to be returned at a time lang : Language to filter the tweets Returns ------- response the response received from Twitter, which will contain either the tweets retrieved from the search, or the error message if any Examples -------- >>> # send request to 7-day search API >>> response = myAnalysisObj.search7dayapi( >>> consumer_key = '[key]', >>> consumer_secret = '[secret]', >>> access_token = '[token]', >>> access_token_secret = '[token_secret]', >>> query = 'austintexas OR atx OR austintx OR atxlife', >>> result_type = 'mixed', >>> max_count = '100', >>> lang = 'en') """ aut = OAuth1(consumer_key, consumer_secret, access_token, access_token_secret) endpoint = 'https://api.twitter.com/1.1/search/tweets.json?q=' + query + '&count=' + max_count + '&lang=' + lang + '&include_entities=true&tweet_mode=extended&result_type=' + result_type + '' response = requests.get(endpoint, auth=aut).json() # if there was an error, print error and end method if 'error' in response: print (response) return '' tweets = json.loads(json.dumps(response, indent = 2)) if 'search_metadata' in tweets: search = tweets['search_metadata'] else: search = {} # insert tweets into DB self.insertTweetToDBFromAPI(tweets, 'statuses', search, '7day') return response # searchPremiumAPI def searchPremiumAPI(self, twitter_bearer, api_name, dev_environment, query, date_start, date_end, next_token = None, max_count='100'): """ Send requests to the Premium search API and save data into MongoDB Parameters ---------- twitter_bearer : bearer authentication token created from the consumer_key and consumer_secret api_name : the options are either 30day or FullArchive dev_environment : the name of the environment created on the Twitter's developer account query : the query that will be used to filter the tweets date_start : the start date that will be used to filter the tweets. date_end : the end date that will be used to filter the tweets. next_token : then token that points to the previous search done with the same query. max_count : the number of tweets to be returned at a time Returns ------- response the response received from Twitter, which will contain either the tweets retrieved from the search, or the error message if any next_token token value that can be used for the next request, that way it is possible to avoid searches for the same records Examples -------- >>> # send request to premium API >>> response, next_token = myAnalysisObj.searchPremiumAPI( >>> twitter_bearer = '[bearer]', >>> api_name = '30day', >>> dev_environment = 'myDevEnv.json', >>> query = '(coronavirus OR COVID19) lang:en', >>> date_start = '202002150000', >>> date_end = '202002160000', >>> next_token = None, >>> max_count = '100' >>> ) """ headers = {"Authorization":"Bearer " + twitter_bearer + "", "Content-Type": "application/json"} endpoint = "https://api.twitter.com/1.1/tweets/search/" + api_name + "/" + dev_environment if next_token is None: data = '{"query":"' + query + '","fromDate":"' + date_start + '","toDate":"' + date_end + '", "maxResults":"' + max_count + '"}' else: data = '{"query":"' + query + '","fromDate":"' + date_start + '","toDate":"' + date_end +'", "next":"' + next_token + '", "maxResults":"' + max_count + '"}' response = requests.post(endpoint,data=data,headers=headers).json() # if there was an error, print error and end method if 'error' in response: print (response) return '' # load tweets into a json variable tweets = json.loads(json.dumps(response, indent = 2)) #Get "next"token if 'next' in tweets: next_token = tweets['next'] else: next_token = "" # save what information was used for this search search = json.loads(data) a_dict = {'next': next_token} search.update(a_dict) # insert tweets into DB self.insertTweetToDBFromAPI(tweets, 'results', search, api_name) return response, next_token # insertTweetToDB def insertTweetToDBFromAPI(self, tweets, parent_field, search, api): seq_no = 0 select_cTweet = self.c_tweet.aggregate( [{"$group": {"_id": "seq_agg" , "count": { "$max": "$seq_no" } } } ]) for tweetCount in select_cTweet: seq_no = tweetCount["count"] if parent_field in tweets: #Insert into searches table a_dict = {'search_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'api': api} search.update(a_dict) result = self.c_searches.insert_one(search) #Insert into tblTweet table for tweet in tweets[parent_field]: seq_no = seq_no + 1 #adding extra fields to document to suport future logic (processed_fl, load_time, file_path ) a_dict = {'processed_fl': 'N', 'seq_no': seq_no, 'seq_agg': "A", 'load_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} tweet.update(a_dict) try: result = self.c_tweet.insert_one(tweet) except: result = "" # create python script and .bat file for scheduled processing def create_bat_file_apisearch(self, mongoDBServer, mongoDBName, file_path, python_path, consumer_key, consumer_secret, access_token, access_token_secret, query, result_type='mixed', max_count='100', lang='en'): """ The method will create two files, one python script containing the code necessary to make the requests, and a *.bat* file that can be used to schedule the call of the python script. Parameters ---------- mongoDBServer : the mongoDB server that will be used to save the tweets mongoDBName : the mongoDB name that will be used to save the tweets file_path : the folder path where the files will be saved python_path : the path where the python.exe is installed consumer_key : User's consumer key consumer_secret : User's consumer secret access_token : User's access token access_token_secret : User's access token secret query : The query that will be used to filter the tweets result_type : Options: recent, popular, or mixed max_count : The number of tweets to be returned at a time lang : Language to filter the tweets Examples -------- >>> create python script and .bat file for scheduled processing >>> create_bat_file_apisearch( >>> mongoDBServer='mongodb://localhost:27017', >>> mongoDBName='myDBName', >>> file_path='C:\\Data\\myScriptsFolder\\MyScriptName.py', >>> python_path='C:\\Users\\Me\Anaconda3\envs\myEnv\python.exe', >>> consumer_key = '[key]', >>> consumer_secret = '[secret]', >>> access_token = '[token]', >>> access_token_secret = '[token_secret]', >>> query = 'austintexas OR atx OR austintx OR atxlife', >>> result_type = 'mixed', >>> max_count = '100', >>> lang = 'en') """ # create path is does not exist if not os.path.exists(os.path.dirname(file_path)): os.makedirs(os.path.dirname(file_path)) f = open(file_path, "w") f.write("import json\n") f.write("import requests\n") f.write("from pymongo import MongoClient\n") f.write("from requests_oauthlib import OAuth1\n") f.write("import datetime\n") f.write("\n") f.write("mongoDBServer = '" + mongoDBServer + "'\n") f.write("client = MongoClient(mongoDBServer)\n") f.write("db = client." + mongoDBName + "\n") f.write("\n") f.write("# Create unique index on tweet table to make sure we don't store duplicate tweets\n") f.write("try:\n") f.write(" resp = self.c_tweet.create_index([('id', pymongo.ASCENDING)],unique = True)\n") f.write("except:\n") f.write(" pass\n") f.write("\n") f.write("aut = OAuth1(" + "'" + consumer_key + "'" + "," + "'" + consumer_secret + "'" + "," + "'" + access_token + "'" + "," + "'" + access_token_secret + "'" + ")\n") f.write("\n") endpoint = 'https://api.twitter.com/1.1/search/tweets.json?q=' + query + '&count=' + max_count + '&lang=' + lang + '&include_entities=true&tweet_mode=extended&result_type=' + result_type + '' f.write("endpoint = " + "'" + endpoint + "'\n") f.write("\n") f.write("response = requests.get(endpoint, auth=aut).json()") f.write("\n") f.write("# if there was an error, print error and end method\n") f.write("if 'error' in response:\n") f.write(" print (response)\n") f.write(" \n") f.write("tweets = json.loads(json.dumps(response, indent = 2))\n") f.write("\n") f.write("search = tweets['search_metadata']\n") f.write("\n") f.write("seq_no = 0\n") f.write("select_cTweet = db.tweet.aggregate( [{'$group': {'_id': 'seq_agg' , 'count': { '$max': '$seq_no' } } } ])\n") f.write("for tweetCount in select_cTweet:\n") f.write(" seq_no = tweetCount['count']\n") f.write(" \n") f.write("if 'statuses' in tweets:\n") f.write(" \n") f.write(" #Insert into searches table\n") f.write(" a_dict = {'search_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'api': '7day'}\n") f.write(" search.update(a_dict)\n") f.write(" result = db.searches.insert_one(search)\n") f.write(" \n") f.write(" #Insert into tblTweet table\n") f.write(" for tweet in tweets['statuses']:\n") f.write(" \n") f.write(" seq_no = seq_no + 1\n") f.write(" \n") f.write(" #adding extra fields to document to suport future logic (processed_fl, load_time, file_path )\n") f.write(" a_dict = {'processed_fl': 'N', 'seq_no': seq_no, 'seq_agg': 'A', 'load_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") f.write(" tweet.update(a_dict)\n") f.write(" \n") f.write(" try:\n") f.write(" result = db.tweet.insert_one(tweet)\n") f.write(" except:\n") f.write(" result = ''\n") f.close() print(python_path) fbat = open(os.path.dirname(file_path) + '\\twitter_request_script.bat', "w") fbat.write('start ' + python_path + ' "' + file_path + '"') fbat.close() import os import csv import datetime import networkx as nx import numpy as np import numpy.linalg as la import community as community_louvain import matplotlib.pyplot as plt import matplotlib.cm as cm from networkx import algorithms from networkx.algorithms import distance_measures from networkx.algorithms.components import is_connected from networkx.algorithms.dominance import immediate_dominators import numpy as np from sklearn.cluster import SpectralClustering from sklearn import metrics import math import scipy as sp from scipy.sparse import csgraph import scipy.cluster.vq as vq import scipy.sparse.linalg as SLA import pandas as pd import seaborn as sns class TwitterGraphs: def __init__(self, folder_path): #creates path if does not exists if not os.path.exists(folder_path): os.makedirs(folder_path) self.folder_path = folder_path self.graph_details_file = folder_path + "\\log_graph_plots.txt" f = open(self.graph_details_file, "a") f.write('graph_name\t scale\t k\t iteration\t kmeans_k\t starttime\t endtime\t' + '\n') f.close() ##################################### # Method: loadGraphFromFile # Description: method receives nodes and edges files and returns an nwtworkx # Parameters: # -nodes_file = file path and file name for netwrok nodes # -edge_file = file path and file name for netwrok edges with weight def loadGraphFromFile(self, edge_file): G = nx.Graph() G = nx.read_edgelist(edge_file, data=(('weight',float),)) return G # function to plot network graph # Parameters: G - NetworkX graph # v_graph_name - The name of your graph # v_scale - Scale factor for positions. The nodes are positioned in a box of size [0,scale] x [0,scale]. # v_k - Optimal distance between nodes - Increase this value to move nodes farther apart. # v_iterations - Number of iterations of spring-force relaxation # cluster_fl - determines if you are sending labels for clusters or not # v_labels - cluster labels # kmeans_k - k_means k used for clustering # node_color - node color, default '#A0CBE2' # edge_color - edge color, default '#A79894' # width - with, default 0.05 # node_size - node size, default 0.6 # font_size - font size, default 1 # dpi - size of the image in dpi, default 800 # More details at: https://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.drawing.layout.spring_layout.html ''' def plotSpringLayoutGraph(self, G, v_graph_name, v_scale, v_k, v_iterations, cluster_fl='N', v_labels=None, kmeans_k='', v_node_color='#A0CBE2', v_edge_color='#A79894', v_width=0.05, v_node_size=0.6, v_font_size=1, v_dpi=900): ''' ##################################### # Method: plot_graph_att_distr # Description: Plot distribution of nodes based on graph attribute (e.g. communitiy) def plot_graph_att_distr(self, G, att, title='Community Counts', xlabel='Community ID', ylabel='Count', file_name=None, replace_existing_file=True): #create dataframe based on the given attribute df = pd.DataFrame.from_dict(nx.get_node_attributes(G, att), orient='index') df.columns = [att] df.index.rename('node' , inplace=True) sns.distplot(df[att], kde=False, bins=100) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) #if file name was give, save file in default folder if file_name != None: if replace_existing_file==True or not os.path.exists(file_name): plt.savefig(file_name) plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window ##################################### # Method: plot_disconnected_graph_distr # Description: plot the distribution of disconnected graphs def plot_disconnected_graph_distr(self, G, file=None, replace_existing_file=True, size_cutoff=None): sub_conn_graphs = sorted(list(nx.connected_component_subgraphs(G)), key = len, reverse=True) if size_cutoff is not None: sub_conn_graphs2 = sub_conn_graphs.copy() sub_conn_graphs = [] for x in sub_conn_graphs2: if len(x.nodes()) > size_cutoff: sub_conn_graphs.append(x) x = [] y = [] for i, a in enumerate(sub_conn_graphs): x.append(len(a.nodes())) y.append(len(a.edges())) fig, axs = plt.subplots(2, 3,figsize=(16,10)) try: axs[0, 0].plot(x, y, 'ro'); axs[0, 0].set_title('All subgraphs') x.pop(0); y.pop(0); axs[0, 1].plot(x, y, 'ro'); axs[0, 1].set_title('Excluding top1') x.pop(0); y.pop(0); axs[0, 2].plot(x, y, 'ro'); axs[0, 2].set_title('Excluding top2') x.pop(0); y.pop(0); axs[1, 0].plot(x, y, 'ro'); axs[1, 0].set_title('Excluding top3') x.pop(0); y.pop(0); axs[1, 1].plot(x, y, 'ro'); axs[1, 1].set_title('Excluding top4') x.pop(0); y.pop(0); axs[1, 2].plot(x, y, 'ro'); axs[1, 2].set_title('Excluding top5') except Exception as e: print("Warning: could not plot all 6 - " +str(e)) pass for ax in axs.flat: ax.set(xlabel='Nodes', ylabel='Edges') #if file name was give, save file in default folder if file != None: if replace_existing_file==True or not os.path.exists(file): plt.savefig(file) plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window return len(sub_conn_graphs) ##################################### # Method: contract_nodes_commty_per # Description: reduce graph based on a percentage given for each community found def contract_nodes_commty_per( self, G, perc, comm_att='community_louvain', enforce_ext_nodes_conn_fl ='N', commty_already_calculated='N'): G_to_contract = G.copy() all_nodes = [] #if we need to calculate the communities because the original report doesn't have the community labels if commty_already_calculated == 'N': G_to_contract, labels, k = self.calculate_louvain_clustering(G_to_contract) #find the number of communities in the graph no_of_comm = max(nx.get_node_attributes(G_to_contract, comm_att).values())+1 #loop through the communities and get the top nodes for each communities based on the given percentage for commty in range(no_of_comm): #find subgraphs of this community com_sub_graph = G_to_contract.subgraph([n for n,attrdict in G_to_contract.node.items() if attrdict [comm_att] == commty ]) arr_nodes = np.array(sorted(com_sub_graph.degree(), key=lambda x: x[1], reverse=True)) #get the comunity size and calculate how many top nodes we want to use based on the given percentage comm_size = len(com_sub_graph) top_nodes = math.ceil(comm_size*(1-(perc/100))) if top_nodes == 1: top_nodes=top_nodes+1 arr_top_nodes = arr_nodes[:top_nodes,0] if enforce_ext_nodes_conn_fl == 'Y': #create subgraph including external edges G_W_Ext_Edges = G_to_analyze.edge_subgraph(G_to_analyze.edges(com_sub_graph.nodes())) #find the nodes in this community with external edges G_edges_Rem = G_W_Ext_Edges.copy() G_edges_Rem.remove_edges_from(com_sub_graph.edges()) nodes_w_ext_edges = G_edges_Rem.edge_subgraph(G_edges_Rem.edges()).nodes() arr_top_nodes = np.concatenate((arr_top_nodes, nodes_w_ext_edges)) all_nodes = np.concatenate((all_nodes, arr_top_nodes)) #Create graph with only the contracted nodes G_Contracted = G_to_contract.subgraph(all_nodes) G_Contracted = self.largest_component_no_self_loops(G_Contracted) return G_Contracted ##################################### # Method: draw_scaled_labels # Description: draw labels in the graphs def draw_labels_for_node(self, G, nodes, size, pos): labels = {} for node in G.nodes(): if node in nodes: #set the node name as the key and the label as its value labels[node] = node nx.draw_networkx_labels(G, pos, labels, font_size=size) ##################################### # Method: draw_scaled_labels # Description: draw labels in the graph scaled by node degree def draw_scaled_labels(self, G, pos, default_font_size, font_size_multiplier): #get array of nodes in sorted order arr = np.array(sorted(G.degree(), key=lambda x: x[1], reverse=True)) #get the value of the highest degree. We will use this as reference to calculate the fonte sizes top_value = int(arr[:1,1]) nodes_with_same_font_size = [] for node, degree in arr: #calculate the font size for this node size_scale = (int(degree) * 1) / top_value new_font_size = size_scale*font_size_multiplier #if the calculate font size is greater than the parameter give, print that label. If not add to the array of nodes with the default size. if new_font_size > default_font_size: self.draw_labels_for_node(G, node, new_font_size, pos) else: nodes_with_same_font_size.append(node) #Print labels for all nodes with the default size. self.draw_labels_for_node(G, nodes_with_same_font_size, default_font_size, pos) ##################################### # Method: plotSpringLayoutGraph # Description: plot graph def plotSpringLayoutGraph( self, G, v_graph_name, v_scale, v_k, v_iterations, cluster_fl='N', v_labels=None, kmeans_k='', v_node_color='#A0CBE2', v_edge_color='#A79894', v_width=0.05, v_node_size=0.6, v_font_size=0.4, v_dpi=900, v_alpha=0.6, v_linewidths=0.6, scale_node_size_fl='Y', draw_in_mult_steps_fl='N', node_size_multiplier=6, font_size_multiplier=7, replace_existing_file=True): if replace_existing_file==True or not os.path.exists(v_graph_name): v_with_labels = True if scale_node_size_fl == 'Y': d = dict(G.degree) v_node_size = [(v * node_size_multiplier)/10 for v in d.values()] v_with_labels = False #node_color=v_labels, #node_color=v_node_color, #draw graph pos=nx.spring_layout(G, scale=v_scale, k=v_k, iterations=v_iterations) #G is my graph if cluster_fl == 'N': nx.draw(G, pos, width=v_width, edge_color=v_edge_color, node_color=v_node_color, edge_cmap=plt.cm.Blues, with_labels=v_with_labels, node_size=v_node_size, font_size=v_font_size, linewidths=v_linewidths, alpha=v_alpha) else: nx.draw(G, pos, node_color=v_labels, edge_color=v_edge_color, width=v_width, cmap=plt.cm.viridis, edge_cmap=plt.cm.Purples, with_labels=v_with_labels, node_size=v_node_size, font_size=v_font_size, linewidths=v_linewidths, alpha=v_alpha) # draw labels - logic to print labels in nodes in case we # want to change the font size to match the scale of the node size if scale_node_size_fl == 'Y': self.draw_scaled_labels(G, pos, v_font_size, font_size_multiplier) plt.savefig(v_graph_name, dpi=v_dpi, facecolor='w', edgecolor='w') plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window ##################################### # Method: largest_component_no_self_loops # Description: remove self loops nodes, isolate nodes and exclude smaller components def largest_component_no_self_loops(self, G): G2 = G.copy() G2.remove_edges_from(nx.selfloop_edges(G2)) for node in list(nx.isolates(G2)): G2.remove_node(node) graphs = sorted(list(nx.connected_component_subgraphs(G2)), key = len, reverse=True) #G = max(nx.connected_components(G), key=len) if len(graphs) > 0: return graphs[0] else: return G2 ##################################### # Method: export_nodes_edges_to_file # Description: export nodes and edges of a graph into a file def export_nodes_edges_to_file(self, G, node_f_name, edge_f_name): nx.write_edgelist(G, edge_f_name) np.savetxt(node_f_name, np.array(sorted(G.degree(), key=lambda x: x[1], reverse=True)), fmt="%s", encoding="utf-8") ##################################### # Method: create_node_subgraph # Description: creates a subgraph for one node. # subgraph contains all nodes connected to that node and their edges to each other def create_node_subgraph(self, G, node): G_subgraph_edges = nx.Graph() G_subgraph_edges = G.edge_subgraph(G.edges(node)) G_subgraph = G.subgraph(G_subgraph_edges.nodes()) return G_subgraph ##################################### # Method: get_top_degree_nodes # Description: returns a array of the top degree nodes based on parameter passed by user def get_top_degree_nodes(self, G, top_degree_start, top_degree_end): return np.array(sorted(G.degree(), key=lambda x: x[1], reverse=True))[top_degree_start-1:top_degree_end] ##################################### # Method: calculate_spectral_clustering_labels # Description: calculate cluster labels for a graph def calculate_spectral_clustering_labels(self, G, k, affinity = 'precomputed', n_init=100): #adj_mat = nx.to_numpy_matrix(G) adj_mat = nx.to_scipy_sparse_matrix(G) sc = SpectralClustering(k, affinity=affinity, n_init=n_init) sc.fit(adj_mat) return sc.labels_ ##################################### # Method: calculate_spectral_clustering # Description: calculate cluster labels for a graph def calculate_spectral_clustering(self, G, k=None, affinity = 'precomputed', n_init=100): #calculate adjacent matrix adj_mat = nx.to_scipy_sparse_matrix(G) #get number of clusters if None was given if k == None: nb_clusters, eigenvalues, eigenvectors = self.eigenDecomposition(adj_mat) k = nb_clusters[0] #calculate spectral clustering labels sc = SpectralClustering(k, affinity=affinity, n_init=n_init) sc.fit(adj_mat) #update graph with the communitites dic = dict(zip(G.nodes(), sc.labels_)) nx.set_node_attributes(G, dic, 'community_spectral') return G, list(sc.labels_), k ##################################### # Method: calculate_louvain_clustering # Description: calculate cluster labels for a graph using community_louvain def calculate_louvain_clustering(self, G): # compute the best partition partition = community_louvain.best_partition(G) #get number of clusters partition_arr = list(partition.values()) partition_arr_no_dups = list(dict.fromkeys(partition_arr)) k = len(partition_arr_no_dups) #number of clusters #update graph with the communitites dic = dict(zip(G.nodes(), partition.values())) nx.set_node_attributes(G, dic, 'community_louvain') return G, partition.values(), k ##################################### # Method: calculate_separability # Description: calculates the separability score for a community # Parameters: # -G_Community: the subgraph with of nodes that belong to the same commty # -G_All: The entire graph # -dens: separability score def calculate_separability(self, G_Community, G_All): # #of edges for that community - (internal nodes) ms = len(G_Community.edges(G_Community.nodes())) # #of edges edges pointing outside of the community - (external nodes) cs = len(G_All.edges(G_Community.nodes())) - ms # ratio between internal and external nodes sep = ms/cs return sep ##################################### # Method: calculate_density # Description: calculates the density score for a community # Parameters: # -G_Community: the subgraph with of nodes that belong to the same commty # Returns: # -dens: density score def calculate_density(self, G_Community): # #of edges for that community ms = len(G_Community.edges()) # #of nodes for that community ns = ms = len(G_Community.nodes()) # fraction of the edges that appear between the nodes in G_Community dens = ms / (ns * (ns-1) / 2) return dens ##################################### # Method: calculate_average_clustering_coef # Description: calculates the average clustering coefficient of a graph # Parameters: # -G_Community: the subgraph with of nodes that belong to the same commty # Returns: # -acc: the average clustering coefficient def calculate_average_clustering_coef(self, G_Community): # calculates the average clustering coefficient number acc = nx.average_clustering(G_Community) return acc ##################################### # Method: calculate_cliques # Description: calculates the clique number of the graph and # the number of maximal cliques in the graph. # Parameters: # -G_Community: the subgraph with of nodes that belong to the same commty # Returns: # -gcn: the clique number of the graph # -nofc: the number of maximal cliques in the graph def calculate_cliques(self, G): gcn = nx.graph_clique_number(G) nofc = nx.graph_number_of_cliques(G) return gcn, nofc ##################################### # Method: calculate_power_nodes_score # Description: calculates power nodes score # This is to calculate how many of the total nodes # in graph are connected to a few top degree nodes # Parameters: # -G: the graph to analyze # -top_no: top number of nodes you want to analyze # Returns: # -pns: power nodes score # 1 means that all other nodes in the graph # are connected to the top nodes def calculate_power_nodes_score(self, G, top_no=3): # number of nodes of the original graph no_of_nodes = len(G.nodes()) # get the top 3 high degree nodes arr_nodes = [] for x in list(self.get_top_degree_nodes(G, 1, top_no)): arr_nodes.append(x[0]) # creates a subgrpah of all nodes conencted to the top nodes sub_graph = self.create_node_subgraph(G, arr_nodes) # number of nodes of the sub-graph of top nodes no_of_nodes_sub_graph = len(sub_graph.nodes()) # calculates the ratio between the two. pns = no_of_nodes_sub_graph / no_of_nodes return pns ##################################### # Method: calculate_average_node_degree # Description: calculates the average of the degree of all nodes # Parameters: # -G: the graph to analyze # Returns: # -deg_mean: the mean def calculate_average_node_degree(self, G): arr = np.array(sorted(G.degree(), key=lambda x: x[1], reverse=True)) deg_mean = np.asarray(arr[:,1], dtype=np.integer).mean() return deg_mean ##################################### # Method: print_cluster_metrics # Description: print cluster graphs metrics def print_cluster_metrics(self, G_Community, G_All, top_no=3, acc_node_size_cutoff=None): if acc_node_size_cutoff is None: acc_node_size_cutoff = len(G_Community.nodes()) print("# of Nodes: " + str(len(G_Community.nodes()))) print("# of Edges: " + str(len(G_Community.edges()))) deg_mean = self.calculate_average_node_degree(G_Community) print("Average Node Degree: " + str(deg_mean) + " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) sep = self.calculate_separability(G_Community, G_All) print("Separability: " + str(sep)+ " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) den = self.calculate_density(G_Community) print("Density: " + str(den)+ " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) if acc_node_size_cutoff > len(G_Community.nodes()): acc = self.calculate_average_clustering_coef(G_Community) print("Average Clustering Coefficient: " + str(acc) + " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) else: print("Average Clustering Coefficient: " + " (**more nodes than the cutoff number)") gcn, nofc = self.calculate_cliques(G_Community) print("Clique number: " + str(gcn)) print("Number of maximal cliques: " + str(nofc) + " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) pns = self.calculate_power_nodes_score(G_Community, top_no) print("Power Nodes Score: " + str(pns) + " - " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) ##################################### # Method: eigenDecomposition # Description: This method performs the eigen decomposition on a given affinity matrix # Re-used code from https://github.com/ciortanmadalina/high_noise_clustering # Parameters: # -af_matrix = Affinity matrix # -bln_plot = flag to determine if we should plot the sorted eigen values for visual inspection or not # -topK = number of suggestions as the optimal number of of clusters # Returns: # -nb_clusters = the optimal number of clusters by eigengap heuristic # -eigenvalues = all eigen values # -eigenvectors = all eigen vectors def eigenDecomposition(self, af_matrix, bln_plot = False, topK = 5): #construct the laplacian of the matrix L = csgraph.laplacian(af_matrix, normed=True) n_components = af_matrix.shape[0] # LM parameter : Eigenvalues with largest magnitude (eigs, eigsh), that is, largest eigenvalues in # the euclidean norm of complex numbers. #eigenvalues, eigenvectors = sp.sparse.linalg.eigs(L) eigenvalues, eigenvectors = SLA.eigsh(L, which = 'LM') if bln_plot: plt.title('Largest eigen values of input matrix') plt.scatter(np.arange(len(eigenvalues)), eigenvalues) plt.grid() # Identify the optimal number of clusters as the index corresponding # to the larger gap between eigen values index_largest_gap = np.argsort(np.diff(eigenvalues))[::-1][:topK] nb_clusters = index_largest_gap + 1 return nb_clusters, eigenvalues, eigenvectors ##################################### # Method: remove_edges # Description: removes edges of nodes with less than the given degree. # (both nodes in the edge must be less than the given degree for us to remove the edge) def remove_edges(self, G, min_degree_no): G2 = G.copy() count = 0 for edge in list(G2.edges()): degree_node_from = G2.degree(edge[0]) degree_node_to = G2.degree(edge[1]) if degree_node_from < min_degree_no and degree_node_to < min_degree_no: count = count +1 G2.remove_edge(edge[0], edge[1]) print(str(count) + ' edges removed') return G2 ##################################### # Method: remove_edges_eithernode # Description: removes edges of nodes with less than the given degree. # (both nodes in the edge must be less than the given degree for us to remove the edge) def remove_edges_eithernode(self, G, min_degree_no): G2 = G.copy() count = 0 for edge in list(G2.edges()): degree_node_from = G2.degree(edge[0]) degree_node_to = G2.degree(edge[1]) if degree_node_from < min_degree_no or degree_node_to < min_degree_no: count = count +1 G2.remove_edge(edge[0], edge[1]) print(str(count) + ' edges removed') return G2 ##################################### # Method: contract_nodes_degree1 # Description: Contract nodes degree 1 in groups of the given number def contract_nodes_degree1(self, G, n_to_group): G2 = G.copy() degree_to_contract = 1 for node_degree in list(sorted(G2.degree, key=lambda x: x[1], reverse=True)): try: D = nx.descendants(G2, node_degree[0]) D.add(node_degree[0]) this_node_subgraph = G2.subgraph(D) ##################### degree1 nodes_degree1 = [node for node, degree in list(this_node_subgraph.degree()) if degree == degree_to_contract] subgraph_degree1 = this_node_subgraph.subgraph(nodes_degree1) j = 0 n = int(n_to_group/(degree_to_contract)) for node in list(subgraph_degree1): if j==0 or j%n==0: first_node = node else: G2 = nx.contracted_nodes(G2, first_node, node, self_loops=True) j=j+1 except Exception as e: continue return G2 ##################################### # Method: print_Measures # Description: print Graph measures to the screen and to a file def print_Measures( self, G, blnCalculateDimater=False, blnCalculateRadius = False, blnCalculateExtremaBounding=False, blnCalculateCenterNodes=False, fileName_to_print = None): #verify if graph is connected or not try: blnGraphConnected = is_connected(G) except: blnGraphConnected = False no_nodes = str(len(G.nodes())) no_edges = str(len(G.edges())) print("# Nodes: " + no_nodes) print("# Edges: " + no_edges) #Calculate and print Diameter if blnCalculateDimater == True: if blnGraphConnected == True: diameter_value = str(distance_measures.diameter(G)) print("Diameter: " + diameter_value) else: diameter_value = "Not possible to calculate diameter. Graph must be connected" print(diameter_value) #Calculate and print Radius if blnCalculateRadius == True: if blnGraphConnected == True: radius_value = str(distance_measures.radius(G)) print("Radius: " + radius_value) else: radius_value = "Not possible to calculate radius. Graph must be connected" print(radius_value) #Calculate and print Extrema bounding if blnCalculateExtremaBounding == True: if blnGraphConnected == True: extrema_bounding_value = str(distance_measures.extrema_bounding(G)) print("Extrema bounding: " + extrema_bounding_value) else: extrema_bounding_value = "Not possible to calculate Extrema bounding. Graph must be connected" print(extrema_bounding_value) #Calculate and print Centers if blnCalculateCenterNodes == True: str_centers_nodes="" if blnGraphConnected == True: centers_nodes = distance_measures.center(G) str_centers_nodes = str(sorted(G.degree(centers_nodes), key=lambda x: x[1], reverse=True)) print("Centers with their degree: " + str_centers_nodes) else: centers_nodes = "Not possible to calculate Centers. Graph must be connected" print(centers_nodes) # if file name is passed in the parameters, we save the measures into a file if fileName_to_print != None: #creates path if does not exists if not os.path.exists(os.path.dirname(fileName_to_print)): os.makedirs(os.path.dirname(fileName_to_print)) f = open(fileName_to_print, "w") f.write("# Nodes: " + no_nodes + "\n") f.write("# Edges: " + no_edges + "\n") if blnCalculateDimater == True: f.write("Diameter: " + diameter_value + "\n") if blnCalculateRadius == True: f.write("Radius: " + radius_value + "\n") #if blnCalculateBaryCenter == True: # f.write("Bary Center: " + barycenter_node + "\n") if blnCalculateExtremaBounding == True: f.write("Extrema bounding: " + extrema_bounding_value + "\n") if blnCalculateCenterNodes == True: f.write("Centers with their degree: " + str_centers_nodes + "\n") f.close() import os import json import datetime import csv import string import gensim from gensim import corpora from gensim.models.coherencemodel import CoherenceModel import nltk from nltk.corpus import words, stopwords, wordnet from nltk.tokenize import RegexpTokenizer from nltk.stem import PorterStemmer, WordNetLemmatizer from nltk import pos_tag from nltk.tokenize import word_tokenize import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import collections import numpy as np from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator #dictionary_words = dict.fromkeys(words.words(), None) #stopWords = set(stopwords.words('english')) #tokenizer = RegexpTokenizer(r'\w+') #stemmer = PorterStemmer() #lemmatiser = WordNetLemmatizer() stop = set(stopwords.words('english')) stop.add ('u') stop.add ('e') exclude = set(string.punctuation) lemma = WordNetLemmatizer() #topic_doc_complete = [] #lda_model = "" class TwitterTopics: def __init__(self, folder_path, mongoDB_database=None): self.folder_path = folder_path self.lda_model = object() self.lsi_model = object() self.doc_term_matrix = object() self.dictionary = object() self.lda_coh_u_mass = 0 self.lda_coh_c_v = 0 self.lsi_coh_u_mass = 0 self.lsi_coh_c_v = 0 self.db = mongoDB_database if mongoDB_database is not None: self.c_topics = self.db.topics else: self.c_topics = None def __del__(self): self.folder_path = None self.lda_model = None self.lsi_model = None self.doc_term_matrix = None self.dictionary = None self.lda_coh_u_mass = None self.lda_coh_c_v = None self.lsi_coh_u_mass = None self.lsi_coh_c_v = None def get_coh_u_mass(self): return self.lda_coh_u_mass, self.lsi_coh_u_mass def get_coh_c_v(self): return self.lda_coh_c_v, self.lda_coh_c_v #create one array with all tweets of one hashtag for topic analysis def get_docs_from_file(self, file_path): docs = [] with open(file_path, 'r', encoding='utf8', errors='ignore') as f: for line in f: docs.append(line) f.close() return docs #clean documents for topic analysis def clean_docs(self, doc, delete_numbers=True, delete_stop_words=True, lemmatize_words=True): doc_clean = doc if delete_numbers ==True: doc_clean = doc.replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '').replace('0', '') if delete_stop_words == True: doc_clean = " ".join([i for i in doc_clean.lower().split() if i not in stop]) doc_clean = ''.join(ch for ch in doc_clean if ch not in exclude) if lemmatize_words == True: doc_clean = " ".join(lemma.lemmatize(word) for word in doc_clean.split()) return doc_clean #train model def train_model(self, topic_docs, num_topics, model_name, blnSaveinDB=False, blnSaveTrainedModelFiles=False, txtFileName=None, model_type='both', lda_num_of_iterations=150, delete_stop_words=True, lemmatize_words=True, delete_numbers=True): #starttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #print("Executing train_model... Started at: " + starttime ) doc_clean = [self.clean_docs(doc, delete_numbers, delete_stop_words, lemmatize_words).split() for doc in topic_docs] # Creating the term dictionary of our corpus, where every unique term is assigned an index. dictionary = corpora.Dictionary(doc_clean) self.dictionary = corpora.Dictionary(doc_clean) # Converting list of documents (corpus) into Document Term Matrix using dictionary prepared above. self.doc_term_matrix = [self.dictionary.doc2bow(doc) for doc in doc_clean] # Creating the object for LDA model using gensim library Lda = gensim.models.ldamodel.LdaModel file_data = [] if model_type in ('lda', 'both'): # Build the LDA model self.lda_model = gensim.models.LdaModel(corpus=self.doc_term_matrix, num_topics=num_topics, id2word=self.dictionary, iterations=lda_num_of_iterations) #get LDA coherence self.lda_coh_u_mass = CoherenceModel(model=self.lda_model, corpus=self.doc_term_matrix, dictionary=self.dictionary, coherence='u_mass') self.lda_coh_c_v = CoherenceModel(model=self.lda_model, texts=doc_clean, dictionary=self.dictionary, coherence='c_v') #create json file with lda results for idx in range(num_topics): topic = idx+1 strtopic = str(topic) data = '{"model_name":"' + model_name + \ '", "model_type":"' + 'lda' + \ '", "timestamp":"' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + \ '", "no_tweets":"' + str(len(topic_docs)) + \ '", "coh_u_mass":"' + str(self.lda_coh_u_mass.get_coherence()) + \ '", "coh_c_v":"' + str(self.lda_coh_c_v.get_coherence()) + \ '", "topic_no":"' + strtopic + \ '", "topic":"' + str(self.lda_model.print_topic(idx, num_topics)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) if model_type in ('lsi', 'both'): # Build the LSI model self.lsi_model = gensim.models.LsiModel(corpus=self.doc_term_matrix, num_topics=num_topics, id2word=self.dictionary) #get LSI coherence self.lsi_coh_u_mass = CoherenceModel(model=self.lsi_model, corpus=self.doc_term_matrix, dictionary=self.dictionary, coherence='u_mass') self.lsi_coh_c_v = CoherenceModel(model=self.lsi_model, texts=doc_clean, dictionary=self.dictionary, coherence='c_v') #create json file with lsi results for idx in range(num_topics): topic = idx+1 strtopic = str(topic) data = '{"model_name":"' + model_name + \ '", "model_type":"' + 'lsi' + \ '", "timestamp":"' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + \ '", "no_tweets":"' + str(len(topic_docs)) + \ '", "coh_u_mass":"' + str(self.lsi_coh_u_mass.get_coherence()) + \ '", "coh_c_v":"' + str(self.lsi_coh_c_v.get_coherence()) + \ '", "topic_no":"' + strtopic + \ '", "topic":"' + str(self.lsi_model.print_topic(idx, num_topics)).replace('"', "-") + '"}' x = json.loads(data) file_data.append(x) # Save if mongoDB collection is asked if blnSaveinDB == True: if self.db is not None: self.c_topics.insert_many(file_data) else: print("Can't save topics in db. No mongoDB connection was set up.") # Save results in a text file if txtFileName is not None: with open(txtFileName, 'w', encoding="utf-8") as outfile: json.dump(file_data, outfile) # Save models into file if blnSaveTrainedModelFiles == True: #creates path if does not exists if not os.path.exists(self.folder_path + "/trained_models/"): os.makedirs(self.folder_path + "/trained_models/") self.lda_model.save(self.folder_path + "/trained_models/" + model_name + "_lda_model.model") self.dictionary.save(self.folder_path + "/trained_models/" + model_name + "_dictionary.dict") #endtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") #print("Finished executing train_model. Ended at: " + endtime) #train model from file def train_model_from_file(self, file_path, num_topics, model_name, blnSaveinDB=False, blnSaveTrainedModelFiles=False, txtFileName=None, model_type='both', lda_num_of_iterations=150, delete_stop_words=True, lemmatize_words=True, delete_numbers=True): docs = self.get_docs_from_file(file_path) self.train_model(docs, num_topics, model_name, blnSaveinDB, blnSaveTrainedModelFiles, txtFileName, model_type, lda_num_of_iterations, delete_stop_words, lemmatize_words, delete_numbers) #plot graph with lda topics def plot_topics(self, file_name, no_of_topics, model_type = 'lda', fig_size_x = 17, fig_size_y=15, replace_existing_file=True): if replace_existing_file==True or not os.path.exists(file_name): fig_size_y = 7*(no_of_topics/2) fiz=plt.figure(figsize=(fig_size_x, fig_size_y)) for i in range(no_of_topics): if model_type == 'lda': df=pd.DataFrame(self.lda_model.show_topic(i), columns=['term','prob']).set_index('term') elif model_type == 'lsi': df=pd.DataFrame(self.lsi_model.show_topic(i), columns=['term','prob']).set_index('term') no_rows = int(no_of_topics/2)+no_of_topics%2 plt.subplot(no_rows,2,i+1) plt.title('topic '+str(i+1)) sns.barplot(x='prob', y=df.index, data=df, label='Cities', palette='Reds_d') plt.xlabel('probability') #save the file plt.savefig(file_name, dpi=200, facecolor='w', edgecolor='w') #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window # read a frequency list into a pandas objects # file format word\tfrequency def read_freq_list_file(self, file_path, delimiter='\t'): #df = pd.read_csv(file_path, encoding = "ISO-8859-1", header=None, sep=delimiter, lineterminator='\n') df = pd.read_csv(file_path, encoding = "utf-8", header=None, sep=delimiter, lineterminator='\n') df.columns = ['word', 'freq'] return df #plot a bar graph with the top frequency list def plot_top_freq_list(self, fr_list, top_no, ylabel, exclude_top_no=0, file=None, replace_existing_file= True): if exclude_top_no != 0: fr_list = fr_list.iloc[exclude_top_no:] fr_list = fr_list.nlargest(top_no,'freq') if len(fr_list) < top_no: for i in range( int((top_no-len(fr_list)) / 2.5)): data = [['', 0], ['', 0] ] df2 = pd.DataFrame(data, columns = ['word', 'freq']) fr_list = fr_list.append(df2) fr_list_gr = fr_list.groupby("word") plt.figure(figsize=(12, len(fr_list)/2.5)) fr_list_gr.max().sort_values(by="freq",ascending=True)["freq"].plot.barh() plt.xticks(rotation=50) plt.xlabel("Frequency") plt.ylabel(ylabel) if file != None: if replace_existing_file==True or not os.path.exists(file): plt.savefig(file, dpi=300, bbox_inches='tight') #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window #plot a word cloudfor a frequency list def plot_word_cloud(self, fr_list, file=None, replace_existing_file=True): wordcount = collections.defaultdict(int) for index, row in fr_list.iterrows(): wordcount[row['word']] = row['freq'] try: wordcloud = WordCloud(width=2000, height=1300, max_words=1000, background_color="white").generate_from_frequencies(wordcount) except: wordcloud = WordCloud(width=2000, height=1300, background_color="white").generate_from_frequencies(wordcount) # Display the generated image: plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") if file is not None: if replace_existing_file==True or not os.path.exists(file): plt.savefig(str(file), dpi=300) #plt.show() plt.cla() # Clear axis plt.clf() # Clear figure plt.close() # Close a figure window #load existing model from file #predict topic of a new tweet based on model
[]
2024-01-10
mIXs222/dbknitter
dbknitter~gpt_previous.py
import requests import time import datetime import openai import os import argparse import sys os.environ['OPENAI_API_KEY'] = "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" openai.api_key = os.getenv("OPENAI_API_KEY") MAX_TOKEN=2000 class Utility: def __init__(self): pass def get_current_time(self): current_datetime = datetime.datetime.now() formatted_datetime = current_datetime.strftime("%Y%m%d_%H:%M:%S") return formatted_datetime util = Utility() ############################################################################################## # Information about all tables must be stored in a single 'config' folder (see examples) # That folder must have one folder per table with folder name same as the table name # Each table folder must have three files: # 1. platform.txt: One line containing the platform table is on (mysql, mongodb) # 2. admin.txt: Has all necessary info to establish connection to the data platform # One spec per line as "word_description : value" eg: "database name : db1" # Note: It is important for admin.txt to be exhaustive, else chatGPT may introduce its own # variable names, which can't be progrmatically identified and the python code given # by it will fail to execute # # TODO: Add port option (URI in gneral) # 3. schema.txt: Schema of the equivalent SQL table. Each line is a column information in # "COLUMN_NAME type NULL" format # # TODO: How to specify non-SQL structures like lists etc? # # TODO: Support for simple files, like json etc # # TODO: Add table name to schema as well, else have to see both admin and schema to contruct queries # # TODO: Many tables may have common admin file # # Once these are defined, feed it into this program by calling the # Datalake.define_datalake_from_folder(...) function. # # Then to create prompt based on that config, use the Prompt.get_full_query(...) # Queries can be fed in through files ('query/query1.txt here). And outputs can saved # in files ('query_output.query1.txt' here) ############################################################################################## ''' Example Prompt: - I have a mysql table named DETAILS and a mongoldb table named INTERESTS. - DETAILS has the columns NAME, AGE, ADDRESS. INTERESTS has the fields of NAME, INTEREST. INTEREST is a list of strings and can be empty. - But the user of my data thinks all data is stored in mysql. - They wrote the following query: - SELECT DETAILS.AGE, INTERESTS.INTEREST - FROM DETAILS JOIN INTERESTS ON DETAILS.NAME = INTERESTS.NAME - Please generate a python code to execute this query. My mysql password is my-pwd. Output of the query must be written to a file name query_output.csv ''' ################################################################################### class SQL_COLUMN: def __init__(self, column_name, datatype, null_property="NO_NULL"): self.name = column_name self.datatype = datatype self.null_property = null_property # SQL schema of a table consists of # 1. Primary key of the table # 2. Columns: Each column has a name, type of data it hold, and propoerties like 'can it be NULL?' class SQL_SCHEMA: def __init__(self): self.primary_key_name = None self.columns = {} # column_name:SQL_COLUMN including primary key self.no_columns = len(self.columns) def add_column(self, Column): # input: SQL_COLUMN object self.columns[Column.name] = Column self.no_columns = len(self.columns) def print_schema(self): print("Primary Key is: ", self.primary_key_name) print ("Number of columns: ", self.no_columns) print("Schema: (Column_Name Type Null_property)") for _, col in self.columns.items(): print(f" {col.name} {col.datatype} {col.null_property}") print("******************************************") class SQL_TABLE: def __init__(self, table_name, schema_filepath=""): self.name = table_name self.schema = SQL_SCHEMA() self.define_schema_from_file(schema_filepath) # self.schema is populate from a schema file of the format: # COLUMN_NAME1 type NULL # COLUMN_NAME2 type NO_NULL .... def define_schema_from_file(self, filepath): ## populate self.schema if filepath == "" : print("SQL Table {}: No Schema filename given".format(self.name)) return 0 file1 = open(filepath, 'r') lines = file1.readlines() line_no = 0 for line in lines: line_no += 1 col_spec = line.strip().split() # Column_name Cloumn_type null_or_nonull if line_no == 1: # primary key self.schema.primary_key_name = col_spec[0] column = SQL_COLUMN(col_spec[0], col_spec[1], col_spec[2]) self.schema.add_column(column) self.print_table() return 0 def print_table(self): print("Table name is: ", self.name) self.schema.print_schema() #################################################################################### # * Table class represents a generic table in any of the data platforms like mysql, mongodb etc # * Properties common to tables in all platforms (like the need for some admin details) is # to be defined in this class # * If we later need to specialize for data platforms, child classes of Table can be defined # * Each table needs 2 files: admin details file, equivalent sql schema file # TODO: May need a separate platform specific schema file too class Table: def __init__(self, table_name, platform, admin_file="", schema_file=""): self.platform = platform # mysql, mongodb etc self.name = table_name self.admin_details = {} # Keys must be strings describing what the values are and # -- the variable names for those you want in the code, # -- This will be used to tell chatgpt about admin details # -- eg: "name" : db_name, "password" : sql_pwd etc self.equivalent_sql_table = SQL_TABLE(table_name, schema_file) # A table, no matter which platform, should have an SQL table equivalent, # -- which is how it is presented to the buisness analyst (the user). self.special_case = None # any special rules like how to handle nulls etc if self.platform == "mysql": self.column_equivalent = "columns" # what are "columns" called in that platform elif self.platform == "mongodb": self.column_equivalent = "fields" else: sys.exit("Invalid platform name. Should be: mysql, mongodb") # TODO: move this kind of information to platform.txt self.define_admin_from_file(admin_file) def define_admin_from_file(self, filepath): ## populate self.admin_details if filepath == "" : print("Table {}: No admin filename given".format(self.name)) return 0 print(f"Updating admin info for table {self.name} from {filepath} :") file1 = open(filepath, 'r') lines = file1.readlines() for line in lines: spec = line.split(":") # "spec description" : "spec value" self.admin_details[spec[0].strip()] = spec[1].strip() print("Admin info update complete!") self.print_admin_info() print("---------------------------------------") return 0 def print_admin_info(self): print(f"Table {self.name} admin info :") for k, v in self.admin_details.items(): print(f" {k} : {v}") ################################################################################################## # Collection of all Table objects in my data setup class Datalake: def __init__(self, name): self.name = name self.tables = {} # table_name : Table self.no_tables = len(self.tables) def add_table(self, table): # table is a Table object self.tables[table.name] = table self.no_tables = len(self.tables) # Folder should contain one folder per table in the datalake # Each table folder should contain: platform.txt, schema.txt, admin.txt def define_datalake_from_folder(self, folder_path): table_folders = [f.path for f in os.scandir(folder_path) if f.is_dir()] for tab in table_folders: table_name = tab.split('/')[-1] platform_file = tab+'/platform.txt' admin_file = tab+'/admin.txt' schema_file = tab+'/schema.txt' with open(platform_file) as f: platform = f.readline() self.add_table(Table(table_name, platform, admin_file, schema_file)) print("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*") print("Table definitions complete! \n\n") ################################################################################################### ####################################### UTILS #################################################### ## Either reurns ", " or " and " or "." depending on which element you are ## appending to a sentence ## eg: "table1, table2 and table3."" def add_delimiter(i, L): if i < (L-1): return f", " elif i == (L-1): return f" and " else: return f". " ## query can be passed as a string or as a filepath (with isfile=True) def get_query(query, isfile=False): if isfile: with open(query, 'r') as file: q = file.read() return q else: return query ############################################################################# # Prompt for a given Datalake setup class Prompt: def __init__(self, datalake, output_file="query_output.csv"): self.datalake = datalake self.output_file = output_file # prefaces self.datalake_info_pref = "I have organized my data as follows: " ## <table1> in <db1>, .... self.schema_info_pref = "" ## "<table1> has <columns/fields etc> <cloumn_name>, of type <type>, and can be <special_cases>" self.admin_info_pref = "Details of my databases are as follows : " self.story = "But the user of my data thinks all the data is stored in mysql with the same column names." # TODO: "with same column names"? # self.sql_schema_pref = "They think the tables have the following schemas : " # TODO: Is this required self.query_pref = "With that assumption, they wrote the following query: " self.output_spec = f"Generate a python code to execute this query on my original data. Query's output should be written to the file {self.output_file}. Please output only the python code and a bash command to installtion all dependencies to run that python code." self.datalake_info = "" self.schema_info = "" self.admin_info = "" def to_dict(self): member_variables = {attr: getattr(self, attr) for attr in dir(self) if not callable(getattr(self, attr)) and not attr.startswith("__")} for k, v in member_variables.items(): print(f"* {k}: {v}") return member_variables def gen_datalake_info(self): ## <table1> in <db1>, .... temp = "" for index, table_n in enumerate(self.datalake.tables): tab = self.datalake.tables[table_n] temp += f"table {table_n} in {tab.platform}" temp += f"{add_delimiter(index+1, self.datalake.no_tables)}" self.datalake_info = self.datalake_info_pref + temp return self.datalake_info def gen_schema_info(self): ## "<table1> has <columns/fields etc> <cloumn_name>, of type <type>, and can be <special_cases>" temp = "" for _, table_n in enumerate(self.datalake.tables): tab = self.datalake.tables[table_n] # Table object temp += f"Table {table_n} has the following {tab.column_equivalent}: " for i, col_name in enumerate(tab.equivalent_sql_table.schema.columns): col = tab.equivalent_sql_table.schema.columns[col_name] # SQL_COLUMN object temp += f"'{col_name}' of type {col.datatype}" temp += f"{add_delimiter(i+1, tab.equivalent_sql_table.schema.no_columns)}" self.schema_info = self.schema_info_pref + temp return self.schema_info def gen_admin_info(self): ## "for <table1> the <hostname> is <insert>, the <password> is <inser> " temp = "" for _, table_n in enumerate(self.datalake.tables): tab = self.datalake.tables[table_n] # Table object temp += f" For table {table_n} " L = len(tab.admin_details) for i, spec_name in enumerate(tab.admin_details): temp += f"the {spec_name} is {tab.admin_details[spec_name]}" temp += f"{add_delimiter(i+1, L)}" self.admin_info = self.admin_info_pref + temp return self.admin_info def gen_full_prompt(self, query, isfile=False): q = get_query(query, isfile) self.gen_datalake_info() self.gen_schema_info() self.gen_admin_info() prompt = "" prompt += self.datalake_info + '\n' + self.schema_info + "\n" + self.admin_info + " \n" + self.story + "\n" + self.query_pref + q + "\n" + self.output_spec print("The final prompt is: \n\n") print(prompt) return prompt ##################################################################################################### class Multi_Message_ChatGPTQuery: def __init__(self): self.messages = list() self.input_message_len = list() self.data = "" self.runtime = -1 self.output_text = "" self.gpt_model = "gpt-4" # "gpt-3.5-turbo" self.finished_reason = "" self.response = "" self.created_time = -1 self.uid = "" self.completion_tokens = -1 self.prompt_tokens = -1 self.total_tokens = -1 def set_input_message_len(self): assert len(self.input_message_len) == 0 for msg in self.messages: self.input_message_len.append(len(msg)) def add_context(self, new_msg, role="user"): formatted_msg ={"role": role, "content": new_msg} self.messages.append(formatted_msg) def chat_with_gpt(self): ################################################### gpt_response = openai.ChatCompletion.create( model=self.gpt_model, messages=self.messages, temperature=1, max_tokens=MAX_TOKEN, top_p=1, frequency_penalty=0, presence_penalty=0 ) ################################################### # TODO: Which one is correct? # self.response = gpt_response.choices[0].text.strip() reason = gpt_response['choices'][0]['finish_reason'] if reason != "stop": print("ERROR: GPT failed, finished_reason: {}") print("return None...") return None self.finished_reason = reason self.response = gpt_response['choices'][0]['message']['content'] self.created_time = gpt_response["created"] self.uid = gpt_response["id"] self.completion_tokens = gpt_response["usage"]["completion_tokens"] self.prompt_tokens = gpt_response["usage"]["prompt_tokens"] self.total_tokens = gpt_response["usage"]["total_tokens"] print(f"gpt response: {gpt_response}") print(f"extracted response: {self.response}") return self.response def write_result(self, output_filepath): temp = list() assert len(self.messages) == len(self.input_message_len) temp.append(f"uid, {self.uid}") for i in range(len(self.messages)): temp.append(f"message_{i},{self.messages[i]},{self.input_message_len[i]}") temp.append(f"input_message_len,{self.input_message_len}") temp.append(f"MAX_TOKEN, {MAX_TOKEN}") temp.append(f"data, {self.data}") temp.append(f"runtime, {self.runtime}") temp.append(f"output_text, {self.output_text}") temp.append(f"gpt_model, {self.gpt_model}") temp.append(f"finished_reason, {self.finished_reason}") temp.append(f"response, {self.response}") temp.append(f"created_time, {self.created_time}") temp.append(f"completion_tokens, {self.completion_tokens}") temp.append(f"prompt_tokens, {self.prompt_tokens}") temp.append(f"total_tokens, {self.total_tokens}") #path_ = util.get_current_time() + "-gpt_output.txt" # Replace with the path to your file path_ = output_filepath with open(path_, "w") as file: for elem in temp: file.write(elem + "\n") class GPT: def __init__(self): self.num_query = 0 # self.api_endpoint = 'https://api.openai.com/v1/engines/davinci-codex/completions' def send_request(self, cq, output_filepath): ''' reference: https://platform.openai.com/docs/guides/gpt/chat-completions-api The system message helps set the behavior of the assistant. For example, you can modify the personality of the assistant or provide specific instructions about how it should behave throughout the conversation. However note that the system message is optional and the model’s behavior without a system message is likely to be similar to using a generic message such as "You are a helpful assistant." ''' cq.set_input_message_len() result = cq.chat_with_gpt() #print(result) ts = time.time() # response = requests.post(self.api_endpoint, json=cq.params, headers=cq.headers) # cq.data = response.json() # data is python dictionary. resopnse is json. assert cq.runtime == -1 cq.runtime = (time.time() - ts) self.num_query += 1 cq.write_result(output_filepath) return cq.response def call_chatgpt_api(self, query_prompt, output_filepath): cq = Multi_Message_ChatGPTQuery() cq.add_context(query_prompt) # cq.add_context(..) # can add more queries gpt_output = self.send_request(cq, output_filepath) # mongodb_code = gpt_output['choices'][0]['text'] #print("********************") #print("** chatgpt output **") #print("********************") #print(gpt_output) ###**************************************************************************************************** if __name__ == "__main__": ################# SETTINGS ############################# ## I think full paths need to be given # TODO: Fix this CONFIG_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/config" QUERY_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/query" OUTPUT_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/query_output" ######################################################### ## Feed in Datalake information (we name it "myData here") datalake = Datalake("myData") datalake.define_datalake_from_folder(CONFIG_FOLDER) ## Create prompt generation object prompt = Prompt(datalake) gpt = GPT() query_files=[f for f in os.listdir(QUERY_FOLDER) if os.path.isfile(QUERY_FOLDER+'/'+f)] for qfile in query_files: query_prompt =prompt.gen_full_prompt(QUERY_FOLDER+'/'+qfile, True) # To get the query from a file #query_prompt = prompt.gen_full_prompt("A_QUERY") # to just pass query as string print("\n\n") output_file = OUTPUT_FOLDER+'/'+qfile gpt.call_chatgpt_api(query_prompt, output_file) # TODO: Fit many queries within same context
[ "\n", " \n" ]
2024-01-10
mIXs222/dbknitter
dbknitter~gpt_tpch.py
import requests import time import datetime import openai import os import argparse import sys from itertools import islice, product from dbknitter.tpch_queries import tpch_queries_sql, tpch_queries_eng_official, tpch_queries_eng_manual, tpch_queries_eng_gpt ## python dbknitter/gpt_tpch.py batch --output_dir platforms/client/source/s01/v1_9 --db_splits_file db_splits.txt --api_key <give_your_key> ## OR ## python3 dbknitter/gpt_tpch.py batch --output_dir junk --db_splits=00001111,10101010 --api_key <give_your_key> #os.environ['OPENAI_API_KEY'] = "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" #openai.api_key = os.getenv("OPENAI_API_KEY") os.environ['OPENAI_API_KEY'] = "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" Platforms = ["mysql", "mongodb", "redis"] openai.api_key = os.getenv("OPENAI_API_KEY") MAX_TOKEN=2000 class Utility: def __init__(self): pass def get_current_time(self): current_datetime = datetime.datetime.now() formatted_datetime = current_datetime.strftime("%Y%m%d_%H:%M:%S") return formatted_datetime util = Utility() ############################################################################################## # Information about all tables must be stored in a single 'config' folder (see examples) # That folder must have one folder per table with folder name same as the table name # Each table folder must have three files: # 1. platform.txt: One line containing the platform table is on (mysql, mongodb) # 2. admin.txt: Has all necessary info to establish connection to the data platform # One spec per line as "word_description : value" eg: "database name : db1" # Note: It is important for admin.txt to be exhaustive, else chatGPT may introduce its own # variable names, which can't be progrmatically identified and the python code given # by it will fail to execute # 3. schema.txt: Schema of the equivalent SQL table. Each line is a column information in # "COLUMN_NAME type NULL" format # # TODO: How to specify non-SQL structures like lists etc? # # TODO: Support for simple files, like json etc # # TODO: Add table name to schema as well, else have to see both admin and schema to contruct queries # # Once these are defined, feed it into this program by calling the # Datalake.define_datalake_from_folder(...) function. # # Then to create prompt based on that config, use the Prompt.get_full_query(...) # Queries can be fed in through files ('query/query1.txt here). And outputs can saved # in files ('query_output.query1.txt' here) ############################################################################################## ''' Example Prompt: - I have several tables stored in different data systems, the configuration is as follow: - MongoDB: INTERESTS - MySQL: DETAILS - the table schemas are as follow: - INTERESTS: NAME, INTEREST - DETAILS: NAME, AGE, ADDRESS - But the user of my data thinks all data is stored in mysql. - They wrote the following query: - SELECT DETAILS.AGE, INTERESTS.INTEREST - FROM DETAILS JOIN INTERESTS ON DETAILS.NAME = INTERESTS.NAME - My mysql password is my-pwd. Output of the query must be written to a file name query_output.csv - Please output only the python code and a bash command to installtion all dependencies to run that python code. ''' ################################################################################### class SQL_COLUMN: def __init__(self, column_name, datatype, null_property="NO_NULL"): self.name = column_name self.datatype = datatype self.null_property = null_property # SQL schema of a table consists of # 1. Primary key of the table # 2. Columns: Each column has a name, type of data it hold, and propoerties like 'can it be NULL?' class SQL_SCHEMA: def __init__(self): self.primary_key_name = None self.columns = {} # column_name:SQL_COLUMN including primary key self.no_columns = len(self.columns) def add_column(self, Column): # input: SQL_COLUMN object self.columns[Column.name] = Column self.no_columns = len(self.columns) def emplace(self, column_name, datatype, null_property): self.columns[column_name] = SQL_COLUMN(column_name, datatype, null_property) self.no_columns = len(self.columns) def print_schema(self): print("Primary Key is: ", self.primary_key_name) print ("Number of columns: ", self.no_columns) print("Schema: (Column_Name Type Null_property)") for _, col in self.columns.items(): print(f" {col.name} {col.datatype} {col.null_property}") print("******************************************") class SQL_TABLE: def __init__(self, table_name, schema): self.name = table_name self.schema = schema # self.schema is populate from a schema file of the format: # COLUMN_NAME1 type NULL # COLUMN_NAME2 type NO_NULL .... @staticmethod def define_schema_from_file(self, table_name, filepath): ## populate self.schema if filepath == "" : raise ValueError("SQL Table {}: No Schema filename given".format(self.name)) file1 = open(filepath, 'r') lines = file1.readlines() line_no = 0 schema = SQL_SCHEMA() for line in lines: line_no += 1 col_spec = line.strip().split() # Column_name Cloumn_type null_or_nonull if line_no == 1: # primary key schema.primary_key_name = col_spec[0] column = SQL_COLUMN(col_spec[0], col_spec[1], col_spec[2]) schema.add_column(column) table = SQL_TABLE(table_name, schema) table.print_table() return table def print_table(self): print("Table name is: ", self.name) self.schema.print_schema() # TPC-H Datalake Setup for benchmarking class TPCHSetup: @staticmethod def iter_all_mappings(splits): all_tpch_tables = ["nation", "region", "part", "supplier", "partsupp", "customer", "orders", "lineitem"] # for platform_idxs in product([0, 1], repeat=len(all_tpch_tables)): for platform_idxs in splits: # for platform_idxs in [ # [0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 1, 1, 1, 1, 1], # [2, 2, 2, 2, 2, 2, 2, 2], # [0, 0, 0, 0, 1, 1, 1, 1], # [0, 1, 0, 1, 0, 1, 0, 1], # [0, 0, 0, 0, 2, 2, 2, 2], # [0, 2, 0, 2, 0, 2, 0, 2], # [1, 1, 1, 1, 2, 2, 2, 2], # [1, 2, 1, 2, 1, 2, 1, 2], # [0, 1, 2, 0, 1, 2, 0, 1], # [1, 2, 0, 1, 2, 0, 1, 2], # [2, 0, 1, 2, 0, 1, 2, 0], # [0, 2, 1, 0, 2, 1, 0, 2], # [2, 1, 0, 2, 1, 0, 2, 1], # [1, 0, 2, 1, 0, 2, 1, 0], # ]: table_lists = [[] for _ in range(3)] for table_idx, platform_idx in enumerate(platform_idxs): table_lists[platform_idx].append(all_tpch_tables[table_idx]) yield "".join(map(str, platform_idxs)), table_lists[0], table_lists[1], table_lists[2] @staticmethod def iter_all_queries(query_language): query_dict = ( tpch_queries_sql if query_language == "sql" else ( tpch_queries_eng_official if query_language == "eng-official" else ( tpch_queries_eng_manual if query_language == "eng-manual" else ( tpch_queries_eng_gpt if query_language == "eng-gpt" else ( {} # invalid query_language ))))) for qidx in range(1, 22 + 1): yield qidx, query_dict[qidx] @staticmethod def mysql_admin(): # from platforms/mysql/tpch_init.sh AND cloudlab/docker-compose.yml return { "database name": "tpch", "username": "root", "password": "my-secret-pw", "hostname": "mysql", "instruction": "Use the python library 'pymysql' to connect the mysql server." " Do NOT use 'pymysql.cursors.DictCursor'.", } @staticmethod def mysql_table(table: str): # table name in lowercase, default TPC-H spelling. schema = SQL_SCHEMA() if table == "nation": schema.emplace("N_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_NAME", "CHAR(25)", "NO_NULL") schema.emplace("N_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("nation", schema) elif table == "region": schema.emplace("R_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("R_NAME", "CHAR(25)", "NO_NULL") schema.emplace("R_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("region", schema) elif table == "part": schema.emplace("P_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("P_NAME", "VARCHAR(55)", "NO_NULL") schema.emplace("P_MFGR", "CHAR(25)", "NO_NULL") schema.emplace("P_BRAND", "CHAR(10)", "NO_NULL") schema.emplace("P_TYPE", "VARCHAR(25)", "NO_NULL") schema.emplace("P_SIZE", "INTEGER", "NO_NULL") schema.emplace("P_CONTAINER", "CHAR(10)", "NO_NULL") schema.emplace("P_RETAILPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("P_COMMENT", "VARCHAR(23)", "NO_NULL") return SQL_TABLE("part", schema) elif table == "supplier": schema.emplace("S_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("S_NAME", "CHAR(25)", "NO_NULL") schema.emplace("S_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("S_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("S_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("S_ACCTBAL", "DECIMAL(15,2)", "NO_NULL") schema.emplace("S_COMMENT", "VARCHAR(101)", "NO_NULL") return SQL_TABLE("supplier", schema) elif table == "partsupp": schema.emplace("PS_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("PS_AVAILQTY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPLYCOST", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("PS_COMMENT", "VARCHAR(199)", "NO_NULL" ) return SQL_TABLE("partsupp", schema) elif table == "customer": schema.emplace("C_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("C_NAME", "VARCHAR(25)", "NO_NULL") schema.emplace("C_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("C_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("C_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("C_ACCTBAL", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("C_MKTSEGMENT", "CHAR(10)", "NO_NULL") schema.emplace("C_COMMENT", "VARCHAR(117)", "NO_NULL") return SQL_TABLE("customer", schema) elif table == "orders": schema.emplace("O_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("O_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("O_ORDERSTATUS", "CHAR(1)", "NO_NULL") schema.emplace("O_TOTALPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("O_ORDERDATE", "DATE", "NO_NULL") schema.emplace("O_ORDERPRIORITY", "CHAR(15)", "NO_NULL", ) schema.emplace("O_CLERK", "CHAR(15)", "NO_NULL",) schema.emplace("O_SHIPPRIORITY", "INTEGER", "NO_NULL") schema.emplace("O_COMMENT", "VARCHAR(79)", "NO_NULL") return SQL_TABLE("orders", schema) elif table == "lineitem": schema.emplace("L_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("L_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("L_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("L_LINENUMBER", "INTEGER", "NO_NULL") schema.emplace("L_QUANTITY", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_EXTENDEDPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_DISCOUNT", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_TAX", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_RETURNFLAG", "CHAR(1)", "NO_NULL") schema.emplace("L_LINESTATUS", "CHAR(1)", "NO_NULL") schema.emplace("L_SHIPDATE", "DATE", "NO_NULL") schema.emplace("L_COMMITDATE", "DATE", "NO_NULL") schema.emplace("L_RECEIPTDATE", "DATE", "NO_NULL") schema.emplace("L_SHIPINSTRUCT", "CHAR(25)", "NO_NULL") schema.emplace("L_SHIPMODE", "CHAR(10)", "NO_NULL") schema.emplace("L_COMMENT", "VARCHAR(44)", "NO_NULL") return SQL_TABLE("lineitem", schema) else: raise ValueError(f"Invalid mysql table name {table}") @staticmethod def mongodb_admin(): # from platforms/mongodb/tpch_init.sh AND cloudlab/docker-compose.yml return { "database name": "tpch", "port": "27017", "hostname": "mongodb", "instruction": "Use 'pymongo' to connect to the mongodb server.", } @staticmethod def mongodb_table(table: str): # table name in lowercase, default TPC-H spelling. schema = SQL_SCHEMA() if table == "nation": schema.emplace("N_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_NAME", "CHAR(25)", "NO_NULL") schema.emplace("N_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("nation", schema) elif table == "region": schema.emplace("R_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("R_NAME", "CHAR(25)", "NO_NULL") schema.emplace("R_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("region", schema) elif table == "part": schema.emplace("P_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("P_NAME", "VARCHAR(55)", "NO_NULL") schema.emplace("P_MFGR", "CHAR(25)", "NO_NULL") schema.emplace("P_BRAND", "CHAR(10)", "NO_NULL") schema.emplace("P_TYPE", "VARCHAR(25)", "NO_NULL") schema.emplace("P_SIZE", "INTEGER", "NO_NULL") schema.emplace("P_CONTAINER", "CHAR(10)", "NO_NULL") schema.emplace("P_RETAILPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("P_COMMENT", "VARCHAR(23)", "NO_NULL") return SQL_TABLE("part", schema) elif table == "supplier": schema.emplace("S_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("S_NAME", "CHAR(25)", "NO_NULL") schema.emplace("S_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("S_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("S_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("S_ACCTBAL", "DECIMAL(15,2)", "NO_NULL") schema.emplace("S_COMMENT", "VARCHAR(101)", "NO_NULL") return SQL_TABLE("supplier", schema) elif table == "partsupp": schema.emplace("PS_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("PS_AVAILQTY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPLYCOST", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("PS_COMMENT", "VARCHAR(199)", "NO_NULL" ) return SQL_TABLE("partsupp", schema) elif table == "customer": schema.emplace("C_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("C_NAME", "VARCHAR(25)", "NO_NULL") schema.emplace("C_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("C_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("C_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("C_ACCTBAL", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("C_MKTSEGMENT", "CHAR(10)", "NO_NULL") schema.emplace("C_COMMENT", "VARCHAR(117)", "NO_NULL") return SQL_TABLE("customer", schema) elif table == "orders": schema.emplace("O_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("O_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("O_ORDERSTATUS", "CHAR(1)", "NO_NULL") schema.emplace("O_TOTALPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("O_ORDERDATE", "DATE", "NO_NULL") schema.emplace("O_ORDERPRIORITY", "CHAR(15)", "NO_NULL", ) schema.emplace("O_CLERK", "CHAR(15)", "NO_NULL",) schema.emplace("O_SHIPPRIORITY", "INTEGER", "NO_NULL") schema.emplace("O_COMMENT", "VARCHAR(79)", "NO_NULL") return SQL_TABLE("orders", schema) elif table == "lineitem": schema.emplace("L_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("L_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("L_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("L_LINENUMBER", "INTEGER", "NO_NULL") schema.emplace("L_QUANTITY", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_EXTENDEDPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_DISCOUNT", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_TAX", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_RETURNFLAG", "CHAR(1)", "NO_NULL") schema.emplace("L_LINESTATUS", "CHAR(1)", "NO_NULL") schema.emplace("L_SHIPDATE", "DATE", "NO_NULL") schema.emplace("L_COMMITDATE", "DATE", "NO_NULL") schema.emplace("L_RECEIPTDATE", "DATE", "NO_NULL") schema.emplace("L_SHIPINSTRUCT", "CHAR(25)", "NO_NULL") schema.emplace("L_SHIPMODE", "CHAR(10)", "NO_NULL") schema.emplace("L_COMMENT", "VARCHAR(44)", "NO_NULL") return SQL_TABLE("lineitem", schema) else: raise ValueError(f"Invalid mongodb table name {table}") @staticmethod def redis_admin(): # from platforms/redis/tpch_init.sh AND cloudlab/docker-compose.yml return { "database name": "0", "port": "6379", "hostname": "redis", "instruction": "Use `direct_redis.DirectRedis` in place of `redis.Redis` to read Pandas DataFrame with `get('tablename')`.", } @staticmethod def redis_table(table: str): # table name in lowercase, default TPC-H spelling. schema = SQL_SCHEMA() if table == "nation": schema.emplace("N_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_NAME", "CHAR(25)", "NO_NULL") schema.emplace("N_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("N_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("nation", schema) elif table == "region": schema.emplace("R_REGIONKEY", "INTEGER", "NO_NULL") schema.emplace("R_NAME", "CHAR(25)", "NO_NULL") schema.emplace("R_COMMENT", "VARCHAR(152)", "NULL") return SQL_TABLE("region", schema) elif table == "part": schema.emplace("P_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("P_NAME", "VARCHAR(55)", "NO_NULL") schema.emplace("P_MFGR", "CHAR(25)", "NO_NULL") schema.emplace("P_BRAND", "CHAR(10)", "NO_NULL") schema.emplace("P_TYPE", "VARCHAR(25)", "NO_NULL") schema.emplace("P_SIZE", "INTEGER", "NO_NULL") schema.emplace("P_CONTAINER", "CHAR(10)", "NO_NULL") schema.emplace("P_RETAILPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("P_COMMENT", "VARCHAR(23)", "NO_NULL") return SQL_TABLE("part", schema) elif table == "supplier": schema.emplace("S_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("S_NAME", "CHAR(25)", "NO_NULL") schema.emplace("S_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("S_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("S_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("S_ACCTBAL", "DECIMAL(15,2)", "NO_NULL") schema.emplace("S_COMMENT", "VARCHAR(101)", "NO_NULL") return SQL_TABLE("supplier", schema) elif table == "partsupp": schema.emplace("PS_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("PS_AVAILQTY", "INTEGER", "NO_NULL") schema.emplace("PS_SUPPLYCOST", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("PS_COMMENT", "VARCHAR(199)", "NO_NULL" ) return SQL_TABLE("partsupp", schema) elif table == "customer": schema.emplace("C_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("C_NAME", "VARCHAR(25)", "NO_NULL") schema.emplace("C_ADDRESS", "VARCHAR(40)", "NO_NULL") schema.emplace("C_NATIONKEY", "INTEGER", "NO_NULL") schema.emplace("C_PHONE", "CHAR(15)", "NO_NULL") schema.emplace("C_ACCTBAL", "DECIMAL(15,2) ", "NO_NULL") schema.emplace("C_MKTSEGMENT", "CHAR(10)", "NO_NULL") schema.emplace("C_COMMENT", "VARCHAR(117)", "NO_NULL") return SQL_TABLE("customer", schema) elif table == "orders": schema.emplace("O_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("O_CUSTKEY", "INTEGER", "NO_NULL") schema.emplace("O_ORDERSTATUS", "CHAR(1)", "NO_NULL") schema.emplace("O_TOTALPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("O_ORDERDATE", "DATE", "NO_NULL") schema.emplace("O_ORDERPRIORITY", "CHAR(15)", "NO_NULL", ) schema.emplace("O_CLERK", "CHAR(15)", "NO_NULL",) schema.emplace("O_SHIPPRIORITY", "INTEGER", "NO_NULL") schema.emplace("O_COMMENT", "VARCHAR(79)", "NO_NULL") return SQL_TABLE("orders", schema) elif table == "lineitem": schema.emplace("L_ORDERKEY", "INTEGER", "NO_NULL") schema.emplace("L_PARTKEY", "INTEGER", "NO_NULL") schema.emplace("L_SUPPKEY", "INTEGER", "NO_NULL") schema.emplace("L_LINENUMBER", "INTEGER", "NO_NULL") schema.emplace("L_QUANTITY", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_EXTENDEDPRICE", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_DISCOUNT", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_TAX", "DECIMAL(15,2)", "NO_NULL") schema.emplace("L_RETURNFLAG", "CHAR(1)", "NO_NULL") schema.emplace("L_LINESTATUS", "CHAR(1)", "NO_NULL") schema.emplace("L_SHIPDATE", "DATE", "NO_NULL") schema.emplace("L_COMMITDATE", "DATE", "NO_NULL") schema.emplace("L_RECEIPTDATE", "DATE", "NO_NULL") schema.emplace("L_SHIPINSTRUCT", "CHAR(25)", "NO_NULL") schema.emplace("L_SHIPMODE", "CHAR(10)", "NO_NULL") schema.emplace("L_COMMENT", "VARCHAR(44)", "NO_NULL") return SQL_TABLE("lineitem", schema) else: raise ValueError(f"Invalid redis table name {table}") #################################################################################### # * Table class represents a generic table in any of the data platforms like mysql, mongodb etc # * Properties common to tables in all platforms (like the need for some admin details) is # to be defined in this class # * If we later need to specialize for data platforms, child classes of Table can be defined # * Each table needs 2 files: admin details file, equivalent sql schema file # TODO: May need a separate platform specific schema file too class Table: def __init__(self, table_name, platform, admin_details, equivalent_sql_table): self.platform = platform # mysql, mongodb etc self.name = table_name self.admin_details = admin_details # Keys must be strings describing what the values are and # -- the variable names for those you want in the code, # -- This will be used to tell chatgpt about admin details # -- eg: "name" : db_name, "password" : sql_pwd etc self.equivalent_sql_table = equivalent_sql_table # A table, no matter which platform, should have an SQL table equivalent, # -- which is how it is presented to the buisness analyst (the user). self.special_case = None # any special rules like how to handle nulls etc if self.platform == "mysql": self.column_equivalent = "columns" # what are "columns" called in that platform elif self.platform == "mongodb": self.column_equivalent = "fields" elif self.platform == "redis": self.column_equivalent = "fields in stream" else: sys.exit("Invalid platform name. Should be: mysql, mongodb, redis") @staticmethod def define_admin_from_file(self, table_name, platform, admin_file, schema_file): ## populate equivalent_sql_table equivalent_sql_table = SQL_TABLE(table_name, schema_file) ## populate admin_details if admin_file == "" : print("Table {}: No admin filename given".format(table_name)) return 0 print(f"Updating admin info for table {table_name} from {admin_file} :") file1 = open(admin_file, 'r') lines = file1.readlines() for line in lines: spec = line.split(":") # "spec description" : "spec value" admin_details[spec[0].strip()] = spec[1].strip() print("Admin info update complete!") self.print_admin_info() print("---------------------------------------") return Table(table_name, platform, admin_details, equivalent_sql_table) def print_admin_info(self): print(f"Table {self.name} admin info :") for k, v in self.admin_details.items(): print(f" {k} : {v}") ################################################################################################## # Collection of all Table objects in my data setup class Datalake: def __init__(self, name): self.name = name self.tables = {} # table_name : Table self.no_tables = len(self.tables) def add_table(self, table): # table is a Table object self.tables[table.name] = table self.no_tables = len(self.tables) @staticmethod def from_tpch_mapping( name, mysql_admin, mysql_tables, mongodb_admin, mongodb_tables, redis_admin, redis_tables, ): datalake = Datalake(name) for table in mysql_tables: sql_table = TPCHSetup.mysql_table(table) datalake.add_table(Table(sql_table.name, "mysql", mysql_admin, sql_table)) for table in mongodb_tables: sql_table = TPCHSetup.mongodb_table(table) datalake.add_table(Table(sql_table.name, "mongodb", mongodb_admin, sql_table)) for table in redis_tables: sql_table = TPCHSetup.redis_table(table) datalake.add_table(Table(sql_table.name, "redis", redis_admin, sql_table)) return datalake # Folder should contain one folder per table in the datalake # Each table folder should contain: platform.txt, schema.txt, admin.txt def define_datalake_from_folder(self, folder_path): table_folders = [f.path for f in os.scandir(folder_path) if f.is_dir()] for tab in table_folders: table_name = tab.split('/')[-1] platform_file = tab+'/platform.txt' admin_file = tab+'/admin.txt' schema_file = tab+'/schema.txt' with open(platform_file) as f: platform = f.readline() self.add_table(Table.define_admin_from_file(table_name, platform, admin_file, schema_file)) print("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*") print("Table definitions complete! \n\n") ################################################################################################### ####################################### UTILS #################################################### ## Either reurns ", " or " and " or "." depending on which element you are ## appending to a sentence ## eg: "table1, table2 and table3."" def add_delimiter(i, L): if i < (L-1): return f", " elif i == (L-1): return f" and " else: return f". " ## query can be passed as a string or as a filepath (with isfile=True) def get_query(query, isfile=False): if isfile: with open(query, 'r') as file: q = file.read() return q else: return query ############################################################################# # Prompt for a given Datalake setup class Prompt: def __init__(self, datalake, output_file="query_output.csv"): self.datalake = datalake self.output_file = output_file # prefaces self.database_platform_info_pref = "I have several databases of different platforms.\n" self.query_pref = "But a user thought that all the tables are stored in a SQL DBMS, and wrote the following query: " self.output_spec = f"Generate a python code to execute this query on my original data (i.e. get the data from different data platforms and combine the results). Query's output should be written to the file {self.output_file}. Without an explanation, please seperately output 1. the python code (.py) and 2. a bash script (.sh) to install all dependencies to run that python code." # Note: Specify the specific python libraries to be used in the python code. And other considerations self.note_info = "Note that if a table is not in a database, it should not appear in the query for that database." self.conf_info = self.gen_all_config_info() def _list_to_string(self, l): list_str = "" for i, elem in enumerate(l): list_str += elem if i < len(l)-1: list_str += ", " return list_str def gen_db_config_info(self, platform_name, database_name, table_names): #platform <> has database named <> with tables: <>,<>,<> inside. #the table schema are as follows: #<table name>: XXX #the database admin info are as follows: #<database name>: XXX print(table_names) if(len(table_names) == 0): return "" gist:str = f"{platform_name} has database named {database_name} with tables: " gist = gist + self._list_to_string(table_names) + "\n" table_schema:str = f"Relevant table names and their schemas in {platform_name}:\n" for table_n in table_names: tab = self.datalake.tables[table_n] # Table object table_schema += f"- {table_n}: " for i, col_name in enumerate(tab.equivalent_sql_table.schema.columns): col = tab.equivalent_sql_table.schema.columns[col_name] # SQL_COLUMN object table_schema += f"{col_name}" # table_schema += f" of type {col.datatype}" if(i < len(tab.equivalent_sql_table.schema.columns)-1): table_schema += ", " else: table_schema += "\n" admin:str = f"To connect with {platform_name}, use these connection information:\n" example_table = self.datalake.tables[table_names[0]] for spec_name in example_table.admin_details: admin += f"- {spec_name}: {example_table.admin_details[spec_name]}\n" return gist + table_schema + admin def gen_all_config_info(self): config_info = self.database_platform_info_pref + "\n" platformdb2table_n = {} for platform in Platforms: platformdb2table_n[platform] = {} for table_n in self.datalake.tables: tab = self.datalake.tables[table_n] if tab.admin_details["database name"] in platformdb2table_n[tab.platform]: platformdb2table_n[tab.platform][tab.admin_details["database name"]].append(table_n) else: platformdb2table_n[tab.platform][tab.admin_details["database name"]] = [table_n] for platform, dbs_with_tables in platformdb2table_n.items(): for db_name, table_names in dbs_with_tables.items(): config_info += self.gen_db_config_info(platform, db_name, table_names) config_info += "\n" return config_info def wrap_query(self, q, query_language): if query_language == "sql": return ( "```sql\n" + \ q + "\n" + \ "```" ) elif query_language in ("eng-official", "eng-manual", "eng-gpt"): return ( "```english\n" + \ q + "\n" + \ "```" ) else: raise ValueError(f"Invalid query_language {query_language}") def gen_full_prompt(self, query, qidx, query_language, isfile=False): assert type(qidx) == type(1) q = get_query(query, isfile) wrapped_q = self.wrap_query(q, query_language) prompt = ( self.conf_info + "\n" + \ self.query_pref + "\n" + \ wrapped_q + "\n" + \ self.output_spec + "\n" + \ self.note_info ) return prompt ##################################################################################################### class Multi_Message_ChatGPTQuery: def __init__(self): self.messages = list() self.input_message_len = list() self.data = "" self.runtime = -1 self.output_text = "" # self.gpt_model = "gpt-4" # "gpt-3.5-turbo", "gpt-4-1106-preview" self.gpt_model = "gpt-4-1106-preview" self.finished_reason = "" self.response = "" self.created_time = -1 self.uid = "" self.completion_tokens = -1 self.prompt_tokens = -1 self.total_tokens = -1 def set_input_message_len(self): assert len(self.input_message_len) == 0 for msg in self.messages: self.input_message_len.append(len(msg)) def add_context(self, new_msg, role="user"): formatted_msg ={"role": role, "content": new_msg} self.messages.append(formatted_msg) def chat_with_gpt(self): ################################################### gpt_response = openai.ChatCompletion.create( model=self.gpt_model, messages=self.messages, temperature=1, max_tokens=MAX_TOKEN, top_p=1, frequency_penalty=0, presence_penalty=0 ) ################################################### # TODO: Which one is correct? # self.response = gpt_response.choices[0].text.strip() reason = gpt_response['choices'][0]['finish_reason'] if reason != "stop": print("ERROR: GPT failed, finished_reason: {}") print("return None...") return None self.finished_reason = reason self.response = gpt_response['choices'][0]['message']['content'] self.created_time = gpt_response["created"] self.uid = gpt_response["id"] self.completion_tokens = gpt_response["usage"]["completion_tokens"] self.prompt_tokens = gpt_response["usage"]["prompt_tokens"] self.total_tokens = gpt_response["usage"]["total_tokens"] print(f"gpt response: {gpt_response}") print(f"extracted response: {self.response}") return self.response def write_result(self, output_filepath): temp = list() assert len(self.messages) == len(self.input_message_len) temp.append(f"uid, {self.uid}") for i in range(len(self.messages)): temp.append(f"message_{i},{self.messages[i]},{self.input_message_len[i]}") temp.append(f"input_message_len,{self.input_message_len}") temp.append(f"MAX_TOKEN, {MAX_TOKEN}") temp.append(f"data, {self.data}") temp.append(f"runtime, {self.runtime}") temp.append(f"output_text, {self.output_text}") temp.append(f"gpt_model, {self.gpt_model}") temp.append(f"finished_reason, {self.finished_reason}") temp.append(f"response, {self.response}") temp.append(f"created_time, {self.created_time}") temp.append(f"completion_tokens, {self.completion_tokens}") temp.append(f"prompt_tokens, {self.prompt_tokens}") temp.append(f"total_tokens, {self.total_tokens}") #path_ = util.get_current_time() + "-gpt_output.txt" # Replace with the path to your file path_ = output_filepath with open(path_, "w") as file: for elem in temp: file.write(elem + "\n") class GPT: def __init__(self): self.num_query = 0 # self.api_endpoint = 'https://api.openai.com/v1/engines/davinci-codex/completions' def send_request(self, cq, output_filepath): ''' reference: https://platform.openai.com/docs/guides/gpt/chat-completions-api The system message helps set the behavior of the assistant. For example, you can modify the personality of the assistant or provide specific instructions about how it should behave throughout the conversation. However note that the system message is optional and the model’s behavior without a system message is likely to be similar to using a generic message such as "You are a helpful assistant." ''' cq.set_input_message_len() ts = time.time() result = cq.chat_with_gpt() #print(result) # response = requests.post(self.api_endpoint, json=cq.params, headers=cq.headers) # cq.data = response.json() # data is python dictionary. resopnse is json. assert cq.runtime == -1 cq.runtime = (time.time() - ts) print(f"Chatted for {cq.runtime} seconds") self.num_query += 1 cq.write_result(output_filepath) return cq.response def call_chatgpt_api(self, query_prompt, output_filepath): cq = Multi_Message_ChatGPTQuery() cq.add_context(query_prompt) # cq.add_context(..) # can add more queries num_tries = 3 while num_tries > 0: try: return self.send_request(cq, output_filepath) except openai.error.RateLimitError as e: num_tries -= 1 print(f"ERROR sleeping due to rate limiting ({e}). {num_tries} tries left.") time.sleep(30) except Exception as e: num_tries -= 1 print(f"ERROR sleeping due to other errors {type(e)}: {e}). {num_tries} tries left.") time.sleep(30) # mongodb_code = gpt_output['choices'][0]['text'] #print("********************") #print("** chatgpt output **") #print("********************") #print(gpt_output) ###**************************************************************************************************** def main_file(): ################# SETTINGS ############################# ## I think full paths need to be given # TODO: Fix this CONFIG_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/config" QUERY_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/query" OUTPUT_FOLDER = "/home/chitty/Desktop/cs598dk/dbknitter/dbknitter/query_output" ######################################################### ## Feed in Datalake information (we name it "myData here") datalake = Datalake("myData") datalake.define_datalake_from_folder(CONFIG_FOLDER) ## Create prompt generation object prompt = Prompt(datalake) gpt = GPT() query_files=[f for f in os.listdir(QUERY_FOLDER) if os.path.isfile(QUERY_FOLDER+'/'+f)] for qfile in query_files: query_prompt =prompt.gen_full_prompt(QUERY_FOLDER+'/'+qfile, True) # To get the query from a file #query_prompt = prompt.gen_full_prompt("A_QUERY") # to just pass query as string print("\n\n") output_file = OUTPUT_FOLDER+'/'+qfile gpt.call_chatgpt_api(query_prompt, output_file) # TODO: Fit many queries within same context def parse_required_tables(): required_tables = dict() with open("dbknitter/tables.txt", 'r') as f_: lines = f_.readlines() for line in lines: qidx = int(line.split(",")[0].strip()) table = line.split(",")[1].strip() if qidx not in required_tables: required_tables[qidx] = list() if table in required_tables[qidx]: print(f"{table} is already in required_tables[{qidx}]") print(required_tables[qidx]) assert False required_tables[qidx].append(table) print(f"required_tables: {required_tables}") return required_tables def main_batch(argv): import argparse from pathlib import Path parser = argparse.ArgumentParser() parser.add_argument("--output_dir", type=str, help="Path to store output CSV if successfully execute.") parser.add_argument("--db_splits", type=str, help="Hwo to split tables among different databases") #OR parser.add_argument("--db_splits_file", type=str, help="Hwo to split tables among different databases") parser.add_argument("--api_key", type=str, default=openai.api_key, help="Chatgpt api key") parser.add_argument("--query_language", type=str, default="sql", # sql, eng-official, eng-manual, eng-gpt help="Query language [sql, eng-official, eng-manual, eng-gpt]") args = parser.parse_args(argv) output_dir = Path(args.output_dir) if args.db_splits_file is not None: db_splits_file = Path(args.db_splits_file) with open(db_splits_file, "r") as file: splits = [[int(x) for x in line.split()] for line in file] # [[0,0,0,1,1..], [0,1,0,...]] elif args.db_splits is not None: db_splits = args.db_splits splits = [[int(x[i]) for i in range(len(x))] for x in db_splits.split(',')] # 0001111,0101010 -> [[0,0,0,1,1..], [0,1,0,...]] else: print("Table splits among databases not given") return query_language = args.query_language if query_language not in ["sql", "eng-official", "eng-manual", "eng-gpt"]: print(f"Invalid query_language {query_language}") chatgpt_api_key = args.api_key os.environ['OPENAI_API_KEY'] = chatgpt_api_key openai.api_key = os.getenv("OPENAI_API_KEY") mysql_admin = TPCHSetup.mysql_admin() mongodb_admin = TPCHSetup.mongodb_admin() redis_admin = TPCHSetup.redis_admin() gpt = GPT() required_tables_by_query = parse_required_tables() for midx, mysql_tables, mongodb_tables, redis_tables in TPCHSetup.iter_all_mappings(splits): # all mappings # for midx, mysql_tables, mongodb_tables, redis_tables in islice(TPCHSetup.iter_all_mappings(), 3): for qidx, query_statement in TPCHSetup.iter_all_queries(query_language): # all 22 queries # for qidx, query_statement in islice(TPCHSetup.iter_all_queries(query_language), 2): required_table = required_tables_by_query[qidx] datalake = Datalake.from_tpch_mapping( "myData", mysql_admin, list(t for t in mysql_tables if t in required_table), mongodb_admin, list(t for t in mongodb_tables if t in required_table), redis_admin, list(t for t in redis_tables if t in required_table), ) prompt = Prompt(datalake) query_prompt = prompt.gen_full_prompt(query_statement, qidx, query_language) print(query_prompt) # Try mulitple times for tidx in range(0, 1): # TODO: higher? output_dir_midx = output_dir / f"m{midx}" if not os.path.exists(output_dir_midx): os.makedirs(output_dir_midx) output_path = output_dir_midx / f"m{midx}_q{qidx}_t{tidx}.txt" gpt.call_chatgpt_api(query_prompt, output_path) print(f"[{midx}, {qidx}, {tidx}] Written to {output_path}") print("=========================================================================\n") if __name__ == "__main__": import sys print(sys.argv) if len(sys.argv) <= 1 or sys.argv[1] == "file": main_file() elif sys.argv[1] == "batch": main_batch(sys.argv[2:]) else: raise ValueError(f"Invalid args: {str(sys.argv)}")
[ "\n" ]
2024-01-10
peevs99/DC-Water-LLM
wendy~src~Tuto_chatbot_csv.py
#pip install streamlit langchain openai faiss-cpu tiktoken import streamlit as st from streamlit_chat import message from langchain.embeddings.openai import OpenAIEmbeddings from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.document_loaders.csv_loader import CSVLoader from langchain.vectorstores import FAISS import tempfile user_api_key = st.sidebar.text_input( label="#### Your OpenAI API key 👇", placeholder="Paste your openAI API key, sk-", type="password") uploaded_file = st.sidebar.file_uploader("upload", type="csv") if uploaded_file : with tempfile.NamedTemporaryFile(delete=False) as tmp_file: tmp_file.write(uploaded_file.getvalue()) tmp_file_path = tmp_file.name loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8") data = loader.load() embeddings = OpenAIEmbeddings() vectors = FAISS.from_documents(data, embeddings) chain = ConversationalRetrievalChain.from_llm(llm = ChatOpenAI(temperature=0.0,model_name='gpt-3.5-turbo', openai_api_key=user_api_key), retriever=vectors.as_retriever()) def conversational_chat(query): result = chain({"question": query, "chat_history": st.session_state['history']}) st.session_state['history'].append((query, result["answer"])) return result["answer"] if 'history' not in st.session_state: st.session_state['history'] = [] if 'generated' not in st.session_state: st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " 🤗"] if 'past' not in st.session_state: st.session_state['past'] = ["Hey ! 👋"] #container for the chat history response_container = st.container() #container for the user's text input container = st.container() with container: with st.form(key='my_form', clear_on_submit=True): user_input = st.text_input("Query:", placeholder="Talk about your csv data here (:", key='input') submit_button = st.form_submit_button(label='Send') if submit_button and user_input: output = conversational_chat(user_input) st.session_state['past'].append(user_input) st.session_state['generated'].append(output) if st.session_state['generated']: with response_container: for i in range(len(st.session_state['generated'])): message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile") message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
[]
2024-01-10
szbmrk/KanBan_Board
backend~app~PythonScripts~subtask.py
# subtask.py import os import requests import sys import json def generate_subtasks(): # Replace 'YOUR_API_KEY' with your actual ChatGPT API key or token api_key = os.environ.get('OPENAI_API_KEY') prompt = sys.argv[1] max_tokens = 1000 # Adjust this value based on your requirement try: headers = { 'Authorization': f"Bearer {api_key}", 'Content-Type': 'application/json', } data = { 'prompt': prompt, 'max_tokens': max_tokens, } response = requests.post('https://api.openai.com/v1/engines/text-davinci-003/completions', headers=headers, json=data) if response.status_code != 200: return f"Error: {response.status_code}, {response.text}" responseData = response.json() if 'choices' in responseData and responseData['choices']: # Handle the response here (e.g., extract the generated subtask from the response). subtask = responseData['choices'][0]['text'] return eval(subtask) else: return 'Invalid response from OpenAI API' except Exception as e: return str(e) if __name__ == "__main__": subtask = generate_subtasks() print(subtask)
[]
2024-01-10
szbmrk/KanBan_Board
backend~app~PythonScripts~performanceMeasure.py
import openai import sys import json def generate_gpt_response(api_key, prompt): openai.api_key = api_key response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=2000 ) return response.choices[0].text.strip() if __name__ == "__main__": prompt = sys.argv[1] api_key = sys.argv[2] response = generate_gpt_response(api_key, prompt) print(response)
[]
2024-01-10
viincci/Syberlihele
actions.py
from custom_actions import CustomActions import json import openai class Action: def __init__(self): openai.api_key = "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" def generate_response(self, prompt): completions = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5, ) message = completions.choices[0].text return message class Actions(CustomActions): pass
[]
2024-01-10
nayeemhoque109/prototype
prototype%20variation%20function.py
''' requires: pip install openai pip install --upgrade openai pip install pillow pip install imageIO ''' import os from io import BytesIO import imageio import openai from datetime import datetime import base64 import requests from PIL import Image import tkinter as tk from PIL import ImageTk import requests import imageio.v2 as imageio def old_package(version, minimum): version_parts = list(map(int, version.split("."))) minimum_parts = list(map(int, minimum.split("."))) return version_parts < minimum_parts if old_package(openai.__version__, "1.2.3"): raise ValueError(f"Error: OpenAI version {openai.__version__}" " is less than the minimum version 1.2.3\n\n" ">>You should run 'pip install --upgrade openai')") from openai import OpenAI # Set your OpenAI API key api_key = "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" # Create the OpenAI client client = OpenAI(api_key=api_key) prompt = ( "Subject: planets. " "Style: cartoon." ) image_params = { "model": "dall-e-2", "n": 1, "size": "256x256", "prompt": prompt, "user": "myName", "response_format": "b64_json" } try: images_response = client.images.generate(**image_params) except Exception as e: print(f"An error occurred: {e}") raise images_dt = datetime.utcfromtimestamp(images_response.created) img_filename = images_dt.strftime('DALLE-%Y%m%d_%H%M%S') revised_prompt = images_response.data[0].revised_prompt image_data_list = [image.model_dump()["b64_json"] for image in images_response.data] image_objects = [] if image_data_list and all(image_data_list): for i, data in enumerate(image_data_list): image_objects.append(Image.open(BytesIO(base64.b64decode(data)))) image_objects[i].save(f"{img_filename}_{i}.png") print(f"{img_filename}_{i}.png was saved") else: print("No image data was obtained. Maybe bad code?") # Convert "b64_json" data to png file for i, data in enumerate(image_data_list): image = Image.open(BytesIO(base64.b64decode(data))) # Open the image image_objects.append(image) # Append the Image object to the list # Resize the image width, height = 256, 256 image = image.resize((width, height)) # Convert the image to a BytesIO object byte_stream = BytesIO() image.save(byte_stream, format='PNG') # Set byte_array to the data of the last image generated byte_array = byte_stream.getvalue() # Initialize an empty list to store the URLs of the image variations num_frames = 4 urls = [] # Create a variation for each frame in the animation for i in range(num_frames): try: image_params = { "image": byte_array, "n": 1, "model": "dall-e-2", "size": "256x256", "response_format": "url" } # Make the request to the API images_response = client.images.create_variation(**image_params) # Get the URL of the image url = images_response.data[0].url urls.append(url) except openai.OpenAIError as e: print(e.http_status) print(e.error) # Download each image from its URL and save it to a local file for i, url in enumerate(urls): response = requests.get(url) filename = f"{img_filename}_{i+1}_variation.png" # Use i+1 to generate a unique filename for each image with open(filename, 'wb') as f: f.write(response.content) print(f"{img_filename}_{i+1}_variation.png was saved") # Open the downloaded image and append it to image_objects img = Image.open(filename) image_objects.append(img) # Create a GIF from the saved images gif_filename = f"{img_filename}_variations.gif" # Adjust the duration to 0.5 seconds for a smoother animation imageio.mimsave(gif_filename, [img for img in image_objects], duration=0.5) print(f"GIF file {gif_filename} was saved")
[ "Subject: planets. Style: cartoon." ]
2024-01-10
nayeemhoque109/prototype
prototype%20generate%20function.py
''' requires: pip install openai pip install --upgrade openai pip install pillow pip install imageIO ''' import os from io import BytesIO import imageio import openai from datetime import datetime import base64 import requests from PIL import Image import tkinter as tk from PIL import ImageTk import requests import imageio.v2 as imageio def old_package(version, minimum): version_parts = list(map(int, version.split("."))) minimum_parts = list(map(int, minimum.split("."))) return version_parts < minimum_parts if old_package(openai.__version__, "1.2.3"): raise ValueError(f"Error: OpenAI version {openai.__version__}" " is less than the minimum version 1.2.3\n\n" ">>You should run 'pip install --upgrade openai')") from openai import OpenAI # Set your OpenAI API key api_key = "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" # Create the OpenAI client client = OpenAI(api_key=api_key) num_frames = 5 # Define a list of prompts prompts = [ "Subject: Full moon.", "Subject: Full moon with in the sky with stars.", "Subject: Full moon with in the sky with stars and skyline.", "Subject: Full moon with in the sky with stars and skyline from a painting in a room.", "Subject: Full moon and a little house.", ] # Initialize the list before the loop image_objects = [] # Create a variation for each image in the list for i in range(num_frames): # Use a different prompt for each image prompt = prompts[i] image_params = { "model": "dall-e-2", "n": 1, "size": "256x256", "prompt": prompt, "user": "myName", "response_format": "b64_json" } try: images_response = client.images.generate(**image_params) except Exception as e: print(f"An error occurred: {e}") raise images_dt = datetime.utcfromtimestamp(images_response.created) img_filename = images_dt.strftime('DALLE-%Y%m%d_%H%M%S') revised_prompt = images_response.data[0].revised_prompt image_data_list = [image.model_dump()["b64_json"] for image in images_response.data] # Save each image and print a message for i, data in enumerate(image_data_list): img = Image.open(BytesIO(base64.b64decode(data))) img.save(f"{img_filename}_{i}.png") print(f"{img_filename}_{i}.png was saved") image_objects.append(img) # Create a GIF from the saved images gif_filename = f"{img_filename}_variations.gif" imageio.mimsave(gif_filename, [img for img in image_objects], duration=0.5) print(f"GIF file {gif_filename} was saved") # Create a tkinter window window = tk.Tk() # Create a photo image from the GIF photo_image = tk.PhotoImage(file=gif_filename) # Create a label with the photo image label = tk.Label(window, image=photo_image) # Pack the label label.pack() # Start the tkinter main loop window.mainloop()
[ "['Subject: Full moon.', 'Subject: Full moon with in the sky with stars.', 'Subject: Full moon with in the sky with stars and skyline.', 'Subject: Full moon with in the sky with stars and skyline from a painting in a room.', 'Subject: Full moon and a little house.']" ]
2024-01-10
nayeemhoque109/prototype
prototype%20edit%20function.py
''' requires: pip install openai pip install --upgrade openai pip install pillow pip install imageIO ''' import os from io import BytesIO import imageio import openai from datetime import datetime import base64 import requests from PIL import Image import tkinter as tk from PIL import ImageTk import requests import imageio.v2 as imageio def old_package(version, minimum): version_parts = list(map(int, version.split("."))) minimum_parts = list(map(int, minimum.split("."))) return version_parts < minimum_parts if old_package(openai.__version__, "1.2.3"): raise ValueError(f"Error: OpenAI version {openai.__version__}" " is less than the minimum version 1.2.3\n\n" ">>You should run 'pip install --upgrade openai')") from openai import OpenAI # Set your OpenAI API key api_key = "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" # Create the OpenAI client client = OpenAI(api_key=api_key) prompt = ( "Subject: planets. " "Style: cartoon." ) image_params = { "model": "dall-e-2", "n": 1, "size": "256x256", "prompt": prompt, "user": "myName", "response_format": "b64_json" } try: images_response = client.images.generate(**image_params) except Exception as e: print(f"An error occurred: {e}") raise images_dt = datetime.utcfromtimestamp(images_response.created) img_filename = images_dt.strftime('DALLE-%Y%m%d_%H%M%S') revised_prompt = images_response.data[0].revised_prompt image_data_list = [image.model_dump()["b64_json"] for image in images_response.data] image_objects = [] if image_data_list and all(image_data_list): for i, data in enumerate(image_data_list): image_objects.append(Image.open(BytesIO(base64.b64decode(data)))) image_objects[i].save(f"{img_filename}_{i}.png") print(f"{img_filename}_{i}.png was saved") else: print("No image data was obtained. Maybe bad code?") # Convert "b64_json" data to png file for i, data in enumerate(image_data_list): image = Image.open(BytesIO(base64.b64decode(data))) # Open the image image_objects.append(image) # Append the Image object to the list # Resize the image width, height = 256, 256 image = image.resize((width, height)) # Convert the image to 'RGBA' format image_rgba = image.convert('RGBA') # Convert the image to a BytesIO object byte_stream = BytesIO() image_rgba.save(byte_stream, format='PNG') # Set byte_array to the data of the last image generated byte_array = byte_stream.getvalue() # Initialize an empty list to store the URLs of the image variations num_frames = 3 urls = [] # Create a variation for each frame in the animation for i in range(num_frames): try: image_params = { "image": byte_array, "prompt": prompt, "n": 1, "model": "dall-e-2", "size": "256x256", "response_format": "url" } # Make the request to the API images_response = client.images.edit(**image_params) # Get the URL of the image url = images_response.data[0].url urls.append(url) except openai.OpenAIError as e: print(e.http_status) print(e.error) # Download each image from its URL and save it to a local file for i, url in enumerate(urls): response = requests.get(url) filename = f"{img_filename}_{i+1}_variation.png" # Use i+1 to generate a unique filename for each image with open(filename, 'wb') as f: f.write(response.content) print(f"{img_filename}_{i+1}_variation.png was saved")
[ "Subject: planets. Style: cartoon." ]
2024-01-10
rafaelrcamargo/from-reddit-to-shorts
functions~uploads~youtube~youtube_upload.py
import os from datetime import datetime from pathlib import Path from googleapiclient.http import MediaFileUpload from functions.uploads.youtube.google import Create_Service from functions.utils.separator import separator # Cool Terminal Colors from rich import print from rich.console import Console # .env file from dotenv import load_dotenv # OpenAI API import openai from functions.utils.timeout import timeout load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") CLIENT_SECRET_FILE = ( str(Path(__file__).cwd()) + "/functions/uploads/youtube/client_secret.json" ) API_NAME = "youtube" API_VERSION = "v3" SCOPES = ["https://www.googleapis.com/auth/youtube.upload"] service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES) console = Console() build_path = ( str(Path(__file__).cwd()) + "/temp/build/" + datetime.today().strftime("%d_%m_%Y") ) ATTEMPTS = 10 def youtube_upload(filename): print("\n>> [yellow]Success logging in[/yellow], starting the [blue]upload![/blue]") global ATTEMPTS if filename.split(".")[1] == "mp4": print(f"\n>> [yellow]File[/yellow]: {filename}") # get last item from array name = filename.split("/")[-1].split(".")[0] subreddit = "r/" + filename.split("/")[-1].split("_")[0] completion = openai.Completion.create( model="text-curie-001", prompt=f"Write a viral funny clickbait title for a Youtube Shorts about the trending subreddit {subreddit} memes compilation:\n", temperature=0.8, max_tokens=128, top_p=1, best_of=4, frequency_penalty=0.5, presence_penalty=0.25, ) print( f">> [yellow]Prompt[/yellow]:", f"Write a viral funny clickbait title for a Youtube Shorts about the trending subreddit {subreddit} memes compilation.", ) print("\n") console.print_json(str(completion)) title = completion.choices[0].text title = title.replace("\n", " ").replace("\r", " ").replace("\t", " ").strip() print(f"\n>> [yellow]Name[/yellow]: {name}") print(f">> [yellow]Subreddit[/yellow]: {subreddit}") print(f">> [yellow]Title[/yellow]: {title}") try: request_body = { "snippet": { "categoryId": 24, "title": title + f" | {subreddit} #Shorts", "description": title + " | " + subreddit + " 🤣 | #Shorts\n\n" + "Subscribe for a cookie 🍪" + "\n\n#fun, #funny, #comedy, #meme, #trending, #memes, #nonsense, #reddit, #viral", "tags": [ "fun", "funny", "comedy", "trending", "meme", "memes", "nonsense", "reddit", "youtube", "viral", "Shorts", ], }, "status": { "privacyStatus": "public", "selfDeclaredMadeForKids": False, }, "notifySubscribers": True, } mediaFile = MediaFileUpload(build_path + "/" + name + ".mp4") try: response_upload = ( service.videos() .insert( part="snippet,status", body=request_body, media_body=mediaFile ) .execute() ) print("\n") console.print_json(str(response_upload)) print("\n>> [blue]Uploaded![/blue]") except Exception as e: if ATTEMPTS <= 0: return print(f"\n>> [red]Upload failed![/red] {str(e)}") print(f"\n>> Trying again. ({str(ATTEMPTS)} attempts left)") timeout(30, 1, "request") print("\n", separator(), "\n") ATTEMPTS -= 1 youtube_upload(filename) except Exception as e: print(f"\n>> [red]Error: {str(e)}[/red]") print("\n", separator(), "\n") else: print("\n", separator(), "\n") print( f">> [red]Why [bold]TF[/bold] is this file here?[/red]\n>> [red]File[/red]:{filename}" ) print("\n", separator(), "\n")
[ "Write a viral funny clickbait title for a Youtube Shorts about the trending subreddit PLACEHOLDER memes compilation:\n" ]
2024-01-10
crenz/Ollama-ChatDocs
Ollama-ChatDocs.py
import lib.settings as settings from dotenv import load_dotenv import os from PyPDF2 import PdfReader import streamlit as st from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings import OllamaEmbeddings from langchain.vectorstores import FAISS from langchain.chains.question_answering import load_qa_chain from langchain.llms import Ollama from langchain.callbacks import get_openai_callback from langchain.vectorstores import Chroma from langchain.chains import RetrievalQA from langchain.callbacks.manager import CallbackManager from typing import Any, Dict, List from lib.stcallbackhandler import StreamingStreamlitCallbackHandler global outputArea # Load environment variables load_dotenv() def main(): settings.init() st.title("Chat with your documents") st.write(settings.chroma._collection.count(), "documents in the database") outputArea = st.empty() clearButton = st.button('Clear') cbhandler = StreamingStreamlitCallbackHandler() cbhandler.set_output_area(outputArea) ollama = Ollama(base_url = settings.ollamaBaseURL, model = settings.ollamaModel, callback_manager = CallbackManager([cbhandler])) if clearButton: outputArea.empty() if query := st.chat_input(placeholder = "Who are you?"): with outputArea: st.spinner("Running query…") docs = settings.chroma.similarity_search(query) qachain = RetrievalQA.from_chain_type(llm = ollama, chain_type = "stuff", retriever = settings.chroma.as_retriever()) result = qachain({"query": query}) # st.write(result["result"]) if __name__ == "__main__": main()
[]
2024-01-10
crenz/Ollama-ChatDocs
pages~01_%F0%9F%93%84_Import_documents.py
import modules.settings as settings import streamlit as st import pypdf import validators from langchain.embeddings import OllamaEmbeddings from langchain.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.document_loaders import SeleniumURLLoader from langchain.vectorstores import Chroma from langchain_community.document_loaders.blob_loaders import Blob from langchain_community.document_loaders.parsers.pdf import ( PyPDFParser, ) def splitAndImport(link, data): with st.spinner("Splitting into documents…"): text_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 20) all_splits = text_splitter.split_documents(data) numDocuments = len(all_splits) + 1 progressText = "Importing " + str(numDocuments) + " documents" progressBar = st.progress(0, text = progressText) for i in range (len(all_splits)): settings.chroma.add_texts(ids=[link + "." + str(i)], metadatas=[all_splits[i].metadata], texts=[all_splits[i].page_content]) progressText = "Importing " + str(numDocuments - i) + " documents" progressBar.progress((i + 1) / numDocuments, progressText) # progressBar.progress(i + 1, all_splits[i].page_content[:100].replace("\n", " ") + "…") progressBar.empty() st.write("Finished import.") settings.init() st.set_page_config(page_title="Import documents", page_icon="📄") st.markdown("# Import documents") tabUploadPDF, tabPDF, tabHTML = st.tabs(["Upload PDF", "Import PDF from URL", "Import Website"]) with tabUploadPDF: uploadedFiles = st.file_uploader('Upload your PDF Document', type='pdf', accept_multiple_files=True) for uploadedFile in uploadedFiles: st.spinner("Importing " + uploadedFile.name) parser = PyPDFParser(extract_images=False) blob = Blob.from_data(data = uploadedFile.read(), path = uploadedFile.name) data = parser.parse(blob) reader = pypdf.PdfReader(uploadedFile) splitAndImport(uploadedFile.name, data) with tabPDF: with st.form("tabPDF_Form"): pdfLink = st.text_input('Link to PDF document', placeholder="https://example.com/example.pdf") pdfLinkSubmitted = st.form_submit_button("Import") if pdfLinkSubmitted: pdfLink = pdfLink.strip() if validators.url(pdfLink): with st.spinner("Loading " + pdfLink + "…"): loader = PyPDFLoader(pdfLink, extract_images=False) data = loader.load() splitAndImport(pdfLink, data) else: st.write("**Please input a valid URL**") with tabHTML: with st.form("tabHTML_Form"): htmlLink = st.text_input('Link to Web page', placeholder="https://example.com/") htmlLinkSubmitted = st.form_submit_button("Import") st.write("*Note: This will only import the page at the URL given. Subpages will not be crawled.*") if htmlLinkSubmitted: htmlLink = htmlLink.strip() if validators.url(htmlLink): with st.spinner("Loading " + htmlLink + "…"): loader = SeleniumURLLoader(urls=[htmlLink]) data = loader.load() splitAndImport(htmlLink, data) else: st.write("**Please input a valid URL**")
[]
2024-01-10
YLab-UChicago/gem5
configs~example~gem5_library~riscv-ubuntu-run.py
# Copyright (c) 2021 The Regents of the University of California # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer; # redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution; # neither the name of the copyright holders nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ This script shows an example of running a full system RISCV Ubuntu boot simulation using the gem5 library. This simulation boots Ubuntu 20.04 using 2 TIMING CPU cores. The simulation ends when the startup is completed successfully. Usage ----- ``` scons build/RISCV/gem5.opt ./build/RISCV/gem5.opt \ configs/example/gem5_library/riscv-ubuntu-run.py ``` """ import m5 from m5.objects import Root from gem5.utils.requires import requires from gem5.components.boards.riscv_board import RiscvBoard from gem5.components.memory import DualChannelDDR4_2400 from gem5.components.processors.simple_processor import ( SimpleProcessor, ) from gem5.components.processors.cpu_types import CPUTypes from gem5.isas import ISA from gem5.coherence_protocol import CoherenceProtocol from gem5.resources.resource import Resource from gem5.simulate.simulator import Simulator # This runs a check to ensure the gem5 binary is compiled for RISCV. requires( isa_required=ISA.RISCV, ) # With RISCV, we use simple caches. from gem5.components.cachehierarchies.classic\ .private_l1_private_l2_cache_hierarchy import ( PrivateL1PrivateL2CacheHierarchy, ) # Here we setup the parameters of the l1 and l2 caches. cache_hierarchy = PrivateL1PrivateL2CacheHierarchy( l1d_size="16kB", l1i_size="16kB", l2_size="256kB", ) # Memory: Dual Channel DDR4 2400 DRAM device. memory = DualChannelDDR4_2400(size = "3GB") # Here we setup the processor. We use a simple processor. processor = SimpleProcessor( cpu_type=CPUTypes.TIMING, isa=ISA.RISCV, num_cores=2, ) # Here we setup the board. The RiscvBoard allows for Full-System RISCV # simulations. board = RiscvBoard( clk_freq="3GHz", processor=processor, memory=memory, cache_hierarchy=cache_hierarchy, ) # Here we set the Full System workload. # The `set_kernel_disk_workload` function for the RiscvBoard accepts a # RISCV bootloader and a disk image. Once the system successfully boots, it # encounters an `m5_exit instruction encountered`. We stop the simulation then. # When the simulation has ended you may inspect `m5out/system.pc.com_1.device` # to see the stdout. board.set_kernel_disk_workload( # The RISCV bootloader will be automatically downloaded to the # `~/.cache/gem5` directory if not already present. # The riscv-ubuntu boot-test was tested with riscv-bootloader-5.10 kernel=Resource( "riscv-bootloader-vmlinux-5.10", ), # The RISCV ubuntu image will be automatically downloaded to the # `~/.cache/gem5` directory if not already present. disk_image=Resource( "riscv-ubuntu-20.04-img", ), ) simulator = Simulator(board=board) simulator.run()
[]
2024-01-10
ErdongChen61/AI-File-Assistant
src~extractor~pdf_text_extractor.py
from PyPDF2 import PdfReader from langchain.text_splitter import RecursiveCharacterTextSplitter from src.extractor.text_extractor import TextExtractor from typing import Sequence class PdfTextExtractor(TextExtractor): """ A singleton class to extract text from PDF and split it into smaller chunks. Attributes: split_chunk_size (int): Maximum size of each split text. split_chunk_overlap (int): Overlap size between adjacent split texts. split_separator (str): Separator used for splitting texts. splitter (RecursiveCharacterTextSplitter): Text splitter. """ def __init__(self) -> None: self.split_chunk_size = 512 self.split_chunk_overlap = 64 self.split_separator = "\n" self.splitter = RecursiveCharacterTextSplitter( separators=self.split_separator, chunk_size=self.split_chunk_size, chunk_overlap=self.split_chunk_overlap, length_function=len, ) def extract_texts(self, path: str) -> Sequence[str]: """ Extract text from a PDF file and split it into smaller chunks. Args: path (str): The path to the PDF file. Returns: Sequence[str]: A sequence of texts. """ text = '' with open(path, 'rb') as file: pdf = PdfReader(file) for _, page in enumerate(pdf.pages): text += page.extract_text() # Split text into smaller chunks texts = self.splitter.split_text(text) return texts
[]
2024-01-10
ErdongChen61/AI-File-Assistant
src~model~embedding_model.py
from langchain.embeddings import HuggingFaceInstructEmbeddings from src.utils.singleton import Singleton class EmbeddingModel(metaclass=Singleton): """A singleton class that loads the embedding model for embedding text.""" def __init__(self) -> None: self.model_name = 'intfloat/e5-base-v2' self.model_kwargs = {'device': 'cpu'} self.model = HuggingFaceInstructEmbeddings(model_name=self.model_name, model_kwargs=self.model_kwargs)
[]
2024-01-10
borgwang/reinforce_py
algorithms~A3C~atari~atari_env.py
# Code borrowed from OpenAI/baseliens (https://github.com/openai/baselines) # Copyright (c) 2017 OpenAI (http://openai.com) import numpy as np import gym import os from collections import deque from PIL import Image from gym import spaces DEFAULT_ENV = 'BreakoutNoFrameskip-v4' RESOLUTION = 84 S_DIM = [RESOLUTION, RESOLUTION, 1] A_DIM = gym.make(DEFAULT_ENV).action_space.n class NoopResetEnv(gym.Wrapper): def __init__(self, env, noop_max=30): """Sample initial states by taking random number of no-ops on reset. No-op is assumed to be action 0. """ gym.Wrapper.__init__(self, env) self.noop_max = noop_max self.override_num_noops = None assert env.unwrapped.get_action_meanings()[0] == 'NOOP' def _reset(self): """ Do no-op action for a number of steps in [1, noop_max].""" self.env.reset() if self.override_num_noops is not None: noops = self.override_num_noops else: noops = self.unwrapped.np_random.randint(1, self.noop_max + 1) assert noops > 0 obs = None for _ in range(noops): obs, _, done, _ = self.env.step(0) if done: obs = self.env.reset() return obs class FireResetEnv(gym.Wrapper): def __init__(self, env): """Take action on reset for environments that are fixed until firing.""" gym.Wrapper.__init__(self, env) assert env.unwrapped.get_action_meanings()[1] == 'FIRE' assert len(env.unwrapped.get_action_meanings()) >= 3 def _reset(self): self.env.reset() obs, _, done, _ = self.env.step(1) if done: self.env.reset() obs, _, done, _ = self.env.step(2) if done: self.env.reset() return obs class EpisodicLifeEnv(gym.Wrapper): def __init__(self, env): """Make end-of-life == end-of-episode, but only reset on true game over. Done by DeepMind for the DQN and co. since it helps value estimation. """ gym.Wrapper.__init__(self, env) self.lives = 0 self.was_real_done = True def _step(self, action): obs, reward, done, info = self.env.step(action) self.was_real_done = done # check current lives, make loss of life terminal, # then update lives to handle bonus lives lives = self.env.unwrapped.ale.lives() if lives < self.lives and lives > 0: # for Qbert somtimes we stay in lives == 0 condtion for a few frames # so its important to keep lives > 0, so that we only reset once # the environment advertises done. done = True self.lives = lives return obs, reward, done, info def _reset(self): """Reset only when lives are exhausted. This way all states are still reachable even though lives are episodic, and the learner need not know about any of this behind-the-scenes. """ if self.was_real_done: obs = self.env.reset() else: # no-op step to advance from terminal/lost life state obs, _, _, _ = self.env.step(0) self.lives = self.env.unwrapped.ale.lives() return obs class MaxAndSkipEnv(gym.Wrapper): def __init__(self, env, skip=4): """Return only every `skip`-th frame""" gym.Wrapper.__init__(self, env) # most recent raw observations (for max pooling across time steps) self._obs_buffer = deque(maxlen=2) self._skip = skip def _step(self, action): """Repeat action, sum reward, and max over last observations.""" total_reward = 0.0 done = None for _ in range(self._skip): obs, reward, done, info = self.env.step(action) self._obs_buffer.append(obs) total_reward += reward if done: break max_frame = np.max(np.stack(self._obs_buffer), axis=0) return max_frame, total_reward, done, info def _reset(self): """Clear past frame buffer and init. to first obs. from inner env.""" self._obs_buffer.clear() obs = self.env.reset() self._obs_buffer.append(obs) return obs class ClipRewardEnv(gym.RewardWrapper): def _reward(self, reward): """Bin reward to {+1, 0, -1} by its sign.""" return np.sign(reward) class WarpFrame(gym.ObservationWrapper): def __init__(self, env): """Warp frames to 84x84 as done in the Nature paper and later work.""" gym.ObservationWrapper.__init__(self, env) self.res = RESOLUTION self.observation_space = spaces.Box(low=0, high=255, shape=(self.res, self.res, 1)) def _observation(self, obs): frame = np.dot(obs.astype('float32'), np.array([0.299, 0.587, 0.114], 'float32')) frame = np.array(Image.fromarray(frame).resize((self.res, self.res), resample=Image.BILINEAR), dtype=np.uint8) return frame.reshape((self.res, self.res, 1)) class FrameStack(gym.Wrapper): def __init__(self, env, k): """Buffer observations and stack across channels (last axis).""" gym.Wrapper.__init__(self, env) self.k = k self.frames = deque([], maxlen=k) shp = env.observation_space.shape assert shp[2] == 1 # can only stack 1-channel frames self.observation_space = spaces.Box(low=0, high=255, shape=(shp[0], shp[1], k)) def _reset(self): """Clear buffer and re-fill by duplicating the first observation.""" ob = self.env.reset() for _ in range(self.k): self.frames.append(ob) return self._observation() def _step(self, action): ob, reward, done, info = self.env.step(action) self.frames.append(ob) return self._observation(), reward, done, info def _observation(self): assert len(self.frames) == self.k return np.concatenate(self.frames, axis=2) def wrap_deepmind(env, episode_life=True, clip_rewards=True): """Configure environment for DeepMind-style Atari. Note: this does not include frame stacking!""" assert 'NoFrameskip' in env.spec.id # required for DeepMind-style skip if episode_life: env = EpisodicLifeEnv(env) env = NoopResetEnv(env, noop_max=30) env = MaxAndSkipEnv(env, skip=4) if 'FIRE' in env.unwrapped.get_action_meanings(): env = FireResetEnv(env) env = WarpFrame(env) if clip_rewards: env = ClipRewardEnv(env) return env def make_env(args, record_video=False): env = gym.make(DEFAULT_ENV) if record_video: video_dir = os.path.join(args.save_path, 'videos') if not os.path.exists(video_dir): os.makedirs(video_dir) env = gym.wrappers.Monitor( env, video_dir, video_callable=lambda x: True, resume=True) return wrap_deepmind(env)
[]
2024-01-10
wan19990901/test
myapp.py
import pandas as pd import dash from dash import dcc from dash import html from dash.dependencies import Input, Output import plotly.express as px import plotly.graph_objects as go from openai import OpenAI import json countbyyear= pd.read_csv('countbyyear.csv') bird_list = [{'label': y, 'value': y} for x, y in zip(countbyyear['species_id'], countbyyear['species_name'])] state_list = [{'label': x, 'value': y} for x, y in zip(countbyyear['iso_subdivision'], countbyyear['iso_subdivision'])] def drop_duplicate_dicts(list_of_dicts): unique_dicts = set() result = [] for d in list_of_dicts: # Convert the dictionary to a JSON string json_str = json.dumps(d, sort_keys=True) # Check if this JSON representation has already been added if json_str not in unique_dicts: unique_dicts.add(json_str) result.append(d) return result bird_list = drop_duplicate_dicts(bird_list) state_list = drop_duplicate_dicts(state_list) iso_subdivisions = countbyyear['iso_subdivision'].unique() external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] mymarkdown = ''' This is my final project on bird banding dataset, I hope you can find the birds you are interested in and know about where to see them. Here are my data sources. * [North American Bird Banding Program](https://www.sciencebase.gov/catalog/) ''' countbyyear[countbyyear.species_name == 'Blue Goose'] countbyyear.head() app = dash.Dash(__name__, external_stylesheets=external_stylesheets) # specification of what goes on app.layout = html.Div( [ # Stuff on Top html.H1("See where the birds were and where they are going!"), html.H2("This is a final project of University of Virginia Data Engineering Course."), html.H3("M.Y."), # Side Bar html.Div([ dcc.Markdown('Please select the bird you like!'), dcc.Dropdown(id='species_name', options=bird_list, value='Lesser Snow Goose'), # New Dropdown for States dcc.Markdown(mymarkdown) ], style={'width': '24%', 'float': 'left'}), ###### Main bar for birds### html.Div([ dcc.Tabs([ dcc.Tab(label = 'Bird Image', children = [ html.Div([html.Img(id = 'birdimage', style={'height':'100%', 'width':'100%'})], style = {'width': '24%', 'float':'left'}), html.Div([dcc.Graph(id = 'heatmap')], style = {'width': '74%', 'float':'right'}) ]), dcc.Tab(label='Population Over Time', value='tab1', children=[ dcc.Checklist( id='state-checklist', options=[{'label': i, 'value': i} for i in iso_subdivisions], value=list(iso_subdivisions), labelStyle={'display': 'block'} # Select all states by default ), dcc.Graph(id='population-over-time')]), dcc.Tab(label = 'Birds Distributed Over States', children = [ dcc.Graph(id = 'timeslider', style = {'height': '100%', 'width': '100%'}) ]) ]) ], style = {'width': '74%', 'float': 'right'}) ] ) @app.callback( Output('state-checklist', 'options'), [Input('species_name', 'value')] ) def update_state_checklist(selected_species): # You may want to replace this with logic to fetch states based on the selected species available_states = countbyyear[countbyyear['species_name'] == selected_species]['iso_subdivision'].unique() return [{'label': state, 'value': state} for state in iso_subdivisions] # Callback to update the line plot based on selected states @app.callback( Output('population-over-time', 'figure'), [Input('species_name', 'value'), Input('state-checklist', 'value')] ) def update_population_plot(selected_species, selected_states): data = countbyyear[countbyyear.species_name == selected_species] filtered_data = data[(data['species_name'] == selected_species) & (data['iso_subdivision'].isin(selected_states))] fig = px.line(filtered_data, x='event_year', y='item_count', color='iso_subdivision', title=f'Population of {selected_species} Over Time') return fig ######################################################################### @app.callback([Output(component_id = 'birdimage', component_property = 'src')], [Input(component_id = 'species_name', component_property = 'value')]) def birdimage(b): OPENAI_API_KEY=openaikey client = OpenAI(api_key = OPENAI_API_KEY) response = client.images.generate( model="dall-e-3", prompt=f"a realistic photo of {b} with no text", size="1024x1024", quality="standard", n=1, ) image_url = response.data[0].url return [image_url] ######################################################################### ######################################################################### @app.callback([Output(component_id = 'timeslider', component_property = 'figure')], [Input(component_id = 'species_name', component_property = 'value')]) def timeslider(b): df_sel = countbyyear[countbyyear.species_name == b] #df =df[df.species_name == b] # Create a list of all years years = sorted(df_sel['event_year'].unique()) # Creating the figure fig = go.Figure() # Add one trace for each year for year in years: df_year = df_sel[df_sel['event_year'] == year] fig.add_trace( go.Choropleth( locations=df_year['iso_subdivision'], z=df_year['item_count'], text=df_year['species_name'], colorscale='Viridis', autocolorscale=False, showscale=True, #geojson = 'us-states.json', geojson='https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json', featureidkey="properties.name" ) ) # Make all traces invisible at the start for trace in fig.data: trace.visible = False # Make the first trace visible fig.data[0].visible = True # Create and add slider steps = [] for i, year in enumerate(years): step = dict( method="update", args=[{"visible": [False] * len(years)}, {"title": f"Item count for year: {year}"}], label=str(year) ) step["args"][0]["visible"][i] = True steps.append(step) sliders = [dict( active=0, currentvalue={"prefix": "Year: "}, pad={"t": 50}, steps=steps )] fig.update_layout( sliders=sliders, title='Birds population changes over time', height=600, width=1000 ) return [fig] ######################################################################### @app.callback([Output(component_id = 'heatmap', component_property = 'figure')], [Input(component_id = 'species_name', component_property = 'value')]) def heatmap(b): myquery = pd.read_csv('heatmap.csv') forheat = myquery[myquery.species_name == b] target_time = 2022 target_df = forheat[forheat['event_year'] == target_time] if not target_df.empty: # Create a Plotly Figure target_df['hover_text'] = target_df.apply(lambda row: f"Geo: {row['iso_subdivision']}, Lat: {row['lat_dd']}, Lon: {row['lon_dd']}", axis=1) fig = px.scatter_geo(target_df, lat='lat_dd', lon='lon_dd', hover_name='hover_text', size='item_count', projection='mercator', title=f'Heatmap of bird population at {target_time}') return [fig] else: # Handle empty DataFrame case return px.scatter_geo(title='No Data Available') ######################################################################### if __name__ == "__main__": app.run_server(host = '0.0.0.0', port = '8050', debug = True)
[]
2024-01-10
aquinteros/wiki-summarize
resources~functions.py
import pandas as pd import wikipediaapi import openai import json # import requests import streamlit as st import time from streamlit_extras.colored_header import colored_header from streamlit_extras.buy_me_a_coffee import button as buy_me_a_coffee from streamlit_extras.mention import mention from urllib.parse import urlparse st.set_page_config(page_title="Wiki Summary", page_icon="📚", initial_sidebar_state="expanded") categorias = pd.read_csv('resources/cat.csv', header=None, index_col=0).index.tolist() @st.cache_resource() def set_openai_api_key(api_key: str): try: st.session_state["OPENAI_API_KEY"] = api_key openai.api_key = st.session_state["OPENAI_API_KEY"] models = pd.json_normalize(openai.Engine.list(), record_path=['data']) model_list = models[(models['owner'] == 'openai') & (models['ready'] == True) & (models['id'].str.contains('gpt'))].id return model_list except Exception as e: st.error(e) return [] def get_completion(prompt, model="gpt-3.5-turbo", temperature=0, num_retries=5, sleep_time=10): """function to return content from the openai api prompt""" messages = [{"role": "user", "content": prompt}] for i in range(num_retries): try: response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, ) break except Exception as e: print(f"Retry {i+1}/{num_retries} failed. Error: {e}") time.sleep(sleep_time) return response.choices[0].message["content"] def import_wiki_page(page_name, language): """Importa una página de wikipedia, dado el nombre de la página y el idioma del artículo""" headers = {'User-Agent': 'wiki-summarize/0.0 (https://wiki-summarize.streamlit.app/; [email protected])'} wiki = wikipediaapi.Wikipedia(language, headers=headers) page = wiki.page(page_name) exists = page.exists() summary = page.summary url = page.fullurl sections = page.sections return page_name, exists, summary, url, sections def get_summary(page_name, summary, model, language): """Trae un resumen del resumen de una página de wikipedia dada el nombre de la página, el texto del resumen y el idioma del artículo""" prompt = f""" Tu tarea es generar un resumen corto de un Artículo de wikipedia sobre {page_name} delimitado en triple comillas simples en no más de 40 palabras Conserva el tono informativo e impersonal del artículo. Omite información de poca relevancia. Clasifíca el artículo en una de las siguientes categorías: {categorias}. Deriva una lista de como máximo 5 keywords principales del artículo. Evita el nombre del artículo como keyword. El idioma del output debe ser '{language}' que es el mismo idioma del artículo. El formato de salida SIEMPRE debe ser JSON con los siguientes valores de llave: [summary, category, keywords]. Artículo: '''{summary}''' """ if len(prompt) > 20000: prompt = prompt[:20000] + "'''" response = json.loads(get_completion(prompt, model).replace('==', '').replace('$ ', '$').replace('# ', '#')) return response['summary'], response['category'], response['keywords'] def get_section_summary(page_name, section, model, language): """Trae summary de una sección de un artículo en wikipedia, dado el nombre de la página, el texto de la sección y el idioma del artículo""" prompt = f""" Tu tarea es generar un resumen corto de una sección de un Artículo de wikipedia sobre {page_name} delimitada en triple comillas simples en no más de 40 palabras Conserva el tono informativo e impersonal de la sección. Omite información de poca relevancia, no incluyas información de otras secciones. El formato de salida debe ser texto plano en el idioma '{language}' que es el mismo idioma del artículo. Artículo: '''{section}''' """ if len(prompt) > 20000: prompt = prompt[:20000] + "'''" response = get_completion(prompt, model).replace('==', '').replace('$ ', '$').replace('# ', '#') return response def return_summary(page_name, model, progress, language): """Trae un resumen de una página de wikipedia, dado el nombre de la página y el idioma del artículo""" page_name, exists, summary, url, sections = import_wiki_page(page_name, language) if exists: summary, category, keywords = get_summary(page_name, summary, model, language) full_text = '' full_text += '# Summary' + '\n' full_text += summary + '\n' full_text += '# Category' + '\n' full_text += category + '\n' full_text += '# Keywords' + '\n' full_text += ', '.join(keywords) + '\n' full_text += '# URL' + '\n' full_text += '<' + url + '>' + '\n' excluded_titles = ['Referencias', 'Véase también', 'Enlaces externos', 'Fuentes', 'Notas', 'Bibliografía', 'Notes', 'References', 'External links', 'See also', 'Further reading' ,'Sources'] full_text += '# Sections' + '\n' for section in sections: if section.title not in excluded_titles: full_text += '## ' + section.title + '\n' full_text += get_section_summary(page_name, section.full_text, model, language) + '\n' progress.progress(sections.index(section)/len(sections)) full_text += '\n' + '``imported from wikipedia and summarized by openai using <https://wiki-summarize.streamlit.app/>``' return full_text else: return "The page doesn't exist"
[ "\n Tu tarea es generar un resumen corto de un Artículo de wikipedia sobre PLACEHOLDER delimitado en triple comillas simples en no más de 40 palabras\n Conserva el tono informativo e impersonal del artículo.\n Omite información de poca relevancia.\n Clasifíca el artículo en una de las siguientes categorías: PLACEHOLDER.\n Deriva una lista de como máximo 5 keywords principales del artículo. Evita el nombre del artículo como keyword.\n El idioma del output debe ser 'PLACEHOLDER' que es el mismo idioma del artículo.\n El formato de salida SIEMPRE debe ser JSON con los siguientes valores de llave:\t[summary, category, keywords].\n Artículo: '''PLACEHOLDER'''\n ", "\n Tu tarea es generar un resumen corto de una sección de un Artículo de wikipedia sobre PLACEHOLDER delimitada en triple comillas simples en no más de 40 palabras\n Conserva el tono informativo e impersonal de la sección.\n Omite información de poca relevancia, no incluyas información de otras secciones.\n El formato de salida debe ser texto plano en el idioma 'PLACEHOLDER' que es el mismo idioma del artículo.\n Artículo: '''PLACEHOLDER'''\n ", "PLACEHOLDER'''" ]
2024-01-10
Day-Go/Prostagma
python_grpc~src~services~data_agent.py
import os import openai class DataAgent: def __init__(self) -> None: self.key = os.environ.get('OPENAI_API_KEY') openai.api_key = self.key self.model = 'gpt-3.5-turbo-0613' self.fncs = [ { "name": "_get_civ_property", "description": "Get the selected property for a civilization", "parameters": { "type": "object", "properties": { "civ": { "type": "string", "description": "The name of the civilization" }, "property": { "type": "string", "description": "The name of the property. Either type, bonuses, unique_units, unique_techs, or team_bonus" } }, "required": ["civ", "property"] } } ] def get_data(self, statment: str) -> list[str]: self.messages = [ { "role": "user", "content": f"{statment}" } ] completion = openai.ChatCompletion.create( model=self.model, messages=self.messages, functions=self.fncs, function_call="auto" ) print(completion)
[ "PLACEHOLDER" ]
2024-01-10
Day-Go/Prostagma
python_grpc~src~audio~audio_parser.py
import os import time import openai from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler class AudioParser(PatternMatchingEventHandler): patterns = ["*.wav"] def on_created(self, event): print(f"{event.src_path} has been added") time.sleep(10) audio_file= open(f"{event.src_path}", "rb") transcript = openai.Audio.transcribe("whisper-1", audio_file) print(transcript)
[]
2024-01-10
Day-Go/Prostagma
python_grpc~src~services~coach_agent.py
import os import openai import json from .data_access import DataAccess class CoachAgent: def __init__(self): self.data_access = DataAccess('aoe.db') self.key = os.environ.get('OPENAI_API_KEY') openai.api_key = self.key self.model = 'gpt-3.5-turbo-0613' self.fnc = [ { "name": "_get_civ_property", "description": "Get the selected property for a civilization", "parameters": { "type": "object", "properties": { "civ": { "type": "string", "description": "The name of the civilization" }, "property": { "type": "string", "description": "The name of the property. Either type, bonuses, unique_units, unique_techs, or team_bonus" } }, "required": ["civ", "property"] } }, { "name": "check_unit_availability", "description": "Check whether a unit is available to a civilization", "parameters": { "type": "object", "properties": { "civ": { "type": "string", "description": "The name of the civilization" }, "unit": { "type": "string", "description": "The name of the unit" } }, "required": ["civ", "unit"] } } ] self.available_functions = { "_get_civ_property": self.data_access._get_civ_property, "check_unit_availability": self.data_access.check_unit_availability } self.messages = [{"role": "system", "content": "You are an Age of Empires 2 chatbot that helps users learn and strategize." + " Keep answers concise and do not mention the game by name. Do not embellish the answer."}, {"role": "assistant", "content": "I understand. Awaiting user questions."}] def prompt(self, message: str): self.messages.append({"role": "user", "content": message}) def get_chat_completion(self): completion = openai.ChatCompletion.create( model=self.model, messages=self.messages, functions=self.fnc, function_call="auto") return completion def process_chat_completion(self, completion): response_message = completion["choices"][0]["message"] if response_message.get("function_call"): function_name = response_message["function_call"]["name"] function_to_call = self.available_functions[function_name] function_args = json.loads(response_message["function_call"]["arguments"]) if function_to_call.__name__ == "_get_civ_property": function_response = function_to_call( civ=function_args.get("civ"), property=function_args.get("property") ) elif function_to_call.__name__ == "check_unit_availability": function_response = function_to_call( civ=function_args.get("civ"), unit=function_args.get("unit") ) self.messages.append(response_message) self.messages.append( { "role": "function", "name": function_name, "content": function_response, } ) print(self.messages) second_response = openai.ChatCompletion.create( model=self.model, messages=self.messages, ) return second_response return None def handle_chat(self): completion = self.get_chat_completion() print(completion) second_response = self.process_chat_completion(completion) if second_response: print(second_response)
[ "You are an Age of Empires 2 chatbot that helps users learn and strategize.", " Keep answers concise and do not mention the game by name. Do not embellish the answer.", "I understand. Awaiting user questions." ]
2024-01-10
odunola499/food_bro
apis~_init_model.py
from transformers import ( AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig) import weaviate from sentence_transformers import SentenceTransformer from prompts import OPENAI_PROMPT_TEMPLATE,SIMPLE_PREDICTION_OPENAI_PROMPT_TEMPLATE, SIMPLE_PREDICTION_OPENAI_PROMPT_TEMPLATE, RETRIEVER_PROMPT_TEMPLATE from peft import PeftModel, PeftConfig from transformers import AutoModelForCausalLM, AutoTokenizer import openai # Load the Lora model # we could for starters tell the user to b as detailed with their request as they can class Models: def __init__(self): use_4bit = True bnb_4bit_compute_dtype = "float16" bnb_4bit_quant_type = "nf4" use_nested_quant = False bnb_config = BitsAndBytesConfig( load_in_4bit = use_4bit, bnb_4bit_quant_type=bnb_4bit_quant_type, bnb_4bit_compute_dtype=bnb_4bit_compute_dtype, bnb_4bit_use_double_quant = use_nested_quant, ) peft_model_id = "odunola/bloomz_reriever_instruct" config = PeftConfig.from_pretrained(peft_model_id) rephrase_model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, quantization_config = bnb_config, device_map = 'auto') self.tokenizer2 = AutoTokenizer.from_pretrained(config.base_model_name_or_path) self.llm2 = PeftModel.from_pretrained(rephrase_model, peft_model_id) self.semantic_model = SentenceTransformer('thenlper/gte-large') self.client = weaviate.Client( url="https://testing-area-4ps7dhgv.weaviate.network", #for testing ) async def retrieve(self, query: str) -> list:#this gets the context from the vector db prompt = f"To generate a representation for this sentence for use in retrieving related articles: {query}" query_vector = self.semantic_model.encode(prompt) response = self.client.query.get( "Recipes", ["texts"] ).with_limit(3).with_near_vector( {'vector': query_vector} ).do() res = response['data']['Get']['Recipes'] return [i['texts'] for i in res] async def reply(self, query:str, contexts: list) -> str: context_str = "\n".join(contexts) user_prompt = OPENAI_PROMPT_TEMPLATE.format(context_str = context_str,query =query) chat_completion = openai.Completion.create(model="gpt-3.5-turbo-instruct", prompt=user_prompt, max_tokens = 500, temperature = 0.7) response = chat_completion['choices'][0]['text'] return response async def predict(self, text: str) -> str: chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role":"system", "content":SIMPLE_PREDICTION_OPENAI_PROMPT_TEMPLATE},{"role": "user", "content": text}]) response = chat_completion['choices'][0]['message']['content'] return response async def generate_interpretation(self,text:str) -> str: prompt = RETRIEVER_PROMPT_TEMPLATE.format(request = text) tokens = self.tokenizer2(prompt, return_tensors = 'pt') outputs = self.llm2.generate(input_ids = tokens['input_ids'].to('cuda'), temperature = 0.2, max_length = 200, do_sample = True) response = self.tokenizer2.decode(outputs[0], skip_special_tokens=True).split('Interpretation:')[-1] return response
[ "To generate a representation for this sentence for use in retrieving related articles: PLACEHOLDER" ]
2024-01-10
odunola499/food_bro
app~build_index.py
from datasets import load_dataset from langchain.vectorstores import Chroma from langchain.embeddings.huggingface import HuggingFaceBgeEmbeddings import os import langchain.schema as lc_schema if 'chroma_db' not in os.listdir('.'): large_context_df = load_dataset('odunola/foodie-large-context') small_context_df = load_dataset('odunola/foodie-small-context') texts_1 = large_context_df['train']['texts'] texts_2 = small_context_df['train']['texts'] #build embedding function, adjust bath_size to optimise or accommodate GPU VRAM embedding_function = HuggingFaceBgeEmbeddings(model_name='thenlper/gte-large', encode_kwargs = {"batch_size":64, "show_progress_bar":True}) large_documents = [lc_schema.Document(page_content = i) for i in texts_1] small_documents = [lc_schema.Document(page_content = i) for i in texts_2] #build chroma database and make persistted in storage index_1 = Chroma.from_documents(large_documents, embedding_function, persist_directory="./chroma_db", collection_name = "foodie") index_2 = Chroma.from_documents(small_documents, embedding_function, persist_directory="./chroma_db", collection_name = 'foodie_small')
[]
2024-01-10
odunola499/food_bro
app~guardrails.py
from utils import Func from langchain.llms import OpenAI import warnings warnings.filterwarnings('ignore') import streamlit as st from transformers import pipeline from langchain.prompts import PromptTemplate from langchain.chains import LLMChain func = Func() st.set_page_config(layout="wide") st.title("Food Recommendation Engine") pipe = pipeline('text-classification','odunola/guardrail_distilled') def greet(question): greeting_template = """ Your name if Chef Lock, you are a highly knowledgeable chef with decades of experience and you are well versed in helping anyone create recipes, reccommend food or cuisine based on what they ask and designing food plans. A user asks you the following question. Respond to this question with full knowledge of who you are an your capability. Question: {question} Response: """ model = OpenAI(temperature = 0.9, model = 'gpt-3.5-turbo-instruct', max_tokens = 256) greeting_prompt = PromptTemplate.from_template(greeting_template) chain = LLMChain(llm=model, prompt=greeting_prompt) result = chain.run(question = question) return result def unwanted_request(question): template = """ Your name if Chef Lock, you are a highly knowledgeable chef with decades of experience and you are well versed in helping anyone create recipes, reccommend food or cuisine based on what they ask and designing food plans. A user asks you the following question or give you a text. This particular question that no body should ask you and it is unwise of you to answer this. Respectively give a response that doesnt answer the question but turns down the request and ensure you also educatethe user on questions you can actually answer such as food related questions Question: {question} Response: """ model = OpenAI(temperature = 0.9, model = 'gpt-3.5-turbo-instruct', max_tokens = 256) prompt = PromptTemplate.from_template(template) chain = LLMChain(llm=model, prompt=prompt) result = chain.run(question = question) return result def short_question(user_input): result = func.RAG_short_content(user_input) st.write(result[0]) with st.expander("See retrieved context"): st.write(result[1]) def long_question(user_input): result = func.RAG_large_content(user_input) st.write(result[0]) with st.expander("See retrieved context"): st.write(result[1]) user_input = st.text_input("Enter Prompt") if user_input: with st.spinner('loading...'): result = pipe(user_input) if result[0]['label'] == 'LABEL_2': st.write(unwanted_request(user_input)) elif result[0]['label'] == 'LABEL_0': short_question(user_input) elif result[0]['label'] == 'LABEL_1': long_question(user_input) elif result[0]['label'] == 'LABEL_3': st.write(greet(user_input))
[ "\n Your name if Chef Lock, you are a highly knowledgeable chef with decades of experience and you are well versed in helping anyone create recipes, reccommend food or cuisine based on what they ask and designing food plans. \n A user asks you the following question or give you a text. This particular question that no body should ask you and it is unwise of you to answer this. Respectively give a response that doesnt answer the question but turns down the request and ensure you also educatethe user on questions you can actually answer such as food related questions\n Question: {question}\n Response: \n ", "\n Your name if Chef Lock, you are a highly knowledgeable chef with decades of experience and you are well versed in helping anyone create recipes, reccommend food or cuisine based on what they ask and designing food plans. \n A user asks you the following question. Respond to this question with full knowledge of who you are an your capability.\n Question: {question}\n Response:\n " ]
2024-01-10
grand-cat-unknown/marvin
src~marvin~components~ai_function.py
import asyncio import inspect import json from typing import ( TYPE_CHECKING, Any, Awaitable, Callable, Generic, Optional, TypeVar, Union, overload, ) from pydantic import BaseModel, Field, ValidationError from typing_extensions import ParamSpec, Self from marvin.components.prompt import PromptFunction from marvin.serializers import create_tool_from_type from marvin.utilities.asyncio import ( ExposeSyncMethodsMixin, expose_sync_method, run_async, ) from marvin.utilities.jinja import ( BaseEnvironment, ) from marvin.utilities.logging import get_logger if TYPE_CHECKING: from openai.types.chat import ChatCompletion T = TypeVar("T") P = ParamSpec("P") class AIFunction(BaseModel, Generic[P, T], ExposeSyncMethodsMixin): fn: Optional[Callable[P, T]] = None environment: Optional[BaseEnvironment] = None prompt: Optional[str] = Field(default=inspect.cleandoc(""" Your job is to generate likely outputs for a Python function with the following signature and docstring: {{_source_code}} The user will provide function inputs (if any) and you must respond with the most likely result. user: The function was called with the following inputs: {%for (arg, value) in _arguments.items()%} - {{ arg }}: {{ value }} {% endfor %} What is its output? """)) name: str = "FormatResponse" description: str = "Formats the response." field_name: str = "data" field_description: str = "The data to format." render_kwargs: dict[str, Any] = Field(default_factory=dict) create: Optional[Callable[..., "ChatCompletion"]] = Field(default=None) def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Union[T, Awaitable[T]]: if self.fn is None: raise NotImplementedError from marvin import settings logger = get_logger("marvin.ai_fn") logger.debug_kv( "AI Function Call", f"Calling {self.fn.__name__} with {args} and {kwargs}", "blue", ) is_async_fn = asyncio.iscoroutinefunction(self.fn) call = "async_call" if is_async_fn else "sync_call" create = ( self.create or settings.openai.chat.completions.acreate if is_async_fn else settings.openai.chat.completions.create ) result = getattr(self, call)(create, *args, **kwargs) logger.debug_kv("AI Function Call", f"Returned {result}", "blue") return result async def async_call( self, acreate: Callable[..., Awaitable[Any]], *args: P.args, **kwargs: P.kwargs ) -> T: _response = await acreate(**self.as_prompt(*args, **kwargs).serialize()) return self.parse(_response) def sync_call( self, create: Callable[..., Any], *args: P.args, **kwargs: P.kwargs ) -> T: _response = create(**self.as_prompt(*args, **kwargs).serialize()) return self.parse(_response) def parse(self, response: "ChatCompletion") -> T: tool_calls = response.choices[0].message.tool_calls if tool_calls is None: raise NotImplementedError if self.fn is None: raise NotImplementedError arguments = tool_calls[0].function.arguments tool = create_tool_from_type( _type=self.fn.__annotations__["return"], model_name=self.name, model_description=self.description, field_name=self.field_name, field_description=self.field_description, ).function if not tool or not tool.model: raise NotImplementedError try: return getattr(tool.model.model_validate_json(arguments), self.field_name) except ValidationError: # When the user provides a dict obj as a type hint, the arguments # are returned usually as an object and not a nested dict. _arguments: str = json.dumps({self.field_name: json.loads(arguments)}) return getattr(tool.model.model_validate_json(_arguments), self.field_name) @expose_sync_method("map") async def amap(self, *map_args: list[Any], **map_kwargs: list[Any]) -> list[T]: """ Map the AI function over a sequence of arguments. Runs concurrently. A `map` twin method is provided by the `expose_sync_method` decorator. You can use `map` or `amap` synchronously or asynchronously, respectively, regardless of whether the user function is synchronous or asynchronous. Arguments should be provided as if calling the function normally, but each argument must be a list. The function is called once for each item in the list, and the results are returned in a list. For example, fn.map([1, 2]) is equivalent to [fn(1), fn(2)]. fn.map([1, 2], x=['a', 'b']) is equivalent to [fn(1, x='a'), fn(2, x='b')]. """ tasks: list[Any] = [] if map_args and map_kwargs: max_length = max( len(arg) for arg in (map_args + tuple(map_kwargs.values())) ) elif map_args: max_length = max(len(arg) for arg in map_args) else: max_length = max(len(v) for v in map_kwargs.values()) for i in range(max_length): call_args = [arg[i] if i < len(arg) else None for arg in map_args] call_kwargs = ( {k: v[i] if i < len(v) else None for k, v in map_kwargs.items()} if map_kwargs else {} ) tasks.append(run_async(self, *call_args, **call_kwargs)) return await asyncio.gather(*tasks) def as_prompt( self, *args: P.args, **kwargs: P.kwargs, ) -> PromptFunction[BaseModel]: return PromptFunction[BaseModel].as_function_call( fn=self.fn, environment=self.environment, prompt=self.prompt, model_name=self.name, model_description=self.description, field_name=self.field_name, field_description=self.field_description, **self.render_kwargs, )(*args, **kwargs) @overload @classmethod def as_decorator( cls: type[Self], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", acreate: Optional[Callable[..., Awaitable[Any]]] = None, **render_kwargs: Any, ) -> Callable[P, Self]: pass @overload @classmethod def as_decorator( cls: type[Self], fn: Callable[P, T], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", acreate: Optional[Callable[..., Awaitable[Any]]] = None, **render_kwargs: Any, ) -> Self: pass @classmethod def as_decorator( cls: type[Self], fn: Optional[Callable[P, T]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", **render_kwargs: Any, ) -> Union[Callable[[Callable[P, T]], Self], Self]: def decorator(func: Callable[P, T]) -> Self: return cls( fn=func, environment=environment, name=model_name, description=model_description, field_name=field_name, field_description=field_description, **({"prompt": prompt} if prompt else {}), **render_kwargs, ) if fn is not None: return decorator(fn) return decorator @overload def ai_fn( *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", **render_kwargs: Any, ) -> Callable[[Callable[P, T]], Callable[P, T]]: pass @overload def ai_fn( fn: Callable[P, T], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", **render_kwargs: Any, ) -> Callable[P, T]: pass def ai_fn( fn: Optional[Callable[P, T]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, model_name: str = "FormatResponse", model_description: str = "Formats the response.", field_name: str = "data", field_description: str = "The data to format.", **render_kwargs: Any, ) -> Union[Callable[[Callable[P, T]], Callable[P, T]], Callable[P, T]]: if fn is not None: return AIFunction.as_decorator( # type: ignore fn=fn, environment=environment, prompt=prompt, model_name=model_name, model_description=model_description, field_name=field_name, field_description=field_description, **render_kwargs, ) def decorator(func: Callable[P, T]) -> Callable[P, T]: return AIFunction.as_decorator( # type: ignore fn=func, environment=environment, prompt=prompt, model_name=model_name, model_description=model_description, field_name=field_name, field_description=field_description, **render_kwargs, ) return decorator
[ "\n Your job is to generate likely outputs for a Python function with the\n following signature and docstring:\n\n {{_source_code}}\n\n The user will provide function inputs (if any) and you must respond with\n the most likely result.\n\n user: The function was called with the following inputs:\n {%for (arg, value) in _arguments.items()%}\n - {{ arg }}: {{ value }}\n {% endfor %}\n\n What is its output?\n " ]
2024-01-10
grand-cat-unknown/marvin
src~marvin~settings.py
import os from contextlib import contextmanager from typing import TYPE_CHECKING, Any, Literal, Optional, Union from pydantic import Field, SecretStr from pydantic_settings import BaseSettings, SettingsConfigDict if TYPE_CHECKING: from openai import AsyncClient, Client from openai._base_client import HttpxBinaryResponseContent from openai.types.chat import ChatCompletion from openai.types.images_response import ImagesResponse class MarvinSettings(BaseSettings): model_config = SettingsConfigDict( env_prefix="marvin_", env_file="~/.marvin/.env", extra="allow", arbitrary_types_allowed=True, ) def __setattr__(self, name: str, value: Any) -> None: """Preserve SecretStr type when setting values.""" field = self.model_fields.get(name) if field: annotation = field.annotation base_types = ( annotation.__args__ if getattr(annotation, "__origin__", None) is Union else (annotation,) ) if SecretStr in base_types and not isinstance(value, SecretStr): value = SecretStr(value) super().__setattr__(name, value) class MarvinModelSettings(MarvinSettings): model: str @property def encoder(self): import tiktoken return tiktoken.encoding_for_model(self.model).encode class ChatCompletionSettings(MarvinModelSettings): model: str = Field( default="gpt-3.5-turbo-1106", description="The default chat model to use.", ) async def acreate(self, **kwargs: Any) -> "ChatCompletion": from marvin.settings import settings return await settings.openai.async_client.chat.completions.create( model=self.model, **kwargs ) def create(self, **kwargs: Any) -> "ChatCompletion": from marvin.settings import settings print(kwargs) return settings.openai.client.chat.completions.create( model=self.model, temperature=0, **kwargs, ) class ImageSettings(MarvinModelSettings): model: str = Field( default="dall-e-3", description="The default image model to use.", ) size: Literal["1024x1024", "1792x1024", "1024x1792"] = Field( default="1024x1024", ) response_format: Literal["url", "b64_json"] = Field(default="url") style: Literal["vivid", "natural"] = Field(default="vivid") async def agenerate(self, prompt: str, **kwargs: Any) -> "ImagesResponse": from marvin.settings import settings return await settings.openai.async_client.images.generate( model=self.model, prompt=prompt, size=self.size, response_format=self.response_format, style=self.style, **kwargs, ) def generate(self, prompt: str, **kwargs: Any) -> "ImagesResponse": from marvin.settings import settings return settings.openai.client.images.generate( model=self.model, prompt=prompt, size=self.size, response_format=self.response_format, style=self.style, **kwargs, ) class SpeechSettings(MarvinModelSettings): model: str = Field( default="tts-1-hd", description="The default image model to use.", ) voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"] = Field( default="alloy", ) response_format: Literal["mp3", "opus", "aac", "flac"] = Field(default="mp3") speed: float = Field(default=1.0) async def acreate(self, input: str, **kwargs: Any) -> "HttpxBinaryResponseContent": from marvin.settings import settings return await settings.openai.async_client.audio.speech.create( model=kwargs.get("model", self.model), input=input, voice=kwargs.get("voice", self.voice), response_format=kwargs.get("response_format", self.response_format), speed=kwargs.get("speed", self.speed), ) def create(self, input: str, **kwargs: Any) -> "HttpxBinaryResponseContent": from marvin.settings import settings return settings.openai.client.audio.speech.create( model=kwargs.get("model", self.model), input=input, voice=kwargs.get("voice", self.voice), response_format=kwargs.get("response_format", self.response_format), speed=kwargs.get("speed", self.speed), ) class AssistantSettings(MarvinModelSettings): model: str = Field( default="gpt-4-1106-preview", description="The default assistant model to use.", ) class ChatSettings(MarvinSettings): completions: ChatCompletionSettings = Field(default_factory=ChatCompletionSettings) class AudioSettings(MarvinSettings): speech: SpeechSettings = Field(default_factory=SpeechSettings) class OpenAISettings(MarvinSettings): model_config = SettingsConfigDict(env_prefix="marvin_openai_") api_key: Optional[SecretStr] = Field( default=None, description="Your OpenAI API key.", ) organization: Optional[str] = Field( default=None, description="Your OpenAI organization ID.", ) chat: ChatSettings = Field(default_factory=ChatSettings) images: ImageSettings = Field(default_factory=ImageSettings) audio: AudioSettings = Field(default_factory=AudioSettings) assistants: AssistantSettings = Field(default_factory=AssistantSettings) @property def async_client( self, api_key: Optional[str] = None, **kwargs: Any ) -> "AsyncClient": from openai import AsyncClient if not (api_key or self.api_key): raise ValueError("No API key provided.") elif not api_key and self.api_key: api_key = self.api_key.get_secret_value() return AsyncClient( api_key=api_key, organization=self.organization, **kwargs, ) @property def client(self, api_key: Optional[str] = None, **kwargs: Any) -> "Client": from openai import Client if not (api_key or self.api_key): raise ValueError("No API key provided.") elif not api_key and self.api_key: api_key = self.api_key.get_secret_value() return Client( api_key=api_key, organization=self.organization, **kwargs, **kwargs ) class AzureAISettings(MarvinSettings): model_config = SettingsConfigDict(env_prefix="marvin_azure_openai_") api_key: Optional[SecretStr] = Field( default=None, description="Your Azure OpenAI API key.", ) api_version: Optional[str] = Field( default=None, description="Your OpenAI API version.", ) api_base: Optional[str] = Field( default=None, description="Your Azure endpoint.", ) azure_deployment: Optional[str] = Field( default=None, description="Your Azure resource deployment name.", ) organization: Optional[str] = Field( default=None, description="Your OpenAI organization ID.", ) chat: ChatSettings = Field(default_factory=ChatSettings) images: ImageSettings = Field(default_factory=ImageSettings) audio: AudioSettings = Field(default_factory=AudioSettings) assistants: AssistantSettings = Field(default_factory=AssistantSettings) @property def async_client( self, api_key: Optional[str] = None, **kwargs: Any ) -> "AsyncClient": from openai import AsyncClient if not (api_key or self.api_key): raise ValueError("No API key provided.") elif not api_key and self.api_key: api_key = self.api_key.get_secret_value() return AsyncClient( api_key=api_key, organization=self.organization, **kwargs, ) @property def client(self, api_key: Optional[str] = None, **kwargs: Any) -> "Client": from openai import AzureOpenAI # Ensure API key is available if not self.api_key: raise ValueError("No API key provided.") # Use get_secret_value to retrieve the API key api_key_value = self.api_key.get_secret_value() # Create and return the AzureOpenAI client object print("Creating AzureOpenAI client object...") print(**kwargs) return AzureOpenAI( api_key=api_key_value, api_version=self.api_version, azure_endpoint=self.api_base, azure_deployment=self.azure_deployment, **kwargs, ) class Settings(MarvinSettings): model_config = SettingsConfigDict(env_prefix="marvin_") if os.environ.get("MARVIN_LLM_PROVIDER", "openai") == "azure_openai": openai: AzureAISettings = Field(default_factory=AzureAISettings) else: # Default to OpenAISettings openai: OpenAISettings = Field(default_factory=OpenAISettings) log_level: str = Field(default="DEBUG", description="The log level to use.") settings = Settings() @contextmanager def temporary_settings(**kwargs: Any): """ Temporarily override Marvin setting values. This will _not_ mutate values that have been already been accessed at module load time. This function should only be used for testing. """ old_env = os.environ.copy() old_settings = settings.model_copy() try: for setting in kwargs: value = kwargs.get(setting) if value is not None: os.environ[setting] = str(value) else: os.environ.pop(setting, None) new_settings = Settings() for field in settings.model_fields: object.__setattr__(settings, field, getattr(new_settings, field)) yield settings finally: for setting in kwargs: value = old_env.get(setting) if value is not None: os.environ[setting] = value else: os.environ.pop(setting, None) for field in settings.model_fields: object.__setattr__(settings, field, getattr(old_settings, field))
[]
2024-01-10
grand-cat-unknown/marvin
src~marvin~beta~assistants~threads.py
import asyncio from typing import TYPE_CHECKING, Callable, Optional from openai.types.beta.threads import ThreadMessage from pydantic import BaseModel, Field from marvin.beta.assistants.formatting import pprint_message from marvin.utilities.asyncio import ( ExposeSyncMethodsMixin, expose_sync_method, ) from marvin.utilities.logging import get_logger from marvin.utilities.openai import get_client from marvin.utilities.pydantic import parse_as logger = get_logger("Threads") if TYPE_CHECKING: from .assistants import Assistant from .runs import Run class Thread(BaseModel, ExposeSyncMethodsMixin): id: Optional[str] = None metadata: dict = {} messages: list[ThreadMessage] = Field([], repr=False) def __enter__(self): self.create() return self def __exit__(self, exc_type, exc_val, exc_tb): self.delete() # If an exception has occurred, you might want to handle it or pass it through # Returning False here will re-raise any exception that occurred in the context return False @expose_sync_method("create") async def create_async(self, messages: list[str] = None): """ Creates a thread. """ if self.id is not None: raise ValueError("Thread has already been created.") if messages is not None: messages = [{"role": "user", "content": message} for message in messages] client = get_client() response = await client.beta.threads.create(messages=messages) self.id = response.id return self @expose_sync_method("add") async def add_async( self, message: str, file_paths: Optional[list[str]] = None ) -> ThreadMessage: """ Add a user message to the thread. """ client = get_client() if self.id is None: await self.create_async() # Upload files and collect their IDs file_ids = [] for file_path in file_paths or []: with open(file_path, mode="rb") as file: response = await client.files.create(file=file, purpose="assistants") file_ids.append(response.id) # Create the message with the attached files response = await client.beta.threads.messages.create( thread_id=self.id, role="user", content=message, file_ids=file_ids ) return ThreadMessage.model_validate(response.model_dump()) @expose_sync_method("get_messages") async def get_messages_async( self, limit: int = None, before_message: Optional[str] = None, after_message: Optional[str] = None, ): if self.id is None: await self.create_async() client = get_client() response = await client.beta.threads.messages.list( thread_id=self.id, # note that because messages are returned in descending order, # we reverse "before" and "after" to the API before=after_message, after=before_message, limit=limit, order="desc", ) return parse_as(list[ThreadMessage], reversed(response.model_dump()["data"])) @expose_sync_method("delete") async def delete_async(self): client = get_client() await client.beta.threads.delete(thread_id=self.id) self.id = None @expose_sync_method("run") async def run_async( self, assistant: "Assistant", **run_kwargs, ) -> "Run": """ Creates and returns a `Run` of this thread with the provided assistant. """ if self.id is None: await self.create_async() from marvin.beta.assistants.runs import Run run = Run(assistant=assistant, thread=self, **run_kwargs) return await run.run_async() class ThreadMonitor(BaseModel, ExposeSyncMethodsMixin): thread_id: str _thread: Thread last_message_id: Optional[str] = None on_new_message: Callable = Field(default=pprint_message) @property def thread(self): return self._thread def __init__(self, **kwargs): super().__init__(**kwargs) self._thread = Thread(id=kwargs["thread_id"]) @expose_sync_method("run_once") async def run_once_async(self): messages = await self.get_latest_messages() for msg in messages: if self.on_new_message: self.on_new_message(msg) @expose_sync_method("run") async def run_async(self, interval_seconds: int = None): if interval_seconds is None: interval_seconds = 1 if interval_seconds < 1: raise ValueError("Interval must be at least 1 second.") while True: try: await self.run_once_async() except KeyboardInterrupt: logger.debug("Keyboard interrupt received; exiting thread monitor.") break except Exception as exc: logger.error(f"Error refreshing thread: {exc}") await asyncio.sleep(interval_seconds) async def get_latest_messages(self) -> list[ThreadMessage]: limit = 20 # Loop to get all new messages in batches of 20 while True: messages = await self.thread.get_messages_async( after_message=self.last_message_id, limit=limit ) # often the API will retrieve messages that have been created but # not populated with text. We filter out these empty messages. filtered_messages = [] for i, msg in enumerate(messages): skip_message = False for c in msg.content: if getattr(getattr(c, "text", None), "value", None) == "": skip_message = True if not skip_message: filtered_messages.append(msg) if filtered_messages: self.last_message_id = filtered_messages[-1].id if len(messages) < limit: break return filtered_messages # async def refresh_messages_async(self) -> list[ThreadMessage]: # """ # Asynchronously refreshes and updates the message list. # This function fetches the latest messages up to a specified limit and # checks if the latest message in the current message list # (`self.messages`) is included in the new batch. If the latest message is # missing, it continues to fetch additional messages in batches, up to a # maximum count, using pagination. The function then updates # `self.messages` with these new messages, ensuring any existing messages # are updated with their latest versions and new messages are appended in # their original order. # """ # new_messages = [] # # fetch up to 100 messages # max_fetched = 100 # limit = 50 # max_attempts = max_fetched / limit + 2 # # Fetch the latest messages # messages = await self.get_messages_async(limit=limit) # if not messages: # return # # Check if the latest message in self.messages is in the new messages # latest_message_id = self.messages[-1].id if self.messages else None # missing_latest = ( # latest_message_id not in {m.id for m in messages} # if latest_message_id # else True # ) # # If the latest message is missing, fetch additional messages # total_fetched = len(messages) # attempts = 0 # while ( # messages # and missing_latest # and total_fetched < max_fetched # and attempts < max_attempts # ): # attempts += 1 # paginated_messages = await self.get_messages_async( # limit=limit, before_message=messages[0].id # ) # total_fetched += len(paginated_messages) # # prepend messages # messages = paginated_messages + messages # if any(m.id == latest_message_id for m in paginated_messages): # missing_latest = False # # Update self.messages with the latest data # new_messages_dict = {m.id: m for m in messages} # for i in range(len(self.messages) - 1, -1, -1): # if self.messages[i].id in new_messages_dict: # self.messages[i] = new_messages_dict.pop(self.messages[i].id) # else: # break # # Append remaining new messages at the end in their original order # self.messages.extend(new_messages_dict.values()) # return messages
[]
2024-01-10
grand-cat-unknown/marvin
src~marvin~components~ai_classifier.py
import inspect from enum import Enum from functools import partial, wraps from types import GenericAlias from typing import ( TYPE_CHECKING, Any, Awaitable, Callable, Generic, Literal, Optional, TypeVar, Union, cast, get_args, get_origin, overload, ) from pydantic import BaseModel, Field, TypeAdapter from typing_extensions import ParamSpec, Self from marvin.components.prompt import PromptFunction from marvin.serializers import create_vocabulary_from_type from marvin.settings import settings from marvin.utilities.jinja import ( BaseEnvironment, ) if TYPE_CHECKING: from openai.types.chat import ChatCompletion T = TypeVar("T", bound=Union[GenericAlias, type, list[str]]) P = ParamSpec("P") class AIClassifier(BaseModel, Generic[P, T]): fn: Optional[Callable[P, T]] = None environment: Optional[BaseEnvironment] = None prompt: Optional[str] = Field( default=inspect.cleandoc( "You are an expert classifier that always choose correctly." " \n- {{_doc}}" " \n- You must classify `{{text}}` into one of the following classes:" "{% for option in _options %}" " Class {{ loop.index - 1}} (value: {{ option }})" "{% endfor %}" "\n\nASSISTANT: The correct class label is Class" ) ) enumerate: bool = True encoder: Callable[[str], list[int]] = Field(default=None) max_tokens: Optional[int] = 1 render_kwargs: dict[str, Any] = Field(default_factory=dict) create: Optional[Callable[..., "ChatCompletion"]] = Field(default=None) def __call__(self, *args: P.args, **kwargs: P.kwargs) -> list[T]: create = self.create if self.fn is None: raise NotImplementedError if create is None: from marvin.settings import settings create = settings.openai.chat.completions.create return self.parse(create(**self.as_prompt(*args, **kwargs).serialize())) def parse(self, response: "ChatCompletion") -> list[T]: if not response.choices[0].message.content: raise ValueError( f"Expected a response, got {response.choices[0].message.content}" ) _response: list[int] = [ int(index) for index in list(response.choices[0].message.content) ] _return: T = cast(T, self.fn.__annotations__.get("return")) _vocabulary: list[str] = create_vocabulary_from_type(_return) if isinstance(_return, list) and next(iter(get_args(list[str])), None) == str: return cast(list[T], [_vocabulary[int(index)] for index in _response]) elif get_origin(_return) == Literal: return [ TypeAdapter(_return).validate_python(_vocabulary[int(index)]) for index in _response ] elif isinstance(_return, type) and issubclass(_return, Enum): return [list(_return)[int(index)] for index in _response] raise TypeError( f"Expected Literal or Enum or list[str], got {type(_return)} with value" f" {_return}" ) def as_prompt( self, *args: P.args, **kwargs: P.kwargs, ) -> PromptFunction[BaseModel]: return PromptFunction[BaseModel].as_grammar( fn=self.fn, environment=self.environment, prompt=self.prompt, enumerate=self.enumerate, encoder=self.encoder, max_tokens=self.max_tokens, **self.render_kwargs, )(*args, **kwargs) @overload @classmethod def as_decorator( cls: type[Self], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, acreate: Optional[Callable[..., Awaitable[Any]]] = None, **render_kwargs: Any, ) -> Callable[P, Self]: pass @overload @classmethod def as_decorator( cls: type[Self], fn: Callable[P, T], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, acreate: Optional[Callable[..., Awaitable[Any]]] = None, **render_kwargs: Any, ) -> Self: pass @classmethod def as_decorator( cls: type[Self], fn: Optional[Callable[P, T]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, acreate: Optional[Callable[..., Awaitable[Any]]] = None, **render_kwargs: Any, ) -> Union[Self, Callable[[Callable[P, T]], Self]]: if fn is None: return partial( cls, environment=environment, prompt=prompt, enumerate=enumerate, encoder=encoder, max_tokens=max_tokens, acreate=acreate, **({"prompt": prompt} if prompt else {}), **render_kwargs, ) return cls( fn=fn, environment=environment, enumerate=enumerate, encoder=encoder, max_tokens=max_tokens, **({"prompt": prompt} if prompt else {}), **render_kwargs, ) @overload def ai_classifier( *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, **render_kwargs: Any, ) -> Callable[[Callable[P, T]], Callable[P, T]]: pass @overload def ai_classifier( fn: Callable[P, T], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, **render_kwargs: Any, ) -> Callable[P, T]: pass def ai_classifier( fn: Optional[Callable[P, T]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, enumerate: bool = True, encoder: Callable[[str], list[int]] = settings.openai.chat.completions.encoder, max_tokens: Optional[int] = 1, **render_kwargs: Any, ) -> Union[Callable[[Callable[P, T]], Callable[P, T]], Callable[P, T]]: def wrapper(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: return AIClassifier[P, T].as_decorator( func, environment=environment, prompt=prompt, enumerate=enumerate, encoder=encoder, max_tokens=max_tokens, **render_kwargs, )(*args, **kwargs)[0] if fn is not None: return wraps(fn)(partial(wrapper, fn)) def decorator(fn: Callable[P, T]) -> Callable[P, T]: return wraps(fn)(partial(wrapper, fn)) return decorator
[ " \n- {{_doc}}", "{% for option in _options %}", "{% endfor %}", "You are an expert classifier that always choose correctly.", " Class {{ loop.index - 1}} (value: {{ option }})", " \n- You must classify `{{text}}` into one of the following classes:", "\n\nASSISTANT: The correct class label is Class" ]
2024-01-10
grand-cat-unknown/marvin
src~marvin~beta~assistants~runs.py
import asyncio from typing import Any, Callable, Optional, Union from openai.types.beta.threads.run import Run as OpenAIRun from openai.types.beta.threads.runs import RunStep as OpenAIRunStep from pydantic import BaseModel, Field, PrivateAttr, field_validator import marvin.utilities.tools from marvin.requests import Tool from marvin.tools.assistants import AssistantTools, CancelRun from marvin.utilities.logging import get_logger from marvin.utilities.openai import get_client from src.marvin.utilities.asyncio import ExposeSyncMethodsMixin, expose_sync_method from .assistants import Assistant from .threads import Thread logger = get_logger("Runs") class Run(BaseModel, ExposeSyncMethodsMixin): thread: Thread assistant: Assistant instructions: Optional[str] = Field( None, description="Replacement instructions to use for the run." ) additional_instructions: Optional[str] = Field( None, description=( "Additional instructions to append to the assistant's instructions." ), ) tools: Optional[AssistantTools] = Field( None, description="Replacement tools to use for the run." ) additional_tools: Optional[AssistantTools] = Field( None, description="Additional tools to append to the assistant's tools. ", ) run: OpenAIRun = None data: Any = None @field_validator("tools", "additional_tools", mode="before") def format_tools(cls, tools: Union[None, list[Union[Tool, Callable]]]): if tools is not None: return [ ( tool if isinstance(tool, Tool) else marvin.utilities.tools.tool_from_function(tool) ) for tool in tools ] @expose_sync_method("refresh") async def refresh_async(self): client = get_client() self.run = await client.beta.threads.runs.retrieve( run_id=self.run.id, thread_id=self.thread.id ) @expose_sync_method("cancel") async def cancel_async(self): client = get_client() await client.beta.threads.runs.cancel( run_id=self.run.id, thread_id=self.thread.id ) async def _handle_step_requires_action(self): client = get_client() if self.run.status != "requires_action": return if self.run.required_action.type == "submit_tool_outputs": tool_outputs = [] tools = self.assistant.tools if self.tools is None else self.tools if self.additional_tools: tools = tools + self.additional_tools for tool_call in self.run.required_action.submit_tool_outputs.tool_calls: try: output = marvin.utilities.tools.call_function_tool( tools=tools, function_name=tool_call.function.name, function_arguments_json=tool_call.function.arguments, ) except CancelRun as exc: logger.debug(f"Ending run with data: {exc.data}") raise except Exception as exc: output = f"Error calling function {tool_call.function.name}: {exc}" logger.error(output) tool_outputs.append( dict(tool_call_id=tool_call.id, output=output or "") ) await client.beta.threads.runs.submit_tool_outputs( thread_id=self.thread.id, run_id=self.run.id, tool_outputs=tool_outputs ) async def run_async(self) -> "Run": client = get_client() create_kwargs = {} if self.instructions is not None: create_kwargs["instructions"] = self.instructions if self.additional_instructions is not None: create_kwargs["instructions"] = ( create_kwargs.get("instructions", self.assistant.instructions or "") + "\n\n" + self.additional_instructions ) if self.tools is not None: create_kwargs["tools"] = self.tools # Check if 'self.additional_tools' is not None if self.additional_tools is not None: # If 'create_kwargs' already has a 'tools' key, use its value; # otherwise, use 'self.assistant.tools' or an empty list if it's None existing_tools = create_kwargs.get("tools", self.assistant.tools or []) # Combine 'existing_tools' with 'self.additional_tools' # and update 'create_kwargs' under the key 'tools' create_kwargs["tools"] = existing_tools + self.additional_tools self.run = await client.beta.threads.runs.create( thread_id=self.thread.id, assistant_id=self.assistant.id, **create_kwargs ) try: while self.run.status in ("queued", "in_progress", "requires_action"): if self.run.status == "requires_action": await self._handle_step_requires_action() await asyncio.sleep(0.1) await self.refresh_async() except CancelRun as exc: logger.debug(f"`CancelRun` raised; ending run with data: {exc.data}") await client.beta.threads.runs.cancel( run_id=self.run.id, thread_id=self.thread.id ) self.data = exc.data await self.refresh_async() if self.run.status == "failed": logger.debug(f"Run failed. Last error was: {self.run.last_error}") return self class RunMonitor(BaseModel): run_id: str thread_id: str _run: Run = PrivateAttr() _thread: Thread = PrivateAttr() steps: list[OpenAIRunStep] = [] def __init__(self, **kwargs): super().__init__(**kwargs) self._thread = Thread(**kwargs["thread_id"]) self._run = Run(**kwargs["run_id"], thread=self.thread) @property def thread(self): return self._thread @property def run(self): return self._run async def refresh_run_steps_async(self): """ Asynchronously refreshes and updates the run steps list. This function fetches the latest run steps up to a specified limit and checks if the latest run step in the current run steps list (`self.steps`) is included in the new batch. If the latest run step is missing, it continues to fetch additional run steps in batches, up to a maximum count, using pagination. The function then updates `self.steps` with these new run steps, ensuring any existing run steps are updated with their latest versions and new run steps are appended in their original order. """ # fetch up to 100 run steps max_fetched = 100 limit = 50 max_attempts = max_fetched / limit + 2 # Fetch the latest run steps client = get_client() response = await client.beta.threads.runs.steps.list( run_id=self.run.id, thread_id=self.thread.id, limit=limit, ) run_steps = list(reversed(response.data)) if not run_steps: return # Check if the latest run step in self.steps is in the new run steps latest_step_id = self.steps[-1].id if self.steps else None missing_latest = ( latest_step_id not in {rs.id for rs in run_steps} if latest_step_id else True ) # If the latest run step is missing, fetch additional run steps total_fetched = len(run_steps) attempts = 0 while ( run_steps and missing_latest and total_fetched < max_fetched and attempts < max_attempts ): attempts += 1 response = await client.beta.threads.runs.steps.list( run_id=self.run.id, thread_id=self.thread.id, limit=limit, # because this is a raw API call, "after" refers to pagination # in descnding chronological order after=run_steps[0].id, ) paginated_steps = list(reversed(response.data)) total_fetched += len(paginated_steps) # prepend run steps run_steps = paginated_steps + run_steps if any(rs.id == latest_step_id for rs in paginated_steps): missing_latest = False # Update self.steps with the latest data new_steps_dict = {rs.id: rs for rs in run_steps} for i in range(len(self.steps) - 1, -1, -1): if self.steps[i].id in new_steps_dict: self.steps[i] = new_steps_dict.pop(self.steps[i].id) else: break # Append remaining new run steps at the end in their original order self.steps.extend(new_steps_dict.values())
[]
2024-01-10
grand-cat-unknown/marvin
src~marvin~components~ai_image.py
from functools import partial, wraps from typing import ( TYPE_CHECKING, Any, Callable, Generic, Optional, TypeVar, Union, overload, ) from pydantic import BaseModel, Field from typing_extensions import ParamSpec, Self from marvin.components.prompt import PromptFunction from marvin.utilities.jinja import ( BaseEnvironment, ) if TYPE_CHECKING: from openai.types.images_response import ImagesResponse T = TypeVar("T") P = ParamSpec("P") class AIImage(BaseModel, Generic[P]): fn: Optional[Callable[P, Any]] = None environment: Optional[BaseEnvironment] = None prompt: Optional[str] = Field(default=None) render_kwargs: dict[str, Any] = Field(default_factory=dict) generate: Optional[Callable[..., "ImagesResponse"]] = Field(default=None) def __call__(self, *args: P.args, **kwargs: P.kwargs) -> "ImagesResponse": generate = self.generate if self.fn is None: raise NotImplementedError if generate is None: from marvin.settings import settings generate = settings.openai.images.generate _response = generate(prompt=self.as_prompt(*args, **kwargs)) return _response def as_prompt( self, *args: P.args, **kwargs: P.kwargs, ) -> str: return ( PromptFunction[BaseModel] .as_function_call( fn=self.fn, environment=self.environment, prompt=self.prompt, **self.render_kwargs, )(*args, **kwargs) .messages[0] .content ) @overload @classmethod def as_decorator( cls: type[Self], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, **render_kwargs: Any, ) -> Callable[P, Self]: pass @overload @classmethod def as_decorator( cls: type[Self], fn: Callable[P, Any], *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, **render_kwargs: Any, ) -> Self: pass @classmethod def as_decorator( cls: type[Self], fn: Optional[Callable[P, Any]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, **render_kwargs: Any, ) -> Union[Self, Callable[[Callable[P, Any]], Self]]: if fn is None: return partial( cls, environment=environment, **({"prompt": prompt} if prompt else {}), **render_kwargs, ) return cls( fn=fn, environment=environment, **({"prompt": prompt} if prompt else {}), **render_kwargs, ) def ai_image( fn: Optional[Callable[P, Any]] = None, *, environment: Optional[BaseEnvironment] = None, prompt: Optional[str] = None, **render_kwargs: Any, ) -> Union[ Callable[[Callable[P, Any]], Callable[P, "ImagesResponse"]], Callable[P, "ImagesResponse"], ]: def wrapper( func: Callable[P, Any], *args: P.args, **kwargs: P.kwargs ) -> "ImagesResponse": return AIImage[P].as_decorator( func, environment=environment, prompt=prompt, **render_kwargs, )(*args, **kwargs) if fn is not None: return wraps(fn)(partial(wrapper, fn)) def decorator(fn: Callable[P, Any]) -> Callable[P, "ImagesResponse"]: return wraps(fn)(partial(wrapper, fn)) return decorator def create_image( prompt: str, environment: Optional[BaseEnvironment] = None, generate: Optional[Callable[..., "ImagesResponse"]] = None, **model_kwargs: Any, ) -> "ImagesResponse": if generate is None: from marvin.settings import settings generate = settings.openai.images.generate return generate(prompt=prompt, **model_kwargs)
[]
2024-01-10
aws-samples/dialogue-idp
dgidp~sagemakerLLM.py
import sagemaker import ai21 from langchain.llms.base import LLM from typing import Optional, List, Mapping, Any, Dict # need to deploy jurassic jumbo instruct model to sagemaker endpoint first class SageMakerLLM(LLM): @property def _llm_type(self) -> str: return "jurassic-jumbo-instruct" def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str: response = ai21.Completion.execute( sm_endpoint="j2-jumbo-instruct", prompt=prompt, maxTokens=500, temperature=0, numResults=1, stopSequences=stop, ) return response['completions'][0]['data']['text']
[]
2024-01-10
aws-samples/dialogue-idp
dgidp~babyAGI_gradio.py
#!/usr/bin/env python # coding: utf-8 # # BabyAGI with Tools # # This notebook builds on top of [baby agi](baby_agi.ipynb), but shows how you can swap out the execution chain. The previous execution chain was just an LLM which made stuff up. By swapping it out with an agent that has access to tools, we can hopefully get re #al reliable information # ## Install and Import Required Modules import os from collections import deque from typing import Dict, List, Optional, Any from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import OpenAIEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field from langchain.chains.base import Chain import faiss import openai # Connect to the Vector Store # Depending on what vectorstore you use, this step may look different. #get_ipython().run_line_magic('pip', 'install faiss-gpu > /dev/null') #get_ipython().run_line_magic('pip', 'install google-search-results > /dev/null') from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore # Define your embedding model openai.api_key = os.environ.get('openai_api_token') embeddings_model = OpenAIEmbeddings(openai_api_key=openai.api_key, model='text-embedding-ada-002') # Initialize the vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) # ## Define the Chains # # BabyAGI relies on three LLM chains: # - Task creation chain to select new tasks to add to the list # - Task prioritization chain to re-prioritize tasks # - Execution Chain to execute the tasks # class TaskCreationChain(LLMChain): """Chain to generates tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_creation_template = ( "You are an task creation AI that uses the result of an execution agent" " to create new tasks with the following objective: {objective}," " The last completed task has the result: {result}." " This result was based on this task description: {task_description}." " These are incomplete tasks: {incomplete_tasks}." " Based on the result, create new tasks to be completed" " by the AI system that do not overlap with incomplete tasks." " Return the tasks as an array." ) prompt = PromptTemplate( template=task_creation_template, input_variables=[ "result", "task_description", "incomplete_tasks", "objective", ], ) return cls(prompt=prompt, llm=llm, verbose=verbose) class TaskPrioritizationChain(LLMChain): """Chain to prioritize tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_prioritization_template = ( "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing" " the following tasks: {task_names}." " Consider the ultimate objective of your team: {objective}." " Do not remove any tasks. Return the result as a numbered list, like:" " #. First task" " #. Second task" " Start the task list with number {next_task_id}." ) prompt = PromptTemplate( template=task_prioritization_template, input_variables=["task_names", "next_task_id", "objective"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain import OpenAI, SerpAPIWrapper, LLMChain todo_prompt = PromptTemplate.from_template( "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}" ) todo_chain = LLMChain(llm=OpenAI(temperature=0, openai_api_key=openai.api_key), prompt=todo_prompt) # search = SerpAPIWrapper(serpapi_api_key=os.environ.get('serp_api_token')) search = SerpAPIWrapper(serpapi_api_key='11f15a428a0386d7705e5a0f6ada5f5e74ee03b7cb51d2f2bfb3450e62db4273') tools = [ Tool( name="Search", func=search.run, description="useful for when you need to answer questions about current events", ), Tool( name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!", ), ] prefix = """You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.""" suffix = """Question: {task} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["objective", "task", "context", "agent_scratchpad"], ) # ### Define the BabyAGI Controller # BabyAGI composes the chains defined above in a (potentially-)infinite loop. def get_next_task( task_creation_chain: LLMChain, result: Dict, task_description: str, task_list: List[str], objective: str, ) -> List[Dict]: """Get the next task.""" incomplete_tasks = ", ".join(task_list) response = task_creation_chain.run( result=result, task_description=task_description, incomplete_tasks=incomplete_tasks, objective=objective, ) new_tasks = response.split("\n") return [{"task_name": task_name} for task_name in new_tasks if task_name.strip()] def prioritize_tasks( task_prioritization_chain: LLMChain, this_task_id: int, task_list: List[Dict], objective: str, ) -> List[Dict]: """Prioritize tasks.""" task_names = [t["task_name"] for t in task_list] next_task_id = int(this_task_id) + 1 response = task_prioritization_chain.run( task_names=task_names, next_task_id=next_task_id, objective=objective ) new_tasks = response.split("\n") prioritized_task_list = [] for task_string in new_tasks: if not task_string.strip(): continue task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = task_parts[0].strip() task_name = task_parts[1].strip() prioritized_task_list.append({"task_id": task_id, "task_name": task_name}) return prioritized_task_list def _get_top_tasks(vectorstore, query: str, k: int) -> List[str]: """Get the top k tasks based on the query.""" results = vectorstore.similarity_search_with_score(query, k=k) if not results: return [] sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True)) return [str(item.metadata["task"]) for item in sorted_results] def execute_task( vectorstore, execution_chain: LLMChain, objective: str, task: str, k: int = 5 ) -> str: """Execute a task.""" context = _get_top_tasks(vectorstore, query=objective, k=k) return execution_chain.run(objective=objective, context=context, task=task) class BabyAGI(Chain, BaseModel): """Controller model for the BabyAGI agent.""" task_list: deque = Field(default_factory=deque) task_creation_chain: TaskCreationChain = Field(...) task_prioritization_chain: TaskPrioritizationChain = Field(...) execution_chain: AgentExecutor = Field(...) task_id_counter: int = Field(1) vectorstore: VectorStore = Field(init=False) max_iterations: Optional[int] = None class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True def add_task(self, task: Dict): self.task_list.append(task) def print_task_list(self): print("\033[95m\033[1m" + "\n*****TASK LIST*****\n" + "\033[0m\033[0m") for t in self.task_list: print(str(t["task_id"]) + ": " + t["task_name"]) def print_next_task(self, task: Dict): print("\033[92m\033[1m" + "\n*****NEXT TASK*****\n" + "\033[0m\033[0m") print(str(task["task_id"]) + ": " + task["task_name"]) def print_task_result(self, result: str): print("\033[93m\033[1m" + "\n*****TASK RESULT*****\n" + "\033[0m\033[0m") print(result) @property def input_keys(self) -> List[str]: return ["objective"] @property def output_keys(self) -> List[str]: return [] def _call(self, inputs: Dict[str, Any]) -> Dict[str, Any]: """Run the agent.""" objective = inputs["objective"] first_task = inputs.get("first_task", "Make a todo list") self.add_task({"task_id": 1, "task_name": first_task}) num_iters = 0 my_dict = {} while True: if self.task_list: self.print_task_list() # Step 1: Pull the first task task = self.task_list.popleft() self.print_next_task(task) # Step 2: Execute the task result = execute_task( self.vectorstore, self.execution_chain, objective, task["task_name"] ) this_task_id = int(task["task_id"]) self.print_task_result(result) my_dict.update({"output": result}) # Step 3: Store the result in Pinecone result_id = f"result_{task['task_id']}" self.vectorstore.add_texts( texts=[result], metadatas=[{"task": task["task_name"]}], ids=[result_id], ) # Step 4: Create new tasks and reprioritize task list new_tasks = get_next_task( self.task_creation_chain, result, task["task_name"], [t["task_name"] for t in self.task_list], objective, ) for new_task in new_tasks: self.task_id_counter += 1 new_task.update({"task_id": self.task_id_counter}) self.add_task(new_task) self.task_list = deque( prioritize_tasks( self.task_prioritization_chain, this_task_id, list(self.task_list), objective, ) ) num_iters += 1 if self.max_iterations is not None and num_iters == self.max_iterations: print( "\033[91m\033[1m" + "\n*****TASK ENDING*****\n" + "\033[0m\033[0m" ) break #return my_dict ## Need fix to return result as a dict but this does not seem to work for some reason???? return{} @classmethod def from_llm( cls, llm: BaseLLM, vectorstore: VectorStore, verbose: bool = False, **kwargs ) -> "BabyAGI": """Initialize the BabyAGI Controller.""" task_creation_chain = TaskCreationChain.from_llm(llm, verbose=verbose) task_prioritization_chain = TaskPrioritizationChain.from_llm( llm, verbose=verbose ) llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names) agent_executor = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True ) return cls( task_creation_chain=task_creation_chain, task_prioritization_chain=task_prioritization_chain, execution_chain=agent_executor, vectorstore=vectorstore, **kwargs, )
[ "task_description", "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing the following tasks: {task_names}. Consider the ultimate objective of your team: {objective}. Do not remove any tasks. Return the result as a numbered list, like: #. First task #. Second task Start the task list with number {next_task_id}.", "You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task has the result: {result}. This result was based on this task description: {task_description}. These are incomplete tasks: {incomplete_tasks}. Based on the result, create new tasks to be completed by the AI system that do not overlap with incomplete tasks. Return the tasks as an array.", "agent_scratchpad", "task_names", "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}", "context", "next_task_id", "incomplete_tasks" ]
2024-01-10
aws-samples/dialogue-idp
dgidp~handler.py
import os import time import sagemaker import boto3 import openai from botocore.exceptions import ClientError from PyPDF2 import PdfReader from textractor import Textractor from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.document_loaders import TextLoader from langchain.docstore import InMemoryDocstore from langchain.agents import load_tools, initialize_agent from langchain.llms import OpenAI from langchain import VectorDBQA from langchain.chains import RetrievalQA from langchain import FAISS from langchain.chains.conversation.memory import ConversationBufferMemory from langchain.vectorstores import Chroma from chain import get_new_chain1 from langchain import PromptTemplate, HuggingFaceHub, LLMChain import faiss # BabyAGI from babyAGI import Optional, BabyAGI import sagemakerLLM from bloom import bloom_inference # API keys and tokens openai.api_key = os.environ.get('OPENAI_API_TOKEN') serp_api_token = os.environ.get('SERP_API_TOKEN') wolframalpha_api_token = os.environ.get('WOLFRAMALPHA_API_TOKEN') stabilityai_api_token = os.environ.get('STABILITYAI_API_TOKEN') # Set up Amazon textract textract_client = boto3.client('textract') output_file = '/tmp/textract_pdf_2_text.txt' # Create a SageMaker session sagemaker_session = sagemaker.Session() # Get the default S3 bucket name default_bucket_name = sagemaker_session.default_bucket() s3_client = boto3.client('s3') # Default embedding model and llm model embedding_model = OpenAIEmbeddings(openai_api_key=openai.api_key) llm_model = OpenAI(temperature=0, openai_api_key=openai.api_key) # Initialize default vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) faiss_store = FAISS(embedding_model.embed_query, index, InMemoryDocstore({}), {}) def parse_credentials(file_path): credentials = {} with open(file_path, 'r') as file: current_user = None for line in file: line = line.strip() if line.startswith('[') and line.endswith(']'): current_user = line[1:-1] credentials[current_user] = {} elif '=' in line and current_user is not None: key, value = line.split('=', 1) credentials[current_user][key] = value return credentials def get_key_from_credential_file(user, key_name, credential_file_path): credentials = parse_credentials(credential_file_path) if user in credentials: user_credentials = credentials[user] if key_name in user_credentials: return user_credentials[key_name] else: raise KeyError(f"'{key_name}' not found for user '{user}'.") else: raise KeyError(f"User '{user}' not found in the credential file.") # Function for openai def openai_create(prompt, model_choice): if len(prompt)>=4096: prompt=prompt[0:4095] response = openai.ChatCompletion.create( #model="gpt-3.5-turbo-0301", model=model_choice, messages=[ {"role": "system", "content": "You are a helpful assistant in answering question, completing sentences and rewrite documents."}, {"role": "user", "content": prompt} ], ) return response['choices'][0]['message']['content'] def transcribe(audio, state=""): #time.sleep(0.5) text = p(audio)["text"] #state += text + " " state = "HF Pipeline ASR done! " return text, state def convert_to_16_bit_wav(data): # Based on: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html #breakpoint() if data.dtype == np.float32: warnings.warn( "Audio data is not in 16-bit integer format." "Trying to convert to 16-bit int format." ) data = data / np.abs(data).max() data = data * 32767 data = data.astype(np.int16) elif data.dtype == np.int32: warnings.warn( "Audio data is not in 16-bit integer format." "Trying to convert to 16-bit int format." ) data = data / 65538 data = data.astype(np.int16) elif data.dtype == np.int16: pass elif data.dtype == np.uint8: warnings.warn( "Audio data is not in 16-bit integer format." "Trying to convert to 16-bit int format." ) data = data * 257 - 32768 data = data.astype(np.int16) else: raise ValueError("Audio data cannot be converted to " "16-bit int format.") return data def openai_asr(audio_file, state, model_choice): if not (audio_file is None): if os.path.isfile(audio_file): audio = open(audio_file, "rb") transcribe = openai.Audio.transcribe("whisper-1", audio) text = transcribe['text'] if model_choice == 'dgidp': out, state2 = langchain_idp(text, state, model_choice) else: out, state2 = chatgpt_clone(text, state, model_choice) return out, state2 else: return "Wrong audio format", state def clear_callback(interface): interface.inputs[0].reset() # Display an image def show_image(input_file): input_image = Image.open(input_file.name) return input_image # Lang Chain search def langchain_search(input_str, history): if input_str != "": langchain_llm = OpenAI(temperature=0, model='text-davinci-003', openai_api_key=openai.api_key) langchain_memory = ConversationBufferMemory(memory_key="chat_history") history = history or [] serapi_search = SerpAPIWrapper(serpapi_api_key=serp_api_token) wolfram_chain = LLMMathChain(llm=langchain_llm, verbose=True) tools = [ Tool( name = "Serapi", func=serapi_search.run, description="useful for when you need to answer questions about current events via SerpAPI" ), Tool( name="Calculator", func=wolfram_chain.run, description="useful for when you need to answer questions about math" ) ] agent = initialize_agent(tools, langchain_llm, agent="conversational-react-description", memory=langchain_memory, verbose=True) output = agent.run(input_str) history.append((input_str, output)) return history, history # Amazon textract extract text from pdf files def pdf_2_text(input_pdf_file, history): #output_file = '/tmp/textract_pdf_2_text.txt' history = history or [] key = 'input-pdf-files/{}'.format(os.path.basename(input_pdf_file.name)) try: response = s3_client.upload_file(input_pdf_file.name, default_bucket_name, key) #print("File uploaded to S3 successfully") except ClientError as e: print("Error uploading file to S3:", e) s3_object = {'Bucket': default_bucket_name, 'Name': key} response = textract_client.start_document_analysis( DocumentLocation={'S3Object': s3_object}, FeatureTypes=['TABLES', 'FORMS'] ) job_id = response['JobId'] #print("Started Textract job with ID:", job_id) while True: response = textract_client.get_document_analysis(JobId=job_id) status = response['JobStatus'] #print("Job status:", status) if status in ['SUCCEEDED', 'FAILED']: break time.sleep(5) if status == 'SUCCEEDED': with open(output_file, 'w') as output_file_io: for block in response['Blocks']: if block['BlockType'] in ['LINE', 'WORD']: output_file_io.write(block['Text'] + '\n') with open(output_file, "r") as file: first_512_chars = file.read(512).replace("\n", "").replace("\r", "").replace("[", "").replace("]", "") + " [...]" history.append(("Document conversion", first_512_chars)) #history = history.append(("", first_512_chars)) return history, history def get_faiss_store(): with open("docs.pkl", 'rb') as f: faiss_store = pickle.load(f) return faiss_store def langchain_idp(query_input, history, model_choice): separator = '\n' overlap_count = 100 chunk_size = 1000 history = history or [] #if len(texts) > 0 : loader = TextLoader(output_file) documents = loader.load() text_splitter = CharacterTextSplitter(separator=separator, chunk_overlap=overlap_count, chunk_size=chunk_size, length_function=len) texts = text_splitter.split_documents(documents) docsearch = Chroma.from_documents(texts, embedding_model) if model_choice=="j2-jumbo-instruct": llm = sagemakerLLM.SageMakerLLM() history.append((query_input, llm(query_input))) if model_choice=="flan-ul2": embeddings = OpenAIEmbeddings(openai_api_key=openai.api_key) vectorstore = get_faiss_store() flan_ul = HuggingFaceHub(repo_id="google/flan-ul2", model_kwargs={"temperature":0.1, "max_new_tokens":200}, huggingfacehub_api_token=hf_api_token) qa_chain = get_new_chain1(vectorstore, flan_ul, flan_ul, isFlan=True) response = qa_chain.run(query_input) elif model_choice=="flan-t5-xl": template = """Question: {question} Answer: Let's think step by step.""" prompt = PromptTemplate(template=template, input_variables=["question"]) qa_chain = LLMChain(prompt=prompt, llm=HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":256}, huggingfacehub_api_token=hf_api_token)) #response = qa_chain.run(query_input) history.append((query_input, qa_chain.run(query_input))) elif model_choice=="bloom": max_length = 128 sample_or_greedy = 'Greedy' history.append((query_input, bloom_inference(query_input, max_length, sample_or_greedy, seed=42))) # elif model_choice=="bedrock": # history.append((query_input, bedrock(query_input))) elif model_choice=="dgidp": llm = sagemakerLLM.SageMakerLLM() #llm = OpenAI(model_name='text-davinci-003', temperature=0, openai_api_key=openai.api_key) #llm = OpenAI(model_name='gpt-3.5-turbo', temperature=0, openai_api_key=openai.api_key) qa_chain = VectorDBQA.from_chain_type(llm=llm, chain_type='stuff', vectorstore=docsearch) #qa_chain = RetrievalQA.from_llm(llm=llm, vectorstore=vectordb) response = qa_chain({'query': query_input}, return_only_outputs=True) history.append((query_input, response['result'])) elif model_choice=="babyagi": # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 3 baby_agi = BabyAGI.from_llm( llm=llm_model, vectorstore=faiss_store, verbose=verbose, max_iterations=max_iterations ) baby_agi({"objective": query_input}) # Process results index = list(faiss_store.index_to_docstore_id)[-1] response = faiss_store.docstore.search(faiss_store.index_to_docstore_id[index]).page_content history.append((query_input, response)) elif model_choice=="gpt-3.5": model_name = 'gpt-3.5-turbo' history.append((query_input, openai_create(query_input, model_name))) else: history.append((query_input, "Not implemented")) #response = qa_chain({'query': query_input}, return_only_outputs=True) #history.append((query_input, response['result'])) return history, history
[ "You are a helpful assistant in answering question, completing sentences and rewrite documents.", "question", "Question: {question}\n Answer: Let's think step by step." ]
2024-01-10
aws-samples/dialogue-idp
dgidp~run_babyAGI_gradio.py
from babyAGI import Optional, BabyAGI from langchain import OpenAI from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings import faiss, os openai_api_key = os.environ.get('openai_api_token') # Define your embedding model embeddings_model = OpenAIEmbeddings(openai_api_key=openai_api_key) # Initialize the vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) OBJECTIVE = "What happened to the Signature Bank and the First Republic Bank, two recent regional bank crisis in late April 2023? Will the FED take the same action as it did on SVB's failure?" llm = OpenAI(temperature=0, openai_api_key=openai_api_key) # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 1 baby_agi = BabyAGI.from_llm( llm=llm, vectorstore=vectorstore, verbose=verbose, max_iterations=max_iterations ) response = baby_agi({"objective": OBJECTIVE}) print(response)
[]
2024-01-10
aws-samples/dialogue-idp
dgidp~babyagi_streamlit.py
from collections import deque from typing import Dict, List, Optional from langchain import LLMChain, OpenAI, PromptTemplate from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import BaseLLM from langchain.vectorstores import FAISS from langchain.vectorstores.base import VectorStore from pydantic import BaseModel, Field import streamlit as st class TaskCreationChain(LLMChain): @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_creation_template = ( "You are an task creation AI that uses the result of an execution agent" " to create new tasks with the following objective: {objective}," " The last completed task has the result: {result}." " This result was based on this task description: {task_description}." " These are incomplete tasks: {incomplete_tasks}." " Based on the result, create new tasks to be completed" " by the AI system that do not overlap with incomplete tasks." " Return the tasks as an array." ) prompt = PromptTemplate( template=task_creation_template, partial_variables={"objective": objective}, input_variables=["result", "task_description", "incomplete_tasks"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def get_next_task(self, result: Dict, task_description: str, task_list: List[str]) -> List[Dict]: """Get the next task.""" incomplete_tasks = ", ".join(task_list) response = self.run(result=result, task_description=task_description, incomplete_tasks=incomplete_tasks) new_tasks = response.split('\n') return [{"task_name": task_name} for task_name in new_tasks if task_name.strip()] class TaskPrioritizationChain(LLMChain): """Chain to prioritize tasks.""" @classmethod def from_llm(cls, llm: BaseLLM, objective: str, verbose: bool = True) -> LLMChain: """Get the response parser.""" task_prioritization_template = ( "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing" " the following tasks: {task_names}." " Consider the ultimate objective of your team: {objective}." " Do not remove any tasks. Return the result as a numbered list, like:" " #. First task" " #. Second task" " Start the task list with number {next_task_id}." ) prompt = PromptTemplate( template=task_prioritization_template, partial_variables={"objective": objective}, input_variables=["task_names", "next_task_id"], ) return cls(prompt=prompt, llm=llm, verbose=verbose) def prioritize_tasks(self, this_task_id: int, task_list: List[Dict]) -> List[Dict]: """Prioritize tasks.""" task_names = [t["task_name"] for t in task_list] next_task_id = int(this_task_id) + 1 response = self.run(task_names=task_names, next_task_id=next_task_id) new_tasks = response.split('\n') prioritized_task_list = [] for task_string in new_tasks: if not task_string.strip(): continue task_parts = task_string.strip().split(".", 1) if len(task_parts) == 2: task_id = task_parts[0].strip() task_name = task_parts[1].strip() prioritized_task_list.append({"task_id": task_id, "task_name": task_name}) return prioritized_task_list class ExecutionChain(LLMChain): """Chain to execute tasks.""" vectorstore: VectorStore = Field(init=False) @classmethod def from_llm(cls, llm: BaseLLM, vectorstore: VectorStore, verbose: bool = True) -> LLMChain: """Get the response parser.""" execution_template = ( "You are an AI who performs one task based on the following objective: {objective}." " Take into account these previously completed tasks: {context}." " Your task: {task}." " Response:" ) prompt = PromptTemplate( template=execution_template, input_variables=["objective", "context", "task"], ) return cls(prompt=prompt, llm=llm, verbose=verbose, vectorstore=vectorstore) def _get_top_tasks(self, query: str, k: int) -> List[str]: """Get the top k tasks based on the query.""" results = self.vectorstore.similarity_search_with_score(query, k=k) if not results: return [] sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True)) return [str(item.metadata['task']) for item in sorted_results] def execute_task(self, objective: str, task: str, k: int = 5) -> str: """Execute a task.""" context = self._get_top_tasks(query=objective, k=k) return self.run(objective=objective, context=context, task=task) class Message: exp: st.expander ai_icon = "./imgs/robot.png" def __init__(self, label: str): message_area, icon_area = st.columns([10, 1]) #icon_area.image(self.ai_icon, caption="BabyAGI") # Expander self.exp = message_area.expander(label=label, expanded=True) def __enter__(self): return self def __exit__(self, ex_type, ex_value, trace): pass def write(self, content): self.exp.markdown(content) class BabyAGI(BaseModel): """Controller model for the BabyAGI agent.""" objective: str = Field(alias="objective") task_list: deque = Field(default_factory=deque) task_creation_chain: TaskCreationChain = Field(...) task_prioritization_chain: TaskPrioritizationChain = Field(...) execution_chain: ExecutionChain = Field(...) task_id_counter: int = Field(1) def add_task(self, task: Dict): self.task_list.append(task) def print_task_list(self): with Message(label="Task List") as m: m.write("### Task List") for t in self.task_list: m.write("- " + str(t["task_id"]) + ": " + t["task_name"]) m.write("") def print_next_task(self, task: Dict): with Message(label="Next Task") as m: m.write("### Next Task") m.write("- " + str(task["task_id"]) + ": " + task["task_name"]) m.write("") def print_task_result(self, result: str): with Message(label="Task Result") as m: m.write("### Task Result") m.write(result) m.write("") def print_task_ending(self): with Message(label="Task Ending") as m: m.write("### Task Ending") m.write("") def run(self, max_iterations: Optional[int] = None): """Run the agent.""" num_iters = 0 while True: if self.task_list: self.print_task_list() # Step 1: Pull the first task task = self.task_list.popleft() self.print_next_task(task) # Step 2: Execute the task result = self.execution_chain.execute_task( self.objective, task["task_name"] ) this_task_id = int(task["task_id"]) self.print_task_result(result) # Step 3: Store the result in Pinecone result_id = f"result_{task['task_id']}" self.execution_chain.vectorstore.add_texts( texts=[result], metadatas=[{"task": task["task_name"]}], ids=[result_id], ) # Step 4: Create new tasks and reprioritize task list new_tasks = self.task_creation_chain.get_next_task( result, task["task_name"], [t["task_name"] for t in self.task_list] ) for new_task in new_tasks: self.task_id_counter += 1 new_task.update({"task_id": self.task_id_counter}) self.add_task(new_task) self.task_list = deque( self.task_prioritization_chain.prioritize_tasks( this_task_id, list(self.task_list) ) ) num_iters += 1 if max_iterations is not None and num_iters == max_iterations: self.print_task_ending() break @classmethod def from_llm_and_objectives( cls, llm: BaseLLM, vectorstore: VectorStore, objective: str, first_task: str, verbose: bool = False, ) -> "BabyAGI": """Initialize the BabyAGI Controller.""" task_creation_chain = TaskCreationChain.from_llm( llm, objective, verbose=verbose ) task_prioritization_chain = TaskPrioritizationChain.from_llm( llm, objective, verbose=verbose ) execution_chain = ExecutionChain.from_llm(llm, vectorstore, verbose=verbose) controller = cls( objective=objective, task_creation_chain=task_creation_chain, task_prioritization_chain=task_prioritization_chain, execution_chain=execution_chain, ) controller.add_task({"task_id": 1, "task_name": first_task}) return controller ''' def main(): st.set_page_config( initial_sidebar_state="expanded", page_title="BabyAGI Streamlit", layout="centered", ) with st.sidebar: openai_api_key = st.text_input('Your OpenAI API KEY', type="password") st.title("BabyAGI Streamlit") objective = st.text_input("Input Ultimate goal", "Solve world hunger") first_task = st.text_input("Input Where to start", "Develop a task list") max_iterations = st.number_input("Max iterations", value=3, min_value=1, step=1) button = st.button("Run") embedding_model = HuggingFaceEmbeddings() vectorstore = FAISS.from_texts(["_"], embedding_model, metadatas=[{"task":first_task}]) if button: try: baby_agi = BabyAGI.from_llm_and_objectives( llm=OpenAI(openai_api_key=openai_api_key), vectorstore=vectorstore, objective=objective, first_task=first_task, verbose=False ) baby_agi.run(max_iterations=max_iterations) except Exception as e: st.error(e) if __name__ == "__main__": main() '''
[ "You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}. Your task: {task}. Response:", "task_description", "You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing the following tasks: {task_names}. Consider the ultimate objective of your team: {objective}. Do not remove any tasks. Return the result as a numbered list, like: #. First task #. Second task Start the task list with number {next_task_id}.", "You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task has the result: {result}. This result was based on this task description: {task_description}. These are incomplete tasks: {incomplete_tasks}. Based on the result, create new tasks to be completed by the AI system that do not overlap with incomplete tasks. Return the tasks as an array.", "task_names", "context", "next_task_id", "incomplete_tasks" ]
2024-01-10
aws-samples/dialogue-idp
dgidp~run_babyagi_streamlit.py
from babyagi import Optional, BabyAGI from langchain import OpenAI from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings from langchain.embeddings import HuggingFaceEmbeddings import faiss, os openai_api_key = os.environ.get('openai_api_token') # Define your embedding model embeddings_model = OpenAIEmbeddings(openai_api_key=openai_api_key) # Initialize the vectorstore as empty embedding_size = 1536 index = faiss.IndexFlatL2(embedding_size) vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) OBJECTIVE = "What happened to the First Republic Bank, another regional crisis in late April 2023? Will the FED take the same action as it did on SVB's failure?" llm = OpenAI(temperature=0, openai_api_key=openai_api_key) first_task = "Develop a task list" # Logging of LLMChains verbose = False # If None, will keep on going forever max_iterations: Optional[int] = 1 baby_agi = BabyAGI.from_llm_and_objectives( llm=llm, vectorstore=vectorstore, objective=OBJECTIVE, first_task=first_task, verbose=verbose ) baby_agi.run(max_iterations=max_iterations)
[]
2024-01-10
aws-samples/dialogue-idp
dgidp~chat_babyagi.py
#from transformers import pipeline import time import os import openai import magic import requests import boto3 import json import pickle import gradio as gr from handler import openai_asr, langchain_idp, pdf_2_text # start_sequence = "\nAI:" # restart_sequence = "\nHuman: " # last_message = prompt prompt = "How can I help you today?" block = gr.Blocks() with block: gr.HTML( f""" <div class="main-div"> <div> <header> <h2>Dialogue Guided Intelligent Document Processing</h2> </header> <p>Dialogue Guided Intelligent Document Processing (DGIDP) is an innovative approach to extracting and processing information from documents by leveraging natural language understanding and conversational AI. This technique allows users to interact with the IDP system using human-like conversations, asking questions, and receiving relevant information in real-time. The system is designed to understand context, process unstructured data, and respond to user queries effectively and efficiently.</p> <p>While the text or voice chat accepts all major languages, the document upload feature only accepts files in English, German, French, Spanish, Italian, and Portuguese. The demo supports <u>multilingual text and voice</u> input, as well as <u>multi-page</u> documents in PDF, PNG, JPG, or TIFF format.</p> </div> <a href="https://www.buymeacoffee.com/alfredcs" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" height="32px" width="108px" alt="Buy Me A Coffee"></a> <br> </div> """ ) model_choice = gr.Dropdown(choices=["dgidp", "gpt-3.5", "babyagi", "bloom", "j2-jumbo-instruct", "flan-t5-xl", "bedrock (coming soon)", "gpt4all (coming soon)", "gpt-4 (coming soon)"], label="Model selection", value="gpt-3.5") gr.HTML(f"""<hr style="color:blue>""") #file1 = gr.File(file_count="single") #upload = gr.Button("OCR") gr.HTML(f"""<hr style="color:blue>""") chatbot = gr.Chatbot().style(height=1750) #message = gr.Textbox(placeholder=prompt, lines=1) #audio = gr.Audio(source="microphone", type="filepath", show_label=True,height=550) #file1 = gr.File(file_count="single") state = gr.State() with gr.Row().style(equal_height=True): with gr.Column(): message = gr.Textbox(placeholder=prompt, show_label=True) #textChat = gr.Button("Text Chat") with gr.Column(): audio = gr.Audio(source="microphone", type="filepath", show_label=True) #voiceChat = gr.Button("Voice Chat") with gr.Row().style(equal_height=True): with gr.Column(): textChat = gr.Button("Text Chat") with gr.Column(): voiceChat = gr.Button("Voice Chat") with gr.Row().style(equal_height=True): with gr.Column(): file1 = gr.File(file_count="single") with gr.Column(): file1_img = gr.Image(type="filepath", label="Upload an Image") upload = gr.Button("Transcribe") state = gr.State() textChat.click(langchain_idp, inputs=[message, state, model_choice], outputs=[chatbot, state]) voiceChat.click(openai_asr, inputs=[audio, state, model_choice], outputs=[chatbot, state]) upload.click(pdf_2_text, inputs=[file1, state], outputs=[chatbot, state]) #clear.click() block.launch(ssl_keyfile=os.environ.get('KEY_PATH'), ssl_certfile=os.environ.get('CERT_PATH'), ssl_verify=False, debug=True, server_name="0.0.0.0", server_port=7862, height=2048, share=False)
[ "How can I help you today?" ]
2024-01-10
aws-samples/dialogue-idp
dgvlp~app_radaide.py
import copy import glob import hashlib import logging import os import re from pathlib import Path from typing import List, Optional, Tuple from urllib.parse import urlparse import gradio as gr import PIL from gradio import processing_utils from gradio_client.client import DEFAULT_TEMP_DIR from text_generation import Client from transformers import AutoProcessor import boto3 import whisper import base64 # For dino_sam segementation import copy import cv2 import torch import matplotlib.pyplot as plt import dino_sam_inpainting as D # Multiclass classification import utils.multi_class as M import random #SDXL import io, base64 from PIL import Image from utils import bedrock from io import BytesIO from base64 import b64encode ## CoT from langchain import PromptTemplate, LLMChain from langchain.llms import HuggingFaceTextGenInference # Keyword extraction from keybert import KeyBERT kw_model = KeyBERT() # Dino SAM cfg config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py' # change the path of the model config file grounded_checkpoint = './models/groundingdino_swint_ogc.pth' # change the path of the model sam_checkpoint = './models/sam_vit_h_4b8939.pth' sam_hq_checkpoint = '' #sam_hq_vit_h.pth use_sam_hq = '' # image_path = image_path # text_prompt = text_prompt output_dir = './outputs' # box_threshold = box_threshold # text_threshold = text_threshold device = 'cuda' s3_client = boto3.client('s3') asr_model = whisper.load_model("large") MODELS = [ #"HuggingFaceM4/idefics-9b-instruct", #"HuggingFaceM4/idefics-80b-instruct", "local/idefics-9b-instruct", ] API_PATHS = { "local/idefics-9b-instruct": ( "http://<tgi_vlm_histname>:8080" ), } SYSTEM_PROMPT = [ """"The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called RadAide, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and RadAide will do its best to answer User’s questions. RadAide has the ability to perceive images and reason about the content of visual inputs. It can also process images by following precise instructs. RadAide was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts. The conversation begins:""", """\nUser:""", "https://miro.medium.com/v2/resize:fit:1332/0*yl2b-bDJeEwKPUI5" "Describe the nature of this image.<end_of_utterance>", """\RadAide: A tattooed person holding a sign that says, “Teach your children well,” in a crowd of people. In the middle of the sign, there’s an illustration of the earth with 2 raised fists on either side that have a rainbow pride square background, a trans pride circle background, and brown skin tone stripes on the fists. The raised fist is a symbol of solidarity and specifically Black power as popularized by the Black Panther Party in the 1960s. The rainbow pride flag has rainbow stripes and symbolizes general LGBTQ pride. The trans pride flag has pink, blue, and white stripes and celebrates pride for the trans and gender non-conforming umbrella.<end_of_utterance>""", "\nUser:How about this image?", "https://cdn.pixabay.com/photo/2017/09/25/13/12/puppy-2785074_1280.jpg", "Can you describe it too?<end_of_utterance>", """\RadAide: A dog is lying on the floor, looking at the camera. It is looking directly at you. The dog has a white body and brown patches on its face and ears. Its eyes are dark. Its nose is black, and it has long, floppy ears, white paws, long fur, big eyes.<end_of_utterance>""", "\nUser: What kind of breed is it?<end_of_utterance>", """\RadAide: It looks like a Cavalier King Charles Spaniel. These breeds are known for their gentle, affectionate nature. Their long ears and big, brown eyes give them a sweet, expressive face. Cavaliers are small dogs weighing between 13 and 20 pounds. They have a silky, medium-length coat that comes in various colors, including black and tan, tricolor, red and white, or black.<end_of_utterance>""", "\nUser: What can you tell me about this breed of dogs?<end_of_utterance>", """\RadAide: One specific characteristic of the Cavalier King Charles Spaniel is its friendly and affectionate nature. This breed is known for being extremely sociable and forming strong bonds with their owners. They are often described as "velcro dogs" because they love to be close to their human companions, whether it's sitting on their laps, cuddling on the couch, or simply following them around the house.<end_of_utterance>""", "\nUser: ghjkhjabnufs<end_of_utterance>", """\RadAide: That doesn’t seem to be a word. Could you ask me another way?<end_of_utterance>""", "\nUser: Do you like Cavalier King Charles Spaniel?<end_of_utterance>", """\RadAide: I do not have personal opinions as I’m just a computer program. However, cavaliers are known for being excellent family pets due to their gentle and patient demeanor, making them great with children and other pets. Their affectionate nature also makes them ideal therapy dogs, as they can provide comfort and emotional support to those in need.<end_of_utterance>""", "\nUser: How many dogs do you see in this image?", "https://i.dailymail.co.uk/i/pix/2011/07/01/article-2010308-0CD22A8300000578-496_634x414.jpg", "<end_of_utterance>", """\nAssistant: There is no dogs in this image. The picture shows a tennis player jumping to volley the ball.<end_of_utterance>""", ] BAN_TOKENS = ( # For documentation puporse. We are not using this list, it is hardcoded inside `idefics_causal_lm.py` inside TGI. "<image>;<fake_token_around_image>" ) EOS_STRINGS = ["<end_of_utterance>", "\nUser:"] STOP_SUSPECT_LIST = [] #GRADIO_LINK = "https://huggingfacem4-idefics-playground.hf.space" GRADIO_LINK = "http://0.0.0.0:7863" HTTPD_URL = "http://<httpd_uploadserver>:8080/" API_TOKEN = os.getenv("hf_api_token") IDEFICS_LOGO = "https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/IDEFICS_logo.png" DocAid_logo = "example_images/medicine.png" global orig_image_path PROCESSOR = AutoProcessor.from_pretrained( "HuggingFaceM4/idefics-9b-instruct", token=API_TOKEN, ) BOT_AVATAR = "IDEFICS_logo.png" BOT_AVATAR = None logging.basicConfig(level=logging.INFO) logger = logging.getLogger() # Monkey patch adapted from gradio.components.image.Image - mostly to make the `save` step optional in `pil_to_temp_file` def hash_bytes(bytes: bytes): sha1 = hashlib.sha1() sha1.update(bytes) return sha1.hexdigest() def pil_to_temp_file(img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png") -> str: """Save a PIL image into a temp file""" bytes_data = processing_utils.encode_pil_to_bytes(img, format) temp_dir = Path(dir) / hash_bytes(bytes_data) temp_dir.mkdir(exist_ok=True, parents=True) filename = str(temp_dir / f"image.{format}") if not os.path.exists(filename): img.save(filename, pnginfo=processing_utils.get_pil_metadata(img)) return filename def add_file(file): return file.name, gr.update(label='🖼️ Uploaded!') # Dino SAM def dino_sam(image_path, text_prompt, text_threshold=0.4, box_threshold=0.5, output_dir='/temp/gradio/outputs'): config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py' # change the path of the model config file grounded_checkpoint = './models/groundingdino_swint_ogc.pth' # change the path of the model sam_checkpoint = './models/sam_vit_h_4b8939.pth' sam_hq_checkpoint = '' #sam_hq_vit_h.pth use_sam_hq = '' output_dir = '/tmp/gradio/outputs' device = 'cuda' # make dir os.makedirs(output_dir, exist_ok=True) # load image image_pil, image = D.load_image(image_path) # load model model = D.load_model(config_file, grounded_checkpoint, device=device) rnum = random.randint(10, 100) output_file_name = f'{rnum}_{format(os.path.basename(image_path))}' # visualize raw image image_pil.save(os.path.join(output_dir, output_file_name)) # run grounding dino model boxes_filt, pred_phrases = D.get_grounding_output( model, image, text_prompt, box_threshold, text_threshold, device=device ) # initialize SAM if use_sam_hq: predictor = D.SamPredictor(D.build_sam_hq(checkpoint=sam_hq_checkpoint).to(device)) else: predictor = D.SamPredictor(D.build_sam(checkpoint=sam_checkpoint).to(device)) image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) predictor.set_image(image) size = image_pil.size H, W = size[1], size[0] for i in range(boxes_filt.size(0)): boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H]) boxes_filt[i][:2] -= boxes_filt[i][2:] / 2 boxes_filt[i][2:] += boxes_filt[i][:2] boxes_filt = boxes_filt.cpu() transformed_boxes = predictor.transform.apply_boxes_torch(boxes_filt, image.shape[:2]).to(device) masks, _, _ = predictor.predict_torch( point_coords = None, point_labels = None, boxes = transformed_boxes.to(device), multimask_output = False, ) # draw output image plt.figure(figsize=(10, 10)) plt.imshow(image) for mask in masks: D.show_mask(mask.cpu().numpy(), plt.gca(), random_color=True) for box, label in zip(boxes_filt, pred_phrases): D.show_box(box.numpy(), plt.gca(), label) #output_file_name = f'{format(os.path.basename(image_path))}' plt.axis('off') plt.savefig( os.path.join(output_dir, f'grounded_sam_{output_file_name}'), bbox_inches="tight", dpi=300, pad_inches=0.0 ) D.save_mask_data(output_dir, masks, boxes_filt, pred_phrases) return f'grounded_sam_{output_file_name}' ## SDXL def image_gen(prompt: str, image_path: str) -> str: if prompt is None: return boto3_bedrock = boto3.client(service_name='bedrock',region_name='us-east-1',endpoint_url='https://bedrock.us-east-1.amazonaws.com') negative_prompts = [ "poorly rendered", "poor background details", "poorly drawn dog", "disfigured dog features", "blurry" ] style_preset = "photographic" # (photographic, digital-art, cinematic, ...) modelId = 'stability.stable-diffusion-xl' model = bedrock.Bedrock(boto3_bedrock) rnum = random.randint(100, 2000) if image_path is None: base_64_img_str = model.generate_image(prompt, modelId=modelId, cfg_scale=5, seed=2143, steps=70, style_preset=style_preset) image_2 = Image.open(io.BytesIO(base64.decodebytes(bytes(base_64_img_str, "utf-8")))) image_2.save(f'/tmp/gradio/outputs/sdxl_{rnum}.jpg') else: buffer = BytesIO() image_1 = Image.open(image_path) # Resize to 512 basewidth = 512 hsize = 512 ''' width, height = image_1.size if width > 512: basewidth = 512 wpercent = (basewidth/float(image_1.size[0])) hsize = int((float(image_1.size[1])*float(wpercent))) ''' image_1 = image_1.resize((basewidth,hsize), Image.Resampling.LANCZOS) # Gen image to image image_1.save(buffer, format="JPEG") img_bytes = buffer.getvalue() init_image = b64encode(img_bytes).decode() base_64_img_str = model.generate_image(prompt, init_image=init_image, start_schedule=0.6, cfg_scale=5, seed=12345, steps=70, style_preset=style_preset) image_3 = Image.open(io.BytesIO(base64.decodebytes(bytes(base_64_img_str, "utf-8")))) image_3.save(f'/tmp/gradio/outputs/sdxl_{rnum}.jpg') return f'sdxl_{rnum}.jpg' # This is a hack to make pre-computing the default examples work. # During normal inference, we pass images as url to a local file using the method `gradio_link` # which allows the tgi server to fetch the local image from the frontend server. # however, we are building the space (and pre-computing is part of building the space), the frontend is not available # and won't answer. So tgi server will try to fetch an image that is not available yet, which will result in a timeout error # because tgi will never be able to return the generation. # To bypass that, we pass instead the images URLs from the spaces repo. all_images = glob.glob(f"{os.path.dirname(__file__)}/example_images/*") DEFAULT_IMAGES_TMP_PATH_TO_URL = {} for im_path in all_images: H = gr.Image(im_path, visible=False, type="filepath") tmp_filename = H.preprocess(H.value) #DEFAULT_IMAGES_TMP_PATH_TO_URL[tmp_filename] = f"https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/example_images/{os.path.basename(im_path)}" #DEFAULT_IMAGES_TMP_PATH_TO_URL[tmp_filename] = f"/https://bedrock-415275363822.s3.us-east-1.amazonaws.com/uploads/{os.path.basename(im_path)}" #print(f"The tem file path {DEFAULT_IMAGES_TMP_PATH_TO_URL[tmp_filename]}") # Utils to handle the image markdown display logic def split_str_on_im_markdown(string: str) -> List[str]: """ Extract from a string (typically the user prompt string) the potential images from markdown Examples: - `User:![](https://favurl.com/chicken_on_money.png)Describe this image.` would become `["User:", "https://favurl.com/chicken_on_money.png", "Describe this image."]` - `User:![](/file=/my_temp/chicken_on_money.png)Describe this image.` would become `["User:", "/my_temp/chicken_on_money.png", "Describe this image."]` """ IMAGES_PATTERN = re.compile(r"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)") parts = [] cursor = 0 for pattern in IMAGES_PATTERN.finditer(string): start = pattern.start() if start != cursor: parts.append(string[cursor:start]) image_url = pattern.group(1) if image_url.startswith("/file="): image_url = image_url[6:] # Remove the 'file=' prefix parts.append(image_url) cursor = pattern.end() if cursor != len(string): parts.append(string[cursor:]) return parts def is_image(string: str) -> bool: """ There are two ways for images: local image path or url. """ return is_url(string) or string.startswith(DEFAULT_TEMP_DIR) def is_url(string: str) -> bool: """ Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately invalidated the url """ if " " in string: return False result = urlparse(string) return all([result.scheme, result.netloc]) def isolate_images_urls(prompt_list: List) -> List: linearized_list = [] for prompt in prompt_list: # Prompt can be either a string, or a PIL image if isinstance(prompt, PIL.Image.Image): linearized_list.append(prompt) elif isinstance(prompt, str): if "<fake_token_around_image>" not in prompt: linearized_list.append(prompt) else: prompt_splitted = prompt.split("<fake_token_around_image>") for ps in prompt_splitted: if ps == "": continue if ps.startswith("<image:"): linearized_list.append(ps[7:-1]) else: linearized_list.append(ps) else: raise TypeError( f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`," " `PIL.Image.Image`]" ) return linearized_list def cot_langchain_llama27b(query_string: str) -> str: inference_server_url_local = "http://<tgi_llama_2_hostname>:8083" llm_local = HuggingFaceTextGenInference( inference_server_url=inference_server_url_local, max_new_tokens=200, top_k=5, top_p=0.96, typical_p=0.95, temperature=0.001, repetition_penalty=1.08, ) template = """Use the following pieces of context to fully understand the intent and create sub staks to address the context. Please try not to, make up an answer nor hallucinate. Use five maximum sentences and keep the sub tasks as precise as possible. List all actionable steps in detail. Be cautious to avoid phrasing that might replicate previous inquiries. This will help in obtaining an accurate and detailed answer. Avoid repetition for clarity. Question: {question} Answer: Understand the intent of the question then break down the {question} in to sub-tasks. """ prompt = PromptTemplate( template=template, input_variables= ["question"] ) llm_chain_local = LLMChain(prompt=prompt, llm=llm_local) cot_return = llm_chain_local(query_string)["text"].replace("\n", "") return f'. Please follow the sub tasks listed below and organize your answers in a short paragraph with precisise and professional writing style plus duplicate avoidance: {cot_return}' def fetch_images(url_list: str) -> PIL.Image.Image: """Fetching images""" return PROCESSOR.image_processor.fetch_images(url_list) def handle_manual_images_in_user_prompt(user_prompt: str) -> List[str]: """ Handle the case of textually manually inputted images (i.e. the `<fake_token_around_image><image:IMG_URL><fake_token_around_image>`) in the user prompt by fetching them, saving them locally and replacing the whole sub-sequence the image local path. """ if "<fake_token_around_image>" in user_prompt: splitted_user_prompt = isolate_images_urls([user_prompt]) resulting_user_prompt = [] for u_p in splitted_user_prompt: if is_url(u_p): img = fetch_images([u_p])[0] tmp_file = pil_to_temp_file(img) resulting_user_prompt.append(tmp_file) else: resulting_user_prompt.append(u_p) return resulting_user_prompt else: return [user_prompt] def gradio_link(img_path: str) -> str: #url = f"{GRADIO_LINK}/file={img_path}" #url = f"{format(os.path.basename(image_path.name))}" #url = f"{img_path}" #key_name = f'uploads/{os.path.basename(img_path)}' new_file_name =str(img_path)[12:] #bucket = 'bedrock-415275363822' #s3_client.upload_file(Filename=img_path, Bucket=bucket, Key=key_name) orig_image_path = img_path return f'{HTTPD_URL}{new_file_name}' #return "https://{0}.s3.us-east-1.amazonaws.com/{1}".format(bucket, key_name) def prompt_list_to_markdown(prompt_list: List[str]) -> str: """ Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into the markdown format that is used for the chatbot history and rendering. """ resulting_string = "" for elem in prompt_list: if is_image(elem): if is_url(elem): resulting_string += f"![]({elem})" else: resulting_string += f"![](/file={elem})" else: resulting_string += elem return resulting_string def prompt_list_to_tgi_input(prompt_list: List[str]) -> str: """ TGI expects a string that contains both text and images in the image markdown format (i.e. the `![]()` ). The images links are parsed on TGI side """ result_string_input = "" for elem in prompt_list: if is_image(elem): if is_url(elem): result_string_input += f"![]({elem})" else: result_string_input += f"![]({gradio_link(img_path=elem)})" else: result_string_input += elem return result_string_input def remove_spaces_around_token(text: str) -> str: pattern = r"\s*(<fake_token_around_image>)\s*" replacement = r"\1" result = re.sub(pattern, replacement, text) return result # Chatbot utils def format_user_prompt_with_im_history_and_system_conditioning( current_user_prompt_str: str, current_image: Optional[str], history: List[Tuple[str, str]] ) -> Tuple[List[str], List[str]]: """ Produces the resulting list that needs to go inside the processor. It handles the potential image box input, the history and the system conditionning. """ resulting_list = copy.deepcopy(SYSTEM_PROMPT) #CoT Alfred cot_added_str = cot_langchain_llama27b(current_user_prompt_str.strip()) if ("detail" in current_user_prompt_str.lower() or "elaborate" in current_user_prompt_str.lower() or "comprehen" in current_user_prompt_str.lower() or 'depict' in current_user_prompt_str.lower()) else "" # Format history for turn in history: user_utterance, assistant_utterance = turn splitted_user_utterance = split_str_on_im_markdown(user_utterance) optional_space = "" if not is_image(splitted_user_utterance[0]): optional_space = " " resulting_list.append(f"\nUser:{optional_space}") resulting_list.extend(splitted_user_utterance) # CoT Alfred resulting_list.append(cot_added_str) resulting_list.append(f"<end_of_utterance>\nAssistant: {assistant_utterance}") # Format current input current_user_prompt_str = remove_spaces_around_token(current_user_prompt_str) if current_image is None: if "![](" in current_user_prompt_str: current_user_prompt_list = split_str_on_im_markdown(current_user_prompt_str) else: current_user_prompt_list = handle_manual_images_in_user_prompt(current_user_prompt_str) optional_space = "" if not is_image(current_user_prompt_list[0]): # Check if the first element is an image (and more precisely a path to an image) optional_space = " " resulting_list.append(f"\nUser:{optional_space}") resulting_list.extend(current_user_prompt_list) #CoT Alfred resulting_list.append(cot_added_str) resulting_list.append("<end_of_utterance>\nAssistant:") else: # Choosing to put the image first when the image is inputted through the UI, but this is an arbiratrary choice. resulting_list.extend(["\nUser:", current_image, f"{current_user_prompt_str}{cot_added_str}<end_of_utterance>\nAssistant:"]) current_user_prompt_list = [current_user_prompt_str] return resulting_list, current_user_prompt_list # dope_callback = gr.CSVLogger() # problematic_callback = gr.CSVLogger() textbox = gr.Textbox( placeholder="Upload an image and send a message", show_label=False, # value="Describe the battle against the fierce dragons.", visible=True, container=False, label="Text input", scale=6, ) with gr.Blocks(title="Multimodal Playground", theme=gr.themes.Base()) as demo: gr.HTML("""<h1 align="center">Multimodal Playground</h1>""") with gr.Row(variant="panel"): with gr.Column(scale=1): gr.Image(DocAid_logo, elem_id="banner-image", show_label=False, show_download_button=False, height=200, weight=100) with gr.Column(scale=5): gr.HTML(""" <p>📚 The demo presents <strong>Dialogue Guided Visual Language Processing</strong>, an multimodality VLP pirpeline based on LLM (i.e. Llama-v2) and VLM (i.e. IDEFICS) model that processes both image, text and voicenputs.</p> <p>🅿️ <strong>Intended uses:</strong> This demo serves as a proof of concept for multimodal generation. To prepare it for production, further refinement, including fine-tuning and expert evaluation, is necessary.</p> <p>⛔️ <strong>Limitations:</strong> The model might generate inaccurate information, invent details from images or text, and often overlooks minute image details. Although it generally avoids responding to dubious user queries, it can still produce outputs that may be racist, stereotypical, or offensive, especially when specifically prompted.</p> """) # with gr.Row(): # with gr.Column(scale=2): with gr.Row(elem_id="model_selector_row"): model_selector = gr.Dropdown( choices=MODELS, value="local/idefics-9b-instruct", interactive=True, show_label=False, container=False, label="Model", visible=False, ) imagebox = gr.Image(type="filepath", label="Image input", visible=False) with gr.Row(): # def prefetch_images_in_history(user_prompt_str): # """ # Pre-fetch the images that are passed in the chatbot default history. # """ # return prompt_list_to_markdown(handle_manual_images_in_user_prompt(user_prompt_str)) chatbot = gr.Chatbot( elem_id="chatbot", label="Multimodal", visible=True, height=1000, avatar_images=[None, BOT_AVATAR] # value=[ # [ # ( # prefetch_images_in_history( # """Write a funny story including the following characters. # There should be drama and most importantly romance. # Character 1:<fake_token_around_image><image:https://m.media-amazon.com/images/M/MV5BODg1OTczMWEtNTU3MS00OTUzLThjODEtNDg1MWQwZmExYmFlXkEyXkFqcGdeQWFybm8@._V1_QL75_UX500_CR0,0,500,281_.jpg><fake_token_around_image> # Character 2:<fake_token_around_image><image:https://static.wikia.nocookie.net/dreamworks/images/0/09/Shrek_dreamworks.png/revision/latest?cb=20170921225439><fake_token_around_image> # Character 3:<fake_token_around_image><image:https://upload.wikimedia.org/wikipedia/en/e/ef/Marsupilami_1.jpg><fake_token_around_image> # The plot should take place in the world pictured here:<fake_token_around_image><image:https://www.astronomy.com/wp-content/uploads/sites/2/2021/09/ON1.jpg><fake_token_around_image>""" # ) # ), # """Once upon a time, in a world where the sky was a dome and the ground was covered in grass, there lived a beautiful princess named Mulan. She was kind, brave, and always ready to help others. One day, while walking through the forest, she stumbled upon a strange creature. It was a green ogre named Shrek, who was lost and looking for his way back home. # Mulan, being the kind-hearted person she was, decided to help Shrek find his way back. As they traveled together, they began to develop a strong bond. Shrek was impressed by Mulan's bravery and determination, while Mulan admired Shrek's loyalty and sense of humor. # However, their journey was not without obstacles. They encountered a mischievous cat named Hoboua, who was always causing trouble and getting into fights. Despite his troublemaking ways, Hoboua had a good heart and eventually became a valuable ally to Mulan and Shrek. # As they traveled, they faced many challenges, including battling fierce dragons and navigating treacherous mountains. Through it all, Mulan and Shrek grew closer, and their feelings for each other deepened. # Finally, they reached Shrek's home, and he was reunited with his family and friends. Mulan, however, was sad to leave him behind. But Shrek had a surprise for her. He had fallen in love with her and wanted to be with her forever. # Mulan was overjoyed, and they shared a passionate kiss. From that day on, they lived happily ever after, exploring the world together and facing any challenges that came their way. # And so, the story of Mulan and Shrek's romance came to an end, leaving a lasting impression on all who heard it.""", # ], # ], ) with gr.Group(): with gr.Row(): with gr.Column(): textbox.render() with gr.Column(): asr_audio = gr.Audio( label="Input Audio", show_label=True, source="microphone", type="filepath") with gr.Row(): #textbox.render() submit_btn = gr.Button(value="▶️ Submit", visible=True) clear_btn = gr.ClearButton([textbox, imagebox, chatbot], value="🧹 Clear") regenerate_btn = gr.Button(value="🔄 Regenerate", visible=True) upload_btn = gr.UploadButton("📁 Upload image", file_types=["image"]) asr_btn = gr.Button("😬 Transcribe") # with gr.Group(): # with gr.Row(): # with gr.Column(scale=1, min_width=50): # dope_bttn = gr.Button("Dope🔥") # with gr.Column(scale=1, min_width=50): # problematic_bttn = gr.Button("Problematic😬") with gr.Row(): with gr.Accordion("Advanced settings", open=False, visible=True) as parameter_row: max_new_tokens = gr.Slider( minimum=8, maximum=1024, value=512, step=1, interactive=True, label="Maximum number of new tokens to generate", ) repetition_penalty = gr.Slider( minimum=0.01, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Repetition penalty", info="1.0 is equivalent to no penalty", ) decoding_strategy = gr.Radio( [ "Greedy", "Top P Sampling", ], value="Greedy", label="Decoding strategy", interactive=True, info="Higher values is equivalent to sampling more low-probability tokens.", ) temperature = gr.Slider( minimum=0.0, maximum=5.0, value=0.4, step=0.1, interactive=True, visible=False, label="Sampling temperature", info="Higher values will produce more diverse outputs.", ) decoding_strategy.change( fn=lambda selection: gr.Slider.update( visible=( selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"] ) ), inputs=decoding_strategy, outputs=temperature, ) top_p = gr.Slider( minimum=0.01, maximum=0.99, value=0.8, step=0.01, interactive=True, visible=False, label="Top P", info="Higher values is equivalent to sampling more low-probability tokens.", ) decoding_strategy.change( fn=lambda selection: gr.Slider.update(visible=(selection in ["Top P Sampling"])), inputs=decoding_strategy, outputs=top_p, ) gr.Markdown( """<p><strong>💡 Pro tip</strong>:<br> You can input an arbitrary number of images at arbitrary positions in the same query.<br> You will need to input each image with its URL with the syntax <code>&lt;fake_token_around_image&gt;&lt;image:IMAGE_URL&gt;&lt;fake_token_around_image&gt;</code>.<br> For example, for two images, you could input <code>TEXT_1&lt;fake_token_around_image&gt;&lt;image:IMAGE_URL_1&gt;&lt;fake_token_around_image&gt;TEXT_2&lt;fake_token_around_image&gt;&lt;image:IMAGE_URL_2&gt;&lt;fake_token_around_image&gt;TEXT_3</code>.<br> In the particular case where two images are consecutive, it is not necessary to add an additional separator: <code>&lt;fake_token_around_image&gt;&lt;image:IMAGE_URL_1&gt;&lt;fake_token_around_image&gt;&lt;image:IMAGE_URL_2&gt;&lt;fake_token_around_image&gt;</code>.</p>""" ) def model_inference( model_selector, user_prompt_str, chat_history, image, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ): if user_prompt_str.strip() == "" and image is None: return "", None, chat_history formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning( current_user_prompt_str=user_prompt_str.strip(), # With CoT #current_user_prompt_str=f'{user_prompt_str.strip()}. {cot_langchain_llama27b(user_prompt_str.strip())}', current_image=image, history=chat_history, ) client_endpoint = API_PATHS[model_selector] client = Client( base_url=client_endpoint, headers={"x-use-cache": "0", "Authorization": f"Bearer {API_TOKEN}"}, ) # Common parameters to all decoding strategies # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies generation_args = { "max_new_tokens": max_new_tokens, "repetition_penalty": repetition_penalty, "stop_sequences": EOS_STRINGS, } assert decoding_strategy in [ "Greedy", "Top P Sampling", ] if decoding_strategy == "Greedy": generation_args["do_sample"] = False elif decoding_strategy == "Top P Sampling": generation_args["temperature"] = temperature generation_args["do_sample"] = True generation_args["top_p"] = top_p mask_filename = None orig_image_path = None if image is None: top_n = M.mclass(text_prompt=user_prompt_str, topics=['Others', 'Generate image from text', 'Generate image from image', 'Image segmentation'], top_k=1) for label, score in top_n: print(f'With label: {label} and score: {score}') if ('Image segmentation' in label and score >= 0.65 ): words_list = kw_model.extract_keywords(docs=user_prompt_str, keyphrase_ngram_range=(1,3)) words_list = [*words_list[0],][0].split() print(f'{words_list} and with type {type(words_list)}') stopwords = ['mask', 'create', 'generate', 'image', 'cut', 'edge', 'picture', 'photo', 'segment', 'new', 'her', 'his', 'my', 'the', 'that', 'this'] top_word = [i for i in words_list if i not in stopwords][0] orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = dino_sam(image_path=orig_image_path, text_prompt=top_word, \ output_dir='/temp/gradio/outputs', box_threshold=0.5, text_threshold=0.55) view_mask_filename = f'[View generated image with with large size.]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ #f"{prompt_list_to_markdown(user_prompt_list + [view_mask_filename] + [mask_filename])}", f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}", ] ) elif ('generate image from image' in label.lower() and score >= 0.81 ): orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = image_gen(prompt=user_prompt_str, image_path=orig_image_path) if filename is not None: view_mask_filename = f' [View generated imagewith large sie.]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}", ] ) elif ('generate image from text' in label.lower() and score >= 0.81 ): filename = image_gen(prompt=user_prompt_str, image_path=None) if filename is not None: view_mask_filename = f' [View generated image]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}" ] ) yield "", None, chat_history else: chat_history.append([prompt_list_to_markdown(user_prompt_list), '']) else: # Case where the image is passed through the Image Box. # Convert the image into base64 for both passing it through the chat history and # displaying the image inside the same bubble as the text. chat_history.append( [ f"{prompt_list_to_markdown([image] + user_prompt_list)}", '', ] ) query = prompt_list_to_tgi_input(formated_prompt_list) print(query) #query += cot_langchain_llama27b(user_prompt_str.strip()) #print(f'New query: {query}') stream = client.generate_stream(prompt=query, **generation_args) acc_text = "" if mask_filename is not None: #chat_history.append([prompt_list_to_markdown(user_prompt_list), '']) yield "", None, chat_history else: for idx, response in enumerate(stream): text_token = response.token.text if response.details: # That's the exit condition return if text_token in STOP_SUSPECT_LIST: acc_text += text_token continue if idx == 0 and text_token.startswith(" "): text_token = text_token.lstrip() acc_text += text_token last_turn = chat_history.pop(-1) last_turn[-1] += acc_text if last_turn[-1].endswith("\nUser"): # Safeguard: sometimes (rarely), the model won't generate the token `<end_of_utterance>` and will go directly to generating `\nUser:` # It will thus stop the generation on `\nUser:`. But when it exits, it will have already generated `\nUser` # This post-processing ensures that we don't have an additional `\nUser` wandering around. last_turn[-1] = last_turn[-1][:-5] chat_history.append(last_turn) yield "", None, chat_history acc_text = "" def asr_inference(audio): audio = whisper.load_audio(audio) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(asr_model.device) _, probs = asr_model.detect_language(mel) options = whisper.DecodingOptions(fp16 = False) result = whisper.decode(asr_model, mel, options) return(result.text) def model_inference_asr( model_selector, audio, chat_history, image, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ): user_prompt_str = asr_inference(audio) acc_text = "" if user_prompt_str.strip() == "" and image is None: return "", None, chat_history formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning( current_user_prompt_str=user_prompt_str.strip(), current_image=image, history=chat_history, ) client_endpoint = API_PATHS[model_selector] client = Client( base_url=client_endpoint, headers={"x-use-cache": "0", "Authorization": f"Bearer {API_TOKEN}"}, ) # Common parameters to all decoding strategies # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies generation_args = { "max_new_tokens": max_new_tokens, "repetition_penalty": repetition_penalty, "stop_sequences": EOS_STRINGS, } print(f'Chat_history:{type(chat_history)} and the 1st {chat_history[0]}') orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] print(f'...... and the image_path {orig_image_path}') assert decoding_strategy in [ "Greedy", "Top P Sampling", ] if decoding_strategy == "Greedy": generation_args["do_sample"] = False elif decoding_strategy == "Top P Sampling": generation_args["temperature"] = temperature generation_args["do_sample"] = True generation_args["top_p"] = top_p mask_filename = None if image is None: top_n = M.mclass(text_prompt=user_prompt_str, topics=['Others', 'Generate image from text', 'Generate image from image', 'Image segmentation'], top_k=1) for label, score in top_n: print(f'With label: {label} and score: {score}') if ('Image segmentation' in label and score >= 0.65 ): words_list = kw_model.extract_keywords(docs=user_prompt_str, keyphrase_ngram_range=(1,3)) words_list = [*words_list[0],][0].split() print(f'{words_list} and with type {type(words_list)}') stopwords = ['mask', 'create', 'generate', 'image', 'cut', 'edge', 'picture', 'photo', 'segment', 'new', 'her', 'his', 'my', 'the', 'that', 'this'] top_word = [i for i in words_list if i not in stopwords][0] orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = dino_sam(image_path=orig_image_path, text_prompt=top_word, \ output_dir='/temp/gradio/outputs', box_threshold=0.5, text_threshold=0.55) view_mask_filename = f' [View generated image with with large size.]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ #f"{prompt_list_to_markdown(user_prompt_list + [view_mask_filename] + [mask_filename])}", f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}", ] ) elif ('generate image from image' in label.lower() and score >= 0.81 ): orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = image_gen(prompt=user_prompt_str, image_path=orig_image_path) if filename is not None: view_mask_filename = f' [View generated imagewith large sie.]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}", ] ) elif ('generate image from text' in label.lower() and score >= 0.81 ): filename = image_gen(prompt=user_prompt_str, image_path=None) if filename is not None: view_mask_filename = f' [View generated image]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list)}", f"{mask_filename} {view_mask_filename}" ] ) yield "", None, chat_history else: chat_history.append([prompt_list_to_markdown(user_prompt_list), '']) ''' for label, score in top_n: print(f'With label: {label} and score: {score}') if ('Others' not in label and score >=0.55): if ('Image segmentation' in label and score >= 0.65 ): words_list = kw_model.extract_keywords(docs=user_prompt_str, keyphrase_ngram_range=(1,3)) words_list = [*words_list[0],][0].split() print(f'{words_list} and with type {type(words_list)}') stopwords = ['mask', 'create', 'generate', 'image', 'cut', 'edge', 'picture', 'photo', 'segment', 'new', 'her', 'his', 'my', 'the', 'that', 'this'] top_word = [i for i in words_list if i not in stopwords][0] orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = dino_sam(image_path=orig_image_path, text_prompt=top_word, \ output_dir='/temp/gradio/outputs', box_threshold=0.5, text_threshold=0.55) view_mask_filename = f' [View generated image]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list + [view_mask_filename] + [mask_filename])}", '', ] ) else: if ('generate image from image' in label.lower() and score >= 0.60 ): orig_image_path = re.findall('\((.*?)\)', chat_history[0][0])[0].split('=')[1] filename = image_gen(prompt=user_prompt_str, image_path=orig_image_path) if filename is not None: view_mask_filename = f' [View generated image]({HTTPD_URL}outputs/{filename})' mask_filename = f'![](/file=/tmp/gradio/outputs/{filename})' chat_history.append( [ f"{prompt_list_to_markdown(user_prompt_list + [view_mask_filename] + [mask_filename])}", '', ] ) yield "", None, chat_history else: chat_history.append([prompt_list_to_markdown(user_prompt_list), '']) ''' elif mask_filename is None: # Case where the image is passed through the Image Box. # Convert the image into base64 for both passing it through the chat history and # displaying the image inside the same bubble as the text. chat_history.append( [ f"{prompt_list_to_markdown([image] + user_prompt_list)}", '', ] ) query = prompt_list_to_tgi_input(formated_prompt_list) stream = client.generate_stream(prompt=query, **generation_args) if mask_filename is not None: yield "", None, chat_history else: for idx, response in enumerate(stream): text_token = response.token.text if response.details: # That's the exit condition return if text_token in STOP_SUSPECT_LIST: acc_text += text_token continue if idx == 0 and text_token.startswith(" "): text_token = text_token.lstrip() acc_text += text_token last_turn = chat_history.pop(-1) last_turn[-1] += acc_text if last_turn[-1].endswith("\nUser"): # Safeguard: sometimes (rarely), the model won't generate the token `<end_of_utterance>` and will go directly to generating `\nUser:` # It will thus stop the generation on `\nUser:`. But when it exits, it will have already generated `\nUser` # This post-processing ensures that we don't have an additional `\nUser` wandering around. last_turn[-1] = last_turn[-1][:-5] chat_history.append(last_turn) yield "", None, chat_history acc_text = "" def process_example(message, image): """ Same as `model_inference` but in greedy mode and with the 80b-instruct. Specifically for pre-computing the default examples. """ model_selector="local/idefics-9b-instruct" user_prompt_str=message chat_history=[] max_new_tokens=512 formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning( current_user_prompt_str=user_prompt_str.strip(), current_image=image, history=chat_history, ) client_endpoint = API_PATHS[model_selector] client = Client( base_url=client_endpoint, headers={"x-use-cache": "0", "Authorization": f"Bearer {API_TOKEN}"}, timeout=240, # Generous time out just in case because we are in greedy. All examples should be computed in less than 30secs with the 80b-instruct. ) # Common parameters to all decoding strategies # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies generation_args = { "max_new_tokens": max_new_tokens, "repetition_penalty": None, "stop_sequences": EOS_STRINGS, "do_sample": False, } if image is None: # Case where there is no image OR the image is passed as `<fake_token_around_image><image:IMAGE_URL><fake_token_around_image>` chat_history.append([prompt_list_to_markdown(user_prompt_list), '']) else: # Case where the image is passed through the Image Box. # Convert the image into base64 for both passing it through the chat history and # displaying the image inside the same bubble as the text. chat_history.append( [ f"{prompt_list_to_markdown([image] + user_prompt_list)}", '', ] ) # Hack - see explanation in `DEFAULT_IMAGES_TMP_PATH_TO_URL` for idx, i in enumerate(formated_prompt_list): if i.startswith(DEFAULT_TEMP_DIR): for k, v in DEFAULT_IMAGES_TMP_PATH_TO_URL.items(): if k == i: formated_prompt_list[idx] = v break query = prompt_list_to_tgi_input(formated_prompt_list) print(query) generated_text = client.generate(prompt=query, **generation_args).generated_text if generated_text.endswith("\nUser"): generated_text = generated_text[:-5] last_turn = chat_history.pop(-1) last_turn[-1] += generated_text chat_history.append(last_turn) return "", None, chat_history textbox.submit( fn=model_inference, inputs=[ model_selector, textbox, chatbot, imagebox, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ], outputs=[textbox, imagebox, chatbot], ) submit_btn.click( fn=model_inference, inputs=[ model_selector, textbox, chatbot, imagebox, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ], outputs=[ textbox, imagebox, chatbot, ], ) def remove_last_turn(chat_history): if len(chat_history) == 0: return gr.Update(), gr.Update() last_interaction = chat_history[-1] chat_history = chat_history[:-1] chat_update = gr.update(value=chat_history) text_update = gr.update(value=last_interaction[0]) return chat_update, text_update regenerate_btn.click(fn=remove_last_turn, inputs=chatbot, outputs=[chatbot, textbox]).then( fn=model_inference, inputs=[ model_selector, textbox, chatbot, imagebox, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ], outputs=[ textbox, imagebox, chatbot, ], ) asr_btn.click( fn=model_inference_asr, inputs=[ model_selector, asr_audio, chatbot, imagebox, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p, ], outputs=[ textbox, imagebox, chatbot, ], ) upload_btn.upload(add_file, [upload_btn], [imagebox, upload_btn], queue=False) submit_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn) textbox.submit(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn) clear_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn) asr_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn) examples_path = os.getcwd() gr.Examples( examples=[ [ ( "Which device produced this image? Please explain the main clinical purpose of such image?" "Can you write a radiology report based on this image?" ), f"{examples_path}/example_images/chest-ct.jpg", ], [ "Can you describe the nature of this image? Do you think it's real?", f"{examples_path}/example_images/fashion_12.jpg", ], [ "Can you describe the action on this image? How many animals total are there in this image? Please identify the species by name with best effort.", f"{examples_path}/example_images/assets/demo8.jpg", ], [ "Name the sport from this image? Please identify the player's role by name with best effort.", f"{examples_path}/example_images/college_football.jpg", ], ], inputs=[textbox, imagebox], outputs=[textbox, imagebox, chatbot], fn=process_example, cache_examples=True, examples_per_page=6, label=( "Click on any example below to get started.\nFor convenience, the model generations have been" " pre-computed with `idefics-9b-instruct`." ), ) demo.queue(concurrency_count=40, max_size=40) demo.launch(debug=True, server_name="0.0.0.0", server_port=7863, height=2048, share=False, ssl_verify=False, ssl_keyfile="<cert_key_file>", ssl_certfile="<cert_pem_file>", auth=("_user_name>", "<passcode>"))
[ "question", "<fake_token_around_image>", "['\"The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called RadAide, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and RadAide will do its best to answer User’s questions. RadAide has the ability to perceive images and reason about the content of visual inputs. It can also process images by following precise instructs. RadAide was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts. The conversation begins:', '\\nUser:', 'https://miro.medium.com/v2/resize:fit:1332/0*yl2b-bDJeEwKPUI5Describe the nature of this image.<end_of_utterance>', '\\\\RadAide: A tattooed person holding a sign that says, “Teach your children well,” in a crowd of people. In the middle of the sign, there’s an illustration of the earth with 2 raised fists on either side that have a rainbow pride square background, a trans pride circle background, and brown skin tone stripes on the fists. The raised fist is a symbol of solidarity and specifically Black power as popularized by the Black Panther Party in the 1960s. The rainbow pride flag has rainbow stripes and symbolizes general LGBTQ pride. The trans pride flag has pink, blue, and white stripes and celebrates pride for the trans and gender non-conforming umbrella.<end_of_utterance>', '\\nUser:How about this image?', 'https://cdn.pixabay.com/photo/2017/09/25/13/12/puppy-2785074_1280.jpg', 'Can you describe it too?<end_of_utterance>', '\\\\RadAide: A dog is lying on the floor, looking at the camera. It is looking directly at you. The dog has a white body and brown patches on its face and ears. Its eyes are dark. Its nose is black, and it has long, floppy ears, white paws, long fur, big eyes.<end_of_utterance>', '\\nUser: What kind of breed is it?<end_of_utterance>', '\\\\RadAide: It looks like a Cavalier King Charles Spaniel. These breeds are known for their gentle, affectionate nature. Their long ears and big, brown eyes give them a sweet, expressive face. Cavaliers are small dogs weighing between 13 and 20 pounds. They have a silky, medium-length coat that comes in various colors, including black and tan, tricolor, red and white, or black.<end_of_utterance>', '\\nUser: What can you tell me about this breed of dogs?<end_of_utterance>', '\\\\RadAide: One specific characteristic of the Cavalier King Charles Spaniel is its friendly and affectionate nature. This breed is known for being extremely sociable and forming strong bonds with their owners. They are often described as \"velcro dogs\" because they love to be close to their human companions, whether it\\'s sitting on their laps, cuddling on the couch, or simply following them around the house.<end_of_utterance>', '\\nUser: ghjkhjabnufs<end_of_utterance>', '\\\\RadAide: That doesn’t seem to be a word. Could you ask me another way?<end_of_utterance>', '\\nUser: Do you like Cavalier King Charles Spaniel?<end_of_utterance>', '\\\\RadAide: I do not have personal opinions as I’m just a computer program. However, cavaliers are known for being excellent family pets due to their gentle and patient demeanor, making them great with children and other pets. Their affectionate nature also makes them ideal therapy dogs, as they can provide comfort and emotional support to those in need.<end_of_utterance>', '\\nUser: How many dogs do you see in this image?', 'https://i.dailymail.co.uk/i/pix/2011/07/01/article-2010308-0CD22A8300000578-496_634x414.jpg', '<end_of_utterance>', '\\nAssistant: There is no dogs in this image. The picture shows a tennis player jumping to volley the ball.<end_of_utterance>']", "[]", "Use the following pieces of context to fully understand the intent and create sub staks to address the context. Please try not to, \n make up an answer nor hallucinate. Use five maximum sentences and keep the sub tasks as precise as possible. List all actionable steps in \n detail. Be cautious to avoid phrasing that might replicate previous inquiries. This will help in obtaining an accurate and detailed answer. \n Avoid repetition for clarity.\n\n Question: {question}\n Answer: Understand the intent of the question then break down the {question} in to sub-tasks. ", "['poorly rendered', 'poor background details', 'poorly drawn dog', 'disfigured dog features', 'blurry']" ]
2024-01-10
thomascherickal1/trainteams.ai
trainteams.com~input_read_and_store.py
import regex as re from PyPDF2 import PdfReader from langchain.vectorstores import DeepLake import spacy from langchain.embeddings import SentenceTransformerEmbeddings pdfFile = open('Rust Programming.pdf', 'rb') reader = PdfReader(pdfFile) document = "" print(len(reader.pages)) for pageNum in range(len(reader.pages)): page = reader.pages[pageNum].extract_text() document += page document = document.lower() # Step 2: Remove punctuation document = re.sub(r'[^\w\s]', '', document) nlp = spacy.load("en_core_web_sm") doc = nlp(document) tokens = [token.text for token in doc] embedding_function = SentenceTransformerEmbeddings(model_name='all-MiniLM-L6-v2') # Define your documents and embedding function docs = tokens # the documents need to be a list of lists of words # Create and persist the vector store db = DeepLake.from_texts(docs, embedding_function, dataset_path="./deeplake_db")
[]
2024-01-10
thomascherickal1/trainteams.ai
trainteams.com~interface_with_the_user.py
import gradio as gr from langchain.vectorstores import VectorStore from langchain.embeddings import SentenceTransformerEmbeddings from langchain.vectorstores import DeepLake from langchain.llms import OpenAI from langchain.chains import ConversationChain from langchain.prompts import PromptTemplate embedding_function = SentenceTransformerEmbeddings(model_name='all-MiniLM-L6-v2') # Initialize vector store vectorstore = DeepLake("./deeplake_db",embedding_function) # Define the LLM llm = OpenAI(temperature=0) # Create the conversation chain chain = ConversationChain(llm=llm, vectorstore=vectorstore) #Prompt formate to the LLM prompt = PromptTemplate( input="Human: {human_input}\nAssistant: ", output="Human: " ) def chat(input): # Chatbot logic response = chain.predict(prompt, vectorstore=vectorstore, input=input)["output"] return response iface = gr.Interface( fn=chat, inputs=gr.inputs.Textbox(lines=2, placeholder="Ask a question..."), outputs=gr.outputs.Textbox(), title="Rust Programming Chatbot", description="I'll teach you the Rust Programming Language!" ) if __name__ == "__main__": iface.launch()
[ "Human: {human_input}\nAssistant: ", "Human: " ]
2024-01-10
pranavp10/chroma
chromadb~utils~embedding_functions.py
import hashlib import logging from tenacity import stop_after_attempt, wait_random, retry, retry_if_exception from chromadb.api.types import ( Document, Documents, Embedding, Image, Images, EmbeddingFunction, Embeddings, is_image, is_document, ) from pathlib import Path import os import tarfile import requests from typing import Any, Dict, List, Mapping, Union, cast import numpy as np import numpy.typing as npt import importlib import inspect import sys from typing import Optional try: from chromadb.is_thin_client import is_thin_client except ImportError: is_thin_client = False logger = logging.getLogger(__name__) def _verify_sha256(fname: str, expected_sha256: str) -> bool: sha256_hash = hashlib.sha256() with open(fname, "rb") as f: # Read and update hash in chunks to avoid using too much memory for byte_block in iter(lambda: f.read(4096), b""): sha256_hash.update(byte_block) return sha256_hash.hexdigest() == expected_sha256 class SentenceTransformerEmbeddingFunction(EmbeddingFunction[Documents]): # Since we do dynamic imports we have to type this as Any models: Dict[str, Any] = {} # If you have a beefier machine, try "gtr-t5-large". # for a full list of options: https://huggingface.co/sentence-transformers, https://www.sbert.net/docs/pretrained_models.html def __init__( self, model_name: str = "all-MiniLM-L6-v2", device: str = "cpu", normalize_embeddings: bool = False, ): if model_name not in self.models: try: from sentence_transformers import SentenceTransformer except ImportError: raise ValueError( "The sentence_transformers python package is not installed. Please install it with `pip install sentence_transformers`" ) self.models[model_name] = SentenceTransformer(model_name, device=device) self._model = self.models[model_name] self._normalize_embeddings = normalize_embeddings def __call__(self, input: Documents) -> Embeddings: return self._model.encode( # type: ignore list(input), convert_to_numpy=True, normalize_embeddings=self._normalize_embeddings, ).tolist() class Text2VecEmbeddingFunction(EmbeddingFunction[Documents]): def __init__(self, model_name: str = "shibing624/text2vec-base-chinese"): try: from text2vec import SentenceModel except ImportError: raise ValueError( "The text2vec python package is not installed. Please install it with `pip install text2vec`" ) self._model = SentenceModel(model_name_or_path=model_name) def __call__(self, input: Documents) -> Embeddings: return self._model.encode(list(input), convert_to_numpy=True).tolist() # type: ignore # noqa E501 class OpenAIEmbeddingFunction(EmbeddingFunction[Documents]): def __init__( self, api_key: Optional[str] = None, model_name: str = "text-embedding-ada-002", organization_id: Optional[str] = None, api_base: Optional[str] = None, api_type: Optional[str] = None, api_version: Optional[str] = None, deployment_id: Optional[str] = None, default_headers: Optional[Mapping[str, str]] = None, ): """ Initialize the OpenAIEmbeddingFunction. Args: api_key (str, optional): Your API key for the OpenAI API. If not provided, it will raise an error to provide an OpenAI API key. organization_id(str, optional): The OpenAI organization ID if applicable model_name (str, optional): The name of the model to use for text embeddings. Defaults to "text-embedding-ada-002". api_base (str, optional): The base path for the API. If not provided, it will use the base path for the OpenAI API. This can be used to point to a different deployment, such as an Azure deployment. api_type (str, optional): The type of the API deployment. This can be used to specify a different deployment, such as 'azure'. If not provided, it will use the default OpenAI deployment. api_version (str, optional): The api version for the API. If not provided, it will use the api version for the OpenAI API. This can be used to point to a different deployment, such as an Azure deployment. deployment_id (str, optional): Deployment ID for Azure OpenAI. default_headers (Mapping, optional): A mapping of default headers to be sent with each API request. """ try: import openai except ImportError: raise ValueError( "The openai python package is not installed. Please install it with `pip install openai`" ) if api_key is not None: openai.api_key = api_key # If the api key is still not set, raise an error elif openai.api_key is None: raise ValueError( "Please provide an OpenAI API key. You can get one at https://platform.openai.com/account/api-keys" ) if api_base is not None: openai.api_base = api_base if api_version is not None: openai.api_version = api_version self._api_type = api_type if api_type is not None: openai.api_type = api_type if organization_id is not None: openai.organization = organization_id self._v1 = openai.__version__.startswith("1.") if self._v1: if api_type == "azure": self._client = openai.AzureOpenAI( api_key=api_key, api_version=api_version, azure_endpoint=api_base, default_headers=default_headers, ).embeddings else: self._client = openai.OpenAI( api_key=api_key, base_url=api_base, default_headers=default_headers ).embeddings else: self._client = openai.Embedding self._model_name = model_name self._deployment_id = deployment_id def __call__(self, input: Documents) -> Embeddings: # replace newlines, which can negatively affect performance. input = [t.replace("\n", " ") for t in input] # Call the OpenAI Embedding API if self._v1: embeddings = self._client.create( input=input, model=self._deployment_id or self._model_name ).data # Sort resulting embeddings by index sorted_embeddings = sorted( embeddings, key=lambda e: e.index ) # type: ignore # Return just the embeddings return [result.embedding for result in sorted_embeddings] else: if self._api_type == "azure": embeddings = self._client.create( input=input, engine=self._deployment_id or self._model_name )["data"] else: embeddings = self._client.create(input=input, model=self._model_name)[ "data" ] # Sort resulting embeddings by index sorted_embeddings = sorted( embeddings, key=lambda e: e["index"] ) # type: ignore # Return just the embeddings return [result["embedding"] for result in sorted_embeddings] class CohereEmbeddingFunction(EmbeddingFunction[Documents]): def __init__(self, api_key: str, model_name: str = "large"): try: import cohere except ImportError: raise ValueError( "The cohere python package is not installed. Please install it with `pip install cohere`" ) self._client = cohere.Client(api_key) self._model_name = model_name def __call__(self, input: Documents) -> Embeddings: # Call Cohere Embedding API for each document. return [ embeddings for embeddings in self._client.embed( texts=input, model=self._model_name, input_type="search_document" ) ] class HuggingFaceEmbeddingFunction(EmbeddingFunction[Documents]): """ This class is used to get embeddings for a list of texts using the HuggingFace API. It requires an API key and a model name. The default model name is "sentence-transformers/all-MiniLM-L6-v2". """ def __init__( self, api_key: str, model_name: str = "sentence-transformers/all-MiniLM-L6-v2" ): """ Initialize the HuggingFaceEmbeddingFunction. Args: api_key (str): Your API key for the HuggingFace API. model_name (str, optional): The name of the model to use for text embeddings. Defaults to "sentence-transformers/all-MiniLM-L6-v2". """ self._api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_name}" self._session = requests.Session() self._session.headers.update({"Authorization": f"Bearer {api_key}"}) def __call__(self, input: Documents) -> Embeddings: """ Get the embeddings for a list of texts. Args: texts (Documents): A list of texts to get embeddings for. Returns: Embeddings: The embeddings for the texts. Example: >>> hugging_face = HuggingFaceEmbeddingFunction(api_key="your_api_key") >>> texts = ["Hello, world!", "How are you?"] >>> embeddings = hugging_face(texts) """ # Call HuggingFace Embedding API for each document return self._session.post( # type: ignore self._api_url, json={"inputs": input, "options": {"wait_for_model": True}} ).json() class JinaEmbeddingFunction(EmbeddingFunction[Documents]): """ This class is used to get embeddings for a list of texts using the Jina AI API. It requires an API key and a model name. The default model name is "jina-embeddings-v2-base-en". """ def __init__(self, api_key: str, model_name: str = "jina-embeddings-v2-base-en"): """ Initialize the JinaEmbeddingFunction. Args: api_key (str): Your API key for the Jina AI API. model_name (str, optional): The name of the model to use for text embeddings. Defaults to "jina-embeddings-v2-base-en". """ self._model_name = model_name self._api_url = "https://api.jina.ai/v1/embeddings" self._session = requests.Session() self._session.headers.update( {"Authorization": f"Bearer {api_key}", "Accept-Encoding": "identity"} ) def __call__(self, input: Documents) -> Embeddings: """ Get the embeddings for a list of texts. Args: texts (Documents): A list of texts to get embeddings for. Returns: Embeddings: The embeddings for the texts. Example: >>> jina_ai_fn = JinaEmbeddingFunction(api_key="your_api_key") >>> input = ["Hello, world!", "How are you?"] >>> embeddings = jina_ai_fn(input) """ # Call Jina AI Embedding API resp = self._session.post( # type: ignore self._api_url, json={"input": input, "model": self._model_name} ).json() if "data" not in resp: raise RuntimeError(resp["detail"]) embeddings = resp["data"] # Sort resulting embeddings by index sorted_embeddings = sorted(embeddings, key=lambda e: e["index"]) # type: ignore # Return just the embeddings return [result["embedding"] for result in sorted_embeddings] class InstructorEmbeddingFunction(EmbeddingFunction[Documents]): # If you have a GPU with at least 6GB try model_name = "hkunlp/instructor-xl" and device = "cuda" # for a full list of options: https://github.com/HKUNLP/instructor-embedding#model-list def __init__( self, model_name: str = "hkunlp/instructor-base", device: str = "cpu", instruction: Optional[str] = None, ): try: from InstructorEmbedding import INSTRUCTOR except ImportError: raise ValueError( "The InstructorEmbedding python package is not installed. Please install it with `pip install InstructorEmbedding`" ) self._model = INSTRUCTOR(model_name, device=device) self._instruction = instruction def __call__(self, input: Documents) -> Embeddings: if self._instruction is None: return self._model.encode(input).tolist() # type: ignore texts_with_instructions = [[self._instruction, text] for text in input] # type: ignore return self._model.encode(texts_with_instructions).tolist() # In order to remove dependencies on sentence-transformers, which in turn depends on # pytorch and sentence-piece we have created a default ONNX embedding function that # implements the same functionality as "all-MiniLM-L6-v2" from sentence-transformers. # visit https://github.com/chroma-core/onnx-embedding for the source code to generate # and verify the ONNX model. class ONNXMiniLM_L6_V2(EmbeddingFunction[Documents]): MODEL_NAME = "all-MiniLM-L6-v2" DOWNLOAD_PATH = Path.home() / ".cache" / "chroma" / "onnx_models" / MODEL_NAME EXTRACTED_FOLDER_NAME = "onnx" ARCHIVE_FILENAME = "onnx.tar.gz" MODEL_DOWNLOAD_URL = ( "https://chroma-onnx-models.s3.amazonaws.com/all-MiniLM-L6-v2/onnx.tar.gz" ) _MODEL_SHA256 = "913d7300ceae3b2dbc2c50d1de4baacab4be7b9380491c27fab7418616a16ec3" tokenizer = None model = None # https://github.com/python/mypy/issues/7291 mypy makes you type the constructor if # no args def __init__(self, preferred_providers: Optional[List[str]] = None) -> None: # Import dependencies on demand to mirror other embedding functions. This # breaks typechecking, thus the ignores. # convert the list to set for unique values if preferred_providers and not all( [isinstance(i, str) for i in preferred_providers] ): raise ValueError("Preferred providers must be a list of strings") # check for duplicate providers if preferred_providers and len(preferred_providers) != len( set(preferred_providers) ): raise ValueError("Preferred providers must be unique") self._preferred_providers = preferred_providers try: # Equivalent to import onnxruntime self.ort = importlib.import_module("onnxruntime") except ImportError: raise ValueError( "The onnxruntime python package is not installed. Please install it with `pip install onnxruntime`" ) try: # Equivalent to from tokenizers import Tokenizer self.Tokenizer = importlib.import_module("tokenizers").Tokenizer except ImportError: raise ValueError( "The tokenizers python package is not installed. Please install it with `pip install tokenizers`" ) try: # Equivalent to from tqdm import tqdm self.tqdm = importlib.import_module("tqdm").tqdm except ImportError: raise ValueError( "The tqdm python package is not installed. Please install it with `pip install tqdm`" ) # Borrowed from https://gist.github.com/yanqd0/c13ed29e29432e3cf3e7c38467f42f51 # Download with tqdm to preserve the sentence-transformers experience @retry( reraise=True, stop=stop_after_attempt(3), wait=wait_random(min=1, max=3), retry=retry_if_exception(lambda e: "does not match expected SHA256" in str(e)), ) def _download(self, url: str, fname: str, chunk_size: int = 1024) -> None: resp = requests.get(url, stream=True) total = int(resp.headers.get("content-length", 0)) with open(fname, "wb") as file, self.tqdm( desc=str(fname), total=total, unit="iB", unit_scale=True, unit_divisor=1024, ) as bar: for data in resp.iter_content(chunk_size=chunk_size): size = file.write(data) bar.update(size) if not _verify_sha256(fname, self._MODEL_SHA256): # if the integrity of the file is not verified, remove it os.remove(fname) raise ValueError( f"Downloaded file {fname} does not match expected SHA256 hash. Corrupted download or malicious file." ) # Use pytorches default epsilon for division by zero # https://pytorch.org/docs/stable/generated/torch.nn.functional.normalize.html def _normalize(self, v: npt.NDArray) -> npt.NDArray: # type: ignore norm = np.linalg.norm(v, axis=1) norm[norm == 0] = 1e-12 return v / norm[:, np.newaxis] # type: ignore # type: ignore def _forward(self, documents: List[str], batch_size: int = 32) -> npt.NDArray: # We need to cast to the correct type because the type checker doesn't know that init_model_and_tokenizer will set the values self.tokenizer = cast(self.Tokenizer, self.tokenizer) # type: ignore self.model = cast(self.ort.InferenceSession, self.model) # type: ignore all_embeddings = [] for i in range(0, len(documents), batch_size): batch = documents[i : i + batch_size] encoded = [self.tokenizer.encode(d) for d in batch] input_ids = np.array([e.ids for e in encoded]) attention_mask = np.array([e.attention_mask for e in encoded]) onnx_input = { "input_ids": np.array(input_ids, dtype=np.int64), "attention_mask": np.array(attention_mask, dtype=np.int64), "token_type_ids": np.array( [np.zeros(len(e), dtype=np.int64) for e in input_ids], dtype=np.int64, ), } model_output = self.model.run(None, onnx_input) last_hidden_state = model_output[0] # Perform mean pooling with attention weighting input_mask_expanded = np.broadcast_to( np.expand_dims(attention_mask, -1), last_hidden_state.shape ) embeddings = np.sum(last_hidden_state * input_mask_expanded, 1) / np.clip( input_mask_expanded.sum(1), a_min=1e-9, a_max=None ) embeddings = self._normalize(embeddings).astype(np.float32) all_embeddings.append(embeddings) return np.concatenate(all_embeddings) def _init_model_and_tokenizer(self) -> None: if self.model is None and self.tokenizer is None: self.tokenizer = self.Tokenizer.from_file( os.path.join( self.DOWNLOAD_PATH, self.EXTRACTED_FOLDER_NAME, "tokenizer.json" ) ) # max_seq_length = 256, for some reason sentence-transformers uses 256 even though the HF config has a max length of 128 # https://github.com/UKPLab/sentence-transformers/blob/3e1929fddef16df94f8bc6e3b10598a98f46e62d/docs/_static/html/models_en_sentence_embeddings.html#LL480 self.tokenizer.enable_truncation(max_length=256) self.tokenizer.enable_padding(pad_id=0, pad_token="[PAD]", length=256) if self._preferred_providers is None or len(self._preferred_providers) == 0: if len(self.ort.get_available_providers()) > 0: logger.debug( f"WARNING: No ONNX providers provided, defaulting to available providers: " f"{self.ort.get_available_providers()}" ) self._preferred_providers = self.ort.get_available_providers() elif not set(self._preferred_providers).issubset( set(self.ort.get_available_providers()) ): raise ValueError( f"Preferred providers must be subset of available providers: {self.ort.get_available_providers()}" ) self.model = self.ort.InferenceSession( os.path.join( self.DOWNLOAD_PATH, self.EXTRACTED_FOLDER_NAME, "model.onnx" ), # Since 1.9 onnyx runtime requires providers to be specified when there are multiple available - https://onnxruntime.ai/docs/api/python/api_summary.html # This is probably not ideal but will improve DX as no exceptions will be raised in multi-provider envs providers=self._preferred_providers, ) def __call__(self, input: Documents) -> Embeddings: # Only download the model when it is actually used self._download_model_if_not_exists() self._init_model_and_tokenizer() res = cast(Embeddings, self._forward(input).tolist()) return res def _download_model_if_not_exists(self) -> None: onnx_files = [ "config.json", "model.onnx", "special_tokens_map.json", "tokenizer_config.json", "tokenizer.json", "vocab.txt", ] extracted_folder = os.path.join(self.DOWNLOAD_PATH, self.EXTRACTED_FOLDER_NAME) onnx_files_exist = True for f in onnx_files: if not os.path.exists(os.path.join(extracted_folder, f)): onnx_files_exist = False break # Model is not downloaded yet if not onnx_files_exist: os.makedirs(self.DOWNLOAD_PATH, exist_ok=True) if not os.path.exists( os.path.join(self.DOWNLOAD_PATH, self.ARCHIVE_FILENAME) ) or not _verify_sha256( os.path.join(self.DOWNLOAD_PATH, self.ARCHIVE_FILENAME), self._MODEL_SHA256, ): self._download( url=self.MODEL_DOWNLOAD_URL, fname=os.path.join(self.DOWNLOAD_PATH, self.ARCHIVE_FILENAME), ) with tarfile.open( name=os.path.join(self.DOWNLOAD_PATH, self.ARCHIVE_FILENAME), mode="r:gz", ) as tar: tar.extractall(path=self.DOWNLOAD_PATH) def DefaultEmbeddingFunction() -> Optional[EmbeddingFunction[Documents]]: if is_thin_client: return None else: return ONNXMiniLM_L6_V2() class GooglePalmEmbeddingFunction(EmbeddingFunction[Documents]): """To use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a PaLM API key.""" def __init__(self, api_key: str, model_name: str = "models/embedding-gecko-001"): if not api_key: raise ValueError("Please provide a PaLM API key.") if not model_name: raise ValueError("Please provide the model name.") try: import google.generativeai as palm except ImportError: raise ValueError( "The Google Generative AI python package is not installed. Please install it with `pip install google-generativeai`" ) palm.configure(api_key=api_key) self._palm = palm self._model_name = model_name def __call__(self, input: Documents) -> Embeddings: return [ self._palm.generate_embeddings(model=self._model_name, text=text)[ "embedding" ] for text in input ] class GoogleGenerativeAiEmbeddingFunction(EmbeddingFunction[Documents]): """To use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a Google API key.""" """Use RETRIEVAL_DOCUMENT for the task_type for embedding, and RETRIEVAL_QUERY for the task_type for retrieval.""" def __init__( self, api_key: str, model_name: str = "models/embedding-001", task_type: str = "RETRIEVAL_DOCUMENT", ): if not api_key: raise ValueError("Please provide a Google API key.") if not model_name: raise ValueError("Please provide the model name.") try: import google.generativeai as genai except ImportError: raise ValueError( "The Google Generative AI python package is not installed. Please install it with `pip install google-generativeai`" ) genai.configure(api_key=api_key) self._genai = genai self._model_name = model_name self._task_type = task_type self._task_title = None if self._task_type == "RETRIEVAL_DOCUMENT": self._task_title = "Embedding of single string" def __call__(self, input: Documents) -> Embeddings: return [ self._genai.embed_content( model=self._model_name, content=text, task_type=self._task_type, title=self._task_title, )["embedding"] for text in input ] class GoogleVertexEmbeddingFunction(EmbeddingFunction[Documents]): # Follow API Quickstart for Google Vertex AI # https://cloud.google.com/vertex-ai/docs/generative-ai/start/quickstarts/api-quickstart # Information about the text embedding modules in Google Vertex AI # https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings def __init__( self, api_key: str, model_name: str = "textembedding-gecko", project_id: str = "cloud-large-language-models", region: str = "us-central1", ): self._api_url = f"https://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/publishers/goole/models/{model_name}:predict" self._session = requests.Session() self._session.headers.update({"Authorization": f"Bearer {api_key}"}) def __call__(self, input: Documents) -> Embeddings: embeddings = [] for text in input: response = self._session.post( self._api_url, json={"instances": [{"content": text}]} ).json() if "predictions" in response: embeddings.append(response["predictions"]["embeddings"]["values"]) return embeddings class OpenCLIPEmbeddingFunction(EmbeddingFunction[Union[Documents, Images]]): def __init__( self, model_name: str = "ViT-B-32", checkpoint: str = "laion2b_s34b_b79k" ) -> None: try: import open_clip except ImportError: raise ValueError( "The open_clip python package is not installed. Please install it with `pip install open-clip-torch`. https://github.com/mlfoundations/open_clip" ) try: self._torch = importlib.import_module("torch") except ImportError: raise ValueError( "The torch python package is not installed. Please install it with `pip install torch`" ) try: self._PILImage = importlib.import_module("PIL.Image") except ImportError: raise ValueError( "The PIL python package is not installed. Please install it with `pip install pillow`" ) model, _, preprocess = open_clip.create_model_and_transforms( model_name=model_name, pretrained=checkpoint ) self._model = model self._preprocess = preprocess self._tokenizer = open_clip.get_tokenizer(model_name=model_name) def _encode_image(self, image: Image) -> Embedding: pil_image = self._PILImage.fromarray(image) with self._torch.no_grad(): image_features = self._model.encode_image( self._preprocess(pil_image).unsqueeze(0) ) image_features /= image_features.norm(dim=-1, keepdim=True) return cast(Embedding, image_features.squeeze().tolist()) def _encode_text(self, text: Document) -> Embedding: with self._torch.no_grad(): text_features = self._model.encode_text(self._tokenizer(text)) text_features /= text_features.norm(dim=-1, keepdim=True) return cast(Embedding, text_features.squeeze().tolist()) def __call__(self, input: Union[Documents, Images]) -> Embeddings: embeddings: Embeddings = [] for item in input: if is_image(item): embeddings.append(self._encode_image(cast(Image, item))) elif is_document(item): embeddings.append(self._encode_text(cast(Document, item))) return embeddings class AmazonBedrockEmbeddingFunction(EmbeddingFunction[Documents]): def __init__( self, session: "boto3.Session", # Quote for forward reference model_name: str = "amazon.titan-embed-text-v1", **kwargs: Any, ): """Initialize AmazonBedrockEmbeddingFunction. Args: session (boto3.Session): The boto3 session to use. model_name (str, optional): Identifier of the model, defaults to "amazon.titan-embed-text-v1" **kwargs: Additional arguments to pass to the boto3 client. Example: >>> import boto3 >>> session = boto3.Session(profile_name="profile", region_name="us-east-1") >>> bedrock = AmazonBedrockEmbeddingFunction(session=session) >>> texts = ["Hello, world!", "How are you?"] >>> embeddings = bedrock(texts) """ self._model_name = model_name self._client = session.client( service_name="bedrock-runtime", **kwargs, ) def __call__(self, input: Documents) -> Embeddings: import json accept = "application/json" content_type = "application/json" embeddings = [] for text in input: input_body = {"inputText": text} body = json.dumps(input_body) response = self._client.invoke_model( body=body, modelId=self._model_name, accept=accept, contentType=content_type, ) embedding = json.load(response.get("body")).get("embedding") embeddings.append(embedding) return embeddings class HuggingFaceEmbeddingServer(EmbeddingFunction[Documents]): """ This class is used to get embeddings for a list of texts using the HuggingFace Embedding server (https://github.com/huggingface/text-embeddings-inference). The embedding model is configured in the server. """ def __init__(self, url: str): """ Initialize the HuggingFaceEmbeddingServer. Args: url (str): The URL of the HuggingFace Embedding Server. """ try: import requests except ImportError: raise ValueError( "The requests python package is not installed. Please install it with `pip install requests`" ) self._api_url = f"{url}" self._session = requests.Session() def __call__(self, input: Documents) -> Embeddings: """ Get the embeddings for a list of texts. Args: texts (Documents): A list of texts to get embeddings for. Returns: Embeddings: The embeddings for the texts. Example: >>> hugging_face = HuggingFaceEmbeddingServer(url="http://localhost:8080/embed") >>> texts = ["Hello, world!", "How are you?"] >>> embeddings = hugging_face(texts) """ # Call HuggingFace Embedding Server API for each document return self._session.post( # type: ignore self._api_url, json={"inputs": input} ).json() # List of all classes in this module _classes = [ name for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isclass) if obj.__module__ == __name__ ] def get_builtins() -> List[str]: return _classes
[]