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
mth93/langchain
libs~community~langchain_community~agent_toolkits~slack~toolkit.py
from __future__ import annotations from typing import TYPE_CHECKING, List from libs.core.langchain_core.pydantic_v1 import Field from langchain_community.agent_toolkits.base import BaseToolkit from langchain_community.tools import BaseTool from langchain_community.tools.slack.get_channel import SlackGetChannel from langchain_community.tools.slack.get_message import SlackGetMessage from langchain_community.tools.slack.schedule_message import SlackScheduleMessage from langchain_community.tools.slack.send_message import SlackSendMessage from langchain_community.tools.slack.utils import login if TYPE_CHECKING: from slack_sdk import WebClient class SlackToolkit(BaseToolkit): """Toolkit for interacting with Slack.""" client: WebClient = Field(default_factory=login) class Config: """Pydantic config.""" arbitrary_types_allowed = True def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" return [ SlackGetChannel(), SlackGetMessage(), SlackScheduleMessage(), SlackSendMessage(), ]
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~chat_loaders~imessage.py
from __future__ import annotations from datetime import datetime from pathlib import Path from typing import TYPE_CHECKING, Iterator, List, Optional, Union from libs.core.langchain_core.chat_sessions import ChatSession from libs.core.langchain_core.messages import HumanMessage from langchain_community.chat_loaders.base import BaseChatLoader if TYPE_CHECKING: import sqlite3 def nanoseconds_from_2001_to_datetime(nanoseconds: int) -> datetime: # Convert nanoseconds to seconds (1 second = 1e9 nanoseconds) timestamp_in_seconds = nanoseconds / 1e9 # The reference date is January 1, 2001, in Unix time reference_date_seconds = datetime(2001, 1, 1).timestamp() # Calculate the actual timestamp by adding the reference date actual_timestamp = reference_date_seconds + timestamp_in_seconds # Convert to a datetime object return datetime.fromtimestamp(actual_timestamp) class IMessageChatLoader(BaseChatLoader): """Load chat sessions from the `iMessage` chat.db SQLite file. It only works on macOS when you have iMessage enabled and have the chat.db file. The chat.db file is likely located at ~/Library/Messages/chat.db. However, your terminal may not have permission to access this file. To resolve this, you can copy the file to a different location, change the permissions of the file, or grant full disk access for your terminal emulator in System Settings > Security and Privacy > Full Disk Access. """ def __init__(self, path: Optional[Union[str, Path]] = None): """ Initialize the IMessageChatLoader. Args: path (str or Path, optional): Path to the chat.db SQLite file. Defaults to None, in which case the default path ~/Library/Messages/chat.db will be used. """ if path is None: path = Path.home() / "Library" / "Messages" / "chat.db" self.db_path = path if isinstance(path, Path) else Path(path) if not self.db_path.exists(): raise FileNotFoundError(f"File {self.db_path} not found") try: import sqlite3 # noqa: F401 except ImportError as e: raise ImportError( "The sqlite3 module is required to load iMessage chats.\n" "Please install it with `pip install pysqlite3`" ) from e def _parse_attributedBody(self, attributedBody: bytes) -> str: """ Parse the attributedBody field of the message table for the text content of the message. The attributedBody field is a binary blob that contains the message content after the byte string b"NSString": 5 bytes 1-3 bytes `len` bytes ... | b"NSString" | preamble | `len` | contents | ... The 5 preamble bytes are always b"\x01\x94\x84\x01+" The size of `len` is either 1 byte or 3 bytes: - If the first byte in `len` is b"\x81" then `len` is 3 bytes long. So the message length is the 2 bytes after, in little Endian. - Otherwise, the size of `len` is 1 byte, and the message length is that byte. Args: attributedBody (bytes): attributedBody field of the message table. Return: str: Text content of the message. """ content = attributedBody.split(b"NSString")[1][5:] length, start = content[0], 1 if content[0] == 129: length, start = int.from_bytes(content[1:3], "little"), 3 return content[start : start + length].decode("utf-8", errors="ignore") def _load_single_chat_session( self, cursor: "sqlite3.Cursor", chat_id: int ) -> ChatSession: """ Load a single chat session from the iMessage chat.db. Args: cursor: SQLite cursor object. chat_id (int): ID of the chat session to load. Returns: ChatSession: Loaded chat session. """ results: List[HumanMessage] = [] query = """ SELECT message.date, handle.id, message.text, message.is_from_me, message.attributedBody FROM message JOIN chat_message_join ON message.ROWID = chat_message_join.message_id JOIN handle ON message.handle_id = handle.ROWID WHERE chat_message_join.chat_id = ? ORDER BY message.date ASC; """ # noqa: E501 cursor.execute(query, (chat_id,)) messages = cursor.fetchall() for date, sender, text, is_from_me, attributedBody in messages: if text: content = text elif attributedBody: content = self._parse_attributedBody(attributedBody) else: # Skip messages with no content continue results.append( HumanMessage( role=sender, content=content, additional_kwargs={ "message_time": date, "message_time_as_datetime": nanoseconds_from_2001_to_datetime( date ), "sender": sender, "is_from_me": bool(is_from_me), }, ) ) return ChatSession(messages=results) def lazy_load(self) -> Iterator[ChatSession]: """ Lazy load the chat sessions from the iMessage chat.db and yield them in the required format. Yields: ChatSession: Loaded chat session. """ import sqlite3 try: conn = sqlite3.connect(self.db_path) except sqlite3.OperationalError as e: raise ValueError( f"Could not open iMessage DB file {self.db_path}.\n" "Make sure your terminal emulator has disk access to this file.\n" " You can either copy the DB file to an accessible location" " or grant full disk access for your terminal emulator." " You can grant full disk access for your terminal emulator" " in System Settings > Security and Privacy > Full Disk Access." ) from e cursor = conn.cursor() # Fetch the list of chat IDs sorted by time (most recent first) query = """SELECT chat_id FROM message JOIN chat_message_join ON message.ROWID = chat_message_join.message_id GROUP BY chat_id ORDER BY MAX(date) DESC;""" cursor.execute(query) chat_ids = [row[0] for row in cursor.fetchall()] for chat_id in chat_ids: yield self._load_single_chat_session(cursor, chat_id) conn.close()
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~document_loaders~readthedocs.py
from __future__ import annotations from pathlib import Path from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Sequence, Tuple, Union from libs.core.langchain_core.documents import Document from langchain_community.document_loaders.base import BaseLoader if TYPE_CHECKING: from bs4 import NavigableString from bs4.element import Comment, Tag class ReadTheDocsLoader(BaseLoader): """Load `ReadTheDocs` documentation directory.""" def __init__( self, path: Union[str, Path], encoding: Optional[str] = None, errors: Optional[str] = None, custom_html_tag: Optional[Tuple[str, dict]] = None, patterns: Sequence[str] = ("*.htm", "*.html"), exclude_links_ratio: float = 1.0, **kwargs: Optional[Any], ): """ Initialize ReadTheDocsLoader The loader loops over all files under `path` and extracts the actual content of the files by retrieving main html tags. Default main html tags include `<main id="main-content>`, <`div role="main>`, and `<article role="main">`. You can also define your own html tags by passing custom_html_tag, e.g. `("div", "class=main")`. The loader iterates html tags with the order of custom html tags (if exists) and default html tags. If any of the tags is not empty, the loop will break and retrieve the content out of that tag. Args: path: The location of pulled readthedocs folder. encoding: The encoding with which to open the documents. errors: Specify how encoding and decoding errors are to be handled—this cannot be used in binary mode. custom_html_tag: Optional custom html tag to retrieve the content from files. patterns: The file patterns to load, passed to `glob.rglob`. exclude_links_ratio: The ratio of links:content to exclude pages from. This is to reduce the frequency at which index pages make their way into retrieved results. Recommended: 0.5 kwargs: named arguments passed to `bs4.BeautifulSoup`. """ try: from bs4 import BeautifulSoup except ImportError: raise ImportError( "Could not import python packages. " "Please install it with `pip install beautifulsoup4`. " ) try: _ = BeautifulSoup( "<html><body>Parser builder library test.</body></html>", "html.parser", **kwargs, ) except Exception as e: raise ValueError("Parsing kwargs do not appear valid") from e self.file_path = Path(path) self.encoding = encoding self.errors = errors self.custom_html_tag = custom_html_tag self.patterns = patterns self.bs_kwargs = kwargs self.exclude_links_ratio = exclude_links_ratio def lazy_load(self) -> Iterator[Document]: """A lazy loader for Documents.""" for file_pattern in self.patterns: for p in self.file_path.rglob(file_pattern): if p.is_dir(): continue with open(p, encoding=self.encoding, errors=self.errors) as f: text = self._clean_data(f.read()) yield Document(page_content=text, metadata={"source": str(p)}) def load(self) -> List[Document]: """Load documents.""" return list(self.lazy_load()) def _clean_data(self, data: str) -> str: from bs4 import BeautifulSoup soup = BeautifulSoup(data, "html.parser", **self.bs_kwargs) # default tags html_tags = [ ("div", {"role": "main"}), ("main", {"id": "main-content"}), ] if self.custom_html_tag is not None: html_tags.append(self.custom_html_tag) element = None # reversed order. check the custom one first for tag, attrs in html_tags[::-1]: element = soup.find(tag, attrs) # if found, break if element is not None: break if element is not None and _get_link_ratio(element) <= self.exclude_links_ratio: text = _get_clean_text(element) else: text = "" # trim empty lines return "\n".join([t for t in text.split("\n") if t]) def _get_clean_text(element: Tag) -> str: """Returns cleaned text with newlines preserved and irrelevant elements removed.""" elements_to_skip = [ "script", "noscript", "canvas", "meta", "svg", "map", "area", "audio", "source", "track", "video", "embed", "object", "param", "picture", "iframe", "frame", "frameset", "noframes", "applet", "form", "button", "select", "base", "style", "img", ] newline_elements = [ "p", "div", "ul", "ol", "li", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "table", "tr", ] text = _process_element(element, elements_to_skip, newline_elements) return text.strip() def _get_link_ratio(section: Tag) -> float: links = section.find_all("a") total_text = "".join(str(s) for s in section.stripped_strings) if len(total_text) == 0: return 0 link_text = "".join( str(string.string.strip()) for link in links for string in link.strings if string ) return len(link_text) / len(total_text) def _process_element( element: Union[Tag, NavigableString, Comment], elements_to_skip: List[str], newline_elements: List[str], ) -> str: """ Traverse through HTML tree recursively to preserve newline and skip unwanted (code/binary) elements """ from bs4 import NavigableString from bs4.element import Comment, Tag tag_name = getattr(element, "name", None) if isinstance(element, Comment) or tag_name in elements_to_skip: return "" elif isinstance(element, NavigableString): return element elif tag_name == "br": return "\n" elif tag_name in newline_elements: return ( "".join( _process_element(child, elements_to_skip, newline_elements) for child in element.children if isinstance(child, (Tag, NavigableString, Comment)) ) + "\n" ) else: return "".join( _process_element(child, elements_to_skip, newline_elements) for child in element.children if isinstance(child, (Tag, NavigableString, Comment)) )
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~chat_loaders~langsmith.py
from __future__ import annotations import logging from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, Optional, Union, cast from libs.core.langchain_core.chat_sessions import ChatSession from libs.core.langchain_core.load import load from langchain_community.chat_loaders.base import BaseChatLoader if TYPE_CHECKING: from langsmith.client import Client from langsmith.schemas import Run logger = logging.getLogger(__name__) class LangSmithRunChatLoader(BaseChatLoader): """ Load chat sessions from a list of LangSmith "llm" runs. Attributes: runs (Iterable[Union[str, Run]]): The list of LLM run IDs or run objects. client (Client): Instance of LangSmith client for fetching data. """ def __init__( self, runs: Iterable[Union[str, Run]], client: Optional["Client"] = None ): """ Initialize a new LangSmithRunChatLoader instance. :param runs: List of LLM run IDs or run objects. :param client: An instance of LangSmith client, if not provided, a new client instance will be created. """ from langsmith.client import Client self.runs = runs self.client = client or Client() def _load_single_chat_session(self, llm_run: "Run") -> ChatSession: """ Convert an individual LangSmith LLM run to a ChatSession. :param llm_run: The LLM run object. :return: A chat session representing the run's data. """ chat_session = LangSmithRunChatLoader._get_messages_from_llm_run(llm_run) functions = LangSmithRunChatLoader._get_functions_from_llm_run(llm_run) if functions: chat_session["functions"] = functions return chat_session @staticmethod def _get_messages_from_llm_run(llm_run: "Run") -> ChatSession: """ Extract messages from a LangSmith LLM run. :param llm_run: The LLM run object. :return: ChatSession with the extracted messages. """ if llm_run.run_type != "llm": raise ValueError(f"Expected run of type llm. Got: {llm_run.run_type}") if "messages" not in llm_run.inputs: raise ValueError(f"Run has no 'messages' inputs. Got {llm_run.inputs}") if not llm_run.outputs: raise ValueError("Cannot convert pending run") messages = load(llm_run.inputs)["messages"] message_chunk = load(llm_run.outputs)["generations"][0]["message"] return ChatSession(messages=messages + [message_chunk]) @staticmethod def _get_functions_from_llm_run(llm_run: "Run") -> Optional[List[Dict]]: """ Extract functions from a LangSmith LLM run if they exist. :param llm_run: The LLM run object. :return: Functions from the run or None. """ if llm_run.run_type != "llm": raise ValueError(f"Expected run of type llm. Got: {llm_run.run_type}") return (llm_run.extra or {}).get("invocation_params", {}).get("functions") def lazy_load(self) -> Iterator[ChatSession]: """ Lazy load the chat sessions from the iterable of run IDs. This method fetches the runs and converts them to chat sessions on-the-fly, yielding one session at a time. :return: Iterator of chat sessions containing messages. """ from langsmith.schemas import Run for run_obj in self.runs: try: if hasattr(run_obj, "id"): run = run_obj else: run = self.client.read_run(run_obj) session = self._load_single_chat_session(cast(Run, run)) yield session except ValueError as e: logger.warning(f"Could not load run {run_obj}: {repr(e)}") continue class LangSmithDatasetChatLoader(BaseChatLoader): """ Load chat sessions from a LangSmith dataset with the "chat" data type. Attributes: dataset_name (str): The name of the LangSmith dataset. client (Client): Instance of LangSmith client for fetching data. """ def __init__(self, *, dataset_name: str, client: Optional["Client"] = None): """ Initialize a new LangSmithChatDatasetLoader instance. :param dataset_name: The name of the LangSmith dataset. :param client: An instance of LangSmith client; if not provided, a new client instance will be created. """ try: from langsmith.client import Client except ImportError as e: raise ImportError( "The LangSmith client is required to load LangSmith datasets.\n" "Please install it with `pip install langsmith`" ) from e self.dataset_name = dataset_name self.client = client or Client() def lazy_load(self) -> Iterator[ChatSession]: """ Lazy load the chat sessions from the specified LangSmith dataset. This method fetches the chat data from the dataset and converts each data point to chat sessions on-the-fly, yielding one session at a time. :return: Iterator of chat sessions containing messages. """ from langchain_community.adapters import openai as oai_adapter # noqa: E402 data = self.client.read_dataset_openai_finetuning( dataset_name=self.dataset_name ) for data_point in data: yield ChatSession( messages=[ oai_adapter.convert_dict_to_message(m) for m in data_point.get("messages", []) ], functions=data_point.get("functions"), )
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~vectorstores~llm_rails.py
"""Wrapper around LLMRails vector database.""" from __future__ import annotations import json import logging import os import uuid from typing import Any, Iterable, List, Optional, Tuple import requests from libs.core.langchain_core.documents import Document from libs.core.langchain_core.embeddings import Embeddings from libs.core.langchain_core.pydantic_v1 import Field from libs.core.langchain_core.vectorstores import VectorStore, VectorStoreRetriever class LLMRails(VectorStore): """Implementation of Vector Store using LLMRails. See https://llmrails.com/ Example: .. code-block:: python from langchain_community.vectorstores import LLMRails vectorstore = LLMRails( api_key=llm_rails_api_key, datastore_id=datastore_id ) """ def __init__( self, datastore_id: Optional[str] = None, api_key: Optional[str] = None, ): """Initialize with LLMRails API.""" self._datastore_id = datastore_id or os.environ.get("LLM_RAILS_DATASTORE_ID") self._api_key = api_key or os.environ.get("LLM_RAILS_API_KEY") if self._api_key is None: logging.warning("Can't find Rails credentials in environment.") self._session = requests.Session() # to reuse connections self.datastore_id = datastore_id self.base_url = "https://api.llmrails.com/v1" def _get_post_headers(self) -> dict: """Returns headers that should be attached to each post request.""" return {"X-API-KEY": self._api_key} def add_texts( self, texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. Returns: List of ids from adding the texts into the vectorstore. """ names: List[str] = [] for text in texts: doc_name = str(uuid.uuid4()) response = self._session.post( f"{self.base_url}/datastores/{self._datastore_id}/text", json={"name": doc_name, "text": text}, verify=True, headers=self._get_post_headers(), ) if response.status_code != 200: logging.error( f"Create request failed for doc_name = {doc_name} with status code " f"{response.status_code}, reason {response.reason}, text " f"{response.text}" ) return names names.append(doc_name) return names def add_files( self, files_list: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> bool: """ LLMRails provides a way to add documents directly via our API where pre-processing and chunking occurs internally in an optimal way This method provides a way to use that API in LangChain Args: files_list: Iterable of strings, each representing a local file path. Files could be text, HTML, PDF, markdown, doc/docx, ppt/pptx, etc. see API docs for full list Returns: List of ids associated with each of the files indexed """ files = [] for file in files_list: if not os.path.exists(file): logging.error(f"File {file} does not exist, skipping") continue files.append(("file", (os.path.basename(file), open(file, "rb")))) response = self._session.post( f"{self.base_url}/datastores/{self._datastore_id}/file", files=files, verify=True, headers=self._get_post_headers(), ) if response.status_code != 200: logging.error( f"Create request failed for datastore = {self._datastore_id} " f"with status code {response.status_code}, reason {response.reason}, " f"text {response.text}" ) return False return True def similarity_search_with_score( self, query: str, k: int = 5 ) -> List[Tuple[Document, float]]: """Return LLMRails documents most similar to query, along with scores. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 5 Max 10. alpha: parameter for hybrid search . Returns: List of Documents most similar to the query and score for each. """ response = self._session.post( headers=self._get_post_headers(), url=f"{self.base_url}/datastores/{self._datastore_id}/search", data=json.dumps({"k": k, "text": query}), timeout=10, ) if response.status_code != 200: logging.error( "Query failed %s", f"(code {response.status_code}, reason {response.reason}, details " f"{response.text})", ) return [] results = response.json()["results"] docs = [ ( Document( page_content=x["text"], metadata={ key: value for key, value in x["metadata"].items() if key != "score" }, ), x["metadata"]["score"], ) for x in results ] return docs def similarity_search( self, query: str, k: int = 4, **kwargs: Any ) -> List[Document]: """Return LLMRails documents most similar to query, along with scores. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 5. Returns: List of Documents most similar to the query """ docs_and_scores = self.similarity_search_with_score(query, k=k) return [doc for doc, _ in docs_and_scores] @classmethod def from_texts( cls, texts: List[str], embedding: Optional[Embeddings] = None, metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> LLMRails: """Construct LLMRails wrapper from raw documents. This is intended to be a quick way to get started. Example: .. code-block:: python from langchain_community.vectorstores import LLMRails llm_rails = LLMRails.from_texts( texts, datastore_id=datastore_id, api_key=llm_rails_api_key ) """ # Note: LLMRails generates its own embeddings, so we ignore the provided # embeddings (required by interface) llm_rails = cls(**kwargs) llm_rails.add_texts(texts) return llm_rails def as_retriever(self, **kwargs: Any) -> LLMRailsRetriever: return LLMRailsRetriever(vectorstore=self, **kwargs) class LLMRailsRetriever(VectorStoreRetriever): """Retriever for LLMRails.""" vectorstore: LLMRails search_kwargs: dict = Field(default_factory=lambda: {"k": 5}) """Search params. k: Number of Documents to return. Defaults to 5. alpha: parameter for hybrid search . """ def add_texts(self, texts: List[str]) -> None: """Add text to the datastore. Args: texts (List[str]): The text """ self.vectorstore.add_texts(texts)
[]
2024-01-10
mth93/langchain
libs~langchain~langchain~chains~openai_functions~qa_with_structure.py
from typing import Any, List, Optional, Type, Union from libs.core.langchain_core.language_models import BaseLanguageModel from libs.core.langchain_core.messages import HumanMessage, SystemMessage from libs.core.langchain_core.output_parsers import BaseLLMOutputParser from libs.core.langchain_core.prompts import PromptTemplate from libs.core.langchain_core.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate from libs.core.langchain_core.pydantic_v1 import BaseModel, Field from langchain.chains.llm import LLMChain from langchain.chains.openai_functions.utils import get_llm_kwargs from langchain.output_parsers.openai_functions import ( OutputFunctionsParser, PydanticOutputFunctionsParser, ) class AnswerWithSources(BaseModel): """An answer to the question, with sources.""" answer: str = Field(..., description="Answer to the question that was asked") sources: List[str] = Field( ..., description="List of sources used to answer the question" ) def create_qa_with_structure_chain( llm: BaseLanguageModel, schema: Union[dict, Type[BaseModel]], output_parser: str = "base", prompt: Optional[Union[PromptTemplate, ChatPromptTemplate]] = None, verbose: bool = False, ) -> LLMChain: """Create a question answering chain that returns an answer with sources based on schema. Args: llm: Language model to use for the chain. schema: Pydantic schema to use for the output. output_parser: Output parser to use. Should be one of `pydantic` or `base`. Default to `base`. prompt: Optional prompt to use for the chain. Returns: """ if output_parser == "pydantic": if not (isinstance(schema, type) and issubclass(schema, BaseModel)): raise ValueError( "Must provide a pydantic class for schema when output_parser is " "'pydantic'." ) _output_parser: BaseLLMOutputParser = PydanticOutputFunctionsParser( pydantic_schema=schema ) elif output_parser == "base": _output_parser = OutputFunctionsParser() else: raise ValueError( f"Got unexpected output_parser: {output_parser}. " f"Should be one of `pydantic` or `base`." ) if isinstance(schema, type) and issubclass(schema, BaseModel): schema_dict = schema.schema() else: schema_dict = schema function = { "name": schema_dict["title"], "description": schema_dict["description"], "parameters": schema_dict, } llm_kwargs = get_llm_kwargs(function) messages = [ SystemMessage( content=( "You are a world class algorithm to answer " "questions in a specific format." ) ), HumanMessage(content="Answer question using the following context"), HumanMessagePromptTemplate.from_template("{context}"), HumanMessagePromptTemplate.from_template("Question: {question}"), HumanMessage(content="Tips: Make sure to answer in the correct format"), ] prompt = prompt or ChatPromptTemplate(messages=messages) chain = LLMChain( llm=llm, prompt=prompt, llm_kwargs=llm_kwargs, output_parser=_output_parser, verbose=verbose, ) return chain def create_qa_with_sources_chain( llm: BaseLanguageModel, verbose: bool = False, **kwargs: Any ) -> LLMChain: """Create a question answering chain that returns an answer with sources. Args: llm: Language model to use for the chain. verbose: Whether to print the details of the chain **kwargs: Keyword arguments to pass to `create_qa_with_structure_chain`. Returns: Chain (LLMChain) that can be used to answer questions with citations. """ return create_qa_with_structure_chain( llm, AnswerWithSources, verbose=verbose, **kwargs )
[ "{context}", "You are a world class algorithm to answer questions in a specific format.", "Tips: Make sure to answer in the correct format", "Question: {question}", "Answer question using the following context" ]
2024-01-10
mth93/langchain
libs~community~langchain_community~document_loaders~snowflake_loader.py
from __future__ import annotations from typing import Any, Dict, Iterator, List, Optional, Tuple from libs.core.langchain_core.documents import Document from langchain_community.document_loaders.base import BaseLoader class SnowflakeLoader(BaseLoader): """Load from `Snowflake` API. Each document represents one row of the result. The `page_content_columns` are written into the `page_content` of the document. The `metadata_columns` are written into the `metadata` of the document. By default, all columns are written into the `page_content` and none into the `metadata`. """ def __init__( self, query: str, user: str, password: str, account: str, warehouse: str, role: str, database: str, schema: str, parameters: Optional[Dict[str, Any]] = None, page_content_columns: Optional[List[str]] = None, metadata_columns: Optional[List[str]] = None, ): """Initialize Snowflake document loader. Args: query: The query to run in Snowflake. user: Snowflake user. password: Snowflake password. account: Snowflake account. warehouse: Snowflake warehouse. role: Snowflake role. database: Snowflake database schema: Snowflake schema parameters: Optional. Parameters to pass to the query. page_content_columns: Optional. Columns written to Document `page_content`. metadata_columns: Optional. Columns written to Document `metadata`. """ self.query = query self.user = user self.password = password self.account = account self.warehouse = warehouse self.role = role self.database = database self.schema = schema self.parameters = parameters self.page_content_columns = ( page_content_columns if page_content_columns is not None else ["*"] ) self.metadata_columns = metadata_columns if metadata_columns is not None else [] def _execute_query(self) -> List[Dict[str, Any]]: try: import snowflake.connector except ImportError as ex: raise ImportError( "Could not import snowflake-connector-python package. " "Please install it with `pip install snowflake-connector-python`." ) from ex conn = snowflake.connector.connect( user=self.user, password=self.password, account=self.account, warehouse=self.warehouse, role=self.role, database=self.database, schema=self.schema, parameters=self.parameters, ) try: cur = conn.cursor() cur.execute("USE DATABASE " + self.database) cur.execute("USE SCHEMA " + self.schema) cur.execute(self.query, self.parameters) query_result = cur.fetchall() column_names = [column[0] for column in cur.description] query_result = [dict(zip(column_names, row)) for row in query_result] except Exception as e: print(f"An error occurred: {e}") query_result = [] finally: cur.close() return query_result def _get_columns( self, query_result: List[Dict[str, Any]] ) -> Tuple[List[str], List[str]]: page_content_columns = ( self.page_content_columns if self.page_content_columns else [] ) metadata_columns = self.metadata_columns if self.metadata_columns else [] if page_content_columns is None and query_result: page_content_columns = list(query_result[0].keys()) if metadata_columns is None: metadata_columns = [] return page_content_columns or [], metadata_columns def lazy_load(self) -> Iterator[Document]: query_result = self._execute_query() if isinstance(query_result, Exception): print(f"An error occurred during the query: {query_result}") return [] page_content_columns, metadata_columns = self._get_columns(query_result) if "*" in page_content_columns: page_content_columns = list(query_result[0].keys()) for row in query_result: page_content = "\n".join( f"{k}: {v}" for k, v in row.items() if k in page_content_columns ) metadata = {k: v for k, v in row.items() if k in metadata_columns} doc = Document(page_content=page_content, metadata=metadata) yield doc def load(self) -> List[Document]: """Load data into document objects.""" return list(self.lazy_load())
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~embeddings~gradient_ai.py
from typing import Any, Dict, List, Optional from libs.core.langchain_core.embeddings import Embeddings from libs.core.langchain_core.pydantic_v1 import BaseModel, Extra, root_validator from libs.core.langchain_core.utils import get_from_dict_or_env from packaging.version import parse __all__ = ["GradientEmbeddings"] class GradientEmbeddings(BaseModel, Embeddings): """Gradient.ai Embedding models. GradientLLM is a class to interact with Embedding Models on gradient.ai To use, set the environment variable ``GRADIENT_ACCESS_TOKEN`` with your API token and ``GRADIENT_WORKSPACE_ID`` for your gradient workspace, or alternatively provide them as keywords to the constructor of this class. Example: .. code-block:: python from langchain_community.embeddings import GradientEmbeddings GradientEmbeddings( model="bge-large", gradient_workspace_id="12345614fc0_workspace", gradient_access_token="gradientai-access_token", ) """ model: str "Underlying gradient.ai model id." gradient_workspace_id: Optional[str] = None "Underlying gradient.ai workspace_id." gradient_access_token: Optional[str] = None """gradient.ai API Token, which can be generated by going to https://auth.gradient.ai/select-workspace and selecting "Access tokens" under the profile drop-down. """ gradient_api_url: str = "https://api.gradient.ai/api" """Endpoint URL to use.""" query_prompt_for_retrieval: Optional[str] = None """Query pre-prompt""" client: Any = None #: :meta private: """Gradient client.""" # LLM call kwargs class Config: """Configuration for this pydantic object.""" extra = Extra.forbid @root_validator(allow_reuse=True) def validate_environment(cls, values: Dict) -> Dict: """Validate that api key and python package exists in environment.""" values["gradient_access_token"] = get_from_dict_or_env( values, "gradient_access_token", "GRADIENT_ACCESS_TOKEN" ) values["gradient_workspace_id"] = get_from_dict_or_env( values, "gradient_workspace_id", "GRADIENT_WORKSPACE_ID" ) values["gradient_api_url"] = get_from_dict_or_env( values, "gradient_api_url", "GRADIENT_API_URL" ) try: import gradientai except ImportError: raise ImportError( 'GradientEmbeddings requires `pip install -U "gradientai>=1.4.0"`.' ) if parse(gradientai.__version__) < parse("1.4.0"): raise ImportError( 'GradientEmbeddings requires `pip install -U "gradientai>=1.4.0"`.' ) gradient = gradientai.Gradient( access_token=values["gradient_access_token"], workspace_id=values["gradient_workspace_id"], host=values["gradient_api_url"], ) values["client"] = gradient.get_embeddings_model(slug=values["model"]) return values def embed_documents(self, texts: List[str]) -> List[List[float]]: """Call out to Gradient's embedding endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ inputs = [{"input": text} for text in texts] result = self.client.embed(inputs=inputs).embeddings return [e.embedding for e in result] async def aembed_documents(self, texts: List[str]) -> List[List[float]]: """Async call out to Gradient's embedding endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ inputs = [{"input": text} for text in texts] result = (await self.client.aembed(inputs=inputs)).embeddings return [e.embedding for e in result] def embed_query(self, text: str) -> List[float]: """Call out to Gradient's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """ query = ( f"{self.query_prompt_for_retrieval} {text}" if self.query_prompt_for_retrieval else text ) return self.embed_documents([query])[0] async def aembed_query(self, text: str) -> List[float]: """Async call out to Gradient's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """ query = ( f"{self.query_prompt_for_retrieval} {text}" if self.query_prompt_for_retrieval else text ) embeddings = await self.aembed_documents([query]) return embeddings[0] class TinyAsyncGradientEmbeddingClient: #: :meta private: """Deprecated, TinyAsyncGradientEmbeddingClient was removed. This class is just for backwards compatibility with older versions of langchain_community. It might be entirely removed in the future. """ def __init__(self, *args, **kwargs) -> None: raise ValueError("Deprecated,TinyAsyncGradientEmbeddingClient was removed.")
[]
2024-01-10
mth93/langchain
libs~core~langchain_core~runnables~history.py
from __future__ import annotations import inspect from typing import ( TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Type, Union, ) from libs.core.langchain_core.chat_history import BaseChatMessageHistory from libs.core.langchain_core.load import load from libs.core.langchain_core.pydantic_v1 import BaseModel, create_model from libs.core.langchain_core.runnables.base import Runnable, RunnableBindingBase, RunnableLambda from libs.core.langchain_core.runnables.config import run_in_executor from libs.core.langchain_core.runnables.passthrough import RunnablePassthrough from libs.core.langchain_core.runnables.utils import ( ConfigurableFieldSpec, get_unique_config_specs, ) if TYPE_CHECKING: from libs.core.langchain_core.messages import BaseMessage from libs.core.langchain_core.runnables.config import RunnableConfig from libs.core.langchain_core.tracers.schemas import Run MessagesOrDictWithMessages = Union[Sequence["BaseMessage"], Dict[str, Any]] GetSessionHistoryCallable = Callable[..., BaseChatMessageHistory] class RunnableWithMessageHistory(RunnableBindingBase): """A runnable that manages chat message history for another runnable. Base runnable must have inputs and outputs that can be converted to a list of BaseMessages. RunnableWithMessageHistory must always be called with a config that contains session_id, e.g. ``{"configurable": {"session_id": "<SESSION_ID>"}}`. Example (dict input): .. code-block:: python from typing import Optional from langchain.chat_models import ChatAnthropic from langchain.memory.chat_message_histories import RedisChatMessageHistory from libs.core.langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from libs.core.langchain_core.runnables.history import RunnableWithMessageHistory prompt = ChatPromptTemplate.from_messages([ ("system", "You're an assistant who's good at {ability}"), MessagesPlaceholder(variable_name="history"), ("human", "{question}"), ]) chain = prompt | ChatAnthropic(model="claude-2") chain_with_history = RunnableWithMessageHistory( chain, RedisChatMessageHistory, input_messages_key="question", history_messages_key="history", ) chain_with_history.invoke( {"ability": "math", "question": "What does cosine mean?"}, config={"configurable": {"session_id": "foo"}} ) # -> "Cosine is ..." chain_with_history.invoke( {"ability": "math", "question": "What's its inverse"}, config={"configurable": {"session_id": "foo"}} ) # -> "The inverse of cosine is called arccosine ..." Example (get_session_history takes two keys, user_id and conversation id): .. code-block:: python store = {} def get_session_history( user_id: str, conversation_id: str ) -> ChatMessageHistory: if (user_id, conversation_id) not in store: store[(user_id, conversation_id)] = ChatMessageHistory() return store[(user_id, conversation_id)] prompt = ChatPromptTemplate.from_messages([ ("system", "You're an assistant who's good at {ability}"), MessagesPlaceholder(variable_name="history"), ("human", "{question}"), ]) chain = prompt | ChatAnthropic(model="claude-2") with_message_history = RunnableWithMessageHistory( chain, get_session_history=get_session_history, input_messages_key="messages", history_messages_key="history", history_factory_config=[ ConfigurableFieldSpec( id="user_id", annotation=str, name="User ID", description="Unique identifier for the user.", default="", is_shared=True, ), ConfigurableFieldSpec( id="conversation_id", annotation=str, name="Conversation ID", description="Unique identifier for the conversation.", default="", is_shared=True, ), ], ) chain_with_history.invoke( {"ability": "math", "question": "What does cosine mean?"}, config={"configurable": {"user_id": "123", "conversation_id": "1"}} ) """ # noqa: E501 get_session_history: GetSessionHistoryCallable input_messages_key: Optional[str] = None output_messages_key: Optional[str] = None history_messages_key: Optional[str] = None history_factory_config: Sequence[ConfigurableFieldSpec] @classmethod def get_lc_namespace(cls) -> List[str]: """Get the namespace of the langchain object.""" return ["langchain", "schema", "runnable"] def __init__( self, runnable: Runnable[ MessagesOrDictWithMessages, Union[str, BaseMessage, MessagesOrDictWithMessages], ], get_session_history: GetSessionHistoryCallable, *, input_messages_key: Optional[str] = None, output_messages_key: Optional[str] = None, history_messages_key: Optional[str] = None, history_factory_config: Optional[Sequence[ConfigurableFieldSpec]] = None, **kwargs: Any, ) -> None: """Initialize RunnableWithMessageHistory. Args: runnable: The base Runnable to be wrapped. Must take as input one of: 1. A sequence of BaseMessages 2. A dict with one key for all messages 3. A dict with one key for the current input string/message(s) and a separate key for historical messages. If the input key points to a string, it will be treated as a HumanMessage in history. Must return as output one of: 1. A string which can be treated as an AIMessage 2. A BaseMessage or sequence of BaseMessages 3. A dict with a key for a BaseMessage or sequence of BaseMessages get_session_history: Function that returns a new BaseChatMessageHistory. This function should either take a single positional argument `session_id` of type string and return a corresponding chat message history instance. .. code-block:: python def get_session_history( session_id: str, *, user_id: Optional[str]=None ) -> BaseChatMessageHistory: ... Or it should take keyword arguments that match the keys of `session_history_config_specs` and return a corresponding chat message history instance. .. code-block:: python def get_session_history( *, user_id: str, thread_id: str, ) -> BaseChatMessageHistory: ... input_messages_key: Must be specified if the base runnable accepts a dict as input. output_messages_key: Must be specified if the base runnable returns a dict as output. history_messages_key: Must be specified if the base runnable accepts a dict as input and expects a separate key for historical messages. history_factory_config: Configure fields that should be passed to the chat history factory. See ``ConfigurableFieldSpec`` for more details. Specifying these allows you to pass multiple config keys into the get_session_history factory. **kwargs: Arbitrary additional kwargs to pass to parent class ``RunnableBindingBase`` init. """ # noqa: E501 history_chain: Runnable = RunnableLambda( self._enter_history, self._aenter_history ).with_config(run_name="load_history") messages_key = history_messages_key or input_messages_key if messages_key: history_chain = RunnablePassthrough.assign( **{messages_key: history_chain} ).with_config(run_name="insert_history") bound = ( history_chain | runnable.with_listeners(on_end=self._exit_history) ).with_config(run_name="RunnableWithMessageHistory") if history_factory_config: _config_specs = history_factory_config else: # If not provided, then we'll use the default session_id field _config_specs = [ ConfigurableFieldSpec( id="session_id", annotation=str, name="Session ID", description="Unique identifier for a session.", default="", is_shared=True, ), ] super().__init__( get_session_history=get_session_history, input_messages_key=input_messages_key, output_messages_key=output_messages_key, bound=bound, history_messages_key=history_messages_key, history_factory_config=_config_specs, **kwargs, ) @property def config_specs(self) -> List[ConfigurableFieldSpec]: return get_unique_config_specs( super().config_specs + list(self.history_factory_config) ) def get_input_schema( self, config: Optional[RunnableConfig] = None ) -> Type[BaseModel]: super_schema = super().get_input_schema(config) if super_schema.__custom_root_type__ is not None: from libs.core.langchain_core.messages import BaseMessage fields: Dict = {} if self.input_messages_key and self.history_messages_key: fields[self.input_messages_key] = ( Union[str, BaseMessage, Sequence[BaseMessage]], ..., ) elif self.input_messages_key: fields[self.input_messages_key] = (Sequence[BaseMessage], ...) else: fields["__root__"] = (Sequence[BaseMessage], ...) return create_model( # type: ignore[call-overload] "RunnableWithChatHistoryInput", **fields, ) else: return super_schema def _get_input_messages( self, input_val: Union[str, BaseMessage, Sequence[BaseMessage]] ) -> List[BaseMessage]: from libs.core.langchain_core.messages import BaseMessage if isinstance(input_val, str): from libs.core.langchain_core.messages import HumanMessage return [HumanMessage(content=input_val)] elif isinstance(input_val, BaseMessage): return [input_val] elif isinstance(input_val, (list, tuple)): return list(input_val) else: raise ValueError( f"Expected str, BaseMessage, List[BaseMessage], or Tuple[BaseMessage]. " f"Got {input_val}." ) def _get_output_messages( self, output_val: Union[str, BaseMessage, Sequence[BaseMessage], dict] ) -> List[BaseMessage]: from libs.core.langchain_core.messages import BaseMessage if isinstance(output_val, dict): output_val = output_val[self.output_messages_key or "output"] if isinstance(output_val, str): from libs.core.langchain_core.messages import AIMessage return [AIMessage(content=output_val)] elif isinstance(output_val, BaseMessage): return [output_val] elif isinstance(output_val, (list, tuple)): return list(output_val) else: raise ValueError() def _enter_history(self, input: Any, config: RunnableConfig) -> List[BaseMessage]: hist = config["configurable"]["message_history"] # return only historic messages if self.history_messages_key: return hist.messages.copy() # return all messages else: input_val = ( input if not self.input_messages_key else input[self.input_messages_key] ) return hist.messages.copy() + self._get_input_messages(input_val) async def _aenter_history( self, input: Dict[str, Any], config: RunnableConfig ) -> List[BaseMessage]: return await run_in_executor(config, self._enter_history, input, config) def _exit_history(self, run: Run, config: RunnableConfig) -> None: hist = config["configurable"]["message_history"] # Get the input messages inputs = load(run.inputs) input_val = inputs[self.input_messages_key or "input"] input_messages = self._get_input_messages(input_val) # If historic messages were prepended to the input messages, remove them to # avoid adding duplicate messages to history. if not self.history_messages_key: historic_messages = config["configurable"]["message_history"].messages input_messages = input_messages[len(historic_messages) :] # Get the output messages output_val = load(run.outputs) output_messages = self._get_output_messages(output_val) for m in input_messages + output_messages: hist.add_message(m) def _merge_configs(self, *configs: Optional[RunnableConfig]) -> RunnableConfig: config = super()._merge_configs(*configs) expected_keys = [field_spec.id for field_spec in self.history_factory_config] configurable = config.get("configurable", {}) missing_keys = set(expected_keys) - set(configurable.keys()) if missing_keys: example_input = {self.input_messages_key: "foo"} example_configurable = { missing_key: "[your-value-here]" for missing_key in missing_keys } example_config = {"configurable": example_configurable} raise ValueError( f"Missing keys {sorted(missing_keys)} in config['configurable'] " f"Expected keys are {sorted(expected_keys)}." f"When using via .invoke() or .stream(), pass in a config; " f"e.g., chain.invoke({example_input}, {example_config})" ) parameter_names = _get_parameter_names(self.get_session_history) if len(expected_keys) == 1: # If arity = 1, then invoke function by positional arguments message_history = self.get_session_history(configurable[expected_keys[0]]) else: # otherwise verify that names of keys patch and invoke by named arguments if set(expected_keys) != set(parameter_names): raise ValueError( f"Expected keys {sorted(expected_keys)} do not match parameter " f"names {sorted(parameter_names)} of get_session_history." ) message_history = self.get_session_history( **{key: configurable[key] for key in expected_keys} ) config["configurable"]["message_history"] = message_history return config def _get_parameter_names(callable_: GetSessionHistoryCallable) -> List[str]: """Get the parameter names of the callable.""" sig = inspect.signature(callable_) return list(sig.parameters.keys())
[]
2024-01-10
mth93/langchain
libs~langchain~tests~unit_tests~agents~test_mrkl_output_parser.py
import pytest from libs.core.langchain_core.agents import AgentAction, AgentFinish from libs.core.langchain_core.exceptions import OutputParserException from langchain.agents.mrkl.output_parser import ( MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE, MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE, MRKLOutputParser, ) mrkl_output_parser = MRKLOutputParser() def test_valid_action_and_action_input_parse() -> None: llm_output = """I can use the `foo` tool to achieve the goal. Action: foo Action Input: bar""" agent_action: AgentAction = mrkl_output_parser.parse(llm_output) # type: ignore assert agent_action.tool == "foo" assert agent_action.tool_input == "bar" def test_valid_final_answer_parse() -> None: llm_output = """Final Answer: The best pizza to eat is margaritta """ agent_finish: AgentFinish = mrkl_output_parser.parse(llm_output) # type: ignore assert ( agent_finish.return_values.get("output") == "The best pizza to eat is margaritta" ) def test_missing_action() -> None: llm_output = """I can use the `foo` tool to achieve the goal.""" with pytest.raises(OutputParserException) as exception_info: mrkl_output_parser.parse(llm_output) assert ( exception_info.value.observation == MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE ) def test_missing_action_input() -> None: llm_output = """I can use the `foo` tool to achieve the goal. Action: foo""" with pytest.raises(OutputParserException) as exception_info: mrkl_output_parser.parse(llm_output) assert ( exception_info.value.observation == MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE ) def test_final_answer_before_parsable_action() -> None: llm_output = """Final Answer: The best pizza to eat is margaritta Action: foo Action Input: bar """ agent_finish: AgentFinish = mrkl_output_parser.parse(llm_output) # type: ignore assert ( agent_finish.return_values.get("output") == "The best pizza to eat is margaritta" ) def test_final_answer_after_parsable_action() -> None: llm_output = """ Observation: I can use the `foo` tool to achieve the goal. Action: foo Action Input: bar Final Answer: The best pizza to eat is margaritta """ with pytest.raises(OutputParserException) as exception_info: mrkl_output_parser.parse(llm_output) assert ( "Parsing LLM output produced both a final answer and a parse-able action" in exception_info.value.args[0] )
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~chat_models~anthropic.py
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, cast from libs.core.langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) from libs.core.langchain_core.language_models.chat_models import ( BaseChatModel, agenerate_from_stream, generate_from_stream, ) from libs.core.langchain_core.messages import ( AIMessage, AIMessageChunk, BaseMessage, ChatMessage, HumanMessage, SystemMessage, ) from libs.core.langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult from libs.core.langchain_core.prompt_values import PromptValue from langchain_community.llms.anthropic import _AnthropicCommon def _convert_one_message_to_text( message: BaseMessage, human_prompt: str, ai_prompt: str, ) -> str: content = cast(str, message.content) if isinstance(message, ChatMessage): message_text = f"\n\n{message.role.capitalize()}: {content}" elif isinstance(message, HumanMessage): message_text = f"{human_prompt} {content}" elif isinstance(message, AIMessage): message_text = f"{ai_prompt} {content}" elif isinstance(message, SystemMessage): message_text = content else: raise ValueError(f"Got unknown type {message}") return message_text def convert_messages_to_prompt_anthropic( messages: List[BaseMessage], *, human_prompt: str = "\n\nHuman:", ai_prompt: str = "\n\nAssistant:", ) -> str: """Format a list of messages into a full prompt for the Anthropic model Args: messages (List[BaseMessage]): List of BaseMessage to combine. human_prompt (str, optional): Human prompt tag. Defaults to "\n\nHuman:". ai_prompt (str, optional): AI prompt tag. Defaults to "\n\nAssistant:". Returns: str: Combined string with necessary human_prompt and ai_prompt tags. """ messages = messages.copy() # don't mutate the original list if not isinstance(messages[-1], AIMessage): messages.append(AIMessage(content="")) text = "".join( _convert_one_message_to_text(message, human_prompt, ai_prompt) for message in messages ) # trim off the trailing ' ' that might come from the "Assistant: " return text.rstrip() class ChatAnthropic(BaseChatModel, _AnthropicCommon): """`Anthropic` chat large language models. To use, you should have the ``anthropic`` python package installed, and the environment variable ``ANTHROPIC_API_KEY`` set with your API key, or pass it as a named parameter to the constructor. Example: .. code-block:: python import anthropic from langchain_community.chat_models import ChatAnthropic model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key") """ class Config: """Configuration for this pydantic object.""" allow_population_by_field_name = True arbitrary_types_allowed = True @property def lc_secrets(self) -> Dict[str, str]: return {"anthropic_api_key": "ANTHROPIC_API_KEY"} @property def _llm_type(self) -> str: """Return type of chat model.""" return "anthropic-chat" @classmethod def is_lc_serializable(cls) -> bool: """Return whether this model can be serialized by Langchain.""" return True @classmethod def get_lc_namespace(cls) -> List[str]: """Get the namespace of the langchain object.""" return ["langchain", "chat_models", "anthropic"] def _convert_messages_to_prompt(self, messages: List[BaseMessage]) -> str: """Format a list of messages into a full prompt for the Anthropic model Args: messages (List[BaseMessage]): List of BaseMessage to combine. Returns: str: Combined string with necessary HUMAN_PROMPT and AI_PROMPT tags. """ prompt_params = {} if self.HUMAN_PROMPT: prompt_params["human_prompt"] = self.HUMAN_PROMPT if self.AI_PROMPT: prompt_params["ai_prompt"] = self.AI_PROMPT return convert_messages_to_prompt_anthropic(messages=messages, **prompt_params) def convert_prompt(self, prompt: PromptValue) -> str: return self._convert_messages_to_prompt(prompt.to_messages()) def _stream( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any, ) -> Iterator[ChatGenerationChunk]: prompt = self._convert_messages_to_prompt(messages) params: Dict[str, Any] = {"prompt": prompt, **self._default_params, **kwargs} if stop: params["stop_sequences"] = stop stream_resp = self.client.completions.create(**params, stream=True) for data in stream_resp: delta = data.completion yield ChatGenerationChunk(message=AIMessageChunk(content=delta)) if run_manager: run_manager.on_llm_new_token(delta) async def _astream( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any, ) -> AsyncIterator[ChatGenerationChunk]: prompt = self._convert_messages_to_prompt(messages) params: Dict[str, Any] = {"prompt": prompt, **self._default_params, **kwargs} if stop: params["stop_sequences"] = stop stream_resp = await self.async_client.completions.create(**params, stream=True) async for data in stream_resp: delta = data.completion yield ChatGenerationChunk(message=AIMessageChunk(content=delta)) if run_manager: await run_manager.on_llm_new_token(delta) def _generate( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, **kwargs: Any, ) -> ChatResult: if self.streaming: stream_iter = self._stream( messages, stop=stop, run_manager=run_manager, **kwargs ) return generate_from_stream(stream_iter) prompt = self._convert_messages_to_prompt( messages, ) params: Dict[str, Any] = { "prompt": prompt, **self._default_params, **kwargs, } if stop: params["stop_sequences"] = stop response = self.client.completions.create(**params) completion = response.completion message = AIMessage(content=completion) return ChatResult(generations=[ChatGeneration(message=message)]) async def _agenerate( self, messages: List[BaseMessage], stop: Optional[List[str]] = None, run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any, ) -> ChatResult: if self.streaming: stream_iter = self._astream( messages, stop=stop, run_manager=run_manager, **kwargs ) return await agenerate_from_stream(stream_iter) prompt = self._convert_messages_to_prompt( messages, ) params: Dict[str, Any] = { "prompt": prompt, **self._default_params, **kwargs, } if stop: params["stop_sequences"] = stop response = await self.async_client.completions.create(**params) completion = response.completion message = AIMessage(content=completion) return ChatResult(generations=[ChatGeneration(message=message)]) def get_num_tokens(self, text: str) -> int: """Calculate number of tokens.""" if not self.count_tokens: raise NameError("Please ensure the anthropic package is loaded") return self.count_tokens(text)
[ "{}" ]
2024-01-10
mth93/langchain
libs~langchain~langchain~callbacks~streamlit~mutable_expander.py
from langchain_community.callbacks.streamlit.mutable_expander import ( ChildRecord, ChildType, MutableExpander, ) __all__ = ["ChildType", "ChildRecord", "MutableExpander"]
[]
2024-01-10
mth93/langchain
libs~community~langchain_community~document_transformers~html2text.py
from typing import Any, Sequence from libs.core.langchain_core.documents import BaseDocumentTransformer, Document class Html2TextTransformer(BaseDocumentTransformer): """Replace occurrences of a particular search pattern with a replacement string Arguments: ignore_links: Whether links should be ignored; defaults to True. ignore_images: Whether images should be ignored; defaults to True. Example: .. code-block:: python from langchain_community.document_transformers import Html2TextTransformer html2text = Html2TextTransformer() docs_transform = html2text.transform_documents(docs) """ def __init__(self, ignore_links: bool = True, ignore_images: bool = True) -> None: self.ignore_links = ignore_links self.ignore_images = ignore_images def transform_documents( self, documents: Sequence[Document], **kwargs: Any, ) -> Sequence[Document]: try: import html2text except ImportError: raise ImportError( """html2text package not found, please install it with `pip install html2text`""" ) # Create a html2text.HTML2Text object and override some properties h = html2text.HTML2Text() h.ignore_links = self.ignore_links h.ignore_images = self.ignore_images new_documents = [] for d in documents: new_document = Document( page_content=h.handle(d.page_content), metadata={**d.metadata} ) new_documents.append(new_document) return new_documents async def atransform_documents( self, documents: Sequence[Document], **kwargs: Any, ) -> Sequence[Document]: raise NotImplementedError
[]
2024-01-10
edwardpwtsoi/sgpt
biencoder~beir~beir_dense_retriever.py
import argparse import collections import json import logging import os import pathlib import pickle from typing import Dict, List, Tuple, ValuesView import numpy as np import torch from transformers import AutoTokenizer, AutoModel from tqdm import tqdm from beir import util, LoggingHandler from beir.datasets.data_loader import GenericDataLoader from beir.retrieval.evaluation import EvaluateRetrieval from custommodels import DenseRetrievalExactSearch, SentenceBERTAsym, SentenceBERTBOSEOS # Code to print debug information to stdout logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()], ) logger = logging.getLogger(__name__) def parse_args(): # Parse command line arguments parser = argparse.ArgumentParser() parser.add_argument("--dataset", type=str, default="scifact", help="Dataset to embed.") parser.add_argument("--modelname", type=str, default="bert-base-uncased", help="Model to use.") parser.add_argument("--method", type=str, default="mean", help="Method to use.") parser.add_argument("--device", type=str, default="cuda:0", help="Device to use.") parser.add_argument("--layeridx", type=int, default=-1, help="Layer to use: -1 is the last.") parser.add_argument( "--usest", action="store_const", default=False, const=True, help="Whether to use Sentence Transformers", ) parser.add_argument("--datapath", type=str, default="./datasets/", help="Path to folder with datasets") parser.add_argument( "--overwrite", action="store_const", default=False, const=True, help="Whether to recompute & overwrite existing results", ) parser.add_argument("--batchsize", type=int, default=250, help="How many requests to batch") parser.add_argument( "--saveemb", action="store_const", default=False, const=True, help="Whether to save embeddings", ) parser.add_argument( "--computeavg", action="store_const", default=False, const=True, help="Whether to only compute model avgs", ) parser.add_argument( "--selectbest", action="store_const", default=False, const=True, help="Compute best ckpts", ) parser.add_argument( "--speca", action="store_const", default=False, const=True, help="Use special token a encoding method", ) parser.add_argument( "--specb", action="store_const", default=False, const=True, help="Use special brackets encoding method", ) parser.add_argument( "--maxseqlen", type=int, default=None, help="Sequence length to use; SGPT-msmarco-specb models use 300" ) args = parser.parse_args() return args SPECB_QUE_BOS = "[" SPECB_QUE_EOS = "]" SPECB_DOC_BOS = "{" SPECB_DOC_EOS = "}" class CustomEmbedder: def __init__( self, model_name="EleutherAI/gpt-neo-1.3B", batch_size=250, device="cuda:0", save_emb=False, reinit=False, layeridx=-1, method="mean", dataset="scifact", specb=False, maxseqlen=None, **kwargs, ): self.device = torch.device(device) self.model = AutoModel.from_pretrained(model_name, **kwargs).to(self.device) if reinit: logging.warn("Reiniting all model weights") self.model.init_weights() self.model.eval() self.max_token_len = maxseqlen if maxseqlen else self.model.config.max_position_embeddings # Account for special tokens: if "bert" in model_name: logging.info("BERT model detected: Reducing token len by 2 to account for [CLS] & [SEP]") self.max_token_len -= 2 if specb: # Leave two tokens for special brackets self.max_token_len -= 2 self.tokenizer = AutoTokenizer.from_pretrained(model_name) # gpt models do not have a padding token by default - Add one and ignore it with the attn mask lateron if "gpt" in model_name.lower(): self.tokenizer.pad_token = self.tokenizer.eos_token self.batch_size = batch_size self.save_emb = save_emb self.layeridx = layeridx self.method = method self.specb = specb if specb: self.bos_token_q = self.tokenizer.encode(SPECB_QUE_BOS) self.eos_token_q = self.tokenizer.encode(SPECB_QUE_EOS) self.bos_token_d = self.tokenizer.encode(SPECB_DOC_BOS) self.eos_token_d = self.tokenizer.encode(SPECB_DOC_EOS) self.base_path = f"embeddings/{model_name.split('/')[-1]}/{self.method}/{dataset}" pathlib.Path(self.base_path).mkdir(parents=True, exist_ok=True) def embed(self, batch, is_query, **kwargs): docs_truncated = 0 toks_truncated = 0 total_toks = 0 batch_tokens = collections.defaultdict(list) gather_indices = [] for i, txt in enumerate(batch): # Recommendation from OpenAI Docs: replace newlines with space txt = txt.replace("\n", " ") # Convert string to list of integers according to tokenizer's vocabulary tokens = self.tokenizer.tokenize(txt) tokens = self.tokenizer.convert_tokens_to_ids(tokens) token_len = len(tokens) total_toks += token_len if token_len > self.max_token_len: docs_truncated += 1 toks_truncated += token_len - self.max_token_len elif token_len == 0: raise ValueError("Empty items should be cleaned prior to running") input_dict = self.tokenizer.prepare_for_model( tokens[: self.max_token_len], add_special_tokens=True ) if self.specb: if is_query: input_dict["input_ids"] = self.bos_token_q + input_dict["input_ids"] + self.eos_token_q else: input_dict["input_ids"] = self.bos_token_d + input_dict["input_ids"] + self.eos_token_d input_dict["attention_mask"] = [1] + input_dict["attention_mask"] + [1] # input_ids: Same as tokens, but with model-specific beginning and end tokens # attention_mask: List of 1s for each input_id, i.e. the tokens it should attend to batch_tokens["input_ids"].append(input_dict["input_ids"]) batch_tokens["attention_mask"].append(input_dict["attention_mask"]) assert len(input_dict["input_ids"]) == len(input_dict["attention_mask"]) gather_indices.append(len(input_dict["input_ids"]) - 1) # Account for 0-indexing # No need for truncation, as all inputs are now trimmed to less than the models seq length batch_tokens = self.tokenizer.pad(batch_tokens, padding=True, return_tensors="pt") # Move to CPU/GPU batch_tokens = {k: v.to(self.device) for k, v in batch_tokens.items()} with torch.no_grad(): embedded_batch = self.model(**batch_tokens, output_hidden_states=True, **kwargs) all_hidden_states = embedded_batch.hidden_states input_mask_expanded = ( batch_tokens["attention_mask"] .unsqueeze(-1) .expand(all_hidden_states[-1].size()) .float() ) if docs_truncated: logging.warn( f"Truncated {docs_truncated} out of {len(batch)} documents by {toks_truncated} out of {total_toks}." ) all_hidden_states = [x.cpu() for x in all_hidden_states] return all_hidden_states, input_mask_expanded.cpu(), gather_indices, embedded_batch def embed_batcher(self, texts: List[Tuple[int, str]], is_query, out_name=None, **kwargs): all_embeddings = {} for i in tqdm(range(0, len(texts), self.batch_size)): # Subselect batch_size items batch = texts[i : i + self.batch_size] ids, sentences = zip(*batch) all_hidden_states, input_mask_expanded, gather_indices, embedded_batch = self.embed(sentences, is_query=is_query) hidden_state = all_hidden_states[self.layeridx] if abs(self.layeridx) > len(all_hidden_states): raise ValueError(f"Layer Idx {self.layeridx} is larger than the {len(all_hidden_states)} hidden states") ### APPLY POOLING ### if self.method == "mean": # bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum(hidden_state * input_mask_expanded, dim=1) sum_mask = input_mask_expanded.sum(dim=1) embedding = sum_embeddings / sum_mask elif self.method == "meanmean": bs, seq_len, hidden_dim = hidden_state.shape num_layers = len(all_hidden_states) hidden_states = torch.stack(all_hidden_states) input_mask_expanded = input_mask_expanded.unsqueeze(0).expand(hidden_states.size()) assert hidden_states.shape == input_mask_expanded.shape # num_layers, bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum( torch.sum(hidden_states * input_mask_expanded, dim=2), dim=0 ) sum_mask = input_mask_expanded.sum(dim=2).sum(dim=0) embedding = sum_embeddings / sum_mask elif self.method == "weightedmean": weights = ( torch.arange(start=1, end=hidden_state.shape[1] + 1) .unsqueeze(0) .unsqueeze(-1) .expand(hidden_state.size()) .float() ) # bs, seq_len, hidden_dim -> bs, hidden_dim sum_embeddings = torch.sum(hidden_state * input_mask_expanded * weights, dim=1) sum_mask = torch.sum(input_mask_expanded * weights, dim=1) embedding = sum_embeddings / sum_mask elif self.method == "lasttoken": bs, seq_len, hidden_dim = hidden_state.shape # Turn indices from shape [bs] --> [bs, 1, hidden_dim] gather_indices = torch.LongTensor(gather_indices) gather_indices = gather_indices.unsqueeze(-1).repeat(1, hidden_dim) gather_indices = gather_indices.unsqueeze(1) assert gather_indices.shape == (bs, 1, hidden_dim) # Gather along the 1st dim (seq_len) (bs, seq_len, hidden_dim -> bs, hidden_dim) # No need for the attention mask as we gather the last token where attn_mask = 1 embedding = torch.gather(hidden_state, 1, gather_indices).squeeze() elif self.method == "lasttokenmean": bs, seq_len, hidden_dim = hidden_state.shape num_layers = len(all_hidden_states) hidden_states = torch.stack(all_hidden_states) # Turn indices from shape [bs] --> [num_layers, bs, 1, hidden_dim] gather_indices = torch.LongTensor(gather_indices) gather_indices = gather_indices.unsqueeze(-1).repeat(1, hidden_dim) gather_indices = gather_indices.unsqueeze(0).repeat(num_layers, 1, 1) gather_indices = gather_indices.unsqueeze(2) assert gather_indices.shape == (num_layers, bs, 1, hidden_dim) # Gather along the 2nd dim (seq_len) (num_layers, bs, seq_len, hidden_dim -> num_layers, bs, hidden_dim) embedding = torch.gather(hidden_states, 2, gather_indices).squeeze() assert embedding.shape == (num_layers, bs, hidden_dim) # num_layers, bs, hidden_dim -> bs, hidden_dim embedding = torch.mean(embedding, 0) elif self.method == "poolout": embedding = embedded_batch.pooler_output.cpu() add_embeddings = {id: emb.numpy() for id, emb in zip(ids, embedding)} all_embeddings = {**all_embeddings, **add_embeddings} assert len(texts) == len(all_embeddings) if self.save_emb: pickle.dump(all_embeddings, open(out_name, "wb")) return all_embeddings def encode_queries(self, queries: List[str], batch_size: int, **kwargs) -> np.ndarray: # Embed if not already present embedding_queries_path = f"{self.base_path}_queries.pickle" if os.path.exists(embedding_queries_path): embeddings = pickle.load(open(embedding_queries_path, "rb")) else: embeddings = self.embed_batcher(texts=queries, out_name=embedding_queries_path, is_query=True, **kwargs) # Sort embeddings according to the order given & take just the values embeddings = [embeddings[id] for (id, _) in queries] embeddings = np.array(embeddings) logger.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings def encode_corpus( self, corpus: List[Dict[str, str]], batch_size: int, batch_num="", **kwargs ) -> np.ndarray: # Embed if not already present embedding_corpus_path = f"{self.base_path}_corpus{batch_num}.pickle" if os.path.exists(embedding_corpus_path): embeddings = pickle.load(open(embedding_corpus_path, "rb")) else: # corpus is of form [(id, {"title": "xxx", "text": "yyy"}), ...] corpus = [(id, (data["title"] + " " + data["text"]).strip()) if "title" in data else data["text"].strip() for (id, data) in corpus] embeddings = self.embed_batcher(texts=corpus, out_name=embedding_corpus_path, is_query=False, **kwargs) # Sort embeddings according to the order given embeddings = [embeddings[id] for (id, _) in corpus] embeddings = np.array(embeddings) logger.info(f"Produced embeddings of shape {embeddings.shape}") return embeddings def main(args): dataset = args.dataset model_name = args.modelname device = args.device use_st = args.usest base_data_path = args.datapath overwrite = args.overwrite batch_size = args.batchsize save_emb = args.saveemb method = args.method layeridx = args.layeridx speca = args.speca specb = args.specb maxseqlen = args.maxseqlen if args.computeavg: compute_model_avg() exit() elif args.selectbest: select_best_ckpt() exit() data_path = f"{base_data_path}/{dataset}" if not os.path.exists(data_path): url = "https://public.ukp.informatik.tu-darmstadt.de/thakur/BEIR/datasets/{}.zip".format( dataset ) out_dir = os.path.join(os.getcwd(), "datasets") data_path = util.download_and_unzip(url, out_dir) print("Dataset downloaded here: {}".format(data_path)) # Load the dataset into BEIR data_path = f"datasets/{dataset}" # In the paper it says, BEIR used the dev set for msmarco split = "dev" if dataset == "msmarco" else "test" corpus, queries, qrels = GenericDataLoader(data_path).load(split=split) corpus = clean_titles(corpus) if "robust04" in data_path else corpus empty_keys = [k for k, v in corpus.items() if not v["text"]] logger.info(f"Found {len(empty_keys)} empty keys in corpus. Removing...") assert len(empty_keys) < len(corpus), "Too many empty keys..." # Remove keys in place for k in empty_keys: del corpus[k] empty_keys = [k for k, v in queries.items() if not v] assert not empty_keys, f"Contains {len(empty_keys)} empty queries" if use_st: from beir.retrieval import models from beir.retrieval.search.dense import DenseRetrievalExactSearch as DRES if "asym" in model_name: logger.info(f"Using asymmetric model.") custom_model = DRES(SentenceBERTAsym(model_name, device=device), batch_size=batch_size) elif speca or specb: custom_model = DRES(SentenceBERTBOSEOS(model_name, speca=speca, specb=specb, device=device), batch_size=batch_size) else: custom_model = DRES(models.SentenceBERT(model_name, device=device), batch_size=batch_size) else: if speca: raise ValueError("speca is only supported with use_st") custom_model = DenseRetrievalExactSearch( CustomEmbedder( model_name=model_name, method=method, device=device, batch_size=batch_size, save_emb=save_emb, layeridx=layeridx, specb=specb, maxseqlen=maxseqlen, ) ) # Turn cqadupstack/english -> cqadupstack_english dataset = dataset.replace("/", "_") model_name = model_name.replace("/", "_") out_path = f"./results_{model_name}_{method}_{dataset}.json" if os.path.exists(out_path) and not overwrite: logger.info(f"Found {out_path} - Skipping ...") return # Optionally use less k-values to save memory # E.g. [.. 100] instead of [.. 1000] will reduce self.results by 90% retriever = EvaluateRetrieval(custom_model, k_values=[1, 3, 5, 10, 100, 1000]) #### Retrieve dense results (format of results is identical to qrels) results = retriever.retrieve(corpus, queries) # Save scores for top 1000 docs for each query, i.e. 1000 * queries lines with open(out_path, "w") as fp: json.dump(results, fp) ndcg, _map, recall, precision = retriever.evaluate(qrels, results, retriever.k_values) ndgcs_path = f"./beir_embeddings_ndcgs.json" if not os.path.exists(ndgcs_path): ndcgs_json = {"ndcgs": {}, "maps": {}, "recalls": {}, "precisions": {}} else: with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) ndcgs_json["ndcgs"].setdefault(model_name, {}) ndcgs_json["ndcgs"][model_name][dataset] = ndcg # Backwards compat ndcgs_json.setdefault("maps", {}) ndcgs_json.setdefault("recalls", {}) ndcgs_json.setdefault("precisions", {}) ndcgs_json["maps"].setdefault(model_name, {}) ndcgs_json["recalls"].setdefault(model_name, {}) ndcgs_json["precisions"].setdefault(model_name, {}) ndcgs_json["maps"][model_name][dataset] = _map ndcgs_json["recalls"][model_name][dataset] = recall ndcgs_json["precisions"][model_name][dataset] = precision # Add average of cqadupstack once all present CQADUPSTACK_DATASETS = [ "android", "english", "gaming", "gis", "mathematica", "physics", "programmers", "stats", "wordpress", "webmasters", "unix", "tex", ] if "cqadupstack" in dataset and all( f"cqadupstack_{cqadataset}" in ndcgs_json["ndcgs"][model_name] for cqadataset in CQADUPSTACK_DATASETS ): ndcgs_json["ndcgs"][model_name]["cqadupstack"] = {} for cqadataset in CQADUPSTACK_DATASETS: for k, v in ndcgs_json["ndcgs"][model_name][f"cqadupstack_{cqadataset}"].items(): ndcgs_json["ndcgs"][model_name]["cqadupstack"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["cqadupstack"][k] += v / len(CQADUPSTACK_DATASETS) with open(ndgcs_path, "w") as f: json.dump(ndcgs_json, f) def clean_titles(corpus): for k in corpus: if "title" in corpus[k] and corpus[k]["title"] is None: corpus[k]["title"] = "" return corpus def compute_model_avg(): ndgcs_path = f"./beir_embeddings_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) subsubavg_datasets = ["nfcorpus", "fiqa", "arguana", "scidocs", "scifact"] subavg_datasets = ["trec-covid", "nfcorpus", "hotpotqa", "fiqa", "arguana", "webis-touche2020", "quora", "dbpedia-entity", "fever", "climate-fever", "scifact"] # Average does not include msmarco due to in-domain avg_datasets = ["nfcorpus", "bioasq", "nq", "hotpotqa", "fiqa", "signal1m", "trec-news", "arguana", "webis-touche2020", "quora", "dbpedia-entity", "scidocs", "fever", "climate-fever", "scifact", "robust04", "cqadupstack", "trec-covid"] for model_name in ndcgs_json["ndcgs"]: ndcgs_json["ndcgs"][model_name]["average"] = {} ndcgs_json["ndcgs"][model_name]["subaverage"] = {} ndcgs_json["ndcgs"][model_name]["subsubaverage"] = {} model_datasets = [ds for ds in ndcgs_json["ndcgs"][model_name] if ds in avg_datasets] for dataset in ndcgs_json["ndcgs"][model_name]: if dataset not in model_datasets: print(f"Skipping {dataset}") continue for k, v in ndcgs_json["ndcgs"][model_name][dataset].items(): ndcgs_json["ndcgs"][model_name]["average"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["average"][k] += v / len(model_datasets) if all(sub_ds in model_datasets for sub_ds in subavg_datasets) and (dataset in subavg_datasets): ndcgs_json["ndcgs"][model_name]["subaverage"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["subaverage"][k] += v / len(subavg_datasets) if all(subsub_ds in model_datasets for subsub_ds in subsubavg_datasets) and (dataset in subsubavg_datasets): ndcgs_json["ndcgs"][model_name]["subsubaverage"].setdefault(k, 0) ndcgs_json["ndcgs"][model_name]["subsubaverage"][k] += v / len(subsubavg_datasets) with open(ndgcs_path, "w") as f: json.dump(ndcgs_json, f) def select_best_ckpt(): """A bit hard-coded function for selecting the best checkpoints given results of many ckpts""" ndgcs_path = "./beir_embeddings_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) best_ndgcs_path = "./beir_embeddings_best_ndcgs.json" if not os.path.exists(best_ndgcs_path): best_ndgcs_json = {"ndcgs": {}} else: with open(best_ndgcs_path, "r") as f: best_ndgcs_json = json.load(f) # SGPT 125M ckpts ckpts = ["15600", "31200", "46800", "62398", "62400", "78000",] # SGPT 2.7B ckpts ckpts += ["101387", "124784", "148181", "156000", "31196", "54593", "7799", "93588", "109186", "132583", "15598", "38995", "62392", "77990", "116985", "140382", "155980", "23397", "46794", "70191", "85789"] # SGPT 6.1B ckpts ckpts += ["112311", "137269", "174706", "237101", "262059", "299496", "37437", "74874", "12479", "149748", "187185", "212143", "24958", "274538", "311975", "49916", "87353", "124790", "162227", "199664", "224622", "249580", "287017", "311990", "62395", "99832",] ckpts = set(ckpts) for model_name in ndcgs_json["ndcgs"]: model_ckpt = model_name.split("_")[-1] model_base_name = model_name.strip(model_ckpt) if model_ckpt in ckpts: best_score = 0 best_model_name = None for ckpt in ckpts: cur_model_name = model_base_name + ckpt if cur_model_name not in ndcgs_json["ndcgs"]: logging.info(f"Did not find {cur_model_name}") continue cur_score = ndcgs_json["ndcgs"][cur_model_name]["average"]["NDCG@10"] if cur_score > best_score: best_score = cur_score best_model_name = cur_model_name best_ndgcs_json["ndcgs"][best_model_name] = ndcgs_json["ndcgs"][best_model_name] else: logger.info(f"Did not find ckpts for {model_name}. Skipping...") with open(best_ndgcs_path, "w") as f: json.dump(best_ndgcs_json, f) def rank_model_avg(): """A function for quickly ranking the best models - Can just be copy pasted into the local Python Interpreter""" import os, json ndgcs_path = "./beir_embeddings_best_ndcgs.json" if os.path.exists(ndgcs_path): with open(ndgcs_path, "r") as f: ndcgs_json = json.load(f) out = sorted(ndcgs_json["ndcgs"], key=lambda x: ndcgs_json["ndcgs"][x]["average"]["NDCG@10"], reverse=True) print({x: ndcgs_json["ndcgs"][x] for x in out[:5]}) print(out[:5]) if __name__ == "__main__": args = parse_args() main(args)
[]
2024-01-10
jkkronk/quiz-game
create_video~quiz.py
from pydantic import BaseModel, Field from openai import OpenAI import instructor import json import random class QuizHost(): intro: str = Field(..., description="The introduction of the quiz.") outro: str = Field(..., description="The outro of the quiz.") #init def __init__(self, intro, outro): self.intro = intro self.outro = outro class QuizClues(BaseModel): clues: list[str] = Field(..., description="A list of size 5 with clues for the quiz.") explanations: list[str] = Field(..., description="A list of size 5 with explanations for the clues.") def clear_city(self): self.clues = [clue.replace("Zurich", "the city") for clue in self.clues] def get_clue(self, round: int) -> str: return self.clues[round] def get_explanation(self, round: int) -> str: return self.explanations[round] def get_all_clues(self): return "\n".join(self.clues) def get_all_explanation(self) -> str: return "\n".join(self.explanations) def save(self, city, file_path: str): data = { "city": city, "clues": self.clues, "explanations": self.explanations } with open(file_path, 'w') as file: json.dump(data, file) @classmethod def open(cls, file_path: str): with open(file_path, 'r') as file: data = json.load(file) return cls(clues=data['clues'], explanations=data['explanations']) def random_destination() -> str: # open the cities text file and pick a random city # return the city path_to_cities = "static/cities.txt" # Opening the file with open(path_to_cities, 'r') as file: cities_text = file.read() # Splitting the text into a list of cities cities_list = cities_text.split(',') # Selecting a random city from the list random_city = random.choice(cities_list) return random_city.replace("\n", "") def create_quiz(city:str, openai_api_key="") -> QuizClues: if openai_api_key == "": client = instructor.patch(OpenAI()) else: client = instructor.patch(OpenAI(api_key=openai_api_key)) prompt = f""" You are a quiz host and you are hosting a quiz where the answer is {city}. You are suppose to come up with 5 clues for the city. Each clue should be easier and easier. In the beginning it shall be very hard. But in the end it shall be very easy. Each clue should be a couple of sentences long. The clues should be written in English. The clues could be on historic facts, famous persons, famous buildings, famous events, famous food, famous drinks, famous music, famous art, famous sports, famous games, famous movies, famous books, from {city}. Each clue should end with "..." The clues should be humorous and engaging. It can be a bit hard to guess the answer and there shall be word plays, rimes and puns. Additionally, add a short explanation for each clue. An example for New York could be: >>First Clue: We leave religious cheese and head north towards great fruit. Liberal Dutch have a historical stamp in our five-star Jorvik... >>Second Clue: The theatrical stage of the masculine headdress lies on the broad road. Reuterswärd's revolver pacifies Moon's headquarters... >>Third Clue: In the center of our new world city is a square garden. Temperate climate notwithstanding, you can skate at Rockefeller but scratch the squares first... >>Fourth Clue: At our entrance we are greeted by freedom unless you land at JFK. King Kong climbed here on an empirical building, but the economy was managed from Vallgatan... >>Last Clue: September 11 will always be remembered in the United States' largest city. Well, you know the answer... Another example for Bejing/Peking could be: >>First Clue: We travel from one "heavenly" city to another in a country where, in other words, you can give clues. Old summer palace was destroyed at our destination by English troops during war in 1860. Other summer palace is also great attraction here in the city... >>Second Clue: Our many-thousand-year-old city has historically been replaced with its southern part. The short-lived lightning record was broken here and happened in just 9.69 seconds. Birdsnest is another clue... >>Third Clue: The Swedish evergreen tree is also said to be extremely popular here. 08 and 22, well then, world olympians competed in our destination... >>Fourth Clue: Our semi-forbidden city is the country's second after shark city and is a possible final destination for the Trans-Siberian journey. When you arrive, you will probably be greeted with "ni hao". Maybe you will also be treated to duck... >>Last Clue: Now we have arrived in China's capital, where the English king is pointed... """ clues: QuizClues = client.chat.completions.create( model="gpt-4", response_model=QuizClues, messages=[ {"role": "user", "content": prompt}, ], max_retries=2, ) return clues
[ "\n You are a quiz host and you are hosting a quiz where the answer is PLACEHOLDER. You are suppose to come up\n with 5 clues for the city. Each clue should be easier and easier. In the beginning it \n shall be very hard. But in the end it shall be very easy. \n \n Each clue should be a couple of sentences long. \n The clues should be written in English. \n The clues could be on historic facts, famous persons, famous buildings, famous events, famous food, \n famous drinks, famous music, famous art, famous sports, famous games, famous movies, famous books, from \n PLACEHOLDER. \n Each clue should end with \"...\"\n The clues should be humorous and engaging. It can be a bit hard to guess the answer and there shall be\n word plays, rimes and puns.\n \n Additionally, add a short explanation for each clue.\n \n An example for New York could be:\n >>First Clue: We leave religious cheese and head north towards great fruit. Liberal Dutch have a historical stamp in our five-star Jorvik...\n >>Second Clue: The theatrical stage of the masculine headdress lies on the broad road. Reuterswärd's revolver pacifies Moon's headquarters...\n >>Third Clue: In the center of our new world city is a square garden. Temperate climate notwithstanding, you can skate at Rockefeller but scratch the squares first...\n >>Fourth Clue: At our entrance we are greeted by freedom unless you land at JFK. King Kong climbed here on an empirical building, but the economy was managed from Vallgatan...\n >>Last Clue: September 11 will always be remembered in the United States' largest city. Well, you know the answer...\n \n Another example for Bejing/Peking could be:\n >>First Clue: We travel from one \"heavenly\" city to another in a country where, in other words, you can give clues. Old summer palace was destroyed at our destination by English troops during war in 1860. Other summer palace is also great attraction here in the city...\n >>Second Clue: Our many-thousand-year-old city has historically been replaced with its southern part. The short-lived lightning record was broken here and happened in just 9.69 seconds. Birdsnest is another clue...\n >>Third Clue: The Swedish evergreen tree is also said to be extremely popular here. 08 and 22, well then, world olympians competed in our destination...\n >>Fourth Clue: Our semi-forbidden city is the country's second after shark city and is a possible final destination for the Trans-Siberian journey. When you arrive, you will probably be greeted with \"ni hao\". Maybe you will also be treated to duck...\n >>Last Clue: Now we have arrived in China's capital, where the English king is pointed...\n " ]
2024-01-10
datasnakes/archives
filter.py
import os import subprocess from pathlib import Path from shutil import copy from Bio import SeqIO from Datasnakes.Orthologs.Align.QualityControl.guidance2 import Guidance2Commandline from Datasnakes.Orthologs.Align.pal2nal import Pal2NalCommandline from Datasnakes.Orthologs.Genbank.utils import multi_fasta_manipulator # TODO-ROB: Create appropriate class variables for Filtered Tree to inherit # TODO-ROB: Create proper class variables to make simpler and for above todo class FilteredAlignment(object): """Filters one alignment per gene at a time.""" def __init__(self, na_fasta, aa_fasta, gene_name=None, home=os.getcwd(), msaProgram='CLUSTALW', na_bootstraps=1, aa_bootstraps=1, na_seqCutoff=0.6, aa_seqCutoff=0.6, na_colCutoff=0, aa_colCutoff=0.88): if gene_name is None: gene_name = Path(na_fasta).stem # Initialize the default command line arguments self.G2C_args = dict(outOrder='as_input', dataset=gene_name, msaProgram=msaProgram) self.P2N_args = dict(nogap=True, nomismatch=True) # Initialize self.home = Path(home) self.na_guidance_path = self.home / Path('NA_Guidance2') self.aa_guidance_path = self.home / Path('AA_Guidance2') self.gene = Path(na_fasta).stem na_seqFile = str(self.home / Path(na_fasta)) # Guidance NA sequence file aa_seqFile = str(self.home / Path(aa_fasta)) # Guidance AA sequence file na_fasta = str(self.home / Path(self.gene + '_G2.ffn')) # Pal2Nal NA sequence file na_alignment = str(Path(self.gene + '_P2N_na')) # Pal2Nal output file # Guidance2 iterations over the nucleic acid sequences. # Returns the file name that contains the filtered sequences. rem_na_seqFile = self.nucleic_acid_guidance(seqFile=na_seqFile, outDir=str(self.na_guidance_path), bootstraps=na_bootstraps, seqCutoff=na_seqCutoff, colCutoff=na_colCutoff) # Guidance 2 amino acid alignment filter. Filters the sequences based on the NA_Guidance2 runs. # Returns the file name that contains the filtered alignment. aa_alignment = self.amino_acid_guidance(seqFile=aa_seqFile, remFile=rem_na_seqFile, outDir=str(self.aa_guidance_path), bootstraps=aa_bootstraps, seqCutoff=aa_seqCutoff, colCutoff=aa_colCutoff) g2_seqFile = str(self.home / Path(self.gene + '_G2.ffn')) # PAL2NAL nucleic acid alignment self.pal2nal_conversion(str(aa_alignment), g2_seqFile, na_alignment) def nucleic_acid_guidance(self, seqFile, outDir, bootstraps, seqCutoff, colCutoff): seqType = 'nuc' iteration_flag = True iteration = 1 while iteration_flag is True: iterDir = Path(outDir) / Path('iter_%s' % iteration) # /home/NA_Guidance2/iter_n Path.mkdir(iterDir, parents=True, exist_ok=True) g2_seqFile = str(self.home / Path(self.gene + '_G2.ffn')) # Need for all iterations g2_rem_file = str(Path(iterDir) / Path('Seqs.Orig.fas.FIXED.Removed_Seq.With_Names')) # Need for all iterations rem_file = str(self.home / Path(self.gene + '_G2_removed.ffn')) # Need for all iterations if iteration == 1: # seqFile is the given input G2Cmd = Guidance2Commandline(self.G2C_args, seqFile=seqFile, seqType=seqType, outDir=str(iterDir), bootstraps=bootstraps, seqCutoff=seqCutoff, colCutoff=colCutoff) print(G2Cmd) subprocess.check_call([str(G2Cmd)], stderr=subprocess.STDOUT, shell=True) # Copy the Guidance removed seq file and paste it to the home directory # Creates the rem_file # Files without any removed don't have the file *.With_Names if os.path.isfile(g2_rem_file) is False: g2_rem_file = str(Path(iterDir) / Path('Seqs.Orig.fas.FIXED.Removed_Seq')) SeqIO.write(SeqIO.parse(g2_rem_file, 'fasta'), rem_file, 'fasta') # Need for iter_1 # Filter the input NA fasta file using Guidance output # Creates the g2_seqFile multi_fasta_manipulator(seqFile, g2_rem_file, g2_seqFile, manipulation='remove') # Do after copying (iter_1) or adding (iter_n) iteration_flag = True elif iteration > 1: # seqFile changes to g2_seqFile and the cutoffs change seqCutoff = 0.7 colCutoff = 0.1 G2Cmd = Guidance2Commandline(self.G2C_args, seqFile=g2_seqFile, seqType=seqType, outDir=str(iterDir), bootstraps=bootstraps, seqCutoff=seqCutoff, colCutoff=colCutoff) print(G2Cmd) subprocess.check_call([str(G2Cmd)], stderr=subprocess.STDOUT, shell=True) # Get the removed sequence count rem_count = 0 # Files without any removed don't have the file *.With_Names if os.path.isfile(g2_rem_file) is False: g2_rem_file = str(Path(iterDir) / Path('Seqs.Orig.fas.FIXED.Removed_Seq')) for rec in SeqIO.parse(g2_rem_file, 'fasta'): # Need for all iterations rem_count += 1 if rem_count > 0: # Add new sequences to the rem_file multi_fasta_manipulator(rem_file, g2_rem_file, rem_file, manipulation='add') # Filter the input NA fasta file using the updated rem_file multi_fasta_manipulator(seqFile, rem_file, g2_seqFile, manipulation='remove') iteration_flag = True else: iteration_flag = False iteration += 1 return rem_file def amino_acid_guidance(self, seqFile, remFile, outDir, bootstraps, seqCutoff, colCutoff): seqType = 'aa' g2_seqFile = str(self.home / Path(self.gene + '_G2.faa')) multi_fasta_manipulator(seqFile, remFile, g2_seqFile) G2Cmd = Guidance2Commandline(self.G2C_args, seqFile=g2_seqFile, seqType=seqType, outDir=outDir, bootstraps=bootstraps, seqCutoff=seqCutoff, colCutoff=colCutoff) print(G2Cmd) subprocess.check_call([str(G2Cmd)], stderr=subprocess.STDOUT, shell=True) # DO not remove any columns. filtered_alignment = Path(outDir) / Path('%s.CLUSTALW.aln.Sorted.With_Names' % self.G2C_args['dataset']) renamed_alignment = copy(str(filtered_alignment), str(Path(self.gene + '_G2_aa.aln'))) renamed_alignment = multi_fasta_manipulator(str(renamed_alignment), str(seqFile), str(renamed_alignment), manipulation='sort') print('Align the filtered amino acid sequences using guidance 2') return Path(renamed_alignment) def pal2nal_conversion(self, aa_alignment, na_fasta, output_file): # TODO-ROB: Add a filter step so if error[0] = Error: #--- ERROR: inconsistency between the following pep and nuc seqs ---# # TODO-ROB: ....then remove error[2] which is the sequence it can't read removed = [] # Create output directory for PAL2NAL outDir = self.home / Path('PAL2NAL') Path.mkdir(outDir, exist_ok=True) output_file = str(outDir / Path(output_file)) # Create an alignment for paml input P2Ncmd = Pal2NalCommandline(self.P2N_args, pepaln=aa_alignment, nucfasta=na_fasta, output_file=output_file + '.paml.aln', output='paml') print(P2Ncmd) pal2nal_flag = True while pal2nal_flag is True: pal2nal = subprocess.Popen([str(P2Ncmd)], stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, encoding='utf-8') error = pal2nal.stderr.readlines() out = pal2nal.stdout.readlines() pal2nal.wait() if 'ERROR: inconsistency between the following pep and nuc seqs' in error[0]: print('Caught the pal2nal error!') print(error[0]) for err in error: if '>' in err: removed.append(err.strip('>' '\n')) multi_fasta_manipulator(na_fasta, removed, na_fasta) multi_fasta_manipulator(aa_alignment, removed, aa_alignment) else: pal2nal_flag = False print('Error: ' + str(error)) print('Out: ' + str(out)) # Create an alignment for iqtree input P2Ncmd = Pal2NalCommandline(self.P2N_args, pepaln=aa_alignment, nucfasta=na_fasta, output_file=output_file + '.iqtree.aln', output='fasta') print(P2Ncmd) pal2nal = subprocess.Popen([str(P2Ncmd)], stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, encoding='utf-8') error = pal2nal.stderr.read() out = pal2nal.stdout.read() pal2nal.wait() print('Error: ' + str(error)) print('Out: ' + str(out)) print('Align the nucleic acids using the amino acid alignment.')
[]
2024-01-10
cezaryborowski/neuspell-config
neuspell~seq_modeling~elmosclstm.py
import time from .downloads import download_pretrained_model from .evals import get_metrics from .helpers import * from .models import ElmoSCLSTM from .util import get_module_or_attr """ NEW: reranking snippets """ # (GPT/GPT-2/CTRL/Transformer-XL/XLNet) import torch from torch.nn import CrossEntropyLoss HFACE_batch_size = 8 RERANKER = "GPT-2" # GPT/GPT-2/CTRL/Transformer-XL/XLNet if RERANKER == "GPT": from transformers import OpenAIGPTTokenizer, OpenAIGPTLMHeadModel gpt2Tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt') gpt2LMHeadModel = OpenAIGPTLMHeadModel.from_pretrained('openai-gpt') gpt2Tokenizer.add_special_tokens({'pad_token': "[PAD]"}) gpt2LMHeadModel.resize_token_embeddings(len(gpt2Tokenizer)) assert gpt2Tokenizer.pad_token == '[PAD]' elif "GPT-2": from transformers import GPT2Tokenizer, GPT2LMHeadModel gpt2Tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium') gpt2LMHeadModel = GPT2LMHeadModel.from_pretrained('gpt2-medium') gpt2Tokenizer.pad_token = gpt2Tokenizer.eos_token elif "Transformer-XL": from transformers import TransfoXLTokenizer, TransfoXLLMHeadModel txlTokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') txlLMHeadModel = TransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103') txlTokenizer.pad_token = txlTokenizer.eos_token else: raise NotImplementedError def get_losses_from_gpt_lm(this_sents: "list[str]", gpt2LMHeadModel, gpt2Tokenizer, device): this_input_ids = gpt2Tokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True)["input_ids"] this_labels = torch.tensor( [[i if i != gpt2Tokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(device) this_input_ids = torch.tensor(this_input_ids).to(device) this_outputs = gpt2LMHeadModel(input_ids=this_input_ids) this_lm_logits = this_outputs[0] # Shift so that tokens < n predict n shift_logits2 = this_lm_logits[:, :-1, :] shift_labels2 = this_labels[:, 1:] # Flatten the tokens loss_fct = CrossEntropyLoss(reduction='none') loss = loss_fct(shift_logits2.permute(0, 2, 1), shift_labels2) losses = loss.sum(dim=-1).cpu().detach().numpy().tolist() return losses def get_losses_from_txl_lm(this_sents: "list[str]", txlLMHeadModel, txlTokenizer, device): this_input_ids_dict = txlTokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True) this_input_ids = this_input_ids_dict["input_ids"] chunks = [sum(val) for val in this_input_ids_dict["attention_mask"]] chunks_cumsum = np.cumsum(chunks).tolist() this_labels = torch.tensor( [[i if i != txlTokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(device) this_input_ids = torch.tensor(this_input_ids).to(device) this_outputs = txlLMHeadModel(input_ids=this_input_ids, labels=this_labels) this_loss = this_outputs[0] this_loss = this_loss.view(-1).cpu().detach().numpy() losses = [sum(this_loss[str_pos:end_pos - 1]) for str_pos, end_pos in zip([0] + chunks_cumsum[:-1], chunks_cumsum)] return losses def load_model(vocab, verbose=False): model = ElmoSCLSTM(3 * len(vocab["chartoken2idx"]), vocab["token2idx"][vocab["pad_token"]], len(vocab["token_freq"])) if verbose: print(model) print(f"Number of parameters in the model: {get_model_nparams(model)}") return model def load_pretrained(model, checkpoint_path, optimizer=None, device='cuda'): if optimizer: raise Exception("If you want optimizer, call `load_pretrained_large(...)` instead of `load_pretrained(...)`") if torch.cuda.is_available() and device != "cpu": map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' print(f"Loading model params from checkpoint dir: {checkpoint_path}") try: checkpoint_data = torch.load(os.path.join(checkpoint_path, "pytorch_model.bin"), map_location=map_location) except FileNotFoundError: download_pretrained_model(checkpoint_path) checkpoint_data = torch.load(os.path.join(checkpoint_path, "pytorch_model.bin"), map_location=map_location) model.load_state_dict(checkpoint_data) return model def load_pretrained_large(model, checkpoint_path, optimizer=None, device='cuda'): if torch.cuda.is_available() and device != "cpu": map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' print(f"Loading model params from checkpoint dir: {CHECKPOINT_PATH}") checkpoint_data = torch.load(os.path.join(CHECKPOINT_PATH, "model.pth.tar"), map_location=map_location) # print(f"previously model saved at : {checkpoint_data['epoch_id']}") model.load_state_dict(checkpoint_data['model_state_dict']) if optimizer is not None: optimizer.load_state_dict(checkpoint_data['optimizer_state_dict']) max_dev_acc, argmax_dev_acc = checkpoint_data["max_dev_acc"], checkpoint_data["argmax_dev_acc"] print(f"previously, max_dev_acc: {max_dev_acc:.5f} and argmax_dev_acc: {argmax_dev_acc:.5f}") if optimizer is not None: return model, optimizer, max_dev_acc, argmax_dev_acc return model def model_predictions(model, data, vocab, device, batch_size=16, backoff="pass-through"): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) """ topk = 1 # print("###############################################") # inference_st_time = time.time() final_sentences = [] VALID_batch_size = batch_size # print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_batch_size, shuffle=False) model.eval() model.to(device) for batch_id, (batch_clean_sentences, batch_corrupt_sentences) in enumerate(data_iter): # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_ == batch_lengths).all() == True batch_idxs = [batch_idxs_.to(device) for batch_idxs_ in batch_idxs] # batch_lengths = batch_lengths.to(device) batch_labels = batch_labels.to(device) elmo_batch_to_ids = get_module_or_attr("allennlp.modules.elmo", "batch_to_ids") batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(device) # forward with torch.no_grad(): """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) """ _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) final_sentences.extend(batch_predictions) # print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) return final_sentences def model_predictions_for_ui(model, data, vocab, device, batch_size=16, backoff="pass-through", beam_search=True, topk=3): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) """ assert len(data) == 1, print(len(data)) if beam_search: if topk < 2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") else: assert topk == 1, print("if not beam_search, topk is set to 1 for UI-website purposes") print(f"beam_search: {beam_search} and topk: {topk}") print("data size: {}".format(len(data))) final_sentences = [] VALID_batch_size = batch_size data_iter = batch_iter(data, batch_size=VALID_batch_size, shuffle=False) model.eval() model.to(device) for batch_id, (batch_clean_sentences, batch_corrupt_sentences) in enumerate(data_iter): # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_ == batch_lengths).all() == True batch_idxs = [batch_idxs_.to(device) for batch_idxs_ in batch_idxs] # batch_lengths = batch_lengths.to(device) batch_labels = batch_labels.to(device) elmo_batch_to_ids = get_module_or_attr("allennlp.modules.elmo", "batch_to_ids") batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(device) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ _, batch_predictions, batch_predictions_probs = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print( f"batch_idxs:{len(batch_idxs)},batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") # based on beam_search, do either greedy topk or beam search for topk if not beam_search: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) final_sentences = batch_predictions # a list with single answer else: k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_clean_sentences, topk) final_sentences = [x[0] for x in k_batch_predictions] # a list with multiple answers print("*&$&%^$*^*&%") print(final_sentences) print("*&$&%^$*^*&%") return final_sentences def model_inference(model, data, topk, device, batch_size=16, beam_search=False, selected_lines_file=None, vocab_=None): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) topk: how many of the topk softmax predictions are considered for metrics calculations device: "cuda:0" or "cpu" batch_size: batch size for input to the model beam_search: if True, greedy topk will not be performed """ if vocab_ is not None: vocab = vocab_ if beam_search: if topk < 2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") if selected_lines_file is not None: raise Exception("when using beam_search, ***selected_lines_file*** arg is not used; no implementation") # list of dicts with keys {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} results = [] line_index = 0 inference_st_time = time.time() VALID_batch_size = batch_size valid_loss, valid_acc = 0., 0. corr2corr, corr2incorr, incorr2corr, incorr2incorr = 0, 0, 0, 0 predictions = [] print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_batch_size, shuffle=False) model.eval() model.to(device) for batch_id, (batch_clean_sentences, batch_corrupt_sentences) in tqdm(enumerate(data_iter)): torch.cuda.empty_cache() # st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_ == batch_lengths).all() == True batch_idxs = [batch_idxs_.to(device) for batch_idxs_ in batch_idxs] # batch_lengths = batch_lengths.to(device) batch_labels = batch_labels.to(device) elmo_batch_to_ids = get_module_or_attr("allennlp.modules.elmo", "batch_to_ids") batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(device) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ batch_loss, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ batch_loss, batch_predictions, batch_predictions_probs = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print( f"batch_idxs:{len(batch_idxs)},batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() # based on beam_search, do either greedy topk or beam search for topk if not beam_search: # based on topk, obtain either strings of batch_predictions or list of tokens if topk == 1: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) else: batch_predictions = untokenize_without_unks2(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) predictions.extend(batch_predictions) batch_clean_sentences = [line.lower() for line in batch_clean_sentences] batch_corrupt_sentences = [line.lower() for line in batch_corrupt_sentences] batch_predictions = [line.lower() for line in batch_predictions] corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences, batch_corrupt_sentences, batch_predictions, check_until_topk=topk, return_mistakes=False) corr2corr += corr2corr_ corr2incorr += corr2incorr_ incorr2corr += incorr2corr_ incorr2incorr += incorr2incorr_ for i, (a, b, c) in enumerate(zip(batch_clean_sentences, batch_corrupt_sentences, batch_predictions)): results.append({"id": line_index + i, "original": a, "noised": b, "predicted": c, "topk": [], "topk_prediction_probs": [], "topk_reranker_losses": []}) line_index += len(batch_clean_sentences) else: """ NEW: use untokenize_without_unks3 for beam search outputs """ # k different lists each of type batch_predictions as in topk==1 # List[List[Strings]] k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_corrupt_sentences, topk) ########################################################## ############## this takes top1 as-is ##################### # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,k_batch_predictions[0],check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## ############### this does reranking ###################### gpt2LMHeadModel.to(device) gpt2LMHeadModel.eval() # txlLMHeadModel.to(device) # txlLMHeadModel.eval() reranked_batch_predictions = [] batch_clean_sentences_ = [] batch_corrupt_sentences_ = [] batch_losses_ = [] with torch.no_grad(): for b in range(len(batch_clean_sentences)): losses = [] this_sents = [k_batch_predictions[k][b] for k in range(topk)] losses = get_losses_from_gpt_lm(this_sents, gpt2LMHeadModel, gpt2Tokenizer, device) # losses = get_losses_from_txl_lm(this_sents, txlLMHeadModel, txlTokenizer, device) kmin = np.argmin(losses) reranked_batch_predictions.append(k_batch_predictions[kmin][b]) batch_clean_sentences_.append(batch_clean_sentences[b]) batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) batch_losses_.append(losses) corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences_, batch_corrupt_sentences_, reranked_batch_predictions, check_until_topk=1, return_mistakes=False) corr2corr += corr2corr_ corr2incorr += corr2incorr_ incorr2corr += incorr2corr_ incorr2incorr += incorr2incorr_ batch_predictions_k = [[k_batch_predictions[j][i] for j in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] batch_predictions_probs_k = [ [k_batch_predictions_probs[j][i] for j in range(len(k_batch_predictions_probs))] for i in range(len(k_batch_predictions_probs[0]))] for i, (a, b, c, d, e, f) in \ enumerate(zip(batch_clean_sentences_, batch_corrupt_sentences_, reranked_batch_predictions, batch_predictions_k, batch_predictions_probs_k, batch_losses_)): results.append({"id": line_index + i, "original": a, "noised": b, "predicted": c, "topk": d, "topk_prediction_probs": e, "topk_reranker_losses": f}) line_index += len(batch_clean_sentences) # delete del batch_loss del batch_predictions del batch_labels, batch_lengths, batch_idxs, batch_lengths_, batch_elmo_inp torch.cuda.empty_cache() # ''' # # update progress # progressBar(batch_id+1, # int(np.ceil(len(data) / VALID_batch_size)), # ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], # [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),None,None]) # ''' print(f"\nEpoch {None} valid_loss: {valid_loss / (batch_id + 1)}") print("total inference time for this data is: {:4f} secs".format(time.time() - inference_st_time)) print("###############################################") print("total token count: {}".format(corr2corr + corr2incorr + incorr2corr + incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr + incorr2corr) / (corr2corr + corr2incorr + incorr2corr + incorr2incorr)}") print(f"word correction rate is {(incorr2corr) / (incorr2corr + incorr2incorr)}") print("###############################################") if not beam_search and selected_lines_file is not None: print("evaluating only for selected lines ... ") assert len(data) == len(predictions), print(len(data), len(predictions), "lengths mismatch") if selected_lines_file is not None: selected_lines = {num: "" for num in [int(line.strip()) for line in open(selected_lines_file, 'r')]} else: selected_lines = None clean_lines, corrupt_lines, predictions_lines = [tpl[0] for tpl in data], [tpl[1] for tpl in data], predictions corr2corr, corr2incorr, incorr2corr, incorr2incorr, mistakes = \ get_metrics(clean_lines, corrupt_lines, predictions_lines, return_mistakes=True, selected_lines=selected_lines) print("###############################################") print("total token count: {}".format(corr2corr + corr2incorr + incorr2corr + incorr2incorr)) print( f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr + incorr2corr) / (corr2corr + corr2incorr + incorr2corr + incorr2incorr)}") print(f"word correction rate is {(incorr2corr) / (incorr2corr + incorr2incorr)}") print("###############################################") return results
[]
2024-01-10
cezaryborowski/neuspell-config
scripts~trainable~seq_modeling~sclstmelmo.py
############################################# # USAGE # CUDA_VISIBLE_DEVICES=0 python sclstmelmo.py probword ../../data -1 # # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py none ../../data 1 # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py random ../../data 1 # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py word ../../data 1 # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py prob ../../data 1 # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py probword ../../data 1 # CUDA_VISIBLE_DEVICES=1 python sclstmelmo.py probword_v2 ../../data 1 ############################################# ############################################ # TO-DO # ---- # 1. How to set multip-gpu in torch for training ############################################ import os, sys # export CUDA_VISIBLE_DEVICES=1,2 && echo $CUDA_VISIBLE_DEVICES # os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # os.environ["CUDA_VISIBLE_DEVICES"] = "2" sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/.") from tqdm import tqdm import numpy as np import re import time from typing import List import torch from torch import nn from torch.nn.utils.rnn import pad_sequence from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence import torch.nn.functional as F # print(torch.cuda.current_device()) # torch.cuda.set_device(1) # print(torch.cuda.current_device()) # DEVICE = torch.device('cuda') if torch.cuda.is_available() else "cpu" DEVICE = 'cuda:0' if torch.cuda.is_available() else "cpu" from helpers import progressBar from helpers import load_vocab_dict, save_vocab_dict from helpers import load_data, train_validation_split, get_char_tokens, get_tokens, num_unk_tokens from helpers import batch_iter, labelize, tokenize, char_tokenize, sclstm_tokenize from helpers import untokenize, untokenize_without_unks, untokenize_without_unks2, untokenize_without_unks3, get_model_nparams from helpers import batch_accuracy_func from helpers2 import get_line_representation, get_lines from models import ElmoSCLSTM from allennlp.modules.elmo import batch_to_ids as elmo_batch_to_ids from evals import get_metrics """ NEW: reranking snippets """ # (GPT/GPT-2/CTRL/Transformer-XL/XLNet) import torch from torch.nn import CrossEntropyLoss HFACE_BATCH_SIZE = 8 # from transformers import OpenAIGPTTokenizer, OpenAIGPTLMHeadModel # gpt2Tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt') # gpt2LMHeadModel = OpenAIGPTLMHeadModel.from_pretrained('openai-gpt') # gpt2Tokenizer.add_special_tokens({'pad_token':"[PAD]"}) # gpt2LMHeadModel.resize_token_embeddings(len(gpt2Tokenizer)) # assert gpt2Tokenizer.pad_token == '[PAD]' from transformers import GPT2Tokenizer, GPT2LMHeadModel gpt2Tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium') gpt2LMHeadModel = GPT2LMHeadModel.from_pretrained('gpt2-medium') gpt2Tokenizer.pad_token = gpt2Tokenizer.eos_token # from transformers import TransfoXLTokenizer, TransfoXLLMHeadModel # txlTokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') # txlLMHeadModel = TransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103') # txlTokenizer.pad_token = txlTokenizer.eos_token def get_losses_from_gpt_lm(this_sents: "list[str]", gpt2LMHeadModel, gpt2Tokenizer, DEVICE): this_input_ids = gpt2Tokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True)["input_ids"] this_labels = torch.tensor([[i if i!=gpt2Tokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = gpt2LMHeadModel(input_ids=this_input_ids) this_lm_logits = this_outputs[0] # Shift so that tokens < n predict n shift_logits2 = this_lm_logits[:, :-1, :] shift_labels2 = this_labels[:, 1:] # Flatten the tokens loss_fct = CrossEntropyLoss(reduction='none') loss = loss_fct(shift_logits2.permute(0,2,1), shift_labels2) losses = loss.sum(dim=-1).cpu().detach().numpy().tolist() return losses def get_losses_from_txl_lm(this_sents: "list[str]", txlLMHeadModel, txlTokenizer, DEVICE): this_input_ids_dict = txlTokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True) this_input_ids = this_input_ids_dict["input_ids"] chunks = [sum(val) for val in this_input_ids_dict["attention_mask"]] chunks_cumsum = np.cumsum(chunks).tolist() this_labels = torch.tensor([[i if i!=txlTokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = txlLMHeadModel(input_ids=this_input_ids,labels=this_labels) this_loss = this_outputs[0] this_loss = this_loss.view(-1).cpu().detach().numpy() losses = [sum(this_loss[str_pos:end_pos-1]) for str_pos,end_pos in zip([0]+chunks_cumsum[:-1],chunks_cumsum)] return losses def load_model(vocab, verbose=False): model = ElmoSCLSTM(3*len(vocab["chartoken2idx"]),vocab["token2idx"][ vocab["pad_token"] ],len(vocab["token_freq"]),early_concat=False) if verbose: print(model) print( get_model_nparams(model) ) return model def load_pretrained(model, CHECKPOINT_PATH, optimizer=None, device='cuda'): if torch.cuda.is_available() and device != "cpu": map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' print(f"Loading model params from checkpoint dir: {CHECKPOINT_PATH}") checkpoint_data = torch.load(os.path.join(CHECKPOINT_PATH, "model.pth.tar"), map_location=map_location) # print(f"previously model saved at : {checkpoint_data['epoch_id']}") model.load_state_dict(checkpoint_data['model_state_dict']) if optimizer is not None: optimizer.load_state_dict(checkpoint_data['optimizer_state_dict']) max_dev_acc, argmax_dev_acc = checkpoint_data["max_dev_acc"], checkpoint_data["argmax_dev_acc"] print(f"previously, max_dev_acc: {max_dev_acc:.5f} and argmax_dev_acc: {argmax_dev_acc:.5f}") if optimizer is not None: return model, optimizer, max_dev_acc, argmax_dev_acc return model def model_predictions(model, data, vocab, DEVICE, BATCH_SIZE=16, backoff="pass-through"): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) """ topk = 1 # print("###############################################") # inference_st_time = time.time() final_sentences = [] VALID_BATCH_SIZE = BATCH_SIZE # print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in enumerate(data_iter): # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward with torch.no_grad(): """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) """ _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) final_sentences.extend(batch_predictions) # print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) return final_sentences def model_inference(model, data, topk, DEVICE, BATCH_SIZE=16, beam_search=False, selected_lines_file=None, vocab_=None): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) topk: how many of the topk softmax predictions are considered for metrics calculations DEVICE: "cuda:0" or "cpu" BATCH_SIZE: batch size for input to the model beam_search: if True, greedy topk will not be performed """ if vocab_ is not None: vocab = vocab_ if beam_search: if topk<2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") if selected_lines_file is not None: raise Exception("when using beam_search, ***selected_lines_file*** arg is not used; no implementation") # list of dicts with keys {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} results = [] line_index = 0 inference_st_time = time.time() VALID_BATCH_SIZE = BATCH_SIZE valid_loss, valid_acc = 0., 0. corr2corr, corr2incorr, incorr2corr, incorr2incorr = 0, 0, 0, 0 predictions = [] print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in tqdm(enumerate(data_iter)): torch.cuda.empty_cache() # st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ batch_loss, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ batch_loss, batch_predictions, batch_predictions_probs = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print(f"batch_idxs:{len(batch_idxs)},batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() # based on beam_search, do either greedy topk or beam search for topk if not beam_search: # based on topk, obtain either strings of batch_predictions or list of tokens if topk==1: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) else: batch_predictions = untokenize_without_unks2(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) predictions.extend(batch_predictions) # batch_clean_sentences = [line.lower() for line in batch_clean_sentences] # batch_corrupt_sentences = [line.lower() for line in batch_corrupt_sentences] # batch_predictions = [line.lower() for line in batch_predictions] corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences,batch_corrupt_sentences,batch_predictions,check_until_topk=topk,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ for i, (a,b,c) in enumerate(zip(batch_clean_sentences,batch_corrupt_sentences,batch_predictions)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":[], "topk_prediction_probs":[], "topk_reranker_losses":[]}) line_index += len(batch_clean_sentences) else: """ NEW: use untokenize_without_unks3 for beam search outputs """ # k different lists each of type batch_predictions as in topk==1 # List[List[Strings]] k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_clean_sentences, topk) ########################################################## ############## this takes top1 as-is ##################### # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,k_batch_predictions[0],check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## ############### this does reranking ###################### gpt2LMHeadModel.to(DEVICE) gpt2LMHeadModel.eval() # txlLMHeadModel.to(DEVICE) # txlLMHeadModel.eval() reranked_batch_predictions = [] batch_clean_sentences_ = [] batch_corrupt_sentences_ = [] batch_losses_ = [] with torch.no_grad(): for b in range(len(batch_clean_sentences)): losses = [] this_sents = [k_batch_predictions[k][b] for k in range(topk)] losses = get_losses_from_gpt_lm(this_sents, gpt2LMHeadModel, gpt2Tokenizer, DEVICE) # losses = get_losses_from_txl_lm(this_sents, txlLMHeadModel, txlTokenizer, DEVICE) kmin = np.argmin(losses) reranked_batch_predictions.append(k_batch_predictions[kmin][b]) batch_clean_sentences_.append(batch_clean_sentences[b]) batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) batch_losses_.append(losses) corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ batch_predictions_k = [[k_batch_predictions[j][i] for j in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] batch_predictions_probs_k = [[k_batch_predictions_probs[j][i] for j in range(len(k_batch_predictions_probs))] for i in range(len(k_batch_predictions_probs[0]))] for i, (a,b,c,d,e,f) in \ enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k,batch_predictions_probs_k,batch_losses_)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":d, "topk_prediction_probs":e, "topk_reranker_losses":f}) line_index += len(batch_clean_sentences) # delete del batch_loss del batch_predictions del batch_labels, batch_lengths, batch_idxs, batch_lengths_, batch_elmo_inp torch.cuda.empty_cache() # ''' # # update progress # progressBar(batch_id+1, # int(np.ceil(len(data) / VALID_BATCH_SIZE)), # ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], # [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),None,None]) # ''' print(f"\nEpoch {None} valid_loss: {valid_loss/(batch_id+1)}") print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") if not beam_search and selected_lines_file is not None: print("evaluating only for selected lines ... ") assert len(data)==len(predictions), print(len(data),len(predictions),"lengths mismatch") if selected_lines_file is not None: selected_lines = {num:"" for num in [int(line.strip()) for line in open(selected_lines_file,'r')]} else: selected_lines = None clean_lines, corrupt_lines,predictions_lines = [tpl[0] for tpl in data], [tpl[1] for tpl in data], predictions corr2corr, corr2incorr, incorr2corr, incorr2incorr, mistakes = \ get_metrics(clean_lines,corrupt_lines,predictions_lines,return_mistakes=True,selected_lines=selected_lines) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") return results if __name__=="__main__": print("#########################"+"\n") # "word", "prob", "probword", 'random', bea40kfinetune', 'moviereviewsfinetune' TRAIN_NOISE_TYPE = sys.argv[1] # "../../data" BASE_PATH = sys.argv[2] # -ve value for inference only; 1 for training a new model from scratch; >1 for continuing training START_EPOCH = int(sys.argv[3]) if START_EPOCH==0: raise Exception("START_EPOCH must be a non-zero value; If starting from scratch, use 1 instead of 0") # :NEW: finetune now from a specific epoch of a model # "probword" if len(sys.argv)>4: FINETUNE = sys.argv[4] if FINETUNE=='probword': SRC_CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-probwordnoise") SRC_VOCAB_PATH = os.path.join(SRC_CHECKPOINT_PATH,"vocab.pkl") print(f"Model finetuning with arg: {FINETUNE}, and source model selected from: {SRC_CHECKPOINT_PATH}") else: raise Exception("only ```probword``` is now supported for finetuning") assert os.path.exists(SRC_CHECKPOINT_PATH), print(f"{SRC_CHECKPOINT_PATH} path unavailable") else: FINETUNE = "" ############################################# # environment ############################################# # checkpoint path for this model if TRAIN_NOISE_TYPE=="word": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-wordnoise") elif TRAIN_NOISE_TYPE=="prob": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-probnoise") elif TRAIN_NOISE_TYPE=="random": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-randomnoise") elif TRAIN_NOISE_TYPE=="probword": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-probwordnoise") elif TRAIN_NOISE_TYPE=="bea40kfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-probwordnoise-bea40kfinetune") elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-probwordnoise-moviereviewsfinetune2") elif TRAIN_NOISE_TYPE=="none": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/scrnnelmo-none") else: raise Exception("invalid TRAIN_NOISE_TYPE") if not os.path.exists(CHECKPOINT_PATH): os.makedirs(CHECKPOINT_PATH) VOCAB_PATH = os.path.join(CHECKPOINT_PATH,"vocab.pkl") # settings print("#########################"+"\n") START_EPOCH, N_EPOCHS = START_EPOCH, 50 TRAIN_BATCH_SIZE, VALID_BATCH_SIZE = 32, 32 # 16, 16 ############################################# # load train data (if required) ############################################# TRAIN_TEST_FILE_PATH = os.path.join(BASE_PATH, "traintest/") if START_EPOCH>0: if FINETUNE!="": print("loading vocab for finetuning") print(f"loading vocab from {SRC_VOCAB_PATH}") vocab = load_vocab_dict(SRC_VOCAB_PATH) save_vocab_dict(VOCAB_PATH, vocab) # load traintest data if TRAIN_NOISE_TYPE=="bea40kfinetune": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.bea40k", "train.bea40k.noise") train_data, valid_data = train_validation_split(train_data, 0.90, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": # train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] # valid_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "valid.moviereviews")) valid_data_noise1 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) valid_data_noise2 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) valid_data_noise3 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) valid_data_noise4 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) valid_data_noise5 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) valid_data_noise = valid_data_noise1+valid_data_noise2+valid_data_noise3+valid_data_noise4+valid_data_noise5 valid_data_clean = valid_data_clean*5 valid_data = [(a,b) for a,b in zip(valid_data_clean,valid_data_noise)] print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE in finetuning") else: # load traintest data if TRAIN_NOISE_TYPE=="word": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="prob": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="random": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.random") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="probword": train_data1 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data1, valid_data1 = train_validation_split(train_data1, 0.8, seed=11690) print(len(train_data1),len(valid_data1)) train_data2 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data2, valid_data2 = train_validation_split(train_data2, 0.8, seed=11690) print(len(train_data2),len(valid_data2)) train_data = train_data1+train_data2 valid_data = valid_data1+valid_data2 print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="none": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE") ############################################# # load vocab ############################################# if START_EPOCH!=1: # if not training from scratch or for inference print(f"loading vocab from {VOCAB_PATH}") vocab = load_vocab_dict(VOCAB_PATH) else: # load a vocab for reference vocab_ref = {} # opfile = open(os.path.join(BASE_PATH, "vocab/phonemedataset.txt"),"r") # for line in opfile: vocab_ref.update( {line.strip():0} ) # opfile.close() print(f"loading vocab from train data itself and saving it at {VOCAB_PATH}") vocab = get_tokens([i[0] for i in train_data], keep_simple=True, min_max_freq=(2,float("inf")), topk=100000, intersect=vocab_ref, load_char_tokens=True) save_vocab_dict(VOCAB_PATH, vocab) if START_EPOCH>0: # see how many tokens in labels are going to be UNK print ( num_unk_tokens([i[0] for i in train_data], vocab) ) print ( num_unk_tokens([i[0] for i in valid_data], vocab) ) print("") print([*vocab.keys()]) #print(vocab["token_freq"]) #print([(idx,vocab["idx2token"][idx]) for idx in range(100)]) ############################################# # load ElmoSCLSTM ############################################# model = load_model(vocab, verbose=False) ############################################# # training or inference ??! ############################################# if START_EPOCH>0: ############################################# # training and validation ############################################# # running stats max_dev_acc, argmax_dev_acc = -1, -1 patience = 100 # Create an optimizer optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # model to device model.to(DEVICE) # load parameters if not training from scratch if START_EPOCH>1: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,f"progress_retrain_from_epoch{START_EPOCH}.txt"),'w') # model and optimizer load_state_dict if FINETUNE!="": print("loading pretrained weights for finetuning") print(f"loading pretrained weights from {SRC_CHECKPOINT_PATH}") model, optimizer, _, _ = load_pretrained(model, SRC_CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {SRC_CHECKPOINT_PATH}\n") else: print(f"loading pretrained weights from {CHECKPOINT_PATH}") model, optimizer, max_dev_acc, argmax_dev_acc = load_pretrained(model, CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {CHECKPOINT_PATH}\n") else: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,"progress.txt"),'w') print(f"Training model params from scratch") progress_write_file.write(f"Training model params from scratch\n") progress_write_file.flush() # train and eval for epoch_id in range(START_EPOCH,N_EPOCHS+1): # check for patience if (epoch_id-argmax_dev_acc)>patience: print("patience count reached. early stopping initiated") print("max_dev_acc: {}, argmax_dev_acc: {}".format(max_dev_acc, argmax_dev_acc)) break # if finetuning and the noise type is moviereviews, # create a different train data every epoch if TRAIN_NOISE_TYPE=="moviereviewsfinetune": train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] print(f"new training instances created, train data size now: {len(train_data)}") # print epoch print(f"In epoch: {epoch_id}") progress_write_file.write(f"In epoch: {epoch_id}\n") progress_write_file.flush() # train loss and backprop train_loss = 0. train_acc = 0. train_acc_count = 0. print("train_data size: {}".format(len(train_data))) progress_write_file.write("train_data size: {}\n".format(len(train_data))) progress_write_file.flush() train_data_iter = batch_iter(train_data, batch_size=TRAIN_BATCH_SIZE, shuffle=True) #for batch_id, (batch_labels,batch_sentences) in tqdm(enumerate(train_data_iter)): for batch_id, (batch_labels,batch_sentences) in enumerate(train_data_iter): optimizer.zero_grad() st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.train() loss = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) batch_loss = loss.cpu().detach().numpy() train_loss += batch_loss # backward loss.backward() optimizer.step() # compute accuracy in numpy if batch_id%1000==0: train_acc_count += 1 model.eval() with torch.no_grad(): _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) model.train() batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal train_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,train_loss/(batch_id+1),batch_acc,train_acc/train_acc_count]) if batch_id==0 or (batch_id+1)%5000==0: nb = int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)) progress_write_file.write(f"{batch_id+1}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {train_loss/(batch_id+1)}, avg_batch_acc: {train_acc/train_acc_count}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} train_loss: {train_loss/(batch_id+1)}") try: # valid loss valid_loss = 0. valid_acc = 0. print("valid_data size: {}".format(len(valid_data))) progress_write_file.write("valid_data size: {}\n".format(len(valid_data))) progress_write_file.flush() valid_data_iter = batch_iter(valid_data, batch_size=VALID_BATCH_SIZE, shuffle=False) for batch_id, (batch_labels,batch_sentences) in enumerate(valid_data_iter): st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.eval() with torch.no_grad(): batch_loss, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) model.train() valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal valid_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),batch_acc,valid_acc/(batch_id+1)]) if batch_id==0 or (batch_id+1)%2000==0: nb = int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)) progress_write_file.write(f"{batch_id}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {valid_loss/(batch_id+1)}, avg_batch_acc: {valid_acc/(batch_id+1)}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} valid_loss: {valid_loss/(batch_id+1)}") # save model, optimizer and test_predictions if val_acc is improved if valid_acc>=max_dev_acc: # to file #name = "model-epoch{}.pth.tar".format(epoch_id) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(CHECKPOINT_PATH,name)) print("Model saved at {} in epoch {}".format(os.path.join(CHECKPOINT_PATH,name),epoch_id)) # re-assign max_dev_acc, argmax_dev_acc = valid_acc, epoch_id except Exception as e: temp_folder = os.path.join(CHECKPOINT_PATH,"temp") if not os.path.exists(temp_folder): os.makedirs(temp_folder) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(temp_folder,name)) print("Model saved at {} in epoch {}".format(os.path.join(temp_folder,name),epoch_id)) raise Exception(e) else: ############################################# # inference ############################################# # load parameters model = load_pretrained(model, CHECKPOINT_PATH) # infer TRAIN_TEST_FILE_PATH1 = os.path.join(BASE_PATH, "traintest") TRAIN_TEST_FILE_PATH2 = os.path.join(BASE_PATH, "traintest/wo_context") ''' paths = [TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] files1 = ["combined_data","aspell_big","aspell_small"] files2 = ["combined_data.noise","aspell_big.noise","aspell_small.noise"] INFER_BATCH_SIZE = 1024 for x,y,z in zip(paths,files1,files2): print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE) ''' ''' paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1] files1 = ["test.jfleg","test.bea4k","test.bea60k"] files2 = ["test.jfleg.noise","test.bea4k.noise","test.bea60k.noise"] INFER_BATCH_SIZE = 8 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) ''' ''' paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm", "test.1blm", "test.1blm"] files2 = ["test.1blm.noise.random", "test.1blm.noise.prob", "test.1blm.noise.word"] INFER_BATCH_SIZE = 20 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) ''' ''' paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k.ambiguous_natural_v7", "test.bea60k.ambiguous_natural_v8"] files2 = ["test.bea60k.ambiguous_natural_v7.noise", "test.bea60k.ambiguous_natural_v8.noise"] INFER_BATCH_SIZE = 8 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) ''' # ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k"] files2 = ["test.bea60k.noise"] INFER_BATCH_SIZE = 8 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] # files1 = ["test.bea60k","test.1blm","test.1blm","combined_data","aspell_big","aspell_small"] # files2 = ["test.bea60k.noise","test.1blm.noise.prob","test.1blm.noise.word","combined_data.noise","aspell_big.noise","aspell_small.noise"] # INFER_BATCH_SIZE = 16 # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] # files1 = ["combined_data","aspell_big","aspell_small"] # files2 = ["combined_data.noise","aspell_big.noise","aspell_small.noise"] # INFER_BATCH_SIZE = 1024 # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.1blm","test.1blm"] # files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] # INFER_BATCH_SIZE = 64 # 128 # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.1blm"] # files2 = ["test.1blm.noise.prob"] # INFER_BATCH_SIZE = 32 #64 #128 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_probnoise" # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.1blm"] # files2 = ["test.1blm.noise.word"] # INFER_BATCH_SIZE = 32 #64 #128 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_wordnoise" # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] # files1 = ["test.1blm","test.1blm"] # files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] # INFER_BATCH_SIZE = 32 # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.bea4k",] # files2 = ["test.bea4k.noise"] # INFER_BATCH_SIZE = 16 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea4k" # selected_lines_file = None # "../gec-pseudodata/test.bea4k.lines.txt" # None # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.bea60k"] # files2 = ["test.bea60k.noise"] # INFER_BATCH_SIZE = 10 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea60k" # selected_lines_file = None # "../gec-pseudodata/test.bea60k.lines.txt" # None # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.bea20k"] # files2 = ["test.bea20k.noise"] # INFER_BATCH_SIZE = 10 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea20k" # selected_lines_file = None # "../gec-pseudodata/test.bea20k.lines.txt" # None # ''' # ''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.jfleg"] # files2 = ["test.jfleg.noise"] # INFER_BATCH_SIZE = 32 # ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_jfleg" # selected_lines_file = None # "../gec-pseudodata/test.jfleg.lines.txt" # None # ''' # # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} # for x,y,z in zip(paths,files1,files2): # print("\n\n\n\n") # print(x,y,z) # test_data = load_data(x,y,z) # print ( num_unk_tokens([i[0] for i in test_data], vocab) ) # greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) # ANALYSIS_DIR = os.path.join("scrnnelmo",ANALYSIS_DIR) # if not os.path.exists(ANALYSIS_DIR): # os.makedirs(ANALYSIS_DIR) # import jsonlines # # # print("greedy...") # greedy_lines_fully_correct = {line["id"]:"" for line in greedy_results if line["original"]==line["predicted"]} # greedy_lines_otherwise = {line["id"]:"" for line in greedy_results if line["original"]!=line["predicted"]} # print(f'# Lines Predicted Fully Correct: {len(greedy_lines_fully_correct)}') # print(f'# Lines Otherwise: {len(greedy_lines_otherwise)}') # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results.jsonl"),'w') # for line in greedy_results: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_corr_preds.jsonl"),'w') # for line in [line for line in greedy_results if line["original"]==line["predicted"]]: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_incorr_preds.jsonl"),'w') # for line in [line for line in greedy_results if line["original"]!=line["predicted"]]: opfile.write(line) # opfile.close() # # print("beam_search...") # beam_search_lines_fully_correct = {line["id"]:"" for line in beam_search_results if line["original"]==line["predicted"]} # beam_search_lines_otherwise = {line["id"]:"" for line in beam_search_results if line["original"]!=line["predicted"]} # print(f'# Lines Predicted Fully Correct: {len(beam_search_lines_fully_correct)}') # print(f'# Lines Otherwise: {len(beam_search_lines_otherwise)}') # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results.jsonl"),'w') # for line in beam_search_results: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_corr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]==line["predicted"]]: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_incorr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]!=line["predicted"]]: opfile.write(line) # opfile.close() # # # # confusion matrix # corr2corr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_fully_correct]) # corr2incorr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_otherwise]) # incorr2corr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_fully_correct]) # incorr2incorr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_otherwise]) # print("Confusion Matrix for before and after beam search: ") # print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") ######################################### # reranking snippets from past ######################################### # if save_dir is not None: # line_index = 0 # analysis_path = save_dir # if not os.path.exists(analysis_path): # os.makedirs(analysis_path) # if beam_search: # line_index_wrong_opfile = open(f"./{analysis_path}/beam_search_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/beam_search_right.txt","w") # k_wrong_opfile = open(f"./{analysis_path}/beam_search_k_wrong.txt","w") # k_right_opfile = open(f"./{analysis_path}/beam_search_k_right.txt","w") # else: # line_index_wrong_opfile = open(f"./{analysis_path}/greedy_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/greedy_right.txt","w") # reranked_batch_predictions = [] # batch_clean_sentences_ = [] # batch_corrupt_sentences_ = [] # with torch.no_grad(): # for b in range(len(batch_clean_sentences)): # try: # losses = [] # for sent in [k_batch_predictions[k][b] for k in range(topk)]: # if sent!="" or sent is not None: # input_ids = torch.tensor(gpt2Tokenizer.encode(sent, add_special_tokens=True)).unsqueeze(0) # Batch size 1 # input_ids = input_ids.to(DEVICE) # outputs = gpt2LMHeadModel(input_ids, labels=input_ids) # loss = outputs[0].item() # else: # loss = 10000.0 # losses.append(loss) # kmin = np.argmin(losses) # reranked_batch_predictions.append(k_batch_predictions[kmin][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # except Exception as e: # reranked_batch_predictions.append(k_batch_predictions[0][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ # this_batch = [[k_batch_predictions[k][i] for k in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] # flat_batch = sum(this_batch,[]); # print(flat_batch); print(len(flat_batch)) # lens = [len(s) for s in this_batch] # ii = 0 # flat_losses = [] # model.eval() # model.to(DEVICE) # with torch.no_grad(): # while ii<len(flat_batch): # try: # curr_batch = flat_batch[ii:ii+HFACE_BATCH_SIZE] # curr_inputs = gpt2Tokenizer.batch_encode_plus(curr_batch,pad_to_max_length=True) # curr_inputs_ids = curr_inputs["input_ids"] # curr_inputs = {k:torch.tensor(v).to(DEVICE) for k,v in curr_inputs.items()} # curr_outputs = gpt2LMHeadModel(input_ids=curr_inputs["input_ids"],token_type_ids=curr_inputs["token_type_ids"],attention_mask=curr_inputs["attention_mask"]) # lm_logits = curr_outputs[0] # labels = torch.tensor([[i if i!=50256 else -100 for i in row] for row in curr_inputs_ids]).to(DEVICE) # # Shift so that tokens < n predict n # shift_logits = lm_logits[..., :-1, :].contiguous(); # print(shift_logits.shape) # shift_labels = labels[..., 1:].contiguous(); # print(shift_labels.shape) # # Flatten the tokens # loss_fct = CrossEntropyLoss(reduction='none') # loss = loss_fct(shift_logits.permute(0, 2, 1), shift_labels) # flat_losses.extend(loss.sum(axis=-1).cpu().detach().numpy().tolist()) # ii += HFACE_BATCH_SIZE # except Exception as e: # # print(this_batch) # raise Exception(e) # offset = 0 # batch_losses = [] # for val in lens: # batch_losses.append(flat_losses[offset:offset+val]) # offset += val # print(np.array(batch_losses)) # reranked_batch_predictions = [k_batch_predictions[np.argmin(batch_losses[i])][i] for i in range(len(batch_losses))] # print(batch_clean_sentences) # print("") # print(reranked_batch_predictions) # raise Exception("debug...") # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## # for i, (a,b,c,d) in enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k)): # if a==c: # right # line_index_right_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # else: # line_index_wrong_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # line_index+=len(batch_clean_sentences_) # line_index_right_opfile.flush() # line_index_wrong_opfile.flush() # __mistakes = [] # __inds = [] # for i in range(len(batch_clean_sentences)): # if batch_clean_sentences[i].strip()!=k_batch_predictions[0][i].strip(): # __mistakes.append(f"{batch_clean_sentences[i]}\n") # __inds.append(i) # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions_probs[ind]:.4f}\t" # ii+=1 # batch_predictions = k_batch_predictions[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions[ind]}\n" # ii+=1 # ii=0 # for i,_ in enumerate(batch_clean_sentences): # if i in __inds: # __mistakes[ii]+="\n" # ii+=1 # for mis in __mistakes: # k_wrong_opfile.write(mis) # __predictions = [] # for sent in batch_clean_sentences: # __predictions.append(f"{sent}\n") # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # for i,val in enumerate(batch_predictions_probs): # __predictions[i]+=f"{val:.4f}\t" # batch_predictions = k_batch_predictions[k] # for i,sent in enumerate(batch_predictions): # __predictions[i]+=f"{sent}\n" # for i,_ in enumerate(batch_clean_sentences): # __predictions[i]+="\n" # for pred in __predictions: # k_right_opfile.write(pred) # if beam_search: # line_index_right_opfile.close() # line_index_wrong_opfile.close() # k_wrong_opfile.close() # k_right_opfile.close() # else: # line_index_right_opfile.close() # line_index_wrong_opfile.close()
[]
2024-01-10
cezaryborowski/neuspell-config
scripts~trainable~seq_modeling~subwordelmo.py
############################################# # USAGE # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py probword ../../data -1 # # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py none ../../data 1 # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py random ../../data 1 # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py word ../../data 1 # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py prob ../../data 1 # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py probword ../../data 1 # CUDA_VISIBLE_DEVICES=1 python subwordelmo.py probword_v2 ../../data 1 ############################################# ############################################ # TO-DO # ---- # 1. How to set multip-gpu in torch for training ############################################ import os, sys # export CUDA_VISIBLE_DEVICES=1,2 && echo $CUDA_VISIBLE_DEVICES # os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # os.environ["CUDA_VISIBLE_DEVICES"] = "2" sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/.") from tqdm import tqdm import numpy as np # import re import time # from typing import List import torch # from torch import nn # from torch.nn.utils.rnn import pad_sequence # from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence # import torch.nn.functional as F # print(torch.cuda.current_device()) # torch.cuda.set_device(1) # print(torch.cuda.current_device()) # DEVICE = torch.device('cuda') if torch.cuda.is_available() else "cpu" DEVICE = 'cuda:0' if torch.cuda.is_available() else "cpu" from helpers import progressBar from helpers import load_vocab_dict, save_vocab_dict from helpers import load_data, train_validation_split, get_tokens, num_unk_tokens #,get_char_tokens from helpers import batch_iter, labelize #, tokenize, char_tokenize, sclstm_tokenize from helpers import untokenize_without_unks, untokenize_without_unks2, untokenize_without_unks3, get_model_nparams #, untokenize from helpers import batch_accuracy_func from helpers2 import get_line_representation, get_lines from models import SubwordElmo from allennlp.modules.elmo import batch_to_ids as elmo_batch_to_ids from evals import get_metrics """ NEW: reranking snippets """ # (GPT/GPT-2/CTRL/Transformer-XL/XLNet) import torch from torch.nn import CrossEntropyLoss HFACE_BATCH_SIZE = 8 # from transformers import OpenAIGPTTokenizer, OpenAIGPTLMHeadModel # gpt2Tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt') # gpt2LMHeadModel = OpenAIGPTLMHeadModel.from_pretrained('openai-gpt') # gpt2Tokenizer.add_special_tokens({'pad_token':"[PAD]"}) # gpt2LMHeadModel.resize_token_embeddings(len(gpt2Tokenizer)) # assert gpt2Tokenizer.pad_token == '[PAD]' from transformers import GPT2Tokenizer, GPT2LMHeadModel gpt2Tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium') gpt2LMHeadModel = GPT2LMHeadModel.from_pretrained('gpt2-medium') gpt2Tokenizer.pad_token = gpt2Tokenizer.eos_token # from transformers import TransfoXLTokenizer, TransfoXLLMHeadModel # txlTokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') # txlLMHeadModel = TransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103') # txlTokenizer.pad_token = txlTokenizer.eos_token def get_losses_from_gpt_lm(this_sents: "list[str]", gpt2LMHeadModel, gpt2Tokenizer, DEVICE): this_input_ids = gpt2Tokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True)["input_ids"] this_labels = torch.tensor([[i if i!=gpt2Tokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = gpt2LMHeadModel(input_ids=this_input_ids) this_lm_logits = this_outputs[0] # Shift so that tokens < n predict n shift_logits2 = this_lm_logits[:, :-1, :] shift_labels2 = this_labels[:, 1:] # Flatten the tokens loss_fct = CrossEntropyLoss(reduction='none') loss = loss_fct(shift_logits2.permute(0,2,1), shift_labels2) losses = loss.sum(dim=-1).cpu().detach().numpy().tolist() return losses def get_losses_from_txl_lm(this_sents: "list[str]", txlLMHeadModel, txlTokenizer, DEVICE): this_input_ids_dict = txlTokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True) this_input_ids = this_input_ids_dict["input_ids"] chunks = [sum(val) for val in this_input_ids_dict["attention_mask"]] chunks_cumsum = np.cumsum(chunks).tolist() this_labels = torch.tensor([[i if i!=txlTokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = txlLMHeadModel(input_ids=this_input_ids,labels=this_labels) this_loss = this_outputs[0] this_loss = this_loss.view(-1).cpu().detach().numpy() losses = [sum(this_loss[str_pos:end_pos-1]) for str_pos,end_pos in zip([0]+chunks_cumsum[:-1],chunks_cumsum)] return losses def load_model(vocab, verbose=False): model = SubwordElmo(3*len(vocab["chartoken2idx"]),vocab["token2idx"][ vocab["pad_token"] ],len(vocab["token_freq"])) if verbose: print(model) print( get_model_nparams(model) ) return model def load_pretrained(model, CHECKPOINT_PATH, optimizer=None, device='cuda'): if torch.cuda.is_available() and device != "cpu": map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' print(f"Loading model params from checkpoint dir: {CHECKPOINT_PATH}") checkpoint_data = torch.load(os.path.join(CHECKPOINT_PATH, "model.pth.tar"), map_location=map_location) # print(f"previously model saved at : {checkpoint_data['epoch_id']}") model.load_state_dict(checkpoint_data['model_state_dict']) if optimizer is not None: optimizer.load_state_dict(checkpoint_data['optimizer_state_dict']) max_dev_acc, argmax_dev_acc = checkpoint_data["max_dev_acc"], checkpoint_data["argmax_dev_acc"] print(f"previously, max_dev_acc: {max_dev_acc:.5f} and argmax_dev_acc: {argmax_dev_acc:.5f}") if optimizer is not None: return model, optimizer, max_dev_acc, argmax_dev_acc return model # def model_predictions(model, data, vocab, DEVICE, BATCH_SIZE=16, backoff="pass-through"): # """ # model: an instance of SubwordElmo # data: list of tuples, with each tuple consisting of correct and incorrect # sentence string (would be split at whitespaces) # """ # topk = 1 # # print("###############################################") # # inference_st_time = time.time() # final_sentences = [] # VALID_BATCH_SIZE = BATCH_SIZE # # print("data size: {}".format(len(data))) # data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) # model.eval() # model.to(DEVICE) # for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in enumerate(data_iter): # # set batch data # batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) # batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) # assert (batch_lengths_==batch_lengths).all()==True # batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] # batch_lengths = batch_lengths.to(DEVICE) # batch_labels = batch_labels.to(DEVICE) # batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # # forward # with torch.no_grad(): # """ # NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) # """ # _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) # final_sentences.extend(batch_predictions) # # print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) # return final_sentences def model_inference(model, data, topk, DEVICE, BATCH_SIZE=16, beam_search=False, selected_lines_file=None): """ model: an instance of SubwordElmo data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) topk: how many of the topk softmax predictions are considered for metrics calculations DEVICE: "cuda:0" or "cpu" BATCH_SIZE: batch size for input to the model beam_search: if True, greedy topk will not be performed """ if beam_search: if topk<2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") if selected_lines_file is not None: raise Exception("when using beam_search, ***selected_lines_file*** arg is not used; no implementation") # list of dicts with keys {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} results = [] line_index = 0 inference_st_time = time.time() VALID_BATCH_SIZE = BATCH_SIZE valid_loss, valid_acc = 0., 0. corr2corr, corr2incorr, incorr2corr, incorr2incorr = 0, 0, 0, 0 predictions = [] print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in tqdm(enumerate(data_iter)): torch.cuda.empty_cache() # st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) # batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) # assert (batch_lengths_==batch_lengths).all()==True # batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ batch_loss, batch_predictions = model(batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ batch_loss, batch_predictions, batch_predictions_probs = model(batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print(f"batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() # based on beam_search, do either greedy topk or beam search for topk if not beam_search: # based on topk, obtain either strings of batch_predictions or list of tokens if topk==1: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) else: batch_predictions = untokenize_without_unks2(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) predictions.extend(batch_predictions) batch_clean_sentences = [line.lower() for line in batch_clean_sentences] batch_corrupt_sentences = [line.lower() for line in batch_corrupt_sentences] batch_predictions = [line.lower() for line in batch_predictions] corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences,batch_corrupt_sentences,batch_predictions,check_until_topk=topk,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ for i, (a,b,c) in enumerate(zip(batch_clean_sentences,batch_corrupt_sentences,batch_predictions)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":[], "topk_prediction_probs":[], "topk_reranker_losses":[]}) line_index += len(batch_clean_sentences) else: """ NEW: use untokenize_without_unks3 for beam search outputs """ # k different lists each of type batch_predictions as in topk==1 # List[List[Strings]] k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_clean_sentences, topk) ########################################################## ############## this takes top1 as-is ##################### # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,k_batch_predictions[0],check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## ############### this does reranking ###################### gpt2LMHeadModel.to(DEVICE) gpt2LMHeadModel.eval() # txlLMHeadModel.to(DEVICE) # txlLMHeadModel.eval() reranked_batch_predictions = [] batch_clean_sentences_ = [] batch_corrupt_sentences_ = [] batch_losses_ = [] with torch.no_grad(): for b in range(len(batch_clean_sentences)): losses = [] this_sents = [k_batch_predictions[k][b] for k in range(topk)] losses = get_losses_from_gpt_lm(this_sents, gpt2LMHeadModel, gpt2Tokenizer, DEVICE) # losses = get_losses_from_txl_lm(this_sents, txlLMHeadModel, txlTokenizer, DEVICE) kmin = np.argmin(losses) reranked_batch_predictions.append(k_batch_predictions[kmin][b]) batch_clean_sentences_.append(batch_clean_sentences[b]) batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) batch_losses_.append(losses) corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ batch_predictions_k = [[k_batch_predictions[j][i] for j in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] batch_predictions_probs_k = [[k_batch_predictions_probs[j][i] for j in range(len(k_batch_predictions_probs))] for i in range(len(k_batch_predictions_probs[0]))] for i, (a,b,c,d,e,f) in \ enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k,batch_predictions_probs_k,batch_losses_)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":d, "topk_prediction_probs":e, "topk_reranker_losses":f}) line_index += len(batch_clean_sentences) # delete del batch_loss del batch_predictions del batch_labels, batch_lengths, batch_elmo_inp torch.cuda.empty_cache() # ''' # # update progress # progressBar(batch_id+1, # int(np.ceil(len(data) / VALID_BATCH_SIZE)), # ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], # [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),None,None]) # ''' print(f"\nEpoch {None} valid_loss: {valid_loss/(batch_id+1)}") print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") if not beam_search and selected_lines_file is not None: print("evaluating only for selected lines ... ") assert len(data)==len(predictions), print(len(data),len(predictions),"lengths mismatch") if selected_lines_file is not None: selected_lines = {num:"" for num in [int(line.strip()) for line in open(selected_lines_file,'r')]} else: selected_lines = None clean_lines, corrupt_lines,predictions_lines = [tpl[0] for tpl in data], [tpl[1] for tpl in data], predictions corr2corr, corr2incorr, incorr2corr, incorr2incorr, mistakes = \ get_metrics(clean_lines,corrupt_lines,predictions_lines,return_mistakes=True,selected_lines=selected_lines) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") return results if __name__=="__main__": print("#########################"+"\n") # "word", "prob", "probword", 'random', bea40kfinetune', 'moviereviewsfinetune' TRAIN_NOISE_TYPE = sys.argv[1] # "../../data" BASE_PATH = sys.argv[2] # -ve value for inference only; 1 for training a new model from scratch; >1 for continuing training START_EPOCH = int(sys.argv[3]) if START_EPOCH==0: raise Exception("START_EPOCH must be a non-zero value; If starting from scratch, use 1 instead of 0") # :NEW: finetune now from a specific epoch of a model # "probword" if len(sys.argv)>4: FINETUNE = sys.argv[4] if FINETUNE=='probword': SRC_CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-probwordnoise") SRC_VOCAB_PATH = os.path.join(SRC_CHECKPOINT_PATH,"vocab.pkl") print(f"Model finetuning with arg: {FINETUNE}, and source model selected from: {SRC_CHECKPOINT_PATH}") else: raise Exception("only ```probword``` is now supported for finetuning") assert os.path.exists(SRC_CHECKPOINT_PATH), print(f"{SRC_CHECKPOINT_PATH} path unavailable") else: FINETUNE = "" ############################################# # environment ############################################# # checkpoint path for this model if TRAIN_NOISE_TYPE=="word": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-wordnoise") elif TRAIN_NOISE_TYPE=="prob": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-probnoise") elif TRAIN_NOISE_TYPE=="random": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-randomnoise") elif TRAIN_NOISE_TYPE=="probword": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-probwordnoise") elif TRAIN_NOISE_TYPE=="bea40kfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-probwordnoise-bea40kfinetune") elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-probwordnoise-moviereviewsfinetune2") elif TRAIN_NOISE_TYPE=="none": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/subwordelmo-none") else: raise Exception("invalid TRAIN_NOISE_TYPE") if not os.path.exists(CHECKPOINT_PATH): os.makedirs(CHECKPOINT_PATH) VOCAB_PATH = os.path.join(CHECKPOINT_PATH,"vocab.pkl") # settings print("#########################"+"\n") START_EPOCH, N_EPOCHS = START_EPOCH, 50 TRAIN_BATCH_SIZE, VALID_BATCH_SIZE = 32, 32 # 16, 16 ############################################# # load train data (if required) ############################################# TRAIN_TEST_FILE_PATH = os.path.join(BASE_PATH, "traintest/") if START_EPOCH>0: if FINETUNE!="": print("loading vocab for finetuning") print(f"loading vocab from {SRC_VOCAB_PATH}") vocab = load_vocab_dict(SRC_VOCAB_PATH) save_vocab_dict(VOCAB_PATH, vocab) # load traintest data if TRAIN_NOISE_TYPE=="bea40kfinetune": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.bea40k", "train.bea40k.noise") train_data, valid_data = train_validation_split(train_data, 0.90, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": # train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] # valid_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "valid.moviereviews")) valid_data_noise1 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) valid_data_noise2 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) valid_data_noise3 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) valid_data_noise4 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) valid_data_noise5 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) valid_data_noise = valid_data_noise1+valid_data_noise2+valid_data_noise3+valid_data_noise4+valid_data_noise5 valid_data_clean = valid_data_clean*5 valid_data = [(a,b) for a,b in zip(valid_data_clean,valid_data_noise)] print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE in finetuning") else: # load traintest data if TRAIN_NOISE_TYPE=="word": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="prob": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="random": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.random") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="probword": train_data1 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data1, valid_data1 = train_validation_split(train_data1, 0.8, seed=11690) print(len(train_data1),len(valid_data1)) train_data2 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data2, valid_data2 = train_validation_split(train_data2, 0.8, seed=11690) print(len(train_data2),len(valid_data2)) train_data = train_data1+train_data2 valid_data = valid_data1+valid_data2 print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="none": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE") ############################################# # load vocab ############################################# if START_EPOCH!=1: # if not training from scratch or for inference print(f"loading vocab from {VOCAB_PATH}") vocab = load_vocab_dict(VOCAB_PATH) else: # load a vocab for reference vocab_ref = {} # opfile = open(os.path.join(BASE_PATH, "vocab/phonemedataset.txt"),"r") # for line in opfile: vocab_ref.update( {line.strip():0} ) # opfile.close() print(f"loading vocab from train data itself and saving it at {VOCAB_PATH}") vocab = get_tokens([i[0] for i in train_data], keep_simple=True, min_max_freq=(2,float("inf")), topk=100000, intersect=vocab_ref, load_char_tokens=True) save_vocab_dict(VOCAB_PATH, vocab) if START_EPOCH>0: # see how many tokens in labels are going to be UNK print ( num_unk_tokens([i[0] for i in train_data], vocab) ) print ( num_unk_tokens([i[0] for i in valid_data], vocab) ) print("") print([*vocab.keys()]) #print(vocab["token_freq"]) #print([(idx,vocab["idx2token"][idx]) for idx in range(100)]) ############################################# # load SubwordElmo ############################################# model = load_model(vocab, verbose=False) ############################################# # training or inference ??! ############################################# if START_EPOCH>0: ############################################# # training and validation ############################################# # running stats max_dev_acc, argmax_dev_acc = -1, -1 patience = 100 # Create an optimizer optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # model to device model.to(DEVICE) # load parameters if not training from scratch if START_EPOCH>1: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,f"progress_retrain_from_epoch{START_EPOCH}.txt"),'w') # model and optimizer load_state_dict if FINETUNE!="": print("loading pretrained weights for finetuning") print(f"loading pretrained weights from {SRC_CHECKPOINT_PATH}") model, optimizer, _, _ = load_pretrained(model, SRC_CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {SRC_CHECKPOINT_PATH}\n") else: print(f"loading pretrained weights from {CHECKPOINT_PATH}") model, optimizer, max_dev_acc, argmax_dev_acc = load_pretrained(model, CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {CHECKPOINT_PATH}\n") else: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,"progress.txt"),'w') print(f"Training model params from scratch") progress_write_file.write(f"Training model params from scratch\n") progress_write_file.flush() # train and eval for epoch_id in range(START_EPOCH,N_EPOCHS+1): # check for patience if (epoch_id-argmax_dev_acc)>patience: print("patience count reached. early stopping initiated") print("max_dev_acc: {}, argmax_dev_acc: {}".format(max_dev_acc, argmax_dev_acc)) break # if finetuning and the noise type is moviereviews, # create a different train data every epoch if TRAIN_NOISE_TYPE=="moviereviewsfinetune": train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] print(f"new training instances created, train data size now: {len(train_data)}") # print epoch print(f"In epoch: {epoch_id}") progress_write_file.write(f"In epoch: {epoch_id}\n") progress_write_file.flush() # train loss and backprop train_loss = 0. train_acc = 0. train_acc_count = 0. print("train_data size: {}".format(len(train_data))) progress_write_file.write("train_data size: {}\n".format(len(train_data))) progress_write_file.flush() train_data_iter = batch_iter(train_data, batch_size=TRAIN_BATCH_SIZE, shuffle=True) #for batch_id, (batch_labels,batch_sentences) in tqdm(enumerate(train_data_iter)): for batch_id, (batch_labels,batch_sentences) in enumerate(train_data_iter): optimizer.zero_grad() st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) # batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) # assert (batch_lengths_==batch_lengths).all()==True # batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.train() loss = model(batch_elmo_inp, targets=batch_labels) batch_loss = loss.cpu().detach().numpy() train_loss += batch_loss # backward loss.backward() optimizer.step() # compute accuracy in numpy if batch_id%1000==0: train_acc_count += 1 model.eval() with torch.no_grad(): _, batch_predictions = model(batch_elmo_inp, targets=batch_labels) model.train() batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal train_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,train_loss/(batch_id+1),batch_acc,train_acc/train_acc_count]) if batch_id==0 or (batch_id+1)%5000==0: nb = int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)) progress_write_file.write(f"{batch_id+1}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {train_loss/(batch_id+1)}, avg_batch_acc: {train_acc/train_acc_count}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} train_loss: {train_loss/(batch_id+1)}") try: # valid loss valid_loss = 0. valid_acc = 0. print("valid_data size: {}".format(len(valid_data))) progress_write_file.write("valid_data size: {}\n".format(len(valid_data))) progress_write_file.flush() valid_data_iter = batch_iter(valid_data, batch_size=VALID_BATCH_SIZE, shuffle=False) for batch_id, (batch_labels,batch_sentences) in enumerate(valid_data_iter): st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) # batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) # assert (batch_lengths_==batch_lengths).all()==True # batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.eval() with torch.no_grad(): batch_loss, batch_predictions = model(batch_elmo_inp, targets=batch_labels) model.train() valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal valid_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),batch_acc,valid_acc/(batch_id+1)]) if batch_id==0 or (batch_id+1)%2000==0: nb = int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)) progress_write_file.write(f"{batch_id}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {valid_loss/(batch_id+1)}, avg_batch_acc: {valid_acc/(batch_id+1)}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} valid_loss: {valid_loss/(batch_id+1)}") # save model, optimizer and test_predictions if val_acc is improved if valid_acc>=max_dev_acc: # to file #name = "model-epoch{}.pth.tar".format(epoch_id) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(CHECKPOINT_PATH,name)) print("Model saved at {} in epoch {}".format(os.path.join(CHECKPOINT_PATH,name),epoch_id)) # re-assign max_dev_acc, argmax_dev_acc = valid_acc, epoch_id except Exception as e: temp_folder = os.path.join(CHECKPOINT_PATH,"temp") if not os.path.exists(temp_folder): os.makedirs(temp_folder) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(temp_folder,name)) print("Model saved at {} in epoch {}".format(os.path.join(temp_folder,name),epoch_id)) raise Exception(e) else: ############################################# # inference ############################################# # load parameters model = load_pretrained(model, CHECKPOINT_PATH) # infer TRAIN_TEST_FILE_PATH1 = os.path.join(BASE_PATH, "traintest") TRAIN_TEST_FILE_PATH2 = os.path.join(BASE_PATH, "traintest/wo_context") ''' paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1] files1 = ["test.jfleg","test.bea4k","test.bea60k"] files2 = ["test.jfleg.noise","test.bea4k.noise","test.bea60k.noise"] INFER_BATCH_SIZE = 8 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) ''' ''' paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm","test.1blm"] files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] INFER_BATCH_SIZE = 32 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) ''' # ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k.ambiguous_natural_v6.2"] files2 = ["test.bea60k.ambiguous_natural_v6.2.noise"] INFER_BATCH_SIZE = 4 selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) # ''' ''' paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] files1 = ["test.bea60k","test.1blm","test.1blm","combined_data","aspell_big","aspell_small"] files2 = ["test.bea60k.noise","test.1blm.noise.prob","test.1blm.noise.word","combined_data.noise","aspell_big.noise","aspell_small.noise"] INFER_BATCH_SIZE = 16 ''' ''' paths = [TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] files1 = ["combined_data","aspell_big","aspell_small"] files2 = ["combined_data.noise","aspell_big.noise","aspell_small.noise"] INFER_BATCH_SIZE = 1024 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm","test.1blm"] files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] INFER_BATCH_SIZE = 64 # 128 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm"] files2 = ["test.1blm.noise.prob"] INFER_BATCH_SIZE = 32 #64 #128 ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_probnoise" ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm"] files2 = ["test.1blm.noise.word"] INFER_BATCH_SIZE = 32 #64 #128 ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_wordnoise" ''' ''' paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm","test.1blm"] files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] INFER_BATCH_SIZE = 32 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea4k",] files2 = ["test.bea4k.noise"] INFER_BATCH_SIZE = 16 ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea4k" selected_lines_file = None # "../gec-pseudodata/test.bea4k.lines.txt" # None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k"] files2 = ["test.bea60k.noise"] INFER_BATCH_SIZE = 10 ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea60k" selected_lines_file = None # "../gec-pseudodata/test.bea60k.lines.txt" # None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea20k"] files2 = ["test.bea20k.noise"] INFER_BATCH_SIZE = 10 ANALYSIS_DIR = f"./analysis_{TRAIN_NOISE_TYPE}_bea20k" selected_lines_file = None # "../gec-pseudodata/test.bea20k.lines.txt" # None ''' # #''' # paths = [TRAIN_TEST_FILE_PATH1] # files1 = ["test.jfleg"] # files2 = ["test.jfleg.noise"] # INFER_BATCH_SIZE = 32 # ANALYSIS_DIR = None #f"./analysis_{TRAIN_NOISE_TYPE}_jfleg" # selected_lines_file = None # "../gec-pseudodata/test.jfleg.lines.txt" # None # #''' # # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} # for x,y,z in zip(paths,files1,files2): # print("\n\n\n\n") # print(x,y,z) # test_data = load_data(x,y,z) # print ( num_unk_tokens([i[0] for i in test_data], vocab) ) # greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) # ANALYSIS_DIR = os.path.join("subwordelmo",ANALYSIS_DIR) # if not os.path.exists(ANALYSIS_DIR): # os.makedirs(ANALYSIS_DIR) # import jsonlines # # # print("greedy...") # greedy_lines_fully_correct = {line["id"]:"" for line in greedy_results if line["original"]==line["predicted"]} # greedy_lines_otherwise = {line["id"]:"" for line in greedy_results if line["original"]!=line["predicted"]} # print(f'# Lines Predicted Fully Correct: {len(greedy_lines_fully_correct)}') # print(f'# Lines Otherwise: {len(greedy_lines_otherwise)}') # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results.jsonl"),'w') # for line in greedy_results: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_corr_preds.jsonl"),'w') # for line in [line for line in greedy_results if line["original"]==line["predicted"]]: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_incorr_preds.jsonl"),'w') # for line in [line for line in greedy_results if line["original"]!=line["predicted"]]: opfile.write(line) # opfile.close() # # print("beam_search...") # beam_search_lines_fully_correct = {line["id"]:"" for line in beam_search_results if line["original"]==line["predicted"]} # beam_search_lines_otherwise = {line["id"]:"" for line in beam_search_results if line["original"]!=line["predicted"]} # print(f'# Lines Predicted Fully Correct: {len(beam_search_lines_fully_correct)}') # print(f'# Lines Otherwise: {len(beam_search_lines_otherwise)}') # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results.jsonl"),'w') # for line in beam_search_results: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_corr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]==line["predicted"]]: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_incorr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]!=line["predicted"]]: opfile.write(line) # opfile.close() # # # # confusion matrix # corr2corr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_fully_correct]) # corr2incorr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_otherwise]) # incorr2corr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_fully_correct]) # incorr2incorr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_otherwise]) # print("Confusion Matrix for before and after beam search: ") # print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") ######################################### # reranking snippets from past ######################################### # if save_dir is not None: # line_index = 0 # analysis_path = save_dir # if not os.path.exists(analysis_path): # os.makedirs(analysis_path) # if beam_search: # line_index_wrong_opfile = open(f"./{analysis_path}/beam_search_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/beam_search_right.txt","w") # k_wrong_opfile = open(f"./{analysis_path}/beam_search_k_wrong.txt","w") # k_right_opfile = open(f"./{analysis_path}/beam_search_k_right.txt","w") # else: # line_index_wrong_opfile = open(f"./{analysis_path}/greedy_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/greedy_right.txt","w") # reranked_batch_predictions = [] # batch_clean_sentences_ = [] # batch_corrupt_sentences_ = [] # with torch.no_grad(): # for b in range(len(batch_clean_sentences)): # try: # losses = [] # for sent in [k_batch_predictions[k][b] for k in range(topk)]: # if sent!="" or sent is not None: # input_ids = torch.tensor(gpt2Tokenizer.encode(sent, add_special_tokens=True)).unsqueeze(0) # Batch size 1 # input_ids = input_ids.to(DEVICE) # outputs = gpt2LMHeadModel(input_ids, labels=input_ids) # loss = outputs[0].item() # else: # loss = 10000.0 # losses.append(loss) # kmin = np.argmin(losses) # reranked_batch_predictions.append(k_batch_predictions[kmin][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # except Exception as e: # reranked_batch_predictions.append(k_batch_predictions[0][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ # this_batch = [[k_batch_predictions[k][i] for k in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] # flat_batch = sum(this_batch,[]); # print(flat_batch); print(len(flat_batch)) # lens = [len(s) for s in this_batch] # ii = 0 # flat_losses = [] # model.eval() # model.to(DEVICE) # with torch.no_grad(): # while ii<len(flat_batch): # try: # curr_batch = flat_batch[ii:ii+HFACE_BATCH_SIZE] # curr_inputs = gpt2Tokenizer.batch_encode_plus(curr_batch,pad_to_max_length=True) # curr_inputs_ids = curr_inputs["input_ids"] # curr_inputs = {k:torch.tensor(v).to(DEVICE) for k,v in curr_inputs.items()} # curr_outputs = gpt2LMHeadModel(input_ids=curr_inputs["input_ids"],token_type_ids=curr_inputs["token_type_ids"],attention_mask=curr_inputs["attention_mask"]) # lm_logits = curr_outputs[0] # labels = torch.tensor([[i if i!=50256 else -100 for i in row] for row in curr_inputs_ids]).to(DEVICE) # # Shift so that tokens < n predict n # shift_logits = lm_logits[..., :-1, :].contiguous(); # print(shift_logits.shape) # shift_labels = labels[..., 1:].contiguous(); # print(shift_labels.shape) # # Flatten the tokens # loss_fct = CrossEntropyLoss(reduction='none') # loss = loss_fct(shift_logits.permute(0, 2, 1), shift_labels) # flat_losses.extend(loss.sum(axis=-1).cpu().detach().numpy().tolist()) # ii += HFACE_BATCH_SIZE # except Exception as e: # # print(this_batch) # raise Exception(e) # offset = 0 # batch_losses = [] # for val in lens: # batch_losses.append(flat_losses[offset:offset+val]) # offset += val # print(np.array(batch_losses)) # reranked_batch_predictions = [k_batch_predictions[np.argmin(batch_losses[i])][i] for i in range(len(batch_losses))] # print(batch_clean_sentences) # print("") # print(reranked_batch_predictions) # raise Exception("debug...") # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## # for i, (a,b,c,d) in enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k)): # if a==c: # right # line_index_right_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # else: # line_index_wrong_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # line_index+=len(batch_clean_sentences_) # line_index_right_opfile.flush() # line_index_wrong_opfile.flush() # __mistakes = [] # __inds = [] # for i in range(len(batch_clean_sentences)): # if batch_clean_sentences[i].strip()!=k_batch_predictions[0][i].strip(): # __mistakes.append(f"{batch_clean_sentences[i]}\n") # __inds.append(i) # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions_probs[ind]:.4f}\t" # ii+=1 # batch_predictions = k_batch_predictions[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions[ind]}\n" # ii+=1 # ii=0 # for i,_ in enumerate(batch_clean_sentences): # if i in __inds: # __mistakes[ii]+="\n" # ii+=1 # for mis in __mistakes: # k_wrong_opfile.write(mis) # __predictions = [] # for sent in batch_clean_sentences: # __predictions.append(f"{sent}\n") # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # for i,val in enumerate(batch_predictions_probs): # __predictions[i]+=f"{val:.4f}\t" # batch_predictions = k_batch_predictions[k] # for i,sent in enumerate(batch_predictions): # __predictions[i]+=f"{sent}\n" # for i,_ in enumerate(batch_clean_sentences): # __predictions[i]+="\n" # for pred in __predictions: # k_right_opfile.write(pred) # if beam_search: # line_index_right_opfile.close() # line_index_wrong_opfile.close() # k_wrong_opfile.close() # k_right_opfile.close() # else: # line_index_right_opfile.close() # line_index_wrong_opfile.close()
[]
2024-01-10
cezaryborowski/neuspell-config
scripts~trainable~seq_modeling~elmosclstm.py
############################################# # USAGE # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py probword ../../data -1 # CUDA_VISIBLE_DEVICES=2 python elmosclstm.py bea40kfinetune ../../data -1 # # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py none ../../data 1 # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py random ../../data 1 # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py word ../../data 1 # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py prob ../../data 1 # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py probword ../../data 1 # CUDA_VISIBLE_DEVICES=1 python elmosclstm.py probword_v2 ../../data 1 ############################################# ############################################ # TO-DO # ---- # 1. How to set multip-gpu in torch for training ############################################ import os, sys sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/.") # export CUDA_VISIBLE_DEVICES=1,2 && echo $CUDA_VISIBLE_DEVICES # os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # os.environ["CUDA_VISIBLE_DEVICES"] = "2" from tqdm import tqdm import numpy as np import re import time from typing import List import torch from torch import nn from torch.nn.utils.rnn import pad_sequence from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence import torch.nn.functional as F DEVICE = 'cuda:0' if torch.cuda.is_available() else "cpu" # torch.cuda.set_device(1) # print(torch.cuda.current_device()) from helpers import progressBar from helpers import load_vocab_dict, save_vocab_dict from helpers import load_data, train_validation_split, get_char_tokens, get_tokens, num_unk_tokens from helpers import batch_iter, labelize, tokenize, char_tokenize, sclstm_tokenize from helpers import untokenize, untokenize_without_unks, untokenize_without_unks2, untokenize_without_unks3, get_model_nparams from helpers import batch_accuracy_func from helpers2 import get_line_representation, get_lines from models import ElmoSCLSTM from allennlp.modules.elmo import batch_to_ids as elmo_batch_to_ids from evals import get_metrics """ NEW: reranking snippets """ # (GPT/GPT-2/CTRL/Transformer-XL/XLNet) import torch from torch.nn import CrossEntropyLoss HFACE_BATCH_SIZE = 8 RERANKER = "GPT-2" # GPT/GPT-2/CTRL/Transformer-XL/XLNet if RERANKER=="GPT": from transformers import OpenAIGPTTokenizer, OpenAIGPTLMHeadModel gpt2Tokenizer = OpenAIGPTTokenizer.from_pretrained('openai-gpt') gpt2LMHeadModel = OpenAIGPTLMHeadModel.from_pretrained('openai-gpt') gpt2Tokenizer.add_special_tokens({'pad_token':"[PAD]"}) gpt2LMHeadModel.resize_token_embeddings(len(gpt2Tokenizer)) assert gpt2Tokenizer.pad_token == '[PAD]' elif "GPT-2": from transformers import GPT2Tokenizer, GPT2LMHeadModel gpt2Tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium') gpt2LMHeadModel = GPT2LMHeadModel.from_pretrained('gpt2-medium') gpt2Tokenizer.pad_token = gpt2Tokenizer.eos_token elif "Transformer-XL": from transformers import TransfoXLTokenizer, TransfoXLLMHeadModel txlTokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') txlLMHeadModel = TransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103') txlTokenizer.pad_token = txlTokenizer.eos_token else: raise NotImplementedError def get_losses_from_gpt_lm(this_sents: "list[str]", gpt2LMHeadModel, gpt2Tokenizer, DEVICE): this_input_ids = gpt2Tokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True)["input_ids"] this_labels = torch.tensor([[i if i!=gpt2Tokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = gpt2LMHeadModel(input_ids=this_input_ids) this_lm_logits = this_outputs[0] # Shift so that tokens < n predict n shift_logits2 = this_lm_logits[:, :-1, :] shift_labels2 = this_labels[:, 1:] # Flatten the tokens loss_fct = CrossEntropyLoss(reduction='none') loss = loss_fct(shift_logits2.permute(0,2,1), shift_labels2) losses = loss.sum(dim=-1).cpu().detach().numpy().tolist() return losses def get_losses_from_txl_lm(this_sents: "list[str]", txlLMHeadModel, txlTokenizer, DEVICE): this_input_ids_dict = txlTokenizer.batch_encode_plus(this_sents, add_special_tokens=True, pad_to_max_length=True, add_space_before_punct_symbol=True) this_input_ids = this_input_ids_dict["input_ids"] chunks = [sum(val) for val in this_input_ids_dict["attention_mask"]] chunks_cumsum = np.cumsum(chunks).tolist() this_labels = torch.tensor([[i if i!=txlTokenizer.pad_token_id else -100 for i in row] for row in this_input_ids]).to(DEVICE) this_input_ids = torch.tensor(this_input_ids).to(DEVICE) this_outputs = txlLMHeadModel(input_ids=this_input_ids,labels=this_labels) this_loss = this_outputs[0] this_loss = this_loss.view(-1).cpu().detach().numpy() losses = [sum(this_loss[str_pos:end_pos-1]) for str_pos,end_pos in zip([0]+chunks_cumsum[:-1],chunks_cumsum)] return losses def load_model(vocab, verbose=False): model = ElmoSCLSTM(3*len(vocab["chartoken2idx"]),vocab["token2idx"][ vocab["pad_token"] ],len(vocab["token_freq"])) if verbose: print(model) print( get_model_nparams(model) ) return model def load_pretrained(model, CHECKPOINT_PATH, optimizer=None, device='cuda'): if torch.cuda.is_available() and device != "cpu": map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' print(f"Loading model params from checkpoint dir: {CHECKPOINT_PATH}") checkpoint_data = torch.load(os.path.join(CHECKPOINT_PATH, "model.pth.tar"), map_location=map_location) # print(f"previously model saved at : {checkpoint_data['epoch_id']}") model.load_state_dict(checkpoint_data['model_state_dict']) if optimizer is not None: optimizer.load_state_dict(checkpoint_data['optimizer_state_dict']) max_dev_acc, argmax_dev_acc = checkpoint_data["max_dev_acc"], checkpoint_data["argmax_dev_acc"] print(f"previously, max_dev_acc: {max_dev_acc:.5f} and argmax_dev_acc: {argmax_dev_acc:.5f}") if optimizer is not None: return model, optimizer, max_dev_acc, argmax_dev_acc return model def model_predictions(model, data, vocab, DEVICE, BATCH_SIZE=16, backoff="pass-through"): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) """ topk = 1 # print("###############################################") # inference_st_time = time.time() final_sentences = [] VALID_BATCH_SIZE = BATCH_SIZE # print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in enumerate(data_iter): # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward with torch.no_grad(): """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) """ _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) final_sentences.extend(batch_predictions) # print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) return final_sentences def model_predictions_for_ui(model, data, vocab, DEVICE, BATCH_SIZE=16, backoff="pass-through", beam_search=True, topk=3): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) """ assert len(data)==1, print(len(data)) if beam_search: if topk<2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") else: assert topk==1, print("if not beam_search, topk is set to 1 for UI-website purposes") print(f"beam_search: {beam_search} and topk: {topk}") print("data size: {}".format(len(data))) final_sentences = [] VALID_BATCH_SIZE = BATCH_SIZE data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in enumerate(data_iter): # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ _, batch_predictions, batch_predictions_probs = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print(f"batch_idxs:{len(batch_idxs)},batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") # based on beam_search, do either greedy topk or beam search for topk if not beam_search: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_clean_sentences, backoff=backoff) final_sentences = batch_predictions # a list with single answer else: k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_clean_sentences, topk) final_sentences = [x[0] for x in k_batch_predictions] # a list with multiple answers print("*&$&%^$*^*&%") print(final_sentences) print("*&$&%^$*^*&%") return final_sentences def model_inference(model, data, topk, DEVICE, BATCH_SIZE=16, beam_search=False, selected_lines_file=None, vocab_=None): """ model: an instance of ElmoSCLSTM data: list of tuples, with each tuple consisting of correct and incorrect sentence string (would be split at whitespaces) topk: how many of the topk softmax predictions are considered for metrics calculations DEVICE: "cuda:0" or "cpu" BATCH_SIZE: batch size for input to the model beam_search: if True, greedy topk will not be performed """ if vocab_ is not None: vocab = vocab_ if beam_search: if topk<2: raise Exception("when using beam_search, topk must be greater than 1, topk is used as beam width") else: print(f":: doing BEAM SEARCH with topk:{topk} ::") if selected_lines_file is not None: raise Exception("when using beam_search, ***selected_lines_file*** arg is not used; no implementation") # list of dicts with keys {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} results = [] line_index = 0 inference_st_time = time.time() VALID_BATCH_SIZE = BATCH_SIZE valid_loss, valid_acc = 0., 0. corr2corr, corr2incorr, incorr2corr, incorr2incorr = 0, 0, 0, 0 predictions = [] print("data size: {}".format(len(data))) data_iter = batch_iter(data, batch_size=VALID_BATCH_SIZE, shuffle=False) model.eval() model.to(DEVICE) for batch_id, (batch_clean_sentences,batch_corrupt_sentences) in tqdm(enumerate(data_iter)): torch.cuda.empty_cache() # st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_clean_sentences, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_corrupt_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_corrupt_sentences]).to(DEVICE) # forward try: with torch.no_grad(): if not beam_search: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk>1, else (batch_size,batch_max_seq_len) if topk==1 """ batch_loss, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk) # topk=1 or 5 else: """ NEW: batch_predictions can now be of shape (batch_size,batch_max_seq_len,topk) if topk==None """ batch_loss, batch_predictions, batch_predictions_probs = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels, topk=topk, beam_search=True) except RuntimeError: print(f"batch_idxs:{len(batch_idxs)},batch_lengths:{batch_lengths.shape},batch_elmo_inp:{batch_elmo_inp.shape},batch_labels:{batch_labels.shape}") raise Exception("") valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() # based on beam_search, do either greedy topk or beam search for topk if not beam_search: # based on topk, obtain either strings of batch_predictions or list of tokens if topk==1: batch_predictions = untokenize_without_unks(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) else: batch_predictions = untokenize_without_unks2(batch_predictions, batch_lengths, vocab, batch_corrupt_sentences) predictions.extend(batch_predictions) batch_clean_sentences = [line.lower() for line in batch_clean_sentences] batch_corrupt_sentences = [line.lower() for line in batch_corrupt_sentences] batch_predictions = [line.lower() for line in batch_predictions] corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences,batch_corrupt_sentences,batch_predictions,check_until_topk=topk,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ for i, (a,b,c) in enumerate(zip(batch_clean_sentences,batch_corrupt_sentences,batch_predictions)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":[], "topk_prediction_probs":[], "topk_reranker_losses":[]}) line_index += len(batch_clean_sentences) else: """ NEW: use untokenize_without_unks3 for beam search outputs """ # k different lists each of type batch_predictions as in topk==1 # List[List[Strings]] k_batch_predictions, k_batch_predictions_probs = untokenize_without_unks3(batch_predictions, batch_predictions_probs, batch_lengths, vocab, batch_corrupt_sentences, topk) ########################################################## ############## this takes top1 as-is ##################### # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,k_batch_predictions[0],check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## ############### this does reranking ###################### gpt2LMHeadModel.to(DEVICE) gpt2LMHeadModel.eval() # txlLMHeadModel.to(DEVICE) # txlLMHeadModel.eval() reranked_batch_predictions = [] batch_clean_sentences_ = [] batch_corrupt_sentences_ = [] batch_losses_ = [] with torch.no_grad(): for b in range(len(batch_clean_sentences)): losses = [] this_sents = [k_batch_predictions[k][b] for k in range(topk)] losses = get_losses_from_gpt_lm(this_sents, gpt2LMHeadModel, gpt2Tokenizer, DEVICE) # losses = get_losses_from_txl_lm(this_sents, txlLMHeadModel, txlTokenizer, DEVICE) kmin = np.argmin(losses) reranked_batch_predictions.append(k_batch_predictions[kmin][b]) batch_clean_sentences_.append(batch_clean_sentences[b]) batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) batch_losses_.append(losses) corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) corr2corr+=corr2corr_ corr2incorr+=corr2incorr_ incorr2corr+=incorr2corr_ incorr2incorr+=incorr2incorr_ batch_predictions_k = [[k_batch_predictions[j][i] for j in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] batch_predictions_probs_k = [[k_batch_predictions_probs[j][i] for j in range(len(k_batch_predictions_probs))] for i in range(len(k_batch_predictions_probs[0]))] for i, (a,b,c,d,e,f) in \ enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k,batch_predictions_probs_k,batch_losses_)): results.append({"id":line_index+i, "original":a, "noised":b, "predicted":c, "topk":d, "topk_prediction_probs":e, "topk_reranker_losses":f}) line_index += len(batch_clean_sentences) # delete del batch_loss del batch_predictions del batch_labels, batch_lengths, batch_idxs, batch_lengths_, batch_elmo_inp torch.cuda.empty_cache() # ''' # # update progress # progressBar(batch_id+1, # int(np.ceil(len(data) / VALID_BATCH_SIZE)), # ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], # [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),None,None]) # ''' print(f"\nEpoch {None} valid_loss: {valid_loss/(batch_id+1)}") print("total inference time for this data is: {:4f} secs".format(time.time()-inference_st_time)) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") if not beam_search and selected_lines_file is not None: print("evaluating only for selected lines ... ") assert len(data)==len(predictions), print(len(data),len(predictions),"lengths mismatch") if selected_lines_file is not None: selected_lines = {num:"" for num in [int(line.strip()) for line in open(selected_lines_file,'r')]} else: selected_lines = None clean_lines, corrupt_lines,predictions_lines = [tpl[0] for tpl in data], [tpl[1] for tpl in data], predictions corr2corr, corr2incorr, incorr2corr, incorr2incorr, mistakes = \ get_metrics(clean_lines,corrupt_lines,predictions_lines,return_mistakes=True,selected_lines=selected_lines) print("###############################################") print("total token count: {}".format(corr2corr+corr2incorr+incorr2corr+incorr2incorr)) print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") print(f"accuracy is {(corr2corr+incorr2corr)/(corr2corr+corr2incorr+incorr2corr+incorr2incorr)}") print(f"word correction rate is {(incorr2corr)/(incorr2corr+incorr2incorr)}") print("###############################################") return results if __name__=="__main__": print("#########################"+"\n") # "word", "prob", "probword", 'random', bea40kfinetune', 'moviereviewsfinetune' TRAIN_NOISE_TYPE = sys.argv[1] # "../../data" BASE_PATH = sys.argv[2] # -ve value for inference only; 1 for training a new model from scratch; >1 for continuing training START_EPOCH = int(sys.argv[3]) if START_EPOCH==0: raise Exception("START_EPOCH must be a non-zero value; If starting from scratch, use 1 instead of 0") # :NEW: finetune now from a specific epoch of a model # "probword" if len(sys.argv)>4: FINETUNE = sys.argv[4] if FINETUNE=='probword': SRC_CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probwordnoise") SRC_VOCAB_PATH = os.path.join(SRC_CHECKPOINT_PATH,"vocab.pkl") print(f"Model finetuning with arg: {FINETUNE}, and source model selected from: {SRC_CHECKPOINT_PATH}") else: raise Exception("only ```probword``` is now supported for finetuning") assert os.path.exists(SRC_CHECKPOINT_PATH), print(f"{SRC_CHECKPOINT_PATH} path unavailable") else: FINETUNE = "" ############################################# # environment ############################################# # checkpoint path for this model if TRAIN_NOISE_TYPE=="word": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-wordnoise") elif TRAIN_NOISE_TYPE=="prob": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probnoise") elif TRAIN_NOISE_TYPE=="random": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-randomnoise") elif TRAIN_NOISE_TYPE=="probword": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probwordnoise") elif TRAIN_NOISE_TYPE=="probword_v2": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probwordnoise_v2") elif TRAIN_NOISE_TYPE=="bea40kfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probwordnoise-bea40kfinetune") elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-probwordnoise-moviereviewsfinetune2") elif TRAIN_NOISE_TYPE=="none": CHECKPOINT_PATH = os.path.join(BASE_PATH, "checkpoints/elmoscrnn-none") else: raise Exception("invalid TRAIN_NOISE_TYPE") if not os.path.exists(CHECKPOINT_PATH): os.makedirs(CHECKPOINT_PATH) VOCAB_PATH = os.path.join(CHECKPOINT_PATH,"vocab.pkl") # settings print("#########################"+"\n") START_EPOCH, N_EPOCHS = START_EPOCH, 50 TRAIN_BATCH_SIZE, VALID_BATCH_SIZE = 64, 50 # 16, 16 ############################################# # load train data (if required) ############################################# TRAIN_TEST_FILE_PATH = os.path.join(BASE_PATH, "traintest/") if START_EPOCH>0: if FINETUNE!="": print("loading vocab for finetuning") print(f"loading vocab from {SRC_VOCAB_PATH}") vocab = load_vocab_dict(SRC_VOCAB_PATH) save_vocab_dict(VOCAB_PATH, vocab) # load traintest data if TRAIN_NOISE_TYPE=="bea40kfinetune": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.bea40k", "train.bea40k.noise") train_data, valid_data = train_validation_split(train_data, 0.90, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="moviereviewsfinetune": # train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] # valid_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "valid.moviereviews")) valid_data_noise1 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) valid_data_noise2 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) valid_data_noise3 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) valid_data_noise4 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) valid_data_noise5 = get_line_representation(valid_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) valid_data_noise = valid_data_noise1+valid_data_noise2+valid_data_noise3+valid_data_noise4+valid_data_noise5 valid_data_clean = valid_data_clean*5 valid_data = [(a,b) for a,b in zip(valid_data_clean,valid_data_noise)] print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE in finetuning") else: # load traintest data if TRAIN_NOISE_TYPE=="word": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="prob": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="random": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.random") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="probword": train_data1 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.prob") train_data1, valid_data1 = train_validation_split(train_data1, 0.8, seed=11690) print(len(train_data1),len(valid_data1)) train_data2 = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm.noise.word") train_data2, valid_data2 = train_validation_split(train_data2, 0.8, seed=11690) print(len(train_data2),len(valid_data2)) train_data = train_data1+train_data2 valid_data = valid_data1+valid_data2 print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="probword_v2": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm.v2", "train.1blm.v2.noise.probword") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) elif TRAIN_NOISE_TYPE=="none": train_data = load_data(TRAIN_TEST_FILE_PATH, "train.1blm", "train.1blm") train_data, valid_data = train_validation_split(train_data, 0.8, seed=11690) print(len(train_data),len(valid_data)) else: raise Exception("invalid TRAIN_NOISE_TYPE") ############################################# # load vocab ############################################# if START_EPOCH!=1: # if not training from scratch or for inference print(f"loading vocab from {VOCAB_PATH}") vocab = load_vocab_dict(VOCAB_PATH) else: # load a vocab for reference vocab_ref = {} # opfile = open(os.path.join(BASE_PATH, "vocab/phonemedataset.txt"),"r") # for line in opfile: vocab_ref.update( {line.strip():0} ) # opfile.close() print(f"loading vocab from train data itself and saving it at {VOCAB_PATH}") vocab = get_tokens([i[0] for i in train_data], keep_simple=True, min_max_freq=(2,float("inf")), topk=100000, intersect=vocab_ref, load_char_tokens=True) save_vocab_dict(VOCAB_PATH, vocab) if START_EPOCH>0: # see how many tokens in labels are going to be UNK print ( num_unk_tokens([i[0] for i in train_data], vocab) ) print ( num_unk_tokens([i[0] for i in valid_data], vocab) ) print("") print([*vocab.keys()]) #print(vocab["token_freq"]) #print([(idx,vocab["idx2token"][idx]) for idx in range(100)]) ############################################# # load ElmoSCLSTM ############################################# model = load_model(vocab, verbose=False) ############################################# # training or inference ??! ############################################# if START_EPOCH>0: ############################################# # training and validation ############################################# # running stats max_dev_acc, argmax_dev_acc = -1, -1 patience = 100 # Create an optimizer optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # model to device model.to(DEVICE) # load parameters if not training from scratch if START_EPOCH>1: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,f"progress_retrain_from_epoch{START_EPOCH}.txt"),'w') # model and optimizer load_state_dict if FINETUNE!="": print("loading pretrained weights for finetuning") print(f"loading pretrained weights from {SRC_CHECKPOINT_PATH}") model, optimizer, _, _ = load_pretrained(model, SRC_CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {SRC_CHECKPOINT_PATH}\n") else: print(f"loading pretrained weights from {CHECKPOINT_PATH}") model, optimizer, max_dev_acc, argmax_dev_acc = load_pretrained(model, CHECKPOINT_PATH, optimizer=optimizer) progress_write_file.write(f"Training model params after loading from path: {CHECKPOINT_PATH}\n") else: # file to write progress to progress_write_file = open(os.path.join(CHECKPOINT_PATH,"progress.txt"),'w') print(f"Training model params from scratch") progress_write_file.write(f"Training model params from scratch\n") progress_write_file.flush() # train and eval for epoch_id in range(START_EPOCH,N_EPOCHS+1): # check for patience if (epoch_id-argmax_dev_acc)>patience: print("patience count reached. early stopping initiated") print("max_dev_acc: {}, argmax_dev_acc: {}".format(max_dev_acc, argmax_dev_acc)) break # if finetuning and the noise type is moviereviews, # create a different train data every epoch if TRAIN_NOISE_TYPE=="moviereviewsfinetune": train_data_clean = get_lines(os.path.join(TRAIN_TEST_FILE_PATH, "train.moviereviews")) train_data_noise1 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.25,0.25,0.25,0.25]) train_data_noise2 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[1.00,0.00,0.00,0.00]) train_data_noise3 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,1.00,0.00,0.00]) train_data_noise4 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,1.00,0.00]) train_data_noise5 = get_line_representation(train_data_clean,rep_list=['swap','drop','add','key'], probs=[0.00,0.00,0.00,1.00]) train_data_noise = train_data_noise1+train_data_noise2+train_data_noise3+train_data_noise4+train_data_noise5 train_data_clean = train_data_clean*5 train_data = [(a,b) for a,b in zip(train_data_clean,train_data_noise)] print(f"new training instances created, train data size now: {len(train_data)}") # print epoch print(f"In epoch: {epoch_id}") progress_write_file.write(f"In epoch: {epoch_id}\n") progress_write_file.flush() # train loss and backprop train_loss = 0. train_acc = 0. train_acc_count = 0. print("train_data size: {}".format(len(train_data))) progress_write_file.write("train_data size: {}\n".format(len(train_data))) progress_write_file.flush() train_data_iter = batch_iter(train_data, batch_size=TRAIN_BATCH_SIZE, shuffle=True) #for batch_id, (batch_labels,batch_sentences) in tqdm(enumerate(train_data_iter)): for batch_id, (batch_labels,batch_sentences) in enumerate(train_data_iter): optimizer.zero_grad() st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.train() loss = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) batch_loss = loss.cpu().detach().numpy() train_loss += batch_loss # backward loss.backward() optimizer.step() # compute accuracy in numpy if batch_id%10000==0: train_acc_count += 1 model.eval() with torch.no_grad(): _, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) model.train() batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal train_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,train_loss/(batch_id+1),batch_acc,train_acc/train_acc_count]) if batch_id==0 or (batch_id+1)%5000==0: nb = int(np.ceil(len(train_data) / TRAIN_BATCH_SIZE)) progress_write_file.write(f"{batch_id+1}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {train_loss/(batch_id+1)}, avg_batch_acc: {train_acc/(batch_id+1)}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} train_loss: {train_loss/(batch_id+1)}") try: # valid loss valid_loss = 0. valid_acc = 0. print("valid_data size: {}".format(len(valid_data))) progress_write_file.write("valid_data size: {}\n".format(len(valid_data))) progress_write_file.flush() valid_data_iter = batch_iter(valid_data, batch_size=VALID_BATCH_SIZE, shuffle=False) for batch_id, (batch_labels,batch_sentences) in enumerate(valid_data_iter): st_time = time.time() # set batch data batch_labels, batch_lengths = labelize(batch_labels, vocab) batch_idxs, batch_lengths_ = sclstm_tokenize(batch_sentences, vocab) assert (batch_lengths_==batch_lengths).all()==True batch_idxs = [batch_idxs_.to(DEVICE) for batch_idxs_ in batch_idxs] batch_lengths = batch_lengths.to(DEVICE) batch_labels = batch_labels.to(DEVICE) batch_elmo_inp = elmo_batch_to_ids([line.split() for line in batch_sentences]).to(DEVICE) # forward model.eval() with torch.no_grad(): batch_loss, batch_predictions = model(batch_idxs, batch_lengths, batch_elmo_inp, targets=batch_labels) model.train() valid_loss += batch_loss # compute accuracy in numpy batch_labels = batch_labels.cpu().detach().numpy() batch_lengths = batch_lengths.cpu().detach().numpy() ncorr,ntotal = batch_accuracy_func(batch_predictions,batch_labels,batch_lengths) batch_acc = ncorr/ntotal valid_acc += batch_acc # update progress progressBar(batch_id+1, int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)), ["batch_time","batch_loss","avg_batch_loss","batch_acc","avg_batch_acc"], [time.time()-st_time,batch_loss,valid_loss/(batch_id+1),batch_acc,valid_acc/(batch_id+1)]) if batch_id==0 or (batch_id+1)%2000==0: nb = int(np.ceil(len(valid_data) / VALID_BATCH_SIZE)) progress_write_file.write(f"{batch_id}/{nb}\n") progress_write_file.write(f"batch_time: {time.time()-st_time}, avg_batch_loss: {valid_loss/(batch_id+1)}, avg_batch_acc: {valid_acc/(batch_id+1)}\n") progress_write_file.flush() print(f"\nEpoch {epoch_id} valid_loss: {valid_loss/(batch_id+1)}") # save model, optimizer and test_predictions if val_acc is improved if valid_acc>=max_dev_acc: # to file #name = "model-epoch{}.pth.tar".format(epoch_id) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(CHECKPOINT_PATH,name)) print("Model saved at {} in epoch {}".format(os.path.join(CHECKPOINT_PATH,name),epoch_id)) # re-assign max_dev_acc, argmax_dev_acc = valid_acc, epoch_id except Exception as e: temp_folder = os.path.join(CHECKPOINT_PATH,"temp") if not os.path.exists(temp_folder): os.makedirs(temp_folder) name = "model.pth.tar".format(epoch_id) torch.save({ 'epoch_id': epoch_id, 'previous_max_dev_acc': max_dev_acc, 'previous_argmax_dev_acc': argmax_dev_acc, 'max_dev_acc': valid_acc, 'argmax_dev_acc': epoch_id, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}, os.path.join(temp_folder,name)) print("Model saved at {} in epoch {}".format(os.path.join(temp_folder,name),epoch_id)) raise Exception(e) else: ############################################# # inference ############################################# # load parameters model = load_pretrained(model, CHECKPOINT_PATH) # infer TRAIN_TEST_FILE_PATH1 = os.path.join(BASE_PATH, "traintest") TRAIN_TEST_FILE_PATH2 = os.path.join(BASE_PATH, "traintest/wo_context") ''' paths = [TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH1,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] files1 = ["test.bea60k","test.1blm","test.1blm","combined_data","aspell_big","aspell_small"] files2 = ["test.bea60k.noise","test.1blm.noise.prob","test.1blm.noise.word","combined_data.noise","aspell_big.noise","aspell_small.noise"] INFER_BATCH_SIZE = 16 ''' ''' paths = [TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2,TRAIN_TEST_FILE_PATH2] files1 = ["combined_data","aspell_big","aspell_small"] files2 = ["combined_data.noise","aspell_big.noise","aspell_small.noise"] INFER_BATCH_SIZE = 1024 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm","test.1blm"] files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] INFER_BATCH_SIZE = 64 # 128 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm"] files2 = ["test.1blm.noise.prob"] INFER_BATCH_SIZE = 20 #64 #128 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_probnoise" selected_lines_file = None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm"] files2 = ["test.1blm.noise.word"] INFER_BATCH_SIZE = 20 #64 #128 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_wordnoise" selected_lines_file = None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm"] files2 = ["test.1blm.noise.random"] INFER_BATCH_SIZE = 20 #20 #64 #128 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_randomnoise" selected_lines_file = None ''' ''' paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] files1 = ["test.1blm","test.1blm"] files2 = ["test.1blm.noise.prob","test.1blm.noise.word"] INFER_BATCH_SIZE = 32 ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea4k",] files2 = ["test.bea4k.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_bea4k" selected_lines_file = None # "../gec-pseudodata/test.bea4k.lines.txt" # None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k"] files2 = ["test.bea60k.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_bea60k" selected_lines_file = None # "../gec-pseudodata/test.bea60k.lines.txt" # None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k.ambiguous_artificial"] files2 = ["test.bea60k.ambiguous_artificial.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_bea60k_ambiguous_artificial" selected_lines_file = None ''' ''' paths = [TRAIN_TEST_FILE_PATH1, TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k.ambiguous_natural_v7", "test.bea60k.ambiguous_natural_v8"] files2 = ["test.bea60k.ambiguous_natural_v7.noise", "test.bea60k.ambiguous_natural_v8.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_bea60k_ambiguous_natural_v5" selected_lines_file = None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea20k"] files2 = ["test.bea20k.noise"] INFER_BATCH_SIZE = 10 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_bea20k" selected_lines_file = None # "../gec-pseudodata/test.bea20k.lines.txt" # None ''' ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.jfleg"] files2 = ["test.jfleg.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_jfleg" selected_lines_file = None # "../gec-pseudodata/test.jfleg.lines.txt" # None ''' paths = [TRAIN_TEST_FILE_PATH1] files1 = ["test.bea60k.ambiguous_natural_v7"] files2 = ["test.bea60k.ambiguous_natural_v7.noise"] INFER_BATCH_SIZE = 8 ANALYSIS_DIR = f"../seq_modeling_analysis/elmoscrnn/analysis_{TRAIN_NOISE_TYPE}_ambiguous_natural_v7" selected_lines_file = None # expect a dict as {"id":, "original":, "noised":, "predicted":, "topk":, "topk_prediction_probs":, "topk_reranker_losses":,} for x,y,z in zip(paths,files1,files2): print("\n\n\n\n") print(x,y,z) test_data = load_data(x,y,z) print ( num_unk_tokens([i[0] for i in test_data], vocab) ) greedy_results = model_inference(model,test_data,topk=1,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=False,selected_lines_file=selected_lines_file) # beam_search_results = model_inference(model,test_data,topk=10,DEVICE=DEVICE,BATCH_SIZE=INFER_BATCH_SIZE,beam_search=True) print(ANALYSIS_DIR) if not os.path.exists(ANALYSIS_DIR): os.makedirs(ANALYSIS_DIR) import jsonlines # print("greedy...") greedy_lines_fully_correct = {line["id"]:"" for line in greedy_results if line["original"]==line["predicted"]} greedy_lines_otherwise = {line["id"]:"" for line in greedy_results if line["original"]!=line["predicted"]} print(f'# Lines Predicted Fully Correct: {len(greedy_lines_fully_correct)}') print(f'# Lines Otherwise: {len(greedy_lines_otherwise)}') opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results.jsonl"),'w') for line in greedy_results: opfile.write(line) opfile.close() opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_corr_preds.jsonl"),'w') for line in [line for line in greedy_results if line["original"]==line["predicted"]]: opfile.write(line) opfile.close() opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"greedy_results_incorr_preds.jsonl"),'w') for line in [line for line in greedy_results if line["original"]!=line["predicted"]]: opfile.write(line) opfile.close() # # for better view opfile = open(os.path.join(ANALYSIS_DIR,"greedy_results.txt"),'w') for line in greedy_results: ls = [(o,n,p) if o==n==p else ("**"+o+"**","**"+n+"**","**"+p+"**")for o,n,p in zip(line["original"].split(),line["noised"].split(),line["predicted"].split())] x,y,z = map(list, zip(*ls)) opfile.write(f'{line["id"]}\n{" ".join(x)}\n{" ".join(y)}\n{" ".join(z)}\n') opfile.close() opfile = open(os.path.join(ANALYSIS_DIR,"greedy_results_corr_preds.txt"),'w') for line in [line for line in greedy_results if line["original"]==line["predicted"]]: ls = [(o,n,p) if o==n==p else ("**"+o+"**","**"+n+"**","**"+p+"**")for o,n,p in zip(line["original"].split(),line["noised"].split(),line["predicted"].split())] x,y,z = map(list, zip(*ls)) opfile.write(f'{line["id"]}\n{" ".join(x)}\n{" ".join(y)}\n{" ".join(z)}\n') opfile.close() opfile = open(os.path.join(ANALYSIS_DIR,"greedy_results_incorr_preds.txt"),'w') for line in [line for line in greedy_results if line["original"]!=line["predicted"]]: ls = [(o,n,p) if o==n==p else ("**"+o+"**","**"+n+"**","**"+p+"**")for o,n,p in zip(line["original"].split(),line["noised"].split(),line["predicted"].split())] x,y,z = map(list, zip(*ls)) opfile.write(f'{line["id"]}\n{" ".join(x)}\n{" ".join(y)}\n{" ".join(z)}\n') opfile.close() # print("beam_search...") # beam_search_lines_fully_correct = {line["id"]:"" for line in beam_search_results if line["original"]==line["predicted"]} # beam_search_lines_otherwise = {line["id"]:"" for line in beam_search_results if line["original"]!=line["predicted"]} # print(f'# Lines Predicted Fully Correct: {len(beam_search_lines_fully_correct)}') # print(f'# Lines Otherwise: {len(beam_search_lines_otherwise)}') # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results.jsonl"),'w') # for line in beam_search_results: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_corr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]==line["predicted"]]: opfile.write(line) # opfile.close() # opfile = jsonlines.open(os.path.join(ANALYSIS_DIR,"beam_search_results_incorr_preds.jsonl"),'w') # for line in [line for line in beam_search_results if line["original"]!=line["predicted"]]: opfile.write(line) # opfile.close() # # # # confusion matrix # corr2corr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_fully_correct]) # corr2incorr = len([k for k in greedy_lines_fully_correct if k in beam_search_lines_otherwise]) # incorr2corr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_fully_correct]) # incorr2incorr = len([k for k in greedy_lines_otherwise if k in beam_search_lines_otherwise]) # print("Confusion Matrix for before and after beam search: ") # print(f"corr2corr:{corr2corr}, corr2incorr:{corr2incorr}, incorr2corr:{incorr2corr}, incorr2incorr:{incorr2incorr}") ######################################### # reranking snippets from past ######################################### # if save_dir is not None: # line_index = 0 # analysis_path = save_dir # if not os.path.exists(analysis_path): # os.makedirs(analysis_path) # if beam_search: # line_index_wrong_opfile = open(f"./{analysis_path}/beam_search_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/beam_search_right.txt","w") # k_wrong_opfile = open(f"./{analysis_path}/beam_search_k_wrong.txt","w") # k_right_opfile = open(f"./{analysis_path}/beam_search_k_right.txt","w") # else: # line_index_wrong_opfile = open(f"./{analysis_path}/greedy_wrong.txt","w") # line_index_right_opfile = open(f"./{analysis_path}/greedy_right.txt","w") # reranked_batch_predictions = [] # batch_clean_sentences_ = [] # batch_corrupt_sentences_ = [] # with torch.no_grad(): # for b in range(len(batch_clean_sentences)): # try: # losses = [] # for sent in [k_batch_predictions[k][b] for k in range(topk)]: # if sent!="" or sent is not None: # input_ids = torch.tensor(gpt2Tokenizer.encode(sent, add_special_tokens=True)).unsqueeze(0) # Batch size 1 # input_ids = input_ids.to(DEVICE) # outputs = gpt2LMHeadModel(input_ids, labels=input_ids) # loss = outputs[0].item() # else: # loss = 10000.0 # losses.append(loss) # kmin = np.argmin(losses) # reranked_batch_predictions.append(k_batch_predictions[kmin][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # except Exception as e: # reranked_batch_predictions.append(k_batch_predictions[0][b]) # batch_clean_sentences_.append(batch_clean_sentences[b]) # batch_corrupt_sentences_.append(batch_corrupt_sentences[b]) # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ # this_batch = [[k_batch_predictions[k][i] for k in range(len(k_batch_predictions))] for i in range(len(k_batch_predictions[0]))] # flat_batch = sum(this_batch,[]); # print(flat_batch); print(len(flat_batch)) # lens = [len(s) for s in this_batch] # ii = 0 # flat_losses = [] # model.eval() # model.to(DEVICE) # with torch.no_grad(): # while ii<len(flat_batch): # try: # curr_batch = flat_batch[ii:ii+HFACE_BATCH_SIZE] # curr_inputs = gpt2Tokenizer.batch_encode_plus(curr_batch,pad_to_max_length=True) # curr_inputs_ids = curr_inputs["input_ids"] # curr_inputs = {k:torch.tensor(v).to(DEVICE) for k,v in curr_inputs.items()} # curr_outputs = gpt2LMHeadModel(input_ids=curr_inputs["input_ids"],token_type_ids=curr_inputs["token_type_ids"],attention_mask=curr_inputs["attention_mask"]) # lm_logits = curr_outputs[0] # labels = torch.tensor([[i if i!=50256 else -100 for i in row] for row in curr_inputs_ids]).to(DEVICE) # # Shift so that tokens < n predict n # shift_logits = lm_logits[..., :-1, :].contiguous(); # print(shift_logits.shape) # shift_labels = labels[..., 1:].contiguous(); # print(shift_labels.shape) # # Flatten the tokens # loss_fct = CrossEntropyLoss(reduction='none') # loss = loss_fct(shift_logits.permute(0, 2, 1), shift_labels) # flat_losses.extend(loss.sum(axis=-1).cpu().detach().numpy().tolist()) # ii += HFACE_BATCH_SIZE # except Exception as e: # # print(this_batch) # raise Exception(e) # offset = 0 # batch_losses = [] # for val in lens: # batch_losses.append(flat_losses[offset:offset+val]) # offset += val # print(np.array(batch_losses)) # reranked_batch_predictions = [k_batch_predictions[np.argmin(batch_losses[i])][i] for i in range(len(batch_losses))] # print(batch_clean_sentences) # print("") # print(reranked_batch_predictions) # raise Exception("debug...") # corr2corr_, corr2incorr_, incorr2corr_, incorr2incorr_ = \ # get_metrics(batch_clean_sentences,batch_corrupt_sentences,reranked_batch_predictions,check_until_topk=1,return_mistakes=False) # corr2corr+=corr2corr_ # corr2incorr+=corr2incorr_ # incorr2corr+=incorr2corr_ # incorr2incorr+=incorr2incorr_ ########################################################## # for i, (a,b,c,d) in enumerate(zip(batch_clean_sentences_,batch_corrupt_sentences_,reranked_batch_predictions,batch_predictions_k)): # if a==c: # right # line_index_right_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # else: # line_index_wrong_opfile.write(f"{line_index+i}\t{a}\t{b}\t{c}\n") # line_index+=len(batch_clean_sentences_) # line_index_right_opfile.flush() # line_index_wrong_opfile.flush() # __mistakes = [] # __inds = [] # for i in range(len(batch_clean_sentences)): # if batch_clean_sentences[i].strip()!=k_batch_predictions[0][i].strip(): # __mistakes.append(f"{batch_clean_sentences[i]}\n") # __inds.append(i) # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions_probs[ind]:.4f}\t" # ii+=1 # batch_predictions = k_batch_predictions[k] # ii = 0 # for ind in __inds: # __mistakes[ii]+=f"{batch_predictions[ind]}\n" # ii+=1 # ii=0 # for i,_ in enumerate(batch_clean_sentences): # if i in __inds: # __mistakes[ii]+="\n" # ii+=1 # for mis in __mistakes: # k_wrong_opfile.write(mis) # __predictions = [] # for sent in batch_clean_sentences: # __predictions.append(f"{sent}\n") # for k in range(topk): # batch_predictions_probs = k_batch_predictions_probs[k] # for i,val in enumerate(batch_predictions_probs): # __predictions[i]+=f"{val:.4f}\t" # batch_predictions = k_batch_predictions[k] # for i,sent in enumerate(batch_predictions): # __predictions[i]+=f"{sent}\n" # for i,_ in enumerate(batch_clean_sentences): # __predictions[i]+="\n" # for pred in __predictions: # k_right_opfile.write(pred) # if beam_search: # line_index_right_opfile.close() # line_index_wrong_opfile.close() # k_wrong_opfile.close() # k_right_opfile.close() # else: # line_index_right_opfile.close() # line_index_wrong_opfile.close()
[]
2024-01-10
perzeuss/dify
api~services~hit_testing_service.py
import json import logging import threading import time from typing import List import numpy as np from flask import current_app from langchain.embeddings.base import Embeddings from langchain.schema import Document from sklearn.manifold import TSNE from core.embedding.cached_embedding import CacheEmbedding from core.model_providers.model_factory import ModelFactory from extensions.ext_database import db from models.account import Account from models.dataset import Dataset, DocumentSegment, DatasetQuery from services.retrieval_service import RetrievalService default_retrieval_model = { 'search_method': 'semantic_search', 'reranking_enable': False, 'reranking_model': { 'reranking_provider_name': '', 'reranking_model_name': '' }, 'top_k': 2, 'score_threshold_enable': False } class HitTestingService: @classmethod def retrieve(cls, dataset: Dataset, query: str, account: Account, retrieval_model: dict, limit: int = 10) -> dict: if dataset.available_document_count == 0 or dataset.available_segment_count == 0: return { "query": { "content": query, "tsne_position": {'x': 0, 'y': 0}, }, "records": [] } start = time.perf_counter() # get retrieval model , if the model is not setting , using default if not retrieval_model: retrieval_model = dataset.retrieval_model if dataset.retrieval_model else default_retrieval_model # get embedding model embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) embeddings = CacheEmbedding(embedding_model) all_documents = [] threads = [] # retrieval_model source with semantic if retrieval_model['search_method'] == 'semantic_search' or retrieval_model['search_method'] == 'hybrid_search': embedding_thread = threading.Thread(target=RetrievalService.embedding_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'top_k': retrieval_model['top_k'], 'score_threshold': retrieval_model['score_threshold'] if retrieval_model['score_threshold_enable'] else None, 'reranking_model': retrieval_model['reranking_model'] if retrieval_model['reranking_enable'] else None, 'all_documents': all_documents, 'search_method': retrieval_model['search_method'], 'embeddings': embeddings }) threads.append(embedding_thread) embedding_thread.start() # retrieval source with full text if retrieval_model['search_method'] == 'full_text_search' or retrieval_model['search_method'] == 'hybrid_search': full_text_index_thread = threading.Thread(target=RetrievalService.full_text_index_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'search_method': retrieval_model['search_method'], 'embeddings': embeddings, 'score_threshold': retrieval_model['score_threshold'] if retrieval_model['score_threshold_enable'] else None, 'top_k': retrieval_model['top_k'], 'reranking_model': retrieval_model['reranking_model'] if retrieval_model['reranking_enable'] else None, 'all_documents': all_documents }) threads.append(full_text_index_thread) full_text_index_thread.start() for thread in threads: thread.join() if retrieval_model['search_method'] == 'hybrid_search': hybrid_rerank = ModelFactory.get_reranking_model( tenant_id=dataset.tenant_id, model_provider_name=retrieval_model['reranking_model']['reranking_provider_name'], model_name=retrieval_model['reranking_model']['reranking_model_name'] ) all_documents = hybrid_rerank.rerank(query, all_documents, retrieval_model['score_threshold'] if retrieval_model['score_threshold_enable'] else None, retrieval_model['top_k']) end = time.perf_counter() logging.debug(f"Hit testing retrieve in {end - start:0.4f} seconds") dataset_query = DatasetQuery( dataset_id=dataset.id, content=query, source='hit_testing', created_by_role='account', created_by=account.id ) db.session.add(dataset_query) db.session.commit() return cls.compact_retrieve_response(dataset, embeddings, query, all_documents) @classmethod def compact_retrieve_response(cls, dataset: Dataset, embeddings: Embeddings, query: str, documents: List[Document]): text_embeddings = [ embeddings.embed_query(query) ] text_embeddings.extend(embeddings.embed_documents([document.page_content for document in documents])) tsne_position_data = cls.get_tsne_positions_from_embeddings(text_embeddings) query_position = tsne_position_data.pop(0) i = 0 records = [] for document in documents: index_node_id = document.metadata['doc_id'] segment = db.session.query(DocumentSegment).filter( DocumentSegment.dataset_id == dataset.id, DocumentSegment.enabled == True, DocumentSegment.status == 'completed', DocumentSegment.index_node_id == index_node_id ).first() if not segment: i += 1 continue record = { "segment": segment, "score": document.metadata.get('score', None), "tsne_position": tsne_position_data[i] } records.append(record) i += 1 return { "query": { "content": query, "tsne_position": query_position, }, "records": records } @classmethod def get_tsne_positions_from_embeddings(cls, embeddings: list): embedding_length = len(embeddings) if embedding_length <= 1: return [{'x': 0, 'y': 0}] concatenate_data = np.array(embeddings).reshape(embedding_length, -1) # concatenate_data = np.concatenate(embeddings) perplexity = embedding_length / 2 + 1 if perplexity >= embedding_length: perplexity = max(embedding_length - 1, 1) tsne = TSNE(n_components=2, perplexity=perplexity, early_exaggeration=12.0) data_tsne = tsne.fit_transform(concatenate_data) tsne_position_data = [] for i in range(len(data_tsne)): tsne_position_data.append({'x': float(data_tsne[i][0]), 'y': float(data_tsne[i][1])}) return tsne_position_data @classmethod def hit_testing_args_check(cls, args): query = args['query'] if not query or len(query) > 250: raise ValueError('Query is required and cannot exceed 250 characters')
[]
2024-01-10
perzeuss/dify
api~tasks~enable_segment_to_index_task.py
import datetime import logging import time import click from celery import shared_task from langchain.schema import Document from werkzeug.exceptions import NotFound from core.index.index import IndexBuilder from extensions.ext_database import db from extensions.ext_redis import redis_client from models.dataset import DocumentSegment @shared_task(queue='dataset') def enable_segment_to_index_task(segment_id: str): """ Async enable segment to index :param segment_id: Usage: enable_segment_to_index_task.delay(segment_id) """ logging.info(click.style('Start enable segment to index: {}'.format(segment_id), fg='green')) start_at = time.perf_counter() segment = db.session.query(DocumentSegment).filter(DocumentSegment.id == segment_id).first() if not segment: raise NotFound('Segment not found') if segment.status != 'completed': return indexing_cache_key = 'segment_{}_indexing'.format(segment.id) try: document = Document( page_content=segment.content, metadata={ "doc_id": segment.index_node_id, "doc_hash": segment.index_node_hash, "document_id": segment.document_id, "dataset_id": segment.dataset_id, } ) dataset = segment.dataset if not dataset: logging.info(click.style('Segment {} has no dataset, pass.'.format(segment.id), fg='cyan')) return dataset_document = segment.document if not dataset_document: logging.info(click.style('Segment {} has no document, pass.'.format(segment.id), fg='cyan')) return if not dataset_document.enabled or dataset_document.archived or dataset_document.indexing_status != 'completed': logging.info(click.style('Segment {} document status is invalid, pass.'.format(segment.id), fg='cyan')) return # save vector index index = IndexBuilder.get_index(dataset, 'high_quality') if index: index.add_texts([document], duplicate_check=True) # save keyword index index = IndexBuilder.get_index(dataset, 'economy') if index: index.add_texts([document]) end_at = time.perf_counter() logging.info(click.style('Segment enabled to index: {} latency: {}'.format(segment.id, end_at - start_at), fg='green')) except Exception as e: logging.exception("enable segment to index failed") segment.enabled = False segment.disabled_at = datetime.datetime.utcnow() segment.status = 'error' segment.error = str(e) db.session.commit() finally: redis_client.delete(indexing_cache_key)
[]
2024-01-10
perzeuss/dify
api~core~tool~dataset_multi_retriever_tool.py
import json import threading from typing import Type, Optional, List from flask import current_app, Flask from langchain.tools import BaseTool from pydantic import Field, BaseModel from core.callback_handler.index_tool_callback_handler import DatasetIndexToolCallbackHandler from core.conversation_message_task import ConversationMessageTask from core.embedding.cached_embedding import CacheEmbedding from core.index.keyword_table_index.keyword_table_index import KeywordTableIndex, KeywordTableConfig from core.model_providers.error import LLMBadRequestError, ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from extensions.ext_database import db from models.dataset import Dataset, DocumentSegment, Document from services.retrieval_service import RetrievalService default_retrieval_model = { 'search_method': 'semantic_search', 'reranking_enable': False, 'reranking_model': { 'reranking_provider_name': '', 'reranking_model_name': '' }, 'top_k': 2, 'score_threshold_enable': False } class DatasetMultiRetrieverToolInput(BaseModel): query: str = Field(..., description="dataset multi retriever and rerank") class DatasetMultiRetrieverTool(BaseTool): """Tool for querying multi dataset.""" name: str = "dataset-" args_schema: Type[BaseModel] = DatasetMultiRetrieverToolInput description: str = "dataset multi retriever and rerank. " tenant_id: str dataset_ids: List[str] top_k: int = 2 score_threshold: Optional[float] = None reranking_provider_name: str reranking_model_name: str conversation_message_task: ConversationMessageTask return_resource: bool retriever_from: str @classmethod def from_dataset(cls, dataset_ids: List[str], tenant_id: str, **kwargs): return cls( name=f'dataset-{tenant_id}', tenant_id=tenant_id, dataset_ids=dataset_ids, **kwargs ) def _run(self, query: str) -> str: threads = [] all_documents = [] for dataset_id in self.dataset_ids: retrieval_thread = threading.Thread(target=self._retriever, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset_id': dataset_id, 'query': query, 'all_documents': all_documents }) threads.append(retrieval_thread) retrieval_thread.start() for thread in threads: thread.join() # do rerank for searched documents rerank = ModelFactory.get_reranking_model( tenant_id=self.tenant_id, model_provider_name=self.reranking_provider_name, model_name=self.reranking_model_name ) all_documents = rerank.rerank(query, all_documents, self.score_threshold, self.top_k) hit_callback = DatasetIndexToolCallbackHandler(self.conversation_message_task) hit_callback.on_tool_end(all_documents) document_score_list = {} for item in all_documents: document_score_list[item.metadata['doc_id']] = item.metadata['score'] document_context_list = [] index_node_ids = [document.metadata['doc_id'] for document in all_documents] segments = DocumentSegment.query.filter( DocumentSegment.completed_at.isnot(None), DocumentSegment.status == 'completed', DocumentSegment.enabled == True, DocumentSegment.index_node_id.in_(index_node_ids) ).all() if segments: index_node_id_to_position = {id: position for position, id in enumerate(index_node_ids)} sorted_segments = sorted(segments, key=lambda segment: index_node_id_to_position.get(segment.index_node_id, float('inf'))) for segment in sorted_segments: if segment.answer: document_context_list.append(f'question:{segment.content} answer:{segment.answer}') else: document_context_list.append(segment.content) if self.return_resource: context_list = [] resource_number = 1 for segment in sorted_segments: dataset = Dataset.query.filter_by( id=segment.dataset_id ).first() document = Document.query.filter(Document.id == segment.document_id, Document.enabled == True, Document.archived == False, ).first() if dataset and document: source = { 'position': resource_number, 'dataset_id': dataset.id, 'dataset_name': dataset.name, 'document_id': document.id, 'document_name': document.name, 'data_source_type': document.data_source_type, 'segment_id': segment.id, 'retriever_from': self.retriever_from, 'score': document_score_list.get(segment.index_node_id, None) } if self.retriever_from == 'dev': source['hit_count'] = segment.hit_count source['word_count'] = segment.word_count source['segment_position'] = segment.position source['index_node_hash'] = segment.index_node_hash if segment.answer: source['content'] = f'question:{segment.content} \nanswer:{segment.answer}' else: source['content'] = segment.content context_list.append(source) resource_number += 1 hit_callback.return_retriever_resource_info(context_list) return str("\n".join(document_context_list)) async def _arun(self, tool_input: str) -> str: raise NotImplementedError() def _retriever(self, flask_app: Flask, dataset_id: str, query: str, all_documents: List): with flask_app.app_context(): dataset = db.session.query(Dataset).filter( Dataset.tenant_id == self.tenant_id, Dataset.id == dataset_id ).first() if not dataset: return [] # get retrieval model , if the model is not setting , using default retrieval_model = dataset.retrieval_model if dataset.retrieval_model else default_retrieval_model if dataset.indexing_technique == "economy": # use keyword table query kw_table_index = KeywordTableIndex( dataset=dataset, config=KeywordTableConfig( max_keywords_per_chunk=5 ) ) documents = kw_table_index.search(query, search_kwargs={'k': self.top_k}) if documents: all_documents.extend(documents) else: try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except LLMBadRequestError: return [] except ProviderTokenNotInitError: return [] embeddings = CacheEmbedding(embedding_model) documents = [] threads = [] if self.top_k > 0: # retrieval_model source with semantic if retrieval_model['search_method'] == 'semantic_search' or retrieval_model[ 'search_method'] == 'hybrid_search': embedding_thread = threading.Thread(target=RetrievalService.embedding_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'top_k': self.top_k, 'score_threshold': self.score_threshold, 'reranking_model': None, 'all_documents': documents, 'search_method': 'hybrid_search', 'embeddings': embeddings }) threads.append(embedding_thread) embedding_thread.start() # retrieval_model source with full text if retrieval_model['search_method'] == 'full_text_search' or retrieval_model[ 'search_method'] == 'hybrid_search': full_text_index_thread = threading.Thread(target=RetrievalService.full_text_index_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'search_method': 'hybrid_search', 'embeddings': embeddings, 'score_threshold': retrieval_model[ 'score_threshold'] if retrieval_model[ 'score_threshold_enable'] else None, 'top_k': self.top_k, 'reranking_model': retrieval_model[ 'reranking_model'] if retrieval_model[ 'reranking_enable'] else None, 'all_documents': documents }) threads.append(full_text_index_thread) full_text_index_thread.start() for thread in threads: thread.join() all_documents.extend(documents)
[ "dataset multi retriever and rerank. " ]
2024-01-10
perzeuss/dify
api~core~agent~agent~multi_dataset_router_agent.py
import json from typing import Tuple, List, Any, Union, Sequence, Optional, cast from langchain.agents import OpenAIFunctionsAgent, BaseSingleActionAgent from langchain.agents.openai_functions_agent.base import _format_intermediate_steps, _parse_ai_message from langchain.callbacks.base import BaseCallbackManager from langchain.callbacks.manager import Callbacks from langchain.prompts.chat import BaseMessagePromptTemplate from langchain.schema import AgentAction, AgentFinish, SystemMessage, Generation, LLMResult, AIMessage from langchain.schema.language_model import BaseLanguageModel from langchain.tools import BaseTool from pydantic import root_validator from core.model_providers.models.entity.message import to_prompt_messages from core.model_providers.models.llm.base import BaseLLM from core.third_party.langchain.llms.fake import FakeLLM class MultiDatasetRouterAgent(OpenAIFunctionsAgent): """ An Multi Dataset Retrieve Agent driven by Router. """ model_instance: BaseLLM class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True @root_validator def validate_llm(cls, values: dict) -> dict: return values def should_use_agent(self, query: str): """ return should use agent :param query: :return: """ return True def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(self.tools) == 0: return AgentFinish(return_values={"output": ''}, log='') elif len(self.tools) == 1: tool = next(iter(self.tools)) rst = tool.run(tool_input={'query': kwargs['input']}) # output = '' # rst_json = json.loads(rst) # for item in rst_json: # output += f'{item["content"]}\n' return AgentFinish(return_values={"output": rst}, log=rst) if intermediate_steps: _, observation = intermediate_steps[-1] return AgentFinish(return_values={"output": observation}, log=observation) try: agent_decision = self.real_plan(intermediate_steps, callbacks, **kwargs) if isinstance(agent_decision, AgentAction): tool_inputs = agent_decision.tool_input if isinstance(tool_inputs, dict) and 'query' in tool_inputs and 'chat_history' not in kwargs: tool_inputs['query'] = kwargs['input'] agent_decision.tool_input = tool_inputs else: agent_decision.return_values['output'] = '' return agent_decision except Exception as e: new_exception = self.model_instance.handle_exceptions(e) raise new_exception def real_plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ agent_scratchpad = _format_intermediate_steps(intermediate_steps) selected_inputs = { k: kwargs[k] for k in self.prompt.input_variables if k != "agent_scratchpad" } full_inputs = dict(**selected_inputs, agent_scratchpad=agent_scratchpad) prompt = self.prompt.format_prompt(**full_inputs) messages = prompt.to_messages() prompt_messages = to_prompt_messages(messages) result = self.model_instance.run( messages=prompt_messages, functions=self.functions, ) ai_message = AIMessage( content=result.content, additional_kwargs={ 'function_call': result.function_call } ) agent_decision = _parse_ai_message(ai_message) return agent_decision async def aplan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: raise NotImplementedError() @classmethod def from_llm_and_tools( cls, model_instance: BaseLLM, tools: Sequence[BaseTool], callback_manager: Optional[BaseCallbackManager] = None, extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None, system_message: Optional[SystemMessage] = SystemMessage( content="You are a helpful AI assistant." ), **kwargs: Any, ) -> BaseSingleActionAgent: prompt = cls.create_prompt( extra_prompt_messages=extra_prompt_messages, system_message=system_message, ) return cls( model_instance=model_instance, llm=FakeLLM(response=''), prompt=prompt, tools=tools, callback_manager=callback_manager, **kwargs, )
[ "You are a helpful AI assistant." ]
2024-01-10
perzeuss/dify
api~core~callback_handler~index_tool_callback_handler.py
from typing import List from langchain.schema import Document from core.conversation_message_task import ConversationMessageTask from extensions.ext_database import db from models.dataset import DocumentSegment class DatasetIndexToolCallbackHandler: """Callback handler for dataset tool.""" def __init__(self, conversation_message_task: ConversationMessageTask) -> None: self.conversation_message_task = conversation_message_task def on_tool_end(self, documents: List[Document]) -> None: """Handle tool end.""" for document in documents: doc_id = document.metadata['doc_id'] # add hit count to document segment db.session.query(DocumentSegment).filter( DocumentSegment.index_node_id == doc_id ).update( {DocumentSegment.hit_count: DocumentSegment.hit_count + 1}, synchronize_session=False ) db.session.commit() def return_retriever_resource_info(self, resource: List): """Handle return_retriever_resource_info.""" self.conversation_message_task.on_dataset_query_finish(resource)
[]
2024-01-10
perzeuss/dify
api~core~tool~dataset_retriever_tool.py
import json import threading from typing import Type, Optional, List from flask import current_app from langchain.tools import BaseTool from pydantic import Field, BaseModel from core.callback_handler.index_tool_callback_handler import DatasetIndexToolCallbackHandler from core.conversation_message_task import ConversationMessageTask from core.embedding.cached_embedding import CacheEmbedding from core.index.keyword_table_index.keyword_table_index import KeywordTableIndex, KeywordTableConfig from core.index.vector_index.vector_index import VectorIndex from core.model_providers.error import LLMBadRequestError, ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from extensions.ext_database import db from models.dataset import Dataset, DocumentSegment, Document from services.retrieval_service import RetrievalService default_retrieval_model = { 'search_method': 'semantic_search', 'reranking_enable': False, 'reranking_model': { 'reranking_provider_name': '', 'reranking_model_name': '' }, 'top_k': 2, 'score_threshold_enable': False } class DatasetRetrieverToolInput(BaseModel): query: str = Field(..., description="Query for the dataset to be used to retrieve the dataset.") class DatasetRetrieverTool(BaseTool): """Tool for querying a Dataset.""" name: str = "dataset" args_schema: Type[BaseModel] = DatasetRetrieverToolInput description: str = "use this to retrieve a dataset. " tenant_id: str dataset_id: str top_k: int = 2 score_threshold: Optional[float] = None conversation_message_task: ConversationMessageTask return_resource: bool retriever_from: str @classmethod def from_dataset(cls, dataset: Dataset, **kwargs): description = dataset.description if not description: description = 'useful for when you want to answer queries about the ' + dataset.name description = description.replace('\n', '').replace('\r', '') return cls( name=f'dataset-{dataset.id}', tenant_id=dataset.tenant_id, dataset_id=dataset.id, description=description, **kwargs ) def _run(self, query: str) -> str: dataset = db.session.query(Dataset).filter( Dataset.tenant_id == self.tenant_id, Dataset.id == self.dataset_id ).first() if not dataset: return '' # get retrieval model , if the model is not setting , using default retrieval_model = dataset.retrieval_model if dataset.retrieval_model else default_retrieval_model if dataset.indexing_technique == "economy": # use keyword table query kw_table_index = KeywordTableIndex( dataset=dataset, config=KeywordTableConfig( max_keywords_per_chunk=5 ) ) documents = kw_table_index.search(query, search_kwargs={'k': self.top_k}) return str("\n".join([document.page_content for document in documents])) else: try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except LLMBadRequestError: return '' except ProviderTokenNotInitError: return '' embeddings = CacheEmbedding(embedding_model) documents = [] threads = [] if self.top_k > 0: # retrieval source with semantic if retrieval_model['search_method'] == 'semantic_search' or retrieval_model['search_method'] == 'hybrid_search': embedding_thread = threading.Thread(target=RetrievalService.embedding_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'top_k': self.top_k, 'score_threshold': retrieval_model['score_threshold'] if retrieval_model[ 'score_threshold_enable'] else None, 'reranking_model': retrieval_model['reranking_model'] if retrieval_model[ 'reranking_enable'] else None, 'all_documents': documents, 'search_method': retrieval_model['search_method'], 'embeddings': embeddings }) threads.append(embedding_thread) embedding_thread.start() # retrieval_model source with full text if retrieval_model['search_method'] == 'full_text_search' or retrieval_model['search_method'] == 'hybrid_search': full_text_index_thread = threading.Thread(target=RetrievalService.full_text_index_search, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'query': query, 'search_method': retrieval_model['search_method'], 'embeddings': embeddings, 'score_threshold': retrieval_model['score_threshold'] if retrieval_model[ 'score_threshold_enable'] else None, 'top_k': self.top_k, 'reranking_model': retrieval_model['reranking_model'] if retrieval_model[ 'reranking_enable'] else None, 'all_documents': documents }) threads.append(full_text_index_thread) full_text_index_thread.start() for thread in threads: thread.join() # hybrid search: rerank after all documents have been searched if retrieval_model['search_method'] == 'hybrid_search': hybrid_rerank = ModelFactory.get_reranking_model( tenant_id=dataset.tenant_id, model_provider_name=retrieval_model['reranking_model']['reranking_provider_name'], model_name=retrieval_model['reranking_model']['reranking_model_name'] ) documents = hybrid_rerank.rerank(query, documents, retrieval_model['score_threshold'] if retrieval_model['score_threshold_enable'] else None, self.top_k) else: documents = [] hit_callback = DatasetIndexToolCallbackHandler(self.conversation_message_task) hit_callback.on_tool_end(documents) document_score_list = {} if dataset.indexing_technique != "economy": for item in documents: document_score_list[item.metadata['doc_id']] = item.metadata['score'] document_context_list = [] index_node_ids = [document.metadata['doc_id'] for document in documents] segments = DocumentSegment.query.filter(DocumentSegment.dataset_id == self.dataset_id, DocumentSegment.completed_at.isnot(None), DocumentSegment.status == 'completed', DocumentSegment.enabled == True, DocumentSegment.index_node_id.in_(index_node_ids) ).all() if segments: index_node_id_to_position = {id: position for position, id in enumerate(index_node_ids)} sorted_segments = sorted(segments, key=lambda segment: index_node_id_to_position.get(segment.index_node_id, float('inf'))) for segment in sorted_segments: if segment.answer: document_context_list.append(f'question:{segment.content} answer:{segment.answer}') else: document_context_list.append(segment.content) if self.return_resource: context_list = [] resource_number = 1 for segment in sorted_segments: context = {} document = Document.query.filter(Document.id == segment.document_id, Document.enabled == True, Document.archived == False, ).first() if dataset and document: source = { 'position': resource_number, 'dataset_id': dataset.id, 'dataset_name': dataset.name, 'document_id': document.id, 'document_name': document.name, 'data_source_type': document.data_source_type, 'segment_id': segment.id, 'retriever_from': self.retriever_from, 'score': document_score_list.get(segment.index_node_id, None) } if self.retriever_from == 'dev': source['hit_count'] = segment.hit_count source['word_count'] = segment.word_count source['segment_position'] = segment.position source['index_node_hash'] = segment.index_node_hash if segment.answer: source['content'] = f'question:{segment.content} \nanswer:{segment.answer}' else: source['content'] = segment.content context_list.append(source) resource_number += 1 hit_callback.return_retriever_resource_info(context_list) return str("\n".join(document_context_list)) async def _arun(self, tool_input: str) -> str: raise NotImplementedError()
[ "use this to retrieve a dataset. " ]
2024-01-10
perzeuss/dify
api~core~index~vector_index~milvus_vector_index.py
from typing import cast, Any, List from langchain.embeddings.base import Embeddings from langchain.schema import Document from langchain.vectorstores import VectorStore from pydantic import BaseModel, root_validator from core.index.base import BaseIndex from core.index.vector_index.base import BaseVectorIndex from core.vector_store.milvus_vector_store import MilvusVectorStore from models.dataset import Dataset class MilvusConfig(BaseModel): host: str port: int user: str password: str secure: bool = False batch_size: int = 100 @root_validator() def validate_config(cls, values: dict) -> dict: if not values['host']: raise ValueError("config MILVUS_HOST is required") if not values['port']: raise ValueError("config MILVUS_PORT is required") if not values['user']: raise ValueError("config MILVUS_USER is required") if not values['password']: raise ValueError("config MILVUS_PASSWORD is required") return values def to_milvus_params(self): return { 'host': self.host, 'port': self.port, 'user': self.user, 'password': self.password, 'secure': self.secure } class MilvusVectorIndex(BaseVectorIndex): def __init__(self, dataset: Dataset, config: MilvusConfig, embeddings: Embeddings): super().__init__(dataset, embeddings) self._client_config = config def get_type(self) -> str: return 'milvus' def get_index_name(self, dataset: Dataset) -> str: if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix class_prefix += '_Node' return class_prefix dataset_id = dataset.id return "Vector_index_" + dataset_id.replace("-", "_") + '_Node' def to_index_struct(self) -> dict: return { "type": self.get_type(), "vector_store": {"class_prefix": self.get_index_name(self.dataset)} } def create(self, texts: list[Document], **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) index_params = { 'metric_type': 'IP', 'index_type': "HNSW", 'params': {"M": 8, "efConstruction": 64} } self._vector_store = MilvusVectorStore.from_documents( texts, self._embeddings, collection_name=self.get_index_name(self.dataset), connection_args=self._client_config.to_milvus_params(), index_params=index_params ) return self def create_with_collection_name(self, texts: list[Document], collection_name: str, **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = MilvusVectorStore.from_documents( texts, self._embeddings, collection_name=collection_name, ids=uuids, content_payload_key='page_content' ) return self def _get_vector_store(self) -> VectorStore: """Only for created index.""" if self._vector_store: return self._vector_store attributes = ['doc_id', 'dataset_id', 'document_id'] return MilvusVectorStore( collection_name=self.get_index_name(self.dataset), embedding_function=self._embeddings, connection_args=self._client_config.to_milvus_params() ) def _get_vector_store_class(self) -> type: return MilvusVectorStore def delete_by_document_id(self, document_id: str): vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) ids = vector_store.get_ids_by_document_id(document_id) if ids: vector_store.del_texts({ 'filter': f'id in {ids}' }) def delete_by_ids(self, doc_ids: list[str]) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) ids = vector_store.get_ids_by_doc_ids(doc_ids) vector_store.del_texts({ 'filter': f' id in {ids}' }) def delete_by_group_id(self, group_id: str) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) vector_store.delete() def delete(self) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="group_id", match=models.MatchValue(value=self.dataset.id), ), ], )) def search_by_full_text_index(self, query: str, **kwargs: Any) -> List[Document]: # milvus/zilliz doesn't support bm25 search return []
[]
2024-01-10
perzeuss/dify
api~core~model_providers~models~reranking~cohere_reranking.py
import logging from typing import Optional, List import cohere import openai from langchain.schema import Document from core.model_providers.error import LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError, \ LLMRateLimitError, LLMAuthorizationError from core.model_providers.models.reranking.base import BaseReranking from core.model_providers.providers.base import BaseModelProvider class CohereReranking(BaseReranking): def __init__(self, model_provider: BaseModelProvider, name: str): self.credentials = model_provider.get_model_credentials( model_name=name, model_type=self.type ) client = cohere.Client(self.credentials.get('api_key')) super().__init__(model_provider, client, name) def rerank(self, query: str, documents: List[Document], score_threshold: Optional[float], top_k: Optional[int]) -> Optional[List[Document]]: docs = [] doc_id = [] for document in documents: if document.metadata['doc_id'] not in doc_id: doc_id.append(document.metadata['doc_id']) docs.append(document.page_content) results = self.client.rerank(query=query, documents=docs, model=self.name, top_n=top_k) rerank_documents = [] for idx, result in enumerate(results): # format document rerank_document = Document( page_content=result.document['text'], metadata={ "doc_id": documents[result.index].metadata['doc_id'], "doc_hash": documents[result.index].metadata['doc_hash'], "document_id": documents[result.index].metadata['document_id'], "dataset_id": documents[result.index].metadata['dataset_id'], 'score': result.relevance_score } ) # score threshold check if score_threshold is not None: if result.relevance_score >= score_threshold: rerank_documents.append(rerank_document) else: rerank_documents.append(rerank_document) return rerank_documents def handle_exceptions(self, ex: Exception) -> Exception: if isinstance(ex, openai.error.InvalidRequestError): logging.warning("Invalid request to OpenAI API.") return LLMBadRequestError(str(ex)) elif isinstance(ex, openai.error.APIConnectionError): logging.warning("Failed to connect to OpenAI API.") return LLMAPIConnectionError(ex.__class__.__name__ + ":" + str(ex)) elif isinstance(ex, (openai.error.APIError, openai.error.ServiceUnavailableError, openai.error.Timeout)): logging.warning("OpenAI service unavailable.") return LLMAPIUnavailableError(ex.__class__.__name__ + ":" + str(ex)) elif isinstance(ex, openai.error.RateLimitError): return LLMRateLimitError(str(ex)) elif isinstance(ex, openai.error.AuthenticationError): return LLMAuthorizationError(str(ex)) elif isinstance(ex, openai.error.OpenAIError): return LLMBadRequestError(ex.__class__.__name__ + ":" + str(ex)) else: return ex
[]
2024-01-10
perzeuss/dify
api~core~model_providers~models~entity~model_params.py
import enum from typing import Optional, TypeVar, Generic from langchain.load.serializable import Serializable from pydantic import BaseModel class ModelMode(enum.Enum): COMPLETION = 'completion' CHAT = 'chat' class ModelType(enum.Enum): TEXT_GENERATION = 'text-generation' EMBEDDINGS = 'embeddings' SPEECH_TO_TEXT = 'speech2text' IMAGE = 'image' VIDEO = 'video' MODERATION = 'moderation' RERANKING = 'reranking' @staticmethod def value_of(value): for member in ModelType: if member.value == value: return member raise ValueError(f"No matching enum found for value '{value}'") class ModelKwargs(BaseModel): max_tokens: Optional[int] temperature: Optional[float] top_p: Optional[float] presence_penalty: Optional[float] frequency_penalty: Optional[float] class KwargRuleType(enum.Enum): STRING = 'string' INTEGER = 'integer' FLOAT = 'float' T = TypeVar('T') class KwargRule(Generic[T], BaseModel): enabled: bool = True min: Optional[T] = None max: Optional[T] = None default: Optional[T] = None alias: Optional[str] = None precision: Optional[int] = None class ModelKwargsRules(BaseModel): max_tokens: KwargRule = KwargRule[int](enabled=False) temperature: KwargRule = KwargRule[float](enabled=False) top_p: KwargRule = KwargRule[float](enabled=False) presence_penalty: KwargRule = KwargRule[float](enabled=False) frequency_penalty: KwargRule = KwargRule[float](enabled=False)
[]
2024-01-10
perzeuss/dify
api~core~orchestrator_rule_parser.py
import json import threading from typing import Optional, List from flask import Flask from langchain import WikipediaAPIWrapper from langchain.callbacks.manager import Callbacks from langchain.memory.chat_memory import BaseChatMemory from langchain.tools import BaseTool, Tool, WikipediaQueryRun from pydantic import BaseModel, Field from core.agent.agent.multi_dataset_router_agent import MultiDatasetRouterAgent from core.agent.agent.output_parser.structured_chat import StructuredChatOutputParser from core.agent.agent.structed_multi_dataset_router_agent import StructuredMultiDatasetRouterAgent from core.agent.agent_executor import AgentExecutor, PlanningStrategy, AgentConfiguration from core.callback_handler.agent_loop_gather_callback_handler import AgentLoopGatherCallbackHandler from core.callback_handler.dataset_tool_callback_handler import DatasetToolCallbackHandler from core.callback_handler.main_chain_gather_callback_handler import MainChainGatherCallbackHandler from core.callback_handler.std_out_callback_handler import DifyStdOutCallbackHandler from core.conversation_message_task import ConversationMessageTask from core.model_providers.error import ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from core.model_providers.models.entity.model_params import ModelKwargs, ModelMode from core.model_providers.models.llm.base import BaseLLM from core.tool.current_datetime_tool import DatetimeTool from core.tool.dataset_multi_retriever_tool import DatasetMultiRetrieverTool from core.tool.dataset_retriever_tool import DatasetRetrieverTool from core.tool.provider.serpapi_provider import SerpAPIToolProvider from core.tool.serpapi_wrapper import OptimizedSerpAPIWrapper, OptimizedSerpAPIInput from core.tool.web_reader_tool import WebReaderTool from extensions.ext_database import db from models.dataset import Dataset, DatasetProcessRule from models.model import AppModelConfig default_retrieval_model = { 'search_method': 'semantic_search', 'reranking_enable': False, 'reranking_model': { 'reranking_provider_name': '', 'reranking_model_name': '' }, 'top_k': 2, 'score_threshold_enable': False } class OrchestratorRuleParser: """Parse the orchestrator rule to entities.""" def __init__(self, tenant_id: str, app_model_config: AppModelConfig): self.tenant_id = tenant_id self.app_model_config = app_model_config def to_agent_executor(self, conversation_message_task: ConversationMessageTask, memory: Optional[BaseChatMemory], rest_tokens: int, chain_callback: MainChainGatherCallbackHandler, tenant_id: str, retriever_from: str = 'dev') -> Optional[AgentExecutor]: if not self.app_model_config.agent_mode_dict: return None agent_mode_config = self.app_model_config.agent_mode_dict model_dict = self.app_model_config.model_dict return_resource = self.app_model_config.retriever_resource_dict.get('enabled', False) chain = None if agent_mode_config and agent_mode_config.get('enabled'): tool_configs = agent_mode_config.get('tools', []) agent_provider_name = model_dict.get('provider', 'openai') agent_model_name = model_dict.get('name', 'gpt-4') dataset_configs = self.app_model_config.dataset_configs_dict agent_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_provider_name, model_name=agent_model_name, model_kwargs=ModelKwargs( temperature=0.2, top_p=0.3, max_tokens=1500 ) ) # add agent callback to record agent thoughts agent_callback = AgentLoopGatherCallbackHandler( model_instance=agent_model_instance, conversation_message_task=conversation_message_task ) chain_callback.agent_callback = agent_callback agent_model_instance.add_callbacks([agent_callback]) planning_strategy = PlanningStrategy(agent_mode_config.get('strategy', 'router')) # only OpenAI chat model (include Azure) support function call, use ReACT instead if not agent_model_instance.support_function_call: if planning_strategy == PlanningStrategy.FUNCTION_CALL: planning_strategy = PlanningStrategy.REACT elif planning_strategy == PlanningStrategy.ROUTER: planning_strategy = PlanningStrategy.REACT_ROUTER try: summary_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_provider_name, model_name=agent_model_name, model_kwargs=ModelKwargs( temperature=0, max_tokens=500 ), deduct_quota=False ) except ProviderTokenNotInitError as e: summary_model_instance = None tools = self.to_tools( tool_configs=tool_configs, callbacks=[agent_callback, DifyStdOutCallbackHandler()], agent_model_instance=agent_model_instance, conversation_message_task=conversation_message_task, rest_tokens=rest_tokens, return_resource=return_resource, retriever_from=retriever_from, dataset_configs=dataset_configs, tenant_id=tenant_id ) if len(tools) == 0: return None agent_configuration = AgentConfiguration( strategy=planning_strategy, model_instance=agent_model_instance, tools=tools, summary_model_instance=summary_model_instance, memory=memory, callbacks=[chain_callback, agent_callback], max_iterations=10, max_execution_time=400.0, early_stopping_method="generate" ) return AgentExecutor(agent_configuration) return chain def to_tools(self, tool_configs: list, callbacks: Callbacks = None, **kwargs) -> list[BaseTool]: """ Convert app agent tool configs to tools :param tool_configs: app agent tool configs :param callbacks: :return: """ tools = [] dataset_tools = [] for tool_config in tool_configs: tool_type = list(tool_config.keys())[0] tool_val = list(tool_config.values())[0] if not tool_val.get("enabled") or tool_val.get("enabled") is not True: continue tool = None if tool_type == "dataset": dataset_tools.append(tool_config) elif tool_type == "web_reader": tool = self.to_web_reader_tool(tool_config=tool_val, **kwargs) elif tool_type == "google_search": tool = self.to_google_search_tool(tool_config=tool_val, **kwargs) elif tool_type == "wikipedia": tool = self.to_wikipedia_tool(tool_config=tool_val, **kwargs) elif tool_type == "current_datetime": tool = self.to_current_datetime_tool(tool_config=tool_val, **kwargs) if tool: if tool.callbacks is not None: tool.callbacks.extend(callbacks) else: tool.callbacks = callbacks tools.append(tool) # format dataset tool if len(dataset_tools) > 0: dataset_retriever_tools = self.to_dataset_retriever_tool(tool_configs=dataset_tools, **kwargs) if dataset_retriever_tools: tools.extend(dataset_retriever_tools) return tools def to_dataset_retriever_tool(self, tool_configs: List, conversation_message_task: ConversationMessageTask, return_resource: bool = False, retriever_from: str = 'dev', **kwargs) \ -> Optional[List[BaseTool]]: """ A dataset tool is a tool that can be used to retrieve information from a dataset :param tool_configs: :param conversation_message_task: :param return_resource: :param retriever_from: :return: """ dataset_configs = kwargs['dataset_configs'] retrieval_model = dataset_configs.get('retrieval_model', 'single') tools = [] dataset_ids = [] tenant_id = None for tool_config in tool_configs: # get dataset from dataset id dataset = db.session.query(Dataset).filter( Dataset.tenant_id == self.tenant_id, Dataset.id == tool_config.get('dataset').get("id") ).first() if not dataset: return None if dataset and dataset.available_document_count == 0 and dataset.available_document_count == 0: return None dataset_ids.append(dataset.id) if retrieval_model == 'single': retrieval_model = dataset.retrieval_model if dataset.retrieval_model else default_retrieval_model top_k = retrieval_model['top_k'] # dynamically adjust top_k when the remaining token number is not enough to support top_k # top_k = self._dynamic_calc_retrieve_k(dataset=dataset, top_k=top_k, rest_tokens=rest_tokens) score_threshold = None score_threshold_enable = retrieval_model.get("score_threshold_enable") if score_threshold_enable: score_threshold = retrieval_model.get("score_threshold") tool = DatasetRetrieverTool.from_dataset( dataset=dataset, top_k=top_k, score_threshold=score_threshold, callbacks=[DatasetToolCallbackHandler(conversation_message_task)], conversation_message_task=conversation_message_task, return_resource=return_resource, retriever_from=retriever_from ) tools.append(tool) if retrieval_model == 'multiple': tool = DatasetMultiRetrieverTool.from_dataset( dataset_ids=dataset_ids, tenant_id=kwargs['tenant_id'], top_k=dataset_configs.get('top_k', 2), score_threshold=dataset_configs.get('score_threshold', 0.5) if dataset_configs.get('score_threshold_enable', False) else None, callbacks=[DatasetToolCallbackHandler(conversation_message_task)], conversation_message_task=conversation_message_task, return_resource=return_resource, retriever_from=retriever_from, reranking_provider_name=dataset_configs.get('reranking_model').get('reranking_provider_name'), reranking_model_name=dataset_configs.get('reranking_model').get('reranking_model_name') ) tools.append(tool) return tools def to_web_reader_tool(self, tool_config: dict, agent_model_instance: BaseLLM, **kwargs) -> Optional[BaseTool]: """ A tool for reading web pages :return: """ try: summary_model_instance = ModelFactory.get_text_generation_model( tenant_id=self.tenant_id, model_provider_name=agent_model_instance.model_provider.provider_name, model_name=agent_model_instance.name, model_kwargs=ModelKwargs( temperature=0, max_tokens=500 ), deduct_quota=False ) except ProviderTokenNotInitError: summary_model_instance = None tool = WebReaderTool( model_instance=summary_model_instance if summary_model_instance else None, max_chunk_length=4000, continue_reading=True ) return tool def to_google_search_tool(self, tool_config: dict, **kwargs) -> Optional[BaseTool]: tool_provider = SerpAPIToolProvider(tenant_id=self.tenant_id) func_kwargs = tool_provider.credentials_to_func_kwargs() if not func_kwargs: return None tool = Tool( name="google_search", description="A tool for performing a Google search and extracting snippets and webpages " "when you need to search for something you don't know or when your information " "is not up to date. " "Input should be a search query.", func=OptimizedSerpAPIWrapper(**func_kwargs).run, args_schema=OptimizedSerpAPIInput ) return tool def to_current_datetime_tool(self, tool_config: dict, **kwargs) -> Optional[BaseTool]: tool = DatetimeTool() return tool def to_wikipedia_tool(self, tool_config: dict, **kwargs) -> Optional[BaseTool]: class WikipediaInput(BaseModel): query: str = Field(..., description="search query.") return WikipediaQueryRun( name="wikipedia", api_wrapper=WikipediaAPIWrapper(doc_content_chars_max=4000), args_schema=WikipediaInput ) @classmethod def _dynamic_calc_retrieve_k(cls, dataset: Dataset, top_k: int, rest_tokens: int) -> int: if rest_tokens == -1: return top_k processing_rule = dataset.latest_process_rule if not processing_rule: return top_k if processing_rule.mode == "custom": rules = processing_rule.rules_dict if not rules: return top_k segmentation = rules["segmentation"] segment_max_tokens = segmentation["max_tokens"] else: segment_max_tokens = DatasetProcessRule.AUTOMATIC_RULES['segmentation']['max_tokens'] # when rest_tokens is less than default context tokens if rest_tokens < segment_max_tokens * top_k: return rest_tokens // segment_max_tokens return min(top_k, 10)
[]
2024-01-10
perzeuss/dify
api~core~model_providers~providers~cohere_provider.py
import json from json import JSONDecodeError from typing import Type from langchain.schema import HumanMessage from core.helper import encrypter from core.model_providers.models.base import BaseProviderModel from core.model_providers.models.entity.model_params import ModelKwargsRules, KwargRule, ModelType, ModelMode from core.model_providers.models.reranking.cohere_reranking import CohereReranking from core.model_providers.providers.base import BaseModelProvider, CredentialsValidateFailedError from models.provider import ProviderType class CohereProvider(BaseModelProvider): @property def provider_name(self): """ Returns the name of a provider. """ return 'cohere' def _get_text_generation_model_mode(self, model_name) -> str: return ModelMode.CHAT.value def _get_fixed_model_list(self, model_type: ModelType) -> list[dict]: if model_type == ModelType.RERANKING: return [ { 'id': 'rerank-english-v2.0', 'name': 'rerank-english-v2.0' }, { 'id': 'rerank-multilingual-v2.0', 'name': 'rerank-multilingual-v2.0' } ] else: return [] def get_model_class(self, model_type: ModelType) -> Type[BaseProviderModel]: """ Returns the model class. :param model_type: :return: """ if model_type == ModelType.RERANKING: model_class = CohereReranking else: raise NotImplementedError return model_class def get_model_parameter_rules(self, model_name: str, model_type: ModelType) -> ModelKwargsRules: """ get model parameter rules. :param model_name: :param model_type: :return: """ return ModelKwargsRules( temperature=KwargRule[float](min=0, max=1, default=0.3, precision=2), top_p=KwargRule[float](min=0, max=0.99, default=0.85, precision=2), presence_penalty=KwargRule[float](enabled=False), frequency_penalty=KwargRule[float](enabled=False), max_tokens=KwargRule[int](enabled=False), ) @classmethod def is_provider_credentials_valid_or_raise(cls, credentials: dict): """ Validates the given credentials. """ if 'api_key' not in credentials: raise CredentialsValidateFailedError('Cohere api_key must be provided.') try: credential_kwargs = { 'api_key': credentials['api_key'], } # todo validate except Exception as ex: raise CredentialsValidateFailedError(str(ex)) @classmethod def encrypt_provider_credentials(cls, tenant_id: str, credentials: dict) -> dict: credentials['api_key'] = encrypter.encrypt_token(tenant_id, credentials['api_key']) return credentials def get_provider_credentials(self, obfuscated: bool = False) -> dict: if self.provider.provider_type == ProviderType.CUSTOM.value: try: credentials = json.loads(self.provider.encrypted_config) except JSONDecodeError: credentials = { 'api_key': None, } if credentials['api_key']: credentials['api_key'] = encrypter.decrypt_token( self.provider.tenant_id, credentials['api_key'] ) if obfuscated: credentials['api_key'] = encrypter.obfuscated_token(credentials['api_key']) return credentials else: return {} def should_deduct_quota(self): return True @classmethod def is_model_credentials_valid_or_raise(cls, model_name: str, model_type: ModelType, credentials: dict): """ check model credentials valid. :param model_name: :param model_type: :param credentials: """ return @classmethod def encrypt_model_credentials(cls, tenant_id: str, model_name: str, model_type: ModelType, credentials: dict) -> dict: """ encrypt model credentials for save. :param tenant_id: :param model_name: :param model_type: :param credentials: :return: """ return {} def get_model_credentials(self, model_name: str, model_type: ModelType, obfuscated: bool = False) -> dict: """ get credentials for llm use. :param model_name: :param model_type: :param obfuscated: :return: """ return self.get_provider_credentials(obfuscated)
[]
2024-01-10
perzeuss/dify
api~core~data_loader~file_extractor.py
import tempfile from pathlib import Path from typing import List, Union, Optional import requests from langchain.document_loaders import TextLoader, Docx2txtLoader, UnstructuredFileLoader, UnstructuredAPIFileLoader from langchain.schema import Document from core.data_loader.loader.csv_loader import CSVLoader from core.data_loader.loader.excel import ExcelLoader from core.data_loader.loader.html import HTMLLoader from core.data_loader.loader.markdown import MarkdownLoader from core.data_loader.loader.pdf import PdfLoader from extensions.ext_storage import storage from models.model import UploadFile SUPPORT_URL_CONTENT_TYPES = ['application/pdf', 'text/plain'] USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" class FileExtractor: @classmethod def load(cls, upload_file: UploadFile, return_text: bool = False, is_automatic: bool = False) -> Union[List[Document] | str]: with tempfile.TemporaryDirectory() as temp_dir: suffix = Path(upload_file.key).suffix file_path = f"{temp_dir}/{next(tempfile._get_candidate_names())}{suffix}" storage.download(upload_file.key, file_path) return cls.load_from_file(file_path, return_text, upload_file, is_automatic) @classmethod def load_from_url(cls, url: str, return_text: bool = False) -> Union[List[Document] | str]: response = requests.get(url, headers={ "User-Agent": USER_AGENT }) with tempfile.TemporaryDirectory() as temp_dir: suffix = Path(url).suffix file_path = f"{temp_dir}/{next(tempfile._get_candidate_names())}{suffix}" with open(file_path, 'wb') as file: file.write(response.content) return cls.load_from_file(file_path, return_text) @classmethod def load_from_file(cls, file_path: str, return_text: bool = False, upload_file: Optional[UploadFile] = None, is_automatic: bool = False) -> Union[List[Document] | str]: input_file = Path(file_path) delimiter = '\n' file_extension = input_file.suffix.lower() if is_automatic: loader = UnstructuredFileLoader( file_path, strategy="hi_res", mode="elements" ) # loader = UnstructuredAPIFileLoader( # file_path=filenames[0], # api_key="FAKE_API_KEY", # ) else: if file_extension == '.xlsx': loader = ExcelLoader(file_path) elif file_extension == '.pdf': loader = PdfLoader(file_path, upload_file=upload_file) elif file_extension in ['.md', '.markdown']: loader = MarkdownLoader(file_path, autodetect_encoding=True) elif file_extension in ['.htm', '.html']: loader = HTMLLoader(file_path) elif file_extension == '.docx': loader = Docx2txtLoader(file_path) elif file_extension == '.csv': loader = CSVLoader(file_path, autodetect_encoding=True) else: # txt loader = TextLoader(file_path, autodetect_encoding=True) return delimiter.join([document.page_content for document in loader.load()]) if return_text else loader.load()
[]
2024-01-10
perzeuss/dify
api~core~vector_store~vector~weaviate.py
"""Wrapper around weaviate vector database.""" from __future__ import annotations import datetime from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type from uuid import uuid4 import numpy as np from langchain.docstore.document import Document from langchain.embeddings.base import Embeddings from langchain.utils import get_from_dict_or_env from langchain.vectorstores.base import VectorStore from langchain.vectorstores.utils import maximal_marginal_relevance def _default_schema(index_name: str) -> Dict: return { "class": index_name, "properties": [ { "name": "text", "dataType": ["text"], } ], } def _create_weaviate_client(**kwargs: Any) -> Any: client = kwargs.get("client") if client is not None: return client weaviate_url = get_from_dict_or_env(kwargs, "weaviate_url", "WEAVIATE_URL") try: # the weaviate api key param should not be mandatory weaviate_api_key = get_from_dict_or_env( kwargs, "weaviate_api_key", "WEAVIATE_API_KEY", None ) except ValueError: weaviate_api_key = None try: import weaviate except ImportError: raise ValueError( "Could not import weaviate python package. " "Please install it with `pip install weaviate-client`" ) auth = ( weaviate.auth.AuthApiKey(api_key=weaviate_api_key) if weaviate_api_key is not None else None ) client = weaviate.Client(weaviate_url, auth_client_secret=auth) return client def _default_score_normalizer(val: float) -> float: return 1 - 1 / (1 + np.exp(val)) def _json_serializable(value: Any) -> Any: if isinstance(value, datetime.datetime): return value.isoformat() return value class Weaviate(VectorStore): """Wrapper around Weaviate vector database. To use, you should have the ``weaviate-client`` python package installed. Example: .. code-block:: python import weaviate from langchain.vectorstores import Weaviate client = weaviate.Client(url=os.environ["WEAVIATE_URL"], ...) weaviate = Weaviate(client, index_name, text_key) """ def __init__( self, client: Any, index_name: str, text_key: str, embedding: Optional[Embeddings] = None, attributes: Optional[List[str]] = None, relevance_score_fn: Optional[ Callable[[float], float] ] = _default_score_normalizer, by_text: bool = True, ): """Initialize with Weaviate client.""" try: import weaviate except ImportError: raise ValueError( "Could not import weaviate python package. " "Please install it with `pip install weaviate-client`." ) if not isinstance(client, weaviate.Client): raise ValueError( f"client should be an instance of weaviate.Client, got {type(client)}" ) self._client = client self._index_name = index_name self._embedding = embedding self._text_key = text_key self._query_attrs = [self._text_key] self.relevance_score_fn = relevance_score_fn self._by_text = by_text if attributes is not None: self._query_attrs.extend(attributes) @property def embeddings(self) -> Optional[Embeddings]: return self._embedding def _select_relevance_score_fn(self) -> Callable[[float], float]: return ( self.relevance_score_fn if self.relevance_score_fn else _default_score_normalizer ) def add_texts( self, texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> List[str]: """Upload texts with metadata (properties) to Weaviate.""" from weaviate.util import get_valid_uuid ids = [] embeddings: Optional[List[List[float]]] = None if self._embedding: if not isinstance(texts, list): texts = list(texts) embeddings = self._embedding.embed_documents(texts) with self._client.batch as batch: for i, text in enumerate(texts): data_properties = {self._text_key: text} if metadatas is not None: for key, val in metadatas[i].items(): data_properties[key] = _json_serializable(val) # Allow for ids (consistent w/ other methods) # # Or uuids (backwards compatble w/ existing arg) # If the UUID of one of the objects already exists # then the existing object will be replaced by the new object. _id = get_valid_uuid(uuid4()) if "uuids" in kwargs: _id = kwargs["uuids"][i] elif "ids" in kwargs: _id = kwargs["ids"][i] batch.add_data_object( data_object=data_properties, class_name=self._index_name, uuid=_id, vector=embeddings[i] if embeddings else None, ) ids.append(_id) return ids def similarity_search( self, query: str, k: int = 4, **kwargs: Any ) -> List[Document]: """Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. Returns: List of Documents most similar to the query. """ if self._by_text: return self.similarity_search_by_text(query, k, **kwargs) else: if self._embedding is None: raise ValueError( "_embedding cannot be None for similarity_search when " "_by_text=False" ) embedding = self._embedding.embed_query(query) return self.similarity_search_by_vector(embedding, k, **kwargs) def similarity_search_by_text( self, query: str, k: int = 4, **kwargs: Any ) -> List[Document]: """Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. Returns: List of Documents most similar to the query. """ content: Dict[str, Any] = {"concepts": [query]} if kwargs.get("search_distance"): content["certainty"] = kwargs.get("search_distance") query_obj = self._client.query.get(self._index_name, self._query_attrs) if kwargs.get("where_filter"): query_obj = query_obj.with_where(kwargs.get("where_filter")) if kwargs.get("additional"): query_obj = query_obj.with_additional(kwargs.get("additional")) result = query_obj.with_near_text(content).with_limit(k).do() if "errors" in result: raise ValueError(f"Error during query: {result['errors']}") docs = [] for res in result["data"]["Get"][self._index_name]: text = res.pop(self._text_key) docs.append(Document(page_content=text, metadata=res)) return docs def similarity_search_by_bm25( self, query: str, k: int = 4, **kwargs: Any ) -> List[Document]: """Return docs using BM25F. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. Returns: List of Documents most similar to the query. """ content: Dict[str, Any] = {"concepts": [query]} if kwargs.get("search_distance"): content["certainty"] = kwargs.get("search_distance") query_obj = self._client.query.get(self._index_name, self._query_attrs) if kwargs.get("where_filter"): query_obj = query_obj.with_where(kwargs.get("where_filter")) if kwargs.get("additional"): query_obj = query_obj.with_additional(kwargs.get("additional")) result = query_obj.with_bm25(query=content).with_limit(k).do() if "errors" in result: raise ValueError(f"Error during query: {result['errors']}") docs = [] for res in result["data"]["Get"][self._index_name]: text = res.pop(self._text_key) docs.append(Document(page_content=text, metadata=res)) return docs def similarity_search_by_vector( self, embedding: List[float], k: int = 4, **kwargs: Any ) -> List[Document]: """Look up similar documents by embedding vector in Weaviate.""" vector = {"vector": embedding} query_obj = self._client.query.get(self._index_name, self._query_attrs) if kwargs.get("where_filter"): query_obj = query_obj.with_where(kwargs.get("where_filter")) if kwargs.get("additional"): query_obj = query_obj.with_additional(kwargs.get("additional")) result = query_obj.with_near_vector(vector).with_limit(k).do() if "errors" in result: raise ValueError(f"Error during query: {result['errors']}") docs = [] for res in result["data"]["Get"][self._index_name]: text = res.pop(self._text_key) docs.append(Document(page_content=text, metadata=res)) return docs def max_marginal_relevance_search( self, query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any, ) -> List[Document]: """Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. fetch_k: Number of Documents to fetch to pass to MMR algorithm. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5. Returns: List of Documents selected by maximal marginal relevance. """ if self._embedding is not None: embedding = self._embedding.embed_query(query) else: raise ValueError( "max_marginal_relevance_search requires a suitable Embeddings object" ) return self.max_marginal_relevance_search_by_vector( embedding, k=k, fetch_k=fetch_k, lambda_mult=lambda_mult, **kwargs ) def max_marginal_relevance_search_by_vector( self, embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any, ) -> List[Document]: """Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. fetch_k: Number of Documents to fetch to pass to MMR algorithm. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5. Returns: List of Documents selected by maximal marginal relevance. """ vector = {"vector": embedding} query_obj = self._client.query.get(self._index_name, self._query_attrs) if kwargs.get("where_filter"): query_obj = query_obj.with_where(kwargs.get("where_filter")) results = ( query_obj.with_additional("vector") .with_near_vector(vector) .with_limit(fetch_k) .do() ) payload = results["data"]["Get"][self._index_name] embeddings = [result["_additional"]["vector"] for result in payload] mmr_selected = maximal_marginal_relevance( np.array(embedding), embeddings, k=k, lambda_mult=lambda_mult ) docs = [] for idx in mmr_selected: text = payload[idx].pop(self._text_key) payload[idx].pop("_additional") meta = payload[idx] docs.append(Document(page_content=text, metadata=meta)) return docs def similarity_search_with_score( self, query: str, k: int = 4, **kwargs: Any ) -> List[Tuple[Document, float]]: """ Return list of documents most similar to the query text and cosine distance in float for each. Lower score represents more similarity. """ if self._embedding is None: raise ValueError( "_embedding cannot be None for similarity_search_with_score" ) content: Dict[str, Any] = {"concepts": [query]} if kwargs.get("search_distance"): content["certainty"] = kwargs.get("search_distance") query_obj = self._client.query.get(self._index_name, self._query_attrs) embedded_query = self._embedding.embed_query(query) if not self._by_text: vector = {"vector": embedded_query} result = ( query_obj.with_near_vector(vector) .with_limit(k) .with_additional("vector") .do() ) else: result = ( query_obj.with_near_text(content) .with_limit(k) .with_additional("vector") .do() ) if "errors" in result: raise ValueError(f"Error during query: {result['errors']}") docs_and_scores = [] for res in result["data"]["Get"][self._index_name]: text = res.pop(self._text_key) score = np.dot(res["_additional"]["vector"], embedded_query) docs_and_scores.append((Document(page_content=text, metadata=res), score)) return docs_and_scores @classmethod def from_texts( cls: Type[Weaviate], texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> Weaviate: """Construct Weaviate wrapper from raw documents. This is a user-friendly interface that: 1. Embeds documents. 2. Creates a new index for the embeddings in the Weaviate instance. 3. Adds the documents to the newly created Weaviate index. This is intended to be a quick way to get started. Example: .. code-block:: python from langchain.vectorstores.weaviate import Weaviate from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings() weaviate = Weaviate.from_texts( texts, embeddings, weaviate_url="http://localhost:8080" ) """ client = _create_weaviate_client(**kwargs) from weaviate.util import get_valid_uuid index_name = kwargs.get("index_name", f"LangChain_{uuid4().hex}") embeddings = embedding.embed_documents(texts) if embedding else None text_key = "text" schema = _default_schema(index_name) attributes = list(metadatas[0].keys()) if metadatas else None # check whether the index already exists if not client.schema.contains(schema): client.schema.create_class(schema) with client.batch as batch: for i, text in enumerate(texts): data_properties = { text_key: text, } if metadatas is not None: for key in metadatas[i].keys(): data_properties[key] = metadatas[i][key] # If the UUID of one of the objects already exists # then the existing objectwill be replaced by the new object. if "uuids" in kwargs: _id = kwargs["uuids"][i] else: _id = get_valid_uuid(uuid4()) # if an embedding strategy is not provided, we let # weaviate create the embedding. Note that this will only # work if weaviate has been installed with a vectorizer module # like text2vec-contextionary for example params = { "uuid": _id, "data_object": data_properties, "class_name": index_name, } if embeddings is not None: params["vector"] = embeddings[i] batch.add_data_object(**params) batch.flush() relevance_score_fn = kwargs.get("relevance_score_fn") by_text: bool = kwargs.get("by_text", False) return cls( client, index_name, text_key, embedding=embedding, attributes=attributes, relevance_score_fn=relevance_score_fn, by_text=by_text, ) def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> None: """Delete by vector IDs. Args: ids: List of ids to delete. """ if ids is None: raise ValueError("No ids provided to delete.") # TODO: Check if this can be done in bulk for id in ids: self._client.data_object.delete(uuid=id)
[]
2024-01-10
perzeuss/dify
api~core~indexing_runner.py
import datetime import json import logging import re import threading import time import uuid from typing import Optional, List, cast from flask import current_app, Flask from flask_login import current_user from langchain.schema import Document from langchain.text_splitter import RecursiveCharacterTextSplitter, TextSplitter from sqlalchemy.orm.exc import ObjectDeletedError from core.data_loader.file_extractor import FileExtractor from core.data_loader.loader.notion import NotionLoader from core.docstore.dataset_docstore import DatesetDocumentStore from core.generator.llm_generator import LLMGenerator from core.index.index import IndexBuilder from core.model_providers.error import ProviderTokenNotInitError from core.model_providers.model_factory import ModelFactory from core.model_providers.models.entity.message import MessageType from core.spiltter.fixed_text_splitter import FixedRecursiveCharacterTextSplitter from extensions.ext_database import db from extensions.ext_redis import redis_client from extensions.ext_storage import storage from libs import helper from models.dataset import Document as DatasetDocument from models.dataset import Dataset, DocumentSegment, DatasetProcessRule from models.model import UploadFile from models.source import DataSourceBinding class IndexingRunner: def __init__(self): self.storage = storage def run(self, dataset_documents: List[DatasetDocument]): """Run the indexing process.""" for dataset_document in dataset_documents: try: # get dataset dataset = Dataset.query.filter_by( id=dataset_document.dataset_id ).first() if not dataset: raise ValueError("no dataset found") # get the process rule processing_rule = db.session.query(DatasetProcessRule). \ filter(DatasetProcessRule.id == dataset_document.dataset_process_rule_id). \ first() # load file text_docs = self._load_data(dataset_document) # get splitter splitter = self._get_splitter(processing_rule) # split to documents documents = self._step_split( text_docs=text_docs, splitter=splitter, dataset=dataset, dataset_document=dataset_document, processing_rule=processing_rule ) self._build_index( dataset=dataset, dataset_document=dataset_document, documents=documents ) except DocumentIsPausedException: raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id)) except ProviderTokenNotInitError as e: dataset_document.indexing_status = 'error' dataset_document.error = str(e.description) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() except ObjectDeletedError: logging.warning('Document deleted, document id: {}'.format(dataset_document.id)) except Exception as e: logging.exception("consume document failed") dataset_document.indexing_status = 'error' dataset_document.error = str(e) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() def run_in_splitting_status(self, dataset_document: DatasetDocument): """Run the indexing process when the index_status is splitting.""" try: # get dataset dataset = Dataset.query.filter_by( id=dataset_document.dataset_id ).first() if not dataset: raise ValueError("no dataset found") # get exist document_segment list and delete document_segments = DocumentSegment.query.filter_by( dataset_id=dataset.id, document_id=dataset_document.id ).all() db.session.delete(document_segments) db.session.commit() # load file text_docs = self._load_data(dataset_document) # get the process rule processing_rule = db.session.query(DatasetProcessRule). \ filter(DatasetProcessRule.id == dataset_document.dataset_process_rule_id). \ first() # get splitter splitter = self._get_splitter(processing_rule) # split to documents documents = self._step_split( text_docs=text_docs, splitter=splitter, dataset=dataset, dataset_document=dataset_document, processing_rule=processing_rule ) # build index self._build_index( dataset=dataset, dataset_document=dataset_document, documents=documents ) except DocumentIsPausedException: raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id)) except ProviderTokenNotInitError as e: dataset_document.indexing_status = 'error' dataset_document.error = str(e.description) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() except Exception as e: logging.exception("consume document failed") dataset_document.indexing_status = 'error' dataset_document.error = str(e) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() def run_in_indexing_status(self, dataset_document: DatasetDocument): """Run the indexing process when the index_status is indexing.""" try: # get dataset dataset = Dataset.query.filter_by( id=dataset_document.dataset_id ).first() if not dataset: raise ValueError("no dataset found") # get exist document_segment list and delete document_segments = DocumentSegment.query.filter_by( dataset_id=dataset.id, document_id=dataset_document.id ).all() documents = [] if document_segments: for document_segment in document_segments: # transform segment to node if document_segment.status != "completed": document = Document( page_content=document_segment.content, metadata={ "doc_id": document_segment.index_node_id, "doc_hash": document_segment.index_node_hash, "document_id": document_segment.document_id, "dataset_id": document_segment.dataset_id, } ) documents.append(document) # build index self._build_index( dataset=dataset, dataset_document=dataset_document, documents=documents ) except DocumentIsPausedException: raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id)) except ProviderTokenNotInitError as e: dataset_document.indexing_status = 'error' dataset_document.error = str(e.description) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() except Exception as e: logging.exception("consume document failed") dataset_document.indexing_status = 'error' dataset_document.error = str(e) dataset_document.stopped_at = datetime.datetime.utcnow() db.session.commit() def file_indexing_estimate(self, tenant_id: str, file_details: List[UploadFile], tmp_processing_rule: dict, doc_form: str = None, doc_language: str = 'English', dataset_id: str = None, indexing_technique: str = 'economy') -> dict: """ Estimate the indexing for the document. """ embedding_model = None if dataset_id: dataset = Dataset.query.filter_by( id=dataset_id ).first() if not dataset: raise ValueError('Dataset not found.') if dataset.indexing_technique == 'high_quality' or indexing_technique == 'high_quality': embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) else: if indexing_technique == 'high_quality': embedding_model = ModelFactory.get_embedding_model( tenant_id=tenant_id ) tokens = 0 preview_texts = [] total_segments = 0 for file_detail in file_details: # load data from file text_docs = FileExtractor.load(file_detail) processing_rule = DatasetProcessRule( mode=tmp_processing_rule["mode"], rules=json.dumps(tmp_processing_rule["rules"]) ) # get splitter splitter = self._get_splitter(processing_rule) # split to documents documents = self._split_to_documents_for_estimate( text_docs=text_docs, splitter=splitter, processing_rule=processing_rule ) total_segments += len(documents) for document in documents: if len(preview_texts) < 5: preview_texts.append(document.page_content) if indexing_technique == 'high_quality' or embedding_model: tokens += embedding_model.get_num_tokens(self.filter_string(document.page_content)) if doc_form and doc_form == 'qa_model': text_generation_model = ModelFactory.get_text_generation_model( tenant_id=tenant_id ) if len(preview_texts) > 0: # qa model document response = LLMGenerator.generate_qa_document(current_user.current_tenant_id, preview_texts[0], doc_language) document_qa_list = self.format_split_text(response) return { "total_segments": total_segments * 20, "tokens": total_segments * 2000, "total_price": '{:f}'.format( text_generation_model.calc_tokens_price(total_segments * 2000, MessageType.USER)), "currency": embedding_model.get_currency(), "qa_preview": document_qa_list, "preview": preview_texts } return { "total_segments": total_segments, "tokens": tokens, "total_price": '{:f}'.format(embedding_model.calc_tokens_price(tokens)) if embedding_model else 0, "currency": embedding_model.get_currency() if embedding_model else 'USD', "preview": preview_texts } def notion_indexing_estimate(self, tenant_id: str, notion_info_list: list, tmp_processing_rule: dict, doc_form: str = None, doc_language: str = 'English', dataset_id: str = None, indexing_technique: str = 'economy') -> dict: """ Estimate the indexing for the document. """ embedding_model = None if dataset_id: dataset = Dataset.query.filter_by( id=dataset_id ).first() if not dataset: raise ValueError('Dataset not found.') if dataset.indexing_technique == 'high_quality' or indexing_technique == 'high_quality': embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) else: if indexing_technique == 'high_quality': embedding_model = ModelFactory.get_embedding_model( tenant_id=tenant_id ) # load data from notion tokens = 0 preview_texts = [] total_segments = 0 for notion_info in notion_info_list: workspace_id = notion_info['workspace_id'] data_source_binding = DataSourceBinding.query.filter( db.and_( DataSourceBinding.tenant_id == current_user.current_tenant_id, DataSourceBinding.provider == 'notion', DataSourceBinding.disabled == False, DataSourceBinding.source_info['workspace_id'] == f'"{workspace_id}"' ) ).first() if not data_source_binding: raise ValueError('Data source binding not found.') for page in notion_info['pages']: loader = NotionLoader( notion_access_token=data_source_binding.access_token, notion_workspace_id=workspace_id, notion_obj_id=page['page_id'], notion_page_type=page['type'] ) documents = loader.load() processing_rule = DatasetProcessRule( mode=tmp_processing_rule["mode"], rules=json.dumps(tmp_processing_rule["rules"]) ) # get splitter splitter = self._get_splitter(processing_rule) # split to documents documents = self._split_to_documents_for_estimate( text_docs=documents, splitter=splitter, processing_rule=processing_rule ) total_segments += len(documents) for document in documents: if len(preview_texts) < 5: preview_texts.append(document.page_content) if indexing_technique == 'high_quality' or embedding_model: tokens += embedding_model.get_num_tokens(document.page_content) if doc_form and doc_form == 'qa_model': text_generation_model = ModelFactory.get_text_generation_model( tenant_id=tenant_id ) if len(preview_texts) > 0: # qa model document response = LLMGenerator.generate_qa_document(current_user.current_tenant_id, preview_texts[0], doc_language) document_qa_list = self.format_split_text(response) return { "total_segments": total_segments * 20, "tokens": total_segments * 2000, "total_price": '{:f}'.format( text_generation_model.calc_tokens_price(total_segments * 2000, MessageType.USER)), "currency": embedding_model.get_currency(), "qa_preview": document_qa_list, "preview": preview_texts } return { "total_segments": total_segments, "tokens": tokens, "total_price": '{:f}'.format(embedding_model.calc_tokens_price(tokens)) if embedding_model else 0, "currency": embedding_model.get_currency() if embedding_model else 'USD', "preview": preview_texts } def _load_data(self, dataset_document: DatasetDocument, automatic: bool = False) -> List[Document]: # load file if dataset_document.data_source_type not in ["upload_file", "notion_import"]: return [] data_source_info = dataset_document.data_source_info_dict text_docs = [] if dataset_document.data_source_type == 'upload_file': if not data_source_info or 'upload_file_id' not in data_source_info: raise ValueError("no upload file found") file_detail = db.session.query(UploadFile). \ filter(UploadFile.id == data_source_info['upload_file_id']). \ one_or_none() if file_detail: text_docs = FileExtractor.load(file_detail, is_automatic=False) elif dataset_document.data_source_type == 'notion_import': loader = NotionLoader.from_document(dataset_document) text_docs = loader.load() # update document status to splitting self._update_document_index_status( document_id=dataset_document.id, after_indexing_status="splitting", extra_update_params={ DatasetDocument.word_count: sum([len(text_doc.page_content) for text_doc in text_docs]), DatasetDocument.parsing_completed_at: datetime.datetime.utcnow() } ) # replace doc id to document model id text_docs = cast(List[Document], text_docs) for text_doc in text_docs: # remove invalid symbol text_doc.page_content = self.filter_string(text_doc.page_content) text_doc.metadata['document_id'] = dataset_document.id text_doc.metadata['dataset_id'] = dataset_document.dataset_id return text_docs def filter_string(self, text): text = re.sub(r'<\|', '<', text) text = re.sub(r'\|>', '>', text) text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\x80-\xFF]', '', text) return text def _get_splitter(self, processing_rule: DatasetProcessRule) -> TextSplitter: """ Get the NodeParser object according to the processing rule. """ if processing_rule.mode == "custom": # The user-defined segmentation rule rules = json.loads(processing_rule.rules) segmentation = rules["segmentation"] if segmentation["max_tokens"] < 50 or segmentation["max_tokens"] > 1000: raise ValueError("Custom segment length should be between 50 and 1000.") separator = segmentation["separator"] if separator: separator = separator.replace('\\n', '\n') character_splitter = FixedRecursiveCharacterTextSplitter.from_tiktoken_encoder( chunk_size=segmentation["max_tokens"], chunk_overlap=0, fixed_separator=separator, separators=["\n\n", "。", ".", " ", ""] ) else: # Automatic segmentation character_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder( chunk_size=DatasetProcessRule.AUTOMATIC_RULES['segmentation']['max_tokens'], chunk_overlap=0, separators=["\n\n", "。", ".", " ", ""] ) return character_splitter def _step_split(self, text_docs: List[Document], splitter: TextSplitter, dataset: Dataset, dataset_document: DatasetDocument, processing_rule: DatasetProcessRule) \ -> List[Document]: """ Split the text documents into documents and save them to the document segment. """ documents = self._split_to_documents( text_docs=text_docs, splitter=splitter, processing_rule=processing_rule, tenant_id=dataset.tenant_id, document_form=dataset_document.doc_form, document_language=dataset_document.doc_language ) # save node to document segment doc_store = DatesetDocumentStore( dataset=dataset, user_id=dataset_document.created_by, document_id=dataset_document.id ) # add document segments doc_store.add_documents(documents) # update document status to indexing cur_time = datetime.datetime.utcnow() self._update_document_index_status( document_id=dataset_document.id, after_indexing_status="indexing", extra_update_params={ DatasetDocument.cleaning_completed_at: cur_time, DatasetDocument.splitting_completed_at: cur_time, } ) # update segment status to indexing self._update_segments_by_document( dataset_document_id=dataset_document.id, update_params={ DocumentSegment.status: "indexing", DocumentSegment.indexing_at: datetime.datetime.utcnow() } ) return documents def _split_to_documents(self, text_docs: List[Document], splitter: TextSplitter, processing_rule: DatasetProcessRule, tenant_id: str, document_form: str, document_language: str) -> List[Document]: """ Split the text documents into nodes. """ all_documents = [] all_qa_documents = [] for text_doc in text_docs: # document clean document_text = self._document_clean(text_doc.page_content, processing_rule) text_doc.page_content = document_text # parse document to nodes documents = splitter.split_documents([text_doc]) split_documents = [] for document_node in documents: if document_node.page_content.strip(): doc_id = str(uuid.uuid4()) hash = helper.generate_text_hash(document_node.page_content) document_node.metadata['doc_id'] = doc_id document_node.metadata['doc_hash'] = hash split_documents.append(document_node) all_documents.extend(split_documents) # processing qa document if document_form == 'qa_model': for i in range(0, len(all_documents), 10): threads = [] sub_documents = all_documents[i:i + 10] for doc in sub_documents: document_format_thread = threading.Thread(target=self.format_qa_document, kwargs={ 'flask_app': current_app._get_current_object(), 'tenant_id': tenant_id, 'document_node': doc, 'all_qa_documents': all_qa_documents, 'document_language': document_language}) threads.append(document_format_thread) document_format_thread.start() for thread in threads: thread.join() return all_qa_documents return all_documents def format_qa_document(self, flask_app: Flask, tenant_id: str, document_node, all_qa_documents, document_language): format_documents = [] if document_node.page_content is None or not document_node.page_content.strip(): return with flask_app.app_context(): try: # qa model document response = LLMGenerator.generate_qa_document(tenant_id, document_node.page_content, document_language) document_qa_list = self.format_split_text(response) qa_documents = [] for result in document_qa_list: qa_document = Document(page_content=result['question'], metadata=document_node.metadata.copy()) doc_id = str(uuid.uuid4()) hash = helper.generate_text_hash(result['question']) qa_document.metadata['answer'] = result['answer'] qa_document.metadata['doc_id'] = doc_id qa_document.metadata['doc_hash'] = hash qa_documents.append(qa_document) format_documents.extend(qa_documents) except Exception as e: logging.exception(e) all_qa_documents.extend(format_documents) def _split_to_documents_for_estimate(self, text_docs: List[Document], splitter: TextSplitter, processing_rule: DatasetProcessRule) -> List[Document]: """ Split the text documents into nodes. """ all_documents = [] for text_doc in text_docs: # document clean document_text = self._document_clean(text_doc.page_content, processing_rule) text_doc.page_content = document_text # parse document to nodes documents = splitter.split_documents([text_doc]) split_documents = [] for document in documents: if document.page_content is None or not document.page_content.strip(): continue doc_id = str(uuid.uuid4()) hash = helper.generate_text_hash(document.page_content) document.metadata['doc_id'] = doc_id document.metadata['doc_hash'] = hash split_documents.append(document) all_documents.extend(split_documents) return all_documents def _document_clean(self, text: str, processing_rule: DatasetProcessRule) -> str: """ Clean the document text according to the processing rules. """ if processing_rule.mode == "automatic": rules = DatasetProcessRule.AUTOMATIC_RULES else: rules = json.loads(processing_rule.rules) if processing_rule.rules else {} if 'pre_processing_rules' in rules: pre_processing_rules = rules["pre_processing_rules"] for pre_processing_rule in pre_processing_rules: if pre_processing_rule["id"] == "remove_extra_spaces" and pre_processing_rule["enabled"] is True: # Remove extra spaces pattern = r'\n{3,}' text = re.sub(pattern, '\n\n', text) pattern = r'[\t\f\r\x20\u00a0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]{2,}' text = re.sub(pattern, ' ', text) elif pre_processing_rule["id"] == "remove_urls_emails" and pre_processing_rule["enabled"] is True: # Remove email pattern = r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)' text = re.sub(pattern, '', text) # Remove URL pattern = r'https?://[^\s]+' text = re.sub(pattern, '', text) return text def format_split_text(self, text): regex = r"Q\d+:\s*(.*?)\s*A\d+:\s*([\s\S]*?)(?=Q|$)" matches = re.findall(regex, text, re.MULTILINE) return [ { "question": q, "answer": re.sub(r"\n\s*", "\n", a.strip()) } for q, a in matches if q and a ] def _build_index(self, dataset: Dataset, dataset_document: DatasetDocument, documents: List[Document]) -> None: """ Build the index for the document. """ vector_index = IndexBuilder.get_index(dataset, 'high_quality') keyword_table_index = IndexBuilder.get_index(dataset, 'economy') embedding_model = None if dataset.indexing_technique == 'high_quality': embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) # chunk nodes by chunk size indexing_start_at = time.perf_counter() tokens = 0 chunk_size = 100 for i in range(0, len(documents), chunk_size): # check document is paused self._check_document_paused_status(dataset_document.id) chunk_documents = documents[i:i + chunk_size] if dataset.indexing_technique == 'high_quality' or embedding_model: tokens += sum( embedding_model.get_num_tokens(document.page_content) for document in chunk_documents ) # save vector index if vector_index: vector_index.add_texts(chunk_documents) # save keyword index keyword_table_index.add_texts(chunk_documents) document_ids = [document.metadata['doc_id'] for document in chunk_documents] db.session.query(DocumentSegment).filter( DocumentSegment.document_id == dataset_document.id, DocumentSegment.index_node_id.in_(document_ids), DocumentSegment.status == "indexing" ).update({ DocumentSegment.status: "completed", DocumentSegment.enabled: True, DocumentSegment.completed_at: datetime.datetime.utcnow() }) db.session.commit() indexing_end_at = time.perf_counter() # update document status to completed self._update_document_index_status( document_id=dataset_document.id, after_indexing_status="completed", extra_update_params={ DatasetDocument.tokens: tokens, DatasetDocument.completed_at: datetime.datetime.utcnow(), DatasetDocument.indexing_latency: indexing_end_at - indexing_start_at, } ) def _check_document_paused_status(self, document_id: str): indexing_cache_key = 'document_{}_is_paused'.format(document_id) result = redis_client.get(indexing_cache_key) if result: raise DocumentIsPausedException() def _update_document_index_status(self, document_id: str, after_indexing_status: str, extra_update_params: Optional[dict] = None) -> None: """ Update the document indexing status. """ count = DatasetDocument.query.filter_by(id=document_id, is_paused=True).count() if count > 0: raise DocumentIsPausedException() document = DatasetDocument.query.filter_by(id=document_id).first() if not document: raise DocumentIsDeletedPausedException() update_params = { DatasetDocument.indexing_status: after_indexing_status } if extra_update_params: update_params.update(extra_update_params) DatasetDocument.query.filter_by(id=document_id).update(update_params) db.session.commit() def _update_segments_by_document(self, dataset_document_id: str, update_params: dict) -> None: """ Update the document segment by document id. """ DocumentSegment.query.filter_by(document_id=dataset_document_id).update(update_params) db.session.commit() def batch_add_segments(self, segments: List[DocumentSegment], dataset: Dataset): """ Batch add segments index processing """ documents = [] for segment in segments: document = Document( page_content=segment.content, metadata={ "doc_id": segment.index_node_id, "doc_hash": segment.index_node_hash, "document_id": segment.document_id, "dataset_id": segment.dataset_id, } ) documents.append(document) # save vector index index = IndexBuilder.get_index(dataset, 'high_quality') if index: index.add_texts(documents, duplicate_check=True) # save keyword index index = IndexBuilder.get_index(dataset, 'economy') if index: index.add_texts(documents) class DocumentIsPausedException(Exception): pass class DocumentIsDeletedPausedException(Exception): pass
[]
2024-01-10
perzeuss/dify
api~core~agent~agent~structed_multi_dataset_router_agent.py
import re from typing import List, Tuple, Any, Union, Sequence, Optional, cast from langchain import BasePromptTemplate, PromptTemplate from langchain.agents import StructuredChatAgent, AgentOutputParser, Agent from langchain.agents.structured_chat.base import HUMAN_MESSAGE_TEMPLATE from langchain.callbacks.base import BaseCallbackManager from langchain.callbacks.manager import Callbacks from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate from langchain.schema import AgentAction, AgentFinish, OutputParserException from langchain.tools import BaseTool from langchain.agents.structured_chat.prompt import PREFIX, SUFFIX from core.chain.llm_chain import LLMChain from core.model_providers.models.entity.model_params import ModelMode from core.model_providers.models.llm.base import BaseLLM from core.tool.dataset_retriever_tool import DatasetRetrieverTool FORMAT_INSTRUCTIONS = """Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input). The nouns in the format of "Thought", "Action", "Action Input", "Final Answer" must be expressed in English. Valid "action" values: "Final Answer" or {tool_names} Provide only ONE action per $JSON_BLOB, as shown: ``` {{{{ "action": $TOOL_NAME, "action_input": $INPUT }}}} ``` Follow this format: Question: input question to answer Thought: consider previous and subsequent steps Action: ``` $JSON_BLOB ``` Observation: action result ... (repeat Thought/Action/Observation N times) Thought: I know what to respond Action: ``` {{{{ "action": "Final Answer", "action_input": "Final response to human" }}}} ```""" class StructuredMultiDatasetRouterAgent(StructuredChatAgent): dataset_tools: Sequence[BaseTool] class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True def should_use_agent(self, query: str): """ return should use agent Using the ReACT mode to determine whether an agent is needed is costly, so it's better to just use an Agent for reasoning, which is cheaper. :param query: :return: """ return True def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations callbacks: Callbacks to run. **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(self.dataset_tools) == 0: return AgentFinish(return_values={"output": ''}, log='') elif len(self.dataset_tools) == 1: tool = next(iter(self.dataset_tools)) rst = tool.run(tool_input={'query': kwargs['input']}) return AgentFinish(return_values={"output": rst}, log=rst) if intermediate_steps: _, observation = intermediate_steps[-1] return AgentFinish(return_values={"output": observation}, log=observation) full_inputs = self.get_full_inputs(intermediate_steps, **kwargs) try: full_output = self.llm_chain.predict(callbacks=callbacks, **full_inputs) except Exception as e: new_exception = self.llm_chain.model_instance.handle_exceptions(e) raise new_exception try: agent_decision = self.output_parser.parse(full_output) if isinstance(agent_decision, AgentAction): tool_inputs = agent_decision.tool_input if isinstance(tool_inputs, dict) and 'query' in tool_inputs: tool_inputs['query'] = kwargs['input'] agent_decision.tool_input = tool_inputs elif isinstance(tool_inputs, str): agent_decision.tool_input = kwargs['input'] else: agent_decision.return_values['output'] = '' return agent_decision except OutputParserException: return AgentFinish({"output": "I'm sorry, the answer of model is invalid, " "I don't know how to respond to that."}, "") @classmethod def create_prompt( cls, tools: Sequence[BaseTool], prefix: str = PREFIX, suffix: str = SUFFIX, human_message_template: str = HUMAN_MESSAGE_TEMPLATE, format_instructions: str = FORMAT_INSTRUCTIONS, input_variables: Optional[List[str]] = None, memory_prompts: Optional[List[BasePromptTemplate]] = None, ) -> BasePromptTemplate: tool_strings = [] for tool in tools: args_schema = re.sub("}", "}}}}", re.sub("{", "{{{{", str(tool.args))) tool_strings.append(f"{tool.name}: {tool.description}, args: {args_schema}") formatted_tools = "\n".join(tool_strings) unique_tool_names = set(tool.name for tool in tools) tool_names = ", ".join('"' + name + '"' for name in unique_tool_names) format_instructions = format_instructions.format(tool_names=tool_names) template = "\n\n".join([prefix, formatted_tools, format_instructions, suffix]) if input_variables is None: input_variables = ["input", "agent_scratchpad"] _memory_prompts = memory_prompts or [] messages = [ SystemMessagePromptTemplate.from_template(template), *_memory_prompts, HumanMessagePromptTemplate.from_template(human_message_template), ] return ChatPromptTemplate(input_variables=input_variables, messages=messages) @classmethod def create_completion_prompt( cls, tools: Sequence[BaseTool], prefix: str = PREFIX, format_instructions: str = FORMAT_INSTRUCTIONS, input_variables: Optional[List[str]] = None, ) -> PromptTemplate: """Create prompt in the style of the zero shot agent. Args: tools: List of tools the agent will have access to, used to format the prompt. prefix: String to put before the list of tools. input_variables: List of input variables the final prompt will expect. Returns: A PromptTemplate with the template assembled from the pieces here. """ suffix = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:. Question: {input} Thought: {agent_scratchpad} """ tool_strings = "\n".join([f"{tool.name}: {tool.description}" for tool in tools]) tool_names = ", ".join([tool.name for tool in tools]) format_instructions = format_instructions.format(tool_names=tool_names) template = "\n\n".join([prefix, tool_strings, format_instructions, suffix]) if input_variables is None: input_variables = ["input", "agent_scratchpad"] return PromptTemplate(template=template, input_variables=input_variables) def _construct_scratchpad( self, intermediate_steps: List[Tuple[AgentAction, str]] ) -> str: agent_scratchpad = "" for action, observation in intermediate_steps: agent_scratchpad += action.log agent_scratchpad += f"\n{self.observation_prefix}{observation}\n{self.llm_prefix}" if not isinstance(agent_scratchpad, str): raise ValueError("agent_scratchpad should be of type string.") if agent_scratchpad: llm_chain = cast(LLMChain, self.llm_chain) if llm_chain.model_instance.model_mode == ModelMode.CHAT: return ( f"This was your previous work " f"(but I haven't seen any of it! I only see what " f"you return as final answer):\n{agent_scratchpad}" ) else: return agent_scratchpad else: return agent_scratchpad @classmethod def from_llm_and_tools( cls, model_instance: BaseLLM, tools: Sequence[BaseTool], callback_manager: Optional[BaseCallbackManager] = None, output_parser: Optional[AgentOutputParser] = None, prefix: str = PREFIX, suffix: str = SUFFIX, human_message_template: str = HUMAN_MESSAGE_TEMPLATE, format_instructions: str = FORMAT_INSTRUCTIONS, input_variables: Optional[List[str]] = None, memory_prompts: Optional[List[BasePromptTemplate]] = None, **kwargs: Any, ) -> Agent: """Construct an agent from an LLM and tools.""" cls._validate_tools(tools) if model_instance.model_mode == ModelMode.CHAT: prompt = cls.create_prompt( tools, prefix=prefix, suffix=suffix, human_message_template=human_message_template, format_instructions=format_instructions, input_variables=input_variables, memory_prompts=memory_prompts, ) else: prompt = cls.create_completion_prompt( tools, prefix=prefix, format_instructions=format_instructions, input_variables=input_variables ) llm_chain = LLMChain( model_instance=model_instance, prompt=prompt, callback_manager=callback_manager, ) tool_names = [tool.name for tool in tools] _output_parser = output_parser return cls( llm_chain=llm_chain, allowed_tools=tool_names, output_parser=_output_parser, dataset_tools=tools, **kwargs, )
[ "\n\n" ]
2024-01-10
perzeuss/dify
api~core~model_providers~model_provider_factory.py
from typing import Type from sqlalchemy.exc import IntegrityError from core.model_providers.models.entity.model_params import ModelType from core.model_providers.providers.base import BaseModelProvider from core.model_providers.rules import provider_rules from extensions.ext_database import db from models.provider import TenantPreferredModelProvider, ProviderType, Provider, ProviderQuotaType DEFAULT_MODELS = { ModelType.TEXT_GENERATION.value: { 'provider_name': 'openai', 'model_name': 'gpt-3.5-turbo', }, ModelType.EMBEDDINGS.value: { 'provider_name': 'openai', 'model_name': 'text-embedding-ada-002', }, ModelType.SPEECH_TO_TEXT.value: { 'provider_name': 'openai', 'model_name': 'whisper-1', } } class ModelProviderFactory: @classmethod def get_model_provider_class(cls, provider_name: str) -> Type[BaseModelProvider]: if provider_name == 'openai': from core.model_providers.providers.openai_provider import OpenAIProvider return OpenAIProvider elif provider_name == 'anthropic': from core.model_providers.providers.anthropic_provider import AnthropicProvider return AnthropicProvider elif provider_name == 'minimax': from core.model_providers.providers.minimax_provider import MinimaxProvider return MinimaxProvider elif provider_name == 'spark': from core.model_providers.providers.spark_provider import SparkProvider return SparkProvider elif provider_name == 'tongyi': from core.model_providers.providers.tongyi_provider import TongyiProvider return TongyiProvider elif provider_name == 'wenxin': from core.model_providers.providers.wenxin_provider import WenxinProvider return WenxinProvider elif provider_name == 'zhipuai': from core.model_providers.providers.zhipuai_provider import ZhipuAIProvider return ZhipuAIProvider elif provider_name == 'chatglm': from core.model_providers.providers.chatglm_provider import ChatGLMProvider return ChatGLMProvider elif provider_name == 'baichuan': from core.model_providers.providers.baichuan_provider import BaichuanProvider return BaichuanProvider elif provider_name == 'azure_openai': from core.model_providers.providers.azure_openai_provider import AzureOpenAIProvider return AzureOpenAIProvider elif provider_name == 'replicate': from core.model_providers.providers.replicate_provider import ReplicateProvider return ReplicateProvider elif provider_name == 'huggingface_hub': from core.model_providers.providers.huggingface_hub_provider import HuggingfaceHubProvider return HuggingfaceHubProvider elif provider_name == 'xinference': from core.model_providers.providers.xinference_provider import XinferenceProvider return XinferenceProvider elif provider_name == 'openllm': from core.model_providers.providers.openllm_provider import OpenLLMProvider return OpenLLMProvider elif provider_name == 'localai': from core.model_providers.providers.localai_provider import LocalAIProvider return LocalAIProvider elif provider_name == 'cohere': from core.model_providers.providers.cohere_provider import CohereProvider return CohereProvider else: raise NotImplementedError @classmethod def get_provider_names(cls): """ Returns a list of provider names. """ return list(provider_rules.keys()) @classmethod def get_provider_rules(cls): """ Returns a list of provider rules. :return: """ return provider_rules @classmethod def get_provider_rule(cls, provider_name: str): """ Returns provider rule. """ return provider_rules[provider_name] @classmethod def get_preferred_model_provider(cls, tenant_id: str, model_provider_name: str): """ get preferred model provider. :param tenant_id: a string representing the ID of the tenant. :param model_provider_name: :return: """ # get preferred provider preferred_provider = cls._get_preferred_provider(tenant_id, model_provider_name) if not preferred_provider or not preferred_provider.is_valid: return None # init model provider model_provider_class = ModelProviderFactory.get_model_provider_class(model_provider_name) return model_provider_class(provider=preferred_provider) @classmethod def get_preferred_type_by_preferred_model_provider(cls, tenant_id: str, model_provider_name: str, preferred_model_provider: TenantPreferredModelProvider): """ get preferred provider type by preferred model provider. :param model_provider_name: :param preferred_model_provider: :return: """ if not preferred_model_provider: model_provider_rules = ModelProviderFactory.get_provider_rule(model_provider_name) support_provider_types = model_provider_rules['support_provider_types'] if ProviderType.CUSTOM.value in support_provider_types: custom_provider = db.session.query(Provider) \ .filter( Provider.tenant_id == tenant_id, Provider.provider_name == model_provider_name, Provider.provider_type == ProviderType.CUSTOM.value, Provider.is_valid == True ).first() if custom_provider: return ProviderType.CUSTOM.value model_provider = cls.get_model_provider_class(model_provider_name) if ProviderType.SYSTEM.value in support_provider_types \ and model_provider.is_provider_type_system_supported(): return ProviderType.SYSTEM.value elif ProviderType.CUSTOM.value in support_provider_types: return ProviderType.CUSTOM.value else: return preferred_model_provider.preferred_provider_type @classmethod def _get_preferred_provider(cls, tenant_id: str, model_provider_name: str): """ get preferred provider of tenant. :param tenant_id: :param model_provider_name: :return: """ # get preferred provider type preferred_provider_type = cls._get_preferred_provider_type(tenant_id, model_provider_name) # get providers by preferred provider type providers = db.session.query(Provider) \ .filter( Provider.tenant_id == tenant_id, Provider.provider_name == model_provider_name, Provider.provider_type == preferred_provider_type ).all() no_system_provider = False if preferred_provider_type == ProviderType.SYSTEM.value: quota_type_to_provider_dict = {} for provider in providers: quota_type_to_provider_dict[provider.quota_type] = provider model_provider_rules = ModelProviderFactory.get_provider_rule(model_provider_name) for quota_type_enum in ProviderQuotaType: quota_type = quota_type_enum.value if quota_type in model_provider_rules['system_config']['supported_quota_types']: if quota_type in quota_type_to_provider_dict.keys(): provider = quota_type_to_provider_dict[quota_type] if provider.is_valid and provider.quota_limit > provider.quota_used: return provider elif quota_type == ProviderQuotaType.TRIAL.value: try: provider = Provider( tenant_id=tenant_id, provider_name=model_provider_name, provider_type=ProviderType.SYSTEM.value, is_valid=True, quota_type=ProviderQuotaType.TRIAL.value, quota_limit=model_provider_rules['system_config']['quota_limit'], quota_used=0 ) db.session.add(provider) db.session.commit() except IntegrityError: db.session.rollback() provider = db.session.query(Provider) \ .filter( Provider.tenant_id == tenant_id, Provider.provider_name == model_provider_name, Provider.provider_type == ProviderType.SYSTEM.value, Provider.quota_type == ProviderQuotaType.TRIAL.value ).first() if provider.quota_limit == 0: return None return provider no_system_provider = True if no_system_provider: providers = db.session.query(Provider) \ .filter( Provider.tenant_id == tenant_id, Provider.provider_name == model_provider_name, Provider.provider_type == ProviderType.CUSTOM.value ).all() if preferred_provider_type == ProviderType.CUSTOM.value or no_system_provider: if providers: return providers[0] else: try: provider = Provider( tenant_id=tenant_id, provider_name=model_provider_name, provider_type=ProviderType.CUSTOM.value, is_valid=False ) db.session.add(provider) db.session.commit() except IntegrityError: db.session.rollback() provider = db.session.query(Provider) \ .filter( Provider.tenant_id == tenant_id, Provider.provider_name == model_provider_name, Provider.provider_type == ProviderType.CUSTOM.value ).first() return provider return None @classmethod def _get_preferred_provider_type(cls, tenant_id: str, model_provider_name: str): """ get preferred provider type of tenant. :param tenant_id: :param model_provider_name: :return: """ preferred_model_provider = db.session.query(TenantPreferredModelProvider) \ .filter( TenantPreferredModelProvider.tenant_id == tenant_id, TenantPreferredModelProvider.provider_name == model_provider_name ).first() return cls.get_preferred_type_by_preferred_model_provider(tenant_id, model_provider_name, preferred_model_provider)
[]
2024-01-10
perzeuss/dify
api~core~index~vector_index~qdrant_vector_index.py
import os from typing import Optional, Any, List, cast import qdrant_client from langchain.embeddings.base import Embeddings from langchain.schema import Document, BaseRetriever from langchain.vectorstores import VectorStore from pydantic import BaseModel from qdrant_client.http.models import HnswConfigDiff from core.index.base import BaseIndex from core.index.vector_index.base import BaseVectorIndex from core.vector_store.qdrant_vector_store import QdrantVectorStore from extensions.ext_database import db from models.dataset import Dataset, DatasetCollectionBinding class QdrantConfig(BaseModel): endpoint: str api_key: Optional[str] root_path: Optional[str] def to_qdrant_params(self): if self.endpoint and self.endpoint.startswith('path:'): path = self.endpoint.replace('path:', '') if not os.path.isabs(path): path = os.path.join(self.root_path, path) return { 'path': path } else: return { 'url': self.endpoint, 'api_key': self.api_key, } class QdrantVectorIndex(BaseVectorIndex): def __init__(self, dataset: Dataset, config: QdrantConfig, embeddings: Embeddings): super().__init__(dataset, embeddings) self._client_config = config def get_type(self) -> str: return 'qdrant' def get_index_name(self, dataset: Dataset) -> str: if dataset.collection_binding_id: dataset_collection_binding = db.session.query(DatasetCollectionBinding). \ filter(DatasetCollectionBinding.id == dataset.collection_binding_id). \ one_or_none() if dataset_collection_binding: return dataset_collection_binding.collection_name else: raise ValueError('Dataset Collection Bindings is not exist!') else: if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] return class_prefix dataset_id = dataset.id return "Vector_index_" + dataset_id.replace("-", "_") + '_Node' def to_index_struct(self) -> dict: return { "type": self.get_type(), "vector_store": {"class_prefix": self.get_index_name(self.dataset)} } def create(self, texts: list[Document], **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = QdrantVectorStore.from_documents( texts, self._embeddings, collection_name=self.get_index_name(self.dataset), ids=uuids, content_payload_key='page_content', group_id=self.dataset.id, group_payload_key='group_id', hnsw_config=HnswConfigDiff(m=0, payload_m=16, ef_construct=100, full_scan_threshold=10000, max_indexing_threads=0, on_disk=False), **self._client_config.to_qdrant_params() ) return self def create_with_collection_name(self, texts: list[Document], collection_name: str, **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = QdrantVectorStore.from_documents( texts, self._embeddings, collection_name=collection_name, ids=uuids, content_payload_key='page_content', group_id=self.dataset.id, group_payload_key='group_id', hnsw_config=HnswConfigDiff(m=0, payload_m=16, ef_construct=100, full_scan_threshold=10000, max_indexing_threads=0, on_disk=False), **self._client_config.to_qdrant_params() ) return self def _get_vector_store(self) -> VectorStore: """Only for created index.""" if self._vector_store: return self._vector_store attributes = ['doc_id', 'dataset_id', 'document_id'] client = qdrant_client.QdrantClient( **self._client_config.to_qdrant_params() ) return QdrantVectorStore( client=client, collection_name=self.get_index_name(self.dataset), embeddings=self._embeddings, content_payload_key='page_content', group_id=self.dataset.id, group_payload_key='group_id' ) def _get_vector_store_class(self) -> type: return QdrantVectorStore def delete_by_document_id(self, document_id: str): vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="metadata.document_id", match=models.MatchValue(value=document_id), ), ], )) def delete_by_ids(self, ids: list[str]) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models for node_id in ids: vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="metadata.doc_id", match=models.MatchValue(value=node_id), ), ], )) def delete_by_group_id(self, group_id: str) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="group_id", match=models.MatchValue(value=group_id), ), ], )) def delete(self) -> None: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models vector_store.del_texts(models.Filter( must=[ models.FieldCondition( key="group_id", match=models.MatchValue(value=self.dataset.id), ), ], )) def _is_origin(self): if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix return True return False def search_by_full_text_index(self, query: str, **kwargs: Any) -> List[Document]: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) from qdrant_client.http import models return vector_store.similarity_search_by_bm25(models.Filter( must=[ models.FieldCondition( key="group_id", match=models.MatchValue(value=self.dataset.id), ), models.FieldCondition( key="page_content", match=models.MatchText(text=query), ) ], ), kwargs.get('top_k', 2))
[]
2024-01-10
perzeuss/dify
api~commands.py
import datetime import json import math import random import string import threading import time import uuid import click import qdrant_client from qdrant_client.http.models import TextIndexParams, TextIndexType, TokenizerType from tqdm import tqdm from flask import current_app, Flask from langchain.embeddings import OpenAIEmbeddings from werkzeug.exceptions import NotFound from core.embedding.cached_embedding import CacheEmbedding from core.index.index import IndexBuilder from core.model_providers.model_factory import ModelFactory from core.model_providers.models.embedding.openai_embedding import OpenAIEmbedding from core.model_providers.models.entity.model_params import ModelType from core.model_providers.providers.hosted import hosted_model_providers from core.model_providers.providers.openai_provider import OpenAIProvider from libs.password import password_pattern, valid_password, hash_password from libs.helper import email as email_validate from extensions.ext_database import db from libs.rsa import generate_key_pair from models.account import InvitationCode, Tenant, TenantAccountJoin from models.dataset import Dataset, DatasetQuery, Document, DatasetCollectionBinding from models.model import Account, AppModelConfig, App import secrets import base64 from models.provider import Provider, ProviderType, ProviderQuotaType, ProviderModel @click.command('reset-password', help='Reset the account password.') @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset') @click.option('--new-password', prompt=True, help='the new password.') @click.option('--password-confirm', prompt=True, help='the new password confirm.') def reset_password(email, new_password, password_confirm): if str(new_password).strip() != str(password_confirm).strip(): click.echo(click.style('sorry. The two passwords do not match.', fg='red')) return account = db.session.query(Account). \ filter(Account.email == email). \ one_or_none() if not account: click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red')) return try: valid_password(new_password) except: click.echo( click.style('sorry. The passwords must match {} '.format(password_pattern), fg='red')) return # generate password salt salt = secrets.token_bytes(16) base64_salt = base64.b64encode(salt).decode() # encrypt password with salt password_hashed = hash_password(new_password, salt) base64_password_hashed = base64.b64encode(password_hashed).decode() account.password = base64_password_hashed account.password_salt = base64_salt db.session.commit() click.echo(click.style('Congratulations!, password has been reset.', fg='green')) @click.command('reset-email', help='Reset the account email.') @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset') @click.option('--new-email', prompt=True, help='the new email.') @click.option('--email-confirm', prompt=True, help='the new email confirm.') def reset_email(email, new_email, email_confirm): if str(new_email).strip() != str(email_confirm).strip(): click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red')) return account = db.session.query(Account). \ filter(Account.email == email). \ one_or_none() if not account: click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red')) return try: email_validate(new_email) except: click.echo( click.style('sorry. {} is not a valid email. '.format(email), fg='red')) return account.email = new_email db.session.commit() click.echo(click.style('Congratulations!, email has been reset.', fg='green')) @click.command('reset-encrypt-key-pair', help='Reset the asymmetric key pair of workspace for encrypt LLM credentials. ' 'After the reset, all LLM credentials will become invalid, ' 'requiring re-entry.' 'Only support SELF_HOSTED mode.') @click.confirmation_option(prompt=click.style('Are you sure you want to reset encrypt key pair?' ' this operation cannot be rolled back!', fg='red')) def reset_encrypt_key_pair(): if current_app.config['EDITION'] != 'SELF_HOSTED': click.echo(click.style('Sorry, only support SELF_HOSTED mode.', fg='red')) return tenant = db.session.query(Tenant).first() if not tenant: click.echo(click.style('Sorry, no workspace found. Please enter /install to initialize.', fg='red')) return tenant.encrypt_public_key = generate_key_pair(tenant.id) db.session.query(Provider).filter(Provider.provider_type == 'custom').delete() db.session.query(ProviderModel).delete() db.session.commit() click.echo(click.style('Congratulations! ' 'the asymmetric key pair of workspace {} has been reset.'.format(tenant.id), fg='green')) @click.command('generate-invitation-codes', help='Generate invitation codes.') @click.option('--batch', help='The batch of invitation codes.') @click.option('--count', prompt=True, help='Invitation codes count.') def generate_invitation_codes(batch, count): if not batch: now = datetime.datetime.now() batch = now.strftime('%Y%m%d%H%M%S') if not count or int(count) <= 0: click.echo(click.style('sorry. the count must be greater than 0.', fg='red')) return count = int(count) click.echo('Start generate {} invitation codes for batch {}.'.format(count, batch)) codes = '' for i in range(count): code = generate_invitation_code() invitation_code = InvitationCode( code=code, batch=batch ) db.session.add(invitation_code) click.echo(code) codes += code + "\n" db.session.commit() filename = 'storage/invitation-codes-{}.txt'.format(batch) with open(filename, 'w') as f: f.write(codes) click.echo(click.style( 'Congratulations! Generated {} invitation codes for batch {} and saved to the file \'{}\''.format(count, batch, filename), fg='green')) def generate_invitation_code(): code = generate_upper_string() while db.session.query(InvitationCode).filter(InvitationCode.code == code).count() > 0: code = generate_upper_string() return code def generate_upper_string(): letters_digits = string.ascii_uppercase + string.digits result = "" for i in range(8): result += random.choice(letters_digits) return result @click.command('recreate-all-dataset-indexes', help='Recreate all dataset indexes.') def recreate_all_dataset_indexes(): click.echo(click.style('Start recreate all dataset indexes.', fg='green')) recreate_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: try: click.echo('Recreating dataset index: {}'.format(dataset.id)) index = IndexBuilder.get_index(dataset, 'high_quality') if index and index._is_origin(): index.recreate_dataset(dataset) recreate_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Recreate dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Recreate {} dataset indexes.'.format(recreate_count), fg='green')) @click.command('clean-unused-dataset-indexes', help='Clean unused dataset indexes.') def clean_unused_dataset_indexes(): click.echo(click.style('Start clean unused dataset indexes.', fg='green')) clean_days = int(current_app.config.get('CLEAN_DAY_SETTING')) start_at = time.perf_counter() thirty_days_ago = datetime.datetime.now() - datetime.timedelta(days=clean_days) page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.created_at < thirty_days_ago) \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: dataset_query = db.session.query(DatasetQuery).filter( DatasetQuery.created_at > thirty_days_ago, DatasetQuery.dataset_id == dataset.id ).all() if not dataset_query or len(dataset_query) == 0: documents = db.session.query(Document).filter( Document.dataset_id == dataset.id, Document.indexing_status == 'completed', Document.enabled == True, Document.archived == False, Document.updated_at > thirty_days_ago ).all() if not documents or len(documents) == 0: try: # remove index vector_index = IndexBuilder.get_index(dataset, 'high_quality') kw_index = IndexBuilder.get_index(dataset, 'economy') # delete from vector index if vector_index: if dataset.collection_binding_id: vector_index.delete_by_group_id(dataset.id) else: if dataset.collection_binding_id: vector_index.delete_by_group_id(dataset.id) else: vector_index.delete() kw_index.delete() # update document update_params = { Document.enabled: False } Document.query.filter_by(dataset_id=dataset.id).update(update_params) db.session.commit() click.echo(click.style('Cleaned unused dataset {} from db success!'.format(dataset.id), fg='green')) except Exception as e: click.echo( click.style('clean dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) end_at = time.perf_counter() click.echo(click.style('Cleaned unused dataset from db success latency: {}'.format(end_at - start_at), fg='green')) @click.command('sync-anthropic-hosted-providers', help='Sync anthropic hosted providers.') def sync_anthropic_hosted_providers(): if not hosted_model_providers.anthropic: click.echo(click.style('Anthropic hosted provider is not configured.', fg='red')) return click.echo(click.style('Start sync anthropic hosted providers.', fg='green')) count = 0 new_quota_limit = hosted_model_providers.anthropic.quota_limit page = 1 while True: try: providers = db.session.query(Provider).filter( Provider.provider_name == 'anthropic', Provider.provider_type == ProviderType.SYSTEM.value, Provider.quota_type == ProviderQuotaType.TRIAL.value, Provider.quota_limit != new_quota_limit ).order_by(Provider.created_at.desc()).paginate(page=page, per_page=100) except NotFound: break page += 1 for provider in providers: try: click.echo('Syncing tenant anthropic hosted provider: {}, origin: limit {}, used {}' .format(provider.tenant_id, provider.quota_limit, provider.quota_used)) original_quota_limit = provider.quota_limit division = math.ceil(new_quota_limit / 1000) provider.quota_limit = new_quota_limit if original_quota_limit == 1000 \ else original_quota_limit * division provider.quota_used = division * provider.quota_used db.session.commit() count += 1 except Exception as e: click.echo(click.style( 'Sync tenant anthropic hosted provider error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Synced {} anthropic hosted providers.'.format(count), fg='green')) @click.command('create-qdrant-indexes', help='Create qdrant indexes.') def create_qdrant_indexes(): click.echo(click.style('Start create qdrant indexes.', fg='green')) create_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: if dataset.index_struct_dict: if dataset.index_struct_dict['type'] != 'qdrant': try: click.echo('Create dataset qdrant index: {}'.format(dataset.id)) try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except Exception: try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id ) dataset.embedding_model = embedding_model.name dataset.embedding_model_provider = embedding_model.model_provider.provider_name except Exception: provider = Provider( id='provider_id', tenant_id=dataset.tenant_id, provider_name='openai', provider_type=ProviderType.SYSTEM.value, encrypted_config=json.dumps({'openai_api_key': 'TEST'}), is_valid=True, ) model_provider = OpenAIProvider(provider=provider) embedding_model = OpenAIEmbedding(name="text-embedding-ada-002", model_provider=model_provider) embeddings = CacheEmbedding(embedding_model) from core.index.vector_index.qdrant_vector_index import QdrantVectorIndex, QdrantConfig index = QdrantVectorIndex( dataset=dataset, config=QdrantConfig( endpoint=current_app.config.get('QDRANT_URL'), api_key=current_app.config.get('QDRANT_API_KEY'), root_path=current_app.root_path ), embeddings=embeddings ) if index: index.create_qdrant_dataset(dataset) index_struct = { "type": 'qdrant', "vector_store": { "class_prefix": dataset.index_struct_dict['vector_store']['class_prefix']} } dataset.index_struct = json.dumps(index_struct) db.session.commit() create_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Create dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Create {} dataset indexes.'.format(create_count), fg='green')) @click.command('update-qdrant-indexes', help='Update qdrant indexes.') def update_qdrant_indexes(): click.echo(click.style('Start Update qdrant indexes.', fg='green')) create_count = 0 page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=50) except NotFound: break page += 1 for dataset in datasets: if dataset.index_struct_dict: if dataset.index_struct_dict['type'] != 'qdrant': try: click.echo('Update dataset qdrant index: {}'.format(dataset.id)) try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except Exception: provider = Provider( id='provider_id', tenant_id=dataset.tenant_id, provider_name='openai', provider_type=ProviderType.CUSTOM.value, encrypted_config=json.dumps({'openai_api_key': 'TEST'}), is_valid=True, ) model_provider = OpenAIProvider(provider=provider) embedding_model = OpenAIEmbedding(name="text-embedding-ada-002", model_provider=model_provider) embeddings = CacheEmbedding(embedding_model) from core.index.vector_index.qdrant_vector_index import QdrantVectorIndex, QdrantConfig index = QdrantVectorIndex( dataset=dataset, config=QdrantConfig( endpoint=current_app.config.get('QDRANT_URL'), api_key=current_app.config.get('QDRANT_API_KEY'), root_path=current_app.root_path ), embeddings=embeddings ) if index: index.update_qdrant_dataset(dataset) create_count += 1 else: click.echo('passed.') except Exception as e: click.echo( click.style('Create dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) continue click.echo(click.style('Congratulations! Update {} dataset indexes.'.format(create_count), fg='green')) @click.command('normalization-collections', help='restore all collections in one') def normalization_collections(): click.echo(click.style('Start normalization collections.', fg='green')) normalization_count = [] page = 1 while True: try: datasets = db.session.query(Dataset).filter(Dataset.indexing_technique == 'high_quality') \ .order_by(Dataset.created_at.desc()).paginate(page=page, per_page=100) except NotFound: break datasets_result = datasets.items page += 1 for i in range(0, len(datasets_result), 5): threads = [] sub_datasets = datasets_result[i:i + 5] for dataset in sub_datasets: document_format_thread = threading.Thread(target=deal_dataset_vector, kwargs={ 'flask_app': current_app._get_current_object(), 'dataset': dataset, 'normalization_count': normalization_count }) threads.append(document_format_thread) document_format_thread.start() for thread in threads: thread.join() click.echo(click.style('Congratulations! restore {} dataset indexes.'.format(len(normalization_count)), fg='green')) @click.command('add-qdrant-full-text-index', help='add qdrant full text index') def add_qdrant_full_text_index(): click.echo(click.style('Start add full text index.', fg='green')) binds = db.session.query(DatasetCollectionBinding).all() if binds and current_app.config['VECTOR_STORE'] == 'qdrant': qdrant_url = current_app.config['QDRANT_URL'] qdrant_api_key = current_app.config['QDRANT_API_KEY'] client = qdrant_client.QdrantClient( qdrant_url, api_key=qdrant_api_key, # For Qdrant Cloud, None for local instance ) for bind in binds: try: text_index_params = TextIndexParams( type=TextIndexType.TEXT, tokenizer=TokenizerType.MULTILINGUAL, min_token_len=2, max_token_len=20, lowercase=True ) client.create_payload_index(bind.collection_name, 'page_content', field_schema=text_index_params) except Exception as e: click.echo( click.style('Create full text index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) click.echo( click.style( 'Congratulations! add collection {} full text index successful.'.format(bind.collection_name), fg='green')) def deal_dataset_vector(flask_app: Flask, dataset: Dataset, normalization_count: list): with flask_app.app_context(): try: click.echo('restore dataset index: {}'.format(dataset.id)) try: embedding_model = ModelFactory.get_embedding_model( tenant_id=dataset.tenant_id, model_provider_name=dataset.embedding_model_provider, model_name=dataset.embedding_model ) except Exception: provider = Provider( id='provider_id', tenant_id=dataset.tenant_id, provider_name='openai', provider_type=ProviderType.CUSTOM.value, encrypted_config=json.dumps({'openai_api_key': 'TEST'}), is_valid=True, ) model_provider = OpenAIProvider(provider=provider) embedding_model = OpenAIEmbedding(name="text-embedding-ada-002", model_provider=model_provider) embeddings = CacheEmbedding(embedding_model) dataset_collection_binding = db.session.query(DatasetCollectionBinding). \ filter(DatasetCollectionBinding.provider_name == embedding_model.model_provider.provider_name, DatasetCollectionBinding.model_name == embedding_model.name). \ order_by(DatasetCollectionBinding.created_at). \ first() if not dataset_collection_binding: dataset_collection_binding = DatasetCollectionBinding( provider_name=embedding_model.model_provider.provider_name, model_name=embedding_model.name, collection_name="Vector_index_" + str(uuid.uuid4()).replace("-", "_") + '_Node' ) db.session.add(dataset_collection_binding) db.session.commit() from core.index.vector_index.qdrant_vector_index import QdrantVectorIndex, QdrantConfig index = QdrantVectorIndex( dataset=dataset, config=QdrantConfig( endpoint=current_app.config.get('QDRANT_URL'), api_key=current_app.config.get('QDRANT_API_KEY'), root_path=current_app.root_path ), embeddings=embeddings ) if index: # index.delete_by_group_id(dataset.id) index.restore_dataset_in_one(dataset, dataset_collection_binding) else: click.echo('passed.') normalization_count.append(1) except Exception as e: click.echo( click.style('Create dataset index error: {} {}'.format(e.__class__.__name__, str(e)), fg='red')) @click.command('update_app_model_configs', help='Migrate data to support paragraph variable.') @click.option("--batch-size", default=500, help="Number of records to migrate in each batch.") def update_app_model_configs(batch_size): pre_prompt_template = '{{default_input}}' user_input_form_template = { "en-US": [ { "paragraph": { "label": "Query", "variable": "default_input", "required": False, "default": "" } } ], "zh-Hans": [ { "paragraph": { "label": "查询内容", "variable": "default_input", "required": False, "default": "" } } ] } click.secho("Start migrate old data that the text generator can support paragraph variable.", fg='green') total_records = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .count() if total_records == 0: click.secho("No data to migrate.", fg='green') return num_batches = (total_records + batch_size - 1) // batch_size with tqdm(total=total_records, desc="Migrating Data") as pbar: for i in range(num_batches): offset = i * batch_size limit = min(batch_size, total_records - offset) click.secho(f"Fetching batch {i + 1}/{num_batches} from source database...", fg='green') data_batch = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .order_by(App.created_at) \ .offset(offset).limit(limit).all() if not data_batch: click.secho("No more data to migrate.", fg='green') break try: click.secho(f"Migrating {len(data_batch)} records...", fg='green') for data in data_batch: # click.secho(f"Migrating data {data.id}, pre_prompt: {data.pre_prompt}, user_input_form: {data.user_input_form}", fg='green') if data.pre_prompt is None: data.pre_prompt = pre_prompt_template else: if pre_prompt_template in data.pre_prompt: continue data.pre_prompt += pre_prompt_template app_data = db.session.query(App) \ .filter(App.id == data.app_id) \ .one() account_data = db.session.query(Account) \ .join(TenantAccountJoin, Account.id == TenantAccountJoin.account_id) \ .filter(TenantAccountJoin.role == 'owner') \ .filter(TenantAccountJoin.tenant_id == app_data.tenant_id) \ .one_or_none() if not account_data: continue if data.user_input_form is None or data.user_input_form == 'null': data.user_input_form = json.dumps(user_input_form_template[account_data.interface_language]) else: raw_json_data = json.loads(data.user_input_form) raw_json_data.append(user_input_form_template[account_data.interface_language][0]) data.user_input_form = json.dumps(raw_json_data) # click.secho(f"Updated data {data.id}, pre_prompt: {data.pre_prompt}, user_input_form: {data.user_input_form}", fg='green') db.session.commit() except Exception as e: click.secho(f"Error while migrating data: {e}, app_id: {data.app_id}, app_model_config_id: {data.id}", fg='red') continue click.secho(f"Successfully migrated batch {i + 1}/{num_batches}.", fg='green') pbar.update(len(data_batch)) @click.command('migrate_default_input_to_dataset_query_variable') @click.option("--batch-size", default=500, help="Number of records to migrate in each batch.") def migrate_default_input_to_dataset_query_variable(batch_size): click.secho("Starting...", fg='green') total_records = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .filter(AppModelConfig.dataset_query_variable == None) \ .count() if total_records == 0: click.secho("No data to migrate.", fg='green') return num_batches = (total_records + batch_size - 1) // batch_size with tqdm(total=total_records, desc="Migrating Data") as pbar: for i in range(num_batches): offset = i * batch_size limit = min(batch_size, total_records - offset) click.secho(f"Fetching batch {i + 1}/{num_batches} from source database...", fg='green') data_batch = db.session.query(AppModelConfig) \ .join(App, App.app_model_config_id == AppModelConfig.id) \ .filter(App.mode == 'completion') \ .filter(AppModelConfig.dataset_query_variable == None) \ .order_by(App.created_at) \ .offset(offset).limit(limit).all() if not data_batch: click.secho("No more data to migrate.", fg='green') break try: click.secho(f"Migrating {len(data_batch)} records...", fg='green') for data in data_batch: config = AppModelConfig.to_dict(data) tools = config["agent_mode"]["tools"] dataset_exists = "dataset" in str(tools) if not dataset_exists: continue user_input_form = config.get("user_input_form", []) for form in user_input_form: paragraph = form.get('paragraph') if paragraph \ and paragraph.get('variable') == 'query': data.dataset_query_variable = 'query' break if paragraph \ and paragraph.get('variable') == 'default_input': data.dataset_query_variable = 'default_input' break db.session.commit() except Exception as e: click.secho(f"Error while migrating data: {e}, app_id: {data.app_id}, app_model_config_id: {data.id}", fg='red') continue click.secho(f"Successfully migrated batch {i + 1}/{num_batches}.", fg='green') pbar.update(len(data_batch)) def register_commands(app): app.cli.add_command(reset_password) app.cli.add_command(reset_email) app.cli.add_command(generate_invitation_codes) app.cli.add_command(reset_encrypt_key_pair) app.cli.add_command(recreate_all_dataset_indexes) app.cli.add_command(sync_anthropic_hosted_providers) app.cli.add_command(clean_unused_dataset_indexes) app.cli.add_command(create_qdrant_indexes) app.cli.add_command(update_qdrant_indexes) app.cli.add_command(update_app_model_configs) app.cli.add_command(normalization_collections) app.cli.add_command(migrate_default_input_to_dataset_query_variable) app.cli.add_command(add_qdrant_full_text_index)
[ "{'en-US': [{'paragraph': {'label': 'Query', 'variable': 'default_input', 'required': False, 'default': ''}}], 'zh-Hans': [{'paragraph': {'label': '查询内容', 'variable': 'default_input', 'required': False, 'default': ''}}]}", "{{default_input}}" ]
2024-01-10
perzeuss/dify
api~core~index~vector_index~weaviate_vector_index.py
from typing import Optional, cast, Any, List import requests import weaviate from langchain.embeddings.base import Embeddings from langchain.schema import Document, BaseRetriever from langchain.vectorstores import VectorStore from pydantic import BaseModel, root_validator from core.index.base import BaseIndex from core.index.vector_index.base import BaseVectorIndex from core.vector_store.weaviate_vector_store import WeaviateVectorStore from models.dataset import Dataset class WeaviateConfig(BaseModel): endpoint: str api_key: Optional[str] batch_size: int = 100 @root_validator() def validate_config(cls, values: dict) -> dict: if not values['endpoint']: raise ValueError("config WEAVIATE_ENDPOINT is required") return values class WeaviateVectorIndex(BaseVectorIndex): def __init__(self, dataset: Dataset, config: WeaviateConfig, embeddings: Embeddings): super().__init__(dataset, embeddings) self._client = self._init_client(config) def _init_client(self, config: WeaviateConfig) -> weaviate.Client: auth_config = weaviate.auth.AuthApiKey(api_key=config.api_key) weaviate.connect.connection.has_grpc = False try: client = weaviate.Client( url=config.endpoint, auth_client_secret=auth_config, timeout_config=(5, 60), startup_period=None ) except requests.exceptions.ConnectionError: raise ConnectionError("Vector database connection error") client.batch.configure( # `batch_size` takes an `int` value to enable auto-batching # (`None` is used for manual batching) batch_size=config.batch_size, # dynamically update the `batch_size` based on import speed dynamic=True, # `timeout_retries` takes an `int` value to retry on time outs timeout_retries=3, ) return client def get_type(self) -> str: return 'weaviate' def get_index_name(self, dataset: Dataset) -> str: if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix class_prefix += '_Node' return class_prefix dataset_id = dataset.id return "Vector_index_" + dataset_id.replace("-", "_") + '_Node' def to_index_struct(self) -> dict: return { "type": self.get_type(), "vector_store": {"class_prefix": self.get_index_name(self.dataset)} } def create(self, texts: list[Document], **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = WeaviateVectorStore.from_documents( texts, self._embeddings, client=self._client, index_name=self.get_index_name(self.dataset), uuids=uuids, by_text=False ) return self def create_with_collection_name(self, texts: list[Document], collection_name: str, **kwargs) -> BaseIndex: uuids = self._get_uuids(texts) self._vector_store = WeaviateVectorStore.from_documents( texts, self._embeddings, client=self._client, index_name=self.get_index_name(self.dataset), uuids=uuids, by_text=False ) return self def _get_vector_store(self) -> VectorStore: """Only for created index.""" if self._vector_store: return self._vector_store attributes = ['doc_id', 'dataset_id', 'document_id'] if self._is_origin(): attributes = ['doc_id'] return WeaviateVectorStore( client=self._client, index_name=self.get_index_name(self.dataset), text_key='text', embedding=self._embeddings, attributes=attributes, by_text=False ) def _get_vector_store_class(self) -> type: return WeaviateVectorStore def delete_by_document_id(self, document_id: str): if self._is_origin(): self.recreate_dataset(self.dataset) return vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) vector_store.del_texts({ "operator": "Equal", "path": ["document_id"], "valueText": document_id }) def _is_origin(self): if self.dataset.index_struct_dict: class_prefix: str = self.dataset.index_struct_dict['vector_store']['class_prefix'] if not class_prefix.endswith('_Node'): # original class_prefix return True return False def search_by_full_text_index(self, query: str, **kwargs: Any) -> List[Document]: vector_store = self._get_vector_store() vector_store = cast(self._get_vector_store_class(), vector_store) return vector_store.similarity_search_by_bm25(query, kwargs.get('top_k', 2), **kwargs)
[]
2024-01-10
perzeuss/dify
api~core~agent~agent~output_parser~retirver_dataset_agent.py
import json from typing import Tuple, List, Any, Union, Sequence, Optional, cast from langchain.agents import OpenAIFunctionsAgent, BaseSingleActionAgent from langchain.agents.openai_functions_agent.base import _format_intermediate_steps, _parse_ai_message from langchain.callbacks.base import BaseCallbackManager from langchain.callbacks.manager import Callbacks from langchain.prompts.chat import BaseMessagePromptTemplate from langchain.schema import AgentAction, AgentFinish, SystemMessage, Generation, LLMResult, AIMessage from langchain.schema.language_model import BaseLanguageModel from langchain.tools import BaseTool from pydantic import root_validator from core.model_providers.models.entity.message import to_prompt_messages from core.model_providers.models.llm.base import BaseLLM from core.third_party.langchain.llms.fake import FakeLLM from core.tool.dataset_retriever_tool import DatasetRetrieverTool class MultiDatasetRouterAgent(OpenAIFunctionsAgent): """ An Multi Dataset Retrieve Agent driven by Router. """ model_instance: BaseLLM class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True @root_validator def validate_llm(cls, values: dict) -> dict: return values def should_use_agent(self, query: str): """ return should use agent :param query: :return: """ return True def plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ if len(self.tools) == 0: return AgentFinish(return_values={"output": ''}, log='') elif len(self.tools) == 1: tool = next(iter(self.tools)) tool = cast(DatasetRetrieverTool, tool) rst = tool.run(tool_input={'query': kwargs['input']}) # output = '' # rst_json = json.loads(rst) # for item in rst_json: # output += f'{item["content"]}\n' return AgentFinish(return_values={"output": rst}, log=rst) if intermediate_steps: _, observation = intermediate_steps[-1] return AgentFinish(return_values={"output": observation}, log=observation) try: agent_decision = self.real_plan(intermediate_steps, callbacks, **kwargs) if isinstance(agent_decision, AgentAction): tool_inputs = agent_decision.tool_input if isinstance(tool_inputs, dict) and 'query' in tool_inputs and 'chat_history' not in kwargs: tool_inputs['query'] = kwargs['input'] agent_decision.tool_input = tool_inputs else: agent_decision.return_values['output'] = '' return agent_decision except Exception as e: new_exception = self.model_instance.handle_exceptions(e) raise new_exception def real_plan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations **kwargs: User inputs. Returns: Action specifying what tool to use. """ agent_scratchpad = _format_intermediate_steps(intermediate_steps) selected_inputs = { k: kwargs[k] for k in self.prompt.input_variables if k != "agent_scratchpad" } full_inputs = dict(**selected_inputs, agent_scratchpad=agent_scratchpad) prompt = self.prompt.format_prompt(**full_inputs) messages = prompt.to_messages() prompt_messages = to_prompt_messages(messages) result = self.model_instance.run( messages=prompt_messages, functions=self.functions, ) ai_message = AIMessage( content=result.content, additional_kwargs={ 'function_call': result.function_call } ) agent_decision = _parse_ai_message(ai_message) return agent_decision async def aplan( self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks = None, **kwargs: Any, ) -> Union[AgentAction, AgentFinish]: raise NotImplementedError() @classmethod def from_llm_and_tools( cls, model_instance: BaseLLM, tools: Sequence[BaseTool], callback_manager: Optional[BaseCallbackManager] = None, extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None, system_message: Optional[SystemMessage] = SystemMessage( content="You are a helpful AI assistant." ), **kwargs: Any, ) -> BaseSingleActionAgent: prompt = cls.create_prompt( extra_prompt_messages=extra_prompt_messages, system_message=system_message, ) return cls( model_instance=model_instance, llm=FakeLLM(response=''), prompt=prompt, tools=tools, callback_manager=callback_manager, **kwargs, )
[ "You are a helpful AI assistant." ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~beerqa.py
import pandas as pd import os import tqdm.auto as tqdm from typing import List, Dict, Optional from langchain.chat_models import ChatAnthropic from langchain.chains import LLMChain from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( SystemMessage ) from ...utils.io import read_json from ...utils.models import get_chat_model from .base import BaseTask MAX_NUM_QUERIES = 3 class BeerQATask(BaseTask): """BeerQA task""" def __init__(self, task_args_str): from ...utils.contriever import Contriever beerqa_config = read_json(task_args_str) self.dataset_base_path = beerqa_config["dataset_base_path"] if "passage_path" in beerqa_config: self.contriever = Contriever.setup( passage_path=beerqa_config["passage_path"], index_path=beerqa_config["index_path"], ) else: self.contriever = None def get_dataset(self, phase: str): filename_list = { "validation": "beerqa_dev_v1.0.json" } return read_json(os.path.join(self.dataset_base_path, filename_list[phase]))["data"] def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # 0. Setup assert chain_name is None initial_llm = get_chat_model(model_name=generation_llm) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm) # === 1a. Initial search === # initial_search_terms_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["question"]) ]) initial_search_terms_chain = LLMChain(llm=initial_llm, prompt=initial_search_terms_prompt, output_key="initial_search_terms") # === 1b. Initial answer === # initial_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} Based on the search results, output the answer to the above question. """.strip(), input_variables=["question", "formatted_search_result"]) ]) initial_answer_chain = LLMChain(llm=initial_llm, prompt=initial_answer_prompt, output_key="initial_answer") # === 2. Feedback === # ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} The student then read the above search results and provided the following answer: "{initial_answer}" How would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers. """.strip(), input_variables=["question", "formatted_search_result", "initial_answer"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") # === 3a. Refinement Search === # refinement_search_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" Based on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["question", "formatted_search_terms", "initial_answer", "feedback"]) ]) refinement_search_chain = LLMChain(llm=refinement_llm, prompt=refinement_search_prompt, output_key="refinement_search_terms") # === 3b. Refinement answer === # refinement_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" We took the above into account and ran a Google/Wikipedia search and obtained the following excerpts: {formatted_refinement_search_result} Based on the search results, output the answer to the above question. """.strip(), input_variables=["question", "formatted_search_terms", "initial_answer", "feedback", "formatted_refinement_search_result"]) ]) refinement_answer_chain = LLMChain(llm=refinement_llm, prompt=refinement_answer_prompt, output_key="refinement_answer") return { "initial_search_chain": initial_search_terms_chain, "initial_answer_chain": initial_answer_chain, "feedback_chain": feedback_chain, "refinement_search_chain": refinement_search_chain, "refinement_answer_chain": refinement_answer_chain, } def process(self, chain, example): return self.batch_process(chain, [example])[0] def batch_process(self, chain, example_list): num_examples = len(example_list) # Get initial search terms search_terms_list = [] out1_list = [] for example in tqdm.tqdm(example_list, desc="Initial"): out1 = chain["initial_search_chain"]({"question": example["question"]}) out1_list.append(out1) search_terms = parse_search_terms(out1["initial_search_terms"])[:MAX_NUM_QUERIES] search_terms_list.append(search_terms) search_result_list = self.contriever.get_multi_passages_batched(search_terms_list, top_k=2) refinement_search_terms_list = [] out2_list, out3_list, out4_list = [], [], [] formatted_search_terms_list = [] for i in tqdm.trange(num_examples, desc="Process Initial and Refine"): example = example_list[i] search_terms = search_terms_list[i] search_result = search_result_list[i] formatted_search_result = format_search_results(search_result) out2 = chain["initial_answer_chain"]({ "question": example["question"], "formatted_search_result": formatted_search_result, }) out2_list.append(out2) out3 = chain["feedback_chain"]({ "question": example["question"], "formatted_search_result": formatted_search_result, "initial_answer": out2["initial_answer"], }) out3_list.append(out3) formatted_search_terms = "\n".join(f"- {search_term}" for search_term in search_terms) formatted_search_terms_list.append(formatted_search_terms) out4 = chain["refinement_search_chain"]({ "question": example["question"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], }) out4_list.append(out4) refinement_search_terms = parse_search_terms(out4["refinement_search_terms"])[:MAX_NUM_QUERIES] refinement_search_terms_list.append(refinement_search_terms) refinement_search_result_list = self.contriever.get_multi_passages_batched( refinement_search_terms_list, top_k=2) out_list = [] for i in tqdm.trange(num_examples, desc="Process Refinement"): example = example_list[i] refinement_search_result = refinement_search_result_list[i] search_result = search_result_list[i] out1, out2, out3, out4 = out1_list[i], out2_list[i], out3_list[i], out4_list[i] formatted_search_terms = formatted_search_terms_list[i] formatted_refinement_search_result = format_search_results(refinement_search_result) out5 = chain["refinement_answer_chain"]({ "question": example["question"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "formatted_refinement_search_result": formatted_refinement_search_result, }) out = { "question": example["question"], "initial_search_terms": out1["initial_search_terms"], "search_results": search_result, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "refinement_search_terms": out4["refinement_search_terms"], "refinement_search_result": refinement_search_result, "refinement_answer": out5["refinement_answer"], } out_list.append(out) return out_list def evaluate(self, phase: str, outputs: List[Dict]): dataset = self.get_dataset(phase=phase) scores = {"initial_score": [], "refined_score": [], "initial_raw": [], "refined_raw": []} for row, example in zip(tqdm.tqdm(outputs), dataset): initial_judgment = self.score_single(example=example, answer=row["initial_answer"]) refined_judgment = self.score_single(example=example, answer=row["refinement_answer"]) scores["initial_score"].append(initial_judgment["judgment"]) scores["refined_score"].append(refined_judgment["judgment"]) scores["initial_raw"].append(initial_judgment["raw_judgment"]) scores["refined_raw"].append(refined_judgment["raw_judgment"]) return { "initial_score": float(pd.Series(scores["initial_score"]).mean()), "refined_score": float(pd.Series(scores["refined_score"]).mean()), "initial_raw": scores["initial_raw"], "refined_raw": scores["initial_raw"], } @classmethod def score_single(cls, example, answer): llm = ChatAnthropic(model="claude-instant-v1.1") judge_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a homework grading assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question on a quiz, where the student is allowed to look up information. QUESTION: {question} The answer key states the following are acceptable answers: ACCEPTED ANSWERS: {true_answers} A student wrote the following answer: Student's Answer: {answer} Think step-by-step, whether the student answered the question correctly, based on the answer key. Then, output "CORRECT" is the answer is correct, and "WRONG" otherwise. It is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong. Your output should look like: <reasoning> ... </reasoning> <score> CORRECT / WRONG </score> """.strip(), input_variables=["question", "true_answers", "answer"]) ]) chain = LLMChain(llm=llm, prompt=judge_prompt, output_key="judgment") raw_judgment = chain({ "question": example["question"], "true_answers": ",".join(example["answers"]), "answer": answer.strip(), })["judgment"] return {"raw_judgment": raw_judgment, "judgment": True if "CORRECT" in raw_judgment else False} def parse_search_terms(string): out_list = [] parts = string.split("<search>") for part in parts: if not part.strip(): continue out_list.append(part.split("</search>")[0]) return out_list def format_search_results(search_result): lines = [] for i, (query, passages) in enumerate(search_result.items()): lines.append(f"Search {i+1}: \"{query}\"") for passage in passages: lines.append(f" Article: {passage['title']}") lines.append(f" Excerpt: {passage['text']}") lines.append("") lines.append("") lines.append("===") lines.append("") return "\n".join(lines)
[ "You are a question-answering assistant.", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers.\n ", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, output the answer to the above question.", "answer", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "initial_answer", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers.", "\nThe following is a question on a quiz, where the student is allowed to look up information.\n\nQUESTION: {question}\n\nThe answer key states the following are acceptable answers:\n\nACCEPTED ANSWERS: {true_answers}\n\nA student wrote the following answer:\n\nStudent's Answer: {answer}\n\n\nThink step-by-step, whether the student answered the question correctly, based on the answer key.\nThen, output \"CORRECT\" is the answer is correct, and \"WRONG\" otherwise.\nIt is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong.\nYour output should look like:\n<reasoning> ... </reasoning>\n<score> CORRECT / WRONG </score>\n ", "true_answers", "question", "formatted_refinement_search_result", "The following is a question on a quiz, where the student is allowed to look up information.\n\nQUESTION: {question}\n\nThe answer key states the following are acceptable answers:\n\nACCEPTED ANSWERS: {true_answers}\n\nA student wrote the following answer:\n\nStudent's Answer: {answer}\n\n\nThink step-by-step, whether the student answered the question correctly, based on the answer key.\nThen, output \"CORRECT\" is the answer is correct, and \"WRONG\" otherwise.\nIt is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong.\nYour output should look like:\n<reasoning> ... </reasoning>\n<score> CORRECT / WRONG </score>", "formatted_search_result", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, output the answer to the above question.\n ", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, output the answer to the above question.\n ", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, output the answer to the above question.", "You are a homework grading assistant.", "formatted_search_terms" ]
2024-01-10
zphang/llm_feedback
llm_feedback~old~quick_feedback.py
import os import argparse from langchain.llms import OpenAI from langchain import PromptTemplate from langchain.chains import LLMChain, SequentialChain import warnings from datasets import load_dataset from evaluate import load import numpy as np from wasabi import color import pyutils.io as io import tqdm.auto as tqdm def create_math_qa_chain(llm): initial_solution_prompt_template = """ You will be given a math problem with multiple-choice answers. Reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice. Question: {text} Options: {options} """ initial_solution_prompt = PromptTemplate( input_variables=["text", "options"], template=initial_solution_prompt_template, ) initial_solution_chain = LLMChain(llm=llm, prompt=initial_solution_prompt, output_key="initial_solution") feedback_prompt_template = """ You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. If answer is already correct, just ouput \"CORRECT\". Instruction: {text} Options: {options} Student's answer: {initial_solution} """ ilf_feedback_prompt = PromptTemplate( input_variables=["text", "options", "initial_solution"], template=feedback_prompt_template ) feedback_chain = LLMChain(llm=llm, prompt=ilf_feedback_prompt, output_key="feedback") refinement_prompt_template = """ You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice. Instruction: {text} Options: {options} Student's answer: {initial_solution} Teacher's feedback: {feedback} """ ilf_refinement_prompt = PromptTemplate( input_variables=["text", "options", "initial_solution", "feedback"], template=refinement_prompt_template ) refinement_chain = LLMChain(llm=llm, prompt=ilf_refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "options"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def create_mbpp_chain(llm): initial_solution_prompt_template = """ You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution! Instruction: {text} Unit test: {test_list[0]} """ initial_solution_prompt = PromptTemplate( input_variables=["text", "test_list"], template=initial_solution_prompt_template, ) initial_solution_chain = LLMChain(llm=llm, prompt=initial_solution_prompt, output_key="initial_solution") feedback_prompt_template = """ You will be given a Python programming task, one unit test and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't require docstring or test cases. If the solution is already okay, just ouput \"OK\". Instruction: {text} Unit test: {test_list[0]} Code: {initial_solution} """ ilf_feedback_prompt = PromptTemplate( input_variables=["text", "test_list", "initial_solution"], template=feedback_prompt_template ) feedback_chain = LLMChain(llm=llm, prompt=ilf_feedback_prompt, output_key="feedback") refinement_prompt_template = """ You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback. Instruction: {text} Unit test: {test_list[0]} Initial solution: {initial_solution} Feedback: {feedback} """ ilf_refinement_prompt = PromptTemplate( input_variables=["text", "test_list", "initial_solution", "feedback"], template=refinement_prompt_template ) refinement_chain = LLMChain(llm=llm, prompt=ilf_refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "test_list"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def main(): parser = argparse.ArgumentParser() parser.add_argument("--model_name",) parser.add_argument("--dataset", choices=["math_qa", "mbpp"]) parser.add_argument("--output_path") parser.add_argument("--phase", choices=["train", "validation"], default="train") parser.add_argument("--num_examples", type=int, default=100) args = parser.parse_args() llm = OpenAI(model_name=args.model_name) if args.dataset == "math_qa": ilf_chain = create_math_qa_chain(llm) elif args.dataset == "mbpp": ilf_chain = create_mbpp_chain(llm) else: raise KeyError(args.dataset) os.makedirs(os.path.split(args.output_path)[0], exist_ok=True) ds = load_dataset(args.dataset, split=args.phase) outputs_list = [] for i in tqdm.trange(args.num_examples): inputs = ds[i] if args.dataset == "math_qa": outputs = ilf_chain({"text": inputs["Problem"], "options": inputs["options"]}) elif args.dataset == "mbpp": outputs = ilf_chain(inputs) else: raise KeyError(args.dataset) outputs_list.append(outputs) io.write_jsonl(outputs_list, args.output_path) if __name__ == "__main__": main()
[ "\n You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution!\n Instruction:\n {text}\n Unit test:\n {test_list[0]}\n ", "\n You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback.\n Instruction:\n {text}\n Unit test:\n {test_list[0]}\n Initial solution:\n {initial_solution}\n Feedback:\n {feedback}\n ", "\n You will be given a Python programming task, one unit test and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't require docstring or test cases. If the solution is already okay, just ouput \"OK\".\n Instruction:\n {text}\n Unit test:\n {test_list[0]}\n Code:\n {initial_solution}\n ", "\n You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n Instruction:\n {text}\n Options:\n {options}\n Student's answer:\n {initial_solution}\n Teacher's feedback:\n {feedback}\n ", "\n You will be given a math problem with multiple-choice answers. Reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n Question:\n {text}\n Options:\n {options}\n ", "options", "\n You will be given a math problem with multiple-choice answers, and a proposed answer and explanation from a student. If answer is already correct, just ouput \"CORRECT\".\n Instruction:\n {text}\n Options:\n {options}\n Student's answer:\n {initial_solution}\n ", "initial_solution" ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~fever.py
from typing import List, Dict, Optional import datasets import pandas as pd import tqdm.auto as tqdm from langchain.chains import LLMChain from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( SystemMessage ) from ...utils.io import read_json from ...utils.models import get_chat_model from .base import BaseTask MAX_NUM_QUERIES = 3 class FEVERTask(BaseTask): """FEVER task""" def __init__(self, task_args_str): from ...utils.contriever import Contriever fever_config = read_json(task_args_str) if "passage_path" in fever_config: self.contriever = Contriever.setup( passage_path=fever_config["passage_path"], index_path=fever_config["index_path"], ) else: self.contriever = None def get_dataset(self, phase: str): return datasets.load_dataset("fever", "v1.0")[phase] def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # 0. Setup assert chain_name is None initial_llm = get_chat_model(model_name=generation_llm) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm) # === 1a. Initial search === # initial_search_terms_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a fact-checking assistant."), HumanMessagePromptTemplate.from_template(""" The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion. Claim: {claim} To help verify this claim, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["claim"]) ]) initial_search_terms_chain = LLMChain(llm=initial_llm, prompt=initial_search_terms_prompt, output_key="initial_search_terms") # === 1b. Initial answer === # initial_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a fact-checking assistant."), HumanMessagePromptTemplate.from_template(""" The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion. Claim: {claim} To help verify this claim, we ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} Based on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly: <answer>SUPPORTS</answer> <answer>REFUTES</answer> <answer>NOT ENOUGH INFO</answer> """.strip(), input_variables=["claim", "formatted_search_result"]) ]) initial_answer_chain = LLMChain(llm=initial_llm, prompt=initial_answer_prompt, output_key="initial_answer") # === 2. Feedback === # ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a fact-checking assistant."), HumanMessagePromptTemplate.from_template(""" The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion. Claim: {claim} To help verify this claim, a student ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} The student then read the above search results and provided the following answer: "{initial_answer}" How would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers. """.strip(), input_variables=["claim", "formatted_search_result", "initial_answer"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") # === 3a. Refinement Search === # refinement_search_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a fact-checking assistant."), HumanMessagePromptTemplate.from_template(""" The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion. Claim: {claim} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" Based on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["claim", "formatted_search_terms", "initial_answer", "feedback"]) ]) refinement_search_chain = LLMChain(llm=refinement_llm, prompt=refinement_search_prompt, output_key="refinement_search_terms") # === 3b. Refinement answer === # refinement_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a fact-checking assistant."), HumanMessagePromptTemplate.from_template(""" The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion. Claim: {claim} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" We took the above into account and ran a Google/Wikipedia search and obtained the following excerpts: {formatted_refinement_search_result} Based on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly: <answer>SUPPORTS</answer> <answer>REFUTES</answer> <answer>NOT ENOUGH INFO</answer> """.strip(), input_variables=["claim", "formatted_search_terms", "initial_answer", "feedback", "formatted_refinement_search_result"]) ]) refinement_answer_chain = LLMChain(llm=refinement_llm, prompt=refinement_answer_prompt, output_key="refinement_answer") return { "initial_search_chain": initial_search_terms_chain, "initial_answer_chain": initial_answer_chain, "feedback_chain": feedback_chain, "refinement_search_chain": refinement_search_chain, "refinement_answer_chain": refinement_answer_chain, } def process(self, chain, example): return self.batch_process(chain, [example])[0] def batch_process(self, chain, example_list): num_examples = len(example_list) # Get initial search terms search_terms_list = [] out1_list = [] for example in tqdm.tqdm(example_list, desc="Initial"): out1 = chain["initial_search_chain"]({"claim": example["claim"]}) out1_list.append(out1) search_terms = parse_search_terms(out1["initial_search_terms"])[:MAX_NUM_QUERIES] search_terms_list.append(search_terms) search_result_list = self.contriever.get_multi_passages_batched(search_terms_list, top_k=2) refinement_search_terms_list = [] out2_list, out3_list, out4_list = [], [], [] formatted_search_terms_list = [] for i in tqdm.trange(num_examples, desc="Process Initial and Refine"): example = example_list[i] search_terms = search_terms_list[i] search_result = search_result_list[i] formatted_search_result = format_search_results(search_result) out2 = chain["initial_answer_chain"]({ "claim": example["claim"], "formatted_search_result": formatted_search_result, }) out2_list.append(out2) out3 = chain["feedback_chain"]({ "claim": example["claim"], "formatted_search_result": formatted_search_result, "initial_answer": out2["initial_answer"], }) out3_list.append(out3) formatted_search_terms = "\n".join(f"- {search_term}" for search_term in search_terms) formatted_search_terms_list.append(formatted_search_terms) out4 = chain["refinement_search_chain"]({ "claim": example["claim"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], }) out4_list.append(out4) refinement_search_terms = parse_search_terms(out4["refinement_search_terms"])[:MAX_NUM_QUERIES] refinement_search_terms_list.append(refinement_search_terms) refinement_search_result_list = self.contriever.get_multi_passages_batched( refinement_search_terms_list, top_k=2) out_list = [] for i in tqdm.trange(num_examples, desc="Process Refinement"): example = example_list[i] refinement_search_result = refinement_search_result_list[i] search_result = search_result_list[i] out1, out2, out3, out4 = out1_list[i], out2_list[i], out3_list[i], out4_list[i] formatted_search_terms = formatted_search_terms_list[i] formatted_refinement_search_result = format_search_results(refinement_search_result) out5 = chain["refinement_answer_chain"]({ "claim": example["claim"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "formatted_refinement_search_result": formatted_refinement_search_result, }) out = { "claim": example["claim"], "initial_search_terms": out1["initial_search_terms"], "search_results": search_result, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "refinement_search_terms": out4["refinement_search_terms"], "refinement_search_result": refinement_search_result, "refinement_answer": out5["refinement_answer"], } out_list.append(out) return out_list def evaluate(self, phase: str, outputs: List[Dict]): dataset = self.get_dataset(phase=phase) scores = {"initial_score": [], "refined_score": []} for row, example in zip(outputs, dataset): initial_solution = get_fever_answer(row["initial_answer"]) refined_solution = get_fever_answer(row["refinement_answer"]) scores["initial_score"].append(example["label"] == initial_solution) scores["refined_score"].append(example["label"] == refined_solution) return { "initial_score": float(pd.Series(scores["initial_score"]).mean()), "refined_score": float(pd.Series(scores["refined_score"]).mean()), } def get_fever_answer(string): if "<answer>SUPPORTS</answer>" in string: return "SUPPORTS" elif "<answer>REFUTES</answer>" in string: return "REFUTES" elif "<answer>NOT ENOUGH INFO</answer>" in string: return "NOT ENOUGH INFO" else: return "other" def parse_search_terms(string): out_list = [] parts = string.split("<search>") for part in parts: if not part.strip(): continue out_list.append(part.split("</search>")[0]) return out_list def format_search_results(search_result): lines = [] for i, (query, passages) in enumerate(search_result.items()): lines.append(f"Search {i+1}: \"{query}\"") for passage in passages: lines.append(f" Article: {passage['title']}") lines.append(f" Excerpt: {passage['text']}") lines.append("") lines.append("") lines.append("===") lines.append("") return "\n".join(lines)
[ "formatted_search_terms", "\nThe following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "initial_answer", "formatted_search_result", "\nThe following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly:\n<answer>SUPPORTS</answer>\n<answer>REFUTES</answer>\n<answer>NOT ENOUGH INFO</answer>", "\nThe following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly:\n<answer>SUPPORTS</answer>\n<answer>REFUTES</answer>\n<answer>NOT ENOUGH INFO</answer>\n ", "formatted_refinement_search_result", "\nThe following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers.\n ", "The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "You are a fact-checking assistant.", "The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly:\n<answer>SUPPORTS</answer>\n<answer>REFUTES</answer>\n<answer>NOT ENOUGH INFO</answer>", "\nThe following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, determine whether the evident supports, refutes, or is insufficient to make a conclusion on the above claim. Output one of the following exactly:\n<answer>SUPPORTS</answer>\n<answer>REFUTES</answer>\n<answer>NOT ENOUGH INFO</answer>\n ", "The following is a claim that we want to verify if it is true, false, or there is insufficient evidence to make a conclusion.\n\nClaim: {claim}\n\nTo help verify this claim, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers." ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~example.py
from typing import List, Dict, Optional from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain, SequentialChain from langchain.prompts.chat import ( ChatPromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( SystemMessage ) from .base import BaseTask class ExampleTask(BaseTask): """Example task""" def get_dataset(self, phase: str): return [ {"text": "What is 1+1?", "target": 2}, {"text": "What is 2+2?", "target": 4}, {"text": "What is 3+3?", "target": 6}, ] def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # 0. Setup assert chain_name is None initial_llm = ChatOpenAI(model_name=generation_llm) feedback_llm = ChatOpenAI(model_name=feedback_llm) refinement_llm = ChatOpenAI(model_name=refinement_llm) # === 1. Initial solution === # initial_solution_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" Question: {text} """.strip(), input_variables=["text"]), ]) initial_solution_chain = LLMChain( llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution", ) # === 2. Feedback === # feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a proposed solution to a math question. Question: {text} Proposed solution: {initial_solution} Please provide feedback on the proposed solution. """.strip(), input_variables=["text", "initial_solution"]), ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=feedback_prompt, output_key="feedback") # === 3. Refinement === # # Simulate an exchange refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" Question: {text} """.strip(), input_variables=["text"]), AIMessagePromptTemplate.from_template(""" {initial_solution} """.strip(), input_variables=["initial_solution"]), HumanMessagePromptTemplate.from_template(""" I'm not sure about that. Here's why I think it's wrong: {feedback} """.strip(), input_variables=["feedback"]), ]) refinement_chain = LLMChain(llm=refinement_llm, prompt=refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def evaluate(self, phase: str, outputs: List[Dict]): # This is a terrible evaluation metric, but it's just an example. # In practice we need to parse the output and get the answer. dataset = self.get_dataset(phase=phase) scores = {"exact_match": []} for row, example in zip(outputs, dataset): exact_match = str(row["refinement"]) == str(example["target"]) scores["exact_match"].append(exact_match) return { "exact_match": sum(scores["exact_match"]) / len(scores["exact_match"]), }
[ "\nI'm not sure about that. Here's why I think it's wrong: {feedback}\n ", "\nQuestion: {text}\n ", "\nQuestion: {text}\n ", "I'm not sure about that. Here's why I think it's wrong: {feedback}", "\n{initial_solution}\n ", "\nThe following is a proposed solution to a math question. \n\nQuestion: {text}\nProposed solution: {initial_solution}\n\nPlease provide feedback on the proposed solution.\n ", "The following is a proposed solution to a math question. \n\nQuestion: {text}\nProposed solution: {initial_solution}\n\nPlease provide feedback on the proposed solution.", "{initial_solution}", "Question: {text}", "initial_solution", "You are a math question-answering assistant." ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~mbpp.py
from typing import List, Dict, Optional import datasets from langchain.chains import LLMChain, SequentialChain from langchain.prompts.chat import ( ChatPromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( SystemMessage ) from .base import BaseTask from ...utils.models import get_chat_model class MBPPTask(BaseTask): """Example task""" def get_dataset(self, phase: str): return datasets.load_dataset("mbpp", split=phase) def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = "regular"): # 0. Setup initial_llm = get_chat_model(model_name=generation_llm) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm) initial_solution_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a helpful Python coding assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution! Output just the improved solution, without any additional comments. Your entire output should be ready to be copy-pasted into a Python console and run. Instruction: {text} Unit test: {test_list_0} Solution: """.strip(), input_variables=["text", "test_list_0"]), ]) if chain_name == "regular": feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a helpful Python coding assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a Python programming task, unit tests and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't comment on the provided unit tests, they're fixed and not meant to be changed. Your feedback should be understandable to someone who doesn't see these unit tests. If the solution is already okay, just output \"OK\". Instruction: {text} Unit tests: {test_list_0} {test_list_1} {test_list_2} Solution: {initial_solution} """.strip(), input_variables=["text", "test_list_0", "test_list_1", "test_list_2" "initial_solution"]), ]) refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a helpful Python coding assistant."), HumanMessagePromptTemplate.from_template(""" Feedback: You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback. Output just the improved solution, without any additional comments. Don't include unit test in your improved solution, they are not part of the solution. Your entire output should be ready to be copy-pasted into a Python console and run. Instruction: {text} Unit test: {test_list[0]} Initial solution: {initial_solution} Feedback: {feedback} Improved solution: """.strip(), input_variables=[ "text", "test_list_0", "test_list_1", "test_list_2", "initial_solution", "feedback" ]), ]) elif chain_name == "chat": feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a helpful Python coding assistant. A human will show you a Python programming task, unit tests for this task and a candidate solution that human wrote. Your job is to provide short feedback on how to improve human's candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't comment on the provided unit tests, they're fixed and not meant to be changed. Your feedback should be understandable to someone who doesn't see these unit tests. If the solution is already okay, just output \"OK\"."), HumanMessagePromptTemplate.from_template(""" Here is my task: {text} The function should pass the following tests: {test_list_0} {test_list_1} {test_list_2} Here is my solution: {initial_solution} How can I improve it? Just give be a short feedback, I don't need the improved solution. """.strip(), input_variables=["text", "test_list_0", "test_list_1", "test_list_2" "initial_solution"]), ]) refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a helpful Python coding assistant. Human will be giving Python programming tasks paired with one unit test. Your job is to write a function that satisfies the specification in task description and passes the unit test. Your replies should consist purely of the improved solution, without any additional comments. Imporant: Do not include the test case in your solution! Output just the improved solution Your entire output should be ready to be copy-pasted into a Python console and run. Human will be giving you feedback on your solution. You should use this feedback to improve your solution. Again, your output should consist purely of the improved solution, without any additional comments. Sometimes human's feedback will be just \"OK\". This means that your solution is already correct and you should repeat it verbatim."), HumanMessagePromptTemplate.from_template(""" {text} The function should pass the following tests: {test_list_0} {test_list_1} {test_list_2} """.strip(), input_variables=["text", "test_list_0", "test_list_1", "test_list_2"]), AIMessagePromptTemplate.from_template("{initial_solution}", input_variables=["initial_solution"]), HumanMessagePromptTemplate.from_template("{feedback}", input_variables=["feedback"]), ]) else: raise KeyError(chain_name) # === 1. Initial solution === # initial_solution_chain = LLMChain( llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution", ) feedback_chain = LLMChain(llm=feedback_llm, prompt=feedback_prompt, output_key="feedback") refinement_chain = LLMChain(llm=refinement_llm, prompt=refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "test_list_0", "test_list_1", "test_list_2"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def process(self, chain, example): return chain({ "text": example["text"], # HumanMessagePromptTemplate appears to not be able to handle lists, # so we need to pass each element separately. "test_list_0": example["test_list"][0], "test_list_1": example["test_list"][1], "test_list_2": example["test_list"][2], }) def evaluate(self, phase: str, outputs: List[Dict]): raise NotImplementedError()
[ "You will be given a Python programming task, unit tests and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't comment on the provided unit tests, they're fixed and not meant to be changed. Your feedback should be understandable to someone who doesn't see these unit tests. If the solution is already okay, just output \"OK\".\nInstruction:\n{text}\n\nUnit tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}\n\nSolution:\n{initial_solution}", "You are a helpful Python coding assistant. A human will show you a Python programming task, unit tests for this task and a candidate solution that human wrote. Your job is to provide short feedback on how to improve human's candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't comment on the provided unit tests, they're fixed and not meant to be changed. Your feedback should be understandable to someone who doesn't see these unit tests. If the solution is already okay, just output \"OK\".", "test_list_1", "test_list_2", "You are a helpful Python coding assistant. Human will be giving Python programming tasks paired with one unit test. Your job is to write a function that satisfies the specification in task description and passes the unit test. Your replies should consist purely of the improved solution, without any additional comments. Imporant: Do not include the test case in your solution! Output just the improved solution Your entire output should be ready to be copy-pasted into a Python console and run. Human will be giving you feedback on your solution. You should use this feedback to improve your solution. Again, your output should consist purely of the improved solution, without any additional comments. Sometimes human's feedback will be just \"OK\". This means that your solution is already correct and you should repeat it verbatim.", "Here is my task:\n{text}\n\nThe function should pass the following tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}\n\nHere is my solution:\n{initial_solution}\n\nHow can I improve it? Just give be a short feedback, I don't need the improved solution.", "\nYou will be given a Python programming task, unit tests and a candidate solution. Your job is to provide short feedback on how to improve the candidate solution such that it satisfies the specification in task description and passes the unit test. Be as concise as possible! Do not provide the corrected solution, limit yourself to short feedback in natural language. Focus on correctness, not on following Python style guide or good variable naming. Don't comment on the provided unit tests, they're fixed and not meant to be changed. Your feedback should be understandable to someone who doesn't see these unit tests. If the solution is already okay, just output \"OK\".\nInstruction:\n{text}\n\nUnit tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}\n\nSolution:\n{initial_solution}\n ", "initial_solution", "{text}\n \nThe function should pass the following tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}", "\nYou will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution! Output just the improved solution, without any additional comments. Your entire output should be ready to be copy-pasted into a Python console and run.\nInstruction:\n{text}\nUnit test:\n{test_list_0}\nSolution:\n ", "\nHere is my task:\n{text}\n\nThe function should pass the following tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}\n\nHere is my solution:\n{initial_solution}\n\nHow can I improve it? Just give be a short feedback, I don't need the improved solution.\n ", "test_list_0", "\n{text}\n \nThe function should pass the following tests:\n{test_list_0}\n{test_list_1}\n{test_list_2}\n ", "Feedback:\nYou will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback. Output just the improved solution, without any additional comments. Don't include unit test in your improved solution, they are not part of the solution. Your entire output should be ready to be copy-pasted into a Python console and run.\n\nInstruction:\n{text}\n\nUnit test:\n{test_list[0]}\n\nInitial solution:\n{initial_solution}\n\nFeedback:\n{feedback}\nImproved solution:", "You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Imporant: Do not include the test case in your solution! Output just the improved solution, without any additional comments. Your entire output should be ready to be copy-pasted into a Python console and run.\nInstruction:\n{text}\nUnit test:\n{test_list_0}\nSolution:", "{feedback}", "You are a helpful Python coding assistant.", "{initial_solution}", "\nFeedback:\nYou will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback. Output just the improved solution, without any additional comments. Don't include unit test in your improved solution, they are not part of the solution. Your entire output should be ready to be copy-pasted into a Python console and run.\n\nInstruction:\n{text}\n\nUnit test:\n{test_list[0]}\n\nInitial solution:\n{initial_solution}\n\nFeedback:\n{feedback}\nImproved solution:\n " ]
2024-01-10
zphang/llm_feedback
llm_feedback~old~cot.py
import os import argparse from langchain.llms import OpenAI from langchain import PromptTemplate from langchain.chains import LLMChain, SequentialChain import warnings from datasets import load_dataset from evaluate import load import numpy as np from wasabi import color import pyutils.io as io import tqdm.auto as tqdm from langchain.chat_models import ChatOpenAI from langchain import PromptTemplate from langchain.chains import LLMChain, SequentialChain from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) import warnings from datasets import load_dataset from evaluate import load import numpy as np from wasabi import color from langchain.schema import ( AIMessage, HumanMessage, SystemMessage ) import datasets def create_math_qa_chain(initial_llm, feedback_llm, refinement_llm): initial_solution_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. "Step 1. ") and finally respond with the right answer. Put the final answer letter on a single line. Question: {text} Options: {options} """.strip(), input_variables=["text", "options"]) ]) initial_solution_chain = LLMChain(llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution") ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error ("OK."). After that, print "REFINE" one a single line if there are errors identified, or if there are no errors, print "CORRECT". The output should look like: Step X: (Description of error) or Step X: OK. for each line. Question: {text} Options: {options} Proposed solution: {initial_solution} """.strip(), input_variables=["text", "options", "initial_solution"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") ilf_refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice. Instruction: {text} Options: {options} Student's answer: {initial_solution} Teacher's feedback: {feedback} """.strip(), input_variables=["text", "options", "initial_solution", "feedback"]) ]) refinement_chain = LLMChain(llm=refinement_llm, prompt=ilf_refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "options"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def create_mbpp_chain(initial_llm, feedback_llm, refinement_llm): initial_solution_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a Python coding assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Prepend every line of code with a comment with the line number and a description of what the code does. Important: Do not include the test case in your solution! For example: # Line 1: set x to 0 x = 0 # Line 2: add 1 to x x += 1 Instruction: {text} Unit test: {test} """.strip(), input_variables=["text", "test"]) ]) initial_solution_chain = LLMChain(llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution") ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a Python coding assistant."), HumanMessagePromptTemplate.from_template(""" The following is a proposed solution to a programming test. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error ("OK."), referring to the line numbers in the code comments. After that, print "REFINE" one a single line if there are errors identified, or if there are no errors, print "CORRECT". The output should look like: Line X: (Description of error) or Line X: OK. for each line. Question: {text} Unit test: {test} Proposed solution: {initial_solution} """.strip(), input_variables=["text", "test", "initial_solution"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") ilf_refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a Python coding assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback. Instruction: {text} Unit test: {test} Initial solution: {initial_solution} Expert feedback: {feedback} """.strip(), input_variables=["text", "test", "initial_solution", "feedback"]) ]) refinement_chain = LLMChain(llm=refinement_llm, prompt=ilf_refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "test"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def main(): parser = argparse.ArgumentParser() parser.add_argument("--initial_llm") parser.add_argument("--feedback_llm") parser.add_argument("--refinement_llm") parser.add_argument("--dataset", choices=["math_qa", "mbpp"]) parser.add_argument("--output_path") parser.add_argument("--phase", choices=["train", "validation"], default="train") parser.add_argument("--num_examples", type=int, default=100) args = parser.parse_args() initial_llm = ChatOpenAI(model_name=args.initial_llm) feedback_llm = ChatOpenAI(model_name=args.feedback_llm) refinement_llm = ChatOpenAI(model_name=args.refinement_llm) if args.dataset == "math_qa": ilf_chain = create_math_qa_chain( initial_llm=initial_llm, feedback_llm=feedback_llm, refinement_llm=refinement_llm, ) elif args.dataset == "mbpp": ilf_chain = create_mbpp_chain( initial_llm=initial_llm, feedback_llm=feedback_llm, refinement_llm=refinement_llm, ) else: raise KeyError(args.dataset) os.makedirs(os.path.split(args.output_path)[0], exist_ok=True) ds = load_dataset(args.dataset, split=args.phase) outputs_list = [] for i in tqdm.trange(args.num_examples): example = ds[i] if args.dataset == "math_qa": outputs = ilf_chain({"text": example["Problem"], "options": example["options"]}) elif args.dataset == "mbpp": outputs = ilf_chain({"text": example["text"], "test": example["test_list"][0]}) else: raise KeyError(args.dataset) outputs_list.append(outputs) io.write_jsonl(outputs_list, args.output_path) if __name__ == "__main__": main()
[ "The following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. \"Step 1. \") and finally respond with the right answer. Put the final answer letter on a single line.\n\nQuestion:\n{text}\nOptions:\n{options}", "\nThe following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. \"Step 1. \") and finally respond with the right answer. Put the final answer letter on a single line.\n\nQuestion:\n{text}\nOptions:\n{options}\n ", "The following is a proposed solution to a programming test. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"), referring to the line numbers in the code comments. After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\nThe output should look like:\n\nLine X: (Description of error)\n\nor \n\nLine X: OK.\n\nfor each line.\n\nQuestion:\n{text}\nUnit test:\n{test}\n\nProposed solution:\n{initial_solution}", "\nYou will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback.\n\nInstruction:\n{text}\nUnit test:\n{test}\nInitial solution:\n{initial_solution}\nExpert feedback:\n{feedback}\n ", "The following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"). After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\nThe output should look like:\n\n Step X: (Description of error)\n \n or \n \n Step X: OK.\n\nfor each line.\n\nQuestion:\n{text}\nOptions:\n{options}\n\nProposed solution:\n{initial_solution}", "You are a Python coding assistant.", "\nThe following is a proposed solution to a programming test. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"), referring to the line numbers in the code comments. After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\nThe output should look like:\n\nLine X: (Description of error)\n\nor \n\nLine X: OK.\n\nfor each line.\n\nQuestion:\n{text}\nUnit test:\n{test}\n\nProposed solution:\n{initial_solution}\n ", "options", "You will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n \nInstruction:\n{text}\nOptions:\n{options}\nStudent's answer:\n{initial_solution}\nTeacher's feedback:\n{feedback}", "\nThe following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"). After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\nThe output should look like:\n\n Step X: (Description of error)\n \n or \n \n Step X: OK.\n\nfor each line.\n\nQuestion:\n{text}\nOptions:\n{options}\n\nProposed solution:\n{initial_solution}\n ", "You will be given a Python programming task, one unit test, an initial solution and feedback an expert provided on that initial solution. Your job is to rewrite the initial solution based on the feedback.\n\nInstruction:\n{text}\nUnit test:\n{test}\nInitial solution:\n{initial_solution}\nExpert feedback:\n{feedback}", "\nYou will be given a Python programming task and one unit test. Write a function that satisfies the specification in task description and passes the unit test. Prepend every line of code with a comment with the line number and a description of what the code does. Important: Do not include the test case in your solution!\nFor example:\n\n# Line 1: set x to 0\nx = 0\n# Line 2: add 1 to x\nx += 1\n\nInstruction:\n{text}\nUnit test:\n{test}\n ", "initial_solution", "\nYou will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n \nInstruction:\n{text}\nOptions:\n{options}\nStudent's answer:\n{initial_solution}\nTeacher's feedback:\n{feedback}\n ", "You are a math question-answering assistant." ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~alfworld.py
import tqdm.auto as tqdm from typing import List, Dict, Optional import pandas as pd from langchain.schema import ( SystemMessage, HumanMessage, AIMessage ) from .base import BaseTask from ...utils.models import get_chat_model MAX_NUM_STEPS = 50 class AlfworldTask(BaseTask): """Alfworld task. Requires installation of https://github.com/alfworld/alfworld. """ def get_dataset(self, phase: str): """We return an environment object instead of a dataset. Call iterate_envs to iterate over the env obj. """ import alfworld.agents.environment minimal_config = { "env": { "domain_randomization": False, "goal_desc_human_anns_prob": 0.0, "regen_game_files": False, "task_types": [1, 2, 3, 4, 5, 6], "expert_type": "handcoded", }, "logic": { "domain": '$ALFWORLD_DATA/logic/alfred.pddl', "grammar": '$ALFWORLD_DATA/logic/alfred.twl2', }, "dataset": { "data_path": '$ALFWORLD_DATA/json_2.1.1/train', "eval_id_data_path": '$ALFWORLD_DATA/json_2.1.1/valid_seen', "eval_ood_data_path": '$ALFWORLD_DATA/json_2.1.1/valid_unseen', "num_eval_games": -1, }, "general": { "training_method": 'dagger', }, "dagger": { "training": { "max_nb_steps_per_episode": 50, } } } if phase == "validation": train_eval = "eval_out_of_distribution" else: raise KeyError(phase) env = alfworld.agents.environment.AlfredTWEnv(minimal_config, train_eval=train_eval) env = env.init_env(batch_size=1) return AlfworldFakeDataset(env) def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # We'll have a specific function for this. return { "initial": AlfworldAgentChain(model=get_chat_model(generation_llm)), "feedback": get_chat_model(feedback_llm), "refinement": AlfworldAgentChain(model=get_chat_model(refinement_llm)), } def process_all(self, chain, dataset, max_num_examples: int): all_outputs = [] for i, env_info in zip(tqdm.trange(max_num_examples), dataset.iterate_envs()): all_outputs.append(self.process_env(chain=chain, env_info=env_info)) return all_outputs def process_env(self, chain, env_info): initial_out = self.tackle_env(agent_chain=chain["initial"], env_info=env_info) messages = construct_messages(initial_out["history"]) if initial_out["reward"] == 0: messages[-1].content += "\nYou did not succeed in completing the task. Based on the above, what advice would you give to the next person who tries this task?" else: messages[-1].content += "\nYou were successful at the task. Based on the above, what advice would you give to the next person who tries this task?" feedback = chain["feedback"].predict_messages(messages).content refinement_out = self.tackle_env(agent_chain=chain["refinement"], env_info=env_info) return { "initial": initial_out, "feedback": feedback, "refinement": refinement_out, } @classmethod def tackle_env(cls, agent_chain: "AlfworldAgentChain", env_info): history = [{"obs": env_info["obs"]}] commands = env_info["commands"] env = env_info["env"] reward = 0 for _ in range(MAX_NUM_STEPS): raw_act_idx_plus_one = agent_chain.process(history=history, commands=commands) act_idx_plus_one = get_num(raw_act_idx_plus_one) if act_idx_plus_one > len(commands): print(f"WARNING: act_idx_plus_one={act_idx_plus_one} but len(commands)={len(commands)}") act_idx_plus_one = 1 act_idx = act_idx_plus_one - 1 act = commands[act_idx] obs, reward, done, info = env.step([act]) obs = process_ob(obs[0]) done = done[0] if done: print("DONE") break commands = info["admissible_commands"][0] history.append({"obs": obs, "act_idx_plus_one": act_idx_plus_one, "act": act}) return { "history": history, "reward": reward[0], } def evaluate(self, phase: str, outputs: List[Dict]): # This is a terrible evaluation metric, but it's just an example. # In practice we need to parse the output and get the answer. scores = {"initial_score": [], "refined_score": []} for row in outputs: scores["initial_score"].append(row["initial"]["reward"]) scores["refined_score"].append(row["refinement"]["reward"]) return { "initial_score": float(pd.Series(scores["initial_score"]).mean()), "refined_score": float(pd.Series(scores["refined_score"]).mean()), } class AlfworldFakeDataset: def __init__(self, env): self.env = env def __len__(self): return len(self.env.gamefiles) def iterate_envs(self): for _ in range(len(self)): ob, info = self.env.reset() yield { "obs": ob[0], "commands": info["admissible_commands"][0], "env": self.env, } class AlfworldAgentChain: def __init__(self, model): self.model = model def query_model(self, messages): return self.model.predict_messages(messages, stop=["\n"]).content def process(self, history, commands, feedback=None): return self.query_model(construct_messages( history=history, commands=commands, feedback=feedback, )) def construct_messages(history, commands=None, feedback=None): messages = [ SystemMessage( content="You are an assistant playing a text-based game. You can only respond by returning the number corresponding to an allowed action."), ] for hist_row in history: if "act" not in hist_row: # First observation if feedback: messages.append(HumanMessage(content="{obs}".format(**hist_row))) else: messages.append(HumanMessage(content="{obs}\n(NOTE: The following feedback was provided on a previous attempt.\n\n{feedback}\n\nPlease take the above into account.)".format( obs=hist_row["obs"], feedback=feedback, ))) else: messages.append(AIMessage(content="{act_idx_plus_one}\n".format(**hist_row))) messages.append( HumanMessage(content="You chose: {act_idx_plus_one} - {act}\n{obs}".format(**hist_row))) if commands: assert isinstance(messages[-1], HumanMessage) messages[-1].content += "\n[Choose one action]\n" + "\n".join( f"{i}: {x}" for i, x in enumerate(commands, start=1)) + f"\nChoice ({1}-{len(commands)}):" return messages def process_ob(ob): if ob.startswith('You arrive at loc '): ob = ob[ob.find('. ')+2:] return ob def get_num(string): string = string.split() ls = [] for s in string: if s.isdigit(): ls.append(s) else: break if ls: return int("".join(ls)) else: return 1
[ "You are an assistant playing a text-based game. You can only respond by returning the number corresponding to an allowed action.", "PLACEHOLDER\n(NOTE: The following feedback was provided on a previous attempt.\n\nPLACEHOLDER\n\nPlease take the above into account.)", "You chose: {act_idx_plus_one} - {act}\n{obs}", "{act_idx_plus_one}\n" ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~hotpotqa.py
import pandas as pd from typing import List, Dict, Optional import tqdm.auto as tqdm import datasets from langchain.chains import LLMChain from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from langchain.chat_models import ChatAnthropic from langchain.schema import ( SystemMessage ) from ...utils.io import read_json from ...utils.models import get_chat_model from .base import BaseTask MAX_NUM_QUERIES = 3 class HotPotQATask(BaseTask): """HotPotQA task""" def __init__(self, task_args_str): from ...utils.contriever import Contriever hotpotqa_config = read_json(task_args_str) if "passage_path" in hotpotqa_config: self.contriever = Contriever.setup( passage_path=hotpotqa_config["passage_path"], index_path=hotpotqa_config["index_path"], ) else: self.contriever = None def get_dataset(self, phase: str): return datasets.load_dataset("hotpot_qa", "fullwiki")[phase] def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # 0. Setup assert chain_name is None initial_llm = get_chat_model(model_name=generation_llm) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm) # === 1a. Initial search === # initial_search_terms_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["question"]) ]) initial_search_terms_chain = LLMChain(llm=initial_llm, prompt=initial_search_terms_prompt, output_key="initial_search_terms") # === 1b. Initial answer === # initial_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} Based on the search results, output the answer to the above question. """.strip(), input_variables=["question", "formatted_search_result"]) ]) initial_answer_chain = LLMChain(llm=initial_llm, prompt=initial_answer_prompt, output_key="initial_answer") # === 2. Feedback === # ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} To help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts: {formatted_search_result} The student then read the above search results and provided the following answer: "{initial_answer}" How would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers. """.strip(), input_variables=["question", "formatted_search_result", "initial_answer"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") # === 3a. Refinement Search === # refinement_search_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" Based on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format: <search>...</search> <search>...</search> <search>...</search> """.strip(), input_variables=["question", "formatted_search_terms", "initial_answer", "feedback"]) ]) refinement_search_chain = LLMChain(llm=refinement_llm, prompt=refinement_search_prompt, output_key="refinement_search_terms") # === 3b. Refinement answer === # refinement_answer_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question that we would like to answer. Question: {question} A previous student performed a search on the following search terms: {formatted_search_terms} Then they provided the following answer: "{initial_answer}" A teacher then provided the following feedback: "{feedback}" We took the above into account and ran a Google/Wikipedia search and obtained the following excerpts: {formatted_refinement_search_result} Based on the search results, output the answer to the above question. """.strip(), input_variables=["question", "formatted_search_terms", "initial_answer", "feedback", "formatted_refinement_search_result"]) ]) refinement_answer_chain = LLMChain(llm=refinement_llm, prompt=refinement_answer_prompt, output_key="refinement_answer") return { "initial_search_chain": initial_search_terms_chain, "initial_answer_chain": initial_answer_chain, "feedback_chain": feedback_chain, "refinement_search_chain": refinement_search_chain, "refinement_answer_chain": refinement_answer_chain, } def process(self, chain, example): return self.batch_process(chain, [example])[0] def batch_process(self, chain, example_list): num_examples = len(example_list) # Get initial search terms search_terms_list = [] out1_list = [] for example in tqdm.tqdm(example_list, desc="Initial"): out1 = chain["initial_search_chain"]({"question": example["question"]}) out1_list.append(out1) search_terms = parse_search_terms(out1["initial_search_terms"])[:MAX_NUM_QUERIES] search_terms_list.append(search_terms) search_result_list = self.contriever.get_multi_passages_batched(search_terms_list, top_k=2) refinement_search_terms_list = [] out2_list, out3_list, out4_list = [], [], [] formatted_search_terms_list = [] for i in tqdm.trange(num_examples, desc="Process Initial and Refine"): example = example_list[i] search_terms = search_terms_list[i] search_result = search_result_list[i] formatted_search_result = format_search_results(search_result) out2 = chain["initial_answer_chain"]({ "question": example["question"], "formatted_search_result": formatted_search_result, }) out2_list.append(out2) out3 = chain["feedback_chain"]({ "question": example["question"], "formatted_search_result": formatted_search_result, "initial_answer": out2["initial_answer"], }) out3_list.append(out3) formatted_search_terms = "\n".join(f"- {search_term}" for search_term in search_terms) formatted_search_terms_list.append(formatted_search_terms) out4 = chain["refinement_search_chain"]({ "question": example["question"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], }) out4_list.append(out4) refinement_search_terms = parse_search_terms(out4["refinement_search_terms"])[:MAX_NUM_QUERIES] refinement_search_terms_list.append(refinement_search_terms) refinement_search_result_list = self.contriever.get_multi_passages_batched( refinement_search_terms_list, top_k=2) out_list = [] for i in tqdm.trange(num_examples, desc="Process Refinement"): example = example_list[i] refinement_search_result = refinement_search_result_list[i] search_result = search_result_list[i] out1, out2, out3, out4 = out1_list[i], out2_list[i], out3_list[i], out4_list[i] formatted_search_terms = formatted_search_terms_list[i] formatted_refinement_search_result = format_search_results(refinement_search_result) out5 = chain["refinement_answer_chain"]({ "question": example["question"], "formatted_search_terms": formatted_search_terms, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "formatted_refinement_search_result": formatted_refinement_search_result, }) out = { "question": example["question"], "initial_search_terms": out1["initial_search_terms"], "search_results": search_result, "initial_answer": out2["initial_answer"], "feedback": out3["feedback"], "refinement_search_terms": out4["refinement_search_terms"], "refinement_search_result": refinement_search_result, "refinement_answer": out5["refinement_answer"], } out_list.append(out) return out_list def evaluate(self, phase: str, outputs: List[Dict]): dataset = self.get_dataset(phase=phase) scores = {"initial_score": [], "refined_score": [], "initial_raw": [], "refined_raw": []} for row, example in zip(tqdm.tqdm(outputs), dataset): initial_judgment = self.score_single(example=example, answer=row["initial_answer"]) refined_judgment = self.score_single(example=example, answer=row["refinement_answer"]) scores["initial_score"].append(initial_judgment["judgment"]) scores["refined_score"].append(refined_judgment["judgment"]) scores["initial_raw"].append(initial_judgment["raw_judgment"]) scores["refined_raw"].append(refined_judgment["raw_judgment"]) return { "initial_score": float(pd.Series(scores["initial_score"]).mean()), "refined_score": float(pd.Series(scores["refined_score"]).mean()), "initial_raw": scores["initial_raw"], "refined_raw": scores["initial_raw"], } @classmethod def score_single(cls, example, answer): llm = ChatAnthropic(model="claude-instant-v1.1") judge_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a homework grading assistant."), HumanMessagePromptTemplate.from_template(""" The following is a question on a quiz, where the student is allowed to look up information. QUESTION: {question} The answer key states that the answer to the question is the following: ANSWER: {true_answer} A student wrote the following answer: Student's Answer: {answer} Think step-by-step, whether the student answered the question correctly, based on the answer key. Then, output "CORRECT" is the answer is correct, and "WRONG" otherwise. It is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong. Your output should look like: <reasoning> ... </reasoning> <score> CORRECT / WRONG </score> """.strip(), input_variables=["question", "true_answer", "answer"]) ]) chain = LLMChain(llm=llm, prompt=judge_prompt, output_key="judgment") raw_judgment = chain({ "question": example["question"], "true_answer": example["answer"], "answer": answer.strip(), })["judgment"] return {"raw_judgment": raw_judgment, "judgment": True if "CORRECT" in raw_judgment else False} def parse_search_terms(string): out_list = [] parts = string.split("<search>") for part in parts: if not part.strip(): continue out_list.append(part.split("</search>")[0]) return out_list def format_search_results(search_result): lines = [] for i, (query, passages) in enumerate(search_result.items()): lines.append(f"Search {i+1}: \"{query}\"") for passage in passages: lines.append(f" Article: {passage['title']}") lines.append(f" Excerpt: {passage['text']}") lines.append("") lines.append("") lines.append("===") lines.append("") return "\n".join(lines)
[ "You are a question-answering assistant.", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nBased on the above, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers.\n ", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, output the answer to the above question.", "\nThe following is a question on a quiz, where the student is allowed to look up information.\n\nQUESTION: {question}\n\nThe answer key states that the answer to the question is the following:\n\nANSWER: {true_answer}\n\nA student wrote the following answer:\n\nStudent's Answer: {answer}\n\n\nThink step-by-step, whether the student answered the question correctly, based on the answer key.\nThen, output \"CORRECT\" is the answer is correct, and \"WRONG\" otherwise.\nIt is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong.\nYour output should look like:\n<reasoning> ... </reasoning>\n<score> CORRECT / WRONG </score>\n ", "answer", "The following is a question on a quiz, where the student is allowed to look up information.\n\nQUESTION: {question}\n\nThe answer key states that the answer to the question is the following:\n\nANSWER: {true_answer}\n\nA student wrote the following answer:\n\nStudent's Answer: {answer}\n\n\nThink step-by-step, whether the student answered the question correctly, based on the answer key.\nThen, output \"CORRECT\" is the answer is correct, and \"WRONG\" otherwise.\nIt is okay if the student provides more information than necessary. However, if the student is unable to answer, that counts as being wrong.\nYour output should look like:\n<reasoning> ... </reasoning>\n<score> CORRECT / WRONG </score>", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, output a list of up to 3 search queries that we want to search Google or Wikipedia for, in the following format:\n<search>...</search>\n<search>...</search>\n<search>...</search>\n ", "initial_answer", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, a student ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nThe student then read the above search results and provided the following answer:\n\"{initial_answer}\"\n\nHow would you improve the above search and answer? Please provide feedback on both the choice of search terms as well as the final answers.", "question", "formatted_refinement_search_result", "formatted_search_result", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, output the answer to the above question.\n ", "\nThe following is a question that we would like to answer. \n\nQuestion: {question}\n\nTo help answer this question, we ran a quick Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_search_result}\n\nBased on the search results, output the answer to the above question.\n ", "The following is a question that we would like to answer. \n\nQuestion: {question}\n\nA previous student performed a search on the following search terms:\n{formatted_search_terms}\n\nThen they provided the following answer:\n\"{initial_answer}\"\n\nA teacher then provided the following feedback:\n\"{feedback}\"\n\nWe took the above into account and ran a Google/Wikipedia search and obtained the following excerpts:\n\n{formatted_refinement_search_result}\n\nBased on the search results, output the answer to the above question.", "You are a homework grading assistant.", "formatted_search_terms", "true_answer" ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~slf5k.py
from typing import List, Dict, Optional from datasets import load_dataset import evaluate from langchain.chains import LLMChain, SequentialChain from langchain.prompts.chat import ( ChatPromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import SystemMessage from .base import BaseTask from ...utils.models import get_chat_model class SLF5KTask(BaseTask): """Example task""" def get_dataset(self, phase: str, ids: Optional[List[int]] = None): dataset = load_dataset("JeremyAlain/SLF5K")[phase] updated_dataset = [] if ids is not None: for element in dataset: if element["id"] in ids: updated_dataset.append(element) return dataset if updated_dataset == [] else updated_dataset def get_chain( self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None, ): # 0. Setup assert chain_name in ["whole_model", "human_feedback", "model_feedback"] initial_llm = get_chat_model(model_name=generation_llm, max_tokens=60) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm, max_tokens=60) if chain_name == "whole_model": # === 1. Initial solution === # initial_solution_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ Title: {title} Text: {post} TL;DR: """.strip(), input_variables=["title", "post"], ), ] ) initial_solution_chain = LLMChain( llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution", ) # === 2. Feedback === # feedback_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ The following is a proposed summary. Title: {title} Text: {post} TL;DR: {initial_solution} Please provide feedback on the proposed solution. Feedback: """.strip(), input_variables=["title", "post", "initial_solution"], ), ] ) feedback_chain = LLMChain( llm=feedback_llm, prompt=feedback_prompt, output_key="feedback" ) # === 3. Refinement === # # Simulate an exchange refinement_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ Title: {title} Text: {post} TL;DR: """.strip(), input_variables=["title, post"], ), AIMessagePromptTemplate.from_template( """ {initial_solution} """.strip(), input_variables=["initial_solution"], ), HumanMessagePromptTemplate.from_template( """ I'm not sure about that. Rewrite the summary making sure that incorporates the following feedback: {feedback} """.strip(), input_variables=["feedback"], ), ] ) refinement_chain = LLMChain( llm=refinement_llm, prompt=refinement_prompt, output_key="refinement" ) ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["title", "post"], output_variables=["initial_solution", "feedback", "refinement"], ) elif chain_name == "human_feedback": # === 3. Refinement === # # Simulate an exchange refinement_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ Title: {title} Text: {post} TL;DR: """.strip(), input_variables=["title, post"], ), AIMessagePromptTemplate.from_template( """ {generated_summary_for_feedback} """.strip(), input_variables=["generated_summary_for_feedback"], ), HumanMessagePromptTemplate.from_template( """ I'm not sure about that. Rewrite the summary making sure that incorporates the following feedback: {feedback} """.strip(), input_variables=["feedback"], ), ] ) refinement_chain = LLMChain( llm=refinement_llm, prompt=refinement_prompt, output_key="refinement" ) ilf_chain = SequentialChain( chains=[refinement_chain], input_variables=[ "title", "post", "generated_summary_for_feedback", "feedback", ], output_variables=[ "refinement", ], ) elif chain_name == "model_feedback": # === 2. Feedback === # feedback_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ The following is a proposed summary. Title: {title} Text: {post} TL;DR: {generated_summary_for_feedback} Please provide feedback on the proposed solution. Feedback: """.strip(), input_variables=[ "title", "post", "generated_summary_for_feedback", ], ), ] ) feedback_chain = LLMChain( llm=feedback_llm, prompt=feedback_prompt, output_key="feedback" ) # === 3. Refinement === # # Simulate an exchange refinement_prompt = ChatPromptTemplate.from_messages( [ SystemMessage( content="You are an assistant for generating summaries." ), HumanMessagePromptTemplate.from_template( """ Title: {title} Text: {post} TL;DR: """.strip(), input_variables=["title, post"], ), AIMessagePromptTemplate.from_template( """ {generated_summary_for_feedback} """.strip(), input_variables=["generated_summary_for_feedback"], ), HumanMessagePromptTemplate.from_template( """ I'm not sure about that. Rewrite the summary making sure that incorporates the following feedback: {feedback} """.strip(), input_variables=["feedback"], ), ] ) refinement_chain = LLMChain( llm=refinement_llm, prompt=refinement_prompt, output_key="refinement" ) ilf_chain = SequentialChain( chains=[feedback_chain, refinement_chain], input_variables=["title", "post", "generated_summary_for_feedback"], output_variables=[ "feedback", "refinement", ], ) else: raise KeyError(chain_name) return ilf_chain def evaluate(self, phase: str, outputs: List[Dict]): # Rouge evaluation by now all_ids = [elem["id"] for elem in outputs] dataset = self.get_dataset(phase=phase) metric = evaluate.load("rouge") gold_feedback, gold_refinement = [], [] model_initial_summary, model_refinement, model_feedback = [], [], [] for row, example in zip(outputs, dataset): gold_feedback.append(example["feedback"]) model_feedback.append(row["feedback"]) gold_refinement.append(example["ideal_human_summary"]) # 48 tokens max_length model_refinement.append( ".".join(" ".join(row["refinement"].split()[:48]).split(".")[:-1]) + "." ) try: # 48 tokens max_length model_initial_summary.append( ".".join( " ".join(row["initial_solution"].split()[:48]).split(".")[:-1] ) + "." ) except KeyError: model_initial_summary.append(example["generated_summary_for_feedback"]) results = { "feedback_rouge": metric.compute( predictions=model_feedback, references=gold_feedback )["rouge1"], "initial_rouge": metric.compute( predictions=model_initial_summary, references=gold_refinement )["rouge1"], "refinement_rouge": metric.compute( predictions=model_refinement, references=gold_refinement )["rouge1"], } return results
[ "You are an assistant for generating summaries.", "\nTitle: {title}\nText: {post}\nTL;DR:\n ", "\n{initial_solution}\n ", "\nI'm not sure about that. Rewrite the summary making sure that incorporates the following feedback: {feedback}\n ", "I'm not sure about that. Rewrite the summary making sure that incorporates the following feedback: {feedback}", "title, post", "Title: {title}\n Text: {post}\n TL;DR:", "The following is a proposed summary.\n\n Title: {title}\n Text: {post}\n TL;DR: {generated_summary_for_feedback}\n\n Please provide feedback on the proposed solution.\n Feedback:", "\n The following is a proposed summary.\n\n Title: {title}\n Text: {post}\n TL;DR: {generated_summary_for_feedback}\n\n Please provide feedback on the proposed solution.\n Feedback: \n ", "Title: {title}\nText: {post}\nTL;DR:", "\n{generated_summary_for_feedback}\n ", "{generated_summary_for_feedback}", "{initial_solution}", "generated_summary_for_feedback", "\n Title: {title}\n Text: {post}\n TL;DR:\n ", "initial_solution", "The following is a proposed summary.\n\n Title: {title}\n Text: {post}\n TL;DR: {initial_solution}\n\n Please provide feedback on the proposed solution. \n Feedback:", "\n The following is a proposed summary.\n\n Title: {title}\n Text: {post}\n TL;DR: {initial_solution}\n\n Please provide feedback on the proposed solution. \n Feedback: \n " ]
2024-01-10
zphang/llm_feedback
llm_feedback~pilot~tasks~mathqa.py
from typing import List, Dict, Optional import pandas as pd from datasets import load_dataset from langchain.chains import LLMChain, SequentialChain from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( SystemMessage ) from .base import BaseTask from ...utils.models import get_chat_model class MathQATask(BaseTask): """MathQA task""" def get_dataset(self, phase: str): ds = load_dataset("math_qa")[phase] return ds def get_chain(self, generation_llm: str, feedback_llm: str, refinement_llm: str, chain_name: Optional[str] = None): # 0. Setup assert chain_name is None initial_llm = get_chat_model(model_name=generation_llm) feedback_llm = get_chat_model(model_name=feedback_llm) refinement_llm = get_chat_model(model_name=refinement_llm) # === 1. Initial solution === # initial_solution_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. "Step 1. ") and finally respond with the right answer. Put the final answer letter on a single line. Question: {text} Options: {options} """.strip(), input_variables=["text", "options"]) ]) initial_solution_chain = LLMChain(llm=initial_llm, prompt=initial_solution_prompt, output_key="initial_solution") # === 2. Feedback === # ilf_feedback_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" The following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error ("OK."). After that, print "REFINE" one a single line if there are errors identified, or if there are no errors, print "CORRECT". The output should look like: Step X: (Description of error) or Step X: OK. for each line. Question: {text} Options: {options} Proposed solution: {initial_solution} """.strip(), input_variables=["text", "options", "initial_solution"]) ]) feedback_chain = LLMChain(llm=feedback_llm, prompt=ilf_feedback_prompt, output_key="feedback") # === 3. Refinement === # ilf_refinement_prompt = ChatPromptTemplate.from_messages([ SystemMessage(content="You are a math question-answering assistant."), HumanMessagePromptTemplate.from_template(""" You will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice. Instruction: {text} Options: {options} Student's answer: {initial_solution} Teacher's feedback: {feedback} """.strip(), input_variables=["text", "options", "initial_solution", "feedback"]) ]) refinement_chain = LLMChain(llm=refinement_llm, prompt=ilf_refinement_prompt, output_key="refinement") ilf_chain = SequentialChain( chains=[initial_solution_chain, feedback_chain, refinement_chain], input_variables=["text", "options"], output_variables=["initial_solution", "feedback", "refinement"], ) return ilf_chain def process(self, chain, example): return chain({"text": example["Problem"], "options": example["options"]}) def evaluate(self, phase: str, outputs: List[Dict]): # This is a terrible evaluation metric, but it's just an example. # In practice we need to parse the output and get the answer. dataset = self.get_dataset(phase=phase) scores = {"initial_score": [], "refined_score": []} for row, example in zip(outputs, dataset): initial_solution = get_math_qa_answer(row["initial_solution"]) refined_solution = get_math_qa_answer(row["refinement"]) scores["initial_score"].append(example["correct"] == initial_solution) scores["refined_score"].append(example["correct"] == refined_solution) return { "initial_score": float(pd.Series(scores["initial_score"]).mean()), "refined_score": float(pd.Series(scores["refined_score"]).mean()), } def get_math_qa_answer(solution): candidates = [ "a)", "b)", "c)", "d)", "e)", "a )", "b )", "c )", "d )", "e )" ] candidates = candidates + [x.upper() for x in candidates] positions = {} for candidate in candidates: positions[candidate] = solution.rfind(candidate) srs = pd.Series(positions) if srs.max() == -1: answer = "X" else: answer = srs.idxmax()[:1].lower() return answer
[ "You are a math question-answering assistant.", "The following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. \"Step 1. \") and finally respond with the right answer. Put the final answer letter on a single line.\n\n Question:\n {text}\n Options:\n {options}", "\n The following is a math problem. Reason through the problem step-by-step, putting each separate reasoning step on a new numbered line (e.g. \"Step 1. \") and finally respond with the right answer. Put the final answer letter on a single line.\n\n Question:\n {text}\n Options:\n {options}\n ", "options", "\n You will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n\n Instruction:\n {text}\n Options:\n {options}\n Student's answer:\n {initial_solution}\n Teacher's feedback:\n {feedback}\n ", "You will be given a math problem with multiple-choice answers, and a proposed answer from a student. You will also be provided feedback a teacher provided on that initial solution. Based on the feedback, reason through the problem step-by-step, and finally respond with the letter corresponding to the right answer choice.\n\n Instruction:\n {text}\n Options:\n {options}\n Student's answer:\n {initial_solution}\n Teacher's feedback:\n {feedback}", "The following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"). After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\n The output should look like:\n\n Step X: (Description of error)\n\n or \n\n Step X: OK.\n\n for each line.\n\n Question:\n {text}\n Options:\n {options}\n\n Proposed solution:\n {initial_solution}", "initial_solution", "\n The following is a proposed solution to a math question. There may be an error with the solution, or it may be correct. Go through each line and indicate if that line has an error (and explain what the error is) or no error (\"OK.\"). After that, print \"REFINE\" one a single line if there are errors identified, or if there are no errors, print \"CORRECT\".\n\n The output should look like:\n\n Step X: (Description of error)\n\n or \n\n Step X: OK.\n\n for each line.\n\n Question:\n {text}\n Options:\n {options}\n\n Proposed solution:\n {initial_solution}\n " ]
2024-01-10
ethux/OpenAI-Local-LLM-Proxy
modules~openllmapi.py
import os from langchain.llms import OpenLLM from modules.prompt import Prompt from dotenv import load_dotenv load_dotenv() import asyncio def chat(messages, max_tokens): max_tokens = max_tokens url = os.environ['API_URL'] output_msg = Prompt.prepare(messages) print(output_msg) output_msg += 'Assistant: ' loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) llm = OpenLLM(server_url=url) response = llm(prompt=output_msg) print(response) loop.close() return response
[]
2024-01-10
blennon/storybot
synthesize_convos.py
import openai from main import * from uuid import uuid4 openai.api_key = os.environ['OPENAI_API_KEY'] params = {'model':'text-davinci-002', 'max_tokens':1024, 'temperature':0.7, 'top_p':1.0, 'frequency_penalty':0.0, 'presence_penalty':0.0, 'stop':None } setting_prompt = """STORYBOT: What sort of setting would you like for your story to take place? If you're not sure, I can brainstorm some ideas.""" antag_prompt = """STORYBOT: Every great story has an antagonist. In can be a character or circumstance. Here are some ideas I came up with.""" start = """STORYBOT: Hi, I’m Storybot, a friendly AI that will help write an amazing story for your little one. Since every great story starts with a main character, can you tell me about your’s? It helps to know their name, age and gender.\nCUSTOMER:""" finish = """STORYBOT: Is there anything else you would like to add to your story? If not, you can say ALL DONE.\nCUSTOMER: ALL DONE.""" def check_presence(convo, keyword): if keyword in convo: return True else: return False def append_prompt(prompt, convo): return '\n'.join([convo, prompt]) def append_and_complete(prompt, response, params): prompt = append_prompt(prompt, response) response = complete(prompt, params) return prompt+response.choices[0].text def append_finish(response): if response.choices[0].finish_reason == 'length': lines = response.choices[0].text.split('\n') if "CUSTOMER:" in lines[-1]: lines = lines[:-2] elif "STORYBOT" in lines[-1]: lines = lines[:-1] text = '\n'.join(lines) else: text = response.choices[0].text return '\n'.join([text, finish]) if __name__ == '__main__': with open('customer_prompts.txt', 'r') as infile: customer_prompts = infile.read().split('\n') count = 257 while count < 400: prompt = open_file('storybot_prompt.txt') prompt = prompt.replace('<<UUID>>', str(uuid4())) prompt = prompt.replace('<<CUSTOMER>>', customer_prompts[count]) try: response = complete(prompt, params, max_retry=1) convo = prompt+response.choices[0].text output = start + append_finish(response) save_file(f'convos/convo_{count}.txt', output) print(f'Saved convo_{count}.txt') count += 1 except: continue
[ "\n", "STORYBOT: Every great story has an antagonist. In can be a character or circumstance. Here are some ideas I came up with.", "<<CUSTOMER>>", "STORYBOT: What sort of setting would you like for your story to take place? If you're not sure, I can brainstorm some ideas.", "storybot_prompt.txt" ]
2024-01-10
blennon/storybot
finetune.py
import requests import openai from pprint import pprint openai.api_key = os.environ['OPENAI_API_KEY'] def file_upload(filename, purpose='fine-tune'): resp = openai.File.create(purpose=purpose, file=open(filename)) pprint(resp) return resp def file_list(): resp = openai.File.list() pprint(resp) def finetune_model(fileid, suffix, model='davinci'): header = {'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % open_ai_api_key} payload = {'training_file': fileid, 'model': model, 'suffix': suffix} resp = requests.request(method='POST', url='https://api.openai.com/v1/fine-tunes', json=payload, headers=header, timeout=45) pprint(resp.json()) def finetune_list(): header = {'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % open_ai_api_key} resp = requests.request(method='GET', url='https://api.openai.com/v1/fine-tunes', headers=header, timeout=45) pprint(resp.json()) def finetune_events(ftid): header = {'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % open_ai_api_key} resp = requests.request(method='GET', url='https://api.openai.com/v1/fine-tunes/%s/events' % ftid, headers=header, timeout=45) pprint(resp.json()) def finetune_get(ftid): header = {'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % open_ai_api_key} resp = requests.request(method='GET', url='https://api.openai.com/v1/fine-tunes/%s' % ftid, headers=header, timeout=45) pprint(resp.json()) if __name__ == "__main__": resp = file_upload('finetune_data.jsonl') finetune_model(resp['id'], 'storybot') finetune_list()
[]
2024-01-10
magn3144/Bachelorproject
Scraper~scraper_generator.py
import openai import os import regex as re api_key = os.environ.get("OPENAI_API_KEY") def get_text_response(prompt): openai.api_key = api_key completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": prompt} ] ) return completion.choices[0].message.content def save_to_file(file_name, code): with open(file_name, 'w') as file: file.write(code) def generate_scraper(prompt): response_text = get_text_response(prompt) scraper_code = re.search(r"(?<=```python\n)[\s\S]+?(?=\n```)", response_text).group(0) save_to_file("scraper_code.py", scraper_code) return scraper_code
[]
2024-01-10
Joonyeong97/langchain-summarize-bot
app~callback.py
"""Callback handlers used in the app.""" from langchain.callbacks.base import AsyncCallbackHandler class LLMCallbackHandler(AsyncCallbackHandler): """Callback handler for streaming LLM responses.""" def __init__(self, websocket): self.websocket = websocket
[]
2024-01-10
vicodevs667/nemoChatbot
run_ai_conversation.py
from langchain import PromptTemplate, HuggingFaceHub from langchain.chains import RetrievalQA from langchain.embeddings import HuggingFaceEmbeddings from langchain import HuggingFacePipeline from langchain.vectorstores import FAISS from langchain.document_loaders import PyPDFLoader, DirectoryLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.llms import CTransformers import os import sys import gradio as gr import time import utils os.environ["HUGGINGFACEHUB_API_TOKEN"] = utils.get_huggingface_api_key() repo_id = "google/flan-t5-large" loader = DirectoryLoader('data/', glob="*.pdf", loader_cls= PyPDFLoader) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64) text_chunks = text_splitter.split_documents(documents) #print(len(text_chunks)) embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device':'cpu'}) vector_store = FAISS.from_documents(text_chunks, embeddings) #print(text_chunks) query="lugares para hacer turismo en México" docs = vector_store.similarity_search(query) """ llm = CTransformers(model = "model/pytorch_model-00001-of-00002.bin", model_type="llama", max_new_tokens=512, temperature=0.2, repetition_penalty=1.13 ) #llm = HuggingFacePipeline.from_model_id(model_id="OpenAssistant/stablelm-7b-sft-v7-epoch-3", task="text-generation", model_kwargs={"temperature": 0.2, "max_length": 2048, 'device_map': 'auto'}) """ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs= {"temperature":0.2, "max_length": 512}) template="""Eres un asistente de turismo a México y debes responder la pregunta tomando en cuenta el contexto dado. Si la pregunta es hola debes responder saludando al usuario y pregúntandole en que le puedes ayudar. Si la pregunta no puede ser respondida usando la información proporcionada, responde con "chale, ni idea wey" Contexto:{context} Pregunta:{question} Respuesta (escribe como mexicano informal): """ qa_prompt = PromptTemplate(template=template, input_variables=['context', 'question']) chain = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=vector_store.as_retriever(search_kwargs={'k': 8}), return_source_documents=True, chain_type_kwargs={'prompt': qa_prompt}) """while True: user_input=input(f"prompt:") if query == 'exit': print('Exiting') sys.exit() if query == '': continue result = chain({'query':user_input}) print(f"Answer:{result['result']}") """ def bot(query): llm_response = chain({'query':query}) return llm_response['result'] with gr.Blocks(title='Nemo Chatbot de Turismo') as demo: gr.Markdown("# Chatbot - México a tu alcance") chatbot = gr.Chatbot([], elem_id="chatbot", height=700) msg = gr.Textbox(label="Usuario", placeholder="Ingrese su consulta") clear = gr.ClearButton([msg, chatbot], value="Limpiar contenido") def respond(message, chat_history): bot_message = bot(message) chat_history.append((message, bot_message)) time.sleep(2) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) demo.launch()
[ "question", "chale, ni idea wey", "context", "Eres un asistente de turismo a México y debes responder la pregunta tomando en cuenta el contexto dado.\nSi la pregunta es hola debes responder saludando al usuario y pregúntandole en que le puedes ayudar.\nSi la pregunta no puede ser respondida usando la información proporcionada, responde con \"chale, ni idea wey\"\n\nContexto:{context}\nPregunta:{question}\n\nRespuesta (escribe como mexicano informal):\n" ]
2024-01-10
vicodevs667/nemoChatbot
run_code_model.py
from langchain.llms import CTransformers from langchain.chains import LLMChain from langchain import PromptTemplate import os import io import gradio as gr import time custom_prompt_template = """ You are an AI tourism assistant to visit Mexico, and return recommendations about beautiful places to visit in Mexico Query: {query} Helpful Answer (escribe como mexicano informal): """ def set_custom_prompt(): prompt = PromptTemplate(template=custom_prompt_template, input_variables=['query']) return prompt #Loading the model def load_model(): llm = CTransformers( model = "model/codellama-7b-instruct.ggmlv3.Q4_0.bin", model_type="llama", max_new_tokens = 512, temperature = 0.2, repetition_penalty = 1.13 ) return llm print(load_model()) def chain_pipeline(): llm = load_model() qa_prompt = set_custom_prompt() qa_chain = LLMChain( prompt=qa_prompt, llm=llm ) return qa_chain llmchain = chain_pipeline() def bot(query): llm_response = llmchain.run({"query": query}) return llm_response with gr.Blocks(title='Nemo Chatbot de Turismo') as demo: gr.Markdown("# Chatbot - México a tu alcance") chatbot = gr.Chatbot([], elem_id="chatbot", height=700) msg = gr.Textbox(label="Usuario", placeholder="Ingrese su consulta") clear = gr.ClearButton([msg, chatbot], value="Limpiar contenido") def respond(message, chat_history): bot_message = bot(message) chat_history.append((message, bot_message)) time.sleep(2) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) demo.launch()
[ "\nYou are an AI tourism assistant to visit Mexico, and return recommendations about beautiful places to visit in Mexico\nQuery: {query}\n\nHelpful Answer (escribe como mexicano informal):\n" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~Graph_Utils.py
from langchain.prompts import PromptTemplate from langchain import LLMChain from sentence_transformers import SentenceTransformer from CustomLibrary.Graph_Queries import construct_path_string, construct_relationship_string from sklearn.cluster import KMeans, MiniBatchKMeans from langchain.vectorstores import Chroma, FAISS from sklearn.preprocessing import StandardScaler import numpy as np from langchain.embeddings import HuggingFaceEmbeddings from typing import List, Optional import gc def cluster_and_select(paths_list, n_cluster, progress_callback=None): model = SentenceTransformer('sentence-transformers/all-MiniLM-L12-v2') sentences_list = [construct_path_string(path['nodes'], path['relationships']) for path in paths_list] batch_size = 2048 total_iterations = len(sentences_list) // batch_size + 1 embeddings_list = [] for i in range(0, len(sentences_list), batch_size): batch_sentences = sentences_list[i:i+batch_size] # Embed documents for the batch batch_embeddings_array = np.array(model.encode(batch_sentences, convert_to_tensor=True).cpu()) embeddings_list.append(batch_embeddings_array) # Update the progress bar if progress_callback: progress_callback((i + len(batch_sentences)) / len(sentences_list)) # Concatenate embeddings from all batches embeddings_array = np.concatenate(embeddings_list) # Continue with the remaining code scaler = StandardScaler() scaled_features = scaler.fit_transform(embeddings_array) if n_cluster == 0: n_clusters = 1 else: n_clusters = n_cluster kmeans = MiniBatchKMeans(n_clusters=n_clusters, init="random", n_init=10, max_iter=300, random_state=42) kmeans.fit(scaled_features) cluster_labels = kmeans.labels_ cluster_centers = kmeans.cluster_centers_ cluster_documents = {} for i, label in enumerate(cluster_labels): document = sentences_list[i] if label not in cluster_documents: cluster_documents[label] = document final_result = list(cluster_documents.values()) print("done clustering") return final_result def embed_and_select(paths_list, question, n_embed): sentences_list = [construct_path_string(path['nodes'], path['relationships']) for path in paths_list] hf = HuggingFaceEmbeddings( model_name='pritamdeka/S-Bluebert-snli-multinli-stsb', model_kwargs={'device': 'cuda'}, encode_kwargs={'normalize_embeddings': True}) db = Chroma.from_texts(sentences_list, hf) retriever = db.as_retriever(search_kwargs={"k": n_embed}) docs = retriever.get_relevant_documents(question)[:n_embed] final_result = [doc.page_content for doc in docs] del db, retriever, docs, hf, sentences_list gc.collect() print("done embedding") return final_result def select_paths(paths, question, n_cluster, n_embed, progress_callback): if len(paths) < n_cluster: n_cluster = len(paths) clustered_paths = cluster_and_select(paths, n_cluster, progress_callback) selected_paths_stage1 = [path for path in paths if construct_path_string(path['nodes'], path['relationships']) in clustered_paths and None not in path['nodes']] # Create a dictionary mapping string representations to original paths path_dict = {construct_path_string(path['nodes'], path['relationships']): path for path in selected_paths_stage1} embedded_paths = embed_and_select(selected_paths_stage1, question, n_embed) selected_paths_stage2 = [path_dict[path_str] for path_str in embedded_paths] selected_nodes = [node for path in selected_paths_stage2 for node in path['nodes']] paths_list = [construct_path_string(path['nodes'], path['relationships']) for path in selected_paths_stage2] paths_list = list(set(paths_list)) unique_rels_list = [construct_relationship_string(path['nodes'], path['relationships']) for path in selected_paths_stage2] unique_rels_list = list(set(unique_rels_list)) return paths_list, selected_nodes, unique_rels_list def cluster_and_select_pharos(paths_list, n_cluster, progress_callback=None): model = SentenceTransformer('sentence-transformers/all-MiniLM-L12-v2') sentences_list = [] for path_list in paths_list: for path in path_list: nodes = path['nodes'] relationships = path['relationships'] sentence = construct_path_string(nodes, relationships) sentences_list.append(sentence) batch_size = 2048 total_iterations = len(sentences_list) // batch_size + 1 embeddings_list = [] for i in range(0, len(sentences_list), batch_size): batch_sentences = sentences_list[i:i+batch_size] # Embed documents for the batch batch_embeddings_array = np.array(model.encode(batch_sentences, convert_to_tensor=True).cpu()) embeddings_list.append(batch_embeddings_array) # Update the progress bar if progress_callback: progress_callback((i + len(batch_sentences)) / len(sentences_list)) # Concatenate embeddings from all batches embeddings_array = np.concatenate(embeddings_list) # Continue with the remaining code scaler = StandardScaler() scaled_features = scaler.fit_transform(embeddings_array) n_clusters = n_cluster kmeans = MiniBatchKMeans(n_clusters=n_clusters, init="random", n_init=10, max_iter=300, random_state=42) kmeans.fit(scaled_features) cluster_labels = kmeans.labels_ cluster_centers = kmeans.cluster_centers_ cluster_documents = {} for i, label in enumerate(cluster_labels): document = sentences_list[i] if label not in cluster_documents: cluster_documents[label] = document final_result = list(cluster_documents.values()) print("done clustering") print(final_result) return final_result def embed_and_select_med_pharos(paths_list, question, n_embed): sentences_list = [] for path in paths_list: nodes = path['nodes'] relationships = path['relationships'] sentence = construct_path_string(nodes, relationships) sentences_list.append(sentence) hf = HuggingFaceEmbeddings( model_name='pritamdeka/S-Bluebert-snli-multinli-stsb', model_kwargs={'device': 'cuda'}, encode_kwargs={'normalize_embeddings': True} ) db = Chroma.from_texts(sentences_list, hf) retriever = db.as_retriever(search_kwargs={"k": n_embed}) docs = retriever.get_relevant_documents(question)[:n_embed] final_result = [doc.page_content for doc in docs] del db, retriever, docs, hf, sentences_list gc.collect() print("done embedding") return final_result def select_paths_pharos(paths, question, n_cluster, n_embed, progress_callback): if len(paths) < n_cluster: n_cluster = len(paths) clustered_paths = cluster_and_select_pharos(paths, n_cluster, progress_callback) selected_paths_stage1 = [path for path_list in paths for path in path_list if construct_path_string(path['nodes'], path['relationships']) in clustered_paths and None not in path['nodes']] print(selected_paths_stage1) # Create a dictionary mapping string representations to original paths path_dict = {construct_path_string(path['nodes'], path['relationships']): path for path in selected_paths_stage1} embedded_paths = embed_and_select_med_pharos(selected_paths_stage1, question, n_embed) selected_paths_stage2 = [path_dict[path_str] for path_str in embedded_paths] selected_nodes = [node for path in selected_paths_stage2 for node in path['nodes']] paths_list = [construct_path_string(path['nodes'], path['relationships']) for path in selected_paths_stage2] paths_list = list(set(paths_list)) unique_rels_list = [construct_relationship_string(path['nodes'], path['relationships']) for path in selected_paths_stage2] unique_rels_list = list(set(unique_rels_list)) return paths_list, selected_nodes, unique_rels_list, selected_paths_stage2
[]
2024-01-10
choung0124/OPC_AI
opc_app_novel.py
import streamlit as st from transformers import logging from langchain.llms import TextGen from langchain.prompts import PromptTemplate from langchain import LLMChain import streamlit as st from pyvis.network import Network from CustomLibrary.Custom_Chains import CustomLLMChain, CustomLLMChainAdditionalEntities from CustomLibrary.Custom_Prompts import ( OPC_Entity_type_Template, OPC_Entity_Extraction_Template, Final_Answer_Template_Alpaca ) from CustomLibrary.App_Utils import( get_umls_info, extract_entities, get_names_list, get_names_list, get_entity_types, get_additional_entity_umls_dict, create_and_display_network, parse_relationships_pyvis, create_docs_from_results ) from itertools import combinations, product from langchain.embeddings import HuggingFaceEmbeddings from CustomLibrary.OPC_GraphQA import OPC_GraphQA from CustomLibrary.OPC_GraphQA_Sim import OPC_GraphQA_Sim from chromadb.utils import embedding_functions import chromadb import re # could there be a synergistic interaction between peanut sprouts and ashwagandha? # Could there be a synergistic interaction between sildenafil and ashwagandha to treat alzheimer's? #Withaferin A, Withanolide A, Withanolide B, Withanolide C, Withanolide D, Withanone, Withanoside IV, Withanoside V # Flavonoid, Resveratrol, Polyphenol, Aspartic acid # Withaferin A, Withanone, Withanoside IV logging.set_verbosity(logging.CRITICAL) sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction( model_name="pritamdeka/S-Bluebert-snli-multinli-stsb", device="cuda", normalize_embeddings=True ) @st.cache_data() def initialize_models(): model_url = "https://enjoy-brought-waters-educational.trycloudflare.com/" local_model_url = "http://127.0.0.1:5000/" llm = TextGen(model_url=model_url, max_new_tokens=2048) local_llm = TextGen(model_url=local_model_url, max_new_tokens=2048) return llm, local_llm @st.cache_data() def initialize_knowledge_graph(): uri = "neo4j://localhost:7687" username = "neo4j" password = "NeO4J" return uri, username, password class SessionState(object): def __init__(self, **kwargs): for key, val in kwargs.items(): setattr(self, key, val) def get_state(**kwargs): if 'state' not in st.session_state: st.session_state['state'] = SessionState(**kwargs) return st.session_state['state'] # Define the progress bar progress_bar = st.empty() # Define the callback function to update the progress bar def progress_callback(progress): progress_bar.progress(progress) def initialize_all(state): if not hasattr(state, 'initialized'): state.llm, state.local_llm = initialize_models() state.uri, state.username, state.password = initialize_knowledge_graph() OPC_Entity_Extraction_Prompt = PromptTemplate(template=OPC_Entity_Extraction_Template, input_variables=["input"]) state.OPC_Entity_Extraction_Chain = CustomLLMChain(prompt=OPC_Entity_Extraction_Prompt, llm=state.llm, output_key="output") Entity_type_prompt = PromptTemplate(template=OPC_Entity_type_Template, input_variables=["input"]) state.Entity_type_chain = LLMChain(prompt=Entity_type_prompt, llm=state.llm) state.initialized = True # Get the state state = get_state(user_options=[]) # Initialize all initialize_all(state) question = st.text_input("Enter your question") # initialize your counter if 'counter' not in st.session_state: st.session_state.counter = 0 # initialize the last processed question if 'last_question' not in st.session_state: st.session_state.last_question = None # initialize the entities_list if 'entities_list' not in st.session_state: st.session_state.entities_list = [] # initialize the constituents_dict if 'constituents_dict' not in st.session_state: st.session_state.constituents_dict = {} if 'paths_list' not in st.session_state: st.session_state.paths_list = [] if 'names_list' not in st.session_state: st.session_state.names_list = [] if 'form_submitted' not in st.session_state: st.session_state.form_submitted = False # if a new question is entered, process it if question and question != st.session_state.last_question: with st.spinner("Processing..."): # Entity extraction entities = state.OPC_Entity_Extraction_Chain.run(question) entities_umls_ids = get_umls_info(entities) names_list = get_names_list(entities_umls_ids) entity_types = get_entity_types(state.Entity_type_chain, names_list) print("entity_types", entity_types) print("names list", names_list) # get the list of entities st.session_state.entities_list = list(entity_types.items()) # store the processed question st.session_state.last_question = question # reset the counter for the new question st.session_state.counter = 0 else: # if there's no new question, use the entities from the last question entities_list = st.session_state.entities_list if st.session_state.counter < len(st.session_state.entities_list): entity, entity_type = st.session_state.entities_list[st.session_state.counter] if entity_type in ["Food", "Metabolite", "Drug"]: with st.form(key=str(entity)): st.write("Please input the chemical constituents of:", entity, "(Please separate each constituent with a comma)") entity_constituents = st.text_input(f"Input the chemical constituents of {entity}:", key=str(entity)) none_checkbox = st.checkbox("None", key=str(entity).join("_NoneCheckbox")) # Move none_checkbox inside the form submitted = st.form_submit_button('Submit') if submitted: if none_checkbox: st.session_state.counter += 1 else: constituents = entity_constituents.split(",") if entity_constituents else [] # Only add entity to constituents_dict if constituents is not empty and contains non-empty strings if constituents and any(constituent.strip() for constituent in constituents): st.session_state.constituents_dict[entity] = constituents for constituent in constituents: path = { 'nodes': [entity, constituent], 'relationships': ['contains constituent'] } st.session_state.paths_list.append(path) st.session_state.counter += 1 print(st.session_state.constituents_dict) # Debug print statement else: st.session_state.entities_list st.session_state.counter += 1 if st.session_state.counter == len(st.session_state.entities_list): st.write("All entities processed") nodes_list = [] paths = st.session_state.paths_list for entity, entity_type in st.session_state.entities_list: if entity_type in ["Food", "Metabolite", "Drug"]: if entity in st.session_state.constituents_dict: constituents = st.session_state.constituents_dict[entity] nodes_list.extend([entity, *constituents]) else: constituents = [] with st.expander(f"Constituents of {entity}"): st.write(constituents) if len(st.session_state.entities_list) == 1 and st.session_state.entities_list[0][1] in ["Food", "Metabolite", "Drug"]: st.write(f"Would you like to find novel target diseases for {st.session_state.entities_list[0][0]}?") if st.button("Yes"): with st.spinner("Running OPC GraphQA..."): persist_directory = ("/mnt/c/aribio/OPC_BRAIN/vectordbs") chromadb_client = chromadb.PersistentClient(path=persist_directory) name = question[:50] # Truncate the string to the first 50 characters # Ensure the truncated string starts and ends with an alphanumeric character name = ''.join(e for e in name if e.isalnum() or e in ['-', '_']) # Replace two consecutive periods with a single period name = name.replace('..', '.') # Check if the string is a valid IPv4 address, if so, append a character to make it invalid if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', name): name += 'a' vectordb = chromadb_client.create_collection(name=name, embedding_function=sentence_transformer_ef, get_or_create=True) entities_list = st.session_state.entities_list for entitiy , entity_type in entities_list: if entity in st.session_state.constituents_dict and st.session_state.constituents_dict[entity]: constituents = st.session_state.constituents_dict[entity] for constituent in constituents: KG = OPC(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=[entity], constituents_dict={entity: [constituent]}, constituents_paths=st.session_state.paths_list) graph_query = KG._call(question, progress_callback=progress_callback) if graph_query is not None and graph_query['result'] is not None: answer = graph_query['result'] all_rels = graph_query['all_rels'] vectordb.add(ids=[answer_id], documents=[answer], metadatas=[{"Entities" :list(entity_combinations.keys()), "Graph_rels": all_rels}]) else: continue st.header("Final Answer:") results = vectordb.query(query_texts=[question], n_results=4) result_dict_list = create_docs_from_results(results) selected_answers = [] for result in result_dict_list: answer = result["document"] selected_answers.append(answer) Final_chain_prompt = PromptTemplate(template=Final_Answer_Template_Alpaca, input_variables=["input", "question"]) Final_chain = LLMChain(llm=state.local_llm, prompt=Final_chain_prompt) final_answer = Final_chain.run(input=selected_answers, question=question) st.write(final_answer) st.header("Top 4 pieces of evidence:") for result in result_dict_list: answer = result["document"] metadata = result["metadata"] entities_list = metadata["Entities"] graph_rels = metadata["Graph_rels"] with st.expander(f"Answer from this combination of entities: {entities_list}"): st.header("Answer:") st.write(answer) nodes, edges = parse_relationships_pyvis(graph_rels) create_and_display_network(nodes, edges, '#fff6fe', "Graph", entities_list[0], entities_list[1]) if len(st.session_state.entities_list) >= 2: if st.button(f"Predict synergy between {st.session_state.entities_list[0][0]} and {st.session_state.entities_list[1][0]}"): with st.spinner("Running OPC GraphQA..."): # Assuming 'entities_list' is a list of all entities entities_list = st.session_state.entities_list # Dictionary to hold combinations for each entity entity_combinations = {} for entity, entity_type in entities_list: if entity in st.session_state.constituents_dict and st.session_state.constituents_dict[entity]: # Get the constituents for the current entity constituents = st.session_state.constituents_dict[entity] if len(constituents) > 2: # Generate all combinations of 4 constituents combinations_of_constituents = list(combinations(constituents, 2)) else: # If there are 4 or fewer constituents, use them directly combinations_of_constituents = [constituents] # Store the combinations in the dictionary entity_combinations[entity] = combinations_of_constituents # Generate all combinations of combinations for each entity all_combinations = list(product(*entity_combinations.values())) st.write("All combinations:") st.write(len(all_combinations)) counter = 0 persist_directory = ("/mnt/c/aribio/OPC_BRAIN/vectordbs") chromadb_client = chromadb.PersistentClient(path=persist_directory) name = question[:50] # Truncate the string to the first 50 characters # Ensure the truncated string starts and ends with an alphanumeric character name = ''.join(e for e in name if e.isalnum() or e in ['-', '_']) # Replace two consecutive periods with a single period name = name.replace('..', '.') # Check if the string is a valid IPv4 address, if so, append a character to make it invalid if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', name): name += 'a' vectordb = chromadb_client.create_collection(name=name, embedding_function=sentence_transformer_ef, get_or_create=True) for combo in all_combinations: counter += 1 answer_count = vectordb.count() answer_id = answer_count + 1 # 'combo' is a tuple of combinations, one for each entity # Convert it to a dictionary constituents_dict = {} for i, entity in enumerate(entity_combinations.keys()): constituents_dict[entity] = list(combo[i]) # The i-th combination for the i-th entity # Run OPC GraphQA for each combination KG = OPC_GraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=list(entity_combinations.keys()), # All entities constituents_dict=constituents_dict, # The current combination of constituents constituents_paths=st.session_state.paths_list) graph_query = KG._call(question, progress_callback=progress_callback) if graph_query is not None and graph_query['result'] is not None: answer = graph_query['result'] all_rels = graph_query['all_rels'] vectordb.add(ids=[answer_id], documents=[answer], metadatas=[{"Entities" :list(entity_combinations.keys()), "Graph_rels": all_rels}]) else: continue st.header("Final Answer:") results = vectordb.query(query_texts=[question], n_results=4) result_dict_list = create_docs_from_results(results) selected_answers = [] for result in result_dict_list: answer = result["document"] selected_answers.append(answer) Final_chain_prompt = PromptTemplate(template=Final_Answer_Template_Alpaca, input_variables=["input", "question"]) Final_chain = LLMChain(llm=state.local_llm, prompt=Final_chain_prompt) final_answer = Final_chain.run(input=selected_answers, question=question) st.write(final_answer) st.header("Top 4 pieces of evidence:") for result in result_dict_list: answer = result["document"] metadata = result["metadata"] entities_list = metadata["Entities"] graph_rels = metadata["Graph_rels"] with st.expander(f"Answer from this combination of entities: {entities_list}"): st.header("Answer:") st.write(answer) nodes, edges = parse_relationships_pyvis(graph_rels) create_and_display_network(nodes, edges, '#fff6fe', "Graph", entities_list[0], entities_list[1])
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~OPC_GraphQA.py
from __future__ import annotations from typing import Any, Dict, List, Optional from langchain.chains.llm import LLMChain from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from py2neo import Graph import numpy as np from CustomLibrary.Graph_Queries import ( query_direct, query_direct_constituents, query_between_direct, get_node_labels_dict, get_node_label ) from CustomLibrary.Graph_Utils import ( select_paths ) from langchain.prompts import PromptTemplate import gc from CustomLibrary.OPC_Utils import pubchem_query, similar_pubchem_query from CustomLibrary.Custom_Prompts import Graph_Answer_Gen_Template_alpaca def generate_answer(llm, entities_list, question, start_paths, mid_paths, inter_direct_inter): prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) #prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) gen_chain = LLMChain(llm=llm, prompt=prompt) start_paths = ','.join(start_paths) Inter_relationships = mid_paths + inter_direct_inter Inter_sentences = ','.join(Inter_relationships) sep1 = f"Starting paths from {entities_list}:" sep2 = f"Intermediate paths of {entities_list}:" sentences = '\n'.join([sep1, start_paths, sep2, Inter_sentences]) answer = gen_chain.run(input=sentences, question=question) print(answer) return answer class OPC_GraphQA: def __init__(self, uri, username, password, llm, entities_list, constituents_dict, constituents_paths): self.graph = Graph(uri, auth=(username, password)) self.llm = llm self.entities_list = entities_list self.constituents_dict = constituents_dict self.constituents_paths = constituents_paths print("entities_list") print(self.entities_list) print("constituents_dict") print(self.constituents_dict) def _call(self, question, progress_callback=None): start_paths = [] start_nodes = [] start_graph_rels = [] for entity in self.entities_list: entity_label, entity_name = get_node_label(self.graph, entity) paths = query_direct(self.graph, entity_name, entity_label) if paths: (CKG_paths, CKG_nodes, CKG_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) start_paths.extend(CKG_paths) start_nodes.extend(CKG_nodes) start_graph_rels.extend(CKG_rels) if entity in self.constituents_dict and self.constituents_dict[entity]: constituents = self.constituents_dict[entity] constituents = [constituent for constituent in constituents if constituent != 'None'] if constituents and 'None' not in constituents: for constituent in constituents: constituent_label, constituent_name = get_node_label(self.graph, constituent) paths = query_direct_constituents(self.graph, constituent_name, constituent_label) if paths: if len(paths)//3 < 1: n_cluster = 1 else: n_cluster = len(paths)//3 (Constituent_CKG_paths, Constituent_CKG_nodes, Constituent_CKG_rels) = select_paths(paths, question, n_cluster, 3, progress_callback) start_paths.extend(Constituent_CKG_paths) start_nodes.extend(Constituent_CKG_nodes) start_graph_rels.extend(Constituent_CKG_rels) pubchem_result = pubchem_query(entity, constituent, question, progress_callback) if pubchem_result: (pubchem_paths, pubchem_nodes, pubchem_rels) = pubchem_result start_paths.extend(pubchem_paths) start_nodes.extend(pubchem_nodes) start_graph_rels.extend(pubchem_rels) similar_pubchem_result = similar_pubchem_query(entity, constituent, question, progress_callback) if similar_pubchem_result: (similar_pubchem_paths, similar_pubchem_nodes, similar_pubchem_rels) = similar_pubchem_result start_paths.extend(similar_pubchem_paths) start_nodes.extend(similar_pubchem_nodes) start_graph_rels.extend(similar_pubchem_rels) else: continue else: continue if len(start_paths) == 0: return None print("start_paths:", len(start_paths)) print("start_nodes:", len(start_nodes)) print("start_graph_rels:", len(start_graph_rels)) query_nodes = list(set(start_nodes)) print("query nodes") print(len(query_nodes)) print(query_nodes) mid_direct_paths = set() mid_direct_nodes = set() mid_direct_graph_rels = set() node_labels = get_node_labels_dict(self.graph, query_nodes) for node in query_nodes: node_label = node_labels.get(node) if node_label is not None: paths = query_direct(self.graph, node, node_label) if paths: (selected_paths, selected_nodes, selected_graph_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) mid_direct_paths.update(selected_paths) mid_direct_nodes.update(selected_nodes) mid_direct_graph_rels.update(selected_graph_rels) print("success") print(len(selected_paths)) print(selected_paths) del paths, selected_paths, selected_nodes, selected_graph_rels gc.collect() else: print("skipping") continue print("number of unique inter_direct_relationships:") print(len(mid_direct_paths)) mid_inter_paths = query_between_direct(self.graph, list(mid_direct_nodes), query_nodes) n_cluster = max(len(mid_inter_paths)//10, 1) if n_cluster > 30: n_embed = 30 else: n_embed = n_cluster print("n_cluster:", n_cluster) (selected_mid_inter_paths, selected_mid_inter_nodes, selected_mid_inter_graph_rels) = select_paths(mid_inter_paths, question, n_cluster, n_embed, progress_callback) print("final_inter_direct_inter_relationships") print(len(selected_mid_inter_paths)) all_graph_rels = set() all_graph_rels.update(selected_mid_inter_graph_rels) all_graph_rels.update(mid_direct_graph_rels) all_graph_rels.update(start_graph_rels) all_graph_rels = list(all_graph_rels) print("all_graph_rels") print(len(all_graph_rels)) ######################################################################################################## params = { "llm": self.llm, "entities_list": self.entities_list, "question": question, "start_paths": start_paths, "mid_paths": list(mid_direct_paths), "inter_direct_inter": list(selected_mid_inter_paths), } final_context = generate_answer(**params) answer = final_context response = {"result": answer, "all_rels": all_graph_rels} return response
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~OpenTargets_Graph_QA.py
from __future__ import annotations from typing import Any, Dict, List, Optional from langchain.chains.llm import LLMChain from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from py2neo import Graph import numpy as np from langchain.prompts import PromptTemplate import gc from CustomLibrary.Graph_Queries import ( query_direct, query_between_direct, get_node_labels_dict, ) from CustomLibrary.Graph_Utils import ( select_paths, ) from CustomLibrary.Custom_Prompts import Graph_Answer_Gen_Template_alpaca from CustomLibrary.OpenTargets import ( query_disease_info, query_drug_info, query_target_info, query_predicted_drug_info ) def generate_answer(llm, entities_list, question, start_paths, mid_paths, inter_direct_inter): prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) #prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) gen_chain = LLMChain(llm=llm, prompt=prompt) start_paths = ','.join(start_paths) Inter_relationships = mid_paths + inter_direct_inter Inter_sentences = ','.join(Inter_relationships) sep1 = f"Starting paths from {entities_list}:" sep2 = f"Intermediate paths of {entities_list}:" sentences = '\n'.join([sep1, start_paths, sep2, Inter_sentences]) answer = gen_chain.run(input=sentences, question=question) print(answer) return answer class OpenTargetsGraphQA: def __init__(self, uri, username, password, llm, entities_list, constituents_dict, constituents_paths): self.graph = Graph(uri, auth=(username, password)) self.llm = llm self.entities_list = entities_list self.constituents_dict = constituents_dict self.constituents_paths = constituents_paths def _call(self, question, progress_callback=None): start_paths = [] start_nodes = [] start_graph_rels = [] for entity in self.entities_list: entity_name, entity_type = entity if entity_type == "Disease": result = query_disease_info(entity_name, question) elif entity_type == "Drug" or "Food" or "Metabolite": result = query_drug_info(entity_name, question) elif entity_type == "Gene": result = query_target_info(entity_name, question) if result is not None: OT_paths, OT_nodes, OT_graph_rels = result start_paths.extend(OT_paths) start_nodes.extend(OT_nodes) start_graph_rels.extend(OT_graph_rels) if entity in self.constituents_dict and self.constituents_dict[entity]: constituents = self.constituents_dict[entity] constituents = [constituent for constituent in constituents if constituent != 'None'] if constituents and 'None' not in constituents: for constituent in constituents: constituent_name, constituent_type = constituent if constituent_type == "Disease": result = query_disease_info(constituent_name, question) elif constituent_type == "Drug" or "Food" or "Metabolite": result = query_drug_info(constituent_name, question) elif constituent_type == "Gene": result = query_target_info(constituent_name, question) if result is not None: (Constituent_OT_Paths, Constituent_OT_Nodes, Constituent_OT_Rels) = result start_paths.extend(Constituent_OT_Paths) start_nodes.extend(Constituent_OT_Nodes) start_graph_rels.extend(Constituent_OT_Rels) else: continue print("start_paths:", len(start_paths)) print("start_nodes:", len(start_nodes)) print("start_graph_rels:", len(start_graph_rels)) query_nodes = list(set(start_nodes)) print("query nodes") print(len(query_nodes)) print(query_nodes) mid_direct_paths = set() mid_direct_nodes = set() mid_direct_graph_rels = set() node_labels = get_node_labels_dict(self.graph, query_nodes) for node in query_nodes: node_label = node_labels.get(node) if node_label is not None: paths = query_direct(self.graph, node, node_label) if paths: (selected_paths, selected_nodes, selected_graph_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) mid_direct_paths.update(selected_paths) mid_direct_nodes.update(selected_nodes) mid_direct_graph_rels.update(selected_graph_rels) print("success") print(len(selected_paths)) print(selected_paths) del paths, selected_paths, selected_nodes, selected_graph_rels gc.collect() else: print("skipping") continue print("number of unique inter_direct_relationships:") print(len(mid_direct_paths)) mid_inter_paths = query_between_direct(self.graph, list(mid_direct_nodes), query_nodes) n_cluster = max(len(mid_inter_paths)//10, 1) if n_cluster > 30: n_embed = 30 else: n_embed = n_cluster print("n_cluster:", n_cluster) (selected_mid_inter_paths, selected_mid_inter_nodes, selected_mid_inter_graph_rels) = select_paths(mid_inter_paths, question, n_cluster, n_embed, progress_callback) print("final_inter_direct_inter_relationships") print(len(selected_mid_inter_paths)) all_graph_rels = set() all_graph_rels.update(selected_mid_inter_graph_rels) all_graph_rels.update(mid_direct_graph_rels) all_graph_rels.update(start_graph_rels) all_graph_rels = list(all_graph_rels) print("all_graph_rels") print(len(all_graph_rels)) ######################################################################################################## params = { "llm": self.llm, "entities_list": self.entities_list, "question": question, "start_paths": start_paths, "mid_paths": list(mid_direct_paths), "inter_direct_inter": list(selected_mid_inter_paths), } final_context = generate_answer(**params) answer = final_context response = {"result": answer, "all_rels": all_graph_rels} return response
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
opc_app_new.py
import streamlit as st from transformers import logging from langchain.llms import TextGen from langchain.prompts import PromptTemplate from langchain import LLMChain import streamlit as st from pyvis.network import Network from CustomLibrary.Custom_Chains import CustomLLMChain, CustomLLMChainAdditionalEntities from CustomLibrary.Custom_Prompts import ( OPC_Entity_type_Template, OPC_Entity_Extraction_Template, Final_Answer_Template_Alpaca ) from CustomLibrary.App_Utils import( get_umls_info, extract_entities, get_names_list, get_names_list, get_entity_types, get_additional_entity_umls_dict, create_and_display_network, parse_relationships_pyvis, create_docs_from_results ) from itertools import combinations, product from langchain.embeddings import HuggingFaceEmbeddings from CustomLibrary.OPC_GraphQA import OPC_GraphQA from CustomLibrary.Pharos_Graph_QA import PharosGraphQA from CustomLibrary.OpenTargets_Graph_QA import OpenTargetsGraphQA from CustomLibrary.Predicted_QA import PredictedGrqphQA from chromadb.utils import embedding_functions import chromadb import ast import re # Could there be a synergistic interaction between the three drugs: lamotrigine, aripiprazole and methylphenidate for bipolar disorder? # What kind of interactions could there be between the three drugs: lamotrigine, aripiprazole and methylphenidate? # Could there be a synergistic interaction between the three drugs: lamotrigine, aripiprazole and methylphenidate? # could there be a synergistic interaction between peanut sprouts and ashwagandha? # Could there be a synergistic interaction between sildenafil and ashwagandha to treat alzheimer's? #Withaferin A, Withanolide A, Withanolide B, Withanolide C, Withanolide D, Withanone, Withanoside IV, Withanoside V # Flavonoid, Resveratrol, Polyphenol, Aspartic acid # Withaferin A, Withanone, Withanoside IV logging.set_verbosity(logging.CRITICAL) sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction( model_name="pritamdeka/S-Bluebert-snli-multinli-stsb", device="cuda", normalize_embeddings=True ) @st.cache_data() def initialize_models(): model_url = "https://enjoy-brought-waters-educational.trycloudflare.com/" local_model_url = "http://127.0.0.1:5000/" llm = TextGen(model_url=model_url, max_new_tokens=2048) local_llm = TextGen(model_url=local_model_url, max_new_tokens=2048) return llm, local_llm @st.cache_data() def initialize_knowledge_graph(): uri = "neo4j://localhost:7687" username = "neo4j" password = "NeO4J" return uri, username, password class SessionState(object): def __init__(self, **kwargs): for key, val in kwargs.items(): setattr(self, key, val) def get_state(**kwargs): if 'state' not in st.session_state: st.session_state['state'] = SessionState(**kwargs) return st.session_state['state'] # Define the progress bar progress_bar = st.empty() # Define the callback function to update the progress bar def progress_callback(progress): progress_bar.progress(progress) def initialize_all(state): if not hasattr(state, 'initialized'): state.llm, state.local_llm = initialize_models() state.uri, state.username, state.password = initialize_knowledge_graph() OPC_Entity_Extraction_Prompt = PromptTemplate(template=OPC_Entity_Extraction_Template, input_variables=["input"]) state.OPC_Entity_Extraction_Chain = CustomLLMChain(prompt=OPC_Entity_Extraction_Prompt, llm=state.llm, output_key="output") Entity_type_prompt = PromptTemplate(template=OPC_Entity_type_Template, input_variables=["input"]) state.Entity_type_chain = LLMChain(prompt=Entity_type_prompt, llm=state.llm) state.initialized = True class EntityCombination: def __init__(self, entity_name, entity_type): self.entityname = entity_name self.entitytype = entity_type # Get the state state = get_state(user_options=[]) # Initialize all initialize_all(state) question = st.text_input("Enter your question") # initialize your counter if 'counter' not in st.session_state: st.session_state.counter = 0 # initialize the last processed question if 'last_question' not in st.session_state: st.session_state.last_question = None # initialize the entities_list if 'entities_list' not in st.session_state: st.session_state.entities_list = [] # initialize the constituents_dict if 'constituents_dict' not in st.session_state: st.session_state.constituents_dict = {} if 'paths_list' not in st.session_state: st.session_state.paths_list = [] if 'names_list' not in st.session_state: st.session_state.names_list = [] if 'form_submitted' not in st.session_state: st.session_state.form_submitted = False # if a new question is entered, process it if question and question != st.session_state.last_question: with st.spinner("Processing..."): # Entity extraction entities = state.OPC_Entity_Extraction_Chain.run(question) entities_umls_ids = get_umls_info(entities) names_list = get_names_list(entities_umls_ids) entity_types = get_entity_types(state.Entity_type_chain, names_list) print("names list", names_list) # get the list of entities st.session_state.entities_list = list(entity_types.items()) # store the processed question st.session_state.last_question = question # reset the counter for the new question st.session_state.counter = 0 else: # if there's no new question, use the entities from the last question entities_list = st.session_state.entities_list if st.session_state.counter < len(st.session_state.entities_list): entity, entity_type = st.session_state.entities_list[st.session_state.counter] if entity_type in ["Food", "Metabolite", "Drug"]: with st.form(key=str(entity)): st.write("Please input the chemical constituents of:", entity, "(Please separate each constituent with a comma)") entity_constituents = st.text_input(f"Input the chemical constituents of {entity}:", key=str(entity)) none_checkbox = st.checkbox("None", key=str(entity).join("_NoneCheckbox")) # Move none_checkbox inside the form submitted = st.form_submit_button('Submit') if submitted: if none_checkbox: st.session_state.constituents_dict[entity] = [entity] st.session_state.counter += 1 else: constituents = entity_constituents.split(",") if entity_constituents else [] # Only add entity to constituents_dict if constituents is not empty and contains non-empty strings if constituents and any(constituent.strip() for constituent in constituents): st.session_state.constituents_dict[entity] = constituents for constituent in constituents: path = { 'nodes': [entity, constituent], 'relationships': ['contains constituent'] } st.session_state.paths_list.append(path) st.session_state.counter += 1 print(st.session_state.constituents_dict) # Debug print statement else: st.session_state.counter += 1 if st.session_state.counter == len(st.session_state.entities_list): st.write("All entities processed") nodes_list = [] paths = st.session_state.paths_list for entity, entity_type in st.session_state.entities_list: if entity_type in ["Food", "Metabolite", "Drug"]: if entity in st.session_state.constituents_dict: constituents = st.session_state.constituents_dict[entity] nodes_list.extend([entity, *constituents]) else: constituents = [] with st.expander(f"Constituents of {entity}"): st.write(constituents) if st.button("Run OPC GraphQA"): with st.spinner("Running OPC GraphQA..."): # Assuming 'entities_list' is a list of all entities entities_list = st.session_state.entities_list # Dictionary to hold combinations for each entity entity_combinations = {} for entity, entity_type in entities_list: if entity in st.session_state.constituents_dict and st.session_state.constituents_dict [entity]: # Get the constituents for the current entity constituents = st.session_state.constituents_dict[entity] if len(constituents) > 2: # Generate all combinations of 4 constituents combinations_of_constituents = list(combinations(constituents, 1)) else: # If there are 4 or fewer constituents, use them directly combinations_of_constituents = [constituents] # Store the combinations in the dictionary entity_combinations[entity] = combinations_of_constituents else: entity_combinations[entity] = [entity] # Generate all combinations of combinations for each entity all_combinations = list(product(*entity_combinations.values())) print("entity_combinations") print(entity_combinations.keys()) print(all_combinations) st.write("All combinations:") st.write(len(all_combinations)) counter = 0 persist_directory = ("/mnt/c/aribio/OPC_BRAIN/vectordbs") chromadb_client = chromadb.PersistentClient(path=persist_directory) name = question[:50] # Truncate the string to the first 50 characters # Ensure the truncated string starts and ends with an alphanumeric character name = ''.join(e for e in name if e.isalnum() or e in ['-', '_']) # Replace two consecutive periods with a single period name = name.replace('..', '.') # Check if the string is a valid IPv4 address, if so, append a character to make it invalid if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', name): name += 'a' vectordb = chromadb_client.create_collection(name=name, embedding_function=sentence_transformer_ef, get_or_create=True) for combo in all_combinations: print("entity_combinations") print(entity_combinations.keys()) print(entity_combinations) counter += 1 answer_count = vectordb.count() answer_id = str(answer_count + 1) # 'combo' is a tuple of combinations, one for each entity # Convert it to a dictionary constituents_dict = {} for i, entity in enumerate(entity_combinations.keys()): if entity in st.session_state.constituents_dict and st.session_state.constituents_dict[entity]: constituents_dict[entity] = list(combo[i]) # Run OPC GraphQA for each combination KG = OPC_GraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=list(entity_combinations.keys()), # All entities constituents_dict=constituents_dict, # The current combination of constituents constituents_paths=st.session_state.paths_list) graph_query = KG._call(question, progress_callback=progress_callback) if graph_query is not None and graph_query['result'] is not None: answer = graph_query['result'] all_rels = graph_query['all_rels'] vectordb.add(ids=[answer_id], documents=[answer], metadatas=[{"Entities" : str(list(entity_combinations.keys())), "Graph_rels": str(all_rels)}]) pharos_answer_id = str(answer_count + 2) PHAROS_KG = PharosGraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=entities_list, constituents_dict=constituents_dict, constituents_paths=st.session_state.paths_list) pharos_graph_query = PHAROS_KG._call(question, progress_callback=progress_callback) if pharos_graph_query is not None and pharos_graph_query['result'] is not None: pharos_answer = pharos_graph_query['result'] pharos_all_rels = pharos_graph_query['all_rels'] vectordb.add(ids=[pharos_answer_id], documents=[pharos_answer], metadatas=[{"Entities" : str(list(entity_combinations.keys())), "Graph_rels": str(pharos_all_rels)}]) open_targets_answer_id = str(answer_count + 3) OpenTargets_KG = OpenTargetsGraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=entities_list, constituents_dict=constituents_dict, constituents_paths=st.session_state.paths_list) open_targets_graph_query = OpenTargets_KG._call(question, progress_callback=progress_callback) if open_targets_graph_query is not None and open_targets_graph_query['result'] is not None: open_targets_answer = open_targets_graph_query['result'] open_targets_all_rels = open_targets_graph_query['all_rels'] vectordb.add(ids=[open_targets_answer_id], documents=[open_targets_answer], metadatas=[{"Entities" : str(list(entity_combinations.keys())), "Graph_rels": str(open_targets_all_rels)}]) predicted_answer_id = str(answer_count + 4) Predicted_KG = PredictedGraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=entities_list, constituents_dict=constituents_dict, constituents_paths=st.session_state.paths_list) for response in Predicted_KG._call(question, progress_callback=progress_callback): if response is not None and response['result'] is not None: predicted_answer = response['result'] predicted_all_rels = response['all_rels'] vectordb.add(ids=[predicted_answer_id], documents=[predicted_answer], metadatas=[{"Entities" : str(list(entity_combinations.keys())), "Graph_rels": str(predicted_all_rels)}]) else: continue st.header("Final Answer:") results = vectordb.query(query_texts=[question], n_results=4) result_dict_list = create_docs_from_results(results) selected_answers = [] for result in result_dict_list: answer = result["document"] selected_answers.append(answer) Final_chain_prompt = PromptTemplate(template=Final_Answer_Template_Alpaca, input_variables=["input", "question"]) Final_chain = LLMChain(llm=state.local_llm, prompt=Final_chain_prompt) final_answer = Final_chain.run(input=selected_answers, question=question) st.write(final_answer) st.header("Top 4 pieces of evidence:") for result in result_dict_list: answer = result["document"] metadata = result["metadata"] entities_list = metadata["Entities"] graph_rels_str = metadata["Graph_rels"] graph_rels = ast.literal_eval(graph_rels_str) with st.expander(f"Answer from this combination of entities: {entities_list}"): st.header("Answer:") st.write(answer) nodes, edges = parse_relationships_pyvis(graph_rels) create_and_display_network(nodes, edges, '#fff6fe', "Graph", entities_list[0], entities_list[1])
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~Predicted_QA.py
from __future__ import annotations from typing import Any, Dict, List, Optional from langchain.chains.llm import LLMChain from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from py2neo import Graph import numpy as np from langchain.prompts import PromptTemplate import gc from CustomLibrary.Graph_Queries import ( query_direct, query_between_direct, get_node_labels_dict ) from CustomLibrary.Graph_Utils import ( select_paths, ) from CustomLibrary.Custom_Prompts import ( Graph_Answer_Gen_Template_alpaca ) from CustomLibrary.OpenTargets import( query_predicted_disease_info, query_predicted_target_info, query_predicted_drug_info ) def generate_answer(llm, entities_list, question, start_paths, mid_paths, inter_direct_inter): prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) #prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) gen_chain = LLMChain(llm=llm, prompt=prompt) start_paths = ','.join(start_paths) Inter_relationships = mid_paths + inter_direct_inter Inter_sentences = ','.join(Inter_relationships) sep1 = f"Starting paths from {entities_list}:" sep2 = f"Intermediate paths of {entities_list}:" sentences = '\n'.join([sep1, start_paths, sep2, Inter_sentences]) answer = gen_chain.run(input=sentences, question=question) print(answer) return answer class PredictedGrqphQA: def __init__(self, uri, username, password, llm, entities_list, constituents_dict, constituents_paths): self.graph = Graph(uri, auth=(username, password)) self.llm = llm self.entities_list = entities_list self.constituents_dict = constituents_dict self.constituents_paths = constituents_paths def _call(self, question, progress_callback=None): result_dict = {} similar_name_dict = {} for entity in self.entities_list: entity_name, entity_type = entity if entity_type == "Disease": result_dict[entity_name] = query_predicted_disease_info(entity_name, question) similar_name_dict[entity_name] = list(result_dict[entity_name].keys()) elif entity_type == "Drug" or "Food" or "Metabolite": result_dict[entity_name] = query_predicted_drug_info(entity_name, question) similar_name_dict[entity_name] = list(result_dict[entity_name].keys()) elif entity_type == "Gene": result_dict[entity_name] = query_predicted_target_info(entity_name, question) similar_name_dict[entity_name] = list(result_dict[entity_name].keys()) if entity in self.constituents_dict and self.constituents_dict[entity]: constituents = self.constituents_dict[entity] constituents = [constituent for constituent in constituents if constituent != 'None'] if constituents and 'None' not in constituents: for constituent in constituents: constituent_name, constituent_type = constituent if constituent_type == "Disease": result_dict[constituent_name] = query_predicted_disease_info(constituent_name, question) similar_name_dict[constituent_name] = list(result_dict[constituent_name].keys()) elif constituent_type == "Drug" or "Food" or "Metabolite": result_dict[constituent_name] = query_predicted_drug_info(constituent_name, question) similar_name_dict[constituent_name] = list(result_dict[constituent_name].keys()) elif constituent_type == "Gene": result_dict[constituent_name] = query_predicted_target_info(constituent_name, question) similar_name_dict[constituent_name] = list(result_dict[constituent_name].keys()) max_length = max([len(similar_names) for similar_names in similar_name_dict.values()]) for i in range(max_length): start_paths = [] start_nodes = [] start_graph_rels = [] for entity_name in self.entities_list: if i < len(similar_name_dict[entity_name]): name = similar_name_dict[entity_name][i] result = result_dict[entity_name][name] if result is not None: PredPaths = result['paths'] PredNodes = result['nodes'] PredRels = result['rels'] start_paths.extend(PredPaths) start_nodes.extend(PredNodes) start_graph_rels.extend(PredRels) else: continue print("start_paths:", len(start_paths)) print("start_nodes:", len(start_nodes)) print("start_graph_rels:", len(start_graph_rels)) query_nodes = list(set(start_nodes)) print("query nodes") print(len(query_nodes)) print(query_nodes) mid_direct_paths = set() mid_direct_nodes = set() mid_direct_graph_rels = set() node_labels = get_node_labels_dict(self.graph, query_nodes) for node in query_nodes: node_label = node_labels.get(node) if node_label is not None: paths = query_direct(self.graph, node, node_label) if paths: (selected_paths, selected_nodes, selected_graph_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) mid_direct_paths.update(selected_paths) mid_direct_nodes.update(selected_nodes) mid_direct_graph_rels.update(selected_graph_rels) print("success") print(len(selected_paths)) print(selected_paths) del paths, selected_paths, selected_nodes, selected_graph_rels gc.collect() else: print("skipping") continue print("number of unique inter_direct_relationships:") print(len(mid_direct_paths)) mid_inter_paths = query_between_direct(self.graph, list(mid_direct_nodes), query_nodes) n_cluster = max(len(mid_inter_paths)//10, 1) if n_cluster > 30: n_embed = 30 else: n_embed = n_cluster print("n_cluster:", n_cluster) (selected_mid_inter_paths, selected_mid_inter_nodes, selected_mid_inter_graph_rels) = select_paths(mid_inter_paths, question, n_cluster, n_embed, progress_callback) print("final_inter_direct_inter_relationships") print(len(selected_mid_inter_paths)) all_graph_rels = set() all_graph_rels.update(selected_mid_inter_graph_rels) all_graph_rels.update(mid_direct_graph_rels) all_graph_rels.update(start_graph_rels) all_graph_rels = list(all_graph_rels) print("all_graph_rels") print(len(all_graph_rels)) ######################################################################################################## params = { "llm": self.llm, "entities_list": self.entities_list, "question": question, "start_paths": start_paths, "mid_paths": list(mid_direct_paths), "inter_direct_inter": list(selected_mid_inter_paths), } final_context = generate_answer(**params) answer = final_context response = {"result": answer, "all_rels": all_graph_rels} yield response
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~OPC_GraphQA_Sim.py
from __future__ import annotations from typing import Any, Dict, List, Optional from langchain.chains.llm import LLMChain from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from py2neo import Graph import numpy as np from CustomLibrary.Graph_Queries import ( query_direct, query_direct_constituents, query_between_direct, get_node_labels_dict, get_node_label ) from CustomLibrary.Graph_Utils import ( select_paths ) from langchain.prompts import PromptTemplate import gc from CustomLibrary.OPC_Utils import pubchem_query, similar_pubchem_query from CustomLibrary.Custom_Prompts import Graph_Answer_Gen_Template_alpaca def generate_answer(llm, entities_list, question, start_paths, mid_paths, inter_direct_inter): prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) #prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) gen_chain = LLMChain(llm=llm, prompt=prompt) start_paths = ','.join(start_paths) Inter_relationships = mid_paths + inter_direct_inter Inter_sentences = ','.join(Inter_relationships) sep1 = f"Starting paths from {entities_list}:" sep2 = f"Intermediate paths of {entities_list}:" sentences = '\n'.join([sep1, start_paths, sep2, Inter_sentences]) answer = gen_chain.run(input=sentences, question=question) print(answer) return answer class OPC_GraphQA_Sim: def __init__(self, uri, username, password, llm, entities_list, constituents_dict, constituents_paths): self.graph = Graph(uri, auth=(username, password)) self.llm = llm self.entities_list = entities_list self.constituents_dict = constituents_dict self.constituents_paths = constituents_paths def _call(self, question, progress_callback=None): start_paths = [] start_nodes = [] start_graph_rels = [] for entity in self.entities_list: entity_label, entity_name = get_node_label(self.graph, entity) paths = query_direct(self.graph, entity_name, entity_label) if paths: (CKG_paths, CKG_nodes, CKG_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) start_paths.extend(CKG_paths) start_nodes.extend(CKG_nodes) start_graph_rels.extend(CKG_rels) if entity in self.constituents_dict and self.constituents_dict[entity]: constituents = self.constituents_dict[entity] constituents = [constituent for constituent in constituents if constituent != 'None'] if constituents and 'None' not in constituents: for constituent in constituents: constituent_label, constituent_name = get_node_label(self.graph, constituent) paths = query_direct_constituents(self.graph, constituent_name, constituent_label) if paths: (Constituent_CKG_paths, Constituent_CKG_nodes, Constituent_CKG_rels) = select_paths(entity, paths, question, len(paths)//3, 3, progress_callback) start_paths.extend(Constituent_CKG_paths) start_nodes.extend(Constituent_CKG_nodes) start_graph_rels.extend(Constituent_CKG_rels) pubchem_result = pubchem_query(entity, constituent, question, progress_callback) if pubchem_result: (pubchem_paths, pubchem_nodes, pubchem_rels) = pubchem_result start_paths.extend(pubchem_paths) start_nodes.extend(pubchem_nodes) start_graph_rels.extend(pubchem_rels) similar_pubchem_result = similar_pubchem_query(entity, constituent, question, progress_callback) if similar_pubchem_result: (similar_pubchem_paths, similar_pubchem_nodes, similar_pubchem_rels) = similar_pubchem_result start_paths.extend(similar_pubchem_paths) start_nodes.extend(similar_pubchem_nodes) start_graph_rels.extend(similar_pubchem_rels) else: continue else: continue if len(start_paths) == 0: return None print("start_paths:", len(start_paths)) print("start_nodes:", len(start_nodes)) print("start_graph_rels:", len(start_graph_rels)) query_nodes = list(set(start_nodes)) print("query nodes") print(len(query_nodes)) print(query_nodes) mid_direct_paths = set() mid_direct_nodes = set() mid_direct_graph_rels = set() node_labels = get_node_labels_dict(self.graph, query_nodes) for node in query_nodes: node_label = node_labels.get(node) if node_label is not None: paths = query_direct(self.graph, node, node_label) if paths: (selected_paths, selected_nodes, selected_graph_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) mid_direct_paths.update(selected_paths) mid_direct_nodes.update(selected_nodes) mid_direct_graph_rels.update(selected_graph_rels) print("success") print(len(selected_paths)) print(selected_paths) del paths, selected_paths, selected_nodes, selected_graph_rels gc.collect() else: print("skipping") continue print("number of unique inter_direct_relationships:") print(len(mid_direct_paths)) mid_inter_paths = query_between_direct(self.graph, list(mid_direct_nodes), query_nodes) n_cluster = max(len(mid_inter_paths)//10, 1) if n_cluster > 30: n_embed = 30 else: n_embed = n_cluster print("n_cluster:", n_cluster) (selected_mid_inter_paths, selected_mid_inter_nodes, selected_mid_inter_graph_rels) = select_paths(mid_inter_paths, question, n_cluster, n_embed, progress_callback) print("final_inter_direct_inter_relationships") print(len(selected_mid_inter_paths)) all_graph_rels = set() all_graph_rels.update(selected_mid_inter_graph_rels) all_graph_rels.update(mid_direct_graph_rels) all_graph_rels.update(start_graph_rels) all_graph_rels = list(all_graph_rels) print("all_graph_rels") print(len(all_graph_rels)) ######################################################################################################## params = { "llm": self.llm, "entities_list": self.entities_list, "question": question, "start_paths": start_paths, "mid_paths": list(mid_direct_paths), "inter_direct_inter": list(selected_mid_inter_paths), } final_context = generate_answer(**params) answer = final_context response = {"result": answer, "all_rels": all_graph_rels} return response
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
CustomLibrary~Pharos_Graph_QA.py
from __future__ import annotations from typing import Any, Dict, List, Optional from langchain.chains.llm import LLMChain from langchain.chains import LLMChain from langchain.embeddings import HuggingFaceEmbeddings from py2neo import Graph import numpy as np from langchain.prompts import PromptTemplate import gc from CustomLibrary.Graph_Queries import ( query_direct, query_between_direct, get_node_label, get_node_labels_dict ) from CustomLibrary.Graph_Utils import ( select_paths, select_paths_pharos ) from CustomLibrary.Custom_Prompts import Graph_Answer_Gen_Template_alpaca from CustomLibrary.Pharos_Queries import ( ligand_query, target_query, disease_query ) def generate_answer(llm, entities_list, question, start_paths, mid_paths, inter_direct_inter): prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) #prompt = PromptTemplate(template=Graph_Answer_Gen_Template_alpaca, input_variables=["input", "question"]) gen_chain = LLMChain(llm=llm, prompt=prompt) start_paths = ','.join(start_paths) Inter_relationships = mid_paths + inter_direct_inter Inter_sentences = ','.join(Inter_relationships) sep1 = f"Starting paths from {entities_list}:" sep2 = f"Intermediate paths of {entities_list}:" sentences = '\n'.join([sep1, start_paths, sep2, Inter_sentences]) answer = gen_chain.run(input=sentences, question=question) print(answer) return answer class PharosGraphQA: def __init__(self, uri, username, password, llm, entities_list, constituents_dict, constituents_paths): self.graph = Graph(uri, auth=(username, password)) self.llm = llm self.entities_list = entities_list self.constituents_dict = constituents_dict self.constituents_paths = constituents_paths def _call(self, question, progress_callback=None): start_paths = [] start_nodes = [] start_graph_rels = [] for entity in self.entities_list: entity_name, entity_type = entity if entity_type == "Disease": result = disease_query(entity_name, question) elif entity_type == "Drug" or "Food" or "Metabolite": result = ligand_query(entity_name, question) elif entity_type == "Gene": result = target_query(entity_name, question) if result is not None: (Pharos_Paths, Pharos_Nodes, Pharos_Rels) = result start_paths.extend(Pharos_Paths) start_nodes.extend(Pharos_Nodes) start_graph_rels.extend(Pharos_Rels) if entity in self.constituents_dict and self.constituents_dict[entity]: constituents = self.constituents_dict[entity] constituents = [constituent for constituent in constituents if constituent != 'None'] if constituents and 'None' not in constituents: for constituent in constituents: constituent_name, constituent_type = constituent if constituent_type == "Disease": result = disease_query(constituent_name, question) elif constituent_type == "Drug" or "Food" or "Metabolite": result = ligand_query(constituent_name, question) elif constituent_type == "Gene": result = target_query(constituent_name, question) if result is not None: (Constituent_Pharos_Paths, Constituent_Pharos_Nodes, Constituent_Pharos_Rels) = result start_paths.extend(Constituent_Pharos_Paths) start_nodes.extend(Constituent_Pharos_Nodes) start_graph_rels.extend(Constituent_Pharos_Rels) else: continue print("start_paths:", len(start_paths)) print("start_nodes:", len(start_nodes)) print("start_graph_rels:", len(start_graph_rels)) query_nodes = list(set(start_nodes)) print("query nodes") print(len(query_nodes)) print(query_nodes) mid_direct_paths = set() mid_direct_nodes = set() mid_direct_graph_rels = set() node_labels = get_node_labels_dict(self.graph, query_nodes) for node in query_nodes: node_label = node_labels.get(node) if node_label is not None: paths = query_direct(self.graph, node, node_label) if paths: (selected_paths, selected_nodes, selected_graph_rels) = select_paths(paths, question, len(paths)//3, 3, progress_callback) mid_direct_paths.update(selected_paths) mid_direct_nodes.update(selected_nodes) mid_direct_graph_rels.update(selected_graph_rels) print("success") print(len(selected_paths)) print(selected_paths) del paths, selected_paths, selected_nodes, selected_graph_rels gc.collect() else: print("skipping") continue print("number of unique inter_direct_relationships:") print(len(mid_direct_paths)) mid_inter_paths = query_between_direct(self.graph, list(mid_direct_nodes), query_nodes) n_cluster = max(len(mid_inter_paths)//10, 1) if n_cluster > 30: n_embed = 30 else: n_embed = n_cluster print("n_cluster:", n_cluster) (selected_mid_inter_paths, selected_mid_inter_nodes, selected_mid_inter_graph_rels) = select_paths(mid_inter_paths, question, n_cluster, n_embed, progress_callback) print("final_inter_direct_inter_relationships") print(len(selected_mid_inter_paths)) all_graph_rels = set() all_graph_rels.update(selected_mid_inter_graph_rels) all_graph_rels.update(mid_direct_graph_rels) all_graph_rels.update(start_graph_rels) all_graph_rels = list(all_graph_rels) print("all_graph_rels") print(len(all_graph_rels)) ######################################################################################################## params = { "llm": self.llm, "entities_list": self.entities_list, "question": question, "start_paths": start_paths, "mid_paths": list(mid_direct_paths), "inter_direct_inter": list(selected_mid_inter_paths), } final_context = generate_answer(**params) answer = final_context response = {"result": answer, "all_rels": all_graph_rels} return response
[ "question", "input" ]
2024-01-10
choung0124/OPC_AI
opc_app.py
import streamlit as st from transformers import logging from langchain.llms import TextGen from langchain.prompts import PromptTemplate from langchain import LLMChain import streamlit as st from pyvis.network import Network from CustomLibrary.Custom_Chains import CustomLLMChain, CustomLLMChainAdditionalEntities from CustomLibrary.Custom_Prompts import ( OPC_Entity_type_Template, OPC_Entity_Extraction_Template, Final_Answer_Template_Alpaca ) from CustomLibrary.App_Utils import( get_umls_info, extract_entities, get_names_list, get_names_list, get_entity_types, get_additional_entity_umls_dict, create_and_display_network, parse_relationships_pyvis, create_docs_from_results ) from itertools import combinations, product from langchain.embeddings import HuggingFaceEmbeddings from CustomLibrary.OPC_GraphQA import OPC_GraphQA from chromadb.utils import embedding_functions import chromadb import ast import re # What kind of interactions could there be between the three drugs: lamotrigine, aripiprazole and methylphenidate? # Could there be a synergistic interaction between the three drugs: lamotrigine, aripiprazole and methylphenidate for bipolar disorder? # could there be a synergistic interaction between peanut sprouts and ashwagandha? # Could there be a synergistic interaction between sildenafil and ashwagandha to treat alzheimer's? #Withaferin A, Withanolide A, Withanolide B, Withanolide C, Withanolide D, Withanone, Withanoside IV, Withanoside V # Flavonoid, Resveratrol, Polyphenol, Aspartic acid # Withaferin A, Withanone, Withanoside IV logging.set_verbosity(logging.CRITICAL) sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction( model_name="pritamdeka/S-Bluebert-snli-multinli-stsb", device="cuda", normalize_embeddings=True ) @st.cache_data() def initialize_models(): model_url = "https://enjoy-brought-waters-educational.trycloudflare.com/" local_model_url = "http://127.0.0.1:5000/" llm = TextGen(model_url=model_url, max_new_tokens=2048) local_llm = TextGen(model_url=local_model_url, max_new_tokens=2048) return llm, local_llm @st.cache_data() def initialize_knowledge_graph(): uri = "neo4j://localhost:7687" username = "neo4j" password = "NeO4J" return uri, username, password class SessionState(object): def __init__(self, **kwargs): for key, val in kwargs.items(): setattr(self, key, val) def get_state(**kwargs): if 'state' not in st.session_state: st.session_state['state'] = SessionState(**kwargs) return st.session_state['state'] # Define the progress bar progress_bar = st.empty() # Define the callback function to update the progress bar def progress_callback(progress): progress_bar.progress(progress) def initialize_all(state): if not hasattr(state, 'initialized'): state.llm, state.local_llm = initialize_models() state.uri, state.username, state.password = initialize_knowledge_graph() OPC_Entity_Extraction_Prompt = PromptTemplate(template=OPC_Entity_Extraction_Template, input_variables=["input"]) state.OPC_Entity_Extraction_Chain = CustomLLMChain(prompt=OPC_Entity_Extraction_Prompt, llm=state.llm, output_key="output") Entity_type_prompt = PromptTemplate(template=OPC_Entity_type_Template, input_variables=["input"]) state.Entity_type_chain = LLMChain(prompt=Entity_type_prompt, llm=state.llm) state.initialized = True # Get the state state = get_state(user_options=[]) # Initialize all initialize_all(state) question = st.text_input("Enter your question") # initialize your counter if 'counter' not in st.session_state: st.session_state.counter = 0 # initialize the last processed question if 'last_question' not in st.session_state: st.session_state.last_question = None # initialize the entities_list if 'entities_list' not in st.session_state: st.session_state.entities_list = [] # initialize the constituents_dict if 'constituents_dict' not in st.session_state: st.session_state.constituents_dict = {} if 'paths_list' not in st.session_state: st.session_state.paths_list = [] if 'names_list' not in st.session_state: st.session_state.names_list = [] if 'form_submitted' not in st.session_state: st.session_state.form_submitted = False # if a new question is entered, process it if question and question != st.session_state.last_question: with st.spinner("Processing..."): # Entity extraction entities = state.OPC_Entity_Extraction_Chain.run(question) entities_umls_ids = get_umls_info(entities) names_list = get_names_list(entities_umls_ids) entity_types = get_entity_types(state.Entity_type_chain, names_list) print("names list", names_list) # get the list of entities st.session_state.entities_list = list(entity_types.items()) # store the processed question st.session_state.last_question = question # reset the counter for the new question st.session_state.counter = 0 else: # if there's no new question, use the entities from the last question entities_list = st.session_state.entities_list if st.session_state.counter < len(st.session_state.entities_list): entity, entity_type = st.session_state.entities_list[st.session_state.counter] if entity_type in ["Food", "Metabolite", "Drug"]: with st.form(key=str(entity)): st.write("Please input the chemical constituents of:", entity, "(Please separate each constituent with a comma)") entity_constituents = st.text_input(f"Input the chemical constituents of {entity}:", key=str(entity)) none_checkbox = st.checkbox("None", key=str(entity).join("_NoneCheckbox")) # Move none_checkbox inside the form submitted = st.form_submit_button('Submit') if submitted: if none_checkbox: st.session_state.constituents_dict[entity] = [entity] st.session_state.counter += 1 else: constituents = entity_constituents.split(",") if entity_constituents else [] # Only add entity to constituents_dict if constituents is not empty and contains non-empty strings if constituents and any(constituent.strip() for constituent in constituents): st.session_state.constituents_dict[entity] = constituents for constituent in constituents: path = { 'nodes': [entity, constituent], 'relationships': ['contains constituent'] } st.session_state.paths_list.append(path) st.session_state.counter += 1 print(st.session_state.constituents_dict) # Debug print statement else: st.session_state.counter += 1 if st.session_state.counter == len(st.session_state.entities_list): st.write("All entities processed") nodes_list = [] paths = st.session_state.paths_list for entity, entity_type in st.session_state.entities_list: if entity_type in ["Food", "Metabolite", "Drug"]: if entity in st.session_state.constituents_dict: constituents = st.session_state.constituents_dict[entity] nodes_list.extend([entity, *constituents]) else: constituents = [] with st.expander(f"Constituents of {entity}"): st.write(constituents) if st.button("Run GraphQA"): with st.spinner("Running GraphQA..."): # Assuming 'entities_list' is a list of all entities entities_list = st.session_state.entities_list # Dictionary to hold combinations for each entity entity_combinations = {} for entity, entity_type in entities_list: if entity in st.session_state.constituents_dict and st.session_state.constituents_dict [entity]: # Get the constituents for the current entity constituents = st.session_state.constituents_dict[entity] if len(constituents) > 2: # Generate all combinations of 4 constituents combinations_of_constituents = list(combinations(constituents, 1)) else: # If there are 4 or fewer constituents, use them directly combinations_of_constituents = [constituents] # Store the combinations in the dictionary entity_combinations[entity] = combinations_of_constituents else: entity_combinations[entity] = [[entity]] # Generate all combinations of combinations for each entity all_combinations = list(product(*entity_combinations.values())) st.write("All combinations:") st.write(len(all_combinations)) counter = 0 persist_directory = ("/mnt/c/aribio/OPC_BRAIN/vectordbs") chromadb_client = chromadb.PersistentClient(path=persist_directory) name = question[:50] # Truncate the string to the first 50 characters # Ensure the truncated string starts and ends with an alphanumeric character name = ''.join(e for e in name if e.isalnum() or e in ['-', '_']) # Replace two consecutive periods with a single period name = name.replace('..', '.') # Check if the string is a valid IPv4 address, if so, append a character to make it invalid if re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', name): name += 'a' vectordb = chromadb_client.create_collection(name=name, embedding_function=sentence_transformer_ef, get_or_create=True) for combo in all_combinations: print("entity_combinations") print(entity_combinations.keys()) print(entity_combinations) counter += 1 answer_count = vectordb.count() answer_id = str(answer_count + 1) # 'combo' is a tuple of combinations, one for each entity # Convert it to a dictionary constituents_dict = {} for i, entity in enumerate(entity_combinations.keys()): if entity in st.session_state.constituents_dict and st.session_state.constituents_dict[entity]: constituents_dict[entity] = list(combo[i]) # Run OPC GraphQA for each combination KG = OPC_GraphQA(uri=state.uri, username=state.username, password=state.password, llm=state.local_llm, entities_list=list(entity_combinations.keys()), # All entities constituents_dict=constituents_dict, # The current combination of constituents constituents_paths=st.session_state.paths_list) graph_query = KG._call(question, progress_callback=progress_callback) if graph_query is not None and graph_query['result'] is not None: answer = graph_query['result'] all_rels = graph_query['all_rels'] vectordb.add(ids=[answer_id], documents=[answer], metadatas=[{"Entities" : str(list(entity_combinations.keys())), "Graph_rels": str(all_rels)}]) else: continue st.header("Final Answer:") results = vectordb.query(query_texts=[question], n_results=4) result_dict_list = create_docs_from_results(results) selected_answers = [] for result in result_dict_list: answer = result["document"] selected_answers.append(answer) Final_chain_prompt = PromptTemplate(template=Final_Answer_Template_Alpaca, input_variables=["input", "question"]) Final_chain = LLMChain(llm=state.local_llm, prompt=Final_chain_prompt) final_answer = Final_chain.run(input=selected_answers, question=question) st.write(final_answer) st.header("Evidence:") for result in result_dict_list: answer = result["document"] metadata = result["metadata"] entities_list = metadata["Entities"] graph_rels_str = metadata["Graph_rels"] graph_rels = ast.literal_eval(graph_rels_str) st.write(f"Answer from this combination of entities: {entities_list}") st.write("Answer:") st.write(answer) nodes, edges = parse_relationships_pyvis(graph_rels) create_and_display_network(nodes, edges, '#fff6fe', "Graph", entities_list[0], entities_list[1])
[ "question", "input" ]
2024-01-10
0oeaaeo/Sam
sam~slack.py
import json import logging import time import urllib.request from typing import Any from openai import OpenAI from slack_bolt import App, Say from . import config, utils logger = logging.getLogger("sam") client = OpenAI() app = App(token=config.SLACK_BOT_TOKEN) USER_HANDLE = None AUDIO_FORMATS = ["mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm"] def handle_message(event: {str, Any}, say: Say): logger.debug(f"handle_message={json.dumps(event)}") global USER_HANDLE if USER_HANDLE is None: logger.debug("Fetching the bot's user id") response = say.client.auth_test() USER_HANDLE = response["user_id"] channel_id = event["channel"] client_msg_id = event["client_msg_id"] channel_type = event["channel_type"] user_id = event["user"] text = event["text"] text = text.replace(f"<@{USER_HANDLE}>", "Sam") thread_id = utils.get_thread_id(channel_id) file_ids = [] voice_prompt = False if "files" in event: for file in event["files"]: req = urllib.request.Request( file["url_private"], headers={"Authorization": f"Bearer {config.SLACK_BOT_TOKEN}"}, ) with urllib.request.urlopen(req) as response: # nosec if file["filetype"] in AUDIO_FORMATS: text += "\n" + client.audio.transcriptions.create( model="whisper-1", file=(file["name"], response.read()), response_format="text", ) logger.info(f"User={user_id} added Audio={file['id']}") voice_prompt = True else: file_ids.append( client.files.create( file=(file["name"], response.read()), purpose="assistants" ).id ) logger.info( f"User={user_id} added File={file_ids[-1]} to Thread={thread_id}" ) client.beta.threads.messages.create( thread_id=thread_id, content=text, role="user", file_ids=file_ids, ) logger.info( f"User={user_id} added Message={client_msg_id} added to Thread={thread_id}" ) if channel_type == "im" or event.get("parent_user_id") == USER_HANDLE: process_run(event, say, voice_prompt=voice_prompt) def process_run(event: {str, Any}, say: Say, voice_prompt: bool = False): logger.debug(f"process_run={json.dumps(event)}") channel_id = event["channel"] user_id = event["user"] thread_ts = event.get("thread_ts") thread_id = utils.get_thread_id(channel_id) run = client.beta.threads.runs.create( thread_id=thread_id, assistant_id=config.OPENAI_ASSISTANT_ID, ) msg = say(f":speech_balloon:", mrkdwn=True, thread_ts=thread_ts) logger.info(f"User={user_id} started Run={run.id} for Thread={thread_id}") for i in range(14): # ~ 10 minutes if run.status not in ["queued", "in_progress"]: break time.sleep(min(2**i, 60)) # exponential backoff capped at 60 seconds run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id) if run.status == "failed": logger.error(run.last_error) say.client.chat_update( channel=say.channel, ts=msg["ts"], text=f"🤖 {run.last_error.message}", mrkdwn=True, ) logger.error(f"Run {run.id} {run.status} for Thread {thread_id}") logger.error(run.last_error.message) return elif run.status != "completed": logger.error(f"Run={run.id} {run.status} for Thread {thread_id}") say.client.chat_update( channel=say.channel, ts=msg["ts"], text=f"🤯", mrkdwn=True, ) return logger.info(f"Run={run.id} {run.status} for Thread={thread_id}") messages = client.beta.threads.messages.list(thread_id=thread_id) for message in messages: if message.role == "assistant": message_content = message.content[0].text if voice_prompt: response = client.audio.speech.create( model="tts-1-hd", voice="alloy", input=message_content.value, ) say.client.files_upload( content=response.read(), channels=say.channel, ts=msg["ts"], ) logger.info( f"Sam responded to the User={user_id} in Channel={channel_id} via Voice" ) else: annotations = message_content.annotations citations = [] # Iterate over the annotations and add footnotes for index, annotation in enumerate(annotations): message_content.value = message_content.value.replace( annotation.text, f" [{index}]" ) if file_citation := getattr(annotation, "file_citation", None): cited_file = client.files.retrieve(file_citation.file_id) citations.append( f"[{index}] {file_citation.quote} — {cited_file.filename}" ) elif file_path := getattr(annotation, "file_path", None): cited_file = client.files.retrieve(file_path.file_id) citations.append(f"[{index}]({cited_file.filename})") # Add footnotes to the end of the message before displaying to user message_content.value += "\n" + "\n".join(citations) say.client.chat_update( channel=say.channel, ts=msg["ts"], text=message_content.value, mrkdwn=True, ) logger.info( f"Sam responded to the User={user_id} in Channel={channel_id} via Text" ) break app.event("message")(handle_message) app.event("app_mention")(process_run)
[ "False", "True" ]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~memory~short_term_memory.py
from langchain.memory import ChatMessageHistory class ChatShortMemory(ChatMessageHistory): def window_buffer_message(self, round: int): if len(self.messages) < round * 2: return self.messages else: return self.messages[len(self.messages) - round * 2:] short_memory = ChatShortMemory()
[]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~actions~interact.py
from actions.IoT_control import mqtt_publish from langchain.agents import Tool from api_key import * from tools.system_control import press_key_wake_up import memory def light_handle(instruction): print("少女行动中...") if "on" in instruction: message = {"switch": "light on"} elif "off" in instruction: message = {"switch": "light off"} mqtt_publish(message) def end_talk(_): print("结束对话捏...") memory.long_memory.summary_write(memory.short_memory) memory.long_memory.short_memory_vector_write(memory.short_memory) press_key_wake_up() def just_chat(_): return "chat" # 操作工具列表 interact_tools = [ Tool( name = "Light Handle", func=light_handle, description="Use this to control the light, input 'on' to turn on the light, and input 'off' to turn off the light.", return_direct=True ), Tool( name = "end conversation", func=end_talk, description="If you think it's time to end conversation, use this.", return_direct=True ), Tool( name = "Chat", func=just_chat, description="If you think I'm not asking a question or you don't need to use other tools or i'm instruct you to do something, take this", return_direct=True ) ]
[]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~perception~auditory.py
from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.asr.v20190614 import asr_client, models from api_key import * import pyaudio import webrtcvad import io import wave import base64 import asyncio import json import openai from io import BytesIO import openai import tempfile from pydub import AudioSegment # 录音,返回base64编码的WAV格式音频 def sound_record(): # 设置录音参数 FORMAT = pyaudio.paInt16 CHANNELS = 1 FRAME_DURATION_MS = 30 RATE = 48000 FRAME_SIZE = int(RATE * FRAME_DURATION_MS / 1000) RECORD_SECONDS = 8 # 最多可录音几秒 SILENCE_DURATION = 1 # 说完后几秒停止录音 # 初始化pyaudio,webrtcvad vad = webrtcvad.Vad(3) audio = pyaudio.PyAudio() # 开启录音流 stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=FRAME_SIZE) print("开始录音喵...") # 将录音记录到帧 SILENCE_CHUNKS = int(SILENCE_DURATION * RATE / FRAME_SIZE) frames = [] silence_count = 0 first_entry = True filter_count = 0 # 用于滤除声音余留 for _ in range(0, int(RATE / FRAME_SIZE * RECORD_SECONDS)): data = stream.read(FRAME_SIZE) frames.append(data) filter_count += 1 if first_entry and filter_count > 11: if vad.is_speech(data, RATE): first_entry = False else: if vad.is_speech(data, RATE): silence_count = 0 else: silence_count += 1 if silence_count >= SILENCE_CHUNKS: break print("结束录音了捏") # 结束相关事件 stream.stop_stream() stream.close() audio.terminate() # 将数据帧编码为base64编码的WAV格式 with io.BytesIO() as wav_buffer: with wave.open(wav_buffer, 'wb') as wf: wf.setnchannels(CHANNELS) wf.setsampwidth(audio.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wav_base64 = base64.b64encode( wav_buffer.getvalue()).decode('utf-8') return wav_base64 # openai whisper asr,不推荐使用,延迟太大,但是支持多语言(这个模型可进行本地部署,以后有空弄) def whisper_asr(wav_base64): openai.api_key = openai_key audio_data_bytes = base64.b64decode(wav_base64) audio_data = AudioSegment.from_file(BytesIO(audio_data_bytes), format="wav") with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file: audio_data.export(temp_file.name, format="wav") transcript = openai.Audio.transcribe("whisper-1", temp_file) os.remove(temp_file.name) return transcript['text'] # 腾讯云asr,输入base64编码的wav音频,输出text,此函数需异步调用,以节约请求事件 async def tencent_asr(wav_base64): cred = credential.Credential(tencent_Id, tencent_key) # 实例化一个http选项,可选的,没有特殊需求可以跳过 httpProfile = HttpProfile() httpProfile.endpoint = "asr.tencentcloudapi.com" # 实例化一个client选项,可选的,没有特殊需求可以跳过 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品的client对象,clientProfile是可选的 client = asr_client.AsrClient(cred, "", clientProfile) # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.SentenceRecognitionRequest() params = { "ProjectId": 0, "SubServiceType": 2, "EngSerViceType": "16k_zh", "SourceType": 1, "VoiceFormat": "wav", "UsrAudioKey": "0", "Data": wav_base64, # 音频二进制数据 "DataLen": len(wav_base64) # 音频长度 } req.from_json_string(json.dumps(params)) response = await asyncio.to_thread(client.SentenceRecognition, req) if response.Result == "": print("你什么都没说~") else: print("你:" + response.Result) return response.Result def listen(model:str="tencent"): audio_data = sound_record() if model == "tencent": user_words = asyncio.run(tencent_asr(audio_data)) return user_words elif model == "whisper": user_words = whisper_asr(audio_data) return user_words
[]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~thinking~agent_search.py
from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate from langchain import OpenAI, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish import re from actions.search import search_tools import time from thinking.prompts import AGENT_SEARCH_PROMPTS_TEMPLATE , AGENT0_SEARCH_ZERO_SHOT , AGENT0_SEARCH_LABEL from api_key import debug_mode , formatted_address , clueai_api import clueai # 设置agent的prompts的模板类 class CustomPromptTemplate(StringPromptTemplate): # 使用的template文本模板 template: str # 可使用的工具 tools: List[Tool] def format(self, **kwargs) -> str: # 获取当前时间 current_time = time.time() local_time = time.localtime(current_time) formatted_time = time.strftime("%Y-%m-%d", local_time) # 获取中间步骤 (AgentAction, Observation tuples) # 将模板格式化为常规形式,即带入变量 intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " kwargs["agent_scratchpad"] = thoughts kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools]) kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools]) kwargs["time"] = formatted_time kwargs["location"] = formatted_address return self.template.format(**kwargs) tools = search_tools prompt = CustomPromptTemplate( template=AGENT_SEARCH_PROMPTS_TEMPLATE, tools=tools, # 这里不用带入agent_scratchpad`,`tools`和`tool_names`三个变量,因为在上面format方法中已经带入了 # 添加可带入的prompts变量 input_variables=["input", "intermediate_steps"] ) # agent输出解析,一般情况下用不到 class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # 查看agent是否该结束 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, ) # 解析action和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) # 返回action和action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() llm = OpenAI(temperature=0) # 由LLM模型和prompt构成llm_chain llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] # 由llm_chain和tools构成agent agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=debug_mode) # 初始化clueai实例 cl = clueai.Client(clueai_api) def agent_search(user_words): response = cl.classify( model_name='clueai-large', task_name='用户意图领域', inputs=[user_words], examples=AGENT0_SEARCH_ZERO_SHOT, labels =AGENT0_SEARCH_LABEL) if response.classifications[0].prediction == AGENT0_SEARCH_LABEL[0]: return "chat" elif response.classifications[0].prediction == AGENT0_SEARCH_LABEL[1]: return agent_executor.run(user_words)
[ "input", "intermediate_steps" ]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~memory~long_term_memory.py
import pinecone import openai from api_key import openai_key , pinecone_key , ai_name , user_name , pinecone_env , pinecone_index from langchain.memory.summary import SummarizerMixin from langchain.llms import OpenAI from memory.prompts import SUMMARY_PROMPT from typing import Any, Optional from memory.short_term_memory import ChatShortMemory import time class ChatLongMemory(SummarizerMixin): index : Optional[Any] = None summary_memory : str = "" def init(self): openai.api_key = openai_key pinecone.init(api_key=pinecone_key, environment=pinecone_env) self.index = pinecone.Index(pinecone_index) with open("HoshiNoYume\memory\long_summary_memory.txt", "r") as file: self.summary_memory = file.read() def short_memory_vector_write(self,short_memory:ChatShortMemory): # 把短期记忆的对话记录写进向量数据库 for i in range(len(short_memory.messages)//2): written_str = short_memory.messages[2*i].content + "&" + short_memory.messages[2*i+1].content vector = openai.Embedding.create( input=written_str, model="text-embedding-ada-002" ) current_time = time.time() local_time = time.localtime(current_time) formatted_time = time.strftime("%Y%m%d%H%M%S", local_time) self.index.upsert( vectors=[ {'id':formatted_time, 'values':vector['data'][0]['embedding'], 'metadata':{'human': short_memory.messages[i].content, 'ai': short_memory.messages[i+1].content}, } ]) def vector_search(self,text): openai.api_key = openai_key vector = openai.Embedding.create( input=text, model="text-embedding-ada-002" ) response = self.index.query( vector=vector['data'][0]['embedding'], top_k=5, include_values=False, include_metadata=True) return response def summary_write(self,short_memory:ChatShortMemory): messages = short_memory.messages self.summary_memory = self.predict_new_summary(messages,self.summary_memory) with open("HoshiNoYume\memory\long_summary_memory.txt", "w") as file: file.write(self.summary_memory) return self.summary_memory long_memory = ChatLongMemory(llm=OpenAI(temperature=0), ai_prefix=ai_name, human_prefix=user_name, prompt=SUMMARY_PROMPT) long_memory.init()
[]
2024-01-10
xuanxuanQAQ/HoshiNoYume
HoshiNoYume~thinking~agent_interact.py
from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser from langchain.prompts import StringPromptTemplate from langchain import OpenAI, LLMChain from typing import List, Union from langchain.schema import AgentAction, AgentFinish import re from actions.interact import interact_tools import time from thinking.prompts import AGENT_INTERACT_PROMPTS_TEMPLATE from api_key import debug_mode , formatted_address # 设置agent的prompts的模板类 class CustomPromptTemplate(StringPromptTemplate): # 使用的template文本模板 template: str # 可使用的工具 tools: List[Tool] def format(self, **kwargs) -> str: # 获取当前时间 current_time = time.time() local_time = time.localtime(current_time) formatted_time = time.strftime("%Y-%m-%d", local_time) # 获取中间步骤 (AgentAction, Observation tuples) # 将模板格式化为常规形式,即带入变量 intermediate_steps = kwargs.pop("intermediate_steps") thoughts = "" for action, observation in intermediate_steps: thoughts += action.log thoughts += f"\nObservation: {observation}\nThought: " kwargs["agent_scratchpad"] = thoughts kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools]) kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools]) kwargs["time"] = formatted_time kwargs["location"] = formatted_address return self.template.format(**kwargs) tools = interact_tools prompt = CustomPromptTemplate( template=AGENT_INTERACT_PROMPTS_TEMPLATE, tools=tools, # 这里不用带入agent_scratchpad`,`tools`和`tool_names`三个变量,因为在上面format方法中已经带入了 # 添加可带入的prompts变量 input_variables=["input", "intermediate_steps"] ) # agent输出解析,一般情况下用不到 class CustomOutputParser(AgentOutputParser): def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]: # 查看agent是否该结束 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, ) # 解析action和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) # 返回action和action input return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output) output_parser = CustomOutputParser() llm = OpenAI(temperature=0) # 由LLM模型和prompt构成llm_chain llm_chain = LLMChain(llm=llm, prompt=prompt) tool_names = [tool.name for tool in tools] # 由llm_chain和tools构成agent agent = LLMSingleActionAgent( llm_chain=llm_chain, output_parser=output_parser, stop=["\nObservation:"], allowed_tools=tool_names ) agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=debug_mode) def agent_interact(user_words): return agent_executor.run(user_words)
[ "input", "intermediate_steps" ]
2024-01-10
u2508/All-Programs
PYTHON~flaskbot1.py
from flask import Flask, request import openai import json # Set up the Flask app app = Flask(__name__) # Set up the OpenAI API credentials openai.api_key = "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" # Set up the GPT model parameters model_engine = "davinci" # use the most powerful GPT model temperature = 0.7 # controls the "creativity" of the AI's responses max_tokens = 100 # maximum length of each response stop_sequence = "\n" # end the response after the first line break # Create the AI chatbot endpoint @app.route("/chat", methods=["POST"]) def chat(): # Get the user's message from the POST request user_message = request.form.get("message") # Use the OpenAI GPT model to generate a response response = openai.Completion.create( engine=model_engine, prompt=user_message, temperature=temperature, max_tokens=max_tokens, stop=stop_sequence ) # Extract the response text from the OpenAI response object response_text = response.choices[0].text.strip() # Package the response as a JSON object and return it to the user return json.dumps({"response": response_text}) # Start the Flask app if __name__ == "__main__": app.run()
[]
2024-01-10
u2508/All-Programs
PYTHON~ai_chatgpt.py
import openai as ai ai.api_key = "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" def generate_response(prompt): response = ai.Completion.create( engine="davinci", prompt=prompt, max_tokens=1440, n=1, stop=None, temperature=0.6, ) message = response.choices[0].text.strip() return message print("Chatbot: Hi, how can I help you today?") while True: user_input = input("You: ") if user_input.lower() in ["bye", "goodbye"]: print("Chatbot: Goodbye!") break prompt = f"Conversation\nUser: {user_input}\nChatbot:" response = generate_response(prompt) print(f"Chatbot: {response}")
[ "Conversation\nUser: PLACEHOLDER\nChatbot:" ]
2024-01-10
WizKnight/Langchain-Celebrity-Search
example1.py
## Integrating coe with openai API import os from constants import openai_key from langchain.chains import LLMChain from langchain.llms import OpenAI from langchain import PromptTemplate from langchain.chains import LLMChain from langchain.chains import SequentialChain import streamlit as st os.environ["OPENAI_API_KEY"] = openai_key # Streamlit framework st.title('Celebrity Search Results') input_text = st.text_input("Search the topic") # Prompt Templates first_input_prompt = PromptTemplate( input_variables = ['name'], template = "Tell me about {name}" ) ## OPENAI LLM Models # This shows how much control an agent should have while providing you the response. llm = OpenAI(temperature=0.8) chain = LLMChain(llm=llm, prompt=first_input_prompt, verbose=True, output_key='name') # Prompt Templates second_input_prompt = PromptTemplate( input_variables = ['person'], template = "Birth Date of {person}" ) chain2 = LLMChain(llm=llm, prompt=second_input_prompt, verbose=True, output_key='dob') parent_chain = SequentialChain(chains=[chain,chain2], input_variables=['name'],output_variables=['person','dob'],verbose=True) if input_text: st.write(parent_chain.run({'name': input_text}))
[ "Tell me about {name}", "name", "person", "Birth Date of {person}" ]
2024-01-10
PKU-ZLR/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
Brady-Lemaster/blem
cmd~imageGen.py
import openai def x(image, key): openai.api_key = key response = openai.Image.create(prompt=image, n=1, size="256x256") image = response['data'][0]['url'] return image
[]
2024-01-10
Brady-Lemaster/blem
cmd~imageGen2.py
import openai def x(prompt, profile): openai.api_key = profile[2] response = openai.Image.create(prompt=prompt, n=profile[1], size=profile[0]) image = response['data'][0]['url'] return image
[]
2024-01-10
AI-Jie01/zeno-build
zeno_build~models~global_models.py
"""A module for global variables regarding models.""" from __future__ import annotations import cohere cohere_client: cohere.Client | None = None
[]
2024-01-10
AI-Jie01/zeno-build
zeno_build~models~text_generate.py
"""Generate from a textual prompt.""" import asyncio import openai import tqdm from zeno_build.models import global_models, lm_config from zeno_build.prompts.prompt_utils import replace_variables async def generate_from_text_prompt( variables: list[dict[str, str]], prompt_template: str, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, ) -> list[str]: """Generate from a textual prompt. Args: variables: The source set of variables to consume. prompt_template: The template for the prompt. model_config: Configuration of the model. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top p value to use. Returns: The generated text. """ print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}..." ) if model_config.provider == "openai": async_responses = [ openai.Completion.acreate( engine=model_config.model, prompt=replace_variables(prompt_template, vars), temperature=temperature, max_tokens=max_tokens, top_p=top_p, ) for vars in variables ] responses = await asyncio.gather(*async_responses) return [x["choices"][0]["text"] for x in responses] elif model_config.provider == "openai_chat": async_responses = [ openai.ChatCompletion.acreate( model=model_config.model, messages=[ { "role": "user", "content": replace_variables(prompt_template, vars), }, ], temperature=temperature, max_tokens=max_tokens, top_p=top_p, ) for vars in variables ] responses = await asyncio.gather(*async_responses) return [x["choices"][0]["message"]["content"] for x in responses] elif model_config.provider == "cohere": import cohere results = [] for vars in tqdm.tqdm(variables, "Generating synchronously from Cohere"): try: prompt = replace_variables(prompt_template, vars) assert global_models.cohere_client is not None response = global_models.cohere_client.generate( model=model_config.model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, p=top_p, ) results.append(response.generations[0].text) except cohere.CohereAPIError as e: # Cohere API sometimes rejects queries, if so output a blank line print(f"Warning! Cohere API rejected query for {prompt=}: {e.message}") results.append("") return results else: raise ValueError("Unknown model_config.provider, but you can add your own!")
[]
2024-01-10
AI-Jie01/zeno-build
zeno_build~models~chat_generate.py
"""Tools to generate from prompts.""" import asyncio import logging import re from typing import Any import aiolimiter import cohere import openai import openai.error import torch import tqdm import transformers from tqdm.asyncio import tqdm_asyncio from zeno_build.models import global_models, lm_config from zeno_build.prompts import chat_prompt async def _throttled_openai_completion_acreate( engine: str, prompt: str, temperature: float, max_tokens: int, top_p: float, limiter: aiolimiter.AsyncLimiter, ) -> dict[str, Any]: async with limiter: for _ in range(3): try: return await openai.Completion.acreate( engine=engine, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=top_p, ) except openai.error.RateLimitError: logging.warning( "OpenAI API rate limit exceeded. Sleeping for 10 seconds." ) await asyncio.sleep(10) except openai.error.APIError as e: logging.warning(f"OpenAI API error: {e}") break return {"choices": [{"message": {"content": ""}}]} async def _generate_from_openai_completion( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, requests_per_minute: int = 300, ) -> list[str]: limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_openai_completion_acreate( engine=model_config.model, prompt=prompt_template.to_text_prompt( full_context=full_context.limit_length(context_length), name_replacements=model_config.name_replacements, ), temperature=temperature, max_tokens=max_tokens, top_p=top_p, limiter=limiter, ) for full_context in full_contexts ] responses = await tqdm_asyncio.gather(*async_responses) return [x["choices"][0]["text"] for x in responses] async def _throttled_openai_chat_completion_acreate( model: str, messages: list[dict[str, str]], temperature: float, max_tokens: int, top_p: float, limiter: aiolimiter.AsyncLimiter, ) -> dict[str, Any]: async with limiter: for _ in range(3): try: return await openai.ChatCompletion.acreate( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, top_p=top_p, ) except openai.error.RateLimitError: logging.warning( "OpenAI API rate limit exceeded. Sleeping for 10 seconds." ) await asyncio.sleep(10) except asyncio.exceptions.TimeoutError: logging.warning("OpenAI API timeout. Sleeping for 10 seconds.") await asyncio.sleep(10) except openai.error.APIError as e: logging.warning(f"OpenAI API error: {e}") break return {"choices": [{"message": {"content": ""}}]} async def _generate_from_openai_chat_completion( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, requests_per_minute: int = 300, ) -> list[str]: limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_openai_chat_completion_acreate( model=model_config.model, messages=prompt_template.to_openai_chat_completion_messages(full_context), temperature=temperature, max_tokens=max_tokens, top_p=top_p, limiter=limiter, ) for full_context in full_contexts ] responses = await tqdm_asyncio.gather(*async_responses) return [x["choices"][0]["message"]["content"] for x in responses] async def _throttled_cohere_acreate( model: str, prompt: str, temperature: float, max_tokens: int, top_p: float, limiter: aiolimiter.AsyncLimiter, ) -> str: async with limiter: assert global_models.cohere_client is not None try: response = global_models.cohere_client.generate( model=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, p=top_p, ) return response.generations[0].text except cohere.CohereAPIError as e: # Cohere API sometimes rejects queries, if so output a blank line logging.getLogger(__name__).warn( f"Warning! Cohere API rejected query for {prompt=}: {e.message}" ) return "" async def _generate_from_cohere( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, requests_per_minute: int, ) -> list[str]: limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_cohere_acreate( model=model_config.model, prompt=prompt_template.to_text_prompt( full_context=full_context.limit_length(context_length), name_replacements=model_config.name_replacements, ), temperature=temperature, max_tokens=max_tokens, top_p=top_p, limiter=limiter, ) for full_context in full_contexts ] return await tqdm_asyncio.gather(*async_responses) def _generate_from_huggingface( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, ) -> list[str]: # Load model torch_device = "cuda" if torch.cuda.is_available() else "cpu" model_cls = ( model_config.model_cls if model_config.model_cls is not None else transformers.AutoModelForCausalLM ) tokenizer_cls = ( model_config.tokenizer_cls if model_config.tokenizer_cls is not None else transformers.AutoTokenizer ) model: transformers.PreTrainedModel = model_cls.from_pretrained( model_config.model, **model_config.model_loader_kwargs, ).to(torch_device) tokenizer: transformers.PreTrainedTokenizer = tokenizer_cls.from_pretrained( model_config.model ) tokenizer.padding_side = "left" if not tokenizer.pad_token: tokenizer.pad_token = tokenizer.eos_token tokenizer.pad_token_id = tokenizer.eos_token_id gen_config = transformers.GenerationConfig( do_sample=True, temperature=temperature, max_new_tokens=max_tokens, top_p=top_p, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.pad_token_id, ) # Create the prompts filled_prompts: list[str] = [ prompt_template.to_text_prompt( full_context=full_context.limit_length(context_length), name_replacements=model_config.name_replacements, ) for full_context in full_contexts ] # Process in batches results = [] batch_size = 8 for i in tqdm.trange(0, len(filled_prompts), batch_size): batch_prompts = filled_prompts[i : i + batch_size] encoded_prompts = tokenizer( batch_prompts, padding=True, return_tensors="pt", return_token_type_ids=False, ).to(torch_device) with torch.no_grad(): outputs = model.generate(**encoded_prompts, generation_config=gen_config) outputs = outputs[:, encoded_prompts["input_ids"].shape[-1] :] results.extend(tokenizer.batch_decode(outputs, skip_special_tokens=True)) # Post-processing to get only the system utterance results = [re.split("\n\n", x)[0].strip() for x in results] return results def generate_from_chat_prompt( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, requests_per_minute: int = 50, ) -> list[str]: """Generate from a list of chat-style prompts. Args: variables: The variables to be replaced in the prompt template. prompt_template: The template for the prompt. api_based_model_config: The API-based model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top p value to use. context_length: The length of the context to use. requests_per_minute: Limit on the number of OpenAI requests per minute Returns: The generated text. """ print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}, {context_length=}..." ) if model_config.provider == "openai": return asyncio.run( _generate_from_openai_completion( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, requests_per_minute, ) ) elif model_config.provider == "openai_chat": return asyncio.run( _generate_from_openai_chat_completion( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, requests_per_minute, ) ) elif model_config.provider == "cohere": return asyncio.run( _generate_from_cohere( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, requests_per_minute, ) ) elif model_config.provider == "huggingface": return _generate_from_huggingface( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, ) else: raise ValueError("Unknown provider, but you can add your own!")
[]
2024-01-10
TahaBinhuraib/nomic
examples~map_hf_dataset_with_cohere.py
from nomic import atlas from nomic import CohereEmbedder import numpy as np from datasets import load_dataset cohere_api_key = '' dataset = load_dataset("sentiment140")['train'] max_documents = 10000 subset_idxs = np.random.randint(len(dataset), size=max_documents).tolist() documents = [dataset[i] for i in subset_idxs] embedder = CohereEmbedder(cohere_api_key=cohere_api_key) print(f"Embedding {len(documents)} documents with Cohere API") embeddings = embedder.embed(texts=[document['user'] for document in documents], model='small') if len(embeddings) != len(documents): raise Exception("Embedding job failed") print("Embedding job complete.") response = atlas.map_embeddings(embeddings=np.array(embeddings), data=documents, colorable_fields=['sentiment'], name='Sentiment 140', description='A 10,000 point sample of the huggingface sentiment140 dataset embedded with cohere', ) print(response)
[]
2024-01-10
olivrg/openai-q-and-a
server~answer_question.py
import openai from flask import current_app, jsonify from config import * from utils import get_embedding TOP_K = 10 def get_answer_from_files(question, session_id, pinecone_index): logging.info(f"Getting answer for question: {question}") search_query_embedding = get_embedding(question, EMBEDDINGS_MODEL) try: query_response = pinecone_index.query( namespace=session_id, top_k=TOP_K, include_values=False, include_metadata=True, vector=search_query_embedding, ) logging.info( f"[get_answer_from_files] received query response from Pinecone: {query_response}") files_string = "" file_text_dict = current_app.config["file_text_dict"] for i in range(len(query_response.matches)): result = query_response.matches[i] file_chunk_id = result.id score = result.score filename = result.metadata["filename"] file_text = file_text_dict.get(file_chunk_id) file_string = f"###\n\"{filename}\"\n{file_text}\n" if score < COSINE_SIM_THRESHOLD and i > 0: logging.info( f"[get_answer_from_files] score {score} is below threshold {COSINE_SIM_THRESHOLD} and i is {i}, breaking") break files_string += file_string prompt = f"Given a question, try to answer it using the content of the file extracts below, and if you cannot answer, or find " \ f"a relevant file, just output \"I couldn't find the answer to that question in your files.\".\n\n" \ f"If the answer is not contained in the files or if there are no file extracts, respond with \"I couldn't find the answer " \ f"to that question in your files.\" If the question is not actually a question, respond with \"That's not a valid question.\"\n\n" \ f"In the cases where you can find the answer, first give the answer. Then explain how you found the answer from the source or sources, " \ f"and use the exact filenames of the source files you mention. Do not make up the names of any other files other than those mentioned "\ f"in the files context. Give the answer in markdown format." \ f"Use the following format:\n\nQuestion: <question>\n\nFiles:\n<###\n\"filename 1\"\nfile text>\n<###\n\"filename 2\"\nfile text>...\n\n"\ f"Answer: <answer or \"I couldn't find the answer to that question in your files\" or \"That's not a valid question.\">\n\n" \ f"Question: {question}\n\n" \ f"Files:\n{files_string}\n" \ f"Answer:" logging.info(f"[get_answer_from_files] prompt: {prompt}") response = openai.Completion.create( prompt=prompt, temperature=0, max_tokens=1000, top_p=1, frequency_penalty=0, presence_penalty=0, engine=GENERATIVE_MODEL, ) answer = response.choices[0].text.strip() logging.info(f"[get_answer_from_files] answer: {answer}") return jsonify({"answer": answer}) except Exception as e: logging.info(f"[get_answer_from_files] error: {e}") return str(e)
[ "Given a question, try to answer it using the content of the file extracts below, and if you cannot answer, or find a relevant file, just output \"I couldn't find the answer to that question in your files.\".\n\nIf the answer is not contained in the files or if there are no file extracts, respond with \"I couldn't find the answer to that question in your files.\" If the question is not actually a question, respond with \"That's not a valid question.\"\n\nIn the cases where you can find the answer, first give the answer. Then explain how you found the answer from the source or sources, and use the exact filenames of the source files you mention. Do not make up the names of any other files other than those mentioned in the files context. Give the answer in markdown format.Use the following format:\n\nQuestion: <question>\n\nFiles:\n<###\n\"filename 1\"\nfile text>\n<###\n\"filename 2\"\nfile text>...\n\nAnswer: <answer or \"I couldn't find the answer to that question in your files\" or \"That's not a valid question.\">\n\nQuestion: PLACEHOLDER\n\nFiles:\n\nAnswer:" ]
2024-01-10
Mohit1345/CineVocal
script_cohere.py
import cohere import os from dotenv import load_dotenv load_dotenv() co = cohere.Client(os.environ['cohere_api']) def scripter(Title, Year, Genre, plot): prompt =f"""Explain a movie named {Title}, {Genre}, {Year} of release,and its description is {plot}""" response = co.generate( model='command-nightly', prompt = prompt, max_tokens=2000, temperature=0.750) intro_paragraph = response.generations[0].text # print(intro_paragraph) return intro_paragraph # print(intro_paragraph)
[ "Explain a movie named PLACEHOLDER, PLACEHOLDER, PLACEHOLDER of release,and its description is PLACEHOLDER" ]
2024-01-10
create-dan/test
openagent~finetune~LLMFinetune.py
from abc import ABC, abstractmethod from logging import Logger import openai class LLMFinetune(ABC): def __init__(self, logger: Logger, openai_key: str): self.logger = logger openai.api_key = openai_key @abstractmethod def transform_data(self, train_csv_file: str, val_csv_file: str , train_output_file: str, val_output_file: str) -> str: pass @abstractmethod def finetune(self, **kwargs): pass
[]
2024-01-10
Kadah/CFG_Rescale_webui
scripts~CFGRescale.py
import math import torch import gradio as gr import numpy as np import modules.scripts as scripts from modules import devices, images, processing, shared, sd_samplers_kdiffusion, sd_samplers_compvis, script_callbacks from modules.processing import Processed from modules.shared import opts, state from ldm.models.diffusion import ddim from ldm.modules.diffusionmodules.util import make_ddim_sampling_parameters, noise_like class Script(scripts.Script): def __init__(self): self.old_denoising = sd_samplers_kdiffusion.CFGDenoiser.combine_denoised self.old_schedule = ddim.DDIMSampler.make_schedule self.old_sample = ddim.DDIMSampler.p_sample_ddim globals()['enable_furry_cocks'] = True def find_module(module_names): if isinstance(module_names, str): module_names = [s.strip() for s in module_names.split(",")] for data in scripts.scripts_data: if data.script_class.__module__ in module_names and hasattr(data, "module"): return data.module return None def rescale_opt(p, x, xs): globals()['cfg_rescale_fi'] = x globals()['enable_furry_cocks'] = False xyz_grid = find_module("xyz_grid.py, xy_grid.py") if xyz_grid: extra_axis_options = [xyz_grid.AxisOption("Rescale CFG", float, rescale_opt)] xyz_grid.axis_options.extend(extra_axis_options) def title(self): return "CFG Rescale Extension" def show(self, is_img2img): return scripts.AlwaysVisible def ui(self, is_img2img): with gr.Accordion("CFG Rescale", open=True, elem_id="cfg_rescale"): rescale = gr.Slider(label="CFG Rescale", show_label=False, minimum=0.0, maximum=1.0, step=0.01, value=0.0) trailing = gr.Checkbox(label="DDIM Trailing", default=False) self.infotext_fields = [ (rescale, "CFG Rescale"), (trailing, "DDIM Trailing"), ] self.paste_field_names = [] for _, field_name in self.infotext_fields: self.paste_field_names.append(field_name) return [rescale, trailing] def cfg_replace(self, x_out, conds_list, uncond, cond_scale): denoised_uncond = x_out[-uncond.shape[0]:] denoised = torch.clone(denoised_uncond) fi = globals()['cfg_rescale_fi'] for i, conds in enumerate(conds_list): for cond_index, weight in conds: if fi == 0: denoised[i] += (x_out[cond_index] - denoised_uncond[i]) * (weight * cond_scale) else: xcfg = (denoised_uncond[i] + (x_out[cond_index] - denoised_uncond[i]) * (cond_scale * weight)) xrescaled = xcfg * (torch.std(x_out[cond_index]) / torch.std(xcfg)) xfinal = fi * xrescaled + (1.0 - fi) * xcfg denoised[i] = xfinal return denoised def process(self, p, rescale, trailing): def schedule_override(self, ddim_num_steps, ddim_discretize="uniform", ddim_eta=0., verbose=True): print("DDIM TRAILING OVERRIDE SUCCESSFUL") def timesteps_override(ddim_discr_method, num_ddim_timesteps, num_ddpm_timesteps, verbose=True): c = -num_ddpm_timesteps / num_ddim_timesteps ddim_timesteps = np.round(np.flip(np.arange(num_ddpm_timesteps, 0, c))) steps_out = ddim_timesteps - 1 if verbose: print(f'Selected timesteps for ddim sampler: {steps_out}') return steps_out self.ddim_timesteps = timesteps_override(ddim_discr_method=ddim_discretize, num_ddim_timesteps=ddim_num_steps, num_ddpm_timesteps=self.ddpm_num_timesteps, verbose=verbose) alphas_cumprod = self.model.alphas_cumprod assert alphas_cumprod.shape[0] == self.ddpm_num_timesteps, 'alphas have to be defined for each timestep' to_torch = lambda x: x.clone().detach().to(torch.float32).to(self.model.device) self.register_buffer('betas', to_torch(self.model.betas)) self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod)) self.register_buffer('alphas_cumprod_prev', to_torch(self.model.alphas_cumprod_prev)) # calculations for diffusion q(x_t | x_{t-1}) and others self.register_buffer('sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod.cpu()))) self.register_buffer('sqrt_one_minus_alphas_cumprod', to_torch(np.sqrt(1. - alphas_cumprod.cpu()))) self.register_buffer('log_one_minus_alphas_cumprod', to_torch(np.log(1. - alphas_cumprod.cpu()))) self.register_buffer('sqrt_recip_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod.cpu()))) self.register_buffer('sqrt_recipm1_alphas_cumprod', to_torch(np.sqrt(1. / alphas_cumprod.cpu() - 1))) # ddim sampling parameters ddim_sigmas, ddim_alphas, ddim_alphas_prev = make_ddim_sampling_parameters(alphacums=alphas_cumprod.cpu(), ddim_timesteps=self.ddim_timesteps, eta=ddim_eta, verbose=verbose) self.register_buffer('ddim_sigmas', ddim_sigmas) self.register_buffer('ddim_alphas', ddim_alphas) self.register_buffer('ddim_alphas_prev', ddim_alphas_prev) self.register_buffer('ddim_sqrt_one_minus_alphas', np.sqrt(1. - ddim_alphas)) sigmas_for_original_sampling_steps = ddim_eta * torch.sqrt( (1 - self.alphas_cumprod_prev) / (1 - self.alphas_cumprod) * ( 1 - self.alphas_cumprod / self.alphas_cumprod_prev)) self.register_buffer('ddim_sigmas_for_original_num_steps', sigmas_for_original_sampling_steps) @torch.no_grad() def p_sample_ddim_override(self, x, c, t, index, repeat_noise=False, use_original_steps=False, quantize_denoised=False, temperature=1., noise_dropout=0., score_corrector=None, corrector_kwargs=None, unconditional_guidance_scale=1., unconditional_conditioning=None, dynamic_threshold=None): b, *_, device = *x.shape, x.device def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0): """ Rescale `noise_cfg` according to `guidance_rescale`. Based on findings of [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). See Section 3.4 """ std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True) std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True) # rescale the results from guidance (fixes overexposure) noise_pred_rescaled = noise_cfg * (std_text / std_cfg) # mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg return noise_cfg if unconditional_conditioning is None or unconditional_guidance_scale == 1.: model_output = self.model.apply_model(x, t, c) else: x_in = torch.cat([x] * 2) t_in = torch.cat([t] * 2) if isinstance(c, dict): assert isinstance(unconditional_conditioning, dict) c_in = dict() for k in c: if isinstance(c[k], list): c_in[k] = [torch.cat([ unconditional_conditioning[k][i], c[k][i]]) for i in range(len(c[k]))] else: c_in[k] = torch.cat([ unconditional_conditioning[k], c[k]]) elif isinstance(c, list): c_in = list() assert isinstance(unconditional_conditioning, list) for i in range(len(c)): c_in.append(torch.cat([unconditional_conditioning[i], c[i]])) else: c_in = torch.cat([unconditional_conditioning, c]) model_uncond, model_t = self.model.apply_model(x_in, t_in, c_in).chunk(2) model_output = model_uncond + unconditional_guidance_scale * (model_t - model_uncond) fi = globals()['cfg_rescale_fi'] if fi > 0: model_output = rescale_noise_cfg(model_output, model_t, guidance_rescale=fi) if self.model.parameterization == "v": e_t = self.model.predict_eps_from_z_and_v(x, t, model_output) else: e_t = model_output if score_corrector is not None: assert self.model.parameterization == "eps", 'not implemented' e_t = score_corrector.modify_score(self.model, e_t, x, t, c, **corrector_kwargs) alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas alphas_prev = self.model.alphas_cumprod_prev if use_original_steps else self.ddim_alphas_prev sqrt_one_minus_alphas = self.model.sqrt_one_minus_alphas_cumprod if use_original_steps else self.ddim_sqrt_one_minus_alphas sigmas = self.model.ddim_sigmas_for_original_num_steps if use_original_steps else self.ddim_sigmas # select parameters corresponding to the currently considered timestep a_t = torch.full((b, 1, 1, 1), alphas[index], device=device) a_prev = torch.full((b, 1, 1, 1), alphas_prev[index], device=device) sigma_t = torch.full((b, 1, 1, 1), sigmas[index], device=device) sqrt_one_minus_at = torch.full((b, 1, 1, 1), sqrt_one_minus_alphas[index],device=device) # current prediction for x_0 if self.model.parameterization != "v": pred_x0 = (x - sqrt_one_minus_at * e_t) / a_t.sqrt() else: pred_x0 = self.model.predict_start_from_z_and_v(x, t, model_output) if quantize_denoised: pred_x0, _, *_ = self.model.first_stage_model.quantize(pred_x0) if dynamic_threshold is not None: raise NotImplementedError() # direction pointing to x_t dir_xt = (1. - a_prev - sigma_t**2).sqrt() * e_t noise = sigma_t * noise_like(x.shape, device, repeat_noise) * temperature if noise_dropout > 0.: noise = torch.nn.functional.dropout(noise, p=noise_dropout) x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise return x_prev, pred_x0 if trailing: ddim.DDIMSampler.make_schedule = schedule_override p.extra_generation_params["DDIM Trailing"] = True if globals()['enable_furry_cocks']: globals()['cfg_rescale_fi'] = rescale globals()['enable_furry_cocks'] = True sd_samplers_kdiffusion.CFGDenoiser.combine_denoised = self.cfg_replace if rescale > 0: ddim.DDIMSampler.p_sample_ddim = p_sample_ddim_override p.extra_generation_params["CFG Rescale"] = rescale def postprocess(self, p, processed, rescale, trailing): sd_samplers_kdiffusion.CFGDenoiser.combine_denoised = self.old_denoising ddim.DDIMSampler.make_schedule = self.old_schedule ddim.DDIMSampler.p_sample_ddim = self.old_sample def on_infotext_pasted(infotext, params): if "CFG Rescale" not in params: params["CFG Rescale"] = 0 if "CFG Rescale φ" in params: params["CFG Rescale"] = params["CFG Rescale φ"] del params["CFG Rescale φ"] if "CFG Rescale phi" in params and scripts.scripts_txt2img.script("Neutral Prompt") is None: params["CFG Rescale"] = params["CFG Rescale phi"] del params["CFG Rescale phi"] if "DDIM Trailing" not in params: params["DDIM Trailing"] = False script_callbacks.on_infotext_pasted(on_infotext_pasted)
[]
2024-01-10
kadirnar/ChatGptHub
chatgpthub~templates~csv_template.py
def load_csv_agent(csv_file, text, model_name="gpt-3.5-turbo", temperature=0.0): from langchain.agents import create_csv_agent from langchain.chat_models import ChatOpenAI model = ChatOpenAI( model_name=model_name, temperature=temperature, ) langchain_agent = create_csv_agent( llm=model, path=csv_file, ) langchain_agent.agent.llm_chain.prompt.template return langchain_agent.run(text)
[]
2024-01-10
kadirnar/ChatGptHub
chatgpthub~templates~translate_template.py
def translate_chatgpt( model_name: str = "gpt-3.5-turbo", input_language: str = "English", output_language: str = "Turkish", text: str = "Hello, how are you?", temperature: float = 0.0, ): """ This function is a template for a chatbot that translates between two languages. Args: model_name: The name of the model to use. Defaults to "gpt-3.5-turbo". input_language: The language to translate from. Defaults to "English". output_language: The language to translate to. Defaults to "Turkish". text: The text to translate. Defaults to "Hello, how are you?". temperature: The temperature to use for the model. Defaults to 0.0. Returns: The translated text. """ from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) chat = ChatOpenAI(model_name=model_name, temperature=temperature) template = "You are a helpful assistant that translates {input_language} to {output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template = "{text}" human_message_prompt = HumanMessagePromptTemplate.from_template( human_template ) chat_prompt = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) chat_completion = chat( chat_prompt.format_prompt( input_language=input_language, output_language=output_language, text=text, ).to_messages() ) last_ai_message = chat_completion.content return last_ai_message
[ "You are a helpful assistant that translates {input_language} to {output_language}.", "[PLACEHOLDER, PLACEHOLDER]", "{text}" ]
2024-01-10
kadirnar/ChatGptHub
chatgpthub~tools~prompt_layer.py
def promptlayer_chatgpt( text: str = "Hello, I am a chatbot and", model_name: str = "gpt-3.5-turbo", temperature: float = 0.0, ): """ This function is a template for a chatbot that uses PromptLayer. Args: text: The text to use as the prompt. Defaults to "Hello, I am a chatbot and". model_name: The name of the model to use. Defaults to "gpt-3.5-turbo". temperature: The temperature to use for the model. Defaults to 0.0. Returns: The chatbot's response. """ from langchain.chat_models import PromptLayerChatOpenAI from langchain.schema import HumanMessage chat = PromptLayerChatOpenAI( pl_tags=["langchain"], model_name=model_name, temperature=temperature ) chat_completion = chat([HumanMessage(content=text)]).content return chat_completion
[]
2024-01-10
kadirnar/ChatGptHub
chatgpthub~templates~create_template.py
def custom_prompt_template( model_name: str = "gpt-3.5-turbo", template: str = "You are a helpful assistant that English to Turkish and you are asked to translate the following text: {text}", input_variables: str = "text", text: str = "Hello, how are you?", temperature: float = 0.0, ): from langchain.chains import LLMChain from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate llm = ChatOpenAI(model_name=model_name, temperature=temperature) prompt = PromptTemplate( input_variables=[input_variables], template=template ) chain = LLMChain(llm=llm, prompt=prompt) output = chain.run(text) return output
[]
2024-01-10
super-sid/assistantGPT
jira_main.py
import logging import chainlit as cl from langchain.llms import Ollama from langchain import PromptTemplate, LLMChain from langchain.memory import ConversationBufferMemory from jira import create_jira_ticket from jira_constants import * from chainlit.input_widget import Select memory = ConversationBufferMemory( memory_key="chat_history", return_messages=True) logger = logging.getLogger(__name__) llm = Ollama( base_url="http://127.0.0.1:11434", model="llama2:13b", temperature=0, ) @cl.on_chat_start async def main(): # Instantiate the chain for that user session settings = await cl.ChatSettings( [ Select( id="Task", label="Select Task", values=["jira", "coding"], initial_index=0, ) ] ).send() value = settings["Task"] print("ASJDASJIDJASJD", value) if value == "jira": print("ASJDASJIDJASJD", value) prompt = PromptTemplate(template=template, input_variables=[ "project_idea", ], partial_variables={"description": JIRA_DESCRIPTION}, ) llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True) # Store the chain in the user session cl.user_session.set("llm_chain", llm_chain) @cl.on_message async def main(message: str): llm_chain = cl.user_session.get("llm_chain") # type: LLMChain # Call the chain asynchronously res = await cl.make_async(llm_chain)( message, callbacks=[cl.LangchainCallbackHandler()] ) print(res) extracted_array = extract_code(res.get("text")) print(extracted_array, "mytexttt") # chain_output = llm_chain.predict(project_idea=res) # print(chain_output, ) # await cl.Message( # content=chain_output, # ).send() create_jira_ticket(extracted_array) await cl.Message(content=res["text"]).send() return llm_chain def extract_code(text): # Splitting based on triple backticks if "```" in text: blocks = text.split("```") # Filtering out empty blocks and taking every alternate block starting from the second one, which should contain the code code_blocks = [block.strip() for block in blocks if block.strip()][1::2] # Joining the blocks while ignoring the lines with backticks return "\n".join([line for block in code_blocks for line in block.splitlines() if not line.strip().startswith("```")]) # Splitting based on triple dashes if "---" in text: blocks = text.split("---") # Filtering out empty blocks and taking every alternate block starting from the second one, which should contain the code code_blocks = [block.strip() for block in blocks if block.strip()][1::2] # Joining the blocks while ignoring the lines with dashes return "\n".join([line for block in code_blocks for line in block.splitlines() if not line.strip().startswith("---")]) # Splitting based on <code></code> tags if "<code>" in text and "</code>" in text: blocks = text.split("<code>") end_blocks = [block.split("</code>")[0] for block in blocks if "</code>" in block] # Joining the blocks while ignoring the lines with code tags return "\n".join([line.strip() for block in end_blocks for line in block.splitlines() if not (line.strip().startswith("<code>") or line.strip().startswith("</code>"))]) return text
[ "description", "project_idea" ]
2024-01-10
GuXng/chatgpt-on-wechat
bot~openai~open_ai_bot.py
# encoding:utf-8 from bot.bot import Bot from config import conf from common.log import logger import openai import time user_session = dict() # OpenAI对话模型API (可用) class OpenAIBot(Bot): def __init__(self): openai.api_key = conf().get('open_ai_api_key') if conf().get('open_ai_api_base'): openai.api_base = conf().get('open_ai_api_base') proxy = conf().get('proxy') if proxy: openai.proxy = proxy def reply(self, query, context=None): # acquire reply content if not context or not context.get('type') or context.get('type') == 'TEXT': logger.info("[OPEN_AI] query={}".format(query)) from_user_id = context.get('from_user_id') or context.get('session_id') if query == '#清除记忆': Session.clear_session(from_user_id) return '记忆已清除' elif query == '#清除所有': Session.clear_all_session() return '所有人记忆已清除' new_query = Session.build_session_query(query, from_user_id) logger.debug("[OPEN_AI] session query={}".format(new_query)) reply_content = self.reply_text(new_query, from_user_id, 0) logger.debug("[OPEN_AI] new_query={}, user={}, reply_cont={}".format(new_query, from_user_id, reply_content)) if reply_content and query: Session.save_session(query, reply_content, from_user_id) return reply_content elif context.get('type', None) == 'IMAGE_CREATE': return self.create_img(query, 0) def reply_text(self, query, user_id, retry_count=0): try: response = openai.Completion.create( model= conf().get("model") or "text-davinci-003", # 对话模型的名称 prompt=query, temperature=0.9, # 值在[0,1]之间,越大表示回复越具有不确定性 max_tokens=1200, # 回复最大的字符数 top_p=1, frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 stop=["\n\n\n"] ) res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '') logger.info("[OPEN_AI] reply={}".format(res_content)) return res_content except openai.error.RateLimitError as e: # rate limit exception logger.warn(e) if retry_count < 1: time.sleep(5) logger.warn("[OPEN_AI] RateLimit exceed, 第{}次重试".format(retry_count+1)) return self.reply_text(query, user_id, retry_count+1) else: return "提问太快啦,请休息一下再问我吧" except Exception as e: # unknown exception logger.exception(e) Session.clear_session(user_id) return "请再问我一次吧" def create_img(self, query, retry_count=0): try: logger.info("[OPEN_AI] image_query={}".format(query)) response = openai.Image.create( prompt=query, #图片描述 n=1, #每次生成图片的数量 size="256x256" #图片大小,可选有 256x256, 512x512, 1024x1024 ) image_url = response['data'][0]['url'] logger.info("[OPEN_AI] image_url={}".format(image_url)) return image_url except openai.error.RateLimitError as e: logger.warn(e) if retry_count < 1: time.sleep(5) logger.warn("[OPEN_AI] ImgCreate RateLimit exceed, 第{}次重试".format(retry_count+1)) return self.reply_text(query, retry_count+1) else: return "提问太快啦,请休息一下再问我吧" except Exception as e: logger.exception(e) return None class Session(object): @staticmethod def build_session_query(query, user_id): ''' build query with conversation history e.g. Q: xxx A: xxx Q: xxx :param query: query content :param user_id: from user id :return: query content with conversaction ''' prompt = conf().get("character_desc", "") if prompt: prompt += "<|endoftext|>\n\n\n" session = user_session.get(user_id, None) if session: for conversation in session: prompt += "Q: " + conversation["question"] + "\n\n\nA: " + conversation["answer"] + "<|endoftext|>\n" prompt += "Q: " + query + "\nA: " return prompt else: return prompt + "Q: " + query + "\nA: " @staticmethod def save_session(query, answer, user_id): max_tokens = conf().get("conversation_max_tokens") if not max_tokens: # default 3000 max_tokens = 1000 conversation = dict() conversation["question"] = query conversation["answer"] = answer session = user_session.get(user_id) logger.debug(conversation) logger.debug(session) if session: # append conversation session.append(conversation) else: # create session queue = list() queue.append(conversation) user_session[user_id] = queue # discard exceed limit conversation Session.discard_exceed_conversation(user_session[user_id], max_tokens) @staticmethod def discard_exceed_conversation(session, max_tokens): count = 0 count_list = list() for i in range(len(session)-1, -1, -1): # count tokens of conversation list history_conv = session[i] count += len(history_conv["question"]) + len(history_conv["answer"]) count_list.append(count) for c in count_list: if c > max_tokens: # pop first conversation session.pop(0) @staticmethod def clear_session(user_id): user_session[user_id] = [] @staticmethod def clear_all_session(): user_session.clear()
[ "<|endoftext|>\n\n\n", "character_desc", "Q: PLACEHOLDER\nA: ", "Q: PLACEHOLDER\n\n\nA: PLACEHOLDER<|endoftext|>\n" ]
2024-01-10
philschmid/datasets
datasets~openwebtext~openwebtext.py
# coding=utf-8 # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The Open WebText Corpus""" from __future__ import absolute_import, division, print_function import os import re from itertools import chain import datasets _CITATION = """\ @misc{Gokaslan2019OpenWeb, title={OpenWebText Corpus}, author={Aaron Gokaslan*, Vanya Cohen*, Ellie Pavlick, Stefanie Tellex}, howpublished{\\url{http://Skylion007.github.io/OpenWebTextCorpus}}, year={2019} } """ _DESCRIPTION = """\ An open-source replication of the WebText dataset from OpenAI. """ _URL = "https://zenodo.org/record/3834942/files/openwebtext.tar.xz" class Openwebtext(datasets.GeneratorBasedBuilder): """The Open WebText dataset.""" BUILDER_CONFIGS = [ datasets.BuilderConfig( name="plain_text", description="Plain text", version=datasets.Version("1.0.0"), ) ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features({"text": datasets.Value("string")}), homepage="https://skylion007.github.io/OpenWebTextCorpus/", citation=_CITATION, ) def _split_generators(self, dl_manager): dl_dir = dl_manager.download_and_extract(_URL) owt_dir = os.path.join(dl_dir, "openwebtext") subset_xzs = [ os.path.join(owt_dir, file_name) for file_name in sorted(os.listdir(owt_dir)) if file_name.endswith("xz") # filter out ...xz.lock ] ex_dirs = dl_manager.extract(subset_xzs, num_proc=round(os.cpu_count() * 0.75)) nested_txt_files = [ [ os.path.join(ex_dir, txt_file_name) for txt_file_name in sorted(os.listdir(ex_dir)) if txt_file_name.endswith("txt") ] for ex_dir in ex_dirs ] txt_files = chain(*nested_txt_files) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"txt_files": txt_files}), ] def _generate_examples(self, txt_files): """ Yields examples. """ for idx, filepath in enumerate(txt_files): with open(filepath, encoding="utf-8") as f: yield idx, {"text": re.sub("\n\n\n+", "\n\n", f.read()).strip()}
[]
2024-01-10
TMoneyBidness/Funnelwriter-3
server_code~FunnelServer.py
import anvil.facebook.auth import anvil.google.auth, anvil.google.drive, anvil.google.mail from anvil.google.drive import app_files import anvil.users import anvil.files from anvil.files import data_files import anvil.tables as tables import anvil.tables.query as q from anvil.tables import app_tables import anvil.secrets import anvil.server import anvil.http import openai import time import requests from langchain.agents import load_tools, initialize_agent, Tool, AgentType from langchain.chat_models import ChatOpenAI from langchain import SerpAPIWrapper, LLMChain, PromptTemplate from langchain.tools import StructuredTool from langchain.utilities import GoogleSearchAPIWrapper from langchain.utilities import SerpAPIWrapper import json import re from collections import OrderedDict import requests from bs4 import BeautifulSoup ############################################################################################################################ openai_api_key = anvil.secrets.get_secret('OPENAI_API_KEY') serpapi_api_key = anvil.secrets.get_secret('SERPAPI_API_KEY') google_cse_id = anvil.secrets.get_secret('GOOGLE_CSE_ID') google_api_key = anvil.secrets.get_secret('GOOGLE_API_KEY') ############################################################################################################################ ############################################################################################################################ ### TOOLS # SERPAPI search = SerpAPIWrapper(serpapi_api_key=serpapi_api_key) tools = Tool( name="Google Search", description="Search Google for recent results.", func=search.run, ) # # GOOGLE SEARCH - DOESN'T WORK # search = GoogleSearchAPIWrapper(google_api_key=google_api_key, google_cse_id=google_cse_id) # tools = Tool( # name="Google Search", # description="Search Google for recent results.", # func=search.run, # ) ############################################################################################################################ ####### -------- PRELIMINARY / FIRST DRAFTS--------########### # COMPANY 1st DRAFT # USE WEBSCRAPER @anvil.server.callable def launch_draft_company_summary_scraper(company_name, company_url): # Launch the background task current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='company_profile_latest') company_dump_row = user_table.get(variable='company_page_dump') # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(company_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces company_context_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces company_context_scraped = re.sub(r'\s+', ' ', company_context_scraped_bulky.strip()) print("Scraped Information:",company_context_scraped) print("Launch task started for researching company:",company_url) task = anvil.server.launch_background_task('draft_company_summary_scraper', company_name, company_url,row,company_context_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def draft_company_summary_scraper(company_name, company_url,row,company_context_scraped): #Perform the Webscraping print("Background task started for generating the company summary:", company_url) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) template_company_summary = """As a highly-skilled business analyst, your task is to conduct an exhaustive analysis to build an informational company profile of {company_name}. \ Leverage the below provided company research context scraped from the company's website {company_url}, to create a complete company profile. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide a meaningful synopsis and findings. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the company. What are the unique features or value propositions of the company's offerings? What does the company aim to achieve? \n \ \n \ Unique Value Proposition: What is the company unique value proposition? What are they uniquely positioned to do? How does their main offer differ from their competitors? \n \ \n \ Founding Story: What inspired the founders to start the company? Are there any unique or interesting anecdotes about the early days of the company? How has the company evolved since its founding? \n \ \n \ Competitors: Who are the likely competitors of this company? What are their strengths and weaknesses? How does your company compare to its competitors in terms of offerings, market share, or other relevant factors? \n \ \n \ Mission & Vision: What is the company's mission statement or core purpose? What are the long-term goals and aspirations of the company? \n \ Values: What does the company value? What do they emphasize in their mission? What do they care about or prioritize? \n \ \n \ NOTES ON FORMAT: This should be at least 800 words. Be confident. If there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. Ensure you keep the headers with the '--': -- Overview (your overview) --Unique Value Proposition (your response) --Competitors (your response) -- Founding Story (your response) --Mission & Vision (your response) --Values (your response) ** END OF FORMAT FINALLY, HERE IS THE COMPANY CONTEXT SCRAPED FROM THEIR WEBSITE: {company_context_scraped} """ prompt_company_summary = PromptTemplate( input_variables=["company_name", "company_url","company_context_scraped"], template=template_company_summary ) chain_company_summary = LLMChain(llm=llm_agents, prompt=prompt_company_summary) draft_company_summary = chain_company_summary.run(company_name=company_name,company_url=company_url,company_context_scraped=company_context_scraped) # Pass in the combined context # Save this generated version as the latest version row['variable_value'] = draft_company_summary row.update() print("Company Research Complete") ### THESE ARE THE COMPANY SEARCH AGENTS # COMPANY 1st DRAFT # @anvil.server.callable # def launch_draft_company_summary(user_table,company_name, company_url): # # Launch the background task # current_user = anvil.users.get_user() # user_table_name = current_user['user_id'] # # Get the table for the current user # user_table = getattr(app_tables, user_table_name) # row = user_table.get(variable='company_profile_latest') # print("Launch task started for researching company:",row, company_name,company_url) # task = anvil.server.launch_background_task('draft_company_summary', row,company_name, company_url) # # Return the task ID # return task.get_id() # @anvil.server.background_task # def draft_company_summary(row,company_name, company_url): # print("Background task started for researching company:", row,company_name,company_url) # llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo', openai_api_key=openai_api_key) # agent_company_context = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) # company_research = agent_company_context({"input": f"""As a highly-skilled business research agent, your task is to conduct an exhaustive analysis to build an informational company profile of {company_name}. \ # Leverage all necessary resources, primarily the company's website {company_url}, but also news articles, and any other relevant sources. \ # to gather the following details about {company_name}. Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful research and findings. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! # \n \ # Overview: Provide a comprehensive introduction to the company. What are the unique features or value propositions of the company's offerings? What does the company aim to achieve? \n \ # \n \ # Unique Value Proposition: What is the company unique value proposition? What are they uniquely positioned to do? How does their main offer differ from their competitors? \n \ # \n \ # Founding Story: What inspired the founders to start the company? Are there any unique or interesting anecdotes about the early days of the company? How has the company evolved since its founding? \n \ # \n \ # Competitors: Who are the likely competitors of this company? What are their strengths and weaknesses? How does your company compare to its competitors in terms of offerings, market share, or other relevant factors? \n \ # \n \ # Mission & Vision: What is the company's mission statement or core purpose? What are the long-term goals and aspirations of the company? \n \ # Values: What does the company value? What do they emphasize in their mission? What do they care about or prioritize? \n \ # \n \ # NOTES ON FORMAT: # This should be at least 800 words. Be confident, do not say there is incomplete information, or there is not information. If you can't answer elements from the above, ignore it! Speak as if you are the authority of the subject. If you don't know the answer, don't talk about it. Do not say "I was unable to find information on XYZ". # Ensure you keep the headers with the '--': # -- Overview # (your overview) # --Unique Value Proposition # (your response) # --Competitors # (your response) # -- Founding Story # (your response) # --Mission & Vision # (your response) # --Values # (your response) # """}) # draft_company_context = company_research['output'] # # Save this generated version as the latest version # row['variable_value'] = draft_company_context # row.update() # print("Company Research Complete") # anvil.server.task_state['result'] = draft_company_context # PRODUCT 1st DRAFT @anvil.server.callable def remove_duplicate_substrings(text, min_len=20): seen = OrderedDict() output_list = [] n = len(text) for i in range(n - min_len + 1): substring = text[i:i + min_len] if substring not in seen: seen[substring] = i # Save the starting index of this substring output_list.append(substring) # Join the substrings to get the final string return ''.join(output_list) # HERE'S THE FUNCTION @anvil.server.callable def launch_draft_deepdive_product_1_generator(user_table,company_name,product_name,product_url): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces product_webpage_scraped = re.sub(r'\s+', ' ', product_webpage_scraped_bulky.strip()) print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_1_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_draft_product_1_generator(user_table,company_name,product_name,product_url,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context # Save it in the table: product_1_latest_row = user_table.search(variable='product_1_latest')[0] product_1_latest_row['variable_value'] = product_summary product_1_latest_row.update() print("Product Research Complete") # PRODUCT 2, 1st DRAFT @anvil.server.callable def launch_draft_deepdive_product_2_generator(user_table,company_name,product_name,product_url): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces product_webpage_scraped = re.sub(r'\s+', ' ', product_webpage_scraped_bulky.strip()) print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_2_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_draft_product_2_generator(user_table,company_name,product_name,product_url,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context # Save it in the table: product_2_latest_row = user_table.search(variable='product_2_latest')[0] product_2_latest_row['variable_value'] = product_summary product_2_latest_row.update() print("Product Research Complete") # PRODUCT 3, 1st DRAFT @anvil.server.callable def launch_draft_deepdive_product_3_generator(user_table,company_name,product_name,product_url): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces product_webpage_scraped = re.sub(r'\s+', ' ', product_webpage_scraped_bulky.strip()) print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_3_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_draft_product_3_generator(user_table,company_name,product_name,product_url,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCTWEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context # Save it in the table: product_3_latest_row = user_table.search(variable='product_3_latest')[0] product_3_latest_row['variable_value'] = product_summary product_3_latest_row.update() print("Product Research Complete") # PRODUCT 4, 1st DRAFT @anvil.server.callable def launch_draft_deepdive_product_4_generator(user_table,company_name,product_name,product_url): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces product_webpage_scraped = re.sub(r'\s+', ' ', product_webpage_scraped_bulky.strip()) print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_4_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_draft_product_4_generator(user_table,company_name,product_name,product_url,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context # Save it in the table: product_4_latest_row = user_table.search(variable='product_4_latest')[0] product_4_latest_row['variable_value'] = product_summary product_4_latest_row.update() print("Product Research Complete") # PRODUCT 5, 1st DRAFT @anvil.server.callable def launch_draft_deepdive_product_5_generator(user_table,company_name,product_name,product_url): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped_bulky = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') # Further remove extra white spaces product_webpage_scraped = re.sub(r'\s+', ' ', product_webpage_scraped_bulky.strip()) print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_5_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_draft_product_5_generator(user_table,company_name,product_name,product_url,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context # Save it in the table: product_5_latest_row = user_table.search(variable='product_5_latest')[0] product_5_latest_row['variable_value'] = product_summary product_5_latest_row.update() print("Product Research Complete") #------AVATARS, 1st DRAFT - AVATAR 1 / PRODUCT 1-------------------------------------------################## @anvil.server.callable def launch_draft_deepdive_avatar_1_product_1_generator(user_table,company_name,product_1_name,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_1_product_1_generator', user_table,company_name,product_1_name,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_1_product_1_generator(user_table,company_name,product_1_name,avatar_1_preview): print("Background task started for generating the avatar:", avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_1_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_1_name", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_1_name=product_1_name, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_1_latest = user_table.search(variable='avatar_1_product_1_latest') first_row_avatar_1_latest = row_avatar_1_latest[0] first_row_avatar_1_latest['variable_value'] = draft_avatar first_row_avatar_1_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 2 / PRODUCT 1 @anvil.server.callable def launch_draft_deepdive_avatar_2_product_1_generator(user_table,company_name,product_1_name,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_2_product_1_generator', user_table,company_name,product_1_name,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_2_product_1_generator(user_table,company_name,product_1_name,avatar_2_preview): print("Background task started for generating the avatar:", avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_1_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_1_name", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_1_name=product_1_name, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_2_latest = user_table.search(variable='avatar_2_product_1_latest') first_row_avatar_2_latest = row_avatar_2_latest[0] first_row_avatar_2_latest['variable_value'] = draft_avatar first_row_avatar_2_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 3 / PRODUCT 1 @anvil.server.callable def launch_draft_deepdive_avatar_3_product_1_generator(user_table,company_name,product_1_name,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_3_product_1_generator', user_table,company_name,product_1_name,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_3_product_1_generator(user_table,company_name,product_1_name,avatar_3_preview): print("Background task started for generating the avatar:", avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_1_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_1_name", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_1_name=product_1_name, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_3_latest = user_table.search(variable='avatar_3_product_1_latest') first_row_avatar_3_latest = row_avatar_3_latest[0] first_row_avatar_3_latest['variable_value'] = draft_avatar first_row_avatar_3_latest.update() print("Avatar Draft Research Complete") #------AVATARS, 1st DRAFT - AVATAR 1 / PRODUCT 2-----------------################## @anvil.server.callable def launch_draft_deepdive_avatar_1_product_2_generator(user_table,company_name,product_2_name,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_1_product_2_generator', user_table,company_name,product_2_name,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_1_product_2_generator(user_table,company_name,product_2_name,avatar_1_preview): print("Background task started for generating the avatar:", avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_2_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_2_name", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_2_name=product_2_name, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_1_latest = user_table.search(variable='avatar_1_product_2_latest') first_row_avatar_1_latest = row_avatar_1_latest[0] first_row_avatar_1_latest['variable_value'] = draft_avatar first_row_avatar_1_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 2 / PRODUCT 2 @anvil.server.callable def launch_draft_deepdive_avatar_2_product_2_generator(user_table,company_name,product_2_name,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_2_product_2_generator', user_table,company_name,product_2_name,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_2_product_2_generator(user_table,company_name,product_2_name,avatar_2_preview): print("Background task started for generating the avatar:", avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_2_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_2_name", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_2_name=product_2_name, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_2_latest = user_table.search(variable='avatar_2_product_2_latest') first_row_avatar_2_latest = row_avatar_2_latest[0] first_row_avatar_2_latest['variable_value'] = draft_avatar first_row_avatar_2_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 3 / PRODUCT 2 @anvil.server.callable def launch_draft_deepdive_avatar_3_product_2_generator(user_table,company_name,product_2_name,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_3_product_2_generator', user_table,company_name,product_2_name,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_3_product_2_generator(user_table,company_name,product_2_name,avatar_3_preview): print("Background task started for generating the avatar:", avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_2_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_2_name", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_2_name=product_2_name, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_3_latest = user_table.search(variable='avatar_3_product_2_latest') first_row_avatar_3_latest = row_avatar_3_latest[0] first_row_avatar_3_latest['variable_value'] = draft_avatar first_row_avatar_3_latest.update() print("Avatar Draft Research Complete") #------AVATARS, 1st DRAFT - AVATAR 1 / PRODUCT 3 -----------------################## @anvil.server.callable def launch_draft_deepdive_avatar_1_product_3_generator(user_table,company_name,product_3_name,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_1_product_3_generator', user_table,company_name,product_3_name,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_1_product_3_generator(user_table,company_name,product_3_name,avatar_1_preview): print("Background task started for generating the avatar:", avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_3_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_3_name", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_3_name=product_3_name, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_1_latest = user_table.search(variable='avatar_1_product_3_latest') first_row_avatar_1_latest = row_avatar_1_latest[0] first_row_avatar_1_latest['variable_value'] = draft_avatar first_row_avatar_1_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 2 / PRODUCT 3 @anvil.server.callable def launch_draft_deepdive_avatar_2_product_3_generator(user_table,company_name,product_3_name,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_2_product_3_generator', user_table,company_name,product_3_name,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_2_product_3_generator(user_table,company_name,product_3_name,avatar_2_preview): print("Background task started for generating the avatar:", avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_3_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_3_name", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_3_name=product_3_name, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_2_latest = user_table.search(variable='avatar_2_product_3_latest') first_row_avatar_2_latest = row_avatar_2_latest[0] first_row_avatar_2_latest['variable_value'] = draft_avatar first_row_avatar_2_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 3 / PRODUCT 3 @anvil.server.callable def launch_draft_deepdive_avatar_3_product_3_generator(user_table,company_name,product_3_name,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_3_product_3_generator', user_table,company_name,product_3_name,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_3_product_3_generator(user_table,company_name,product_3_name,avatar_3_preview): print("Background task started for generating the avatar:", avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_3_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_3_name", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_3_name=product_3_name, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_3_latest = user_table.search(variable='avatar_3_product_3_latest') first_row_avatar_3_latest = row_avatar_3_latest[0] first_row_avatar_3_latest['variable_value'] = draft_avatar first_row_avatar_3_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 1 / PRODUCT 4 @anvil.server.callable def launch_draft_deepdive_avatar_1_product_4_generator(user_table,company_name,product_4_name,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_1_product_4_generator', user_table,company_name,product_4_name,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_1_product_4_generator(user_table,company_name,product_4_name,avatar_1_preview): print("Background task started for generating the avatar:", avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_4_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_4_name", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_4_name=product_4_name, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_1_latest = user_table.search(variable='avatar_1_product_4_latest') first_row_avatar_1_latest = row_avatar_1_latest[0] first_row_avatar_1_latest['variable_value'] = draft_avatar first_row_avatar_1_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 2 / PRODUCT 4 @anvil.server.callable def launch_draft_deepdive_avatar_2_product_4_generator(user_table,company_name,product_4_name,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_2_product_4_generator', user_table,company_name,product_4_name,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_2_product_4_generator(user_table,company_name,product_4_name,avatar_2_preview): print("Background task started for generating the avatar:", avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_4_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_4_name", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_4_name=product_4_name, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_2_latest = user_table.search(variable='avatar_2_product_4_latest') first_row_avatar_2_latest = row_avatar_2_latest[0] first_row_avatar_2_latest['variable_value'] = draft_avatar first_row_avatar_2_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 3 / PRODUCT 4 @anvil.server.callable def launch_draft_deepdive_avatar_3_product_4_generator(user_table,company_name,product_4_name,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_3_product_4_generator', user_table,company_name,product_4_name,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_3_product_4_generator(user_table,company_name,product_4_name,avatar_3_preview): print("Background task started for generating the avatar:", avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_4_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_4_name", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_4_name=product_4_name, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_3_latest = user_table.search(variable='avatar_3_product_4_latest') first_row_avatar_3_latest = row_avatar_3_latest[0] first_row_avatar_3_latest['variable_value'] = draft_avatar first_row_avatar_3_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 1 / PRODUCT 5 @anvil.server.callable def launch_draft_deepdive_avatar_1_product_5_generator(user_table,company_name,product_5_name,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_1_product_5_generator', user_table,company_name,product_5_name,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_1_product_5_generator(user_table,company_name,product_5_name,avatar_1_preview): print("Background task started for generating the avatar:", avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_5_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_5_name", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_5_name=product_5_name, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_1_latest = user_table.search(variable='avatar_1_product_5_latest') first_row_avatar_1_latest = row_avatar_1_latest[0] first_row_avatar_1_latest['variable_value'] = draft_avatar first_row_avatar_1_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 2 / PRODUCT 5 @anvil.server.callable def launch_draft_deepdive_avatar_2_product_5_generator(user_table,company_name,product_5_name,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_2_product_5_generator', user_table,company_name,product_5_name,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_2_product_5_generator(user_table,company_name,product_5_name,avatar_2_preview): print("Background task started for generating the avatar:", avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_5_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_5_name", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_5_name=product_5_name, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_2_latest = user_table.search(variable='avatar_2_product_5_latest') first_row_avatar_2_latest = row_avatar_2_latest[0] first_row_avatar_2_latest['variable_value'] = draft_avatar first_row_avatar_2_latest.update() print("Avatar Draft Research Complete") # AVATARS, 1st DRAFT - AVATAR 3 / PRODUCT 5 @anvil.server.callable def launch_draft_deepdive_avatar_3_product_5_generator(user_table,company_name,product_5_name,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('draft_deepdive_avatar_3_product_5_generator', user_table,company_name,product_5_name,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def draft_deepdive_avatar_3_product_5_generator(user_table,company_name,product_5_name,avatar_3_preview): print("Background task started for generating the avatar:", avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. Company Context: The company, {company_name}, is selling {product_5_name}. Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["company_name","product_5_name", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) draft_avatar = chain_avatar.run(company_name=company_name, product_5_name=product_5_name, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = draft_avatar # Save this generated version as the latest version row_avatar_3_latest = user_table.search(variable='avatar_3_product_5_latest') first_row_avatar_3_latest = row_avatar_3_latest[0] first_row_avatar_3_latest['variable_value'] = draft_avatar first_row_avatar_3_latest.update() print("Avatar Draft Research Complete") # BRAND TONE 1st DRAFT @anvil.server.callable def launch_draft_brand_tone_research(user_table,company_url): # Launch the background task task = anvil.server.launch_background_task('draft_brand_tone_research',user_table,company_url) # Return the task ID return task.get_id() @anvil.server.background_task def draft_brand_tone_research(user_table,brand_tone_url): print("Background task started for extracting brand tone:", user_table,brand_tone_url) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-4', openai_api_key=openai_api_key) agent_tone_extraction = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) tone_research = agent_tone_extraction({ "input":f"""You are CopywriterAI, the best copywriter on the planet. We are looking to generate a description of the tone that best describes the copywriting style and tone of an existing web page. Go and research this URL {brand_tone_url}, and provide your analysis. For example, for PROFESSIONAL / ENTERPRISE, it would be described as: - 'Formal and Polished': (sophisticated language and complex sentence structures). - 'Objective and Analytical': (incorporates data and facts, prioritizing logical arguments). - 'Business-like': Efficient, frequently employs industry-specific jargon and business terminology). - 'Trustworthy and Reliable': Underscoring credibility, reliability, and accuracy. - 'Instructional and Informative': (providing clear, direct instructions or information). - 'Respectful and Considerate': (acknowledging the audience's needs and viewpoints while avoiding excessive casualness). - 'Controlled and Consistent': (providing coherence, ensuring careful, consistent writing). For Russell Brunson, sales page style, it would be described as: - 'Conversational': (friendly, casual, and approachable). - 'Storytelling': (using compelling stories to illustrate his points). - 'Educational': (being informative, teaching something new). - 'Persuasive': (being compelling and enticing, using ideas of scarcity (limited time offers), social proof (testimonials), and authority (expertise and success). - 'Inspiring': (motivating and inspiring, encouraging the reader to take action). - 'Clear and Direct': (providing clarity and simplicity, avoiding jargon). However, it is up to you to go and review the website, think about the tone of the existing copy, and return 5-6 descriptors, in the similar format as above. They don't have to be listed above- they can be new! OUTPUT TEMPLATE: AN EXAMPLE OUTPUT SHOULD BE AS BELOW: 'The businesstone can be described as': - 'Conversational': (friendly, casual, and approachable). - 'Storytelling': (using compelling stories to illustrate his points). - 'Educational': (being informative, teaching something new). - 'Persuasive': (being compelling and enticing, using ideas of scarcity (limited time offers), social proof (testimonials), and authority (expertise and success). - 'Inspiring': (motivating and inspiring, encouraging the reader to take action). - 'Clear and Direct': (providing clarity and simplicity, avoiding jargon).Conversational, Storytelling, Educational, Persuasive, Inspiring, Clear and Direct' FINAL RULES: Don't mention the business name, or source. Just say "the business" and refer to it as 'company tone' or 'the business tone' """}) extracted_tone = tone_research['output'] anvil.server.task_state['result'] = extracted_tone # Save the brand tone brand_tone_latest_row = list(user_table.search(variable='brand_tone')) first_row_brand_tone_latest = brand_tone_latest_row[0] first_row_brand_tone_latest['variable_value'] = extracted_tone first_row_brand_tone_latest['variable_title'] = brand_tone_url first_row_brand_tone_latest.update() print("Brand Tone Research Complete") # Function to get the status of a background task @anvil.server.callable def get_status_function(task_id): # Retrieve the task status from the Data Table (assuming you have a Data Table named 'tasks') task_table = app_tables.tasks # Replace 'tasks' with your actual Data Table name task_row = task_table.get(task_id=task_id) status = task_row['status'] return status ####### -------------------------------- COMPANY ----------------------------------------------------########### @anvil.server.callable def launch_company_summary(company_name, company_url): # Launch the background task print("Launch task started for researching company:",company_name,company_url) task = anvil.server.launch_background_task('company_summary', company_name, company_url) # Return the task ID return task.get_id() @anvil.server.background_task def company_summary(company_name, company_url): print("Background task started for researching company:", company_name,company_url) # Here, you should write the code that uses the company_name and company_url # to research the company and generate a context. For example: llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) agent_company_context = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) #max_execution_time=300,max_iterations=300 company_research = agent_company_context({"input": f"""As a highly-skilled business research agent, your task is to conduct an exhaustive analysis to build an informational company profile of {company_name}. \ Leverage all necessary resources, primarily the company's website {company_url}, but also news articles, and any other relevant sources. \ to gather the following details about {company_name}. Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful research and findings. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the company. What are the unique features or value propositions of the company's offerings? What does the company aim to achieve? \n \ \n \ Unique Value Proposition: What is the company unique value proposition? What are they uniquely positioned to do? How does their main offer differ from their competitors? \n \ \n \ Founding Story: What inspired the founders to start the company? Are there any unique or interesting anecdotes about the early days of the company? How has the company evolved since its founding? \n \ \n \ Competitors: Who are the likely competitors of this company? What are their strengths and weaknesses? How does your company compare to its competitors in terms of offerings, market share, or other relevant factors? \n \ \n \ Mission & Vision: What is the company's mission statement or core purpose? What are the long-term goals and aspirations of the company? \n \ Values: What does the company value? What do they emphasize in their mission? What do they care about or prioritize? \n \ \n \ NOTES ON FORMAT: This should be at least 800 words. Be confident, do not say there is incomplete information, or there is not information. If you can't answer elements from the above, ignore it! Speak as if you are the authority of the subject. If you don't know the answer, don't talk about it. Do not say "I was unable to find information on XYZ". Ensure you keep the headers with the '--': -- Overview (your overview) --Unique Value Proposition (your response) --Competitors (your response) -- Founding Story (your response) --Mission & Vision (your response) --Values (your response) """}) company_context = company_research['output'] # Check if the output indicates insufficient information if "I couldn't find more information" in company_context: company_context = "Insufficient information. Please write the company description yourself." # Store the result in the task's state instead of returning it anvil.server.task_state['result'] = company_context ####### -------- PRODUCT --------################################################### # @anvil.server.callable # def launch_all_products_generator(company_profile, company_url): # print("Launch all products research function started") # # Launch the background task # task = anvil.server.launch_background_task('all_products_generator', company_profile, company_url) # # Return the task ID # return task.get_id() # @anvil.server.background_task # def all_products_generator(company_profile, company_url): # print("Background task started for generating all the products:", company_profile, company_url) # llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-4', openai_api_key=openai_api_key) # agent_products_research = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) # all_products_research = agent_products_research({"input": f""" You are ProductFinderAI, an advanced marketing consultant, your role is to guide a company determine their most popular and obvious products they should sell in order to boost their online presence, attract a larger customer base, and increase sales. You will employ the strategies of Russell Brunson, the founder of ClickFunnels, as detailed in his book "Dotcom Secrets". # Your mission is to pinpoint five potential products or services that align best with the company's business. You should also rank these products or services based on their value to the company. # For each product or service, provide a title and a single sentence description that includes any other pertinent details (like pricing, access, special features, etc.) # The output should be formatted as follows, and include "->" as a seperator which is very important! # 'Title' -> Description of Product/Service 1. # 'Title' -> Description of Product/Service 2 # 'Title' -> Description of Product/Service 3 # 'Title' -> Description of Product/Service 4 # 'Title' -> Description of Product/Service 5 # For instance: # -- Freshsales Free CRM -> This plan is free for up to 3 users and includes a visual sales pipeline, automation via workflows and sales sequences, and built-in email, phone, and chat for contextual engagement. It provides everything you need to engage leads across phone, email & SMS. # -- Freshsales Growth ->Priced at $15/user/month when billed annually or $18/user/month when billed monthly, the Growth plan includes everything in the Free CRM plan, plus AI-powered contact scoring, up to 2,000 bot sessions per month, and sales sequences. It also includes 1 CPQ license. This plan is designed to help growing sales teams avoid repetitive work and spend more time selling. # -- Freshsales Pro -> Priced at $39/user/month when billed annually or $47/user/month when billed monthly, the Pro plan includes everything in the Growth plan, plus multiple sales pipelines, time-based workflows, AI-powered deal insights & next best action, up to 3,000 bot sessions per month, and sales teams & territory management. This plan is designed for managing multiple sales teams and growing revenue. # -- Freshsales Enterprise -> Priced at $69/user/month when billed annually or $83/user/month when billed monthly, the Enterprise plan includes everything in the Pro plan, plus custom modules, AI-based forecasting insights, audit logs, up to 5,000 bot sessions per month, and a dedicated account manager. This plan offers advanced customization, governance, and controls. # FORMAT: # CONTEXTUAL INFORMATION: # COMPANY CONTEXT: {company_profile} # COMPANY WEBSITE: {company_url} # Chatbot: # """}) # all_products_grouped = all_products_research['output'] # print("all_products_grouped:", all_products_grouped) # # # Check if the output indicates insufficient information # # if "I couldn't find more information" in all_products_research: # # all_products_research = "Insufficient information. Please write the company description yourself." # # # Store the result in the task's state instead of returning it # all_product_lines = all_products_grouped.split("\n") # # Initialize an empty dictionary # all_products = {} # # Loop over each line # i = 1 # for product_line in all_product_lines: # # Ignore empty lines # if not product_line.strip(): # continue # # Check if the line starts with 'Ranking:' # if product_line.startswith('Ranking:'): # all_products['ranking'] = product_line.strip() # else: # # Split the line into title and description using '->' as the separator, if possible # line_parts = product_line.strip().split(' -> ') # if len(line_parts) >= 2: # title, description = line_parts # else: # # If the line doesn't contain the separator, consider the entire line as the title # title = product_line.strip() # description = "" # Set description to an empty string or any default value # key = f"product_{i}" # value = f"{title} -> {description}" # # Add to dictionary # all_products[key] = value # i += 1 # # # Return the resulting dictionary # # anvil.server.task_state['result'] = all_avatars # # Convert the dictionary to a JSON string # all_products_json = json.dumps(all_products) # # Return the resulting JSON string # anvil.server.task_state['result'] = all_products_json #### LAUNCH THE PRODUCT DEEP DIVES # PRODUCT 1 @anvil.server.callable def launch_deepdive_product_1_generator(user_table,company_name,product_name,product_url,product_preview): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_1_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_product_1_generator(user_table,company_name,product_name,product_url,product_preview,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview} This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_preview","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_preview=product_preview,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context print("PRODUCT SUMMARY:",product_summary) # Save it in the table: product_1_latest_row = user_table.search(variable='product_1_latest')[0] product_1_latest_row['variable_value'] = product_summary product_1_latest_row.update() print("Product Research Complete") # PRODUCT 2 @anvil.server.callable def launch_deepdive_product_2_generator(user_table,company_name,product_name,product_url,product_preview): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_2_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_product_2_generator(user_table,company_name,product_name,product_url,product_preview,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview} This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_preview","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_preview=product_preview,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context print("PRODUCT SUMMARY:",product_summary) # Save it in the table: product_2_latest_row = user_table.search(variable='product_2_latest')[0] product_2_latest_row['variable_value'] = product_summary product_2_latest_row.update() print("Product Research Complete") # PRODUCT 3 @anvil.server.callable def launch_deepdive_product_3_generator(user_table,company_name,product_name,product_url,product_preview): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_3_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_product_3_generator(user_table,company_name,product_name,product_url,product_preview,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview} This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_preview","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_preview=product_preview,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context print("PRODUCT SUMMARY:",product_summary) # Save it in the table: product_3_latest_row = user_table.search(variable='product_3_latest')[0] product_3_latest_row['variable_value'] = product_summary product_3_latest_row.update() print("Product Research Complete") # PRODUCT 4 @anvil.server.callable def launch_deepdive_product_4_generator(user_table,company_name,product_name,product_url,product_preview): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_4_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_product_4_generator(user_table,company_name,product_name,product_url,product_preview,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview} This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_preview","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_preview=product_preview,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context print("PRODUCT SUMMARY:",product_summary) # Save it in the table: product_4_latest_row = user_table.search(variable='product_4_latest')[0] product_4_latest_row['variable_value'] = product_summary product_4_latest_row.update() print("Product Research Complete") # PRODUCT 5 @anvil.server.callable def launch_deepdive_product_5_generator(user_table,company_name,product_name,product_url,product_preview): # Launch the background task # START THE WEB SCRAPING headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"} page_content = requests.get(product_url, headers=headers).content soup = BeautifulSoup(page_content, "html.parser") # Extract all the text from the page bulky_text_content = soup.get_text() # Remove leading and trailing whitespaces, replace newlines and extra spaces product_webpage_scraped = bulky_text_content.strip().replace('\n', ' ').replace('\r', '').replace(' ', ' ') print("Scraped Information:",product_webpage_scraped) task = anvil.server.launch_background_task('deepdive_draft_product_5_generator',user_table,company_name,product_name,product_url,product_webpage_scraped) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_product_5_generator(user_table,company_name,product_name,product_url,product_preview,product_webpage_scraped): print("Background task started for the Deep Dive of Researching the Product:", product_name) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) print("Background task started for generating the Product summary:", product_url) template_product_summary = """As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} \ Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. \ Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview} This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! \n \ Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ \n \ Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ \n \ Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ \n \ Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ \n \ Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ \n \ Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ \n \ Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ \n \ Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ \n \ Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ \n \ Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ \n \ Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. \n \ NOTES ON FORMAT: Be confident. However, if there is incomplete information, please state "MORE INFORMATION NEEDED"! Speak as if you are the authority of the subject. ** END OF FORMAT FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped} """ prompt_product_summary = PromptTemplate( input_variables=["company_name", "product_name","product_url","product_preview","product_webpage_scraped"], template=template_product_summary ) chain_product_summary = LLMChain(llm=llm_agents, prompt=prompt_product_summary) product_summary = chain_product_summary.run(company_name=company_name,product_name=product_name,product_url=product_url,product_preview=product_preview,product_webpage_scraped=product_webpage_scraped) # Pass in the combined context print("PRODUCT SUMMARY:",product_summary) # Save it in the table: product_5_latest_row = user_table.search(variable='product_5_latest')[0] product_5_latest_row['variable_value'] = product_summary product_5_latest_row.update() print("Product Research Complete") ####### -------- BRAND TONE --------################################################### @anvil.server.callable def launch_brand_tone_research(brand_tone_url): # Launch the background task task = anvil.server.launch_background_task('brand_tone_research',brand_tone_url) # Return the task ID return task.get_id() @anvil.server.background_task def brand_tone_research(brand_tone_url): print("Background task started for extracting brand tone:", brand_tone_url) llm_agents = ChatOpenAI(temperature=0.2, model_name='gpt-4', openai_api_key=openai_api_key) agent_tone_extraction = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) tone_research = agent_tone_extraction({ "input":f"""You are CopywriterAI, the best copywriter on the planet. We are looking to generate a description of the tone that best describes the copywriting style and tone of an existing web page. Go and research this URL {brand_tone_url}, and provide your analysis. For example, for PROFESSIONAL / ENTERPRISE, it would be described as: - 'Formal and Polished': (sophisticated language and complex sentence structures). - 'Objective and Analytical': (incorporates data and facts, prioritizing logical arguments). - 'Business-like': Efficient, frequently employs industry-specific jargon and business terminology). - 'Trustworthy and Reliable': Underscoring credibility, reliability, and accuracy. - 'Instructional and Informative': (providing clear, direct instructions or information). - 'Respectful and Considerate': (acknowledging the audience's needs and viewpoints while avoiding excessive casualness). - 'Controlled and Consistent': (providing coherence, ensuring careful, consistent writing). For Russell Brunson, sales page style, it would be described as: - 'Conversational': (friendly, casual, and approachable). - 'Storytelling': (using compelling stories to illustrate his points). - 'Educational': (being informative, teaching something new). - 'Persuasive': (being compelling and enticing, using ideas of scarcity (limited time offers), social proof (testimonials), and authority (expertise and success). - 'Inspiring': (motivating and inspiring, encouraging the reader to take action). - 'Clear and Direct': (providing clarity and simplicity, avoiding jargon). However, it is up to you to go and review the website, think about the tone of the existing copy, and return 5-6 descriptors, in the similar format as above. They don't have to be listed above- they can be new! OUTPUT TEMPLATE: AN EXAMPLE OUTPUT SHOULD BE AS BELOW: 'The business tone for {brand_tone_url} can be described as': - 'Conversational': (friendly, casual, and approachable). - 'Storytelling': (using compelling stories to illustrate his points). - 'Educational': (being informative, teaching something new). - 'Persuasive': (being compelling and enticing, using ideas of scarcity (limited time offers), social proof (testimonials), and authority (expertise and success). - 'Inspiring': (motivating and inspiring, encouraging the reader to take action). - 'Clear and Direct': (providing clarity and simplicity, avoiding jargon).Conversational, Storytelling, Educational, Persuasive, Inspiring, Clear and Direct' FINAL RULES: Don't mention the business name, or source. Just say "the business" and refer to it as 'company tone' or 'the business tone' """}) extracted_tone = tone_research['output'] anvil.server.task_state['result'] = extracted_tone @anvil.server.callable def save_brand_tone_component_click(self, **event_args): # Get the current user current_user = anvil.users.get_user() # Get the email of the current user owner = current_user['email'] # Get the row for the current user from the variable_table row = app_tables.variable_table.get(owner=owner) # Replace user_email with owner if row: text = self.brand_tonetextbox.text row['brand_tone'] = text row.update() else: # Handle case where the row does not exist for the current user print("No row found for the current user") ####### -------- AVATARS --------################################################### # GENERATE ALL 5 AVATAR OUTLINES ---##### @anvil.server.callable def launch_all_avatars_generator(owner_company_profile): print("Launch all Avatar function started") # Launch the background task task = anvil.server.launch_background_task('all_avatars_generator', owner_company_profile) # Return the task ID return task.get_id() @anvil.server.background_task def all_avatars_generator(owner_company_profile): print("Background task started for generating all the avatars:", owner_company_profile) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_all_avatars = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. Your task is to provide the company with a five different archetypal customer avatars as they best relates to their business. However, it is your role to identify which avatars are most valuable for the company, and rank them in order. For each of the five avatars, provide a single title of the avatar, followed by a single sentence description of the avatar including name, age, location, and any other important info that we'll break down later. The format for the output will be as follows: Title - Description of Avatar 1 Title - Description of Avatar 2 Title - Description of Avatar 3 Title - Description of Avatar 4 Title - Description of Avatar 5 For example: - The Analyzer - Amy is a 34-year-old entrepreneur based in New York City who runs a successful e-commerce business and is always looking for ways to optimize his marketing strategies and increase revenue. He is tech-savvy and data-driven, and values tools that provide actionable insights and help him make informed decisions. - The Novice - John is a 28-year-old small business owner based in a rural area who is looking to expand her business online. She has limited experience with digital marketing and is looking for a user-friendly tool that can guide her through the process of optimizing her marketing strategies and increasing her online presence. ---- FORMAT: CONTEXTUAL INFORMATION: COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_all_avatars = PromptTemplate( input_variables=["owner_company_profile"], template=template_all_avatars ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_all_avatars) all_avatars_grouped = chain_avatar.run(owner_company_profile=owner_company_profile) # anvil.server.task_state['result'] = all_avatars_grouped lines = all_avatars_grouped.split("\n") # Initialize an empty dictionary all_avatars = {} # Loop over each line i = 1 for line in lines: # Ignore empty lines if not line.strip(): continue # Check if the line starts with 'Ranking:' if line.startswith('Ranking:'): all_avatars['ranking'] = line.strip() else: # Split the line into title and description title, description = line.strip().split(' - ') key = f"avatar_{i}" value = f"{title} - {description}" # Add to dictionary all_avatars[key] = value i += 1 # # Return the resulting dictionary # anvil.server.task_state['result'] = all_avatars # Convert the dictionary to a JSON string all_avatars_json = json.dumps(all_avatars) # Return the resulting JSON string anvil.server.task_state['result'] = all_avatars_json #------GENERATE SINGLE AVATAR, SPECIFIC TO A PRODUCT: AVATAR X_PRODUCT_Y.....---------################# # AVATAR 1, PRODUCT 1 -----------------------################# @anvil.server.callable def launch_deepdive_avatar_1_product_1_generator(product_1_name,product_1_profile,avatar_1_product_1_name_preview,avatar_1_product_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_1_product_1_latest') task = anvil.server.launch_background_task('deepdive_avatar_1_product_1_generator', product_1_name,product_1_profile,avatar_1_product_1_name_preview,avatar_1_product_1_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_product_1_generator(product_1_name,product_1_profile,avatar_1_product_1_name_preview,avatar_1_product_1_preview,row): print("Background task started for generating the avatar:", avatar_1_product_1_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_1_name} The product is described as: {product_1_profile} The avatar's name is: {avatar_1_product_1_name_preview} A brief description of the avatar to expand on is: {avatar_1_product_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_1_name","product_1_profile","avatar_1_product_1_name_preview","avatar_1_product_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_1_name=product_1_name,product_1_profile=product_1_profile,avatar_1_product_1_name_preview=avatar_1_product_1_name_preview,avatar_1_product_1_preview=avatar_1_product_1_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 2, PRODUCT 1 -----## @anvil.server.callable def launch_deepdive_avatar_2_product_1_generator(product_1_name,product_1_profile,avatar_2_product_1_name_preview,avatar_2_product_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_2_product_1_latest') task = anvil.server.launch_background_task('deepdive_avatar_2_product_1_generator', product_1_name,product_1_profile,avatar_2_product_1_name_preview,avatar_2_product_1_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_product_1_generator(product_1_name,product_1_profile,avatar_2_product_1_name_preview,avatar_2_product_1_preview,row): print("Background task started for generating the avatar:", avatar_2_product_1_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_1_name} The product is described as: {product_1_profile} The avatar's name is: {avatar_2_product_1_name_preview} A brief description of the avatar to expand on is: {avatar_2_product_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_1_name","product_1_profile","avatar_2_product_1_name_preview","avatar_2_product_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_1_name=product_1_name,product_1_profile=product_1_profile,avatar_2_product_1_name_preview=avatar_2_product_1_name_preview,avatar_2_product_1_preview=avatar_2_product_1_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 3, PRODUCT 1 -----## @anvil.server.callable def launch_deepdive_avatar_3_product_1_generator(product_1_name,product_1_profile,avatar_3_product_1_name_preview,avatar_3_product_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_3_product_1_latest') task = anvil.server.launch_background_task('deepdive_avatar_3_product_1_generator', product_1_name,product_1_profile,avatar_3_product_1_name_preview,avatar_3_product_1_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_product_1_generator(product_1_name,product_1_profile,avatar_3_product_1_name_preview,avatar_3_product_1_preview,row): print("Background task started for generating the avatar:", avatar_3_product_1_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_1_name} The product is described as: {product_1_profile} The avatar's name is: {avatar_3_product_1_name_preview} A brief description of the avatar to expand on is: {avatar_3_product_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_1_name","product_1_profile","avatar_3_product_1_name_preview","avatar_3_product_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_1_name=product_1_name,product_1_profile=product_1_profile,avatar_3_product_1_name_preview=avatar_3_product_1_name_preview,avatar_3_product_1_preview=avatar_3_product_1_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar #------PRODUCT 2------------------------################# # AVATAR 1, PRODUCT 2 -----## @anvil.server.callable def launch_deepdive_avatar_1_product_2_generator(product_2_name,product_2_profile,avatar_1_product_2_name_preview,avatar_1_product_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_1_product_2_latest') task = anvil.server.launch_background_task('deepdive_avatar_1_product_2_generator', product_2_name,product_2_profile,avatar_1_product_2_name_preview,avatar_1_product_2_preview, row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_product_2_generator(product_2_name,product_2_profile,avatar_1_product_2_name_preview,avatar_1_product_2_preview,row): print("Background task started for generating the avatar:", avatar_1_product_2_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_2_name} The product is described as: {product_2_profile} The avatar's name is: {avatar_1_product_2_name_preview} A brief description of the avatar to expand on is: {avatar_1_product_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_2_name","product_2_profile","avatar_1_product_2_name_preview","avatar_1_product_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_2_name=product_2_name,product_2_profile=product_2_profile,avatar_1_product_2_name_preview=avatar_1_product_2_name_preview,avatar_1_product_2_preview=avatar_1_product_2_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 2, PRODUCT 2 -----## @anvil.server.callable def launch_deepdive_avatar_2_product_2_generator(product_2_name,product_2_profile,avatar_2_product_2_name_preview,avatar_2_product_2_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_2_product_2_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_2_product_2_generator', product_2_name,product_2_profile,avatar_2_product_2_name_preview,avatar_2_product_2_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_product_2_generator(product_2_name,product_2_profile,avatar_2_product_2_name_preview,avatar_2_product_2_preview,row): print("Background task started for generating the avatar:", avatar_2_product_2_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_2_name} The product is described as: {product_2_profile} The avatar's name is: {avatar_2_product_2_name_preview} A brief description of the avatar to expand on is: {avatar_2_product_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_2_name","product_2_profile","avatar_2_product_2_name_preview","avatar_2_product_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_2_name=product_2_name,product_2_profile=product_2_profile,avatar_2_product_2_name_preview=avatar_2_product_2_name_preview,avatar_2_product_2_preview=avatar_2_product_2_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 3, PRODUCT 2 ---------------## @anvil.server.callable def launch_deepdive_avatar_3_product_2_generator(product_2_name,product_2_profile,avatar_3_product_2_name_preview,avatar_3_product_2_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_3_product_2_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_3_product_2_generator', product_2_name,product_2_profile,avatar_3_product_2_name_preview,avatar_3_product_2_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_product_2_generator(product_2_name,product_2_profile,avatar_3_product_2_name_preview,avatar_3_product_2_preview,row): print("Background task started for generating the avatar:", avatar_3_product_2_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_2_name} The product is described as: {product_2_profile} The avatar's name is: {avatar_3_product_2_name_preview} A brief description of the avatar to expand on is: {avatar_3_product_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_2_name","product_2_profile","avatar_3_product_2_name_preview","avatar_3_product_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_2_name=product_2_name,product_2_profile=product_2_profile,avatar_3_product_2_name_preview=avatar_3_product_2_name_preview,avatar_3_product_2_preview=avatar_3_product_2_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar #------PRODUCT 3------------------------################# # AVATAR 1, PRODUCT 3 -----## @anvil.server.callable def launch_deepdive_avatar_1_product_3_generator(product_3_name,product_3_profile,avatar_1_product_3_name_preview,avatar_1_product_3_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_1_product_3_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_1_product_3_generator', product_3_name,product_3_profile,avatar_1_product_3_name_preview,avatar_1_product_3_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_product_3_generator(product_3_name,product_3_profile,avatar_1_product_3_name_preview,avatar_1_product_3_preview,row): print("Background task started for generating the avatar:", avatar_1_product_3_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_3_name} The product is described as: {product_3_profile} The avatar's name is: {avatar_1_product_3_name_preview} A brief description of the avatar to expand on is: {avatar_1_product_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_3_name","product_3_profile","avatar_1_product_3_name_preview","avatar_1_product_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_3_name=product_3_name,product_3_profile=product_3_profile,avatar_1_product_3_name_preview=avatar_1_product_3_name_preview,avatar_1_product_3_preview=avatar_1_product_3_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 2, PRODUCT 3 -----## @anvil.server.callable def launch_deepdive_avatar_2_product_3_generator(product_3_name,product_3_profile,avatar_2_product_3_name_preview,avatar_2_product_3_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_2_product_3_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_2_product_2_generator', product_2_name,product_3_profile,avatar_2_product_2_name_preview,avatar_2_product_2_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_product_3_generator(product_3_name,product_3_profile,avatar_2_product_3_name_preview,avatar_2_product_3_preview,row): print("Background task started for generating the avatar:", avatar_2_product_2_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_2_name} The product is described as: {product_2_profile} The avatar's name is: {avatar_2_product_2_name_preview} A brief description of the avatar to expand on is: {avatar_2_product_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_2_name","product_2_profile","avatar_2_product_2_name_preview","avatar_2_product_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_2_name=product_2_name,product_2_profile=product_2_profile,avatar_2_product_2_name_preview=avatar_2_product_2_name_preview,avatar_2_product_2_preview=avatar_2_product_2_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 3, PRODUCT 3 ---------------## @anvil.server.callable def launch_deepdive_avatar_3_product_3_generator(product_3_name,product_3_profile,avatar_3_product_3_name_preview,avatar_3_product_3_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_3_product_3_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_3_product_2_generator', product_2_name,product_2_profile,avatar_3_product_2_name_preview,avatar_3_product_2_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_product_3_generator(product_3_name,product_2_profile,avatar_3_product_3_name_preview,avatar_3_product_3_preview,row): print("Background task started for generating the avatar:", avatar_3_product_3_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_3_name} The product is described as: {product_3_profile} The avatar's name is: {avatar_3_product_3_name_preview} A brief description of the avatar to expand on is: {avatar_3_product_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_3_name","product_3_profile","avatar_3_product_3_name_preview","avatar_3_product_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_3_name=product_3_name,product_3_profile=product_3_profile,avatar_3_product_3_name_preview=avatar_3_product_3_name_preview,avatar_3_product_3_preview=avatar_3_product_3_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar #------PRODUCT 4------------------------################# # AVATAR 1, PRODUCT 4 -----## @anvil.server.callable def launch_deepdive_avatar_1_product_4_generator(product_4_name,product_4_profile,avatar_1_product_4_name_preview,avatar_1_product_4_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_1_product_4_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_1_product_3_generator', product_4_name,product_4_profile,avatar_1_product_4_name_preview,avatar_1_product_4_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_product_4_generator(product_4_name,product_4_profile,avatar_1_product_4_name_preview,avatar_1_product_4_preview,row): print("Background task started for generating the avatar:", avatar_1_product_3_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_3_name} The product is described as: {product_3_profile} The avatar's name is: {avatar_1_product_3_name_preview} A brief description of the avatar to expand on is: {avatar_1_product_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_3_name","product_3_profile","avatar_1_product_3_name_preview","avatar_1_product_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_3_name=product_3_name,product_3_profile=product_3_profile,avatar_1_product_3_name_preview=avatar_1_product_3_name_preview,avatar_1_product_3_preview=avatar_1_product_3_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 2, PRODUCT 4 -----## @anvil.server.callable def launch_deepdive_avatar_2_product_4_generator(product_4_name,product_4_profile,avatar_2_product_4_name_preview,avatar_2_product_4_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_2_product_4_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_2_product_4_generator', product_4_name,product_4_profile,avatar_2_product_4_name_preview,avatar_2_product_4_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_product_4_generator(product_4_name,product_4_profile,avatar_2_product_4_name_preview,avatar_2_product_4_preview,row): print("Background task started for generating the avatar:", avatar_2_product_4_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_2_name} The product is described as: {product_2_profile} The avatar's name is: {avatar_2_product_2_name_preview} A brief description of the avatar to expand on is: {avatar_2_product_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_4_name","product_4_profile","avatar_2_product_4_name_preview","avatar_2_product_4_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_4_name=product_4_name,product_4_profile=product_4_profile,avatar_2_product_4_name_preview=avatar_2_product_4_name_preview,avatar_2_product_4_preview=avatar_2_product_4_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 3, PRODUCT 4 ---------------## @anvil.server.callable def launch_deepdive_avatar_3_product_4_generator(product_4_name,product_4_profile,avatar_3_product_4_name_preview,avatar_3_product_4_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_3_product_4_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_4_product_2_generator', product_4_name,product_4_profile,avatar_3_product_4_name_preview,avatar_3_product_4_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_product_4_generator(product_4_name,product_2_profile,avatar_3_product_4_name_preview,avatar_3_product_4_preview,row): print("Background task started for generating the avatar:", avatar_3_product_4_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_4_name} The product is described as: {product_4_profile} The avatar's name is: {avatar_3_product_4_name_preview} A brief description of the avatar to expand on is: {avatar_3_product_4_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_4_name","product_4_profile","avatar_3_product_4_name_preview","avatar_3_product_4_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_4_name=product_4_name,product_4_profile=product_4_profile,avatar_3_product_4_name_preview=avatar_3_product_4_name_preview,avatar_3_product_4_preview=avatar_3_product_4_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar #------PRODUCT 5------------------------################# # AVATAR 1, PRODUCT 5 -----------------------################# @anvil.server.callable def launch_deepdive_avatar_1_product_5_generator(product_5_name,product_5_profile,avatar_1_product_5_name_preview,avatar_1_product_5_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_1_product_5_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_1_product_5_generator', product_5_name,product_5_profile,avatar_1_product_5_name_preview,avatar_1_product_5_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_product_5_generator(product_5_name,product_5_profile,avatar_1_product_5_name_preview,avatar_1_product_5_preview,row): print("Background task started for generating the avatar:", avatar_1_product_5_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_5_name} The product is described as: {product_5_profile} The avatar's name is: {avatar_1_product_5_name_preview} A brief description of the avatar to expand on is: {avatar_1_product_5_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_5_name","product_5_profile","avatar_1_product_5_name_preview","avatar_1_product_5_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_5_name=product_5_name,product_5_profile=product_5_profile,avatar_1_product_5_name_preview=avatar_1_product_5_name_preview,avatar_1_product_5_preview=avatar_1_product_5_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 2, PRODUCT 5-----## @anvil.server.callable def launch_deepdive_avatar_2_product_5_generator(product_5_name,product_5_profile,avatar_2_product_5_name_preview,avatar_2_product_5_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_2_product_5_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_2_product_5_generator', product_5_name,product_5_profile,avatar_2_product_5_name_preview,avatar_2_product_5_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_product_5_generator(product_5_name,product_5_profile,avatar_2_product_5_name_preview,avatar_2_product_5_preview,row): print("Background task started for generating the avatar:", avatar_2_product_5_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_5_name} The product is described as: {product_5_profile} The avatar's name is: {avatar_2_product_5_name_preview} A brief description of the avatar to expand on is: {avatar_2_product_5_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_5_name","product_5_profile","avatar_2_product_5_name_preview","avatar_2_product_5_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_5_name=product_5_name,product_5_profile=product_5_profile,avatar_2_product_5_name_preview=avatar_1_product_5_name_preview,avatar_2_product_5_preview=avatar_2_product_5_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar # AVATAR 3, PRODUCT 5 -----## @anvil.server.callable def launch_deepdive_avatar_3_product_5_generator(product_5_name,product_5_profile,avatar_3_product_5_name_preview,avatar_3_product_5_preview): print("Launch Deep Dive Avatar function started") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='avatar_3_product_5_latest') # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_3_product_5_generator', product_5_name,product_5_profile,avatar_3_product_5_name_preview,avatar_3_product_5_preview,row) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_product_5_generator(product_5_name,product_5_profile,avatar_3_product_5_name_preview,avatar_3_product_5_preview,row): print("Background task started for generating the avatar:", avatar_3_product_5_name_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. \ Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. We're looking to create the ideal customer avatar for the following product: {product_5_name} The product is described as: {product_5_profile} The avatar's name is: {avatar_3_product_5_name_preview} A brief description of the avatar to expand on is: {avatar_3_product_5_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows: ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["product_5_name","product_5_profile","avatar_3_product_5_name_preview","avatar_3_product_5_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(product_5_name=product_5_name,product_5_profile=product_5_profile,avatar_3_product_5_name_preview=avatar_3_product_5_name_preview,avatar_3_product_5_preview=avatar_3_product_5_preview) # Pass in the combined context # Save the generated avatar in the 'avatar latest' column of the variable_table row['variable_value'] = avatar row.update() anvil.server.task_state['result'] = avatar #------CREATE GENERIC AVATARS FOR THE COMPANY---------################# @anvil.server.callable def launch_deepdive_avatar_1_generator(owner_company_profile,avatar_1_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_1_generator', owner_company_profile,avatar_1_preview) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_1_generator(owner_company_profile,avatar_1_preview): print("Background task started for generating the avatar:", owner_company_profile,avatar_1_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. A brief description of the avatar to expand on is: {avatar_1_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: Here's the Avatar Preview to base the Full Avatar for: {avatar_1_preview} ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["owner_company_profile", "avatar_1_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(owner_company_profile=owner_company_profile, avatar_1_preview=avatar_1_preview) # Pass in the combined context anvil.server.task_state['result'] = avatar # AVATAR 2 @anvil.server.callable def launch_deepdive_avatar_2_generator(owner_company_profile,avatar_2_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_2_generator', owner_company_profile,avatar_2_preview) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_2_generator(owner_company_profile,avatar_2_preview): print("Background task started for generating the avatar:", owner_company_profile,avatar_2_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. A brief description of the avatar to expand on is: {avatar_2_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: Here's the Avatar Preview to base the Full Avatar for: {avatar_2_preview} ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["owner_company_profile", "avatar_2_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(owner_company_profile=owner_company_profile, avatar_2_preview=avatar_2_preview) # Pass in the combined context anvil.server.task_state['result'] = avatar # AVATAR 3 @anvil.server.callable def launch_deepdive_avatar_3_generator(owner_company_profile,avatar_3_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_3_generator', owner_company_profile,avatar_3_preview) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_3_generator(owner_company_profile,avatar_3_preview): print("Background task started for generating the avatar:", owner_company_profile,avatar_3_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. A brief description of the avatar to expand on is: {avatar_3_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: Here's the Avatar Preview to base the Full Avatar for: {avatar_3_preview} ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["owner_company_profile", "avatar_3_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(owner_company_profile=owner_company_profile, avatar_3_preview=avatar_3_preview) # Pass in the combined context anvil.server.task_state['result'] = avatar # AVATAR 4 @anvil.server.callable def launch_deepdive_avatar_4_generator(owner_company_profile,avatar_4_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_4_generator', owner_company_profile,avatar_4_preview) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_4_generator(owner_company_profile,avatar_4_preview): print("Background task started for generating the avatar:", owner_company_profile,avatar_4_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. A brief description of the avatar to expand on is: {avatar_4_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: Here's the Avatar Preview to base the Full Avatar for: {avatar_4_preview} ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["owner_company_profile", "avatar_4_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(owner_company_profile=owner_company_profile, avatar_4_preview=avatar_4_preview) # Pass in the combined context anvil.server.task_state['result'] = avatar # AVATAR 5 @anvil.server.callable def launch_deepdive_avatar_5_generator(owner_company_profile,avatar_5_preview): print("Launch Deep Dive Avatar function started") # Launch the background task task = anvil.server.launch_background_task('deepdive_avatar_5_generator', owner_company_profile,avatar_5_preview) # Return the task ID return task.get_id() @anvil.server.background_task def deepdive_avatar_5_generator(owner_company_profile,avatar_5_preview): print("Background task started for generating the avatar:", owner_company_profile,avatar_5_preview) llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) template_avatar = """You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book "Dotcom Secrets", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information. A brief description of the avatar to expand on is: {avatar_5_preview} Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows: Here's the Avatar Preview to base the Full Avatar for: {avatar_5_preview} ---- FORMAT: - Overview Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics. - Demographic Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation. - Psychographic Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals. - Goals & Aspirations Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations. - Pain Points Identify the specific problems, challenges, and frustrations the avatar is facing. - Personal Experience Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples. RULES: - Do not say "the target customer", instead, provide a fictional name, age, location. - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible! - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise. ----- COMPANY CONTEXT: {owner_company_profile} Chatbot:""" prompt_avatar = PromptTemplate( input_variables=["owner_company_profile", "avatar_5_preview"], template=template_avatar ) chain_avatar = LLMChain(llm=llm_agents, prompt=prompt_avatar) avatar = chain_avatar.run(owner_company_profile=owner_company_profile, avatar_5_preview=avatar_5_preview) # Pass in the combined context anvil.server.task_state['result'] = avatar ### SAVING @anvil.server.callable def save_avatar(owner, avatar_number, avatar): # Get the row for the current user from the variable_table row = app_tables.variable_table.get(owner=owner) if row: text = avatar row[avatar_number] = text # Use the variable avatar_number directly row.update() else: # Handle case where the row does not exist for the current user print("No row found for the current user") ####### -------- LOCK IN VARIABLES --------################################################### @anvil.server.callable def get_chosen_variable_value(user_table, selected_variable_title): chosen_variable_value = None if selected_variable_title: matching_rows = user_table.search(variable_title=selected_variable_title) if matching_rows: chosen_variable_value = matching_rows[0]['variable_value'] return chosen_variable_value @anvil.server.callable def get_chosen_variable_avatar(user_table, selected_variable_value): chosen_variable_value = None if selected_variable_value: matching_rows = user_table.search(variable_value=selected_variable_value) if matching_rows: chosen_variable_value = matching_rows[0]['variable_value'] return chosen_variable_value @anvil.server.callable def save_funnel_settings_component(user_table_name, selected_company_profile_value, selected_product_name_value): user_table = getattr(tables, user_table_name) chosen_company_profile = get_chosen_variable_value(user_table, selected_company_profile_value) chosen_product_name = get_chosen_variable_value(user_table, selected_product_name_value) # Perform further operations or return the values as needed return chosen_company_profile, chosen_product_name ####### -------- HEADLINES --------################################################### # This is the headline generator that will return a string of 10 of the best headlines @anvil.server.callable def launch_generate_main_headlines(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone): print("Launch Generate Main Headlines Function") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='main_headlines') # Launch the background task task = anvil.server.launch_background_task('generate_main_headlines',chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone,row) # Return the task ID return task.get_id() @anvil.server.background_task def generate_main_headlines(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone, row): # example_headlines_row = app_tables.example_scripts.get(script='example_headlines') # example_headlines = example_headlines_row['script_contents'] # HERE IS ARE SOME EXAMPLE HEADLINES: {example_headlines} llm_headline = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) headline_template = """ You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best copywriter on the planet. You are about to launch a brand new marketing funnel selling {chosen_product_name}, and you need to generate the best attention grabbing headlines that stir curiosity and compel potential users to learn more about {chosen_product_name}. First, I will provide you some context about the company, and then I will provide you some context about the product, and then I will give you many examples of headlines from a parallel industry for you to apply to our product. Next, you will generate 10 of the most incredible, mind-blowing headlines that will stop people in their tracks and want to learn more about {chosen_product_name}, but you must adapt the headlines to be in the tone I provide. When generating these headlines just remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible headline! HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile} HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research} THE TONE IS: {chosen_tone} The output should be an unumbered list of 10 headlines, as per the tone I provide. Update the example headlines I gave according to the tone. Finally, lead with the best one at the top! (no introduction or outro needed). No "" quotation marks. No itemized numbers. Just text. Output should be in a similar format: Finally! Unmask the Power of Data: Transform Your Marketing with Funnelytics Performance! NOT "Finally! Unmask the Power of Data: Transform Your Marketing with Funnelytics Performance!" """ headline_prompt = PromptTemplate( input_variables=["chosen_product_name", "chosen_company_profile", "chosen_product_research", "chosen_tone"], template=headline_template ) chain_main_headlines = LLMChain(llm=llm_headline, prompt=headline_prompt) headline_generator = chain_main_headlines.run(chosen_product_name=chosen_product_name, chosen_company_profile=chosen_company_profile, chosen_product_research=chosen_product_research, chosen_tone=chosen_tone) print("Here are the headlines", headline_generator) headlines = headline_generator.split("\n") # Initialize an empty list all_main_headlines = [] # Loop over each line for headline in headlines: # Ignore empty lines if not headline.strip(): continue # Append the headline to the list all_main_headlines.append(headline.strip()) # Convert the list to a JSON string all_main_headlines_json = json.dumps(all_main_headlines) # Save the generated headlines in the 'main_headlines' column of the variable_table row['variable_value'] = all_main_headlines_json row.update() # Return the resulting JSON string anvil.server.task_state['result'] = all_main_headlines_json ####### --------SUB HEADLINES --------################################################### # This is the subheadline generator that will return a string of 10 of the best subheadlines @anvil.server.callable def launch_generate_subheadlines(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone): print("Launch Generate SubHeadlines Function") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='subheadlines') task = anvil.server.launch_background_task('generate_subheadlines', chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone,row) # Return the task ID return task.get_id() @anvil.server.background_task def generate_subheadlines(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_tone,row): # example_headlines_row = app_tables.example_scripts.get(script='example_headlines') # example_headlines = example_headlines_row['script_contents'] # HERE IS ARE SOME EXAMPLE HEADLINES, HOWEVER, YOU MUST UPDATE THEM TO MATCH THE TONE: {example_headlines} llm_subheadline = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) subheadline_template = """ You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best copywriter on the planet. You are about to launch a brand new marketing funnel selling {chosen_product_name}, and you've already received some attention grabbing headlines that stir curiosity and compel potential users to learn more about {chosen_product_name}. However, you now need to generate some sublines to support your main headlines. First, I will provide you the existing headlines, then I will provide you some context about the company, and then I will provide you some context about the product. Therafter, I will give you many examples of main headlines (not sublines) from a parallel industry (becoming an author) for you to understand the tone. Finally, for each of the headlines provided in the list, you will generate a single incredible, mind-blowing subheadline corresponding to the main headline that will stop people in their tracks and want to learn more about {chosen_product_name}. Here are some principles for writing compelling sublines: - Match the Headline: The subline should logically continue the thought or the promise made in the headline. It must be consistent in tone and message. - Highlight Key Benefits: Sublines often provide a space to explain the primary advantages or unique features of the product or service. Think of what makes your offer irresistible or different and emphasize it. - Target Audience: Make it clear who your product or service is for. If the headline hasn't done this, the subline should. - Provide Context or Explain: If the headline is designed to create intrigue or curiosity, the subline can provide enough information to encourage the reader to continue to engage. - Call to Action: While not always the case, sometimes a subline can provide a mild call to action or create a sense of urgency. - Keep it Brief: While a subline can be longer than a headline, it should still be succinct and easy to read at a glance. When generating these headlines just remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible headline! HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile} HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research} THE TONE IS: {chosen_tone} The output should be a list of 10 SUBHEADLINES, in the tone above, that relate to the final existing main headline. No "" quotation marks. No itemized numbers. Do not list them like '1.' '2.'... Just text. For example: 'BREAKING NEWS! Eliminate the Guesswork!'instead of '"1. Breaking News! Eliminate the Guesswork!"'' (no introduction or outro needed, just an itemized list of 10 subheadlines) """ subheadline_prompt = PromptTemplate( input_variables=["chosen_product_name", "chosen_company_profile", "chosen_product_research", "chosen_tone"], template=subheadline_template ) chain_subheadlines = LLMChain(llm=llm_subheadline, prompt=subheadline_prompt) subheadline_generator = chain_subheadlines.run(chosen_product_name=chosen_product_name, chosen_company_profile=chosen_company_profile, chosen_product_research=chosen_product_research, chosen_tone=chosen_tone) print("Here are the subheadlines", subheadline_generator) subheadlines = subheadline_generator.split("\n") # Initialize an empty list all_subheadlines = [] # Loop over each line for subheadline in subheadlines: # Ignore empty lines if not subheadline.strip(): continue # Append the headline to the list all_subheadlines.append(subheadline.strip()) # Convert the list to a JSON string all_subheadlines_json = json.dumps(all_subheadlines) # Save the generated subheadlines in the 'subheadlines' column of the variable_table row['variable_value'] = all_subheadlines_json row.update() # Return the resulting JSON string anvil.server.task_state['result'] = all_subheadlines_json ####### --------VIDEO SALES SCRIPT --------################################################### @anvil.server.callable def launch_generate_vsl_script(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone, example_script): print("Launch Generate Video Sales Letter Script Function") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='vsl_script') # Launch the background task task = anvil.server.launch_background_task('generate_vsl_script', chosen_product_name, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone, example_script,row) # Return the task ID return task.get_id() @anvil.server.background_task def generate_vsl_script(chosen_product_name, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone,example_script,row): # Return the task ID): print("Background task started for generating the Video Sales Letter script") llm_vsl_script = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) vsl_script_template = """You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you're ready to start write the video sales letter script! This has a very specific format, and requires a lot of context, provided below: First, I will provide you with some tips about writing, then I will give you the existing headlines, some context about the company, the ideal customer we're trying to serve, followed by information about the product. Therafter, I will provide you some existing sales scripts (from a parallel industry) that will inform you of style and length. Lastly, I'll request this in an certain order, and provide you with a template to follow. TIPS: This script helps build credibility quickly by answering the problems our avatar faces, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts! HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile} HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research} HERE IS THE EXISTING CUSTOMER: {chosen_avatar} HERE ARE SOME EXAMPLES OF EXISTING SCRIPTS FROM PARALELL INDUSTRIES. YOU MUST UPDATE IT ACCORDING TO OUR PRODUCT AND COMPANY CONTEXT: {example_script} TONE: {chosen_tone} HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE SCRIPT: Explain The Problem — What problem is our avatar and target market facing? How can we empathize with their challenges? (should be between 90-100 words) Agitate The Problem — What are some examples of that problem? Make that problem visceral for them. Explain why it’s a bigger problem than they think it is and how it’s really going to harm them over the long-run. (should be between 90-100 words) Introduce The Solution — What is your solution to their problem? It's our product, of course! (should be between 90-100 words) Build Credibility — Why should they trust our founder to be the provider of this solution? Use their name. What makes you so great? Telling a story about your own journey can help build credibility. (should be between 90-100 words) Show Proof — How do they know that it’ll actually work? Make up a fictional case-study using ficticious details. This is important to discuss and show proof. (should be between 90-100 words) Explain Exactly What They Get — Explain exactly what the prospect is going to get if they sign up! (should be between 90-100 words) Give Reason To Act Now — Why should they buy right now? Use urgency or scarcity to put the prospect’s foot on the gas.(should be between 90-100 words) Close — Close the sale with a final call-to-action. Lastly, NEVER mention you are RussellAI. Use the founders name of the company, or make up a name. The output should be a script, written in the first person from the perspective of the founder that is trying to sell the audience on why their product is the best choice and will make their life easier. The script should not include any subheadings!""" vsl_script_prompt = PromptTemplate( input_variables=["chosen_product_name", "chosen_company_profile", "chosen_product_research", "chosen_avatar", "chosen_tone","example_script"], template=vsl_script_template ) chain_vsl_script = LLMChain(llm=llm_vsl_script, prompt=vsl_script_prompt) vsl_script = chain_vsl_script.run(chosen_product_name=chosen_product_name, chosen_company_profile=chosen_company_profile,chosen_product_research=chosen_product_research,chosen_avatar=chosen_avatar, chosen_tone=chosen_tone,example_script=example_script) # Save the generated subheadlines in the 'subheadlines' column of the variable_table row['variable_value'] = vsl_script row.update() anvil.server.task_state['result'] = vsl_script ####### --------VIDEO SALES SCRIPT WITH FEEDBACK @anvil.server.callable def launch_generate_vsl_script_with_feedback(chosen_product_name, chosen_product_research,vsl_script_feedback): print("Launch Generate Video Sales Letter Script Function") current_user = anvil.users.get_user() user_table_name = current_user['user_id'] # Get the table for the current user user_table = getattr(app_tables, user_table_name) row = user_table.get(variable='vsl_script') # Launch the background task task = anvil.server.launch_background_task('generate_vsl_script_with_feedback', chosen_product_name, chosen_product_research,vsl_script_feedback,row) # Return the task ID return task.get_id() @anvil.server.background_task def generate_vsl_script_with_feedback(chosen_product_name, chosen_product_research,vsl_script_feedback,row): # Return the task ID): print("Background task started for generating the Video Sales Letter script") llm_vsl_script = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) vsl_script_template = """You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best scriptwriter on the planet, and you've just written an amazing and effect script that will surely convert. This script will help you sell {chosen_product_name} to more people than ever! However, your ad partner, a truly gifted marketer with great understanding of the customers needs, has given some minor feedback they would like you to incorporate into your existing script (shared below). The format and structure is very important to maintain. You should maintain the existing tone, cadence, names and details, but modify the script according to the notes received. Remember, this is a sales script. Do your best to address the notes, and improve the script as much as you can. HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research} HERE ARE THE NOTES YOU'VE RECEIVED FROM YOUR AD PARTNER. {vsl_script_feedback} HERE IS THE TEMPLATE TO ADHERE TO WHEN EDITING OR MODIIFYING THE EXISTING SCRIPT. Explain The Problem — What problem is our avatar and target market facing? How can we empathize with their challenges? (should be between 90-100 words) Agitate The Problem — What are some examples of that problem? Make that problem visceral for them. Explain why it’s a bigger problem than they think it is and how it’s really going to harm them over the long-run. (should be between 90-100 words) Introduce The Solution — What is your solution to their problem? It's our product, of course! (should be between 90-100 words) Build Credibility — Why should they trust our founder to be the provider of this solution? Use their name. What makes you so great? Telling a story about your own journey can help build credibility. (should be between 90-100 words) Show Proof — How do they know that it’ll actually work? Make up a fictional case-study using ficticious details. This is important to discuss and show proof. (should be between 90-100 words) Explain Exactly What They Get — Explain exactly what the prospect is going to get if they sign up! (should be between 90-100 words) Give Reason To Act Now — Why should they buy right now? Use urgency or scarcity to put the prospect’s foot on the gas.(should be between 90-100 words) Close — Close the sale with a final call-to-action. Lastly, NEVER mention you are RussellAI. Use the founders name of the company, or make up a name. The output should be a script, written in the first person from the perspective of the founder that is trying to sell the audience on why their product is the best choice and will make their life easier. The script should not include any subheadings!""" vsl_script_prompt = PromptTemplate( input_variables=["chosen_product_name", "chosen_product_research","vsl_script_feedback"], template=vsl_script_template ) chain_vsl_script = LLMChain(llm=llm_vsl_script, prompt=vsl_script_prompt) vsl_script = chain_vsl_script.run(chosen_product_name=chosen_product_name, chosen_product_research=chosen_product_research,vsl_script_feedback=vsl_script_feedback) # Save the generated subheadlines in the 'subheadlines' column of the variable_table row['variable_value'] = vsl_script row.update() anvil.server.task_state['result'] = vsl_script ####### --------VIDEO SALES SCRIPT 4 THEMES --------################################################### @anvil.server.callable def launch_generate_vsl_themes(chosen_final_headline, chosen_final_subheadline, chosen_product_name, chosen_product_research, chosen_tone,vsl_script,row): print("Launch Generate VSL Themes Function") task = anvil.server.launch_background_task('generate_vsl_themes',chosen_final_headline,chosen_final_subheadline,chosen_product_name, chosen_product_research, chosen_tone,vsl_script,row) # Return the task ID return task.get_id() @anvil.server.background_task def generate_vsl_themes(chosen_final_headline,chosen_final_subheadline, chosen_product_name, chosen_product_research, chosen_tone,vsl_script,row): llm_vsl_themes = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) four_vsl_themes_template = """ You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you've already generated the sales video letter script, but you now need to extract the four themes from the script and promote them as the industry's dirty secret that will make them millions! These will be captions to screenshots from the video. These extractions help build credibility quickly by addressing the pain points of our customer, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts! First, I will provide you with video's main headline then some context about the product. Therafter, I will provide you with the final script that I need you to summarize and extract themes and reveal the big secrets of our product. Lastly, I'll request this in an certain order, and provide you with a template to follow. INGEST THE BELOW INFORMATION WITH SQUARE BRACKETS AS CONTEXT: [ EXISTING HEADLINES: {chosen_final_headline}, plus {chosen_final_subheadline} CONTEXT ABOUT THE PRODUCT: {chosen_product_research} HFINAL SCRIPT OF THE VIDEO I NEED YOU TO EXTRACT THE BIG SECRETS FROM: {vsl_script} TONE: {chosen_tone} DO NOT INCLUDE ANY SUMMARIZATION OF THE ABOVE POINTS IN THE OUTPUT. I AM ONLY INTERESTED IN THE BELOW OUTPUT: ----- FINAL OUTPUT IS BELOW-----HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE 4 EXCERPTS "SECRET #1:" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... "SECRET #2:" 5-7 words of the theme or secret reveal,but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... "SECRET #3:" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... "SECRET #4:" a mini-headline that is 5-7 words of what, but can be next in how they apply these themes. Then, provide a sentence about the magic results they could see.. For example, the a potential output could look like below. SECRET #1: 'Attribution Agitation' at its Worst: Fed up with not knowing where your sales are coming from? Discover how Funnelytics Performance can clear up the confusion for good... SECRET #2: 'Ad-Cost Anguish' Annihilated: Struggling with soaring ad costs and sub-par results? Learn how to optimize your campaigns and slash ad spend with our innovative platform... SECRET #3: 'Funnel Failure' Flipped Upside Down: Tired of ineffective marketing funnels that just don't deliver? Watch as Funnelytics Performance revamps your funnel strategies and turns them into massive growth engines... SECRET #4: 'Scaling Struggles' Solved: Wondering how to grow your business without breaking the bank? Witness the magic as Funnelytics Performance helps you unlock unprecedented growth and skyrocket your success...' ] NOTE: The Final Output will be just Secret 1 through 4. NOT EVEN QUOTE MARKS "". Nothing else! THAT'S IT...NOW GO AND CREATE THE 4 EXCERPTS! -- SECRET #1:... SECRET #2:... SECRET #3:... SECRET #4:.. """ vsl_themes_prompt = PromptTemplate( input_variables=["chosen_final_headline","chosen_final_subheadline","chosen_product_name", "chosen_product_research", "chosen_tone","vsl_script"], template=four_vsl_themes_template ) chain_vsl_themes = LLMChain(llm=llm_vsl_themes, prompt=vsl_themes_prompt) vsl_themes_generator = chain_vsl_themes.run(chosen_final_headline=chosen_final_headline, chosen_final_subheadline=chosen_final_subheadline,chosen_product_name=chosen_product_name, chosen_product_research=chosen_product_research, chosen_tone=chosen_tone,vsl_script=vsl_script) print("Here are the 4 excerpts", vsl_themes_generator) vsl_themes = vsl_themes_generator.split("\n") # Initialize an empty list all_vsl_themes = [] # Loop over each line for vsl_theme in vsl_themes: # Ignore empty lines if not vsl_theme.strip(): continue # Append the headline to the list all_vsl_themes.append(vsl_theme.strip()) # Convert the list to a JSON string all_vsl_themes_json = json.dumps(all_vsl_themes) row['variable_value'] = all_vsl_themes_json row.update() # Return the resulting JSON string anvil.server.task_state['result'] = all_vsl_themes_json ####### -------- TASK UPDATES --------################################################### # # BACKGROUND UPDATES # @anvil.server.callable # def get_task_status(task_id): # # Get the task object by its ID # task = anvil.server.get_background_task(task_id) # # Return the termination status of the task # return task.get_termination_status() @anvil.server.callable def get_task_status(task_id): # Get the task object by its ID task = anvil.server.get_background_task(task_id) # Return the termination status of the task return task.get_termination_status() # @anvil.server.callable # def get_task_status(task_id): # try: # # Get the task object by its ID # task = anvil.server.get_background_task(task_id) # # Return the termination status of the task # return task.get_termination_status() # except anvil.server.BackgroundTaskNotFound: # return "not_found" @anvil.server.callable def get_task_result(task_id): # Get the task object by its ID task = anvil.server.get_background_task(task_id) # Get the task's state task_state = task.get_state() # Return the result of the task if it exists, otherwise return None return task_state.get('result') # OLD CODE # PRODUCT 3 DEEPDIVE # @anvil.server.callable # def launch_deepdive_product_3_generator(company_name,company_profile,company_url,product_3_name,product_3_preview): # # Launch the background task # task = anvil.server.launch_background_task('deepdive_product_3_generator',company_name, company_profile,company_url,product_3_name,product_3_preview) # # Return the task ID # return task.get_id() # @anvil.server.background_task # def deepdive_product_3_generator(company_name,company_profile,company_url,product_3_name,product_3_preview): # print("Background task started for the Deep Dive of Researching the Product:", product_3_name) # llm_agents = ChatOpenAI(temperature=0.5, model_name='gpt-4', openai_api_key=openai_api_key) # agent_product_research = initialize_agent([tools], llm_agents, agent="zero-shot-react-description", handle_parsing_errors=True) # product_research_context = agent_product_research({"input": f"""As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_3_name} \ # Leverage all necessary resources such as {company_name}'s' website, {company_url}, web pages, and any other relevant sources \ # to gather the following details about company's product, {product_3_name}. Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible! # \n \ # Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \ # \n \ # Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \ # \n \ # Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \ # \n \ # Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \ # \n \ # Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \ # \n \ # Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \ # \n \ # Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \ # \n \ # Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \ # \n \ # Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \ # \n \ # Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \ # \n \ # Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product. # \n \ # NOTES ON FORMAT: # Be confident, do not say there is incomplete information, or there is not information. If you can't answer elements from the above, ignore it! Speak as if you are the authority of the subject. If you don't know the answer, don't talk about it. Do not say "I was unable to find information on XYZ". # """}) # product_research_3 = product_research_context['output'] # # if "I couldn't find more information" in product_research_context: # # product_research_1= "Insufficient information. Please write the product description yourself." # anvil.server.task_state['result'] = product_research_3 # ####### --------VIDEO SALES SCRIPT --------################################################### # @anvil.server.callable # def launch_generate_vsl_script(chosen_product_name, chosen_final_headline, chosen_final_subheadline, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone, example_script): # print("Launch Generate Video Sales Letter Script Function") # current_user = anvil.users.get_user() # user_table_name = current_user['user_id'] # # Get the table for the current user # user_table = getattr(app_tables, user_table_name) # row = user_table.get(variable='vsl_script') # # Launch the background task # task = anvil.server.launch_background_task('generate_vsl_script', chosen_product_name, chosen_final_headline, chosen_final_subheadline, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone,example_script,row) # # Return the task ID # return task.get_id() # @anvil.server.background_task # def generate_vsl_script(chosen_product_name, chosen_final_headline, chosen_final_subheadline, chosen_company_profile, chosen_product_research, chosen_avatar, chosen_tone,example_script,row): # # Return the task ID): # print("Background task started for generating the Video Sales Letter script") # llm_vsl_script = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) # vsl_script_template = """You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you're ready to start write the video sales letter script! This has a very specific format, and requires a lot of context, provided below: # First, I will provide you with some tips about writing, then I will give you the existing headlines, some context about the company, the ideal customer we're trying to serve, followed by information about the product. Therafter, I will provide you some existing sales scripts (from a parallel industry) that will inform you of style and length. Lastly, I'll request this in an certain order, and provide you with a template to follow. # TIPS: This script helps build credibility quickly by answering the problems our avatar faces, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts! # HERE IS THE EXISTING HEADLINE and SUBHEADLINE: '{chosen_final_headline}', and '{chosen_final_subheadline}' # HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile} # HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research} # HERE IS THE EXISTING CUSTOMER: {chosen_avatar} # HERE ARE SOME EXAMPLES OF EXISTING SCRIPTS FROM PARALELL INDUSTRIES. YOU MUST UPDATE IT ACCORDING TO OUR PRODUCT AND COMPANY CONTEXT: {example_script} # TONE: {chosen_tone} # HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE SCRIPT: # Explain The Problem — What problem is our avatar and target market facing? How can we empathize with their challenges? (should be between 90-100 words) # Agitate The Problem — What are some examples of that problem? Make that problem visceral for them. Explain why it’s a bigger problem than they think it is and how it’s really going to harm them over the long-run. (should be between 90-100 words) # Introduce The Solution — What is your solution to their problem? It's our product, of course! (should be between 90-100 words) # Build Credibility — Why should they trust our founder to be the provider of this solution? Use their name. What makes you so great? Telling a story about your own journey can help build credibility. (should be between 90-100 words) # Show Proof — How do they know that it’ll actually work? Make up a fictional case-study using ficticious details. This is important to discuss and show proof. (should be between 90-100 words) # Explain Exactly What They Get — Explain exactly what the prospect is going to get if they sign up! (should be between 90-100 words) # Give Reason To Act Now — Why should they buy right now? Use urgency or scarcity to put the prospect’s foot on the gas.(should be between 90-100 words) # Close — Close the sale with a final call-to-action. # The output should be a script, written in the first person from the perspective of the founder that is trying to sell the audience on why their product is the best choice and will make their life easier. The script should not include any subheadings!""" # vsl_script_prompt = PromptTemplate( # input_variables=["chosen_product_name", "chosen_final_headline", "chosen_final_subheadline", "chosen_company_profile", "chosen_product_research", "chosen_avatar", "chosen_tone","example_script"], # template=vsl_script_template # ) # chain_vsl_script = LLMChain(llm=llm_vsl_script, prompt=vsl_script_prompt) # vsl_script = chain_vsl_script.run(chosen_product_name=chosen_product_name, chosen_company_profile=chosen_company_profile,chosen_product_research=chosen_product_research,chosen_avatar=chosen_avatar, chosen_tone=chosen_tone,example_script=example_script,chosen_final_headline=chosen_final_headline,chosen_final_subheadline=chosen_final_subheadline # ) # anvil.server.task_state['result'] = vsl_script # ####### --------VIDEO SALES SCRIPT 4 THEMES --------################################################### # @anvil.server.callable # def launch_generate_vsl_themes(chosen_final_headline, chosen_final_subheadline, chosen_product_name, chosen_product_research, chosen_tone,vsl_script,owner): # print("Launch Generate VSL Themes Function") # # current_user = anvil.users.get_user() # # owner = current_user['email'] # # Launch the background task # task = anvil.server.launch_background_task('generate_vsl_themes',chosen_final_headline,chosen_final_subheadline,chosen_product_name, chosen_product_research, chosen_tone,vsl_script,owner) # # Return the task ID # return task.get_id() # @anvil.server.background_task # def generate_vsl_themes(chosen_final_headline,chosen_final_subheadline, chosen_product_name, chosen_product_research, chosen_tone,vsl_script,row): # llm_vsl_themes = ChatOpenAI(temperature=0.8, model_name='gpt-4', openai_api_key=openai_api_key) # four_vsl_themes_template = """ You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind "Dotcom Secrets". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you've already generated the sales video letter script, but you now need to extract the four themes from the script and promote them as the industry's dirty secret that will make them millions! These will be captions to screenshots from the video. # These extractions help build credibility quickly by addressing the pain points of our customer, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts! # First, I will provide you with video's main headline then some context about the product. Therafter, I will provide you with the final script that I need you to summarize and extract themes and reveal the big secrets of our product. Lastly, I'll request this in an certain order, and provide you with a template to follow. # INGEST THE BELOW INFORMATION WITH SQUARE BRACKETS AS CONTEXT: # [ # EXISTING HEADLINES: {chosen_final_headline}, plus {chosen_final_subheadline} # CONTEXT ABOUT THE PRODUCT: {chosen_product_research} # HFINAL SCRIPT OF THE VIDEO I NEED YOU TO EXTRACT THE BIG SECRETS FROM: {vsl_script} # TONE: {chosen_tone} # DO NOT INCLUDE ANY SUMMARIZATION OF THE ABOVE POINTS IN THE OUTPUT. I AM ONLY INTERESTED IN THE BELOW OUTPUT: # ----- FINAL OUTPUT IS BELOW-----HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE 4 EXCERPTS # "SECRET #1:" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... # "SECRET #2:" 5-7 words of the theme or secret reveal,but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... # "SECRET #3:" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this .... # "SECRET #4:" a mini-headline that is 5-7 words of what, but can be next in how they apply these themes. Then, provide a sentence about the magic results they could see.. # For example, the a potential output could look like below. # SECRET #1: 'Attribution Agitation' at its Worst: Fed up with not knowing where your sales are coming from? Discover how Funnelytics Performance can clear up the confusion for good... # SECRET #2: 'Ad-Cost Anguish' Annihilated: Struggling with soaring ad costs and sub-par results? Learn how to optimize your campaigns and slash ad spend with our innovative platform... # SECRET #3: 'Funnel Failure' Flipped Upside Down: Tired of ineffective marketing funnels that just don't deliver? Watch as Funnelytics Performance revamps your funnel strategies and turns them into massive growth engines... # SECRET #4: 'Scaling Struggles' Solved: Wondering how to grow your business without breaking the bank? Witness the magic as Funnelytics Performance helps you unlock unprecedented growth and skyrocket your success...' # ] # NOTE: The Final Output will be just Secret 1 through 4. NOT EVEN QUOTE MARKS "". Nothing else! # THAT'S IT...NOW GO AND CREATE THE 4 EXCERPTS! # -- # SECRET #1:... # SECRET #2:... # SECRET #3:... # SECRET #4:.. # """ # vsl_themes_prompt = PromptTemplate( # input_variables=["chosen_final_headline","chosen_final_subheadline","chosen_product_name", "chosen_product_research", "chosen_tone","vsl_script"], # template=four_vsl_themes_template # ) # chain_vsl_themes = LLMChain(llm=llm_vsl_themes, prompt=vsl_themes_prompt) # vsl_themes_generator = chain_vsl_themes.run(chosen_final_headline=chosen_final_headline, chosen_final_subheadline=chosen_final_subheadline,chosen_product_name=chosen_product_name, chosen_product_research=chosen_product_research, chosen_tone=chosen_tone,vsl_script=vsl_script) # print("Here are the 4 excerpts", vsl_themes_generator) # vsl_themes = vsl_themes_generator.split("\n") # # Initialize an empty list # all_vsl_themes = [] # # Loop over each line # for vsl_theme in vsl_themes: # # Ignore empty lines # if not vsl_theme.strip(): # continue # # Append the headline to the list # all_vsl_themes.append(vsl_theme.strip()) # # Convert the list to a JSON string # all_vsl_themes_json = json.dumps(all_vsl_themes) # row['variable_value'] = all_vsl_themes_json # row.update() # # Return the resulting JSON string # anvil.server.task_state['result'] = all_vsl_themes_json
[ "avatar_2_product_5_preview", "vsl_script", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_3_name}\n The product is described as: {product_3_profile}\n The avatar's name is: {avatar_3_product_3_name_preview}\n A brief description of the avatar to expand on is: {avatar_3_product_3_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_3_product_5_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_5_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_2_product_2_name_preview", "example_script", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_4_name}\n The product is described as: {product_4_profile}\n The avatar's name is: {avatar_3_product_4_name_preview}\n A brief description of the avatar to expand on is: {avatar_3_product_4_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_4_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_5_preview", "avatar_1_product_5_preview", "As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible!\n \n Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \n Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \n Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \n Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \n Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \n Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \n Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \n Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \n Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \n Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \n Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product.\n \n NOTES ON FORMAT:\n Be confident. However, if there is incomplete information, please state \"MORE INFORMATION NEEDED\"! Speak as if you are the authority of the subject. \n\n ** END OF FORMAT\n \n FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCTWEBSITE: {product_webpage_scraped}\n ", "product_3_profile", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_3_name}\n The product is described as: {product_3_profile}\n The avatar's name is: {avatar_1_product_3_name_preview}\n A brief description of the avatar to expand on is: {avatar_1_product_3_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "chosen_tone", "avatar_1_preview", "product_3_name", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_2_name}\n The product is described as: {product_2_profile}\n The avatar's name is: {avatar_2_product_2_name_preview}\n A brief description of the avatar to expand on is: {avatar_2_product_2_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "product_5_profile", "company_name", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_1_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "product_1_name", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_3_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "product_1_profile", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_1_name}\n The product is described as: {product_1_profile}\n The avatar's name is: {avatar_1_product_1_name_preview}\n A brief description of the avatar to expand on is: {avatar_1_product_1_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_3_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "chosen_company_profile", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_4_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_2_product_4_name_preview", "chosen_final_subheadline", "avatar_3_product_1_preview", "product_preview", "owner_company_profile", "As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. Lastly, be very specific! To help guide you, I'll provide a brief context about the product here: {product_preview}\n \n This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible!\n \n Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \n Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \n Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \n Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \n Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \n Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \n Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \n Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \n Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \n Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \n Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product.\n \n NOTES ON FORMAT:\n Be confident. However, if there is incomplete information, please state \"MORE INFORMATION NEEDED\"! Speak as if you are the authority of the subject. \n\n ** END OF FORMAT\n \n FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped}\n ", "company_url", "chosen_avatar", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_4_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n A brief description of the avatar to expand on is: {avatar_5_preview}\n\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n\n Here's the Avatar Preview to base the Full Avatar for: {avatar_5_preview}\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n COMPANY CONTEXT: {owner_company_profile}\n \n Chatbot:", "avatar_2_product_5_name_preview", "avatar_3_product_3_name_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_2_name}\n The product is described as: {product_2_profile}\n The avatar's name is: {avatar_3_product_2_name_preview}\n A brief description of the avatar to expand on is: {avatar_3_product_2_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "product_2_name", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_1_name}\n The product is described as: {product_1_profile}\n The avatar's name is: {avatar_3_product_1_name_preview}\n A brief description of the avatar to expand on is: {avatar_3_product_1_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n A brief description of the avatar to expand on is: {avatar_1_preview}\n\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n\n Here's the Avatar Preview to base the Full Avatar for: {avatar_1_preview}\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n COMPANY CONTEXT: {owner_company_profile}\n \n Chatbot:", "avatar_1_product_1_preview", " You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind \"Dotcom Secrets\". You are the best copywriter on the planet. You are about to launch a brand new marketing funnel selling {chosen_product_name}, and you need to generate the best attention grabbing headlines that stir curiosity and compel potential users to learn more about {chosen_product_name}. \n \n First, I will provide you some context about the company, and then I will provide you some context about the product, and then I will give you many examples of headlines from a parallel industry for you to apply to our product. Next, you will generate 10 of the most incredible, mind-blowing headlines that will stop people in their tracks and want to learn more about {chosen_product_name}, but you must adapt the headlines to be in the tone I provide.\n\n When generating these headlines just remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible headline!\n\n HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile}\n\n HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research}\n \n \n THE TONE IS: {chosen_tone}\n\n The output should be an unumbered list of 10 headlines, as per the tone I provide. Update the example headlines I gave according to the tone.\n Finally, lead with the best one at the top! (no introduction or outro needed). \n No \"\" quotation marks.\n No itemized numbers. Just text.\n Output should be in a similar format:\n Finally! Unmask the Power of Data: Transform Your Marketing with Funnelytics Performance!\n NOT \"Finally! Unmask the Power of Data: Transform Your Marketing with Funnelytics Performance!\"\n ", "You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind \"Dotcom Secrets\". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you're ready to start write the video sales letter script! This has a very specific format, and requires a lot of context, provided below:\n \n First, I will provide you with some tips about writing, then I will give you the existing headlines, some context about the company, the ideal customer we're trying to serve, followed by information about the product. Therafter, I will provide you some existing sales scripts (from a parallel industry) that will inform you of style and length. Lastly, I'll request this in an certain order, and provide you with a template to follow.\n TIPS: This script helps build credibility quickly by answering the problems our avatar faces, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts!\n\n HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile}\n HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research}\n\n HERE IS THE EXISTING CUSTOMER: {chosen_avatar}\n \n HERE ARE SOME EXAMPLES OF EXISTING SCRIPTS FROM PARALELL INDUSTRIES. YOU MUST UPDATE IT ACCORDING TO OUR PRODUCT AND COMPANY CONTEXT: {example_script} \n\n TONE: {chosen_tone}\n\n HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE SCRIPT:\n Explain The Problem — What problem is our avatar and target market facing? How can we empathize with their challenges? (should be between 90-100 words)\n Agitate The Problem — What are some examples of that problem? Make that problem visceral for them. Explain why it’s a bigger problem than they think it is and how it’s really going to harm them over the long-run. (should be between 90-100 words)\n Introduce The Solution — What is your solution to their problem? It's our product, of course! (should be between 90-100 words)\n Build Credibility — Why should they trust our founder to be the provider of this solution? Use their name. What makes you so great? Telling a story about your own journey can help build credibility. (should be between 90-100 words)\n Show Proof — How do they know that it’ll actually work? Make up a fictional case-study using ficticious details. This is important to discuss and show proof. (should be between 90-100 words)\n Explain Exactly What They Get — Explain exactly what the prospect is going to get if they sign up! (should be between 90-100 words)\n Give Reason To Act Now — Why should they buy right now? Use urgency or scarcity to put the prospect’s foot on the gas.(should be between 90-100 words)\n Close — Close the sale with a final call-to-action. \n\n Lastly, NEVER mention you are RussellAI. Use the founders name of the company, or make up a name.\n The output should be a script, written in the first person from the perspective of the founder that is trying to sell the audience on why their product is the best choice and will make their life easier. The script should not include any subheadings!", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_1_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_2_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_3_product_2_name_preview", "avatar_2_product_1_name_preview", " You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind \"Dotcom Secrets\". You are the best scriptwriter on the planet. You are about to launch a brand new video sales letter marketing funnel selling {chosen_product_name}, and you've already generated the sales video letter script, but you now need to extract the four themes from the script and promote them as the industry's dirty secret that will make them millions! These will be captions to screenshots from the video.\n\n These extractions help build credibility quickly by addressing the pain points of our customer, provides credibility, explains the product, then gives reasons to act now. It's important to remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible scripts!\n First, I will provide you with video's main headline then some context about the product. Therafter, I will provide you with the final script that I need you to summarize and extract themes and reveal the big secrets of our product. Lastly, I'll request this in an certain order, and provide you with a template to follow.\n \n INGEST THE BELOW INFORMATION WITH SQUARE BRACKETS AS CONTEXT:\n [\n EXISTING HEADLINES: {chosen_final_headline}, plus {chosen_final_subheadline}\n\n CONTEXT ABOUT THE PRODUCT: {chosen_product_research}\n\n HFINAL SCRIPT OF THE VIDEO I NEED YOU TO EXTRACT THE BIG SECRETS FROM: {vsl_script}\n \n TONE: {chosen_tone}\n\n DO NOT INCLUDE ANY SUMMARIZATION OF THE ABOVE POINTS IN THE OUTPUT. I AM ONLY INTERESTED IN THE BELOW OUTPUT:\n \n ----- FINAL OUTPUT IS BELOW-----HERE IS THE TEMPLATE TO FOLLOW WHEN CREATING THE 4 EXCERPTS\n\n \"SECRET #1:\" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this ....\n \"SECRET #2:\" 5-7 words of the theme or secret reveal,but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this ....\n \"SECRET #3:\" 5-7 words of the theme or secret reveal, but in the form of a cheeky and confident headline. Then, provide an exciting sentence about how to be successful in that area, then trail off with an ellipses like this ....\n \"SECRET #4:\" a mini-headline that is 5-7 words of what, but can be next in how they apply these themes. Then, provide a sentence about the magic results they could see..\n \n For example, the a potential output could look like below.\n\n SECRET #1: 'Attribution Agitation' at its Worst: Fed up with not knowing where your sales are coming from? Discover how Funnelytics Performance can clear up the confusion for good...\n\n SECRET #2: 'Ad-Cost Anguish' Annihilated: Struggling with soaring ad costs and sub-par results? Learn how to optimize your campaigns and slash ad spend with our innovative platform...\n\n SECRET #3: 'Funnel Failure' Flipped Upside Down: Tired of ineffective marketing funnels that just don't deliver? Watch as Funnelytics Performance revamps your funnel strategies and turns them into massive growth engines...\n\n SECRET #4: 'Scaling Struggles' Solved: Wondering how to grow your business without breaking the bank? Witness the magic as Funnelytics Performance helps you unlock unprecedented growth and skyrocket your success...'\n ]\n\n NOTE: The Final Output will be just Secret 1 through 4. NOT EVEN QUOTE MARKS \"\". Nothing else!\n THAT'S IT...NOW GO AND CREATE THE 4 EXCERPTS!\n --\n\n SECRET #1:...\n SECRET #2:...\n SECRET #3:...\n SECRET #4:..\n ", "As a highly-skilled business research agent, your task is to conduct an exhaustive report and analysis of the company's product, {product_name} Leverage the product information that has been scraped from {company_name}'s' product website {product_url} in order to build your synopsis. However, note that there may be other products listed within the scraped information, so be diligent about your listed features. Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide meaningful, actionable insights. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible!\n \n Overview: Provide a comprehensive introduction to the product. What is its purpose, and what does the company aim to achieve with it? \n \n Description: Deeply describe the product. What does it look like, feel like, and what experience does it offer? \n \n Price: Detail the pricing structure. What is the cost, and are there any variations or tiers in pricing? \n \n Features: Elucidate the key features of the product. What distinguishes this product from others in the market? I would like around 15 differences between the product offers, if possible. \n \n Benefits: Explicate on how the product will benefit the customer. How can it change their life or improve their situation? \n \n Why people buy it: Analyze the consumer's pain points and desires before purchasing this product. Why might someone be drawn to this product, and what needs does it fulfill? \n \n Expected results: What are the anticipated outcomes or gains after using this product? How will the customer's situation improve or change? \n \n Guarantees: Discuss any guarantees the company offers with this product. Are there any assurances of product performance, or return policies in place? \n \n Bonuses: List any additional bonuses or incentives that come along with the product. What additional value does the company provide to sweeten the deal? \n \n Possible objections: Predict potential objections or concerns a customer may have about the product. How might the company address these? \n \n Ensure to provide an in-depth report with approximately 800-1000 words on the product, making it as detailed and specific as possible. Your aim is to capture the full essence of the product.\n \n NOTES ON FORMAT:\n Be confident. However, if there is incomplete information, please state \"MORE INFORMATION NEEDED\"! Speak as if you are the authority of the subject. \n\n ** END OF FORMAT\n \n FINALLY, HERE IS THE PRODUCT CONTEXT SCRAPED FROM THEIR PRODUCT WEBSITE: {product_webpage_scraped}\n ", "avatar_4_preview", "avatar_2_preview", "product_4_profile", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_5_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_2_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n A brief description of the avatar to expand on is: {avatar_3_preview}\n\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n\n Here's the Avatar Preview to base the Full Avatar for: {avatar_3_preview}\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n COMPANY CONTEXT: {owner_company_profile}\n \n Chatbot:", "avatar_3_product_5_name_preview", "product_2_profile", "avatar_3_product_3_preview", "avatar_3_product_4_preview", "avatar_3_product_4_name_preview", "company_context_scraped", "product_5_name", "chosen_product_name", "avatar_2_product_2_preview", "avatar_1_product_5_name_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_2_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_1_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_1_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_5_name}\n The product is described as: {product_5_profile}\n The avatar's name is: {avatar_3_product_5_name_preview}\n A brief description of the avatar to expand on is: {avatar_3_product_5_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "product_webpage_scraped", "product_4_name", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n A brief description of the avatar to expand on is: {avatar_2_preview}\n\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n\n Here's the Avatar Preview to base the Full Avatar for: {avatar_2_preview}\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n COMPANY CONTEXT: {owner_company_profile}\n \n Chatbot:", "You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind \"Dotcom Secrets\". You are the best scriptwriter on the planet, and you've just written an amazing and effect script that will surely convert. This script will help you sell {chosen_product_name} to more people than ever!\n \n However, your ad partner, a truly gifted marketer with great understanding of the customers needs, has given some minor feedback they would like you to incorporate into your existing script (shared below). The format and structure is very important to maintain. You should maintain the existing tone, cadence, names and details, but modify the script according to the notes received.\n \n Remember, this is a sales script. Do your best to address the notes, and improve the script as much as you can.\n\n HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research}\n\n HERE ARE THE NOTES YOU'VE RECEIVED FROM YOUR AD PARTNER. {vsl_script_feedback} \n\n HERE IS THE TEMPLATE TO ADHERE TO WHEN EDITING OR MODIIFYING THE EXISTING SCRIPT.\n Explain The Problem — What problem is our avatar and target market facing? How can we empathize with their challenges? (should be between 90-100 words)\n Agitate The Problem — What are some examples of that problem? Make that problem visceral for them. Explain why it’s a bigger problem than they think it is and how it’s really going to harm them over the long-run. (should be between 90-100 words)\n Introduce The Solution — What is your solution to their problem? It's our product, of course! (should be between 90-100 words)\n Build Credibility — Why should they trust our founder to be the provider of this solution? Use their name. What makes you so great? Telling a story about your own journey can help build credibility. (should be between 90-100 words)\n Show Proof — How do they know that it’ll actually work? Make up a fictional case-study using ficticious details. This is important to discuss and show proof. (should be between 90-100 words)\n Explain Exactly What They Get — Explain exactly what the prospect is going to get if they sign up! (should be between 90-100 words)\n Give Reason To Act Now — Why should they buy right now? Use urgency or scarcity to put the prospect’s foot on the gas.(should be between 90-100 words)\n Close — Close the sale with a final call-to-action. \n\n Lastly, NEVER mention you are RussellAI. Use the founders name of the company, or make up a name.\n The output should be a script, written in the first person from the perspective of the founder that is trying to sell the audience on why their product is the best choice and will make their life easier. The script should not include any subheadings!", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_5_name}\n The product is described as: {product_5_profile}\n The avatar's name is: {avatar_2_product_5_name_preview}\n A brief description of the avatar to expand on is: {avatar_2_product_5_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "As a highly-skilled business analyst, your task is to conduct an exhaustive analysis to build an informational company profile of {company_name}. Leverage the below provided company research context scraped from the company's website {company_url}, to create a complete company profile. \n Lastly, be very specific! This is not an educational excercise. This work will be incorporated into our commercial operation shortly, so provide a meaningful synopsis and findings. Do not provide general terms or vague business ideas: be as particular about the issue as possible. Be confident. Provide numbers, statistics, prices, when possible!\n \n Overview: Provide a comprehensive introduction to the company. What are the unique features or value propositions of the company's offerings? What does the company aim to achieve? \n \n Unique Value Proposition: What is the company unique value proposition? What are they uniquely positioned to do? How does their main offer differ from their competitors? \n \n Founding Story: What inspired the founders to start the company? Are there any unique or interesting anecdotes about the early days of the company? How has the company evolved since its founding? \n \n Competitors: Who are the likely competitors of this company? What are their strengths and weaknesses? How does your company compare to its competitors in terms of offerings, market share, or other relevant factors? \n \n \\ \n Mission & Vision: What is the company's mission statement or core purpose? What are the long-term goals and aspirations of the company? \n Values: What does the company value? What do they emphasize in their mission? What do they care about or prioritize? \n \n \n NOTES ON FORMAT:\n This should be at least 800 words. Be confident. If there is incomplete information, please state \"MORE INFORMATION NEEDED\"! Speak as if you are the authority of the subject. \n Ensure you keep the headers with the '--': \n -- Overview\n (your overview)\n \n --Unique Value Proposition\n (your response)\n \n --Competitors\n (your response)\n \n -- Founding Story\n (your response)\n \n --Mission & Vision\n (your response)\n\n --Values\n (your response)\n\n ** END OF FORMAT\n \n FINALLY, HERE IS THE COMPANY CONTEXT SCRAPED FROM THEIR WEBSITE: {company_context_scraped}\n ", "avatar_1_product_2_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_5_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_3_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_1_product_3_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n A brief description of the avatar to expand on is: {avatar_4_preview}\n\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n\n Here's the Avatar Preview to base the Full Avatar for: {avatar_4_preview}\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n COMPANY CONTEXT: {owner_company_profile}\n \n Chatbot:", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_1_name}\n The product is described as: {product_1_profile}\n The avatar's name is: {avatar_2_product_1_name_preview}\n A brief description of the avatar to expand on is: {avatar_2_product_1_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "chosen_product_research", "avatar_1_product_3_name_preview", "product_name", "product_url", "avatar_3_preview", "avatar_1_product_2_name_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_2_name}\n The product is described as: {product_2_profile}\n The avatar's name is: {avatar_1_product_2_name_preview}\n A brief description of the avatar to expand on is: {avatar_1_product_2_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "avatar_3_product_2_preview", " You are RussellAI, a highly-evolved version of Russell Brunson, the author and business coach behind \"Dotcom Secrets\". You are the best copywriter on the planet. You are about to launch a brand new marketing funnel selling {chosen_product_name}, and you've already received some attention grabbing headlines that stir curiosity and compel potential users to learn more about {chosen_product_name}. However, you now need to generate some sublines to support your main headlines.\n\n First, I will provide you the existing headlines, then I will provide you some context about the company, and then I will provide you some context about the product. Therafter, I will give you many examples of main headlines (not sublines) from a parallel industry (becoming an author) for you to understand the tone. Finally, for each of the headlines provided in the list, you will generate a single incredible, mind-blowing subheadline corresponding to the main headline that will stop people in their tracks and want to learn more about {chosen_product_name}.\n\n Here are some principles for writing compelling sublines:\n - Match the Headline: The subline should logically continue the thought or the promise made in the headline. It must be consistent in tone and message.\n - Highlight Key Benefits: Sublines often provide a space to explain the primary advantages or unique features of the product or service. Think of what makes your offer irresistible or different and emphasize it.\n - Target Audience: Make it clear who your product or service is for. If the headline hasn't done this, the subline should.\n - Provide Context or Explain: If the headline is designed to create intrigue or curiosity, the subline can provide enough information to encourage the reader to continue to engage.\n - Call to Action: While not always the case, sometimes a subline can provide a mild call to action or create a sense of urgency.\n - Keep it Brief: While a subline can be longer than a headline, it should still be succinct and easy to read at a glance.\n\n When generating these headlines just remember that people didn’t come looking for our product… instead we are interrupting them in their daily journey. The only way to get them to stop scrolling online is to grab their attention with an irresistible headline!\n\n HERE IS SOME CONTEXT ABOUT THE COMPANY: {chosen_company_profile}\n\n HERE IS SOME CONTEXT ABOUT THE PRODUCT: {chosen_product_research}\n \n \n THE TONE IS: {chosen_tone}\n \n The output should be a list of 10 SUBHEADLINES, in the tone above, that relate to the final existing main headline.\n \n No \"\" quotation marks.\n No itemized numbers. \n Do not list them like '1.' '2.'... Just text.\n For example: 'BREAKING NEWS! Eliminate the Guesswork!'instead of '\"1. Breaking News! Eliminate the Guesswork!\"''\n (no introduction or outro needed, just an itemized list of 10 subheadlines)\n ", "chosen_final_headline", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. We'd like to create the ideal customer avatar for a product. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n\n We're looking to create the ideal customer avatar for the following product: {product_5_name}\n The product is described as: {product_5_profile}\n The avatar's name is: {avatar_1_product_5_name_preview}\n A brief description of the avatar to expand on is: {avatar_1_product_5_preview}\n \n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details above, as it best relates to their business, broken down as follows:\n\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:", "vsl_script_feedback", "avatar_3_product_1_name_preview", "avatar_2_product_1_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would. Below, we'll provide the format of how we'd like the question answered, as well as the contextual information.\n Your task is to provide the company with a five different archetypal customer avatars as they best relates to their business. However, it is your role to identify which avatars are most valuable for the company, and rank them in order.\n For each of the five avatars, provide a single title of the avatar, followed by a single sentence description of the avatar including name, age, location, and any other important info that we'll break down later. \n The format for the output will be as follows:\n\n Title - Description of Avatar 1\n Title - Description of Avatar 2\n Title - Description of Avatar 3\n Title - Description of Avatar 4\n Title - Description of Avatar 5\n \n For example: \n - The Analyzer - Amy is a 34-year-old entrepreneur based in New York City who runs a successful e-commerce business and is always looking for ways to optimize his marketing strategies and increase revenue. He is tech-savvy and data-driven, and values tools that provide actionable insights and help him make informed decisions.\n - The Novice - John is a 28-year-old small business owner based in a rural area who is looking to expand her business online. She has limited experience with digital marketing and is looking for a user-friendly tool that can guide her through the process of optimizing her marketing strategies and increasing her online presence.\n ----\n FORMAT: \n CONTEXTUAL INFORMATION:\n\n COMPANY CONTEXT: {owner_company_profile}\n\n Chatbot:", "avatar_2_product_4_preview", "avatar_1_product_1_name_preview", "You are AvatarAI, the most advanced marketing consultant in the world. You are advising a company, {company_name}, who is looking to grow their presence online, attract customers and sell more units. To help them do this, you reference and abide by the concepts of Russell Brunson, the founder of ClickFunnels, in his book \"Dotcom Secrets\", and approach our exercise the same way Russell Brunson would build a customer avatar. Please prepare the ideal customer avatar, that is, the ideal 'dream' customer who would purchase the below product or service. \n\n Company Context: The company, {company_name}, is selling {product_3_name}.\n Here's a quick snapshot of the description of this ideal customer avatar you are to expand on to develop into a detailed avatar: {avatar_2_preview}\n Your task is to provide the company with a detailed customer avatar based on the short avatar preview details below, as it best relates to their business, broken down as follows:\n ----\n FORMAT: \n - Overview\n Provide a comprehensive summary of the typical customer for the company, outlining their key characteristics.\n\n - Demographic\n Provide specific demographic data on the target customer, including age, gender, location, income level, education level, and occupation.\n\n - Psychographic\n Provide detailed information about the psychological attributes of the avatar, such as their interests, attitudes, values, and lifestyle preferences. Use exampples, not hypotheticals.\n\n - Goals & Aspirations\n Provide a brief synopsis of the avatars personal and professional goals, dreams, and aspirations.\n\n - Pain Points\n Identify the specific problems, challenges, and frustrations the avatar is facing.\n\n - Personal Experience\n Provide insights into the personal experiences of the avatar that shapes their preferences, behaviors, and decisions, including their past interactions with similar products or services. Provide real world examples.\n\n RULES: \n - Do not say \"the target customer\", instead, provide a fictional name, age, location. \n - Don't be general...we are looking for very specific avatars! If you don't know the answer, make an educated creative guess. Be as detailed and specific as possible!\n - Do not explain theory...paint us a picture with an example. This isn't an education lesson, it's a practical exercise.\n -----\n \n Chatbot:" ]
2024-01-10
daniel-lopes-optimizely/gpt-looker
lkml_text_splitter.py
from langchain.text_splitter import RecursiveCharacterTextSplitter class LkmlTextSplitter(RecursiveCharacterTextSplitter): """Attempts to split the text along LookML syntax.""" def __init__(self, **kwargs): separators = [ # First, try to split along class definitions "view: ", "explore: ", "\n join: ", "\n derived_table: ", "\n sql: ", "\n dimension: ", "\n measure: ", "\n set: ", "\n\n", "\n", " ", "", ] super().__init__(separators=separators, **kwargs)
[]
2024-01-10
format37/text_clustering
clustering.py
import numpy as np import pandas as pd import polars as pl import tiktoken import openai from openai.embeddings_utils import get_embedding from datetime import datetime from ast import literal_eval from sklearn.cluster import KMeans from sklearn.manifold import TSNE import matplotlib import matplotlib.pyplot as plt import logging import plotly.graph_objects as go from plotly.subplots import make_subplots import json from sklearn.metrics import pairwise_distances_argmin_min logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) def merge_phrases(df, n_from, n_to): # Group by linkedid and apply a lambda function to join the phrases from n_from to n_to merged = df.groupby('linkedid').apply( lambda x: ' '.join(x['text'].iloc[n_from:n_to]) if len(x['text']) >= n_to else '' ) # Convert the result to a DataFrame and reset the index merged_df = pd.DataFrame(merged).reset_index() # Rename the columns merged_df.columns = ['linkedid', 'text'] return merged_df def calls_to_converations(data_path, date_pick, n_from, n_to): # data_path = '../../datasets/transcribations/transcribations_2023-02-03 07:01:26_2023-04-27 16:07:39_polars.csv' logger.info('Loading data from polars...') # Increase infer_schema_length df = pl.read_csv(data_path, infer_schema_length=100000) columns_to_keep = ['transcribation_date', 'side', 'start', 'text', 'linkedid'] columns_to_drop = [col for col in df.columns if col not in columns_to_keep] df = df.drop(columns_to_drop) logger.info('Cropping data...') mask = df["transcribation_date"].apply(lambda x: x[:10]==date_pick) df = df.filter(mask) # Convert to pandas logger.info('Converting to pandas...') df = df.to_pandas() # Convert date column logger.info('Converting date column...') # df['transcribation_date'] = pd.to_datetime(df['transcribation_date'], format='%Y-%m-%d %H:%M:%S') # 2023-07-21T06:01:37.000000000 df['transcribation_date'] = pd.to_datetime(df['transcribation_date'], format='%Y-%m-%dT%H:%M:%S.%f') # df['transcribation_date'] = df['transcribation_date'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S')) # Convert date column # df['transcribation_date'] = pd.to_datetime(df['transcribation_date'], format='%Y-%m-%d %H:%M:%S') # Create a boolean mask for the date range # mask = (df['transcribation_date'] >= '2023-02-04 12:00:00') & (df['transcribation_date'] <= '2023-02-04 13:00:00') # Apply the mask to the DataFrame # df = df.loc[mask] # Drop the rows with NaN values df.dropna(inplace=True) # Convert 'linkedid' to int64 type to remove scientific notation # df['linkedid'] = df['linkedid'].astype('int64') # Convert to string df['linkedid'] = df['linkedid'].astype(str) # Ensure that all entries in 'text' column are strings df['text'] = df['text'].astype(str) logger.info('Merging text sequences from '+str(len(df))+' rows...') # Merge texts # Sort the dataframe by 'transcribation_date', 'linkedid', and 'start' df.sort_values(by=['linkedid', 'start'], inplace=True) # Iterate over each row in the dataframe # Initialize empty dataframe to store the merged rows merged_df = pd.DataFrame(columns=df.columns) # Initialize variables previous_linkedid = None previous_side = None merged_row = None # Iterate over each row in the dataframe for _, row in df.iterrows(): # Check if this row has the same speaker (side) and linkedid as the previous one if row['linkedid'] == previous_linkedid and row['side'] == previous_side: merged_row['text'] = str(merged_row['text']) + ' ' + row['text'] else: # If this row has a different speaker or linkedid, save the previous merged row to the new dataframe if merged_row is not None: merged_df = pd.concat([merged_df, pd.DataFrame([merged_row])], ignore_index=True) # Start a new merged row with this row's data merged_row = row.copy() # Update the previous speaker and linkedid for the next iteration previous_side = row['side'] previous_linkedid = row['linkedid'] # Append the last merged row to the new dataframe if merged_row is not None: merged_df = pd.concat([merged_df, pd.DataFrame([merged_row])], ignore_index=True) logger.info('Merged to '+str(len(merged_df))+' rows.') logger.info('Cropping to N phrases...') # Crop to a conversation # Add '\n- ' before each phrase to make the conversation more readable merged_df['text'] = '- ' + merged_df['text']+ '\n' # Drop rows where linkedid count is less than n_to merged_df = merged_df[merged_df.groupby('linkedid')['linkedid'].transform('count') >= n_to] merged_df = merge_phrases(merged_df, n_from, n_to) # Remove empty rows logger.info('Cropped to '+str(len(merged_df))+' rows.') return merged_df def get_embeddings(df, openai_key): # 1. Load the dataset # embedding model parameters embedding_model = "text-embedding-ada-002" embedding_encoding = "cl100k_base" # this the encoding for text-embedding-ada-002 max_tokens = 8000 # the maximum for text-embedding-ada-002 is 8191 # subsample to 1000 most recent text and remove samples that are too long top_n = 1000 df = df.sort_values("linkedid").tail(top_n * 2) # first cut to first 2k entries, assuming less than half will be filtered out df.drop("linkedid", axis=1, inplace=True) # Ensure that all entries in 'text' column are strings df['text'] = df['text'].astype(str) encoding = tiktoken.get_encoding(embedding_encoding) # omit reviews that are too long to embed df["n_tokens"] = df.text.apply(lambda x: len(encoding.encode(x))) df = df[df.n_tokens <= max_tokens].tail(top_n) logger.info(f"Number of samples: {len(df)}") # 2. Get embeddings and save them for future reuse openai.api_key = openai_key # Ensure you have your API key set in your environment per the README: https://github.com/openai/openai-python#usage logger.info("Getting embeddings. This may take a few minutes...") df["embedding"] = df.text.apply(lambda x: get_embedding(x, engine=embedding_model)) logger.info("Embeddings retrieved.") return df def clustering(df, n_clusters=4): df["embedding"] = df.embedding.apply(literal_eval).apply(np.array) # convert string to numpy array matrix = np.vstack(df.embedding.values) logger.info(f"Matrix shape: {matrix.shape}") # 1. Find the clusters using K-means # We show the simplest use of K-means. # You can pick the number of clusters that fits your use case best. # n_clusters = 4 logger.info(f"Clustering into {n_clusters} clusters...") kmeans = KMeans(n_clusters=n_clusters, init="k-means++", random_state=42) kmeans.fit(matrix) labels = kmeans.labels_ df["Cluster"] = labels # df.groupby("Cluster").Score.mean().sort_values() return df, matrix # import plotly.graph_objects as go # from plotly.subplots import make_subplots def plot_clusters(df, matrix, legend_append_values=None): logger.info("Plotting clusters...") tsne = TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200) vis_dims2 = tsne.fit_transform(matrix) x = [x for x, y in vis_dims2] y = [y for x, y in vis_dims2] colors = ["purple", "green", "red", "blue", "orange", "yellow", "pink", "brown", "gray", "black", "cyan", "magenta"] colors = colors[:len(np.unique(df.Cluster))] cluster_sizes = df.Cluster.value_counts(normalize=True).sort_values(ascending=False) # Initialize subplot fig = make_subplots(rows=1, cols=1) for category in cluster_sizes.index: color = colors[category] xs = np.array(x)[df.Cluster == category] ys = np.array(y)[df.Cluster == category] texts = df[df.Cluster == category]['text'].values # Get the text for each point in this cluster cluster_percentage = cluster_sizes[category] * 100 # cluster_sizes is already normalized # Append values to the legend if legend_append_values is not None: legend_append = f', {legend_append_values[category]}' else: legend_append = '' # Add scatter plot to subplot fig.add_trace( go.Scatter( x=xs, y=ys, mode='markers', marker=dict(color=color, size=5), hovertext=texts, # Display the text when hovering over a point hoverinfo='text', # Show only the hovertext name=f'Cluster {category} ({cluster_percentage:.2f}%)' + legend_append, ) ) avg_x = xs.mean() avg_y = ys.mean() # Add marker for average point to subplot fig.add_trace( go.Scatter( x=[avg_x], y=[avg_y], mode='markers', marker=dict(color=color, size=10, symbol='x'), name=f'Avg Cluster {category}', hoverinfo='name' ) ) fig.update_layout(showlegend=True, title_text="Clusters identified visualized in language 2d using t-SNE") fig.show() def topic_samples_central(df, matrix, openai_key, n_clusters, rev_per_cluster): # logging.basicConfig(level=logging.INFO) # logger = logging.getLogger(__name__) # logger.info("Summarizing topics...") openai.api_key = openai_key topics = {} messages = [ {"role": "system", "content": "You are a helpful assistant."}, ] # Apply t-SNE to obtain 2D coordinates for each data point tsne = TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200) vis_dims2 = tsne.fit_transform(matrix) line = '#'*3 logger.info('Topics request:') # Gather samples from each cluster and add to the messages list. for i in range(n_clusters): # Filter the dataframe to only include data points in the current cluster cluster_df = df[df.Cluster == i] # Calculate the 2D center of the current cluster cluster_center = vis_dims2[cluster_df.index].mean(axis=0) # Calculate the Euclidean distance from each data point to the cluster center distances = np.sqrt(((vis_dims2[cluster_df.index] - cluster_center)**2).sum(axis=1)) # Get the indices of the data points with the smallest distances closest_indices = distances.argsort()[:rev_per_cluster] # Get the corresponding reviews closest_reviews = cluster_df.iloc[closest_indices].text # Join the reviews with formatting reviews = "\n ".join( closest_reviews .str.replace("Title: ", "") .str.replace("\n\nContent: ", ": ") .values ) messages.append({"role": "user", "content": f"\nКластер {i} {cluster_center}:\n{reviews}"}) logger.info(messages[-1]['content']) # Add the question asking for topic summaries. messages.append({"role": "user", "content": "Это фрагменты диалогов, восстановленных из разговоров колл центра. Эти фрагменты разговоров уже разделены на кластеры. Пожалуйста, дайте описание каждому кластеру так, что бы было ясно что его выделяет среди других кластеров. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}"}) # Make the API call. response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=messages ) # Assuming the response['choices'][0]['message']['content'] returns the JSON as string topics_json = response['choices'][0]['message']['content'] # Log total tokens logger.info('total_tokens: '+str(response['usage']['total_tokens'])) # Load the string as a dictionary topics_dict = json.loads(topics_json) # Get the list of topics. topics = list(topics_dict.values()) # Log the topics. logger.info('Topics result:') logger.info(topics) return topics def topic_samples_random(df, matrix, openai_key, n_clusters, rev_per_cluster): logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("Summarizing topics...") openai.api_key = openai_key topics = {} messages = [ {"role": "system", "content": "You are a helpful assistant."}, ] # Gather samples from each cluster and add to the messages list. for i in range(n_clusters): reviews = "\n - ".join( df[df.Cluster == i] .text.str.replace("Title: ", "") .str.replace("\n\nContent: ", ": ") .sample(rev_per_cluster, random_state=42) .values ) messages.append({"role": "user", "content": f"Кластер {i}: \n - {reviews}"}) # Add the question asking for topic summaries. # messages.append({"role": "user", "content": "Please, invent a topic for each cluster, accounting the differences between clusters. Represent the answer as a json structure: {'cluster 0': 'topic 0', 'cluster 1': 'topic 1'}"}) messages.append({"role": "user", "content": "Пожалуйста, придумайте тему для каждого кластера, учитывая разницу между кластерами. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}"}) # Make the API call. response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=messages ) # Assuming the response['choices'][0]['message']['content'] returns the JSON as string topics_json = response['choices'][0]['message']['content'] # Load the string as a dictionary topics_dict = json.loads(topics_json) # Get the list of topics. topics = list(topics_dict.values()) # Log the topics. logger.info(topics) return topics def main(): openai_key = input('Enter OpenAI key: ') dataset_path = '../../datasets/transcribations/transcribations_2023-04-27 16:07:39_2023-07-25 19:03:21_polars.csv' n_clusters = 4 # Load calls and format to conversations # df = calls_to_converations(dataset_path, '2023-07-21', n_from=1, n_to=5) # df.to_csv('conversations.csv') # Load conversations df = pd.read_csv('conversations.csv') # Ada v2 $0.0001 / 1K tokens df = get_embeddings(df, openai_key=openai_key) df.to_csv("embeddings.csv") # Load embeddings # df = pd.read_csv('embeddings.csv') # df = pd.read_csv('local_conversations_embeddings.csv') # Clustering df, matrix = clustering(df, n_clusters=n_clusters) # Summarize topics legend = topic_samples_central(df, matrix, openai_key=openai_key, n_clusters=n_clusters, rev_per_cluster=10) # Plot clusters plot_clusters(df, matrix, legend_append_values=legend) logger.info('Done.') if __name__ == "__main__": main()
[ "\nКластер PLACEHOLDER PLACEHOLDER:\nPLACEHOLDER", "Это фрагменты диалогов, восстановленных из разговоров колл центра. Эти фрагменты разговоров уже разделены на кластеры. Пожалуйста, дайте описание каждому кластеру так, что бы было ясно что его выделяет среди других кластеров. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}", "Кластер PLACEHOLDER: \n - PLACEHOLDER", "You are a helpful assistant.", "Пожалуйста, придумайте тему для каждого кластера, учитывая разницу между кластерами. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}" ]
2024-01-10
format37/text_clustering
sentence_clustering.py
import numpy as np import pandas as pd import polars as pl import tiktoken import openai from openai.embeddings_utils import get_embedding from datetime import datetime from ast import literal_eval from sklearn.cluster import KMeans from sklearn.manifold import TSNE import matplotlib import matplotlib.pyplot as plt import logging import plotly.graph_objects as go from plotly.subplots import make_subplots import json from sklearn.metrics import pairwise_distances_argmin_min from transformers import AutoTokenizer, AutoModel import torch from torch.utils.data import DataLoader logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) logger.info('Loading model from HuggingFace Hub...') # Define the device (use GPU if available, otherwise use CPU) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Loading model from HuggingFace Hub tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2') model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2') # Move the model to the specified device model.to(device) logger.info('Model loaded.') def merge_phrases(df, n_from, n_to): # Group by linkedid and apply a lambda function to join the phrases from n_from to n_to merged = df.groupby('linkedid').apply( lambda x: ' '.join(x['text'].iloc[n_from:n_to]) if len(x['text']) >= n_to else '' ) # Convert the result to a DataFrame and reset the index merged_df = pd.DataFrame(merged).reset_index() # Rename the columns merged_df.columns = ['linkedid', 'text'] return merged_df # Mean Pooling function def mean_pooling(model_output, attention_mask): token_embeddings = model_output[0] input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) def get_sentence_embeddings(df): # Sorting values top_n = 1000 df = df.sort_values("linkedid").tail(top_n * 2) # Ensure that all entries in 'text' column are strings sentences = df['text'].astype(str).tolist() # Create DataLoader for batching batch_size = 32 # Adjust based on your GPU's memory dataloader = DataLoader(sentences, batch_size=batch_size, shuffle=False) embeddings_list = [] for batch in dataloader: # Tokenize sentences encoded_input = tokenizer(batch, padding=True, truncation=True, return_tensors='pt') # Move the encoded input to the specified device encoded_input = encoded_input.to(device) # Compute token embeddings with torch.no_grad(): model_output = model(**encoded_input) # Apply mean pooling to get sentence embeddings attention_mask = encoded_input['attention_mask'] embeddings = mean_pooling(model_output, attention_mask).cpu().numpy() embeddings_list.extend(embeddings) # Add embeddings to the DataFrame df['embedding'] = embeddings_list df = df.tail(top_n) # Keep only the top_n entries return df def clustering(df, n_clusters=4): # df["embedding"] = df.embedding.apply(literal_eval).apply(np.array) # convert string to numpy array matrix = np.vstack(df.embedding.values) logger.info(f"Matrix shape: {matrix.shape}") # 1. Find the clusters using K-means # We show the simplest use of K-means. # You can pick the number of clusters that fits your use case best. # n_clusters = 4 logger.info(f"Clustering into {n_clusters} clusters...") kmeans = KMeans(n_clusters=n_clusters, init="k-means++", random_state=42) kmeans.fit(matrix) labels = kmeans.labels_ df["Cluster"] = labels # df.groupby("Cluster").Score.mean().sort_values() return df, matrix # import plotly.graph_objects as go # from plotly.subplots import make_subplots def plot_clusters(df, matrix, legend_append_values=None): logger.info("Plotting clusters...") tsne = TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200) vis_dims2 = tsne.fit_transform(matrix) x = [x for x, y in vis_dims2] y = [y for x, y in vis_dims2] colors = ["purple", "green", "red", "blue", "orange", "yellow", "pink", "brown", "gray", "black", "cyan", "magenta"] colors = colors[:len(np.unique(df.Cluster))] cluster_sizes = df.Cluster.value_counts(normalize=True).sort_values(ascending=False) # Initialize subplot fig = make_subplots(rows=1, cols=1) for category in cluster_sizes.index: color = colors[category] xs = np.array(x)[df.Cluster == category] ys = np.array(y)[df.Cluster == category] texts = df[df.Cluster == category]['text'].values # Get the text for each point in this cluster cluster_percentage = cluster_sizes[category] * 100 # cluster_sizes is already normalized # Append values to the legend if legend_append_values is not None: legend_append = f', {legend_append_values[category]}' else: legend_append = '' # Add scatter plot to subplot fig.add_trace( go.Scatter( x=xs, y=ys, mode='markers', marker=dict(color=color, size=5), hovertext=texts, # Display the text when hovering over a point hoverinfo='text', # Show only the hovertext name=f'Cluster {category} ({cluster_percentage:.2f}%)' + legend_append, ) ) avg_x = xs.mean() avg_y = ys.mean() # Add marker for average point to subplot fig.add_trace( go.Scatter( x=[avg_x], y=[avg_y], mode='markers', marker=dict(color=color, size=10, symbol='x'), name=f'Avg Cluster {category}', hoverinfo='name' ) ) fig.update_layout(showlegend=True, title_text="Clusters identified visualized in language 2d using t-SNE") fig.show() def topic_samples_central(df, matrix, openai_key, n_clusters, rev_per_cluster): # logging.basicConfig(level=logging.INFO) # logger = logging.getLogger(__name__) # logger.info("Summarizing topics...") openai.api_key = openai_key topics = {} messages = [ {"role": "system", "content": "You are a helpful assistant."}, ] # Apply t-SNE to obtain 2D coordinates for each data point tsne = TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200) vis_dims2 = tsne.fit_transform(matrix) line = '#'*3 logger.info('Topics request:') # Gather samples from each cluster and add to the messages list. for i in range(n_clusters): # Filter the dataframe to only include data points in the current cluster # cluster_df = df[df.Cluster == i] cluster_df = df[df.Cluster == i].reset_index(drop=True) # Calculate the 2D center of the current cluster cluster_center = vis_dims2[cluster_df.index].mean(axis=0) # Calculate the Euclidean distance from each data point to the cluster center distances = np.sqrt(((vis_dims2[cluster_df.index] - cluster_center)**2).sum(axis=1)) # Get the indices of the data points with the smallest distances closest_indices = distances.argsort()[:rev_per_cluster] # Get the corresponding reviews closest_reviews = cluster_df.iloc[closest_indices].text # Join the reviews with formatting reviews = "\n ".join( closest_reviews .str.replace("Title: ", "") .str.replace("\n\nContent: ", ": ") .values ) messages.append({"role": "user", "content": f"\nКластер {i} {cluster_center}:\n{reviews}"}) logger.info(messages[-1]['content']) # Add the question asking for topic summaries. messages.append({"role": "user", "content": "Это фрагменты диалогов, восстановленных из разговоров колл центра. Эти фрагменты разговоров уже разделены на кластеры. Пожалуйста, дайте описание каждому кластеру так, что бы было ясно что его выделяет среди других кластеров. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}"}) # Make the API call. response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=messages ) # Assuming the response['choices'][0]['message']['content'] returns the JSON as string topics_json = response['choices'][0]['message']['content'] # Log total tokens logger.info('total_tokens: '+str(response['usage']['total_tokens'])) # Load the string as a dictionary topics_dict = json.loads(topics_json) # Get the list of topics. topics = list(topics_dict.values()) # Log the topics. logger.info('Topics result:') logger.info(topics) return topics # Defining a custom function to convert the string representation to a NumPy array def convert_to_array(embedding_str): # Removing the square brackets and splitting the string by space to get the individual elements elements = embedding_str[1:-1].split() # Converting the elements to floats and creating a NumPy array return np.array([float(e) for e in elements]) def main(): openai_key = input('Enter OpenAI key: ') dataset_path = '../../datasets/transcribations/transcribations_2023-04-27 16:07:39_2023-07-25 19:03:21_polars.csv' n_clusters = 4 # Load calls and format to conversations # df = calls_to_converations(dataset_path, '2023-07-21', n_from=1, n_to=5) # df.to_csv('conversations.csv') # Load conversations df = pd.read_csv('conversations.csv') # Ada v2 $0.0001 / 1K tokens # df = get_embeddings(df, openai_key=openai_key) df = get_sentence_embeddings(df) df.to_csv("embeddings.csv") # Load embeddings # df = pd.read_csv('embeddings.csv') # df = pd.read_csv('local_conversations_embeddings.csv') # Reloading the original DataFrame from the CSV file # df = pd.read_csv('embeddings.csv') # Applying the custom conversion function to the 'embedding' column # df['embedding'] = df['embedding'].apply(convert_to_array) # Clustering df, matrix = clustering(df, n_clusters=n_clusters) # Summarize topics legend = topic_samples_central(df, matrix, openai_key=openai_key, n_clusters=n_clusters, rev_per_cluster=10) # Fake legend # legend = ['Topic 0', 'Topic 1', 'Topic 2', 'Topic 3'] # Plot clusters plot_clusters(df, matrix, legend_append_values=legend) logger.info('Done.') if __name__ == "__main__": main()
[ "Это фрагменты диалогов, восстановленных из разговоров колл центра. Эти фрагменты разговоров уже разделены на кластеры. Пожалуйста, дайте описание каждому кластеру так, что бы было ясно что его выделяет среди других кластеров. Ответ представьте в виде JSON структуры: {'Кластер 0': 'Тема 0', 'Кластер 1': 'Топик 1'}", "You are a helpful assistant.", "\nКластер PLACEHOLDER PLACEHOLDER:\nPLACEHOLDER" ]
2024-01-10
Diezalottt/PuntChat
puntchat.py
import openai import os import time ##################### # Configuration Section ##################### # Load the OpenAI API key from the environment variable for security openai.api_key = os.getenv("OPENAI_API_KEY") # Initialize the OpenAI client for beta functionality client = openai.Client() # Define the Assistant's ID for later usage assistant_id = "asst_N1P9Wv5HOiJRJKAekqs4dVP7" # Specify custom HTTP headers needed to access beta features headers = {"OpenAI-Beta": "assistants=v1"} # Specify the interval to wait between checks when polling for a response SLEEP_INTERVAL = 2 ##################### # Assistant Configuration ##################### # Create the Assistant with predefined configurations and capabilities assistant = client.beta.assistants.create( name="Reese Istor", instructions=( "As an authority in power electronics theory and design, your guidance is sought in addressing inquiries related to a variety of power applications. Please provide expert advice on design principles, theoretical understanding, troubleshooting techniques, safety protocols, and the latest technological developments in the field. Draw upon the information from the extensive range of books at your disposal to inform your responses." ), model="gpt-4-1106-preview", tools=[{"type": "code_interpreter"}, {"type": "retrieval"}] ) ##################### # Thread Configuration ##################### # Initialize a thread which represents a conversation between a user and the assistant thread = client.beta.threads.create( messages=[ {"role": "user", "content": "Please assist me with power electronics."} ] ) ##################### # Utility Functions Section ##################### def create_thread(): """ Create a new conversation thread for interacting with the assistant. """ return client.beta.threads.create() def add_message_to_thread(thread_id, content): """ Add a user's message to the conversation thread. """ return client.beta.threads.messages.create(thread_id=thread_id, role="user", content=content) def run_assistant_on_thread(thread_id, assistant_id): """ Trigger the assistant to process the messages in the thread. """ return client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id) def wait_for_run_completion(thread_id, run): """ Poll the API until the assistant run is complete and print the responses. """ while run.status not in ['completed', 'failed', 'cancelled', 'expired']: print("Waiting for Reese Istor to respond...") time.sleep(SLEEP_INTERVAL) run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id) if run.status == 'completed': print_assistant_responses(thread_id) else: print(f"The run did not complete successfully. Status: {run.status}") def print_assistant_responses(thread_id): """ Print the assistant's responses after the run is complete. """ messages = client.beta.threads.messages.list(thread_id=thread_id) for msg in messages.data: if msg.role == "assistant": print(msg.content) ##################### # Main Interaction Loop ##################### def main(): """ Run the main interaction loop allowing a user to ask questions to the assistant. """ print("Welcome to Reese Istor, your power electronics expert!") thread_id = thread.id # Access the ID attribute of the thread object while True: user_input = input("Ask Reese Istor a question about power electronics (or type 'exit' to quit): ") if user_input.lower() == 'exit': print("Thank you for using Reese Istor. Goodbye!") break add_message_to_thread(thread_id, user_input) run = run_assistant_on_thread(thread_id, assistant_id) wait_for_run_completion(thread_id, run) if __name__ == '__main__': main()
[ "Please assist me with power electronics." ]
2024-01-10
slavachalnev/NeuronLabel
neuronlabel~ask_gpt.py
import os import json import openai openai.api_key = os.environ["OAIKEY"] TASK_EXPLANATION = "Create a simple rule unifying the following snippets and determine if the rule \ explains most of the snippets (say 'EXPLAINS') or does not explain most of (80%) the snippets (say 'DOES NOT EXPLAIN'). If the rule is not simple, then say 'DOES NOT EXPLAIN' \n\n\ The text is split into HIGH: (the rule should explain these) and LOW: (the rule should not explain these). \ The snippets are separated by -----.\n\n\ The output should be of the form:\n\nRULE:\n[RULE]\nRESULT:\n[EXPLAINS/DOES NOT EXPLAIN]" def load_data_from_json(file_path): with open(file_path, "r") as f: data = json.load(f) return data def process_snippet(snippet): token_activation_pairs = snippet["token_activation_pairs"] max_activation = snippet["max_activation"] original_text = snippet["text"] # Normalize activations by dividing by max_activation normalized_activations = [(token, activation / max_activation) for token, activation in token_activation_pairs] # Split tokens into segments based on activation levels segments = [] current_segment = {"type": None, "tokens": []} for token, activation in normalized_activations: segment_type = "HIGH:" if activation >= 0.8 else "LOW:" if current_segment["type"] != segment_type: if current_segment["tokens"]: segments.append(current_segment) current_segment = {"type": segment_type, "tokens": []} current_segment["tokens"].append(token) if current_segment["tokens"]: segments.append(current_segment) processed_segments = "\n".join([f"{segment['type']}\n{''.join(segment['tokens'])}" for segment in segments]) return f"\n-----\nFULL TEXT:\n{original_text}\n{processed_segments}\n" def process_snippets_into_prompt(snippets): top_snippets = snippets[:10] processed_snippets = [process_snippet(snippet) for snippet in top_snippets] prompt = f"{TASK_EXPLANATION}\n\n{''.join(processed_snippets)}\n\nRULE:\n" return prompt def call_gpt_api(prompt): response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.5, ) return response.choices[0].text.strip() def main(): # json_file_path = "../solu-4l.json" json_file_path = "../gelu-4l.json" data = load_data_from_json(json_file_path) for neuron in data: snippets = neuron["snippets"] prompt = process_snippets_into_prompt(snippets) # print(prompt) gpt_response = call_gpt_api(prompt) print(f"Neuron {neuron['neuron_id']}:\n{gpt_response}") print() if __name__ == "__main__": main()
[ "DOES NOT EXPLAIN", "f\"{TASK_EXPLANATION}\\n\\n{''.join(processed_snippets)}\\n\\nRULE:\\n" ]